scoop:scoop.ui.component:0.1.0
src/virtual/extent.scoop
Selected production source · src/virtual/extent.scoop
1
package scoop.ui.component3
import scoop.collections.HashMap5
public enum ItemExtent {6
Fixed(val value: Double), Variable(val estimate: Double)7
}9
public enum VirtualListAnchor {10
None, KeepVisible, FollowEnd11
}13
public enum VirtualListEndPadding {14
Fixed(val length: Double), FillViewport15
}17
internal fun itemEstimate(mode: ItemExtent): Double {18
val value =19
when (mode) {20
case Fixed(val extent) -> extent21
case Variable(val estimate) -> estimate22
}23
requireComponent(value.isFinite() && value > 0.0, "invalid item extent or estimate")24
return value25
}27
/** Sparse Fenwick deltas from a constant estimate, with no allocation proportional to count. */28
public class ItemExtentIndex public constructor(29
public val count: Long,30
public val mode: ItemExtent31
) {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(_) -> true38
case Fixed(_) -> false39
}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] ?: estimate48
}49
public fun record(index: Long, value: Double) {50
checkIndex(index)51
nonnegative(value, "invalid measured item extent")52
if (!variable)53
return54
val previous = values[index] ?: estimate55
if (previous == value)56
return57
val change = value - previous58
requireComponent((total + change).isFinite(), "virtual content extent overflow")59
val updates = ArrayList<ExtentDelta>()60
var at = index + 1L61
while (at <= count) {62
val sum = (sums[at] ?: 0.0) + change63
requireComponent(sum.isFinite(), "virtual extent sum overflow")64
updates.add(ExtentDelta(at, sum))65
val step = at and -at66
if (step > count - at)67
break68
at += step69
}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() * estimate77
if (!variable)78
return result79
var at = end80
while (at > 0L) {81
result += sums[at] ?: 0.082
at -= at and -at83
}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 count93
if (!variable)94
return boundIndex((position / estimate).toLong())95
var low = 0L96
var high = count97
while (low < high) {98
val middle = low + (high - low) / 2L99
if (prefix(middle + 1L) <= position)100
low = middle + 1L101
else102
high = middle103
}104
return low105
}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 += 1L113
return end114
}115
var low = 0L116
var high = count117
while (low < high) {118
val middle = low + (high - low) / 2L119
if (prefix(middle) < position)120
low = middle + 1L121
else122
high = middle123
}124
return low125
}126
private fun boundIndex(index: Long): Long =127
if (index < count)128
index129
else130
count131
private fun checkIndex(index: Long) {132
requireComponent(index >= 0L && index < count, "virtual item index is out of range")133
}134
}136
private class ExtentDelta(val index: Long, val value: Double)