1 collections define: #OrderedTree
2 &parents: {LinkedCollection. ExtensibleSequence}
3 &slots: {#treeParent. "The parent node, Nil for top-level nodes."
4 #children -> ExtensibleArray new "The sub-nodes."}.
5 "A Tree node, linking to its parent node and storing its children in a
8 ot@(OrderedTree traits) clear
10 ot children := ot children new.
14 ot@(OrderedTree traits) new &capacity: n
15 [ot clone `setting: #{#children} to: {ot children new &capacity: n}].
17 ot@(OrderedTree traits) newFor: obj &capacity: n
18 [(ot new &capacity: n) `>> [add: obj]].
20 ot@(OrderedTree traits) newForAll: c@(Collection traits)
21 [ot clone `setting: #{#children} to: {c as: ot children}].
23 ot@(OrderedTree traits) size [ot children size].
25 ot@(OrderedTree traits) do: block [ot children do: block].
27 ot@(OrderedTree traits) at: index [ot children at: index].
29 ot@(OrderedTree traits) iterator
30 [ot children iterator].
32 ot@(OrderedTree traits) reader
35 ot@(OrderedTree traits) writer
36 "NOTE: If the children object is not Extensible. Otherwise, a new structure
37 may be constructed which is not placed into the slot implicitly."
40 ot@(OrderedTree traits) siblings
41 "Return all children of the node's parent, excepting the original node.
42 Answer Nil if there is no parent or the wrong parent."
45 ifNil: [ExtensibleArray new]
46 ifNotNilDo: [| :p | p children copy `>> [remove: ot ifAbsent: [^ Nil]. ]]
49 ot@(OrderedTree traits) previousSibling
50 [ot treeParent ifNotNilDo: [| :p | p children before: ot]].
52 ot@(OrderedTree traits) nextSibling
53 [ot treeParent ifNotNilDo: [| :p | p children after: ot]].
55 ot@(OrderedTree traits) raise
56 "Moves this tree element to the first in the collection list of the parent -
57 relies on being able to directly manipulate that collection."
59 ot treeParent children move: ot to: ot children indexFirst.
62 ot@(OrderedTree traits) bury
63 "Moves this tree element to the last in the children list of the parent -
64 relies on being able to directly manipulate that collection."
66 (siblings := ot treeParent children) move: ot to: siblings indexLast.
69 ch@(OrderedTree traits) reparentTo: ot
70 "Handle the aspect of unsetting any parent link and backlink as necessary.
71 Avoid adding to the new parent, since the position matters."
73 ch treeParent ifNotNilDo: #(remove: ch) `er.
77 obj reparentTo: ot@(OrderedTree traits)
80 ot@(OrderedTree traits) isLeaf: child
81 "Answer whether the given element would not count as a sub-tree of the
82 tree if it were a child of it - if it's anything but a tree itself."
85 ot@(OrderedTree traits) at: index put: ch
88 ot children at: index put: ch.
92 ot@(OrderedTree traits) addFirst: ch
95 ot children addFirst: ch.
99 ot@(OrderedTree traits) addLast: ch
100 "Add a tree node as a child, and set the parent back-link."
103 ot children addLast: ch.
107 ot@(OrderedTree traits) remove: ch
108 "Remove the tree node as a child, making sure to remove the parent link."
110 ot children remove: ch ifAbsent: [^ Nil].
115 ot@(OrderedTree traits) remove: ch ifAbsent: block
117 ot children remove: ch ifAbsent: [block do]