Scoop API

scoop:scoop.std:0.1.0

src/collections/btree_balance.scoop

Selected production source · src/collections/btree_balance.scoop

1package scoop.collections
3internal fun <K, V> borrowBTreeLeft(node: BTreeNode<K, V>, parent: BTreeNode<K, V>, index: Long) {
4 val left = parent.child(index - 1L)!!
5 var position = node.count
6 while (position > 0L) {
7 node.write(position, node.entry(position - 1L))
8 position -= 1L
9 }
10 if (!node.leaf) {
11 position = node.count + 1L
12 while (position > 0L) {
13 node.setChild(position, node.child(position - 1L))
14 position -= 1L
15 }
16 node.setChild(0L, left.child(left.count))
17 left.clearChild(left.count)
18 }
19 node.write(0L, parent.entry(index - 1L))
20 parent.write(index - 1L, left.entry(left.count - 1L))
21 left.erase(left.count - 1L)
22 left.count -= 1L
23 node.count += 1L
26internal fun <K, V> borrowBTreeRight(node: BTreeNode<K, V>, parent: BTreeNode<K, V>, index: Long) {
27 val right = parent.child(index + 1L)!!
28 node.write(node.count, parent.entry(index))
29 if (!node.leaf) node.setChild(node.count + 1L, right.child(0L))
30 parent.write(index, right.entry(0L))
31 var position = 0L
32 while (position + 1L < right.count) {
33 right.write(position, right.entry(position + 1L))
34 position += 1L
35 }
36 right.erase(right.count - 1L)
37 if (!right.leaf) {
38 position = 0L
39 while (position < right.count) {
40 right.setChild(position, right.child(position + 1L))
41 position += 1L
42 }
43 right.clearChild(right.count)
44 }
45 right.count -= 1L
46 node.count += 1L
49internal fun <K, V> mergeBTreeChildren(parent: BTreeNode<K, V>, index: Long) {
50 val left = parent.child(index)!!
51 val right = parent.child(index + 1L)!!
52 val offset = left.count + 1L
53 left.write(left.count, parent.entry(index))
54 var position = 0L
55 while (position < right.count) {
56 left.write(offset + position, right.entry(position))
57 position += 1L
58 }
59 if (!left.leaf) {
60 position = 0L
61 while (position <= right.count) {
62 left.setChild(offset + position, right.child(position))
63 position += 1L
64 }
65 }
66 left.count += right.count + 1L
67 right.retire()
68 parent.removeSlot(index)