using System.Collections.Generic; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; /// /// Finds the index of the top lit-type item in the top type vector. /// /// [StorableClass] public abstract class VectorIndexOfExpression : StatelessExpression { protected VectorIndexOfExpression() { } [StorableConstructor] protected VectorIndexOfExpression(bool deserializing) : base(deserializing) { } protected bool IsNoop(IPushStack> vectorStack, IPushStack literalStack) { return vectorStack.IsEmpty || literalStack.IsEmpty; } protected void Eval( IInternalPushInterpreter interpreter, IPushStack> vectorStack, IPushStack literalStack, bool canOverride = false) { var vector = vectorStack.Pop(); var literal = canOverride ? literalStack.Top : literalStack.Pop(); var index = -1; for (var i = 0; i < vector.Count && index < 0; i++) { if (vector[i].Equals(literal)) { index = i; break; } } if (canOverride) interpreter.IntegerStack.Top = index; else interpreter.IntegerStack.Push(index); } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].INDEXOF", "Pushes the index of the top INTEGER in the top INTEGER[] onto the INTEGER stack.", StackTypes.Integer)] public class IntegerVectorIndexOfExpression : VectorIndexOfExpression { public IntegerVectorIndexOfExpression() { } [StorableConstructor] protected IntegerVectorIndexOfExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true); } } [StorableClass] [PushExpression( StackTypes.FloatVector, "FLOAT[].INDEXOF", "Pushes the index of the top FLOAT in the top FLOAT[] onto the INTEGER stack.", StackTypes.Float | StackTypes.Integer)] public class FloatVectorIndexOfExpression : VectorIndexOfExpression { public FloatVectorIndexOfExpression() { } [StorableConstructor] protected FloatVectorIndexOfExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack); } } [StorableClass] [PushExpression( StackTypes.BooleanVector, "BOOLEAN[].INDEXOF", "Pushes the index of the top BOOLEAN in the top BOOLEAN[] onto the INTEGER stack.", StackTypes.Boolean | StackTypes.Integer)] public class BooleanVectorIndexOfExpression : VectorIndexOfExpression { public BooleanVectorIndexOfExpression() { } [StorableConstructor] protected BooleanVectorIndexOfExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack); } } [StorableClass] [PushExpression( StackTypes.StringVector, "STRING[].INDEXOF", "Pushes the index of the top STRING in the top STRING[] onto the INTEGER stack.", StackTypes.String | StackTypes.Integer)] public class StringVectorIndexOfExpression : VectorIndexOfExpression { public StringVectorIndexOfExpression() { } [StorableConstructor] protected StringVectorIndexOfExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.StringVectorStack, interpreter.StringStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack); } } }