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