Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorRestExpressions.cs @ 16958

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

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

File size: 4.1 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3  using System.Collections.Generic;
4  using System.Linq;
5
6  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
9  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
10
11  /// <summary>
12  /// Takes the rest of the top item on the type stack.
13  /// </summary>
14  /// <typeparam name="T"></typeparam>
15  [StorableClass]
16  public abstract class VectorRestExpression<T> : StatelessExpression {
17    protected VectorRestExpression() { }
18    [StorableConstructor]
19    protected VectorRestExpression(bool deserializing) : base(deserializing) { }
20
21    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
22      return vectorStack.IsEmpty || vectorStack.Top.Count < 2;
23    }
24
25    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack) {
26      var vector = vectorStack.Top;
27      var list = vector as List<T>;
28      var newLength = vector.Count - 1;
29      if (list != null) {
30        var newTop = new T[newLength];
31        list.CopyTo(1, newTop, 0, newLength);
32        vectorStack.Top = newTop;
33        return;
34      }
35
36      var array = vector as T[];
37      if (array != null) {
38        var newTop = new T[newLength];
39        Array.Copy(array, 1, newTop, 0, newLength);
40        vectorStack.Top = newTop;
41        return;
42      }
43
44      vectorStack.Top = vector.Skip(1).ToList();
45    }
46  }
47
48  [StorableClass]
49  [PushExpression(
50    StackTypes.IntegerVector,
51    "INTEGER[].REST",
52    "Removes the first item of the top INTEGER[].")]
53  public class IntegerVectorRestExpression : VectorRestExpression<long> {
54    public IntegerVectorRestExpression() { }
55    [StorableConstructor]
56    protected IntegerVectorRestExpression(bool deserializing) : base(deserializing) { }
57
58    public override bool IsNoop(IInternalPushInterpreter interpreter) {
59      return IsNoop(interpreter.IntegerVectorStack);
60    }
61
62    public override void Eval(IInternalPushInterpreter interpreter) {
63      Eval(interpreter.IntegerVectorStack);
64    }
65  }
66
67  [StorableClass]
68  [PushExpression(
69    StackTypes.FloatVector,
70    "FLOAT[].REST",
71    "Removes the first item of the top FLOAT[].")]
72  public class FloatVectorRestExpression : VectorRestExpression<double> {
73    public FloatVectorRestExpression() { }
74    [StorableConstructor]
75    protected FloatVectorRestExpression(bool deserializing) : base(deserializing) { }
76
77    public override bool IsNoop(IInternalPushInterpreter interpreter) {
78      return IsNoop(interpreter.FloatVectorStack);
79    }
80
81    public override void Eval(IInternalPushInterpreter interpreter) {
82      Eval(interpreter.FloatVectorStack);
83    }
84  }
85
86  [StorableClass]
87  [PushExpression(
88    StackTypes.BooleanVector,
89    "BOOLEAN[].REST",
90    "Removes the first item of the top BOOLEAN[].")]
91  public class BooleanVectorRestExpression : VectorRestExpression<bool> {
92    public BooleanVectorRestExpression() { }
93    [StorableConstructor]
94    protected BooleanVectorRestExpression(bool deserializing) : base(deserializing) { }
95
96    public override bool IsNoop(IInternalPushInterpreter interpreter) {
97      return IsNoop(interpreter.BooleanVectorStack);
98    }
99
100    public override void Eval(IInternalPushInterpreter interpreter) {
101      Eval(interpreter.BooleanVectorStack);
102    }
103  }
104
105  [StorableClass]
106  [PushExpression(
107    StackTypes.StringVector,
108    "STRING[].REST",
109    "Removes the first item of the top STRING[].")]
110  public class StringVectorRestExpression : VectorRestExpression<string> {
111    public StringVectorRestExpression() { }
112    [StorableConstructor]
113    protected StringVectorRestExpression(bool deserializing) : base(deserializing) { }
114
115    public override bool IsNoop(IInternalPushInterpreter interpreter) {
116      return IsNoop(interpreter.StringVectorStack);
117    }
118
119    public override void Eval(IInternalPushInterpreter interpreter) {
120      Eval(interpreter.StringVectorStack);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.