[14834] | 1 | using System.Collections.Generic;
|
---|
| 2 |
|
---|
| 3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
[14952] | 4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[14834] | 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>
|
---|
[14952] | 13 | [StorableClass]
|
---|
[14834] | 14 | public abstract class VectorSetExpression<T> : StatelessExpression {
|
---|
[14952] | 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(
|
---|
[14834] | 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;
|
---|
[14875] | 37 | index = (int)interpreter.IntegerStack[1];
|
---|
[14834] | 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)
|
---|
[14909] | 48 | index *= -1;
|
---|
[14834] | 49 |
|
---|
| 50 | vector[index] = literal;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
[14952] | 54 | [StorableClass]
|
---|
[14834] | 55 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].SET", StackTypes.Integer)]
|
---|
| 56 | public class IntegerVectorSetExpression : VectorSetExpression<long> {
|
---|
[14952] | 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);
|
---|
[14834] | 63 | }
|
---|
[14952] | 64 |
|
---|
| 65 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
| 66 | Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
|
---|
| 67 | }
|
---|
[14834] | 68 | }
|
---|
| 69 |
|
---|
[14952] | 70 | [StorableClass]
|
---|
[14834] | 71 | [PushExpression(StackTypes.FloatVector, "FLOAT[].SET", StackTypes.Float | StackTypes.Integer)]
|
---|
| 72 | public class FloatVectorSetExpression : VectorSetExpression<double> {
|
---|
[14952] | 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);
|
---|
[14834] | 79 | }
|
---|
[14952] | 80 |
|
---|
| 81 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
| 82 | Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
| 83 | }
|
---|
[14834] | 84 | }
|
---|
| 85 |
|
---|
[14952] | 86 | [StorableClass]
|
---|
[14834] | 87 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].SET", StackTypes.Boolean | StackTypes.Integer)]
|
---|
| 88 | public class BooleanVectorSetExpression : VectorSetExpression<bool> {
|
---|
[14952] | 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);
|
---|
[14834] | 95 | }
|
---|
[14952] | 96 |
|
---|
| 97 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
| 98 | Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
| 99 | }
|
---|
[14834] | 100 | }
|
---|
| 101 |
|
---|
[14952] | 102 | [StorableClass]
|
---|
[14834] | 103 | [PushExpression(StackTypes.StringVector, "STRING[].SET", StackTypes.String | StackTypes.Integer)]
|
---|
| 104 | public class StringVectorSetExpression : VectorSetExpression<string> {
|
---|
[14952] | 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);
|
---|
[14834] | 111 | }
|
---|
[14952] | 112 |
|
---|
| 113 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
| 114 | Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
|
---|
| 115 | }
|
---|
[14834] | 116 | }
|
---|
| 117 | }
|
---|