Scoop API

scoop:scoop.ui.component:0.1.0

src/virtual/extent.scoop

Selected production source · src/virtual/extent.scoop

1package scoop.ui.component
3import scoop.collections.HashMap
5public enum ItemExtent {
6 Fixed(val value: Double), Variable(val estimate: Double)
7}
9public enum VirtualListAnchor {
10 None, KeepVisible, FollowEnd
13public enum VirtualListEndPadding {
14 Fixed(val length: Double), FillViewport
17internal fun itemEstimate(mode: ItemExtent): Double {
18 val value =
19 when (mode) {
20 case Fixed(val extent) -> extent
21 case Variable(val estimate) -> estimate
22 }
23 requireComponent(value.isFinite() && value > 0.0, "invalid item extent or estimate")
24 return value
27/** Sparse Fenwick deltas from a constant estimate, with no allocation proportional to count. */
28public class ItemExtentIndex public constructor(
29 public val count: Long,
30 public val mode: ItemExtent
31) {
32 private val estimate = itemEstimate(mode)
33 private val values = HashMap<Long, Double>()
34 private val sums = HashMap<Long, Double>()
35 public val variable: Boolean =
36 when (mode) {
37 case Variable(_) -> true
38 case Fixed(_) -> false
39 }
40 init {
41 requireComponent(count >= 0L, "negative item count")
42 nonnegative(count.toDouble() * estimate, "virtual content extent overflow")
43 }
44 public val total: Double get() = prefix(count)
45 public fun extent(index: Long): Double {
46 checkIndex(index)
47 return values[index] ?: estimate
48 }
49 public fun record(index: Long, value: Double) {
50 checkIndex(index)
51 nonnegative(value, "invalid measured item extent")
52 if (!variable)
53 return
54 val previous = values[index] ?: estimate
55 if (previous == value)
56 return
57 val change = value - previous
58 requireComponent((total + change).isFinite(), "virtual content extent overflow")
59 val updates = ArrayList<ExtentDelta>()
60 var at = index + 1L
61 while (at <= count) {
62 val sum = (sums[at] ?: 0.0) + change
63 requireComponent(sum.isFinite(), "virtual extent sum overflow")
64 updates.add(ExtentDelta(at, sum))
65 val step = at and -at
66 if (step > count - at)
67 break
68 at += step
69 }
70 values.put(index, value)
71 for (update in updates)
72 sums.put(update.index, update.value)
73 }
74 public fun prefix(end: Long): Double {
75 requireComponent(end >= 0L && end <= count, "virtual prefix index is out of range")
76 var result = end.toDouble() * estimate
77 if (!variable)
78 return result
79 var at = end
80 while (at > 0L) {
81 result += sums[at] ?: 0.0
82 at -= at and -at
83 }
84 requireComponent(result.isFinite(), "virtual content extent overflow")
85 // Cancellation of estimated and measured extents may round slightly below zero.
86 return lowerBound(result, 0.0)
87 }
88 /** First item whose end is strictly after position; known zero-size items are skipped. */
89 public fun indexAt(position: Double): Long {
90 nonnegative(position, "invalid virtual position")
91 if (position >= total)
92 return count
93 if (!variable)
94 return boundIndex((position / estimate).toLong())
95 var low = 0L
96 var high = count
97 while (low < high) {
98 val middle = low + (high - low) / 2L
99 if (prefix(middle + 1L) <= position)
100 low = middle + 1L
101 else
102 high = middle
103 }
104 return low
105 }
106 /** Exclusive end of the items starting before position. */
107 public fun endBefore(position: Double): Long {
108 nonnegative(position, "invalid virtual position")
109 if (!variable) {
110 var end = boundIndex((position / estimate).toLong())
111 if (end < count && end.toDouble() * estimate < position)
112 end += 1L
113 return end
114 }
115 var low = 0L
116 var high = count
117 while (low < high) {
118 val middle = low + (high - low) / 2L
119 if (prefix(middle) < position)
120 low = middle + 1L
121 else
122 high = middle
123 }
124 return low
125 }
126 private fun boundIndex(index: Long): Long =
127 if (index < count)
128 index
129 else
130 count
131 private fun checkIndex(index: Long) {
132 requireComponent(index >= 0L && index < count, "virtual item index is out of range")
133 }
136private class ExtentDelta(val index: Long, val value: Double)