Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorIterateExpressions.cs @ 15341

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

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 5.5 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4  using System;
5  using System.Linq;
6
7  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
9  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
10  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
11
12  /// <summary>
13  /// 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:
14  /// ((first vector) (top-item :exec state) (rest vector) exec_do*vector_type (top-item :exec state) rest_of_program)
15  /// </summary>
16  /// <typeparam name="T"></typeparam>
17  [StorableClass]
18  public abstract class VectorIterateExpression<T> : StatelessExpression {
19    protected VectorIterateExpression() { }
20    [StorableConstructor]
21    protected VectorIterateExpression(bool deserializing) : base(deserializing) { }
22
23    protected bool IsNoop(IInternalPushInterpreter interpter, IPushStack<IReadOnlyList<T>> vectorStack) {
24      return vectorStack.IsEmpty || interpter.ExecStack.IsEmpty;
25    }
26
27    protected void Eval(IInternalPushInterpreter interpter, IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
28      var vector = vectorStack.Top;
29
30      if (vector.Count == 0) {
31        interpter.ExecStack.Pop();
32        vectorStack.Pop();
33      } else if (vector.Count == 1) {
34        literalStack.Push(vector[0]);
35        vectorStack.Pop();
36      } else {
37        interpter.ExecStack.Push(this, interpter.ExecStack.Top);
38
39        var last = vector.Count - 1;
40        literalStack.Push(vector[last]);
41
42        var list = vector as List<T>;
43        if (list != null) {
44          var newTop = new T[last];
45          list.CopyTo(0, newTop, 0, last);
46          vectorStack.Top = newTop;
47          return;
48        }
49
50        var array = vector as T[];
51        if (array != null) {
52          var newTop = new T[last];
53          Array.Copy(array, 0, newTop, 0, last);
54          vectorStack.Top = newTop;
55          return;
56        }
57
58        vectorStack.Top = vector.Take(last).ToList();
59      }
60    }
61  }
62
63  [StorableClass]
64  [PushExpression(
65    StackTypes.IntegerVector,
66    "INTEGER[].ITERATE",
67    "Iterates in reverse order (due to performance reasons) over the top INTEGER[] using the top item of the EXEC stack.",
68    StackTypes.Integer | StackTypes.Exec,
69    requiredBlockCount: 1)]
70  public class IntegerVectorIterateExpression : VectorIterateExpression<long> {
71    public IntegerVectorIterateExpression() { }
72    [StorableConstructor]
73    protected IntegerVectorIterateExpression(bool deserializing) : base(deserializing) { }
74
75    public override bool IsNoop(IInternalPushInterpreter interpreter) {
76      return IsNoop(interpreter, interpreter.IntegerVectorStack);
77    }
78
79    public override void Eval(IInternalPushInterpreter interpreter) {
80      Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack);
81    }
82  }
83
84  [StorableClass]
85  [PushExpression(
86    StackTypes.FloatVector,
87    "FLOAT[].ITERATE",
88    "Iterates in reverse order (due to performance reasons) over the top FLOAT[] using the top item of the EXEC stack.",
89    StackTypes.Float | StackTypes.Exec, requiredBlockCount: 1)]
90  public class FloatVectorIterateExpression : VectorIterateExpression<double> {
91    public FloatVectorIterateExpression() { }
92    [StorableConstructor]
93    protected FloatVectorIterateExpression(bool deserializing) : base(deserializing) { }
94
95    public override bool IsNoop(IInternalPushInterpreter interpreter) {
96      return IsNoop(interpreter, interpreter.FloatVectorStack);
97    }
98
99    public override void Eval(IInternalPushInterpreter interpreter) {
100      Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
101    }
102  }
103
104  [StorableClass]
105  [PushExpression(
106    StackTypes.BooleanVector,
107    "BOOLEAN[].ITERATE",
108    "Iterates in reverse order (due to performance reasons) over the top BOOLEAN[] using the top item of the EXEC stack.",
109    StackTypes.Boolean | StackTypes.Exec, requiredBlockCount: 1)]
110  public class BooleanVectorIterateExpression : VectorIterateExpression<bool> {
111    public BooleanVectorIterateExpression() { }
112    [StorableConstructor]
113    protected BooleanVectorIterateExpression(bool deserializing) : base(deserializing) { }
114
115    public override bool IsNoop(IInternalPushInterpreter interpreter) {
116      return IsNoop(interpreter, interpreter.BooleanVectorStack);
117    }
118
119    public override void Eval(IInternalPushInterpreter interpreter) {
120      Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
121    }
122  }
123
124  [StorableClass]
125  [PushExpression(
126    StackTypes.StringVector,
127    "STRING[].ITERATE",
128    "Iterates in reverse order (due to performance reasons) over the top STRING[] using the top item of the EXEC stack.",
129    StackTypes.String | StackTypes.Exec, requiredBlockCount: 1)]
130  public class StringVectorIterateExpression : VectorIterateExpression<string> {
131    public StringVectorIterateExpression() { }
132    [StorableConstructor]
133    protected StringVectorIterateExpression(bool deserializing) : base(deserializing) { }
134
135    public override bool IsNoop(IInternalPushInterpreter interpreter) {
136      return IsNoop(interpreter, interpreter.StringVectorStack);
137    }
138
139    public override void Eval(IInternalPushInterpreter interpreter) {
140      Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
141    }
142  }
143}
Note: See TracBrowser for help on using the repository browser.