scoop:scoop.std:0.1.0
src/collections/btree_balance.scoop
Selected production source · src/collections/btree_balance.scoop
1
package scoop.collections3
internal 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.count6
while (position > 0L) {7
node.write(position, node.entry(position - 1L))8
position -= 1L9
}10
if (!node.leaf) {11
position = node.count + 1L12
while (position > 0L) {13
node.setChild(position, node.child(position - 1L))14
position -= 1L15
}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 -= 1L23
node.count += 1L24
}26
internal 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 = 0L32
while (position + 1L < right.count) {33
right.write(position, right.entry(position + 1L))34
position += 1L35
}36
right.erase(right.count - 1L)37
if (!right.leaf) {38
position = 0L39
while (position < right.count) {40
right.setChild(position, right.child(position + 1L))41
position += 1L42
}43
right.clearChild(right.count)44
}45
right.count -= 1L46
node.count += 1L47
}49
internal 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 + 1L53
left.write(left.count, parent.entry(index))54
var position = 0L55
while (position < right.count) {56
left.write(offset + position, right.entry(position))57
position += 1L58
}59
if (!left.leaf) {60
position = 0L61
while (position <= right.count) {62
left.setChild(offset + position, right.child(position))63
position += 1L64
}65
}66
left.count += right.count + 1L67
right.retire()68
parent.removeSlot(index)69
}