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 | /// Finds the index of the top lit-type item in the top type vector.
|
---|
10 | /// </summary>
|
---|
11 | /// <typeparam name="T"></typeparam>
|
---|
12 | public abstract class VectorIndexOfExpression<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 | var result = vector.IndexOf(literal);
|
---|
24 |
|
---|
25 | if (canOverride) interpreter.IntegerStack.SetTop(result);
|
---|
26 | else interpreter.IntegerStack.Push(result);
|
---|
27 | return true;
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].INDEXOF", StackTypes.Integer)]
|
---|
32 | public class IntegerVectorIndexOfExpression : VectorIndexOfExpression<long> {
|
---|
33 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
34 | return Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [PushExpression(StackTypes.FloatVector, "FLOAT[].INDEXOF", StackTypes.Float | StackTypes.Integer)]
|
---|
39 | public class FloatVectorIndexOfExpression : VectorIndexOfExpression<double> {
|
---|
40 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
41 | return Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].INDEXOF", StackTypes.Boolean | StackTypes.Integer)]
|
---|
46 | public class BooleanVectorIndexOfExpression : VectorIndexOfExpression<bool> {
|
---|
47 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
48 | return Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [PushExpression(StackTypes.StringVector, "STRING[].INDEXOF", StackTypes.String | StackTypes.String)]
|
---|
53 | public class StringVectorIndexOfExpression : VectorIndexOfExpression<string> {
|
---|
54 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
55 | return Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|