Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorButLastExpressions.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: 3.7 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Collections.Generic;
3  using System.Linq;
4
5  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
9
10  /// <summary>
11  /// Takes the rest of the top item on the type stack.
12  /// </summary>
13  /// <typeparam name="T"></typeparam>
14  [StorableClass]
15  public abstract class VectorButLastExpression<T> : StatelessExpression {
16    protected VectorButLastExpression() { }
17    [StorableConstructor]
18    protected VectorButLastExpression(bool deserializing) : base(deserializing) { }
19
20    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
21      return vectorStack.IsEmpty || vectorStack.Top.Count < 2;
22    }
23
24    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack) {
25      vectorStack.Top = vectorStack.Top
26        .Take(vectorStack.Top.Count - 1)
27        .ToList();
28    }
29  }
30
31  [StorableClass]
32  [PushExpression(
33    StackTypes.IntegerVector,
34    "INTEGER[].BUTLAST",
35    "Takes the rest of the top item on the INTEGER[] stack.")]
36  public class IntegerVectorButLastExpression : VectorButLastExpression<long> {
37    public IntegerVectorButLastExpression() { }
38    [StorableConstructor]
39    protected IntegerVectorButLastExpression(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[].BUTLAST",
54    "Takes the rest of the top item on the FLOAT[] stack.")]
55  public class FloatVectorButLastExpression : VectorButLastExpression<double> {
56    public FloatVectorButLastExpression() { }
57    [StorableConstructor]
58    protected FloatVectorButLastExpression(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[].BUTLAST",
73    "Takes the rest of the top item on the BOOLEAN[] stack.")]
74  public class BooleanVectorButLastExpression : VectorButLastExpression<bool> {
75    public BooleanVectorButLastExpression() { }
76    [StorableConstructor]
77    protected BooleanVectorButLastExpression(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[].BUTLAST",
92    "Takes the rest of the top item on the STRING[] stack.")]
93  public class StringVectorButLastExpression : VectorButLastExpression<string> {
94    public StringVectorButLastExpression() { }
95    [StorableConstructor]
96    protected StringVectorButLastExpression(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.