OLD | NEW |
| (Empty) |
1 library node; | |
2 | |
3 class Node { | |
4 | |
5 // Nodes always have a 'depth' greater than their ancestors'. | |
6 // There's no guarantee regarding depth between siblings. The depth | |
7 // of a node is used to ensure that nodes are processed in depth | |
8 // order. The 'depth' of a child can be more than one greater than | |
9 // the 'depth' of the parent, because the 'depth' values are never | |
10 // decreased: all that matters is that it's greater than the parent. | |
11 // Consider a tree with a root node A, a child B, and a grandchild | |
12 // C. Initially, A will have 'depth' 0, B 'depth' 1, and C 'depth' | |
13 // 2. If C is moved to be a child of A, sibling of B, then the | |
14 // numbers won't change. C's 'depth' will still be 2. | |
15 | |
16 int _depth = 0; | |
17 int get depth => _depth; | |
18 void redepthChild(Node child) { // internal, do not call | |
19 assert(child._attached == _attached); | |
20 if (child._depth <= _depth) { | |
21 child._depth = _depth + 1; | |
22 child.redepthChildren(); | |
23 } | |
24 } | |
25 void redepthChildren() { // internal, do not call | |
26 // override this in subclasses with child nodes | |
27 // simply call redepthChild(child) for each child | |
28 } | |
29 | |
30 bool _attached = false; | |
31 bool get attached => _attached; | |
32 void attach() { | |
33 // override this in subclasses with child nodes | |
34 // simply call attach() for each child then call your superclass | |
35 _attached = true; | |
36 attachChildren(); | |
37 } | |
38 attachChildren() { } // workaround for lack of inter-class mixins in Dart | |
39 void detach() { | |
40 // override this in subclasses with child nodes | |
41 // simply call detach() for each child then call your superclass | |
42 _attached = false; | |
43 detachChildren(); | |
44 } | |
45 detachChildren() { } // workaround for lack of inter-class mixins in Dart | |
46 | |
47 void setAsChild(Node child) { // only for use by subclasses | |
48 assert(child != null); | |
49 if (attached) | |
50 child.attach(); | |
51 redepthChild(child); | |
52 } | |
53 void dropChild(Node child) { // only for use by subclasses | |
54 assert(child != null); | |
55 assert(child.attached == attached); | |
56 if (attached) | |
57 child.detach(); | |
58 } | |
59 | |
60 } | |
OLD | NEW |