Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17104 was 15189, checked in by pkimmesw, 7 years ago

#2665 Fixed small issues, testet benchmark suite, added INX Expressions

File size: 4.7 KB
Line 
1using System.Collections.Generic;
2
3namespace 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).ToList();
56    }
57  }
58
59  [StorableClass]
60  [PushExpression(
61    StackTypes.IntegerVector,
62    "INTEGER[].TAKE",
63    "Takes the first n items of the top INTEGER[], where n is from the INTEGER stack.",
64    StackTypes.Integer)]
65  public class IntegerVectorTakeExpression : VectorTakeExpression<long> {
66    public IntegerVectorTakeExpression() { }
67    [StorableConstructor]
68    protected IntegerVectorTakeExpression(bool deserializing) : base(deserializing) { }
69
70    public override bool IsNoop(IInternalPushInterpreter interpreter) {
71      return IsNoop(interpreter, interpreter.IntegerVectorStack);
72    }
73
74    public override void Eval(IInternalPushInterpreter interpreter) {
75      Eval(interpreter, interpreter.IntegerVectorStack);
76    }
77  }
78
79  [StorableClass]
80  [PushExpression(
81    StackTypes.FloatVector,
82    "FLOAT[].TAKE",
83    "Takes the first n items of the top FLOAT[], where n is from the INTEGER stack.",
84    StackTypes.Integer)]
85  public class FloatVectorTakeExpression : VectorTakeExpression<double> {
86    public FloatVectorTakeExpression() { }
87    [StorableConstructor]
88    protected FloatVectorTakeExpression(bool deserializing) : base(deserializing) { }
89
90    public override bool IsNoop(IInternalPushInterpreter interpreter) {
91      return IsNoop(interpreter, interpreter.FloatVectorStack);
92    }
93
94    public override void Eval(IInternalPushInterpreter interpreter) {
95      Eval(interpreter, interpreter.FloatVectorStack);
96    }
97  }
98
99  [StorableClass]
100  [PushExpression(
101    StackTypes.BooleanVector,
102    "BOOLEAN[].TAKE",
103    "Takes the first n items of the top BOOLEAN[], where n is from the INTEGER stack.",
104    StackTypes.Integer)]
105  public class BooleanVectorTakeExpression : VectorTakeExpression<bool> {
106    public BooleanVectorTakeExpression() { }
107    [StorableConstructor]
108    protected BooleanVectorTakeExpression(bool deserializing) : base(deserializing) { }
109
110    public override bool IsNoop(IInternalPushInterpreter interpreter) {
111      return IsNoop(interpreter, interpreter.BooleanVectorStack);
112    }
113
114    public override void Eval(IInternalPushInterpreter interpreter) {
115      Eval(interpreter, interpreter.BooleanVectorStack);
116    }
117  }
118
119  [StorableClass]
120  [PushExpression(
121    StackTypes.StringVector,
122    "STRING[].TAKE",
123    "Takes the first n items of the top STRING[], where n is from the INTEGER stack.",
124    StackTypes.Integer)]
125  public class StringVectorTakeExpression : VectorTakeExpression<string> {
126    public StringVectorTakeExpression() { }
127    [StorableConstructor]
128    protected StringVectorTakeExpression(bool deserializing) : base(deserializing) { }
129
130    public override bool IsNoop(IInternalPushInterpreter interpreter) {
131      return IsNoop(interpreter, interpreter.StringVectorStack);
132    }
133
134    public override void Eval(IInternalPushInterpreter interpreter) {
135      Eval(interpreter, interpreter.StringVectorStack);
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.