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

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

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

File size: 2.3 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4  using System;
5
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
9
10  /// <summary>
11  /// Takes the subvec of the top item on the type stack.
12  /// </summary>
13  /// <typeparam name="T"></typeparam>
14  public abstract class VectorSubExpression<T> : StatelessExpression {
15    protected bool Eval(IInternalPushInterpreter interpreter, IPushStack<List<T>> vectorStack) {
16      if (vectorStack.IsEmpty || interpreter.IntegerStack.Count < 2) return false;
17
18      var first = (int)Math.Min(vectorStack.Count, Math.Max(0, interpreter.IntegerStack.ReverseElementAt(1)));
19      var second = (int)Math.Min(vectorStack.Count, Math.Max(first, interpreter.IntegerStack.Top));
20      interpreter.IntegerStack.Remove(2);
21
22      var result = vectorStack.Top.GetRange(first, second - first);
23      vectorStack.Push(result);
24      return true;
25    }
26  }
27
28  [PushExpression(StackTypes.IntegerVector, "INTEGER[].SUB", StackTypes.Integer)]
29  public class IntegerVectorSubExpression : VectorSubExpression<long> {
30    public override bool Eval(IInternalPushInterpreter interpreter) {
31      return Eval(interpreter, interpreter.IntegerVectorStack);
32    }
33  }
34
35  [PushExpression(StackTypes.FloatVector, "FLOAT[].SUB", StackTypes.Integer)]
36  public class FloatVectorSubExpression : VectorSubExpression<double> {
37    public override bool Eval(IInternalPushInterpreter interpreter) {
38      return Eval(interpreter, interpreter.FloatVectorStack);
39    }
40  }
41
42  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].SUB", StackTypes.Integer)]
43  public class BooleanVectorSubExpression : VectorSubExpression<bool> {
44    public override bool Eval(IInternalPushInterpreter interpreter) {
45      return Eval(interpreter, interpreter.BooleanVectorStack);
46    }
47  }
48
49  [PushExpression(StackTypes.StringVector, "STRING[].SUB", StackTypes.Integer)]
50  public class StringVectorSubExpression : VectorSubExpression<string> {
51    public override bool Eval(IInternalPushInterpreter interpreter) {
52      return Eval(interpreter, interpreter.StringVectorStack);
53    }
54  }
55}
Note: See TracBrowser for help on using the repository browser.