Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/IntegerExpressions.cs @ 14777

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

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

File size: 5.3 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
6
7  using Interpreter;
8
9  /// <summary>
10  ///     Pushes the sum of the top two items.
11  /// </summary>
12  [PushExpression(StackTypes.Integer, "INTEGER.+")]
13  public class IntegerAddExpression : PushResultExpression<long> {
14    public override bool Eval(IPushInterpreter interpreter) {
15      return Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]);
16    }
17  }
18
19  /// <summary>
20  ///     Pushes the difference of the top two items; that is, the second item minus the top item.
21  /// </summary>
22  [PushExpression(StackTypes.Integer, "INTEGER.-")]
23  public class IntegerSubtractExpression : PushResultExpression<long> {
24    public override bool Eval(IPushInterpreter interpreter) {
25      return Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]);
26    }
27  }
28
29  /// <summary>
30  ///     Pushes the product of the top two items.
31  /// </summary>
32  [PushExpression(StackTypes.Integer, "INTEGER.*")]
33  public class IntegerMultiplyExpression : PushResultExpression<long> {
34    public override bool Eval(IPushInterpreter interpreter) {
35      return Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]);
36    }
37  }
38
39  /// <summary>
40  ///     Pushes the quotient of the top two items; that is, the second item divided by the top item.
41  ///     If the top item is zero this acts as a NOOP.
42  /// </summary>
43  [PushExpression(StackTypes.Integer, "INTEGER./")]
44  public class IntegerDivideExpression : PushResultExpression<long> {
45    public override bool Eval(IPushInterpreter interpreter) {
46      return Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0);
47    }
48  }
49
50  /// <summary>
51  ///     Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP. The modulus is
52  ///     computed as the
53  ///     remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken
54  ///     from the definition
55  ///     for the generic MOD function in Common Lisp, which is described for example at
56  ///     http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
57  /// </summary>
58  [PushExpression(StackTypes.Integer, "INTEGER.%")]
59  public class IntegerModuloExpression : PushResultExpression<long> {
60    public override bool Eval(IPushInterpreter interpreter) {
61      return Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0);
62    }
63  }
64
65  /// <summary>
66  ///     Pushes the minimum of the top two items.
67  /// </summary>
68  [PushExpression(StackTypes.Integer, "INTEGER.MIN")]
69  public class IntegerMinExpression : PushResultExpression<long> {
70    public override bool Eval(IPushInterpreter interpreter) {
71      return Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1]));
72    }
73  }
74
75  /// <summary>
76  ///     Pushes the maximum of the top two items.
77  /// </summary>
78  [PushExpression(StackTypes.Integer, "INTEGER.MAX")]
79  public class IntegerMaxExpression : PushResultExpression<long> {
80    public override bool Eval(IPushInterpreter interpreter) {
81      return Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1]));
82    }
83  }
84
85  /// <summary>
86  ///     Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
87  /// </summary>
88  [PushExpression(StackTypes.Integer, "INTEGER.<", StackTypes.Boolean)]
89  public class IntegerSmallerThanExpression : PushResultExpression<long> {
90    public override bool Eval(IPushInterpreter interpreter) {
91      return Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
92    }
93  }
94
95  /// <summary>
96  ///     Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
97  /// </summary>
98  [PushExpression(StackTypes.Integer, "INTEGER.>", StackTypes.Boolean)]
99  public class IntegerGreaterThanExpression : PushResultExpression<long> {
100    public override bool Eval(IPushInterpreter interpreter) {
101      return Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
102    }
103  }
104
105  /// <summary>
106  ///     Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
107  /// </summary>
108  [PushExpression(StackTypes.Integer, "INTEGER.FROMBOOLEAN", StackTypes.Boolean)]
109  public class IntegerFromBooleanExpression : StatelessExpression {
110    public override bool Eval(IPushInterpreter interpreter) {
111      if (interpreter.BooleanStack.Count == 0) return false;
112
113      var condition = interpreter.BooleanStack.Pop();
114      var value = condition ? 1 : 0;
115
116      interpreter.IntegerStack.Push(value);
117
118      return true;
119    }
120  }
121
122  /// <summary>
123  ///     Pushes the result of truncating the top FLOAT.
124  /// </summary>
125  [PushExpression(StackTypes.Integer, "INTEGER.FROMFLOAT", StackTypes.Float)]
126  public class IntegerFromFloatExpression : StatelessExpression {
127    public override bool Eval(IPushInterpreter interpreter) {
128      if (interpreter.FloatStack.Count == 0) return false;
129
130      var value = (int)interpreter.FloatStack.Pop();
131
132      interpreter.IntegerStack.Push(value);
133
134      return true;
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.