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 occurrence 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(
|
---|
47 | StackTypes.IntegerVector,
|
---|
48 | "INTEGER[].REPLACEFIRST",
|
---|
49 | "Replace first occurrence of the second INTEGER with the INTEGER in the top INTEGER[].",
|
---|
50 | StackTypes.Integer)]
|
---|
51 | public class IntegerVectorReplaceFirstExpression : VectorReplaceFirstExpression<long> {
|
---|
52 | public IntegerVectorReplaceFirstExpression() { }
|
---|
53 | [StorableConstructor]
|
---|
54 | protected IntegerVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
55 |
|
---|
56 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
57 | return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
61 | Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableClass]
|
---|
66 | [PushExpression(
|
---|
67 | StackTypes.FloatVector,
|
---|
68 | "FLOAT[].REPLACEFIRST",
|
---|
69 | "Replace first occurrence of the second FLOAT with the FLOAT in the top FLOAT[].",
|
---|
70 | StackTypes.Float)]
|
---|
71 | public class FloatVectorReplaceFirstExpression : VectorReplaceFirstExpression<double> {
|
---|
72 | public FloatVectorReplaceFirstExpression() { }
|
---|
73 | [StorableConstructor]
|
---|
74 | protected FloatVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
75 |
|
---|
76 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
77 | return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
81 | Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | [StorableClass]
|
---|
86 | [PushExpression(
|
---|
87 | StackTypes.BooleanVector,
|
---|
88 | "BOOLEAN[].REPLACEFIRST",
|
---|
89 | "Replace first occurrence of the second BOOLEAN with the BOOLEAN in the top BOOLEAN[].",
|
---|
90 | StackTypes.Boolean)]
|
---|
91 | public class BooleanVectorReplaceFirstExpression : VectorReplaceFirstExpression<bool> {
|
---|
92 | public BooleanVectorReplaceFirstExpression() { }
|
---|
93 | [StorableConstructor]
|
---|
94 | protected BooleanVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
95 |
|
---|
96 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
97 | return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
98 | }
|
---|
99 |
|
---|
100 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
101 | Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | [StorableClass]
|
---|
106 | [PushExpression(
|
---|
107 | StackTypes.StringVector,
|
---|
108 | "STRING[].REPLACEFIRST",
|
---|
109 | "Replace first occurrence of the second STRING with the STRING in the top STRING[].",
|
---|
110 | StackTypes.String)]
|
---|
111 | public class StringVectorReplaceFirstExpression : VectorReplaceFirstExpression<string> {
|
---|
112 | public StringVectorReplaceFirstExpression() { }
|
---|
113 | [StorableConstructor]
|
---|
114 | protected StringVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
115 |
|
---|
116 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
117 | return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
121 | Eval(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|