source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorIterateExpressions.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.8 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  /// Iterates in reverse order (due to performance reasons) over the type vector using the code on the exec stack.If the vector isn't empty, expands to:
10  /// ((first vector) (top-item :exec state) (rest vector) exec_do*vector_type (top-item :exec state) rest_of_program)
11  /// </summary>
12  /// <typeparam name="T"></typeparam>
13  public abstract class VectorIterateExpression<T> : StatelessExpression {
14    protected bool Eval(IInternalPushInterpreter interpter, IPushStack<List<T>> vectorStack, IPushStack<T> literalStack) {
15      if (vectorStack.IsEmpty ||
16          interpter.ExecStack.IsEmpty)
17        return false;
18
19      var vector = vectorStack.Top;
20
21      if (vector.Count == 0) {
22        interpter.ExecStack.Pop();
23        vectorStack.Pop();
24      } else if (vector.Count == 1) {
25        literalStack.Push(vector[0]);
26        vectorStack.Pop();
27      } else {
28        interpter.ExecStack.Push(this, interpter.ExecStack.Top);
29
30        var last = vector.Count - 1;
31        literalStack.Push(vector[last]);
32        vector.RemoveAt(last);
33      }
34
35      return true;
36    }
37  }
38
39  [PushExpression(StackTypes.IntegerVector, "INTEGER[].ITERATE", StackTypes.Integer | StackTypes.Exec)]
40  public class IntegerVectorIterateExpression : VectorIterateExpression<long> {
41    public override bool Eval(IInternalPushInterpreter interpreter) {
42      return Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack);
43    }
44  }
45
46  [PushExpression(StackTypes.FloatVector, "FLOAT[].ITERATE", StackTypes.Float | StackTypes.Exec)]
47  public class FloatVectorIterateExpression : VectorIterateExpression<double> {
48    public override bool Eval(IInternalPushInterpreter interpreter) {
49      return Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
50    }
51  }
52
53  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].ITERATE", StackTypes.Boolean | StackTypes.Exec)]
54  public class BooleanVectorIterateExpression : VectorIterateExpression<bool> {
55    public override bool Eval(IInternalPushInterpreter interpreter) {
56      return Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
57    }
58  }
59
60  [PushExpression(StackTypes.StringVector, "STRING[].ITERATE", StackTypes.String | StackTypes.Exec)]
61  public class StringVectorIterateExpression : VectorIterateExpression<string> {
62    public override bool Eval(IInternalPushInterpreter interpreter) {
63      return Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
64    }
65  }
66}
Note: See TracBrowser for help on using the repository browser.