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<List<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
20 | return vectorStack.IsEmpty || literalStack.Count < 2;
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected void Eval(IPushStack<List<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
24 | var vector = vectorStack.Top;
|
---|
25 | var first = literalStack.Top;
|
---|
26 | var second = literalStack[1];
|
---|
27 | var index = vector.IndexOf(second);
|
---|
28 | literalStack.Remove(2);
|
---|
29 |
|
---|
30 | if (index >= 0)
|
---|
31 | vector[index] = first;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [StorableClass]
|
---|
36 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].REPLACEFIRST", StackTypes.Integer)]
|
---|
37 | public class IntegerVectorReplaceFirstExpression : VectorReplaceFirstExpression<long> {
|
---|
38 | public IntegerVectorReplaceFirstExpression() { }
|
---|
39 | [StorableConstructor]
|
---|
40 | protected IntegerVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
41 |
|
---|
42 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
43 | return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
47 | Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [StorableClass]
|
---|
52 | [PushExpression(StackTypes.FloatVector, "FLOAT[].REPLACEFIRST", StackTypes.Float)]
|
---|
53 | public class FloatVectorReplaceFirstExpression : VectorReplaceFirstExpression<double> {
|
---|
54 | public FloatVectorReplaceFirstExpression() { }
|
---|
55 | [StorableConstructor]
|
---|
56 | protected FloatVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
57 |
|
---|
58 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
59 | return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
63 | Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | [StorableClass]
|
---|
68 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].REPLACEFIRST", StackTypes.Boolean)]
|
---|
69 | public class BooleanVectorReplaceFirstExpression : VectorReplaceFirstExpression<bool> {
|
---|
70 | public BooleanVectorReplaceFirstExpression() { }
|
---|
71 | [StorableConstructor]
|
---|
72 | protected BooleanVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
73 |
|
---|
74 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
75 | return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
79 | Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [StorableClass]
|
---|
84 | [PushExpression(StackTypes.StringVector, "STRING[].REPLACEFIRST", StackTypes.String)]
|
---|
85 | public class StringVectorReplaceFirstExpression : VectorReplaceFirstExpression<string> {
|
---|
86 | public StringVectorReplaceFirstExpression() { }
|
---|
87 | [StorableConstructor]
|
---|
88 | protected StringVectorReplaceFirstExpression(bool deserializing) : base(deserializing) { }
|
---|
89 |
|
---|
90 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
91 | return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
95 | Eval(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|