using System.Collections.Generic;
namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
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;
///
/// Removes all occurrences of the first lit-type item in the top type vector.
///
///
[StorableClass]
public abstract class VectorRemoveExpression : StatelessExpression {
protected VectorRemoveExpression() { }
[StorableConstructor]
protected VectorRemoveExpression(bool deserializing) : base(deserializing) { }
protected bool IsNoop(IPushStack> vectorStack, IPushStack literalStack) {
return vectorStack.IsEmpty || literalStack.IsEmpty;
}
protected void Eval(IPushStack> vectorStack, IPushStack literalStack) {
var literal = literalStack.Pop();
vectorStack.Top = vectorStack.Top.Where(x => !x.Equals(literal)).ToList();
}
}
[StorableClass]
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].REMOVE",
"Removes all occurrences of the top INTEGER in the top INTEGER[].",
StackTypes.Integer)]
public class IntegerVectorRemoveExpression : VectorRemoveExpression {
public IntegerVectorRemoveExpression() { }
[StorableConstructor]
protected IntegerVectorRemoveExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.FloatVector,
"FLOAT[].REMOVE",
"Removes all occurrences of the top FLOAT in the top FLOAT[].",
StackTypes.Float)]
public class FloatVectorRemoveExpression : VectorRemoveExpression {
public FloatVectorRemoveExpression() { }
[StorableConstructor]
protected FloatVectorRemoveExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.BooleanVector,
"BOOLEAN[].REMOVE",
"Removes all occurrences of the top BOOLEAN in the top BOOLEAN[].",
StackTypes.Boolean)]
public class BooleanVectorRemoveExpression : VectorRemoveExpression {
public BooleanVectorRemoveExpression() { }
[StorableConstructor]
protected BooleanVectorRemoveExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.StringVector,
"STRING[].REMOVE",
"Removes all occurrences of the top STRING in the top STRING[].",
StackTypes.String)]
public class StringVectorRemoveExpression : VectorRemoveExpression {
public StringVectorRemoveExpression() { }
[StorableConstructor]
protected StringVectorRemoveExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter.StringVectorStack, interpreter.StringStack);
}
}
}