scoop:scoop.ui.component:0.1.0
src/tree/projection.scoop
Selected production source · src/tree/projection.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap4
import scoop.ui.ViewKey6
/** Metadata for one visible row; parent and subtreeEnd address this same projection. */7
public class TreeRow<T> internal constructor(8
public val node: TreeNode<T>,9
public val parent: Long?,10
public val depth: Long,11
public val position: Long,12
public val siblingCount: Long,13
public val expanded: Boolean,14
public val matched: Boolean,15
public val subtreeEnd: Long16
) {17
public val key: ViewKey get() = node.key18
public val lastSibling: Boolean get() = position + 1L == siblingCount19
}21
public class TreeProjection<T> internal constructor(22
public val data: TreeData<T>,23
public val rows: Array<TreeRow<T>>24
) {25
private val keyed = HashMap<ViewKey, Long>()26
public val count: Long get() = rows.size27
init {28
for (index in 0L..<rows.size)29
keyed[rows[index].key] = index30
}31
public fun indexOfKey(key: ViewKey): Long? = keyed[key]32
public fun keyAt(index: Long): ViewKey = rows[index].key33
public fun rowAt(index: Long): TreeRow<T> = rows[index]34
}36
internal class TreeProjectionCache<T>(37
val order: TreeOrder<T>,38
val matches: TreeMatches<T>,39
val expanded: TreeKeySnapshot40
) {41
val projection: TreeProjection<T> = treeProject(order, matches, expanded)42
}44
private class TreeProjectionCursor(val children: Array<Long>, val parent: Long?, val depth: Long) {45
var next = 0L46
}48
private class TreeRowBuilder<T>(49
val node: TreeNode<T>,50
val parent: Long?,51
val depth: Long,52
val position: Long,53
val siblingCount: Long,54
val expanded: Boolean,55
val matched: Boolean56
) {57
var end = 0L58
}60
private fun <T> treeIncludedChildren(61
order: TreeOrder<T>,62
matches: TreeMatches<T>,63
parent: Long?64
): Array<Long> {65
val children = order.children(parent)66
if (!matches.active || matches.query.mode != TreeQueryMode.Filter) return children67
val included = ArrayList<Long>()68
for (child in children)69
if (matches.includes(child)) included.add(child)70
return included.toArray()71
}73
private fun <T> treeProject(74
order: TreeOrder<T>,75
matches: TreeMatches<T>,76
expanded: TreeKeySnapshot77
): TreeProjection<T> {78
val built = ArrayList<TreeRowBuilder<T>>()79
val stack = ArrayList<TreeProjectionCursor>()80
stack.add(TreeProjectionCursor(treeIncludedChildren(order, matches, None), None, 0L))81
while (stack.size > 0L) {82
val cursor = stack[stack.size - 1L]83
if (cursor.next == cursor.children.size) {84
when (cursor.parent) {85
case Some(val parent) -> built[parent].end = built.size86
case None -> {}87
}88
stack.removeAt(stack.size - 1L)89
continue90
}91
val position = cursor.next92
val index = cursor.children[position]93
cursor.next += 1L94
val node = order.data.records[index].node95
val open = node.expandable &&96
(expanded.lookup.contains(node.key) || matches.expands(node.key))97
val at = built.size98
val row = TreeRowBuilder(99
node,100
cursor.parent,101
cursor.depth,102
position,103
cursor.children.size,104
open,105
matches.matched.contains(node.key)106
)107
built.add(row)108
if (open)109
stack110
.add(111
TreeProjectionCursor(112
treeIncludedChildren(order, matches, Some(index)),113
Some(at),114
cursor.depth + 1L115
)116
)117
else118
row.end = at + 1L119
}120
return TreeProjection(121
order.data,122
Array<TreeRow<T>>(built.size) { index ->123
val row = built[index]124
TreeRow(125
row.node,126
row.parent,127
row.depth,128
row.position,129
row.siblingCount,130
row.expanded,131
row.matched,132
row.end133
)134
}135
)136
}