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.Extensions;
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
///
///
[StorableClass]
public abstract class VectorTakeExpression : StatelessExpression {
protected VectorTakeExpression() { }
[StorableConstructor]
protected VectorTakeExpression(bool deserializing) : base(deserializing) { }
protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack> vectorStack) {
return vectorStack.IsEmpty || interpreter.IntegerStack.IsEmpty;
}
protected void Eval(IInternalPushInterpreter interpreter, IPushStack> vectorStack) {
if (vectorStack.Top.Count == 0)
return;
var count = interpreter.IntegerStack.Pop().AsInt(vectorStack.Top.Count);
if (count < 0)
count *= -1;
var vector = vectorStack.Top;
var list = vector as List;
if (list != null) {
var newTop = new T[count];
list.CopyTo(0, newTop, 0, count);
vectorStack.Top = newTop;
return;
}
var array = vector as T[];
if (array != null) {
var newTop = new T[count];
Array.Copy(array, 0, newTop, 0, count);
vectorStack.Top = newTop;
return;
}
vectorStack.Top = vector.Take(count).ToList();
}
}
[StorableClass]
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].TAKE",
"Takes the first n items of the top INTEGER[], where n is from the INTEGER stack.",
StackTypes.Integer)]
public class IntegerVectorTakeExpression : VectorTakeExpression {
public IntegerVectorTakeExpression() { }
[StorableConstructor]
protected IntegerVectorTakeExpression(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[].TAKE",
"Takes the first n items of the top FLOAT[], where n is from the INTEGER stack.",
StackTypes.Integer)]
public class FloatVectorTakeExpression : VectorTakeExpression {
public FloatVectorTakeExpression() { }
[StorableConstructor]
protected FloatVectorTakeExpression(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[].TAKE",
"Takes the first n items of the top BOOLEAN[], where n is from the INTEGER stack.",
StackTypes.Integer)]
public class BooleanVectorTakeExpression : VectorTakeExpression {
public BooleanVectorTakeExpression() { }
[StorableConstructor]
protected BooleanVectorTakeExpression(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[].TAKE",
"Takes the first n items of the top STRING[], where n is from the INTEGER stack.",
StackTypes.Integer)]
public class StringVectorTakeExpression : VectorTakeExpression {
public StringVectorTakeExpression() { }
[StorableConstructor]
protected StringVectorTakeExpression(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);
}
}
}