1 | using System.Collections.Generic;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
4 | using System.Linq;
|
---|
5 |
|
---|
6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
7 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
8 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
9 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
10 |
|
---|
11 | /// <summary>
|
---|
12 | /// Removes all occurrences of the first lit-type item in the top type vector.
|
---|
13 | /// </summary>
|
---|
14 | /// <typeparam name="T"></typeparam>
|
---|
15 | [StorableClass]
|
---|
16 | public abstract class VectorRemoveExpression<T> : StatelessExpression {
|
---|
17 | protected VectorRemoveExpression() { }
|
---|
18 | [StorableConstructor]
|
---|
19 | protected VectorRemoveExpression(bool deserializing) : base(deserializing) { }
|
---|
20 |
|
---|
21 | protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
22 | return vectorStack.IsEmpty || literalStack.IsEmpty;
|
---|
23 | }
|
---|
24 |
|
---|
25 | protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
26 | var literal = literalStack.Pop();
|
---|
27 | vectorStack.Top = vectorStack.Top.Where(x => !x.Equals(literal)).ToList();
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | [StorableClass]
|
---|
32 | [PushExpression(
|
---|
33 | StackTypes.IntegerVector,
|
---|
34 | "INTEGER[].REMOVE",
|
---|
35 | "Removes all occurrences of the top INTEGER in the top INTEGER[].",
|
---|
36 | StackTypes.Integer)]
|
---|
37 | public class IntegerVectorRemoveExpression : VectorRemoveExpression<long> {
|
---|
38 | public IntegerVectorRemoveExpression() { }
|
---|
39 | [StorableConstructor]
|
---|
40 | protected IntegerVectorRemoveExpression(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(
|
---|
53 | StackTypes.FloatVector,
|
---|
54 | "FLOAT[].REMOVE",
|
---|
55 | "Removes all occurrences of the top FLOAT in the top FLOAT[].",
|
---|
56 | StackTypes.Float)]
|
---|
57 | public class FloatVectorRemoveExpression : VectorRemoveExpression<double> {
|
---|
58 | public FloatVectorRemoveExpression() { }
|
---|
59 | [StorableConstructor]
|
---|
60 | protected FloatVectorRemoveExpression(bool deserializing) : base(deserializing) { }
|
---|
61 |
|
---|
62 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
63 | return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
67 | Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | [StorableClass]
|
---|
72 | [PushExpression(
|
---|
73 | StackTypes.BooleanVector,
|
---|
74 | "BOOLEAN[].REMOVE",
|
---|
75 | "Removes all occurrences of the top BOOLEAN in the top BOOLEAN[].",
|
---|
76 | StackTypes.Boolean)]
|
---|
77 | public class BooleanVectorRemoveExpression : VectorRemoveExpression<bool> {
|
---|
78 | public BooleanVectorRemoveExpression() { }
|
---|
79 | [StorableConstructor]
|
---|
80 | protected BooleanVectorRemoveExpression(bool deserializing) : base(deserializing) { }
|
---|
81 |
|
---|
82 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
83 | return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
87 | Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | [StorableClass]
|
---|
92 | [PushExpression(
|
---|
93 | StackTypes.StringVector,
|
---|
94 | "STRING[].REMOVE",
|
---|
95 | "Removes all occurrences of the top STRING in the top STRING[].",
|
---|
96 | StackTypes.String)]
|
---|
97 | public class StringVectorRemoveExpression : VectorRemoveExpression<string> {
|
---|
98 | public StringVectorRemoveExpression() { }
|
---|
99 | [StorableConstructor]
|
---|
100 | protected StringVectorRemoveExpression(bool deserializing) : base(deserializing) { }
|
---|
101 |
|
---|
102 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
103 | return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
104 | }
|
---|
105 |
|
---|
106 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
107 | Eval(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|