1 | using System.Collections.Generic;
|
---|
2 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
5 | /// <summary>
|
---|
6 | /// Counts the occurrences of the top lit-type item in the top type vector.
|
---|
7 | /// </summary>
|
---|
8 | /// <typeparam name="T"></typeparam>
|
---|
9 | [StorableClass]
|
---|
10 | public abstract class VectorOccurrenceOfExpression<T> : StatelessExpression {
|
---|
11 | protected VectorOccurrenceOfExpression() { }
|
---|
12 | [StorableConstructor]
|
---|
13 | protected VectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
|
---|
14 |
|
---|
15 | protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
16 | return vectorStack.IsEmpty || literalStack.IsEmpty;
|
---|
17 | }
|
---|
18 |
|
---|
19 | protected void Eval(
|
---|
20 | IInternalPushInterpreter interpreter,
|
---|
21 | IPushStack<IReadOnlyList<T>> vectorStack,
|
---|
22 | IPushStack<T> literalStack,
|
---|
23 | bool canOverride = false) {
|
---|
24 | var vector = vectorStack.Pop();
|
---|
25 | var literal = canOverride ? literalStack.Top : literalStack.Pop();
|
---|
26 |
|
---|
27 | var occurrence = 0;
|
---|
28 | for (var i = 0; i < vector.Count; i++) {
|
---|
29 | if (vector[i].Equals(literal)) occurrence++;
|
---|
30 | }
|
---|
31 |
|
---|
32 | if (canOverride) interpreter.IntegerStack.Top = occurrence;
|
---|
33 | else interpreter.IntegerStack.Push(occurrence);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | [StorableClass]
|
---|
38 | [PushExpression(
|
---|
39 | StackTypes.IntegerVector,
|
---|
40 | "INTEGER[].OCCURRENCEOF",
|
---|
41 | "Pushes the amount of occurrences of the top INTEGER in the top INTEGER[] onto the INTEGER stack.",
|
---|
42 | 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(
|
---|
59 | StackTypes.FloatVector,
|
---|
60 | "FLOAT[].OCCURRENCEOF",
|
---|
61 | "Pushes the amount of occurrences of the top FLOAT in the top FLOAT[] onto the INTEGER stack.",
|
---|
62 | StackTypes.Float | StackTypes.Integer)]
|
---|
63 | public class FloatVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<double> {
|
---|
64 | public FloatVectorOccurrenceOfExpression() { }
|
---|
65 | [StorableConstructor]
|
---|
66 | protected FloatVectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
|
---|
67 |
|
---|
68 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
69 | return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
73 | Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | [StorableClass]
|
---|
78 | [PushExpression(
|
---|
79 | StackTypes.BooleanVector,
|
---|
80 | "BOOLEAN[].OCCURRENCEOF",
|
---|
81 | "Pushes the amount of occurrences of the top BOOLEAN in the top BOOLEAN[] onto the INTEGER stack.",
|
---|
82 | StackTypes.Boolean | StackTypes.Integer)]
|
---|
83 | public class BooleanVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<bool> {
|
---|
84 | public BooleanVectorOccurrenceOfExpression() { }
|
---|
85 | [StorableConstructor]
|
---|
86 | protected BooleanVectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
|
---|
87 |
|
---|
88 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
89 | return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
93 | Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | [StorableClass]
|
---|
98 | [PushExpression(
|
---|
99 | StackTypes.StringVector,
|
---|
100 | "STRING[].OCCURRENCEOF",
|
---|
101 | "Pushes the amount of occurrences of the top STRING in the top STRING[] onto the INTEGER stack.",
|
---|
102 | StackTypes.String | StackTypes.Integer)]
|
---|
103 | public class StringVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<string> {
|
---|
104 | public StringVectorOccurrenceOfExpression() { }
|
---|
105 | [StorableConstructor]
|
---|
106 | protected StringVectorOccurrenceOfExpression(bool deserializing) : base(deserializing) { }
|
---|
107 |
|
---|
108 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
109 | return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
110 | }
|
---|
111 |
|
---|
112 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
113 | Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | }
|
---|