Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorReplaceExpressions.cs @ 14875

Last change on this file since 14875 was 14875, checked in by pkimmesw, 7 years ago

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File size: 2.3 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
7
8  /// <summary>
9  /// Replaces all occurrences of the second lit-type item with the first lit-type item in the top type vector.
10  /// </summary>
11  /// <typeparam name="T"></typeparam>
12  public abstract class VectorReplaceExpression<T> : StatelessExpression {
13    protected bool Eval(IPushStack<List<T>> vectorStack, IPushStack<T> literalStack) {
14      if (vectorStack.IsEmpty ||
15          literalStack.Count < 2)
16        return false;
17
18      var vector = vectorStack.Top;
19      var first = literalStack.Top;
20      var second = literalStack[1];
21      literalStack.Remove(2);
22
23      for (var i = 0; i < vector.Count; i++) {
24        if (vector[i].Equals(second))
25          vector[i] = first;
26      }
27
28      return true;
29    }
30  }
31
32  [PushExpression(StackTypes.IntegerVector, "INTEGER[].REPLACE", StackTypes.Integer)]
33  public class IntegerVectorReplaceExpression : VectorReplaceExpression<long> {
34    public override bool Eval(IInternalPushInterpreter interpreter) {
35      return Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
36    }
37  }
38
39  [PushExpression(StackTypes.FloatVector, "FLOAT[].REPLACE", StackTypes.Float)]
40  public class FloatVectorReplaceExpression : VectorReplaceExpression<double> {
41    public override bool Eval(IInternalPushInterpreter interpreter) {
42      return Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
43    }
44  }
45
46  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].REPLACE", StackTypes.Boolean)]
47  public class BooleanVectorReplaceExpression : VectorReplaceExpression<bool> {
48    public override bool Eval(IInternalPushInterpreter interpreter) {
49      return Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
50    }
51  }
52
53  [PushExpression(StackTypes.StringVector, "STRING[].REPLACE", StackTypes.String)]
54  public class StringVectorReplaceExpression : VectorReplaceExpression<string> {
55    public override bool Eval(IInternalPushInterpreter interpreter) {
56      return Eval(interpreter.StringVectorStack, interpreter.StringStack);
57    }
58  }
59}
Note: See TracBrowser for help on using the repository browser.