Scoop API

scoop:scoop.std:0.1.0

src/collections/btree_node.scoop

Selected production source · src/collections/btree_node.scoop

1package scoop.collections
3// Degree eight: 7..15 entries, with one temporary overflow slot.
4internal 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 = 0L
11 internal var parent: BTreeNode<K, V>? = None
12 internal var parentIndex: Long = 0L
14 public override operator fun equals(other: BTreeNode<K, V>): Boolean = this === other
16 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] = child
30 if (child != None) {
31 child!!.parent = Some(this)
32 child!!.parentIndex = index
33 }
34 }
36 internal fun insert(index: Long, entry: Map.Entry<K, V>, right: BTreeNode<K, V>?) {
37 var position = count
38 while (position > index) {
39 write(position, this.entry(position - 1L))
40 position -= 1L
41 }
42 if (!leaf) {
43 position = count + 1L
44 while (position > index + 1L) {
45 setChild(position, child(position - 1L))
46 position -= 1L
47 }
48 setChild(index + 1L, right)
49 }
50 write(index, entry)
51 count += 1L
52 }
54 // Internal-node removal also removes the child right of the separator.
55 internal fun removeSlot(index: Long) {
56 var position = index
57 while (position + 1L < count) {
58 write(position, entry(position + 1L))
59 position += 1L
60 }
61 erase(count - 1L)
62 if (!leaf) {
63 position = index + 1L
64 while (position < count) {
65 setChild(position, child(position + 1L))
66 position += 1L
67 }
68 clearChild(count)
69 }
70 count -= 1L
71 }
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 = 0L
77 while (index < 7L) {
78 right.write(index, entry(index + 9L))
79 erase(index + 9L)
80 index += 1L
81 }
82 if (!leaf) {
83 index = 0L
84 while (index < 8L) {
85 right.setChild(index, child(index + 9L))
86 clearChild(index + 9L)
87 index += 1L
88 }
89 }
90 erase(8L)
91 count = 8L
92 right.count = 7L
93 return (promoted, right)
94 }
96 internal fun retire() {
97 var index = 0L
98 while (index < count) { erase(index); index += 1L }
99 if (!leaf) {
100 index = 0L
101 while (index <= count) { clearChild(index); index += 1L }
102 }
103 count = 0L
104 parent = None
105 parentIndex = 0L
106 }