Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17104 was 15032, checked in by pkimmesw, 8 years ago

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

File size: 3.8 KB
Line 
1using System.Collections.Generic;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
8
9  /// <summary>
10  /// Gets the last item from the type stack
11  /// </summary>
12  /// <typeparam name="T"></typeparam>
13  [StorableClass]
14  public abstract class VectorLastExpression<T> : StatelessExpression {
15    protected VectorLastExpression() { }
16    [StorableConstructor]
17    protected VectorLastExpression(bool deserializing) : base(deserializing) { }
18
19    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
20      return vectorStack.IsEmpty || vectorStack.Top.Count == 0;
21    }
22
23    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack, IPushStack<T> literalStack) {
24      var vector = vectorStack.Pop();
25      literalStack.Push(vector[vector.Count - 1]);
26    }
27  }
28
29  [StorableClass]
30  [PushExpression(
31    StackTypes.IntegerVector,
32    "INTEGER[].LAST",
33    "Gets the last item from the top INTEGER[].",
34    StackTypes.Integer)]
35  public class IntegerVectorLastExpression : VectorLastExpression<long> {
36    public IntegerVectorLastExpression() { }
37    [StorableConstructor]
38    protected IntegerVectorLastExpression(bool deserializing) : base(deserializing) { }
39
40    public override bool IsNoop(IInternalPushInterpreter interpreter) {
41      return IsNoop(interpreter.IntegerVectorStack);
42    }
43
44    public override void Eval(IInternalPushInterpreter interpreter) {
45      Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
46    }
47  }
48
49  [StorableClass]
50  [PushExpression(
51    StackTypes.FloatVector,
52    "FLOAT[].LAST",
53    "Gets the last item from the top FLOAT[].",
54    StackTypes.Float)]
55  public class FloatVectorLastExpression : VectorLastExpression<double> {
56    public FloatVectorLastExpression() { }
57    [StorableConstructor]
58    protected FloatVectorLastExpression(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, interpreter.FloatStack);
66    }
67  }
68
69  [StorableClass]
70  [PushExpression(
71    StackTypes.BooleanVector,
72    "BOOLEAN[].LAST",
73    "Gets the last item from the top BOOLEAN[].",
74    StackTypes.Boolean)]
75  public class BooleanVectorLastExpression : VectorLastExpression<bool> {
76    public BooleanVectorLastExpression() { }
77    [StorableConstructor]
78    protected BooleanVectorLastExpression(bool deserializing) : base(deserializing) { }
79
80    public override bool IsNoop(IInternalPushInterpreter interpreter) {
81      return IsNoop(interpreter.BooleanVectorStack);
82    }
83
84    public override void Eval(IInternalPushInterpreter interpreter) {
85      Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
86    }
87  }
88
89  [StorableClass]
90  [PushExpression(
91    StackTypes.StringVector,
92    "STRING[].LAST",
93    "Gets the last item from the top STRING[].",
94    StackTypes.String)]
95  public class StringVectorLastExpression : VectorLastExpression<string> {
96    public StringVectorLastExpression() { }
97    [StorableConstructor]
98    protected StringVectorLastExpression(bool deserializing) : base(deserializing) { }
99
100    public override bool IsNoop(IInternalPushInterpreter interpreter) {
101      return IsNoop(interpreter.StringVectorStack);
102    }
103
104    public override void Eval(IInternalPushInterpreter interpreter) {
105      Eval(interpreter.StringVectorStack, interpreter.StringStack);
106    }
107  }
108}
109
Note: See TracBrowser for help on using the repository browser.