Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ShoveExpressions.cs @ 14875

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

#2665 BenchmarkSuite, all examples, partially tested, VectorExpressions added

File size: 4.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3  using System.Collections.Generic;
4
5  using Attributes;
6  using Interpreter;
7  using Stack;
8
9  public abstract class ShoveExpression<T> : StatelessExpression {
10    protected bool Eval(IPushStack<T> stack, IPushStack<long> integerStack) {
11      if ((stack == integerStack && integerStack.Count < 3) ||
12          (stack != integerStack && (integerStack.IsEmpty || stack.Count < 2)))
13        return false;
14
15      var index = (int)Math.Abs(integerStack.Pop() % stack.Count);
16      var item = stack.Pop();
17      stack.Insert(index, item);
18      return true;
19    }
20  }
21
22  [PushExpression(StackTypes.Integer, "INTEGER.SHOVE")]
23  public class IntegerShoveExpression : ShoveExpression<long> {
24    public override bool Eval(IInternalPushInterpreter interpreter) {
25      return Eval(interpreter.IntegerStack, interpreter.IntegerStack);
26    }
27  }
28
29  [PushExpression(StackTypes.Float, "FLOAT.SHOVE", StackTypes.Integer)]
30  public class FloatShoveExpression : ShoveExpression<double> {
31    public override bool Eval(IInternalPushInterpreter interpreter) {
32      return Eval(interpreter.FloatStack, interpreter.IntegerStack);
33    }
34  }
35
36  [PushExpression(StackTypes.Boolean, "BOOLEAN.SHOVE", StackTypes.Integer)]
37  public class BooleanShoveExpression : ShoveExpression<bool> {
38    public override bool Eval(IInternalPushInterpreter interpreter) {
39      return Eval(interpreter.BooleanStack, interpreter.IntegerStack);
40    }
41  }
42
43  [PushExpression(StackTypes.Name, "NAME.SHOVE", StackTypes.Integer)]
44  public class NameShoveExpression : ShoveExpression<string> {
45    public override bool Eval(IInternalPushInterpreter interpreter) {
46      return Eval(interpreter.NameStack, interpreter.IntegerStack);
47    }
48  }
49
50  [PushExpression(StackTypes.Exec, "EXEC.SHOVE", StackTypes.Integer)]
51  public class ExecShoveExpression : ShoveExpression<Expression> {
52    public override bool Eval(IInternalPushInterpreter interpreter) {
53      return Eval(interpreter.ExecStack, interpreter.IntegerStack);
54    }
55  }
56
57  [PushExpression(StackTypes.Code, "CODE.SHOVE", StackTypes.Integer)]
58  public class CodeShoveExpression : ShoveExpression<Expression> {
59    public override bool Eval(IInternalPushInterpreter interpreter) {
60      return Eval(interpreter.CodeStack, interpreter.IntegerStack);
61    }
62  }
63
64  [PushExpression(StackTypes.Char, "CHAR.SHOVE", StackTypes.Integer)]
65  public class CharShoveExpression : ShoveExpression<char> {
66    public override bool Eval(IInternalPushInterpreter interpreter) {
67      return Eval(interpreter.CharStack, interpreter.IntegerStack);
68    }
69  }
70
71  [PushExpression(StackTypes.String, "STRING.SHOVE", StackTypes.Integer)]
72  public class StringShoveExpression : ShoveExpression<string> {
73    public override bool Eval(IInternalPushInterpreter interpreter) {
74      return Eval(interpreter.StringStack, interpreter.IntegerStack);
75    }
76  }
77
78  [PushExpression(StackTypes.IntegerVector, "INTEGER[].SHOVE", StackTypes.Integer)]
79  public class IntegerVectorShoveExpression : ShoveExpression<List<long>> {
80    public override bool Eval(IInternalPushInterpreter interpreter) {
81      return Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
82    }
83  }
84
85  [PushExpression(StackTypes.FloatVector, "FLOAT[].SHOVE", StackTypes.Integer)]
86  public class FloatVectorShoveExpression : ShoveExpression<List<double>> {
87    public override bool Eval(IInternalPushInterpreter interpreter) {
88      return Eval(interpreter.FloatVectorStack, interpreter.IntegerStack);
89    }
90  }
91
92  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].SHOVE", StackTypes.Integer)]
93  public class BooleanVectorShoveExpression : ShoveExpression<List<bool>> {
94    public override bool Eval(IInternalPushInterpreter interpreter) {
95      return Eval(interpreter.BooleanVectorStack, interpreter.IntegerStack);
96    }
97  }
98
99  [PushExpression(StackTypes.StringVector, "STRING[].SHOVE", StackTypes.Integer)]
100  public class StringVectorShoveExpression : ShoveExpression<List<string>> {
101    public override bool Eval(IInternalPushInterpreter interpreter) {
102      return Eval(interpreter.StringVectorStack, interpreter.IntegerStack);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.