1 | namespace 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 | .ToArray();
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | [StorableClass]
|
---|
32 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].BUTLAST")]
|
---|
33 | public class IntegerVectorButLastExpression : VectorButLastExpression<long> {
|
---|
34 | public IntegerVectorButLastExpression() { }
|
---|
35 | [StorableConstructor]
|
---|
36 | protected IntegerVectorButLastExpression(bool deserializing) : base(deserializing) { }
|
---|
37 |
|
---|
38 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
39 | return IsNoop(interpreter.IntegerVectorStack);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
43 | Eval(interpreter.IntegerVectorStack);
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [StorableClass]
|
---|
48 | [PushExpression(StackTypes.FloatVector, "FLOAT[].BUTLAST")]
|
---|
49 | public class FloatVectorButLastExpression : VectorButLastExpression<double> {
|
---|
50 | public FloatVectorButLastExpression() { }
|
---|
51 | [StorableConstructor]
|
---|
52 | protected FloatVectorButLastExpression(bool deserializing) : base(deserializing) { }
|
---|
53 |
|
---|
54 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
55 | return IsNoop(interpreter.FloatVectorStack);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
59 | Eval(interpreter.FloatVectorStack);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | [StorableClass]
|
---|
64 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].BUTLAST")]
|
---|
65 | public class BooleanVectorButLastExpression : VectorButLastExpression<bool> {
|
---|
66 | public BooleanVectorButLastExpression() { }
|
---|
67 | [StorableConstructor]
|
---|
68 | protected BooleanVectorButLastExpression(bool deserializing) : base(deserializing) { }
|
---|
69 |
|
---|
70 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
71 | return IsNoop(interpreter.BooleanVectorStack);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
75 | Eval(interpreter.BooleanVectorStack);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | [StorableClass]
|
---|
80 | [PushExpression(StackTypes.StringVector, "STRING[].BUTLAST")]
|
---|
81 | public class StringVectorButLastExpression : VectorButLastExpression<string> {
|
---|
82 | public StringVectorButLastExpression() { }
|
---|
83 | [StorableConstructor]
|
---|
84 | protected StringVectorButLastExpression(bool deserializing) : base(deserializing) { }
|
---|
85 |
|
---|
86 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
87 | return IsNoop(interpreter.StringVectorStack);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
91 | Eval(interpreter.StringVectorStack);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|