Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/DuplicateExpressions.cs @ 14392

Last change on this file since 14392 was 14392, checked in by pkimmesw, 8 years ago

#2665 Full Push 3.0 instruction set and tests; Added first benchmark test (count odds) for random walk tests;

File size: 3.4 KB
Line 
1using HeuristicLab.Algorithms.PushGP.Interpreter;
2using HeuristicLab.Algorithms.PushGP.Stack;
3using HeuristicLab.Common;
4
5namespace HeuristicLab.Algorithms.PushGP.Expressions
6{
7    /// <summary>
8    /// Duplicates the top item on the INTEGER stack. Does not pop its argument
9    /// (which, if it did, would negate the effect of the duplication!).
10    /// </summary>
11    /// <typeparam name="T">Stacktype</typeparam>
12    public abstract class DuplicateExpression<T> : Expression
13    {
14        public DuplicateExpression(string stringRepresentation) : base(stringRepresentation)
15        { }
16
17        public void Eval(IStack<T> stack)
18        {
19            if (stack.Count == 0)
20                return;
21
22            var clone = this.Clone(stack.Top);
23
24            stack.Push(clone);
25        }
26
27        public abstract T Clone(T original);
28    }
29
30    public class IntegerDuplicateExpression : DuplicateExpression<long>
31    {
32        public IntegerDuplicateExpression() : base("INTEGER.DUP")
33        { }
34
35        public override void Eval(IInterpreter interpreter)
36        {
37            this.Eval(interpreter.IntegerStack);
38        }
39
40        public override long Clone(long value)
41        {
42            return value;
43        }
44    }
45
46    public class FloatDuplicateExpression : DuplicateExpression<double>
47    {
48        public FloatDuplicateExpression() : base("FLOAT.DUP")
49        { }
50
51        public override void Eval(IInterpreter interpreter)
52        {
53            this.Eval(interpreter.FloatStack);
54        }
55
56        public override double Clone(double value)
57        {
58            return value;
59        }
60    }
61
62    public class BooleanDuplicateExpression : DuplicateExpression<bool>
63    {
64        public BooleanDuplicateExpression() : base("BOOLEAN.DUP")
65        { }
66
67        public override void Eval(IInterpreter interpreter)
68        {
69            this.Eval(interpreter.BooleanStack);
70        }
71
72        public override bool Clone(bool value)
73        {
74            return value;
75        }
76    }
77
78    public class NameDuplicateExpression : DuplicateExpression<string>
79    {
80        public NameDuplicateExpression() : base("NAME.DUP")
81        { }
82
83        public override void Eval(IInterpreter interpreter)
84        {
85            this.Eval(interpreter.NameStack);
86        }
87
88        public override string Clone(string value)
89        {
90            return string.Copy(value);
91        }
92    }
93
94    public class ExecDuplicateExpression : DuplicateExpression<Expression>
95    {
96        private readonly Cloner cloner = new Cloner();
97        public ExecDuplicateExpression() : base("EXEC.DUP")
98        { }
99
100        public override void Eval(IInterpreter interpreter)
101        {
102            this.Eval(interpreter.ExecStack);
103        }
104
105        public override Expression Clone(Expression original)
106        {
107            return cloner.Clone(original);
108        }
109    }
110
111    public class CodeDuplicateExpression : DuplicateExpression<Expression>
112    {
113        private readonly Cloner cloner = new Cloner();
114
115        public CodeDuplicateExpression() : base("CODE.DUP")
116        { }
117
118        public override void Eval(IInterpreter interpreter)
119        {
120            this.Eval(interpreter.CodeStack);
121        }
122
123        public override Expression Clone(Expression original)
124        {
125            return cloner.Clone(original);
126        }
127    }
128}
Note: See TracBrowser for help on using the repository browser.