using System.Collections.Generic; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using System; using System.Linq; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; /// /// Takes the subvec of the top item on the type stack. /// /// [StorableClass] public abstract class VectorSubExpression : StatelessExpression { protected VectorSubExpression() { } [StorableConstructor] protected VectorSubExpression(bool deserializing) : base(deserializing) { } protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack> vectorStack) { return vectorStack.IsEmpty || interpreter.IntegerStack.Count < 2; } protected void Eval(IInternalPushInterpreter interpreter, IPushStack> vectorStack) { var first = (int)Math.Min(vectorStack.Top.Count, Math.Max(0, interpreter.IntegerStack[1])); var second = (int)Math.Min(vectorStack.Top.Count, Math.Max(first, interpreter.IntegerStack.Top)); interpreter.IntegerStack.Remove(2); if (vectorStack.Top.Count == 0) return; var vector = vectorStack.Top; var list = vector as List; var lenght = second - first; if (list != null) { var newTop = new T[lenght]; list.CopyTo(first, newTop, 0, lenght); vectorStack.Top = newTop; return; } var array = vector as T[]; if (array != null) { var newTop = new T[lenght]; Array.Copy(array, first, newTop, 0, lenght); vectorStack.Top = newTop; return; } vectorStack.Top = vector.Skip(first).Take(lenght).ToList(); } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].SUB", "Pushes the sub vector of the top INTEGER[].", StackTypes.Integer)] public class IntegerVectorSubExpression : VectorSubExpression { public IntegerVectorSubExpression() { } [StorableConstructor] protected IntegerVectorSubExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.IntegerVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.IntegerVectorStack); } } [StorableClass] [PushExpression( StackTypes.FloatVector, "FLOAT[].SUB", "Pushes the sub vector of the top FLOAT[].", StackTypes.Integer)] public class FloatVectorSubExpression : VectorSubExpression { public FloatVectorSubExpression() { } [StorableConstructor] protected FloatVectorSubExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.FloatVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.FloatVectorStack); } } [StorableClass] [PushExpression( StackTypes.BooleanVector, "BOOLEAN[].SUB", "Pushes the sub vector of the top BOOLEAN[].", StackTypes.Integer)] public class BooleanVectorSubExpression : VectorSubExpression { public BooleanVectorSubExpression() { } [StorableConstructor] protected BooleanVectorSubExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.BooleanVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.BooleanVectorStack); } } [StorableClass] [PushExpression( StackTypes.StringVector, "STRING[].SUB", "Pushes the sub vector of the top STRING[].", StackTypes.Integer)] public class StringVectorSubExpression : VectorSubExpression { public StringVectorSubExpression() { } [StorableConstructor] protected StringVectorSubExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.StringVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.StringVectorStack); } } }