1 | using System.Collections.Generic;
|
---|
2 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
5 | /// <summary>
|
---|
6 | /// Gets the nth item from the type stack
|
---|
7 | /// </summary>
|
---|
8 | /// <typeparam name="T"></typeparam>
|
---|
9 | [StorableClass]
|
---|
10 | public abstract class VectorNthExpression<T> : StatelessExpression {
|
---|
11 | protected VectorNthExpression() { }
|
---|
12 | [StorableConstructor]
|
---|
13 | protected VectorNthExpression(bool deserializing) : base(deserializing) { }
|
---|
14 |
|
---|
15 | protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
|
---|
16 | return vectorStack.IsEmpty ||
|
---|
17 | vectorStack.Top.Count == 0 ||
|
---|
18 | interpreter.IntegerStack.IsEmpty;
|
---|
19 | }
|
---|
20 |
|
---|
21 | protected void Eval(
|
---|
22 | IInternalPushInterpreter interpreter,
|
---|
23 | IPushStack<IReadOnlyList<T>> vectorStack,
|
---|
24 | IPushStack<T> literalStack) {
|
---|
25 | var vector = vectorStack.Pop();
|
---|
26 |
|
---|
27 | if (literalStack == interpreter.IntegerStack) {
|
---|
28 | var n = interpreter.IntegerStack.Top.AsInt(vector.Count);
|
---|
29 | literalStack.Top = vector[n];
|
---|
30 | } else {
|
---|
31 | var n = interpreter.IntegerStack.Pop().AsInt(vector.Count);
|
---|
32 | literalStack.Push(vector[n]);
|
---|
33 | }
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | [StorableClass]
|
---|
38 | [PushExpression(
|
---|
39 | StackTypes.IntegerVector,
|
---|
40 | "INTEGER[].NTH",
|
---|
41 | "Pushes the nth item from the top INTEGER[] onto the INTEGER stack, whereby n is taken from the INTEGER stack.",
|
---|
42 | StackTypes.Integer)]
|
---|
43 | public class IntegerVectorNthExpression : VectorNthExpression<long> {
|
---|
44 | public IntegerVectorNthExpression() { }
|
---|
45 | [StorableConstructor]
|
---|
46 | protected IntegerVectorNthExpression(bool deserializing) : base(deserializing) { }
|
---|
47 |
|
---|
48 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
49 | return IsNoop(interpreter, interpreter.IntegerVectorStack);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
53 | Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | [StorableClass]
|
---|
58 | [PushExpression(
|
---|
59 | StackTypes.FloatVector,
|
---|
60 | "FLOAT[].NTH",
|
---|
61 | "Pushes the nth item from the top FLOAT[] onto the FLOAT stack, whereby n is taken from the INTEGER stack.",
|
---|
62 | StackTypes.Float | StackTypes.Integer)]
|
---|
63 | public class FloatVectorNthExpression : VectorNthExpression<double> {
|
---|
64 | public FloatVectorNthExpression() { }
|
---|
65 | [StorableConstructor]
|
---|
66 | protected FloatVectorNthExpression(bool deserializing) : base(deserializing) { }
|
---|
67 |
|
---|
68 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
69 | return IsNoop(interpreter, interpreter.FloatVectorStack);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
73 | Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | [StorableClass]
|
---|
78 | [PushExpression(
|
---|
79 | StackTypes.BooleanVector,
|
---|
80 | "BOOLEAN[].NTH",
|
---|
81 | "Pushes the nth item from the top BOOLEAN[] onto the BOOLEAN stack, whereby n is taken from the INTEGER stack.",
|
---|
82 | StackTypes.Boolean | StackTypes.Integer)]
|
---|
83 | public class BooleanVectorNthExpression : VectorNthExpression<bool> {
|
---|
84 | public BooleanVectorNthExpression() { }
|
---|
85 | [StorableConstructor]
|
---|
86 | protected BooleanVectorNthExpression(bool deserializing) : base(deserializing) { }
|
---|
87 |
|
---|
88 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
89 | return IsNoop(interpreter, interpreter.BooleanVectorStack);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
93 | Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | [StorableClass]
|
---|
98 | [PushExpression(
|
---|
99 | StackTypes.StringVector,
|
---|
100 | "STRING[].NTH",
|
---|
101 | "Pushes the nth item from the top STRING[] onto the BOOLEAN stack, whereby n is taken from the INTEGER stack.",
|
---|
102 | StackTypes.String | StackTypes.Integer)]
|
---|
103 | public class StringVectorNthExpression : VectorNthExpression<string> {
|
---|
104 | public StringVectorNthExpression() { }
|
---|
105 | [StorableConstructor]
|
---|
106 | protected StringVectorNthExpression(bool deserializing) : base(deserializing) { }
|
---|
107 |
|
---|
108 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
109 | return IsNoop(interpreter, interpreter.StringVectorStack);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
113 | Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|