scoop:scoop.ui.component:0.1.0
src/tree/data.scoop
Selected production source · src/tree/data.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.ViewKey6
/** Known([]) is an empty loaded branch; Deferred is the only unknown child state. */7
public enum TreeChildren<T> {8
Leaf, Known(Array<TreeNode<T>>), Deferred9
}11
/** Immutable node metadata. The label names the node independently of custom cell views. */12
public 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 = true22
) {23
init {24
requireComponent(key != ViewKey.Position, "tree node requires an explicit key")25
}26
public val expandable: Boolean27
get() = when (children) {28
case Leaf -> false29
case Known(val nodes) -> nodes.size > 0L30
case Deferred -> true31
}32
public val canCheck: Boolean get() = enabled && checkable && checkboxEnabled33
public val canSelect: Boolean get() = enabled && selectable34
public fun withChildren(value: TreeChildren<T>): TreeNode<T> = TreeNode(35
key, this.value, label, value, enabled, selectable, checkable, checkboxEnabled, draggable36
)37
}39
internal 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: Long47
)49
/** Indexes a loaded snapshot once, without allocating views or recursive traversal frames. */50
public 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.records54
internal val rootIndices: Array<Long> get() = indexed.roots55
public val count: Long get() = records.size56
public val loadedKeys: Array<ViewKey> get() = indexed.loaded57
internal val deferredKeys: Array<ViewKey> get() = indexed.deferred59
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.size71
case None -> {}72
}73
stack.removeAt(stack.size - 1L)74
continue75
}76
val position = cursor.next77
val node = cursor.nodes[position]78
cursor.next += 1L79
requireComponent(keyed[node.key] == None, "duplicate tree key or repeated subtree")80
val index = rows.size81
keyed[node.key] = index82
rows83
.add(84
TreeRecordBuilder(85
node,86
cursor.parent,87
cursor.depth,88
position,89
cursor.nodes.size90
)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 + 1L105
}107
case Leaf -> rows[index].end = index + 1L108
}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.end120
)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 -> None129
}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 None133
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 = parent139
case None -> break140
}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].children153
val chosen = at154
val next = replacement155
replacement = records[parent]156
.node157
.withChildren(158
TreeChildren159
.Known(160
Array<TreeNode<T>>(siblings.size) { index ->161
if (siblings[index] == chosen)162
next163
else164
records[siblings[index]].node165
}166
)167
)168
at = parent169
}171
case None -> {172
val root = replacement173
return TreeData(174
Array<TreeNode<T>>(roots.size) { index ->175
if (index == row.position) root else roots[index]176
}177
)178
}179
}180
}181
}182
}184
private class TreeIndex<T>(185
val records: Array<TreeRecord<T>>,186
val roots: Array<Long>,187
val loaded: Array<ViewKey>,188
val deferred: Array<ViewKey>189
)191
private class TreeRecordBuilder<T>(192
val node: TreeNode<T>,193
val parent: Long?,194
val depth: Long,195
val position: Long,196
val siblingCount: Long197
) {198
val children = ArrayList<Long>()199
var end = 0L200
}202
private class TreeDataCursor<T>(val nodes: Array<TreeNode<T>>, val parent: Long?, val depth: Long) {203
var next = 0L204
}