- Timestamp:
- 04/22/10 18:41:07 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/SimpleArithmeticExpressionInterpreter.cs
r3462 r3491 33 33 [StorableClass] 34 34 [Item("SimpleArithmeticExpressionInterpreter", "Interpreter for arithmetic symbolic expression trees including function calls.")] 35 // not thread safe! 35 36 public class SimpleArithmeticExpressionInterpreter : Item, ISymbolicExpressionTreeInterpreter { 36 37 private class OpCodes { … … 45 46 } 46 47 48 private const int ARGUMENT_STACK_SIZE = 1024; 47 49 private Dataset dataset; 48 50 private int row; … … 58 60 this.row = row; 59 61 pc = 0; 60 arg umentStack.Clear();62 argStackPointer = 0; 61 63 var estimatedValue = Evaluate(); 62 64 if (double.IsNaN(estimatedValue) || double.IsInfinity(estimatedValue)) yield return 0.0; … … 85 87 } 86 88 87 private Stack<List<double>> argumentStack = new Stack<List<double>>(); 89 private double[] argumentStack = new double[ARGUMENT_STACK_SIZE]; 90 private int argStackPointer; 91 88 92 public double Evaluate() { 89 93 var currentInstr = code[pc++]; … … 118 122 } 119 123 case OpCodes.Call: { 120 // save current arguments121 List<double> arguments = new List<double>();122 124 // evaluate sub-trees 125 // push on argStack in reverse order 123 126 for (int i = 0; i < currentInstr.nArguments; i++) { 124 arguments.Add(Evaluate()); 127 argumentStack[argStackPointer + currentInstr.nArguments - i] = Evaluate(); 128 argStackPointer++; 125 129 } 126 argumentStack.Push(arguments); 130 127 131 // save the pc 128 132 int nextPc = pc; … … 131 135 // evaluate the function 132 136 double v = Evaluate(); 133 argumentStack.Pop(); 137 138 // decrease the argument stack pointer by the number of arguments pushed 139 // to set the argStackPointer back to the original location 140 argStackPointer -= currentInstr.nArguments; 141 134 142 // restore the pc => evaluation will continue at point after my subtrees 135 143 pc = nextPc; … … 137 145 } 138 146 case OpCodes.Arg: { 139 return argumentStack .Peek()[currentInstr.iArg0];147 return argumentStack[argStackPointer - currentInstr.iArg0]; 140 148 } 141 149 case OpCodes.Variable: {
Note: See TracChangeset
for help on using the changeset viewer.