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 | /// Tells whether the top lit-type item is in the top type vector.
|
---|
13 | /// </summary>
|
---|
14 | /// <typeparam name="T"></typeparam>
|
---|
15 | [StorableClass]
|
---|
16 | public abstract class VectorContainsExpression<T> : StatelessExpression {
|
---|
17 | protected VectorContainsExpression() { }
|
---|
18 | [StorableConstructor]
|
---|
19 | protected VectorContainsExpression(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(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack, bool canOverride = false) {
|
---|
26 | var vector = vectorStack.Pop();
|
---|
27 | var literal = canOverride ? literalStack.Top : literalStack.Pop();
|
---|
28 | var contains = vector.Contains(literal);
|
---|
29 |
|
---|
30 | if (canOverride) interpreter.BooleanStack.Top = contains;
|
---|
31 | else interpreter.BooleanStack.Push(contains);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [StorableClass]
|
---|
36 | [PushExpression(
|
---|
37 | StackTypes.IntegerVector,
|
---|
38 | "INTEGER[].CONTAINS",
|
---|
39 | "Tells whether the top INTEGER is in the top INTEGER[].",
|
---|
40 | StackTypes.Integer | StackTypes.Boolean)]
|
---|
41 | public class IntegerVectorContainsExpression : VectorContainsExpression<long> {
|
---|
42 | public IntegerVectorContainsExpression() { }
|
---|
43 | [StorableConstructor]
|
---|
44 | protected IntegerVectorContainsExpression(bool deserializing) : base(deserializing) { }
|
---|
45 |
|
---|
46 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
47 | return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
51 | Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | [StorableClass]
|
---|
56 | [PushExpression(
|
---|
57 | StackTypes.FloatVector,
|
---|
58 | "FLOAT[].CONTAINS",
|
---|
59 | "Tells whether the top FLOAT is in the top FLOAT[].",
|
---|
60 | StackTypes.Float | StackTypes.Boolean)]
|
---|
61 | public class FloatVectorContainsExpression : VectorContainsExpression<double> {
|
---|
62 | public FloatVectorContainsExpression() { }
|
---|
63 | [StorableConstructor]
|
---|
64 | protected FloatVectorContainsExpression(bool deserializing) : base(deserializing) { }
|
---|
65 |
|
---|
66 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
67 | return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
71 | Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | [StorableClass]
|
---|
76 | [PushExpression(
|
---|
77 | StackTypes.BooleanVector,
|
---|
78 | "BOOLEAN[].CONTAINS",
|
---|
79 | "Tells whether the top BOOLEAN is in the top BOOLEAN[].",
|
---|
80 | StackTypes.Boolean)]
|
---|
81 | public class BooleanVectorContainsExpression : VectorContainsExpression<bool> {
|
---|
82 | public BooleanVectorContainsExpression() { }
|
---|
83 | [StorableConstructor]
|
---|
84 | protected BooleanVectorContainsExpression(bool deserializing) : base(deserializing) { }
|
---|
85 |
|
---|
86 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
87 | return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
91 | Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack, true);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | [StorableClass]
|
---|
96 | [PushExpression(
|
---|
97 | StackTypes.StringVector,
|
---|
98 | "STRING[].CONTAINS",
|
---|
99 | "Tells whether the top STRING is in the top STRING[].",
|
---|
100 | StackTypes.String | StackTypes.Boolean)]
|
---|
101 | public class StringVectorContainsExpression : VectorContainsExpression<string> {
|
---|
102 | public StringVectorContainsExpression() { }
|
---|
103 | [StorableConstructor]
|
---|
104 | protected StringVectorContainsExpression(bool deserializing) : base(deserializing) { }
|
---|
105 |
|
---|
106 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
107 | return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
108 | }
|
---|
109 |
|
---|
110 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
111 | Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | }
|
---|