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;
///
/// Counts the occurrences of the top lit-type item in the top type vector.
///
///
[StorableClass]
public abstract class VectorOccurrenceOfExpression : StatelessExpression {
protected VectorOccurrenceOfExpression() { }
[StorableConstructor]
protected VectorOccurrenceOfExpression(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 occurrence = 0;
for (var i = 0; i < vector.Count; i++) {
if (vector[i].Equals(literal)) occurrence++;
}
if (canOverride) interpreter.IntegerStack.Top = occurrence;
else interpreter.IntegerStack.Push(occurrence);
}
}
[StorableClass]
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].OCCURRENCEOF",
"Pushes the amount of occurrences of the top INTEGER in the top INTEGER[] onto the INTEGER stack.",
StackTypes.Integer)]
public class IntegerVectorOccurrenceOfExpression : VectorOccurrenceOfExpression {
public IntegerVectorOccurrenceOfExpression() { }
[StorableConstructor]
protected IntegerVectorOccurrenceOfExpression(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, true);
}
}
[StorableClass]
[PushExpression(
StackTypes.FloatVector,
"FLOAT[].OCCURRENCEOF",
"Pushes the amount of occurrences of the top FLOAT in the top FLOAT[] onto the INTEGER stack.",
StackTypes.Float | StackTypes.Integer)]
public class FloatVectorOccurrenceOfExpression : VectorOccurrenceOfExpression {
public FloatVectorOccurrenceOfExpression() { }
[StorableConstructor]
protected FloatVectorOccurrenceOfExpression(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[].OCCURRENCEOF",
"Pushes the amount of occurrences of the top BOOLEAN in the top BOOLEAN[] onto the INTEGER stack.",
StackTypes.Boolean | StackTypes.Integer)]
public class BooleanVectorOccurrenceOfExpression : VectorOccurrenceOfExpression {
public BooleanVectorOccurrenceOfExpression() { }
[StorableConstructor]
protected BooleanVectorOccurrenceOfExpression(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);
}
}
[StorableClass]
[PushExpression(
StackTypes.StringVector,
"STRING[].OCCURRENCEOF",
"Pushes the amount of occurrences of the top STRING in the top STRING[] onto the INTEGER stack.",
StackTypes.String | StackTypes.Integer)]
public class StringVectorOccurrenceOfExpression : VectorOccurrenceOfExpression {
public StringVectorOccurrenceOfExpression() { }
[StorableConstructor]
protected StringVectorOccurrenceOfExpression(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);
}
}
}