using System.Collections.Generic; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; /// /// Takes the first N items from the type stack, where N is from the integer stack /// /// public abstract class VectorTakeExpression : StatelessExpression { protected bool Eval(IInternalPushInterpreter interpreter, IPushStack> vectorStack) { if (vectorStack.IsEmpty || interpreter.IntegerStack.IsEmpty) return false; var count = (int)interpreter.IntegerStack.Pop(); if (vectorStack.Top.Count == 0) return true; count %= vectorStack.Top.Count; if (count < 0) count *= -1; var result = vectorStack.Top.GetRange(0, count); vectorStack.SetTop(result); return true; } } [PushExpression(StackTypes.IntegerVector, "INTEGER[].TAKE", StackTypes.Integer)] public class IntegerVectorTakeExpression : VectorTakeExpression { public override bool Eval(IInternalPushInterpreter interpreter) { return Eval(interpreter, interpreter.IntegerVectorStack); } } [PushExpression(StackTypes.FloatVector, "FLOAT[].TAKE", StackTypes.Integer)] public class FloatVectorTakeExpression : VectorTakeExpression { public override bool Eval(IInternalPushInterpreter interpreter) { return Eval(interpreter, interpreter.FloatVectorStack); } } [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].TAKE", StackTypes.Integer)] public class BooleanVectorTakeExpression : VectorTakeExpression { public override bool Eval(IInternalPushInterpreter interpreter) { return Eval(interpreter, interpreter.BooleanVectorStack); } } [PushExpression(StackTypes.StringVector, "STRING[].TAKE", StackTypes.Integer)] public class StringVectorTakeExpression : VectorTakeExpression { public override bool Eval(IInternalPushInterpreter interpreter) { return Eval(interpreter, interpreter.StringVectorStack); } } }