namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using System; using System.Collections.Generic; 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 rest of the top item on the type stack. /// /// [StorableClass] public abstract class VectorRestExpression : StatelessExpression { protected VectorRestExpression() { } [StorableConstructor] protected VectorRestExpression(bool deserializing) : base(deserializing) { } protected bool IsNoop(IPushStack> vectorStack) { return vectorStack.IsEmpty || vectorStack.Top.Count < 2; } protected void Eval(IPushStack> vectorStack) { var vector = vectorStack.Top; var list = vector as List; var newLength = vector.Count - 1; if (list != null) { var newTop = new T[newLength]; list.CopyTo(1, newTop, 0, newLength); vectorStack.Top = newTop; return; } var array = vector as T[]; if (array != null) { var newTop = new T[newLength]; Array.Copy(array, 1, newTop, 0, newLength); vectorStack.Top = newTop; return; } vectorStack.Top = vector.Skip(1).ToList(); } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].REST", "Removes the first item of the top INTEGER[].")] public class IntegerVectorRestExpression : VectorRestExpression { public IntegerVectorRestExpression() { } [StorableConstructor] protected IntegerVectorRestExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.IntegerVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.IntegerVectorStack); } } [StorableClass] [PushExpression( StackTypes.FloatVector, "FLOAT[].REST", "Removes the first item of the top FLOAT[].")] public class FloatVectorRestExpression : VectorRestExpression { public FloatVectorRestExpression() { } [StorableConstructor] protected FloatVectorRestExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.FloatVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.FloatVectorStack); } } [StorableClass] [PushExpression( StackTypes.BooleanVector, "BOOLEAN[].REST", "Removes the first item of the top BOOLEAN[].")] public class BooleanVectorRestExpression : VectorRestExpression { public BooleanVectorRestExpression() { } [StorableConstructor] protected BooleanVectorRestExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.BooleanVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.BooleanVectorStack); } } [StorableClass] [PushExpression( StackTypes.StringVector, "STRING[].REST", "Removes the first item of the top STRING[].")] public class StringVectorRestExpression : VectorRestExpression { public StringVectorRestExpression() { } [StorableConstructor] protected StringVectorRestExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.StringVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.StringVectorStack); } } }