Changeset 5510 for branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs
- Timestamp:
- 02/17/11 13:51:04 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeNode.cs
r5499 r5510 31 31 public class SymbolicExpressionTreeNode : DeepCloneable, ISymbolicExpressionTreeNode { 32 32 [Storable] 33 private IList< SymbolicExpressionTreeNode> subTrees;33 private IList<ISymbolicExpressionTreeNode> subTrees; 34 34 [Storable] 35 private Symbol symbol;35 private ISymbol symbol; 36 36 37 37 // cached values to prevent unnecessary tree iterations … … 39 39 private ushort height; 40 40 41 public Symbol Symbol {41 public ISymbol Symbol { 42 42 get { return symbol; } 43 43 protected set { symbol = value; } … … 45 45 46 46 // parent relation is not persisted or cloned (will be set on AddSubtree or RemoveSubtree) 47 private SymbolicExpressionTreeNode parent;48 internalSymbolicExpressionTreeNode Parent {47 private ISymbolicExpressionTreeNode parent; 48 public ISymbolicExpressionTreeNode Parent { 49 49 get { return parent; } 50 50 set { parent = value; } … … 56 56 : base() { 57 57 symbol = original.symbol; // symbols are reused 58 subTrees = new List< SymbolicExpressionTreeNode>(original.SubTrees.Count);59 foreach (var subtree in original. SubTrees) {58 subTrees = new List<ISymbolicExpressionTreeNode>(original.subTrees.Count); 59 foreach (var subtree in original.subTrees) { 60 60 var clonedSubTree = cloner.Clone(subtree); 61 61 subTrees.Add(clonedSubTree); … … 73 73 } 74 74 75 public SymbolicExpressionTreeNode( Symbol symbol)75 public SymbolicExpressionTreeNode(ISymbol symbol) 76 76 : base() { 77 subTrees = new List< SymbolicExpressionTreeNode>(3);77 subTrees = new List<ISymbolicExpressionTreeNode>(3); 78 78 this.symbol = symbol; 79 79 } … … 82 82 [StorableHook(HookType.AfterDeserialization)] 83 83 private void AfterDeserialization() { 84 foreach (var subtree in SubTrees) {84 foreach (var subtree in subTrees) { 85 85 subtree.Parent = this; 86 86 } … … 91 91 } 92 92 93 public virtual I List<SymbolicExpressionTreeNode> SubTrees {93 public virtual IEnumerable<ISymbolicExpressionTreeNode> SubTrees { 94 94 get { return subTrees; } 95 95 } … … 103 103 else { 104 104 size = 1; 105 if ( SubTrees != null) {106 for (int i = 0; i < SubTrees.Count; i++) {107 checked { size += (ushort) SubTrees[i].GetSize(); }105 if (subTrees != null) { 106 for (int i = 0; i < subTrees.Count; i++) { 107 checked { size += (ushort)subTrees[i].GetSize(); } 108 108 } 109 109 } … … 115 115 if (height > 0) return height; 116 116 else { 117 if ( SubTrees != null) {118 for (int i = 0; i < SubTrees.Count; i++) height = Math.Max(height, (ushort)SubTrees[i].GetHeight());117 if (subTrees != null) { 118 for (int i = 0; i < subTrees.Count; i++) height = Math.Max(height, (ushort)subTrees[i].GetHeight()); 119 119 } 120 120 height++; … … 126 126 public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { } 127 127 128 public virtual void AddSubTree(SymbolicExpressionTreeNode tree) { 129 SubTrees.Add(tree); 128 public virtual ISymbolicExpressionTreeNode GetSubTree(int index) { 129 return subTrees[index]; 130 } 131 public virtual int IndexOfSubTree(ISymbolicExpressionTreeNode tree) { 132 return subTrees.IndexOf(tree); 133 } 134 public virtual void AddSubTree(ISymbolicExpressionTreeNode tree) { 135 subTrees.Add(tree); 130 136 tree.Parent = this; 131 137 ResetCachedValues(); 132 138 } 133 134 public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) { 135 SubTrees.Insert(index, tree); 139 public virtual void InsertSubTree(int index, ISymbolicExpressionTreeNode tree) { 140 subTrees.Insert(index, tree); 136 141 tree.Parent = this; 137 142 ResetCachedValues(); 138 143 } 139 140 144 public virtual void RemoveSubTree(int index) { 141 SubTrees[index].Parent = null;142 SubTrees.RemoveAt(index);145 subTrees[index].Parent = null; 146 subTrees.RemoveAt(index); 143 147 ResetCachedValues(); 144 148 } 145 149 146 public IEnumerable< SymbolicExpressionTreeNode> IterateNodesPrefix() {147 List< SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();150 public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPrefix() { 151 List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>(); 148 152 ForEachNodePrefix((n) => list.Add(n)); 149 153 return list; 150 154 } 151 155 152 public void ForEachNodePrefix(Action< SymbolicExpressionTreeNode> a) {156 public void ForEachNodePrefix(Action<ISymbolicExpressionTreeNode> a) { 153 157 a(this); 154 158 if (SubTrees != null) { 155 for (int i = 0; i < SubTrees.Count; i++) {156 SubTrees[i].ForEachNodePrefix(a);157 } 158 } 159 } 160 161 public IEnumerable< SymbolicExpressionTreeNode> IterateNodesPostfix() {162 List< SymbolicExpressionTreeNode> list = new List<SymbolicExpressionTreeNode>();159 foreach (var subtree in SubTrees) { 160 subtree.ForEachNodePrefix(a); 161 } 162 } 163 } 164 165 public IEnumerable<ISymbolicExpressionTreeNode> IterateNodesPostfix() { 166 List<ISymbolicExpressionTreeNode> list = new List<ISymbolicExpressionTreeNode>(); 163 167 ForEachNodePostfix((n) => list.Add(n)); 164 168 return list; 165 169 } 166 170 167 public void ForEachNodePostfix(Action< SymbolicExpressionTreeNode> a) {171 public void ForEachNodePostfix(Action<ISymbolicExpressionTreeNode> a) { 168 172 if (SubTrees != null) { 169 for (int i = 0; i < SubTrees.Count; i++) {170 SubTrees[i].ForEachNodePostfix(a);173 foreach (var subtree in SubTrees) { 174 subtree.ForEachNodePostfix(a); 171 175 } 172 176 } … … 190 194 private void ResetCachedValues() { 191 195 size = 0; height = 0; 192 if (parent != null) parent.ResetCachedValues(); 196 SymbolicExpressionTreeNode parentNode = parent as SymbolicExpressionTreeNode; 197 if (parentNode != null) parentNode.ResetCachedValues(); 193 198 } 194 199 }
Note: See TracChangeset
for help on using the changeset viewer.