Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorLastExpressions.cs @ 16752

Last change on this file since 16752 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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