Changeset 14744 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
- Timestamp:
- 03/10/17 21:42:09 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PushProgram.cs
r14730 r14744 2 2 3 3 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { 4 using System;5 4 using System.Diagnostics; 6 5 using System.Linq; … … 14 13 private const string Delimiter = " "; 15 14 15 public static readonly PushProgram Empty = new PushProgram(new Expression[0]); 16 16 17 public readonly IReadOnlyList<Expression> Expressions; 17 public readonly bool IsEmpty; 18 18 public bool IsEmpty { get; private set; } 19 20 public PushProgram(Expression[] expressions) { 21 this.Expressions = expressions; 22 this.IsEmpty = expressions.Length == 0; 23 } 19 24 20 25 private string stringRepresentation; 21 private string StringRepresentation { 22 get { 26 private string StringRepresentation 27 { 28 get 29 { 23 30 if (string.IsNullOrEmpty(stringRepresentation)) 24 31 stringRepresentation = BuildString(); … … 28 35 29 36 private int depth = -1; 30 private int Depth { 31 get { 37 private int Depth 38 { 39 get 40 { 32 41 if (depth == -1) depth = CalcDepth(); 33 42 return depth; … … 36 45 37 46 private int[] treeIndex = null; 38 public int[] TreeIndex { 39 get { 40 if (treeIndex == null) treeIndex = BuildTreeIndex(); 41 return treeIndex; 47 public int[] TreeIndex 48 { 49 get 50 { 51 return this.treeIndex ?? (this.treeIndex = this.BuildTreeIndex()); 42 52 } 43 53 } 44 54 45 55 private int hashCode; 46 private int HashCode { 47 get { 56 private int HashCode 57 { 58 get 59 { 48 60 if (hashCode == default(int)) hashCode = HashExpressions(); 49 61 return hashCode; 50 62 } 51 63 } 52 53 public PushProgram(Expression[] expressions) {54 this.Expressions = expressions;55 this.IsEmpty = expressions.Length == 0;56 }57 58 64 59 65 public override int GetHashCode() { … … 69 75 /// </summary> 70 76 /// <returns></returns> 71 public int TotalCount { 72 get { 77 public int TotalCount 78 { 79 get 80 { 73 81 return this.IsEmpty 74 82 ? 1 75 // + 1 because "this" is also counted83 // + 1 because "this" is also counted 76 84 : TreeIndex[0] + 1; 77 85 } … … 90 98 91 99 public PushProgram Copy() { 92 var copy = this.CopyExpressions(); 93 94 return new PushProgram(copy); 100 return IsEmpty ? this : new PushProgram((Expression[])Expressions) { 101 stringRepresentation = this.stringRepresentation, 102 hashCode = this.hashCode, 103 depth = this.depth, 104 treeIndex = this.treeIndex 105 }; 95 106 } 96 107 97 108 public Expression[] CopyExpressions() { 98 109 var copy = new Expression[this.Expressions.Count]; 99 Array.Copy((Expression[])this.Expressions, copy, this.Expressions.Count);110 ((Expression[])this.Expressions).CopyTo(copy, Expressions.Count); 100 111 101 112 return copy;
Note: See TracChangeset
for help on using the changeset viewer.