Scoop API

scoop:scoop.ui.component:0.1.0

src/tree/projection.scoop

Selected production source · src/tree/projection.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
4import scoop.ui.ViewKey
6/** Metadata for one visible row; parent and subtreeEnd address this same projection. */
7public 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: Long
16) {
17 public val key: ViewKey get() = node.key
18 public val lastSibling: Boolean get() = position + 1L == siblingCount
21public 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.size
27 init {
28 for (index in 0L..<rows.size)
29 keyed[rows[index].key] = index
30 }
31 public fun indexOfKey(key: ViewKey): Long? = keyed[key]
32 public fun keyAt(index: Long): ViewKey = rows[index].key
33 public fun rowAt(index: Long): TreeRow<T> = rows[index]
36internal class TreeProjectionCache<T>(
37 val order: TreeOrder<T>,
38 val matches: TreeMatches<T>,
39 val expanded: TreeKeySnapshot
40) {
41 val projection: TreeProjection<T> = treeProject(order, matches, expanded)
44private class TreeProjectionCursor(val children: Array<Long>, val parent: Long?, val depth: Long) {
45 var next = 0L
48private 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: Boolean
56) {
57 var end = 0L
60private 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 children
67 val included = ArrayList<Long>()
68 for (child in children)
69 if (matches.includes(child)) included.add(child)
70 return included.toArray()
73private fun <T> treeProject(
74 order: TreeOrder<T>,
75 matches: TreeMatches<T>,
76 expanded: TreeKeySnapshot
77): 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.size
86 case None -> {}
87 }
88 stack.removeAt(stack.size - 1L)
89 continue
90 }
91 val position = cursor.next
92 val index = cursor.children[position]
93 cursor.next += 1L
94 val node = order.data.records[index].node
95 val open = node.expandable &&
96 (expanded.lookup.contains(node.key) || matches.expands(node.key))
97 val at = built.size
98 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 stack
110 .add(
111 TreeProjectionCursor(
112 treeIncludedChildren(order, matches, Some(index)),
113 Some(at),
114 cursor.depth + 1L
115 )
116 )
117 else
118 row.end = at + 1L
119 }
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.end
133 )
134 }
135 )