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.Extensions; 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; long x; if (isLiteralTypeInteger) { literal = literalStack.Top; x = interpreter.IntegerStack[1]; interpreter.IntegerStack.Remove(2); } else { literal = literalStack.Pop(); x = interpreter.IntegerStack.Pop(); } var vector = vectorStack.Top; var newTop = new List(vector); var index = x.AsInt(vector.Count); if (index < 0) index *= -1; newTop[index] = literal; vectorStack.Top = newTop; } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].SET", "Replaces in the top INTEGER[] item at index (from INTEGER stack) with the first INTEGER item.", 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", "Replaces in the top FLOAT[] item at index (from INTEGER stack) with the first FLOAT item.", 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", "Replaces in the top BOOLEAN[] item at index (from INTEGER stack) with the first BOOLEAN item.", 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", "Replaces in the top STRING[] item at index (from INTEGER stack) with the first STRING item.", 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); } } }