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;
///
/// Replace first occurrence of the second lit-type item with the first lit-type item in the top type vector.
///
///
[StorableClass]
public abstract class VectorReplaceFirstExpression : StatelessExpression {
protected VectorReplaceFirstExpression() { }
[StorableConstructor]
protected VectorReplaceFirstExpression(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 index = -1;
for (var i = 0; i < vector.Count && index < 0; i++) {
if (vector[i].Equals(second)) {
index = i;
break;
}
}
if (index >= 0) {
var result = new List(vector);
result[index] = first;
vectorStack.Top = result;
}
}
}
[StorableClass]
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].REPLACEFIRST",
"Replace first occurrence of the second INTEGER with the INTEGER in the top INTEGER[].",
StackTypes.Integer)]
public class IntegerVectorReplaceFirstExpression : VectorReplaceFirstExpression {
public IntegerVectorReplaceFirstExpression() { }
[StorableConstructor]
protected IntegerVectorReplaceFirstExpression(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[].REPLACEFIRST",
"Replace first occurrence of the second FLOAT with the FLOAT in the top FLOAT[].",
StackTypes.Float)]
public class FloatVectorReplaceFirstExpression : VectorReplaceFirstExpression {
public FloatVectorReplaceFirstExpression() { }
[StorableConstructor]
protected FloatVectorReplaceFirstExpression(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[].REPLACEFIRST",
"Replace first occurrence of the second BOOLEAN with the BOOLEAN in the top BOOLEAN[].",
StackTypes.Boolean)]
public class BooleanVectorReplaceFirstExpression : VectorReplaceFirstExpression {
public BooleanVectorReplaceFirstExpression() { }
[StorableConstructor]
protected BooleanVectorReplaceFirstExpression(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[].REPLACEFIRST",
"Replace first occurrence of the second STRING with the STRING in the top STRING[].",
StackTypes.String)]
public class StringVectorReplaceFirstExpression : VectorReplaceFirstExpression {
public StringVectorReplaceFirstExpression() { }
[StorableConstructor]
protected StringVectorReplaceFirstExpression(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);
}
}
}