Changeset 3988
- Timestamp:
- 06/30/10 15:25:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
r3905 r3988 39 39 [Storable] 40 40 private Symbol symbol; 41 42 // cached values to prevent unnecessary tree iterations 43 private short size; 44 private short height; 45 private List<SymbolicExpressionTreeNode> prefixForm; 46 private List<SymbolicExpressionTreeNode> postfixForm; 47 41 48 public Symbol Symbol { 42 49 get { return symbol; } … … 64 71 protected SymbolicExpressionTreeNode(SymbolicExpressionTreeNode original) { 65 72 symbol = original.symbol; 66 subTrees = new List<SymbolicExpressionTreeNode>( );73 subTrees = new List<SymbolicExpressionTreeNode>(original.SubTrees.Count); 67 74 foreach (var subtree in original.SubTrees) { 68 75 AddSubTree((SymbolicExpressionTreeNode)subtree.Clone()); … … 91 98 92 99 public int GetSize() { 93 int size = 1; 94 foreach (SymbolicExpressionTreeNode tree in SubTrees) size += tree.GetSize(); 95 return size; 100 if (size > 0) return size; 101 else { 102 size = 1; 103 for (int i = 0; i < SubTrees.Count; i++) size += (short)SubTrees[i].GetSize(); 104 return size; 105 } 96 106 } 97 107 98 108 public int GetHeight() { 99 int maxHeight = 0; 100 foreach (SymbolicExpressionTreeNode tree in SubTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight()); 101 return maxHeight + 1; 109 if (height > 0) return height; 110 else { 111 for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (short)SubTrees[i].GetHeight()); 112 height++; 113 return height; 114 } 102 115 } 103 116 … … 108 121 subTrees.Add(tree); 109 122 tree.Parent = this; 123 ResetCachedValues(); 110 124 } 111 125 … … 113 127 subTrees.Insert(index, tree); 114 128 tree.Parent = this; 129 ResetCachedValues(); 115 130 } 116 131 … … 118 133 subTrees[index].Parent = null; 119 134 subTrees.RemoveAt(index); 135 ResetCachedValues(); 120 136 } 121 137 122 138 public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() { 123 if (SubTrees != null) { 124 return (new SymbolicExpressionTreeNode[] { this }) 125 .Concat(SubTrees.SelectMany(tree => tree.IterateNodesPrefix())); 126 } else { 127 return new SymbolicExpressionTreeNode[] { this }; 139 if (prefixForm == null) { 140 prefixForm = new List<SymbolicExpressionTreeNode>(200); 141 ForEachNodePrefix(x => prefixForm.Add(x)); 142 } 143 return prefixForm; 144 } 145 146 private void ForEachNodePrefix(Action<SymbolicExpressionTreeNode> a) { 147 a(this); 148 for (int i = 0; i < SubTrees.Count; i++) { 149 SubTrees[i].ForEachNodePrefix(a); 128 150 } 129 151 } 130 152 131 153 public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() { 132 if (SubTrees != null) { 133 return SubTrees.SelectMany(tree => tree.IterateNodesPrefix()) 134 .Concat(new SymbolicExpressionTreeNode[] { this }); 135 } else { 136 return new SymbolicExpressionTreeNode[] { this }; 154 if (postfixForm == null) { 155 postfixForm = new List<SymbolicExpressionTreeNode>(200); 156 ForEachNodePostfix(x => postfixForm.Add(x)); 137 157 } 158 return postfixForm; 138 159 } 160 161 private void ForEachNodePostfix(Action<SymbolicExpressionTreeNode> a) { 162 for (int i = 0; i < SubTrees.Count; i++) { 163 SubTrees[i].ForEachNodePrefix(a); 164 } 165 a(this); 166 } 167 139 168 public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) { 140 169 return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex)); … … 158 187 return Symbol.Name; 159 188 } 189 190 private void ResetCachedValues() { 191 size = 0; height = 0; 192 prefixForm = null; postfixForm = null; 193 if (parent != null) parent.ResetCachedValues(); 194 } 160 195 } 161 196 }
Note: See TracChangeset
for help on using the changeset viewer.