Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/EqualsExpressions.cs @ 14834

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

#2665 LexicaseSelector, Performance improvements, UI Fixes, Debugger only shows used stacks, fixed Debugger stepping, Added vector expressions, ERCOptions,

File size: 3.5 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Collections.Generic;
3
4  using HeuristicLab.Common;
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  ///     Pushes TRUE onto the BOOLEAN stack if the top two items are equal, or FALSE otherwise
11  /// </summary>
12  /// <typeparam name="T">Stacktype</typeparam>
13  public abstract class EqualsExpression<T> : StatelessExpression {
14    public bool Eval(IPushStack<T> stack, IPushStack<bool> booleanStack) {
15      if (stack.Count < 2) return false;
16
17      var items = stack.Pop(2);
18
19      booleanStack.Push(items[0].Equals(items[1]));
20
21      return true;
22    }
23  }
24
25  [PushExpression(StackTypes.Integer, "INTEGER.=", StackTypes.Boolean)]
26  public class IntegerEqualsExpression : EqualsExpression<long> {
27    public override bool Eval(IInternalPushInterpreter interpreter) {
28      return Eval(interpreter.IntegerStack, interpreter.BooleanStack);
29    }
30  }
31
32  [PushExpression(StackTypes.Float, "FLOAT.=", StackTypes.Boolean)]
33  public class FloatEqualsExpression : EqualsExpression<double> {
34    public override bool Eval(IInternalPushInterpreter interpreter) {
35      if (interpreter.FloatStack.Count < 2) return false;
36
37      var items = interpreter.FloatStack.Pop(2);
38      interpreter.BooleanStack.Push(items[0].IsAlmost(items[1]));
39      return true;
40    }
41  }
42
43  [PushExpression(StackTypes.Boolean, "BOOLEAN.=", StackTypes.Boolean)]
44  public class BooleanEqualsExpression : EqualsExpression<bool> {
45    public override bool Eval(IInternalPushInterpreter interpreter) {
46      return Eval(interpreter.BooleanStack, interpreter.BooleanStack);
47    }
48  }
49
50  [PushExpression(StackTypes.Name, "NAME.=", StackTypes.Boolean)]
51  public class NameEqualsExpression : EqualsExpression<string> {
52    public override bool Eval(IInternalPushInterpreter interpreter) {
53      return Eval(interpreter.NameStack, interpreter.BooleanStack);
54    }
55  }
56
57  [PushExpression(StackTypes.Exec, "EXEC.=", StackTypes.Boolean)]
58  public class ExecEqualsExpression : EqualsExpression<Expression> {
59    public override bool Eval(IInternalPushInterpreter interpreter) {
60      return Eval(interpreter.ExecStack, interpreter.BooleanStack);
61    }
62  }
63
64  [PushExpression(StackTypes.Code, "CODE.=", StackTypes.Boolean)]
65  public class CodeEqualsExpression : EqualsExpression<Expression> {
66    public override bool Eval(IInternalPushInterpreter interpreter) {
67      return Eval(interpreter.CodeStack, interpreter.BooleanStack);
68    }
69  }
70
71  [PushExpression(StackTypes.Char, "CHAR.=", StackTypes.Boolean)]
72  public class CharEqualsExpression : EqualsExpression<char> {
73    public override bool Eval(IInternalPushInterpreter interpreter) {
74      return Eval(interpreter.CharStack, interpreter.BooleanStack);
75    }
76  }
77
78  [PushExpression(StackTypes.String, "STRING.=", StackTypes.Boolean)]
79  public class StringEqualsExpression : EqualsExpression<string> {
80    public override bool Eval(IInternalPushInterpreter interpreter) {
81      return Eval(interpreter.StringStack, interpreter.BooleanStack);
82    }
83  }
84
85  [PushExpression(StackTypes.IntegerVector, "INTEGER[].=", StackTypes.Boolean)]
86  public class IntegerVectorEqualsExpression : EqualsExpression<List<long>> {
87    public override bool Eval(IInternalPushInterpreter interpreter) {
88      return Eval(interpreter.IntegerVectorStack, interpreter.BooleanStack);
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.