Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/data.scoop

Selected production source · src/tree/data.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.ViewKey
6/** Known([]) is an empty loaded branch; Deferred is the only unknown child state. */
7public enum TreeChildren<T> {
8 Leaf, Known(Array<TreeNode<T>>), Deferred
9}
11/** Immutable node metadata. The label names the node independently of custom cell views. */
12public class TreeNode<T> public constructor(
13 public val key: ViewKey,
14 public val value: T,
15 public val label: String,
16 public val children: TreeChildren<T> = TreeChildren.Leaf,
17 public val enabled: Boolean = true,
18 public val selectable: Boolean = true,
19 public val checkable: Boolean = true,
20 public val checkboxEnabled: Boolean = true,
21 public val draggable: Boolean = true
22) {
23 init {
24 requireComponent(key != ViewKey.Position, "tree node requires an explicit key")
25 }
26 public val expandable: Boolean
27 get() = when (children) {
28 case Leaf -> false
29 case Known(val nodes) -> nodes.size > 0L
30 case Deferred -> true
31 }
32 public val canCheck: Boolean get() = enabled && checkable && checkboxEnabled
33 public val canSelect: Boolean get() = enabled && selectable
34 public fun withChildren(value: TreeChildren<T>): TreeNode<T> = TreeNode(
35 key, this.value, label, value, enabled, selectable, checkable, checkboxEnabled, draggable
36 )
39internal class TreeRecord<T>(
40 val node: TreeNode<T>,
41 val parent: Long?,
42 val depth: Long,
43 val position: Long,
44 val siblingCount: Long,
45 val children: Array<Long>,
46 val end: Long
49/** Indexes a loaded snapshot once, without allocating views or recursive traversal frames. */
50public class TreeData<T> public constructor(public val roots: Array<TreeNode<T>>) {
51 private val keyed = HashMap<ViewKey, Long>()
52 private val indexed = index()
53 internal val records: Array<TreeRecord<T>> get() = indexed.records
54 internal val rootIndices: Array<Long> get() = indexed.roots
55 public val count: Long get() = records.size
56 public val loadedKeys: Array<ViewKey> get() = indexed.loaded
57 internal val deferredKeys: Array<ViewKey> get() = indexed.deferred
59 private fun index(): TreeIndex<T> {
60 val rows = ArrayList<TreeRecordBuilder<T>>()
61 val rootIds = ArrayList<Long>()
62 val loaded = ArrayList<ViewKey>()
63 val deferred = ArrayList<ViewKey>()
64 val stack = ArrayList<TreeDataCursor<T>>()
65 stack.add(TreeDataCursor(roots, None, 0L))
66 while (stack.size > 0L) {
67 val cursor = stack[stack.size - 1L]
68 if (cursor.next == cursor.nodes.size) {
69 when (cursor.parent) {
70 case Some(val parent) -> rows[parent].end = rows.size
71 case None -> {}
72 }
73 stack.removeAt(stack.size - 1L)
74 continue
75 }
76 val position = cursor.next
77 val node = cursor.nodes[position]
78 cursor.next += 1L
79 requireComponent(keyed[node.key] == None, "duplicate tree key or repeated subtree")
80 val index = rows.size
81 keyed[node.key] = index
82 rows
83 .add(
84 TreeRecordBuilder(
85 node,
86 cursor.parent,
87 cursor.depth,
88 position,
89 cursor.nodes.size
90 )
91 )
92 when (cursor.parent) {
93 case Some(val parent) -> rows[parent].children.add(index)
94 case None -> rootIds.add(index)
95 }
96 when (node.children) {
97 case Known(val children) -> {
98 loaded.add(node.key)
99 stack.add(TreeDataCursor(children, Some(index), cursor.depth + 1L))
100 }
102 case Deferred -> {
103 deferred.add(node.key)
104 rows[index].end = index + 1L
105 }
107 case Leaf -> rows[index].end = index + 1L
108 }
109 }
110 val records = Array<TreeRecord<T>>(rows.size) { index ->
111 val row = rows[index]
112 TreeRecord(
113 row.node,
114 row.parent,
115 row.depth,
116 row.position,
117 row.siblingCount,
118 row.children.toArray(),
119 row.end
120 )
121 }
122 return TreeIndex(records, rootIds.toArray(), loaded.toArray(), deferred.toArray())
123 }
125 public fun indexOfKey(key: ViewKey): Long? = keyed[key]
126 public fun node(key: ViewKey): TreeNode<T>? = when (keyed[key]) {
127 case Some(val index) -> Some(records[index].node)
128 case None -> None
129 }
130 /** Returns the known root-to-node path, including the requested key; never loads children. */
131 public fun pathTo(key: ViewKey): Array<ViewKey>? {
132 var at = keyed[key] ?: return None
133 val reversed = ArrayList<ViewKey>()
134 while (true) {
135 val row = records[at]
136 reversed.add(row.node.key)
137 when (row.parent) {
138 case Some(val parent) -> at = parent
139 case None -> break
140 }
141 }
142 return Some(Array<ViewKey>(reversed.size) { index -> reversed[reversed.size - index - 1L] })
143 }
144 /** Replaces one branch and its ancestor path while retaining all unaffected node references. */
145 public fun withChildren(key: ViewKey, children: TreeChildren<T>): TreeData<T> {
146 var at = keyed[key] ?: throw IllegalArgumentException(Some("unknown tree parent key"))
147 var replacement = records[at].node.withChildren(children)
148 while (true) {
149 val row = records[at]
150 when (row.parent) {
151 case Some(val parent) -> {
152 val siblings = records[parent].children
153 val chosen = at
154 val next = replacement
155 replacement = records[parent]
156 .node
157 .withChildren(
158 TreeChildren
159 .Known(
160 Array<TreeNode<T>>(siblings.size) { index ->
161 if (siblings[index] == chosen)
162 next
163 else
164 records[siblings[index]].node
165 }
166 )
167 )
168 at = parent
169 }
171 case None -> {
172 val root = replacement
173 return TreeData(
174 Array<TreeNode<T>>(roots.size) { index ->
175 if (index == row.position) root else roots[index]
176 }
177 )
178 }
179 }
180 }
181 }
184private class TreeIndex<T>(
185 val records: Array<TreeRecord<T>>,
186 val roots: Array<Long>,
187 val loaded: Array<ViewKey>,
188 val deferred: Array<ViewKey>
191private class TreeRecordBuilder<T>(
192 val node: TreeNode<T>,
193 val parent: Long?,
194 val depth: Long,
195 val position: Long,
196 val siblingCount: Long
197) {
198 val children = ArrayList<Long>()
199 var end = 0L
202private class TreeDataCursor<T>(val nodes: Array<TreeNode<T>>, val parent: Long?, val depth: Long) {
203 var next = 0L