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 | /// Replace first occurrences of the second lit-type item with the first lit-type item in the top type vector.
|
---|
11 | /// </summary>
|
---|
12 | /// <typeparam name="T"></typeparam>
|
---|
13 | [StorableClass]
|
---|
14 | public abstract class VectorReplaceFirstExpression<T> : StatelessExpression {
|
---|
15 | protected VectorReplaceFirstExpression() { }
|
---|
16 | [StorableConstructor]
|
---|
17 | protected VectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
18 |
|
---|
19 | protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
20 | return vectorStack.IsEmpty || literalStack.Count < 2;
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
24 | var vector = vectorStack.Top;
|
---|
25 | var first = literalStack.Top;
|
---|
26 | var second = literalStack[1];
|
---|
27 | literalStack.Remove(2);
|
---|
28 |
|
---|
29 | var index = -1;
|
---|
30 | for (var i = 0; i < vector.Count && index < 0; i++) {
|
---|
31 | if (vector[i].Equals(second)) {
|
---|
32 | index = i;
|
---|
33 | break;
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | if (index >= 0) {
|
---|
38 | var result = new List<T>(vector);
|
---|
39 | result[index] = first;
|
---|
40 | vectorStack.Top = result;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableClass]
|
---|
46 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].REPLACEFIRST", StackTypes.Integer)]
|
---|
47 | public class IntegerVectorReplaceFirstExpression : VectorReplaceFirstExpression<long> {
|
---|
48 | public IntegerVectorReplaceFirstExpression() { }
|
---|
49 | [StorableConstructor]
|
---|
50 | protected IntegerVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
51 |
|
---|
52 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
53 | return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
57 | Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [StorableClass]
|
---|
62 | [PushExpression(StackTypes.FloatVector, "FLOAT[].REPLACEFIRST", StackTypes.Float)]
|
---|
63 | public class FloatVectorReplaceFirstExpression : VectorReplaceFirstExpression<double> {
|
---|
64 | public FloatVectorReplaceFirstExpression() { }
|
---|
65 | [StorableConstructor]
|
---|
66 | protected FloatVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
67 |
|
---|
68 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
69 | return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
73 | Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | [StorableClass]
|
---|
78 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].REPLACEFIRST", StackTypes.Boolean)]
|
---|
79 | public class BooleanVectorReplaceFirstExpression : VectorReplaceFirstExpression<bool> {
|
---|
80 | public BooleanVectorReplaceFirstExpression() { }
|
---|
81 | [StorableConstructor]
|
---|
82 | protected BooleanVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
83 |
|
---|
84 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
85 | return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
89 | Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | [StorableClass]
|
---|
94 | [PushExpression(StackTypes.StringVector, "STRING[].REPLACEFIRST", StackTypes.String)]
|
---|
95 | public class StringVectorReplaceFirstExpression : VectorReplaceFirstExpression<string> {
|
---|
96 | public StringVectorReplaceFirstExpression() { }
|
---|
97 | [StorableConstructor]
|
---|
98 | protected StringVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
99 |
|
---|
100 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
101 | return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
102 | }
|
---|
103 |
|
---|
104 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
105 | Eval(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|