Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorTakeExpressions.cs @ 14909

Last change on this file since 14909 was 14909, checked in by pkimmesw, 8 years ago

#2665 Fixed VectorExpression errors, Fixed BenchmarkSuite Problem Data View issues

File size: 2.2 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
19      if (vectorStack.Top.Count == 0)
20        return true;
21
22      count %= vectorStack.Top.Count;
23
24      if (count < 0)
25        count *= -1;
26
27      var result = vectorStack.Top.GetRange(0, count);
28      vectorStack.SetTop(result);
29
30      return true;
31    }
32  }
33
34  [PushExpression(StackTypes.IntegerVector, "INTEGER[].TAKE", StackTypes.Integer)]
35  public class IntegerVectorTakeExpression : VectorTakeExpression<long> {
36    public override bool Eval(IInternalPushInterpreter interpreter) {
37      return Eval(interpreter, interpreter.IntegerVectorStack);
38    }
39  }
40
41  [PushExpression(StackTypes.FloatVector, "FLOAT[].TAKE", StackTypes.Integer)]
42  public class FloatVectorTakeExpression : VectorTakeExpression<double> {
43    public override bool Eval(IInternalPushInterpreter interpreter) {
44      return Eval(interpreter, interpreter.FloatVectorStack);
45    }
46  }
47
48  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].TAKE", StackTypes.Integer)]
49  public class BooleanVectorTakeExpression : VectorTakeExpression<bool> {
50    public override bool Eval(IInternalPushInterpreter interpreter) {
51      return Eval(interpreter, interpreter.BooleanVectorStack);
52    }
53  }
54
55  [PushExpression(StackTypes.StringVector, "STRING[].TAKE", StackTypes.Integer)]
56  public class StringVectorTakeExpression : VectorTakeExpression<string> {
57    public override bool Eval(IInternalPushInterpreter interpreter) {
58      return Eval(interpreter, interpreter.StringVectorStack);
59    }
60  }
61}
Note: See TracBrowser for help on using the repository browser.