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