Skip to content

Commit d4da5f1

Browse files
committed
add comments
1 parent 502da5f commit d4da5f1

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

include/btree.h

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -371,24 +371,30 @@ namespace alg {
371371
}
372372

373373
// case 3b.
374-
if (left->n == T-1) {
374+
// If x.c[i] and both of x.c[i]’s immediate siblings have t-1 keys, merge x.c[i]
375+
// with one sibling, which involves moving a key from x down into the new
376+
// merged node to become the median key for that node.
377+
if (left->n == T-1 && right->n == T-1) {
375378
// copy x[i] to left
376379
left->key[left->n] = x->key[i];
377380
left->n = left->n + 1;
378-
379-
// shift x
381+
382+
// remove key[i] from x and also the child
380383
int j;
381384
for (j=i;j<x->n-1;j++) {
382385
x->key[j] = x->key[j+1];
383386
}
384387

385-
for (j=i+1;j<x->n;j++) { // always overwrite right child
388+
for (j=i;j<x->n;j++) {
386389
x->c[j] = x->c[j+1];
387390
}
388391

389-
// append ci into left sibling
392+
// point child-0 to left
393+
x->c[0] = left->offset;
394+
395+
// append x.c[i] into left sibling
390396
for (j=0;j<ci->n;j++) {
391-
left->key[j+left->n] = ci->key[j];
397+
left->key[left->n + j] = ci->key[j];
392398
}
393399

394400
for (j=0;j<ci->n+1;j++) {
@@ -398,36 +404,18 @@ namespace alg {
398404
ci->flag |= MARKFREE; // free ci
399405
WRITE(ci.get());
400406
WRITE(x);
401-
WRITE(left.get());
402-
delete_op(left.get(), k);
403-
} else {
404-
// copy x[i] to ci
405-
ci->key[ci->n] = x->key[i];
406-
ci->n = ci->n + 1;
407407

408-
// shift x
409-
int j;
410-
for (j=i;j<x->n-1;j++) {
411-
x->key[j] = x->key[j+1];
412-
}
413-
414-
for (j=i+1;j<x->n;j++) { // always overwrite right child
415-
x->c[j] = x->c[j+1];
416-
}
417-
418-
// append right sibling into ci
419-
for (j=0;j<right->n;j++) {
420-
ci->key[ci->n+j] = right->key[j];
408+
// root check
409+
if (x->n == 0) {
410+
m_root = left.get(); // free the old block , and
411+
left->flag |= MARKFREE; // make root the first block of the file
412+
WRITE(left.get());
413+
left->flag &= ~MARKFREE;
414+
left->offset = 0;
421415
}
422416

423-
for (j=0;j<left->n+1;j++) {
424-
ci->c[ci->n+j] = right->c[j];
425-
}
426-
right->flag |= MARKFREE;
427-
WRITE(ci.get());
428-
WRITE(x);
429-
WRITE(right.get());
430-
delete_op(ci.get(),k);
417+
WRITE(left.get());
418+
delete_op(left.get(), k);
431419
}
432420
}
433421

0 commit comments

Comments
 (0)