Skip to content

Commit f7b646b

Browse files
committed
tmp save
1 parent d4da5f1 commit f7b646b

File tree

3 files changed

+133
-52
lines changed

3 files changed

+133
-52
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
2. 算法演示的demo程序放到src下. ( one demo per algorithm. )
1616
3. 程序正确通过后,请发起Pull Requests,代码被验证后入库,并在README中发布新算法实现。
1717
(Please Use Fork+Pull Requests !!! Correctness is the most important!)
18+
4. TAB = 4 space. set ts=4 in vim
1819

1920
####已实现 ( Implemented ):
2021

include/btree.h

Lines changed: 126 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#ifndef __BTREE_H__
2222
#define __BTREE_H__
2323

24+
#include <stdio.h>
25+
#include <iostream>
2426
#include <stdint.h>
2527
#include <stdlib.h>
2628
#include <string.h>
@@ -107,7 +109,6 @@ namespace alg {
107109
}
108110

109111
void DeleteKey(int32_t k) {
110-
node x = m_root;
111112
delete_op(m_root, k);
112113
}
113114

@@ -240,6 +241,7 @@ namespace alg {
240241
x->key[j] = x->key[j+1];
241242
}
242243
WRITE(x);
244+
printf("case1");
243245
return;
244246
} else {
245247
// case 2a:
@@ -249,13 +251,14 @@ namespace alg {
249251
// (We can find k0 and delete it in a single downward pass.)
250252
std::auto_ptr<node_t> y(READ(x, i));
251253
if (y->n >= T) {
254+
printf("case2a");
252255
int32_t k0 = y->key[y->n-1];
253256
x->key[i] = k0;
254257
WRITE(x);
255258
delete_op(y.get(), k0);
256259
return;
257260
}
258-
261+
259262
// case 2b.
260263
// If y has fewer than t keys, then, symmetrically, examine
261264
// the child z that follows k in node x. If z has at least t keys,
@@ -264,6 +267,7 @@ namespace alg {
264267
// and delete it in a single downward pass.)
265268
std::auto_ptr<node_t> z(READ(x, i+1));
266269
if (z->n >= T) {
270+
printf("case2b");
267271
int32_t k0 = z->key[0];
268272
x->key[i] = k0;
269273
WRITE(x);
@@ -277,6 +281,7 @@ namespace alg {
277281
// pointer to z, and y now contains 2t - 1 keys.
278282
// Then free z and recursively delete k from y.
279283
if (y->n == T-1 && z->n == T-1) {
284+
printf("case2c");
280285
// merge k & z into y
281286
y->key[y->n] = k;
282287

@@ -308,6 +313,7 @@ namespace alg {
308313
delete_op(y.get(), k);
309314
return;
310315
}
316+
printf("other in case2");
311317

312318
}
313319
} else {
@@ -321,15 +327,17 @@ namespace alg {
321327
// appropriate child pointer from the sibling into x.c[i].
322328
if (ci->n == T-1) {
323329
std::auto_ptr<node_t> left(READ(x, i-1));
324-
if (i-1>=0 && left->n > T) {
330+
printf("case 3a: %d %d\n", left->n, i-1);
331+
if (i-1>=0 && left->n >= T) {
332+
printf("case3a, left");
325333
// right shift keys and childs of x.c[i] to make place for a key
326334
// right shift ci childs
327335
int j;
328-
for (j=ci->n-1;j>0;j++) {
336+
for (j=ci->n-1;j>0;j--) {
329337
ci->key[j] = ci->key[j-1];
330338
}
331339

332-
for (j=ci->n;j>0;j++) {
340+
for (j=ci->n;j>0;j--) {
333341
ci->c[j] = ci->c[j-1];
334342
}
335343
ci->n = ci->n+1;
@@ -347,7 +355,9 @@ namespace alg {
347355

348356
// case 3a. right sibling
349357
std::auto_ptr<node_t> right(READ(x, i+1));
350-
if (i+1<ci->n && right->n > T) {
358+
printf("case 3a-r: %d %d\n", right->n, i+1);
359+
if (i+1<=x->n && right->n >= T) {
360+
printf("case3a, right");
351361
ci->key[ci->n] = x->key[i]; // append key from x
352362
ci->c[ci->n+1] = right->c[0]; // append child from right
353363
ci->n = ci->n+1;
@@ -374,63 +384,128 @@ namespace alg {
374384
// If x.c[i] and both of x.c[i]’s immediate siblings have t-1 keys, merge x.c[i]
375385
// with one sibling, which involves moving a key from x down into the new
376386
// merged node to become the median key for that node.
377-
if (left->n == T-1 && right->n == T-1) {
378-
// copy x[i] to left
379-
left->key[left->n] = x->key[i];
380-
left->n = left->n + 1;
381-
382-
// remove key[i] from x and also the child
383-
int j;
384-
for (j=i;j<x->n-1;j++) {
385-
x->key[j] = x->key[j+1];
386-
}
387-
388-
for (j=i;j<x->n;j++) {
389-
x->c[j] = x->c[j+1];
387+
if ((i-1<0 ||left->n == T-1) && (i+1 <=x->n || right->n == T-1)) {
388+
std::cerr << "case3b in";
389+
if (left->n == T-1) {
390+
std::cerr<<"case3b, left";
391+
// copy x[i] to left
392+
left->key[left->n] = x->key[i];
393+
left->n = left->n + 1;
394+
395+
// remove key[i] from x and also the child
396+
// shrink the size & set the child-0 to left
397+
int j;
398+
for (j=i;j<x->n-1;j++) {
399+
x->key[j] = x->key[j+1];
400+
}
401+
402+
for (j=i;j<x->n;j++) {
403+
x->c[j] = x->c[j+1];
404+
}
405+
x->n = x->n - 1;
406+
x->c[0] = left->offset;
407+
408+
// append x.c[i] into left sibling
409+
for (j=0;j<ci->n;j++) {
410+
left->key[left->n + j] = ci->key[j];
411+
}
412+
413+
for (j=0;j<ci->n+1;j++) {
414+
left->c[left->n + j] = ci->c[j];
415+
}
416+
left->n += ci->n; // left became 2T-1
417+
ci->flag |= MARKFREE; // free ci
418+
WRITE(ci.get());
419+
WRITE(x);
420+
421+
// root check
422+
if (x->n == 0 && x == m_root) {
423+
left->flag |= MARKFREE;
424+
WRITE(left.get());
425+
left->flag &= ~MARKFREE;
426+
left->offset = 0; // set to the first block
427+
WRITE(left.get());
428+
free(m_root);
429+
m_root = DUP(left.get());
430+
} else {
431+
WRITE(left.get());
432+
}
433+
434+
delete_op(left.get(), k);
435+
return;
436+
} else if (right->n == T-1) {
437+
std::cerr<<"case3b, right";
438+
// copy x[i] to x.c[i]
439+
ci->key[x->n] = x->key[i];
440+
ci->n = x->n + 1;
441+
442+
// remove key[i] from x and also the child
443+
// shrink the size & set the child-0 to ci
444+
int j;
445+
for (j=i;j<x->n-1;j++) {
446+
x->key[j] = x->key[j+1];
447+
}
448+
449+
for (j=i;j<x->n;j++) {
450+
x->c[j] = x->c[j+1];
451+
}
452+
x->n = x->n - 1;
453+
x->c[0] = ci->offset;
454+
455+
// append right sibling into x.c[i]
456+
for (j=0;j<ci->n;j++) {
457+
ci->key[ci->n + j] =right->key[j];
458+
}
459+
460+
for (j=0;j<ci->n+1;j++) {
461+
ci->c[ci->n + j] = right->c[j];
462+
}
463+
ci->n += right->n; // ci became 2T-1
464+
right->flag |= MARKFREE; // free right
465+
WRITE(right.get());
466+
WRITE(x);
467+
468+
// root check,
469+
// free the old block , and make root the first block of the file
470+
if (x->n == 0 && x == m_root) {
471+
ci->flag |= MARKFREE;
472+
WRITE(ci.get());
473+
ci->flag &= ~MARKFREE;
474+
ci->offset = 0;
475+
WRITE(ci.get());
476+
free(m_root);
477+
m_root = DUP(ci.get());
478+
} else {
479+
WRITE(ci.get());
480+
}
481+
482+
delete_op(ci.get(), k);
483+
return;
390484
}
391-
392-
// point child-0 to left
393-
x->c[0] = left->offset;
394-
395-
// append x.c[i] into left sibling
396-
for (j=0;j<ci->n;j++) {
397-
left->key[left->n + j] = ci->key[j];
398-
}
399-
400-
for (j=0;j<ci->n+1;j++) {
401-
left->c[j+left->n] = ci->c[j];
402-
}
403-
left->n += ci->n; // left became 2T-1
404-
ci->flag |= MARKFREE; // free ci
405-
WRITE(ci.get());
406-
WRITE(x);
407-
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;
415-
}
416-
417-
WRITE(left.get());
418-
delete_op(left.get(), k);
419485
}
420486
}
421-
422-
return;
423487
}
424488
}
425489

426490

491+
/**
492+
* duplicate the node
493+
*/
494+
node DUP(node x) {
495+
void *n = allocate_node();
496+
memcpy(n, x, BLOCKSIZE);
497+
return (node)n;
498+
}
499+
427500
/**
428501
* Read a 4K-block from disk, and returns the node struct.
429502
*/
430503
node READ(node x, int32_t i) {
431504
void *xi = allocate_node();
432-
lseek(fd, x->c[i], SEEK_SET);
433-
read(fd, xi, BLOCKSIZE);
505+
if (i >=0 || i <= x->n) {
506+
lseek(fd, x->c[i], SEEK_SET);
507+
read(fd, xi, BLOCKSIZE);
508+
}
434509
return (node)xi;
435510
}
436511

src/btree_demo.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ int main(void) {
99
for (i=0;i<100000;i++) {
1010
x.Insert(i);
1111
BTree::nr r = x.Search(i);
12-
printf("offset[%x] idx[%d]\n", r.offset, r.idx);
12+
}
13+
14+
for (i=0;i<100000;i++) {
15+
x.DeleteKey(i);
16+
BTree::nr r = x.Search(i);
17+
printf("key[%d] offset[%x] idx[%d]\n", i, r.offset, r.idx);
1318
}
1419
}

0 commit comments

Comments
 (0)