1 | using System.Collections.Generic;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
7 |
|
---|
8 | /// <summary>
|
---|
9 | /// Counts the occurrences of the top lit-type item in the top type vector.
|
---|
10 | /// </summary>
|
---|
11 | /// <typeparam name="T"></typeparam>
|
---|
12 | public abstract class VectorOccurrenceOfExpression<T> : StatelessExpression {
|
---|
13 | protected bool Eval(
|
---|
14 | IInternalPushInterpreter interpreter,
|
---|
15 | IPushStack<List<T>> vectorStack,
|
---|
16 | IPushStack<T> literalStack,
|
---|
17 | bool canOverride = false) {
|
---|
18 | if (vectorStack.IsEmpty || literalStack.IsEmpty)
|
---|
19 | return false;
|
---|
20 |
|
---|
21 | var vector = vectorStack.Pop();
|
---|
22 | var literal = canOverride ? literalStack.Top : literalStack.Pop();
|
---|
23 |
|
---|
24 | var occurrence = 0;
|
---|
25 | for (var i = 0; i < vector.Count; i++) {
|
---|
26 | if (vector[i].Equals(literal)) occurrence++;
|
---|
27 | }
|
---|
28 |
|
---|
29 | if (canOverride) interpreter.IntegerStack.SetTop(occurrence);
|
---|
30 | else interpreter.IntegerStack.Push(occurrence);
|
---|
31 | return true;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].OCCURRENCEOF", StackTypes.Integer)]
|
---|
36 | public class IntegerVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<long> {
|
---|
37 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
38 | return Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [PushExpression(StackTypes.FloatVector, "FLOAT[].OCCURRENCEOF", StackTypes.Float | StackTypes.Integer)]
|
---|
43 | public class FloatVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<double> {
|
---|
44 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
45 | return Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].OCCURRENCEOF", StackTypes.Boolean | StackTypes.Integer)]
|
---|
50 | public class BooleanVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<bool> {
|
---|
51 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
52 | return Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | [PushExpression(StackTypes.StringVector, "STRING[].OCCURRENCEOF", StackTypes.String | StackTypes.Integer)]
|
---|
57 | public class StringVectorOccurrenceOfExpression : VectorOccurrenceOfExpression<string> {
|
---|
58 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
59 | return Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|