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;
///
/// Tells whether the top lit-type item is in the top type vector.
///
///
[StorableClass]
public abstract class VectorContainsExpression : StatelessExpression {
protected VectorContainsExpression() { }
[StorableConstructor]
protected VectorContainsExpression(bool deserializing) : base(deserializing) { }
protected bool IsNoop(IPushStack> vectorStack, IPushStack literalStack) {
return vectorStack.IsEmpty || literalStack.IsEmpty;
}
protected void Eval(IInternalPushInterpreter interpreter, IPushStack> vectorStack, IPushStack literalStack, bool canOverride = false) {
var vector = vectorStack.Pop();
var literal = canOverride ? literalStack.Top : literalStack.Pop();
var contains = vector.Contains(literal);
if (canOverride) interpreter.BooleanStack.Top = contains;
else interpreter.BooleanStack.Push(contains);
}
}
[StorableClass]
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].CONTAINS",
"Tells whether the top INTEGER is in the top INTEGER[].",
StackTypes.Integer | StackTypes.Boolean)]
public class IntegerVectorContainsExpression : VectorContainsExpression {
public IntegerVectorContainsExpression() { }
[StorableConstructor]
protected IntegerVectorContainsExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.FloatVector,
"FLOAT[].CONTAINS",
"Tells whether the top FLOAT is in the top FLOAT[].",
StackTypes.Float | StackTypes.Boolean)]
public class FloatVectorContainsExpression : VectorContainsExpression {
public FloatVectorContainsExpression() { }
[StorableConstructor]
protected FloatVectorContainsExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.FloatVectorStack, interpreter.FloatStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.BooleanVector,
"BOOLEAN[].CONTAINS",
"Tells whether the top BOOLEAN is in the top BOOLEAN[].",
StackTypes.Boolean)]
public class BooleanVectorContainsExpression : VectorContainsExpression {
public BooleanVectorContainsExpression() { }
[StorableConstructor]
protected BooleanVectorContainsExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.BooleanVectorStack, interpreter.BooleanStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack, true);
}
}
[StorableClass]
[PushExpression(
StackTypes.StringVector,
"STRING[].CONTAINS",
"Tells whether the top STRING is in the top STRING[].",
StackTypes.String | StackTypes.Boolean)]
public class StringVectorContainsExpression : VectorContainsExpression {
public StringVectorContainsExpression() { }
[StorableConstructor]
protected StringVectorContainsExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.StringVectorStack, interpreter.StringStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
}
}
}