Changeset 14745 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs
- Timestamp:
- 03/11/17 12:42:31 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExecExpressions.cs
r14744 r14745 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 2 using System;3 using System.Collections.Generic;4 using System.Linq;5 2 6 3 using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; … … 8 5 9 6 using Interpreter; 10 11 public class ExecExpandExpression : StatefulExpression<PushProgram> {12 public static readonly ExecExpandExpression Empty = new ExecExpandExpression(PushProgram.Empty);13 14 public ExecExpandExpression(PushProgram program)15 : base(program) {16 }17 18 public ExecExpandExpression(params Expression[] state) : this(new PushProgram(state)) {19 }20 21 public ExecExpandExpression(IEnumerable<Expression> state) : this(state.ToArray()) {22 // TODO: lazy evaluation of ToArray()23 }24 25 public override string StringRepresentation { get { return this.State.ToString(); } }26 27 public ExecExpandExpression Copy() {28 // as the empty list is a static stateless expression, no copy required29 return ReferenceEquals(this, Empty) || this.State.IsEmpty30 ? Empty31 : new ExecExpandExpression(this.State.Copy());32 }33 34 public override bool Eval(IPushInterpreter interpreter) {35 interpreter.ExecStack.Push(this.State.Expressions);36 return true;37 }38 39 /// <summary>40 /// Returns the element with the given index whereby the tree is indexed in depth first manner41 /// </summary>42 /// <param name="index">The index of the required element</param>43 /// <returns>The required element</returns>44 public Expression GetFromTree(int index) {45 return GetFromTree(index, (root, parent, child, childIndex, parentIndex) => child);46 }47 48 /// <returns>The required element</returns>49 public T GetFromTree<T>(int index,50 Func<ExecExpandExpression, ExecExpandExpression, Expression, int, int, T> resolver) {51 return GetFromTree(index, 0, null, resolver);52 }53 54 private T GetFromTree<T>(int index, int parentIndex, ExecExpandExpression parent,55 Func<ExecExpandExpression, ExecExpandExpression, Expression, int, int, T> resolver) {56 if (index == 0) return resolver(parent, parent, this, 0, parentIndex);57 58 var min = State.Expressions.Count - 1;59 var max = 0;60 var mid = (min + max) / 2;61 62 while ((max <= min) && (this.State.TreeIndex[mid] != index)) {63 if (this.State.TreeIndex[mid] > index) max = mid + 1;64 else min = mid - 1;65 66 mid = (min + max) / 2;67 }68 69 return this.State.TreeIndex[mid] == index70 ? resolver(parent, this, State.Expressions[mid], mid, parentIndex)71 : (State.Expressions[mid + 1] as ExecExpandExpression).GetFromTree(index - this.State.TreeIndex[mid + 1], mid + 1,72 this, resolver);73 }74 }75 7 76 8 /// <summary> … … 110 42 var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>(); 111 43 112 var result = new ExecExpandExpression(top, execYExpression);44 var result = new PushProgram(top, execYExpression); 113 45 114 46 interpreter.ExecStack.SetTop(result); … … 149 81 var c = interpreter.ExecStack.Top; 150 82 151 var newTop = new ExecExpandExpression(c, b);83 var newTop = new PushProgram(c, b); 152 84 153 85 interpreter.ExecStack.SetTop(newTop);
Note: See TracChangeset
for help on using the changeset viewer.