Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorReplaceFirstExpressions.cs @ 14834

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

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

File size: 2.4 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  /// Replace first 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 VectorReplaceFirstExpression<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.ReverseElementAt(1);
21      var index = vector.IndexOf(second);
22      literalStack.Remove(2);
23
24      if (index >= 0)
25        vector[index] = first;
26
27      return true;
28    }
29  }
30
31  [PushExpression(StackTypes.IntegerVector, "INTEGER[].REPLACEFIRST", StackTypes.Integer)]
32  public class IntegerVectorReplaceFirstExpression : VectorReplaceFirstExpression<long> {
33    public override bool Eval(IInternalPushInterpreter interpreter) {
34      return Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
35    }
36  }
37
38  [PushExpression(StackTypes.FloatVector, "FLOAT[].REPLACEFIRST", StackTypes.Float)]
39  public class FloatVectorReplaceFirstExpression : VectorReplaceFirstExpression<double> {
40    public override bool Eval(IInternalPushInterpreter interpreter) {
41      return Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
42    }
43  }
44
45  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].REPLACEFIRST", StackTypes.Boolean)]
46  public class BooleanVectorReplaceFirstExpression : VectorReplaceFirstExpression<bool> {
47    public override bool Eval(IInternalPushInterpreter interpreter) {
48      return Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
49    }
50  }
51
52  [PushExpression(StackTypes.StringVector, "STRING[].REPLACEFIRST", StackTypes.String)]
53  public class StringVectorReplaceFirstExpression : VectorReplaceFirstExpression<string> {
54    public override bool Eval(IInternalPushInterpreter interpreter) {
55      return Eval(interpreter.StringVectorStack, interpreter.StringStack);
56    }
57  }
58}
Note: See TracBrowser for help on using the repository browser.