Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorReverseExpressions.cs @ 17104

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

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

File size: 3.6 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
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 reverse of the top item on the type stack.
13  /// </summary>
14  /// <param name="vectorStack"></param>
15  /// <returns></returns>
16  [StorableClass]
17  public abstract class VectorReverseExpression<T> : StatelessExpression {
18    protected VectorReverseExpression() { }
19    [StorableConstructor]
20    protected VectorReverseExpression(bool deserializing) : base(deserializing) { }
21
22    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
23      return vectorStack.IsEmpty || vectorStack.Top.Count < 2;
24    }
25
26    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack) {
27      vectorStack.Top = vectorStack.Top.Reverse().ToList();
28    }
29  }
30
31  [StorableClass]
32  [PushExpression(
33    StackTypes.IntegerVector,
34    "INTEGER[].REVERSE",
35    "Reverses the top INTEGER[].")]
36  public class IntegerVectorReverseExpression : VectorReverseExpression<long> {
37    public IntegerVectorReverseExpression() { }
38    [StorableConstructor]
39    protected IntegerVectorReverseExpression(bool deserializing) : base(deserializing) { }
40
41    public override bool IsNoop(IInternalPushInterpreter interpreter) {
42      return IsNoop(interpreter.IntegerVectorStack);
43    }
44
45    public override void Eval(IInternalPushInterpreter interpreter) {
46      Eval(interpreter.IntegerVectorStack);
47    }
48  }
49
50  [StorableClass]
51  [PushExpression(
52    StackTypes.FloatVector,
53    "FLOAT[].REVERSE",
54    "Reverses the top FLOAT[].")]
55  public class FloatVectorReverseExpression : VectorReverseExpression<double> {
56    public FloatVectorReverseExpression() { }
57    [StorableConstructor]
58    protected FloatVectorReverseExpression(bool deserializing) : base(deserializing) { }
59
60    public override bool IsNoop(IInternalPushInterpreter interpreter) {
61      return IsNoop(interpreter.FloatVectorStack);
62    }
63
64    public override void Eval(IInternalPushInterpreter interpreter) {
65      Eval(interpreter.FloatVectorStack);
66    }
67  }
68
69  [StorableClass]
70  [PushExpression(
71    StackTypes.BooleanVector,
72    "BOOLEAN[].REVERSE",
73    "Reverses the top BOOLEAN[].")]
74  public class BooleanVectorReverseExpression : VectorReverseExpression<bool> {
75    public BooleanVectorReverseExpression() { }
76    [StorableConstructor]
77    protected BooleanVectorReverseExpression(bool deserializing) : base(deserializing) { }
78
79    public override bool IsNoop(IInternalPushInterpreter interpreter) {
80      return IsNoop(interpreter.BooleanVectorStack);
81    }
82
83    public override void Eval(IInternalPushInterpreter interpreter) {
84      Eval(interpreter.BooleanVectorStack);
85    }
86  }
87
88  [StorableClass]
89  [PushExpression(
90    StackTypes.StringVector,
91    "STRING[].REVERSE",
92    "Reverses the top STRING[].")]
93  public class StringVectorReverseExpression : VectorReverseExpression<string> {
94    public StringVectorReverseExpression() { }
95    [StorableConstructor]
96    protected StringVectorReverseExpression(bool deserializing) : base(deserializing) { }
97
98    public override bool IsNoop(IInternalPushInterpreter interpreter) {
99      return IsNoop(interpreter.StringVectorStack);
100    }
101
102    public override void Eval(IInternalPushInterpreter interpreter) {
103      Eval(interpreter.StringVectorStack);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.