btree: optimize deletion
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Antoine Bartuccio 2019-07-30 17:54:01 +02:00
parent 33657122f9
commit 8dcadb219d
Signed by: klmp200
GPG Key ID: E7245548C53F904B
1 changed files with 17 additions and 19 deletions

View File

@ -215,17 +215,17 @@ func (n *Node) splitChild(i int, y *Node) {
z := newNode(y.t, y.isLeaf)
z.numberOfKeys = n.t - 1
// TODO: make optimisations
// Copy the last (t-1) keys of y to z
for j := 0; j < n.t-1; j++ {
z.keys[j] = y.keys[j+n.t]
if !y.isLeaf {
z.children[j] = y.children[j+n.t]
}
}
// Copy the last t children of y to z
if !y.isLeaf {
for j := 0; j < n.t; j++ {
z.children[j] = y.children[j+n.t]
}
z.children[n.t-1] = y.children[2*n.t-1]
}
// Reduce the number of keys in y
@ -442,17 +442,17 @@ func (n *Node) borrowFromNext(index int) {
// The first key from sibling is inserted into keys[index]
n.keys[index] = sibling.keys[0]
// TODO: optimize loops here
// Moving all keys in sibling one step behind
for i := 1; i < sibling.numberOfKeys; i++ {
sibling.keys[i-1] = sibling.keys[i]
if !sibling.isLeaf {
sibling.children[i-1] = sibling.children[i]
}
}
// Moving the child pointers one step behind
if !sibling.isLeaf {
for i := 1; i <= sibling.numberOfKeys; i++ {
sibling.children[i-1] = sibling.children[i]
}
sibling.children[n.numberOfKeys-1] = sibling.children[n.numberOfKeys]
}
child.numberOfKeys++
@ -471,25 +471,23 @@ func (n *Node) merge(index int) {
// Copies the keys from children[index+1] to children[index] at the end
for i := 0; i < sibling.numberOfKeys; i++ {
child.keys[i+n.t] = sibling.keys[i]
}
// Copies the child pointers from C[index+1] to children[index]
if !child.isLeaf {
for i := 0; i <= sibling.numberOfKeys; i++ {
if !child.isLeaf {
child.children[i+n.t] = sibling.children[i]
}
}
// Moves all keys after index in the current node one step before
// to fill the gap created by moving keys[index] to children[index]
for i := index + 1; i < n.numberOfKeys; i++ {
n.keys[i-1] = n.keys[i]
// Copies the child pointers from C[index+1] to children[index]
if !child.isLeaf {
child.children[sibling.numberOfKeys+n.t] = sibling.children[sibling.numberOfKeys]
}
// Moves all keys after index in the current node one step before
// to fill the gap created by moving keys[index] to children[index]
// Moves the child pointer after (index+1) in the current node one step before
// This action marks sibling for deletion by the GC
for i := index + 2; i <= n.numberOfKeys; i++ {
n.children[i-1] = n.children[i]
for i := index + 1; i < n.numberOfKeys; i++ {
n.keys[i-1] = n.keys[i]
n.children[i] = n.children[i+1]
}
// Updates the key count of child and the current node