Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorTakeExpressions.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.1 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  /// Takes the first N items from the type stack, where N is from the integer stack
10  /// </summary>
11  /// <typeparam name="T"></typeparam>
12  public abstract class VectorTakeExpression<T> : StatelessExpression {
13    protected bool Eval(IInternalPushInterpreter interpreter, IPushStack<List<T>> vectorStack) {
14      if (vectorStack.IsEmpty || interpreter.IntegerStack.IsEmpty)
15        return false;
16
17      var count = (int)interpreter.IntegerStack.Pop();
18      var result = vectorStack.Top.GetRange(0, count);
19
20      vectorStack.Push(result);
21      return true;
22    }
23  }
24
25  [PushExpression(StackTypes.IntegerVector, "INTEGER[].TAKE", StackTypes.Integer)]
26  public class IntegerVectorTakeExpression : VectorTakeExpression<long> {
27    public override bool Eval(IInternalPushInterpreter interpreter) {
28      return Eval(interpreter, interpreter.IntegerVectorStack);
29    }
30  }
31
32  [PushExpression(StackTypes.FloatVector, "FLOAT[].TAKE", StackTypes.Integer)]
33  public class FloatVectorTakeExpression : VectorTakeExpression<double> {
34    public override bool Eval(IInternalPushInterpreter interpreter) {
35      return Eval(interpreter, interpreter.FloatVectorStack);
36    }
37  }
38
39  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].TAKE", StackTypes.Integer)]
40  public class BooleanVectorTakeExpression : VectorTakeExpression<bool> {
41    public override bool Eval(IInternalPushInterpreter interpreter) {
42      return Eval(interpreter, interpreter.BooleanVectorStack);
43    }
44  }
45
46  [PushExpression(StackTypes.StringVector, "STRING[].TAKE", StackTypes.Integer)]
47  public class StringVectorTakeExpression : VectorTakeExpression<string> {
48    public override bool Eval(IInternalPushInterpreter interpreter) {
49      return Eval(interpreter, interpreter.StringVectorStack);
50    }
51  }
52}
Note: See TracBrowser for help on using the repository browser.