Changeset 14730 for branches/PushGP
- Timestamp:
- 03/08/17 10:23:51 (8 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
r14727 r14730 17 17 public readonly bool IsEmpty; 18 18 19 private readonly Lazy<int> depth; 20 private readonly Lazy<int[]> treeIndex; 21 private readonly Lazy<int> hashCode; 22 private readonly Lazy<string> stringRepresentation; 19 20 private string stringRepresentation; 21 private string StringRepresentation { 22 get { 23 if (string.IsNullOrEmpty(stringRepresentation)) 24 stringRepresentation = BuildString(); 25 return stringRepresentation; 26 } 27 } 28 29 private int depth = -1; 30 private int Depth { 31 get { 32 if (depth == -1) depth = CalcDepth(); 33 return depth; 34 } 35 } 36 37 private int[] treeIndex = null; 38 public int[] TreeIndex { 39 get { 40 if (treeIndex == null) treeIndex = BuildTreeIndex(); 41 return treeIndex; 42 } 43 } 44 45 private int hashCode; 46 private int HashCode { 47 get { 48 if (hashCode == default(int)) hashCode = HashExpressions(); 49 return hashCode; 50 } 51 } 23 52 24 53 public PushProgram(Expression[] expressions) { 25 54 this.Expressions = expressions; 26 55 this.IsEmpty = expressions.Length == 0; 27 28 this.depth = new Lazy<int>(this.CalcDepth, true);29 this.treeIndex = new Lazy<int[]>(this.BuildTreeIndex, true);30 this.hashCode = new Lazy<int>(this.HashExpressions, true);31 this.stringRepresentation = new Lazy<string>(this.BuildString, true);32 56 } 33 57 34 public int Depth { get { return depth.Value; } }35 public int[] TreeIndex { get { return treeIndex.Value; } }36 58 37 59 public override int GetHashCode() { 38 return this.hashCode.Value;60 return HashCode; 39 61 } 40 62 41 63 public override string ToString() { 42 return this. stringRepresentation.Value;64 return this.StringRepresentation; 43 65 } 44 66 … … 47 69 /// </summary> 48 70 /// <returns></returns> 49 public int TotalCount 50 { 51 get 52 { 71 public int TotalCount { 72 get { 53 73 return this.IsEmpty 54 74 ? 1 55 56 : this.treeIndex.Value[0] + 1;75 // + 1 because "this" is also counted 76 : TreeIndex[0] + 1; 57 77 } 58 78 } … … 111 131 112 132 private int[] BuildTreeIndex() { 113 var TreeIndex = new int[this.Expressions.Count];133 var local_treeIndex = new int[this.Expressions.Count]; 114 134 115 135 var next = 1; … … 118 138 var subExpression = this.Expressions[i]; 119 139 120 TreeIndex[i] = next;140 local_treeIndex[i] = next; 121 141 122 142 if (subExpression.CanExpand) { … … 127 147 } 128 148 129 return TreeIndex;149 return local_treeIndex; 130 150 } 131 151 -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/StatefullExpression.cs
r14727 r14730 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 2 using System;3 2 4 3 public abstract class StatefullExpression<T> : Expression { 5 4 6 5 public readonly T State; 7 private readonly Lazy<int> hashCode; 6 7 private int hashCode; 8 private int HashCode { 9 get { 10 if (hashCode == default(int)) hashCode = CalcHashCode(); 11 return hashCode; 12 } 13 } 8 14 9 15 protected StatefullExpression(T state) { 10 16 this.State = state; 11 this.hashCode = new Lazy<int>(this.CalcHashCode, true);12 17 } 13 18 … … 30 35 31 36 public override int GetHashCode() { 32 return this.hashCode.Value;37 return HashCode; 33 38 } 34 39 }
Note: See TracChangeset
for help on using the changeset viewer.