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