Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/VectorRestExpressions.cs @ 17021

Last change on this file since 17021 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 3.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5
6namespace HeuristicLab.Problems.ProgramSynthesis {
7  /// <summary>
8  /// Takes the rest of the top item on the type stack.
9  /// </summary>
10  /// <typeparam name="T"></typeparam>
11  [StorableClass]
12  public abstract class VectorRestExpression<T> : StatelessExpression {
13    protected VectorRestExpression() { }
14    [StorableConstructor]
15    protected VectorRestExpression(bool deserializing) : base(deserializing) { }
16
17    protected bool IsNoop(IPushStack<IReadOnlyList<T>> vectorStack) {
18      return vectorStack.IsEmpty || vectorStack.Top.Count < 2;
19    }
20
21    protected void Eval(IPushStack<IReadOnlyList<T>> vectorStack) {
22      var vector = vectorStack.Top;
23      var list = vector as List<T>;
24      var newLength = vector.Count - 1;
25      if (list != null) {
26        var newTop = new T[newLength];
27        list.CopyTo(1, newTop, 0, newLength);
28        vectorStack.Top = newTop;
29        return;
30      }
31
32      var array = vector as T[];
33      if (array != null) {
34        var newTop = new T[newLength];
35        Array.Copy(array, 1, newTop, 0, newLength);
36        vectorStack.Top = newTop;
37        return;
38      }
39
40      vectorStack.Top = vector.Skip(1).ToList();
41    }
42  }
43
44  [StorableClass]
45  [PushExpression(
46    StackTypes.IntegerVector,
47    "INTEGER[].REST",
48    "Removes the first item of the top INTEGER[].")]
49  public class IntegerVectorRestExpression : VectorRestExpression<long> {
50    public IntegerVectorRestExpression() { }
51    [StorableConstructor]
52    protected IntegerVectorRestExpression(bool deserializing) : base(deserializing) { }
53
54    public override bool IsNoop(IInternalPushInterpreter interpreter) {
55      return IsNoop(interpreter.IntegerVectorStack);
56    }
57
58    public override void Eval(IInternalPushInterpreter interpreter) {
59      Eval(interpreter.IntegerVectorStack);
60    }
61  }
62
63  [StorableClass]
64  [PushExpression(
65    StackTypes.FloatVector,
66    "FLOAT[].REST",
67    "Removes the first item of the top FLOAT[].")]
68  public class FloatVectorRestExpression : VectorRestExpression<double> {
69    public FloatVectorRestExpression() { }
70    [StorableConstructor]
71    protected FloatVectorRestExpression(bool deserializing) : base(deserializing) { }
72
73    public override bool IsNoop(IInternalPushInterpreter interpreter) {
74      return IsNoop(interpreter.FloatVectorStack);
75    }
76
77    public override void Eval(IInternalPushInterpreter interpreter) {
78      Eval(interpreter.FloatVectorStack);
79    }
80  }
81
82  [StorableClass]
83  [PushExpression(
84    StackTypes.BooleanVector,
85    "BOOLEAN[].REST",
86    "Removes the first item of the top BOOLEAN[].")]
87  public class BooleanVectorRestExpression : VectorRestExpression<bool> {
88    public BooleanVectorRestExpression() { }
89    [StorableConstructor]
90    protected BooleanVectorRestExpression(bool deserializing) : base(deserializing) { }
91
92    public override bool IsNoop(IInternalPushInterpreter interpreter) {
93      return IsNoop(interpreter.BooleanVectorStack);
94    }
95
96    public override void Eval(IInternalPushInterpreter interpreter) {
97      Eval(interpreter.BooleanVectorStack);
98    }
99  }
100
101  [StorableClass]
102  [PushExpression(
103    StackTypes.StringVector,
104    "STRING[].REST",
105    "Removes the first item of the top STRING[].")]
106  public class StringVectorRestExpression : VectorRestExpression<string> {
107    public StringVectorRestExpression() { }
108    [StorableConstructor]
109    protected StringVectorRestExpression(bool deserializing) : base(deserializing) { }
110
111    public override bool IsNoop(IInternalPushInterpreter interpreter) {
112      return IsNoop(interpreter.StringVectorStack);
113    }
114
115    public override void Eval(IInternalPushInterpreter interpreter) {
116      Eval(interpreter.StringVectorStack);
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.