Skip to content

Commit 913a12d

Browse files
author
electricessence
committed
Improvements
1 parent 5c3480f commit 913a12d

2 files changed

Lines changed: 46 additions & 7 deletions

File tree

Node.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
using Open.Disposable;
12
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Collections;
5-
using Open.Disposable;
4+
using System.Collections.Generic;
65

76
namespace Open.Hierarchy
87
{
@@ -124,10 +123,15 @@ public Node<T> Root
124123

125124
object IHaveRoot.Root => Root;
126125

127-
internal void Teardown()
126+
public void Teardown()
128127
{
129128
Value = default(T);
130129
Detatch(); // If no parent then this does nothing...
130+
TeardownChildren();
131+
}
132+
133+
public void TeardownChildren()
134+
{
131135
foreach (var c in _children)
132136
{
133137
c._parent = null; // Don't initiate a 'Detach' (which does a lookup) since we are clearing here;
@@ -136,12 +140,21 @@ internal void Teardown()
136140
_children.Clear();
137141
}
138142

139-
internal void Recycle(Factory factory)
143+
public void Recycle(Factory factory)
140144
{
141-
if (factory == null) throw new ArgumentNullException("factory");
145+
if (factory == null)
146+
throw new ArgumentNullException("factory");
142147

143148
Value = default(T);
144149
Detatch(); // If no parent then this does nothing...
150+
RecycleChildren(factory);
151+
}
152+
153+
public void RecycleChildren(Factory factory)
154+
{
155+
if (factory == null)
156+
throw new ArgumentNullException("factory");
157+
145158
foreach (var c in _children)
146159
{
147160
c._parent = null; // Don't initiate a 'Detach' (which does a lookup) since we are clearing here;
@@ -150,7 +163,6 @@ internal void Recycle(Factory factory)
150163
_children.Clear();
151164
}
152165

153-
154166
/// <summary>
155167
/// Used for mapping a tree of evaluations which do not have access to their parent nodes.
156168
/// </summary>
@@ -173,6 +185,13 @@ protected override void OnDispose(bool calledExplicitly)
173185

174186
ConcurrentQueueObjectPool<Node<T>> Pool;
175187

188+
public Node<T> GetBlankNode()
189+
{
190+
AssertIsAlive();
191+
192+
return Pool?.Take();
193+
}
194+
176195
public void Recycle(Node<T> n)
177196
{
178197
AssertIsAlive();

Readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Open.Hierarchy
2+
3+
## ```Node<T>```
4+
One of the important abilities of ```Node<T>``` and its supporting classes is to allow for creating and modifying tree structures that only have a parent to child relationship. It's important to ensure that a node cannot occur multiple times in a tree but an instance of its value can occur any number of times. This facilitates potential 'value sub-trees' that can have dupicate references but not duplicate instances. By using ```Node<T>``` as a container, a single instance can exist multiple times in a tree but still be uniquely indentifyable by its position.
5+
6+
## ```Node<T>.Factory```
7+
8+
### Blank Instance
9+
Calling ```.GetBlankNode()``` will retrieve a blank node from the underlying object pool or create a new one.
10+
11+
### Mapping
12+
Calling ```.Map(root)``` generates node hierarchy map based upon if the root or any of its children implement ```IParent```.
13+
14+
### Cloning
15+
Calling ```.Clone(node)``` creates a copy of the node map.
16+
17+
### Recycling
18+
```Node<T>``` instances can be recycled by calling the ```Factory.Recycle(node)``` method. The node itself and its children are torn down and recycled to an object pool.
19+
20+
***WARNING:*** The ```Factory.Recycle(node)``` method is the one point of potential trouble if multiple references are retained. It must be used with care.

0 commit comments

Comments
 (0)