using System.Collections.Generic;
namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
///
/// Replaces all occurrences of the second lit-type item with the first lit-type item in the top type vector.
///
///
[StorableClass]
public abstract class VectorReplaceExpression : StatelessExpression {
protected VectorReplaceExpression() { }
[StorableConstructor]
protected VectorReplaceExpression(bool deserializing) : base(deserializing) { }
protected bool IsNoop(IPushStack> vectorStack, IPushStack literalStack) {
return vectorStack.IsEmpty || literalStack.Count < 2;
}
protected void Eval(IPushStack> vectorStack, IPushStack literalStack) {
var vector = vectorStack.Top;
var first = literalStack.Top;
var second = literalStack[1];
literalStack.Remove(2);
var result = new List(vector);
for (var i = 0; i < vector.Count; i++) {
if (vector[i].Equals(second))
result[i] = first;
}
vectorStack.Top = result;
}
}
[StorableClass]
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].REPLACE",
"Replaces all occurrences of the second INTEGER with the INTEGER in the top INTEGER[].",
StackTypes.Integer)]
public class IntegerVectorReplaceExpression : VectorReplaceExpression {
public IntegerVectorReplaceExpression() { }
[StorableConstructor]
protected IntegerVectorReplaceExpression(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[].REPLACE",
"Replaces all occurrences of the second FLOAT with the FLOAT in the top FLOAT[].",
StackTypes.Float)]
public class FloatVectorReplaceExpression : VectorReplaceExpression {
public FloatVectorReplaceExpression() { }
[StorableConstructor]
protected FloatVectorReplaceExpression(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[].REPLACE",
"Replaces all occurrences of the second BOOLEAN with the BOOLEAN in the top BOOLEAN[].",
StackTypes.Boolean)]
public class BooleanVectorReplaceExpression : VectorReplaceExpression {
public BooleanVectorReplaceExpression() { }
[StorableConstructor]
protected BooleanVectorReplaceExpression(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[].REPLACE",
"Replaces all occurrences of the second STRING with the STRING in the top STRING[].",
StackTypes.String)]
public class StringVectorReplaceExpression : VectorReplaceExpression {
public StringVectorReplaceExpression() { }
[StorableConstructor]
protected StringVectorReplaceExpression(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);
}
}
}