Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorSubExpressions.cs @ 17133

Last change on this file since 17133 was 15189, checked in by pkimmesw, 7 years ago

#2665 Fixed small issues, testet benchmark suite, added INX Expressions

File size: 4.6 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  /// Takes the subvec of the top item on the type stack.
14  /// </summary>
15  /// <typeparam name="T"></typeparam>
16  [StorableClass]
17  public abstract class VectorSubExpression<T> : StatelessExpression {
18    protected VectorSubExpression() { }
19    [StorableConstructor]
20    protected VectorSubExpression(bool deserializing) : base(deserializing) { }
21
22    protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
23      return vectorStack.IsEmpty || interpreter.IntegerStack.Count < 2;
24    }
25
26    protected void Eval(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
27      var first = (int)Math.Min(vectorStack.Top.Count, Math.Max(0, interpreter.IntegerStack[1]));
28      var second = (int)Math.Min(vectorStack.Top.Count, Math.Max(first, interpreter.IntegerStack.Top));
29      interpreter.IntegerStack.Remove(2);
30
31      if (vectorStack.Top.Count == 0)
32        return;
33
34      var vector = vectorStack.Top;
35      var list = vector as List<T>;
36      var lenght = second - first;
37
38      if (list != null) {
39        var newTop = new T[lenght];
40        list.CopyTo(first, newTop, 0, lenght);
41        vectorStack.Top = newTop;
42        return;
43      }
44
45      var array = vector as T[];
46      if (array != null) {
47        var newTop = new T[lenght];
48        Array.Copy(array, first, newTop, 0, lenght);
49        vectorStack.Top = newTop;
50        return;
51      }
52
53      vectorStack.Top = vector.Skip(first).Take(lenght).ToList();
54    }
55  }
56
57  [StorableClass]
58  [PushExpression(
59    StackTypes.IntegerVector,
60    "INTEGER[].SUB",
61    "Pushes the sub vector of the top INTEGER[].",
62    StackTypes.Integer)]
63  public class IntegerVectorSubExpression : VectorSubExpression<long> {
64    public IntegerVectorSubExpression() { }
65    [StorableConstructor]
66    protected IntegerVectorSubExpression(bool deserializing) : base(deserializing) { }
67
68    public override bool IsNoop(IInternalPushInterpreter interpreter) {
69      return IsNoop(interpreter, interpreter.IntegerVectorStack);
70    }
71
72    public override void Eval(IInternalPushInterpreter interpreter) {
73      Eval(interpreter, interpreter.IntegerVectorStack);
74    }
75  }
76
77  [StorableClass]
78  [PushExpression(
79    StackTypes.FloatVector,
80    "FLOAT[].SUB",
81    "Pushes the sub vector of the top FLOAT[].",
82    StackTypes.Integer)]
83  public class FloatVectorSubExpression : VectorSubExpression<double> {
84    public FloatVectorSubExpression() { }
85    [StorableConstructor]
86    protected FloatVectorSubExpression(bool deserializing) : base(deserializing) { }
87
88    public override bool IsNoop(IInternalPushInterpreter interpreter) {
89      return IsNoop(interpreter, interpreter.FloatVectorStack);
90    }
91
92    public override void Eval(IInternalPushInterpreter interpreter) {
93      Eval(interpreter, interpreter.FloatVectorStack);
94    }
95  }
96
97  [StorableClass]
98  [PushExpression(
99    StackTypes.BooleanVector,
100    "BOOLEAN[].SUB",
101    "Pushes the sub vector of the top BOOLEAN[].",
102    StackTypes.Integer)]
103  public class BooleanVectorSubExpression : VectorSubExpression<bool> {
104    public BooleanVectorSubExpression() { }
105    [StorableConstructor]
106    protected BooleanVectorSubExpression(bool deserializing) : base(deserializing) { }
107
108    public override bool IsNoop(IInternalPushInterpreter interpreter) {
109      return IsNoop(interpreter, interpreter.BooleanVectorStack);
110    }
111
112    public override void Eval(IInternalPushInterpreter interpreter) {
113      Eval(interpreter, interpreter.BooleanVectorStack);
114    }
115  }
116
117  [StorableClass]
118  [PushExpression(
119    StackTypes.StringVector,
120    "STRING[].SUB",
121    "Pushes the sub vector of the top STRING[].",
122    StackTypes.Integer)]
123  public class StringVectorSubExpression : VectorSubExpression<string> {
124    public StringVectorSubExpression() { }
125    [StorableConstructor]
126    protected StringVectorSubExpression(bool deserializing) : base(deserializing) { }
127
128    public override bool IsNoop(IInternalPushInterpreter interpreter) {
129      return IsNoop(interpreter, interpreter.StringVectorStack);
130    }
131
132    public override void Eval(IInternalPushInterpreter interpreter) {
133      Eval(interpreter, interpreter.StringVectorStack);
134    }
135  }
136}
Note: See TracBrowser for help on using the repository browser.