66
77namespace Open . Hierarchy
88{
9-
10- public sealed class Node < T > : ICollection < Node < T > > , IHaveRoot < Node < T > > , IParent < Node < T > >
9+ public sealed class Node < T > : INode < Node < T > >
1110 {
11+ #region IChild<Node<T>> Implementation
1212 Node < T > _parent ;
1313 public Node < T > Parent => _parent ;
14+ object IChild . Parent => _parent ;
15+ #endregion
1416
17+ #region IParent<Node<T>> Implementation
1518 readonly List < Node < T > > _children ;
1619 public IReadOnlyList < Node < T > > Children { get ; private set ; }
1720 IReadOnlyList < object > IParent . Children => Children ;
21+ #endregion
1822
1923 public T Value { get ; set ; }
2024
@@ -23,6 +27,9 @@ public sealed class Node<T> : ICollection<Node<T>>, IHaveRoot<Node<T>>, IParent<
2327 _children = new List < Node < T > > ( ) ;
2428 Children = _children . AsReadOnly ( ) ;
2529 }
30+
31+ // WARNING: Care must be taken not to have duplicate nodes anywhere in the tree but having duplicate values are allowed.
32+
2633 #region ICollection<Node<T>> Implementation
2734 public bool IsReadOnly => false ;
2835
@@ -98,66 +105,47 @@ public void Detatch()
98105 }
99106
100107
101- /// <summary>
102- /// Iterates through all of the descendants of this node starting breadth first.
103- /// </summary>
104- /// <returns>All the descendants of this node.</returns>
105- public IEnumerable < Node < T > > GetDescendants ( )
106- {
107- // Attempt to be more breadth first.
108-
109- foreach ( var child in _children )
110- yield return child ;
111-
112- var grandchildren = _children . SelectMany ( c => c ) ;
113- foreach ( var grandchild in grandchildren )
114- yield return grandchild ;
115-
116- foreach ( var descendant in grandchildren . SelectMany ( c => c . GetDescendants ( ) ) )
117- yield return descendant ;
118- }
119-
120- /// <returns>This and all of its descendants.</returns>
121- public IEnumerable < Node < T > > GetNodes ( )
122- {
123- yield return this ;
124- foreach ( var descendant in GetDescendants ( ) )
125- yield return descendant ;
126- }
127-
128108 /// <summary>
129109 /// Finds the root node of this tree.
130110 /// </summary>
131111 public Node < T > Root
132112 {
133113 get
134114 {
135- var current = this ;
136- while ( current . _parent != null )
137- current = current . _parent ;
115+ Node < T > current = this ;
116+ Node < T > parent ;
117+ while ( ( parent = current . _parent ) != null )
118+ {
119+ current = parent ;
120+ }
138121 return current ;
139122 }
140123 }
141124
142- internal void Teardown ( Factory factory = null )
125+ object IHaveRoot . Root => Root ;
126+
127+ internal void Teardown ( )
143128 {
144129 Value = default ( T ) ;
145130 Detatch ( ) ; // If no parent then this does nothing...
146- if ( factory == null )
131+ foreach ( var c in _children )
147132 {
148- foreach ( var c in _children )
149- {
150- c . _parent = null ; // Don't initiate a 'Detach' (which does a lookup) since we are clearing here;
151- c . Teardown ( ) ;
152- }
133+ c . _parent = null ; // Don't initiate a 'Detach' (which does a lookup) since we are clearing here;
134+ c . Teardown ( ) ;
153135 }
154- else
136+ _children . Clear ( ) ;
137+ }
138+
139+ internal void Recycle ( Factory factory )
140+ {
141+ if ( factory == null ) throw new ArgumentNullException ( "factory" ) ;
142+
143+ Value = default ( T ) ;
144+ Detatch ( ) ; // If no parent then this does nothing...
145+ foreach ( var c in _children )
155146 {
156- foreach ( var c in _children )
157- {
158- c . _parent = null ; // Don't initiate a 'Detach' (which does a lookup) since we are clearing here;
159- factory . RecycleInternal ( c ) ;
160- }
147+ c . _parent = null ; // Don't initiate a 'Detach' (which does a lookup) since we are clearing here;
148+ factory . RecycleInternal ( c ) ;
161149 }
162150 _children . Clear ( ) ;
163151 }
@@ -201,12 +189,10 @@ internal void RecycleInternal(Node<T> n)
201189
202190 void PrepareForPool ( Node < T > n )
203191 {
204- n . Teardown ( this ) ;
192+ n . Recycle ( this ) ;
205193 }
206194 #endregion
207195
208- // WARNING: Care must be taken not to have duplicate nodes anywhere in the tree but having duplicate values are allowed.
209-
210196 /// <summary>
211197 /// Clones a node by recreating the tree and copying the values.
212198 /// </summary>
@@ -268,7 +254,7 @@ public Node<T> CloneTree(Node<T> target)
268254 /// <param name="root">The root instance.</param>
269255 /// <returns>The full map of the root.</returns>
270256 public Node < T > Map < TRoot > ( TRoot root )
271- where TRoot : T
257+ where TRoot : T
272258 {
273259 AssertIsAlive ( ) ;
274260
0 commit comments