OLD | NEW |
1 library node; | 1 library node; |
2 | 2 |
3 class Node { | 3 class AbstractNode { |
4 | 4 |
5 // Nodes always have a 'depth' greater than their ancestors'. | 5 // Nodes always have a 'depth' greater than their ancestors'. |
6 // There's no guarantee regarding depth between siblings. The depth | 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 | 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 | 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 | 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. | 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 | 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' | 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 | 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. | 14 // numbers won't change. C's 'depth' will still be 2. |
15 | 15 |
16 int _depth = 0; | 16 int _depth = 0; |
17 int get depth => _depth; | 17 int get depth => _depth; |
18 void redepthChild(Node child) { // internal, do not call | 18 void redepthChild(AbstractNode child) { // internal, do not call |
19 assert(child._attached == _attached); | 19 assert(child._attached == _attached); |
20 if (child._depth <= _depth) { | 20 if (child._depth <= _depth) { |
21 child._depth = _depth + 1; | 21 child._depth = _depth + 1; |
22 child.redepthChildren(); | 22 child.redepthChildren(); |
23 } | 23 } |
24 } | 24 } |
25 void redepthChildren() { // internal, do not call | 25 void redepthChildren() { // internal, do not call |
26 // override this in subclasses with child nodes | 26 // override this in subclasses with child nodes |
27 // simply call redepthChild(child) for each child | 27 // simply call redepthChild(child) for each child |
28 } | 28 } |
29 | 29 |
30 bool _attached = false; | 30 bool _attached = false; |
31 bool get attached => _attached; | 31 bool get attached => _attached; |
32 void attach() { | 32 void attach() { |
33 // override this in subclasses with child nodes | 33 // override this in subclasses with child nodes |
34 // simply call attach() for each child then call your superclass | 34 // simply call attach() for each child then call your superclass |
35 _attached = true; | 35 _attached = true; |
36 attachChildren(); | 36 attachChildren(); |
37 } | 37 } |
38 attachChildren() { } // workaround for lack of inter-class mixins in Dart | 38 attachChildren() { } // workaround for lack of inter-class mixins in Dart |
39 void detach() { | 39 void detach() { |
40 // override this in subclasses with child nodes | 40 // override this in subclasses with child nodes |
41 // simply call detach() for each child then call your superclass | 41 // simply call detach() for each child then call your superclass |
42 _attached = false; | 42 _attached = false; |
43 detachChildren(); | 43 detachChildren(); |
44 } | 44 } |
45 detachChildren() { } // workaround for lack of inter-class mixins in Dart | 45 detachChildren() { } // workaround for lack of inter-class mixins in Dart |
46 | 46 |
47 Node _parent; | 47 AbstractNode _parent; |
48 Node get parent => _parent; | 48 AbstractNode get parent => _parent; |
49 void adoptChild(Node child) { // only for use by subclasses | 49 void adoptChild(AbstractNode child) { // only for use by subclasses |
50 assert(child != null); | 50 assert(child != null); |
51 assert(child._parent == null); | 51 assert(child._parent == null); |
52 child._parent = this; | 52 child._parent = this; |
53 if (attached) | 53 if (attached) |
54 child.attach(); | 54 child.attach(); |
55 redepthChild(child); | 55 redepthChild(child); |
56 } | 56 } |
57 void dropChild(Node child) { // only for use by subclasses | 57 void dropChild(AbstractNode child) { // only for use by subclasses |
58 assert(child != null); | 58 assert(child != null); |
59 assert(child._parent == this); | 59 assert(child._parent == this); |
60 assert(child.attached == attached); | 60 assert(child.attached == attached); |
61 child._parent = null; | 61 child._parent = null; |
62 if (attached) | 62 if (attached) |
63 child.detach(); | 63 child.detach(); |
64 } | 64 } |
65 | 65 |
66 } | 66 } |
OLD | NEW |