1 | using System.Collections.Generic;
|
---|
2 | using System;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
5 |
|
---|
6 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
7 | /// <summary>
|
---|
8 | /// Takes the first N items from the type stack, where N is from the integer stack
|
---|
9 | /// </summary>
|
---|
10 | /// <typeparam name="T"></typeparam>
|
---|
11 | [StorableClass]
|
---|
12 | public abstract class VectorTakeExpression<T> : StatelessExpression {
|
---|
13 | protected VectorTakeExpression() { }
|
---|
14 | [StorableConstructor]
|
---|
15 | protected VectorTakeExpression(bool deserializing) : base(deserializing) { }
|
---|
16 |
|
---|
17 | protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
|
---|
18 | return vectorStack.IsEmpty || interpreter.IntegerStack.IsEmpty;
|
---|
19 | }
|
---|
20 |
|
---|
21 | protected void Eval(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
|
---|
22 |
|
---|
23 | if (vectorStack.Top.Count == 0)
|
---|
24 | return;
|
---|
25 |
|
---|
26 | var count = interpreter.IntegerStack.Pop().AsInt(vectorStack.Top.Count);
|
---|
27 |
|
---|
28 | if (count < 0)
|
---|
29 | count *= -1;
|
---|
30 |
|
---|
31 | var vector = vectorStack.Top;
|
---|
32 | var list = vector as List<T>;
|
---|
33 |
|
---|
34 | if (list != null) {
|
---|
35 | var newTop = new T[count];
|
---|
36 | list.CopyTo(0, newTop, 0, count);
|
---|
37 | vectorStack.Top = newTop;
|
---|
38 | return;
|
---|
39 | }
|
---|
40 |
|
---|
41 | var array = vector as T[];
|
---|
42 | if (array != null) {
|
---|
43 | var newTop = new T[count];
|
---|
44 | Array.Copy(array, 0, newTop, 0, count);
|
---|
45 | vectorStack.Top = newTop;
|
---|
46 | return;
|
---|
47 | }
|
---|
48 |
|
---|
49 | vectorStack.Top = vector.Take(count).ToList();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [StorableClass]
|
---|
54 | [PushExpression(
|
---|
55 | StackTypes.IntegerVector,
|
---|
56 | "INTEGER[].TAKE",
|
---|
57 | "Takes the first n items of the top INTEGER[], where n is from the INTEGER stack.",
|
---|
58 | StackTypes.Integer)]
|
---|
59 | public class IntegerVectorTakeExpression : VectorTakeExpression<long> {
|
---|
60 | public IntegerVectorTakeExpression() { }
|
---|
61 | [StorableConstructor]
|
---|
62 | protected IntegerVectorTakeExpression(bool deserializing) : base(deserializing) { }
|
---|
63 |
|
---|
64 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
65 | return IsNoop(interpreter, interpreter.IntegerVectorStack);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
69 | Eval(interpreter, interpreter.IntegerVectorStack);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | [StorableClass]
|
---|
74 | [PushExpression(
|
---|
75 | StackTypes.FloatVector,
|
---|
76 | "FLOAT[].TAKE",
|
---|
77 | "Takes the first n items of the top FLOAT[], where n is from the INTEGER stack.",
|
---|
78 | StackTypes.Integer)]
|
---|
79 | public class FloatVectorTakeExpression : VectorTakeExpression<double> {
|
---|
80 | public FloatVectorTakeExpression() { }
|
---|
81 | [StorableConstructor]
|
---|
82 | protected FloatVectorTakeExpression(bool deserializing) : base(deserializing) { }
|
---|
83 |
|
---|
84 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
85 | return IsNoop(interpreter, interpreter.FloatVectorStack);
|
---|
86 | }
|
---|
87 |
|
---|
88 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
89 | Eval(interpreter, interpreter.FloatVectorStack);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | [StorableClass]
|
---|
94 | [PushExpression(
|
---|
95 | StackTypes.BooleanVector,
|
---|
96 | "BOOLEAN[].TAKE",
|
---|
97 | "Takes the first n items of the top BOOLEAN[], where n is from the INTEGER stack.",
|
---|
98 | StackTypes.Integer)]
|
---|
99 | public class BooleanVectorTakeExpression : VectorTakeExpression<bool> {
|
---|
100 | public BooleanVectorTakeExpression() { }
|
---|
101 | [StorableConstructor]
|
---|
102 | protected BooleanVectorTakeExpression(bool deserializing) : base(deserializing) { }
|
---|
103 |
|
---|
104 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
105 | return IsNoop(interpreter, interpreter.BooleanVectorStack);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
109 | Eval(interpreter, interpreter.BooleanVectorStack);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | [StorableClass]
|
---|
114 | [PushExpression(
|
---|
115 | StackTypes.StringVector,
|
---|
116 | "STRING[].TAKE",
|
---|
117 | "Takes the first n items of the top STRING[], where n is from the INTEGER stack.",
|
---|
118 | StackTypes.Integer)]
|
---|
119 | public class StringVectorTakeExpression : VectorTakeExpression<string> {
|
---|
120 | public StringVectorTakeExpression() { }
|
---|
121 | [StorableConstructor]
|
---|
122 | protected StringVectorTakeExpression(bool deserializing) : base(deserializing) { }
|
---|
123 |
|
---|
124 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
125 | return IsNoop(interpreter, interpreter.StringVectorStack);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
129 | Eval(interpreter, interpreter.StringVectorStack);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|