scoop:scoop.std:0.1.0
src/collections/btree_node.scoop
Selected production source · src/collections/btree_node.scoop
1
package scoop.collections3
// Degree eight: 7..15 entries, with one temporary overflow slot.4
internal class BTreeNode<K, V> internal constructor(internal val leaf: Boolean)5
: Equals<BTreeNode<K, V>> {6
private val entries: MutableArray<MaybeUninit<Map.Entry<K, V>>> =7
MutableArray<MaybeUninit<Map.Entry<K, V>>>(16L) { _ -> MaybeUninit<Map.Entry<K, V>>.uninit() }8
private val children: MutableArray<BTreeNode<K, V>?>? =9
if (leaf) None else Some(MutableArray<BTreeNode<K, V>?>(17L) { _ -> None })10
internal var count: Long = 0L11
internal var parent: BTreeNode<K, V>? = None12
internal var parentIndex: Long = 0L14
public override operator fun equals(other: BTreeNode<K, V>): Boolean = this === other16
internal fun entry(index: Long): Map.Entry<K, V> {17
@Unsafe { return entries[index].assumeInit() }18
}20
internal fun write(index: Long, entry: Map.Entry<K, V>) {21
entries[index] = MaybeUninit<Map.Entry<K, V>>.initialized(entry)22
}24
internal fun erase(index: Long) { entries[index] = MaybeUninit<Map.Entry<K, V>>.uninit() }25
internal fun child(index: Long): BTreeNode<K, V>? = if (leaf) None else children!![index]26
internal fun clearChild(index: Long) { if (!leaf) children!![index] = None }28
internal fun setChild(index: Long, child: BTreeNode<K, V>?) {29
children!![index] = child30
if (child != None) {31
child!!.parent = Some(this)32
child!!.parentIndex = index33
}34
}36
internal fun insert(index: Long, entry: Map.Entry<K, V>, right: BTreeNode<K, V>?) {37
var position = count38
while (position > index) {39
write(position, this.entry(position - 1L))40
position -= 1L41
}42
if (!leaf) {43
position = count + 1L44
while (position > index + 1L) {45
setChild(position, child(position - 1L))46
position -= 1L47
}48
setChild(index + 1L, right)49
}50
write(index, entry)51
count += 1L52
}54
// Internal-node removal also removes the child right of the separator.55
internal fun removeSlot(index: Long) {56
var position = index57
while (position + 1L < count) {58
write(position, entry(position + 1L))59
position += 1L60
}61
erase(count - 1L)62
if (!leaf) {63
position = index + 1L64
while (position < count) {65
setChild(position, child(position + 1L))66
position += 1L67
}68
clearChild(count)69
}70
count -= 1L71
}73
internal fun split(): (Map.Entry<K, V>, BTreeNode<K, V>) {74
val promoted = entry(8L)75
val right = BTreeNode<K, V>(leaf)76
var index = 0L77
while (index < 7L) {78
right.write(index, entry(index + 9L))79
erase(index + 9L)80
index += 1L81
}82
if (!leaf) {83
index = 0L84
while (index < 8L) {85
right.setChild(index, child(index + 9L))86
clearChild(index + 9L)87
index += 1L88
}89
}90
erase(8L)91
count = 8L92
right.count = 7L93
return (promoted, right)94
}96
internal fun retire() {97
var index = 0L98
while (index < count) { erase(index); index += 1L }99
if (!leaf) {100
index = 0L101
while (index <= count) { clearChild(index); index += 1L }102
}103
count = 0L104
parent = None105
parentIndex = 0L106
}107
}