Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorLengthExpressions.cs @ 17133

Last change on this file since 17133 was 15032, checked in by pkimmesw, 7 years ago

#2665 Fixed bias 0 issue, PushExpressionFrequencyAnalyzer, Fixed probability for ERC settings, Fixed enable/disable instructions, Added expression descriptions

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