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