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 all 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 VectorReplaceExpression<T> : StatelessExpression {
|
---|
15 | protected VectorReplaceExpression() { }
|
---|
16 | [StorableConstructor]
|
---|
17 | protected VectorReplaceExpression(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 | literalStack.Remove(2);
|
---|
28 |
|
---|
29 | for (var i = 0; i < vector.Count; i++) {
|
---|
30 | if (vector[i].Equals(second))
|
---|
31 | vector[i] = first;
|
---|
32 | }
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | [StorableClass]
|
---|
37 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].REPLACE", StackTypes.Integer)]
|
---|
38 | public class IntegerVectorReplaceExpression : VectorReplaceExpression<long> {
|
---|
39 | public IntegerVectorReplaceExpression() { }
|
---|
40 | [StorableConstructor]
|
---|
41 | protected IntegerVectorReplaceExpression(bool deserializing) : base(deserializing) { }
|
---|
42 |
|
---|
43 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
44 | return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
48 | Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [StorableClass]
|
---|
53 | [PushExpression(StackTypes.FloatVector, "FLOAT[].REPLACE", StackTypes.Float)]
|
---|
54 | public class FloatVectorReplaceExpression : VectorReplaceExpression<double> {
|
---|
55 | public FloatVectorReplaceExpression() { }
|
---|
56 | [StorableConstructor]
|
---|
57 | protected FloatVectorReplaceExpression(bool deserializing) : base(deserializing) { }
|
---|
58 |
|
---|
59 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
60 | return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
64 | Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [StorableClass]
|
---|
69 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].REPLACE", StackTypes.Boolean)]
|
---|
70 | public class BooleanVectorReplaceExpression : VectorReplaceExpression<bool> {
|
---|
71 | public BooleanVectorReplaceExpression() { }
|
---|
72 | [StorableConstructor]
|
---|
73 | protected BooleanVectorReplaceExpression(bool deserializing) : base(deserializing) { }
|
---|
74 |
|
---|
75 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
76 | return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
80 | Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | [StorableClass]
|
---|
85 | [PushExpression(StackTypes.StringVector, "STRING[].REPLACE", StackTypes.String)]
|
---|
86 | public class StringVectorReplaceExpression : VectorReplaceExpression<string> {
|
---|
87 | public StringVectorReplaceExpression() { }
|
---|
88 | [StorableConstructor]
|
---|
89 | protected StringVectorReplaceExpression(bool deserializing) : base(deserializing) { }
|
---|
90 |
|
---|
91 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
92 | return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
96 | Eval(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|