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; /// /// Replaces, in the top type vector, item at index(from integer stack) with the first lit-type item. /// /// [StorableClass] public abstract class VectorSetExpression : StatelessExpression { protected VectorSetExpression() { } [StorableConstructor] protected VectorSetExpression(bool deserializing) : base(deserializing) { } protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack> vectorStack, IPushStack literalStack, bool isLiteralTypeInteger = false) { return vectorStack.IsEmpty || vectorStack.Top.Count == 0 || literalStack.IsEmpty || (isLiteralTypeInteger && interpreter.IntegerStack.Count < 2) || (!isLiteralTypeInteger && interpreter.IntegerStack.IsEmpty); } protected void Eval( IInternalPushInterpreter interpreter, IPushStack> vectorStack, IPushStack literalStack, bool isLiteralTypeInteger = false) { T literal; int index; if (isLiteralTypeInteger) { literal = literalStack.Top; index = (int)interpreter.IntegerStack[1]; interpreter.IntegerStack.Remove(2); } else { literal = literalStack.Pop(); index = (int)interpreter.IntegerStack.Pop(); } var vector = vectorStack.Pop(); index = index % vector.Count; if (index < 0) index *= -1; vector[index] = literal; } } [StorableClass] [PushExpression(StackTypes.IntegerVector, "INTEGER[].SET", StackTypes.Integer)] public class IntegerVectorSetExpression : VectorSetExpression { public IntegerVectorSetExpression() { } [StorableConstructor] protected IntegerVectorSetExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true); } } [StorableClass] [PushExpression(StackTypes.FloatVector, "FLOAT[].SET", StackTypes.Float | StackTypes.Integer)] public class FloatVectorSetExpression : VectorSetExpression { public FloatVectorSetExpression() { } [StorableConstructor] protected FloatVectorSetExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack); } } [StorableClass] [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].SET", StackTypes.Boolean | StackTypes.Integer)] public class BooleanVectorSetExpression : VectorSetExpression { public BooleanVectorSetExpression() { } [StorableConstructor] protected BooleanVectorSetExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack); } } [StorableClass] [PushExpression(StackTypes.StringVector, "STRING[].SET", StackTypes.String | StackTypes.Integer)] public class StringVectorSetExpression : VectorSetExpression { public StringVectorSetExpression() { } [StorableConstructor] protected StringVectorSetExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.StringVectorStack, interpreter.StringStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack); } } }