1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
|
/*
* Copyright (C) 2010 Joseph Adams <joeyadams3.14159@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "btree.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX (BTREE_ITEM_MAX)
#define MIN (BTREE_ITEM_MAX >> 1)
static struct btree_node *node_alloc(int internal);
static void node_delete(struct btree_node *node, struct btree *btree);
static void branch_begin(btree_iterator iter);
static void branch_end(btree_iterator iter);
static void begin_end_lr(btree_iterator iter, struct btree_node *node, int lr);
/*
* If iter->node has parent, returns 1 and ascends the iterator such that
* iter->node->branch[iter->k] will be what iter->node was.
*
* If iter->node does not have a parent (is a root), returns 0 and leaves the
* iterator untouched.
*/
#define ascend(iter) ((iter)->node->parent \
? (iter)->k = (iter)->node->k, (iter)->node = (iter)->node->parent, 1 \
: 0)
static void node_insert(const void *x, struct btree_node *xr,
struct btree_node *p, unsigned int k);
static void node_split(const void **x, struct btree_node **xr,
struct btree_node *p, unsigned int k);
static void node_remove_leaf_item(struct btree_node *node, unsigned int k);
void node_restore(struct btree_node *node, unsigned int k);
static int node_walk_backward(const struct btree_node *node,
btree_action_t action, void *ctx);
static int node_walk_forward(const struct btree_node *node,
btree_action_t action, void *ctx);
/************************* Public functions *************************/
struct btree *btree_new(btree_search_t search)
{
struct btree *btree = calloc(1, sizeof(struct btree));
struct btree_node *node = node_alloc(0);
node->parent = NULL;
node->count = 0;
node->depth = 0;
btree->root = node;
btree->search = search;
btree->multi = false;
return btree;
}
void btree_delete(struct btree *btree)
{
node_delete(btree->root, btree);
free(btree);
}
bool btree_insert(struct btree *btree, const void *item)
{
btree_iterator iter;
if (btree_find_last(btree, item, iter) && !btree->multi)
return false;
btree_insert_at(iter, item);
return true;
}
bool btree_remove(struct btree *btree, const void *key)
{
btree_iterator iter;
bool success = false;
bool multi = btree->multi;
do {
if (btree_find_first(btree, key, iter)) {
btree_remove_at(iter);
success = true;
}
} while (multi);
return success;
}
void *btree_lookup(struct btree *btree, const void *key)
{
btree_iterator iter;
if (btree_find_first(btree, key, iter))
return iter->item;
return NULL;
}
int btree_begin_end_lr(const struct btree *btree, btree_iterator iter, int lr)
{
struct btree_node *node;
iter->btree = (struct btree *)btree;
begin_end_lr(iter, btree->root, lr);
/* Set iter->item if any items exist. */
node = iter->node;
if (node->count) {
iter->item = (void*)node->item[iter->k - lr];
return 1;
}
return 0;
}
int btree_deref(btree_iterator iter)
{
if (iter->k >= iter->node->count) {
struct btree_iterator_s tmp = *iter;
do {
if (!ascend(iter)) {
*iter = tmp;
return 0;
}
} while (iter->k >= iter->node->count);
}
iter->item = (void*)iter->node->item[iter->k];
return 1;
}
int btree_prev(btree_iterator iter)
{
if (iter->node->depth) {
branch_end(iter);
} else if (iter->k == 0) {
struct btree_iterator_s tmp = *iter;
do {
if (!ascend(iter)) {
*iter = tmp;
return 0;
}
} while (iter->k == 0);
}
iter->item = (void*)iter->node->item[--iter->k];
return 1;
}
int btree_next(btree_iterator iter)
{
int ret = btree_deref(iter);
if (ret) {
iter->k++;
if (iter->node->depth)
branch_begin(iter);
}
return ret;
}
int btree_find_lr(const struct btree *btree, const void *key,
btree_iterator iter, int lr)
{
struct btree_node *node = btree->root;
unsigned int k;
unsigned int depth;
int found = 0;
iter->btree = (struct btree *)btree;
iter->item = NULL;
depth = node->depth;
for (;;) {
int f = 0;
k = btree->search(key, node->item, node->count, lr, &f);
if (f) {
iter->item = (void*)node->item[k - lr];
found = 1;
}
if (!depth--)
break;
node = node->branch[k];
}
iter->node = node;
iter->k = k;
return found;
}
int btree_walk_backward(const struct btree *btree,
btree_action_t action, void *ctx)
{
return node_walk_backward(btree->root, action, ctx);
}
int btree_walk_forward(const struct btree *btree,
btree_action_t action, void *ctx)
{
return node_walk_forward(btree->root, action, ctx);
}
void btree_insert_at(btree_iterator iter, const void *item)
{
const void *x = item;
struct btree_node *xr = NULL;
struct btree_node *p;
struct btree *btree = iter->btree;
/* btree_insert_at always sets iter->item to item. */
iter->item = (void*)item;
/*
* If node is not a leaf, fall to the end of the left branch of item[k]
* so that it will be a leaf. This does not modify the iterator's logical
* position.
*/
if (iter->node->depth)
branch_end(iter);
/*
* First try inserting item into this node.
* If it's too big, split it, and repeat by
* trying to insert the median and right subtree into parent.
*/
if (iter->node->count < MAX) {
node_insert(x, xr, iter->node, iter->k);
goto finished;
} else {
for (;;) {
node_split(&x, &xr, iter->node, iter->k);
if (!ascend(iter))
break;
if (iter->node->count < MAX) {
node_insert(x, xr, iter->node, iter->k);
goto finished;
}
}
/*
* If splitting came all the way up to the root, create a new root whose
* left branch is the current root, median is x, and right branch is the
* half split off from the root.
*/
assert(iter->node == btree->root);
p = node_alloc(1);
p->parent = NULL;
p->count = 1;
p->depth = btree->root->depth + 1;
p->item[0] = x;
p->branch[0] = btree->root;
btree->root->parent = p;
btree->root->k = 0;
p->branch[1] = xr;
xr->parent = p;
xr->k = 1;
btree->root = p;
}
finished:
btree->count++;
iter->node = NULL;
}
int btree_remove_at(btree_iterator iter)
{
struct btree *btree = iter->btree;
struct btree_node *root;
if (!btree_deref(iter))
return 0;
if (!iter->node->depth) {
node_remove_leaf_item(iter->node, iter->k);
if (iter->node->count >= MIN || !iter->node->parent)
goto finished;
} else {
/*
* We can't remove an item from an internal node, so we'll replace it
* with its successor (which will always be in a leaf), then remove
* the original copy of the successor.
*/
/* Save pointer to condemned item. */
const void **x = &iter->node->item[iter->k];
/* Descend to successor. */
iter->k++;
branch_begin(iter);
/* Replace condemned item with successor. */
*x = iter->node->item[0];
/* Remove successor. */
node_remove_leaf_item(iter->node, 0);
}
/*
* Restore nodes that fall under their minimum count. This may
* propagate all the way up to the root.
*/
for (;;) {
if (iter->node->count >= MIN)
goto finished;
if (!ascend(iter))
break;
node_restore(iter->node, iter->k);
}
/*
* If combining came all the way up to the root, and it has no more
* dividers, delete it and make its only branch the root.
*/
root = iter->node;
assert(root == btree->root);
assert(root->depth > 0);
if (root->count == 0) {
btree->root = root->branch[0];
btree->root->parent = NULL;
free(root);
}
finished:
btree->count--;
iter->node = NULL;
return 1;
}
/*
* ascends iterator a until it matches iterator b's depth.
*
* Returns -1 if they end up on the same k (meaning a < b).
* Returns 0 otherwise.
*/
static int elevate(btree_iterator a, btree_iterator b)
{
while (a->node->depth < b->node->depth)
ascend(a);
if (a->k == b->k)
return -1;
return 0;
}
int btree_cmp_iters(const btree_iterator iter_a, const btree_iterator iter_b)
{
btree_iterator a = {*iter_a}, b = {*iter_b};
int ad, bd;
ad = btree_deref(a);
bd = btree_deref(b);
/* Check cases where one or both iterators are at the end. */
if (!ad)
return bd ? 1 : 0;
if (!bd)
return ad ? -1 : 0;
/* Bring iterators to the same depth. */
if (a->node->depth < b->node->depth) {
if (elevate(a, b))
return -1;
} else if (a->node->depth > b->node->depth) {
if (elevate(b, a))
return 1;
}
/* Bring iterators to the same node. */
while (a->node != b->node) {
ascend(a);
ascend(b);
}
/* Now we can compare by k directly. */
if (a->k < b->k)
return -1;
if (a->k > b->k)
return 1;
return 0;
}
/********************* Built-in ordering functions *******************/
btree_search_implement
(
btree_strcmp,
char*,
int c = strcmp(a, b),
c == 0,
c < 0
)
/************************* Private functions *************************/
static struct btree_node *node_alloc(int internal)
{
struct btree_node *node;
size_t isize = internal
? sizeof(struct btree_node*) * (BTREE_ITEM_MAX+1)
: 0;
node = malloc(sizeof(struct btree_node) + isize);
return node;
}
static void node_delete(struct btree_node *node, struct btree *btree)
{
unsigned int i, count = node->count;
if (!node->depth) {
if (btree->destroy) {
for (i=0; i<count; i++)
btree->destroy((void*)node->item[i], btree->destroy_ctx);
}
} else {
for (i=0; i<count; i++) {
node_delete(node->branch[i], btree);
if (btree->destroy)
btree->destroy((void*)node->item[i], btree->destroy_ctx);
}
node_delete(node->branch[count], btree);
}
free(node);
}
/* Set iter to beginning of branch pointed to by iter. */
static void branch_begin(btree_iterator iter)
{
struct btree_node *node = iter->node->branch[iter->k];
unsigned int depth = node->depth;
while (depth--)
node = node->branch[0];
iter->node = node;
iter->k = 0;
}
/* Set iter to end of branch pointed to by iter. */
static void branch_end(btree_iterator iter)
{
struct btree_node *node = iter->node->branch[iter->k];
unsigned int depth = node->depth;
while (depth--)
node = node->branch[node->count];
iter->node = node;
iter->k = node->count;
}
/* Traverse to the beginning or end of node, depending on lr. */
static void begin_end_lr(btree_iterator iter, struct btree_node *node, int lr)
{
iter->node = node;
iter->k = lr ? node->count : 0;
if (node->depth)
(lr ? branch_end : branch_begin)(iter);
}
/*
* Inserts item x and right branch xr into node p at position k.
*
* Assumes p exists and has enough room.
* Ignores xr if p is a leaf.
*/
static void node_insert(const void *x, struct btree_node *xr,
struct btree_node *p, unsigned int k)
{
unsigned int i;
for (i = p->count; i-- > k;)
p->item[i+1] = p->item[i];
p->item[k] = x;
if (p->depth) {
k++;
for (i = p->count+1; i-- > k;) {
p->branch[i+1] = p->branch[i];
p->branch[i+1]->k = i+1;
}
p->branch[k] = xr;
xr->parent = p;
xr->k = k;
}
p->count++;
}
/*
* Inserts item *x and subtree *xr into node p at position k, splitting it into
* nodes p and *xr with median item *x.
*
* Assumes p->count == MAX.
* Ignores original *xr if p is a leaf, but always sets it.
*/
static void node_split(const void **x, struct btree_node **xr,
struct btree_node *p, unsigned int k)
{
unsigned int i, split;
struct btree_node *l = p, *r;
/*
* If k <= MIN, item will be inserted into left subtree, so give l
* fewer items initially.
* Otherwise, item will be inserted into right subtree, so give r
* fewer items initially.
*/
if (k <= MIN)
split = MIN;
else
split = MIN + 1;
/*
* If l->depth is 0, allocate a leaf node.
* Otherwise, allocate an internal node.
*/
r = node_alloc(l->depth);
/* l and r will be siblings, so they will have the same parent and depth. */
r->parent = l->parent;
r->depth = l->depth;
/*
* Initialize items/branches of right side.
* Do not initialize r's leftmost branch yet because we don't know
* whether it will be l's current rightmost branch or if *xr will
* take its place.
*/
for (i = split; i < MAX; i++)
r->item[i-split] = l->item[i];
if (r->depth) {
for (i = split+1; i <= MAX; i++) {
r->branch[i-split] = l->branch[i];
r->branch[i-split]->parent = r;
r->branch[i-split]->k = i-split;
}
}
/* Update counts. */
l->count = split;
r->count = MAX - split;
/*
* The nodes are now split, but the key isn't inserted yet.
*
* Insert key into left or right half,
* depending on which side it fell on.
*/
if (k <= MIN)
node_insert(*x, *xr, l, k);
else
node_insert(*x, *xr, r, k - split);
/*
* Give l's rightmost branch to r because l's rightmost item
* is going up to become the median.
*/
if (r->depth) {
r->branch[0] = l->branch[l->count];
r->branch[0]->parent = r;
r->branch[0]->k = 0;
}
/*
* Take up l's rightmost item to make it the median.
* That item's right branch is now r.
*/
*x = l->item[--l->count];
*xr = r;
}
/*
* Removes item k from node p, shifting successor items back and
* decrementing the count.
*
* Assumes node p has the item k and is a leaf.
*/
static void node_remove_leaf_item(struct btree_node *node, unsigned int k)
{
unsigned int i;
for (i = k+1; i < node->count; i++)
node->item[i-1] = node->item[i];
node->count--;
}
static void move_left(struct btree_node *node, unsigned int k);
static void move_right(struct btree_node *node, unsigned int k);
static void combine(struct btree_node *node, unsigned int k);
/*
* Fixes node->branch[k]'s problem of having one less than MIN items.
* May or may not cause node to fall below MIN items, depending on whether
* two branches are combined or not.
*/
void node_restore(struct btree_node *node, unsigned int k)
{
if (k == 0) {
if (node->branch[1]->count > MIN)
move_left(node, 0);
else
combine(node, 0);
} else if (k == node->count) {
if (node->branch[k-1]->count > MIN)
move_right(node, k-1);
else
combine(node, k-1);
} else if (node->branch[k-1]->count > MIN) {
move_right(node, k-1);
} else if (node->branch[k+1]->count > MIN) {
move_left(node, k);
} else {
combine(node, k-1);
}
}
static void move_left(struct btree_node *node, unsigned int k)
{
struct btree_node *l = node->branch[k], *r = node->branch[k+1], *mv;
unsigned int i;
l->item[l->count] = node->item[k];
node->item[k] = r->item[0];
for (i = 1; i < r->count; i++)
r->item[i-1] = r->item[i];
if (r->depth) {
mv = r->branch[0];
l->branch[l->count+1] = mv;
mv->parent = l;
mv->k = l->count+1;
for (i = 1; i <= r->count; i++) {
r->branch[i-1] = r->branch[i];
r->branch[i-1]->k = i-1;
}
}
l->count++;
r->count--;
}
static void move_right(struct btree_node *node, unsigned int k)
{
struct btree_node *l = node->branch[k], *r = node->branch[k+1];
unsigned int i;
for (i = r->count; i--;)
r->item[i+1] = r->item[i];
r->item[0] = node->item[k];
node->item[k] = l->item[l->count-1];
if (r->depth) {
for (i = r->count+1; i--;) {
r->branch[i+1] = r->branch[i];
r->branch[i+1]->k = i+1;
}
r->branch[0] = l->branch[l->count];
r->branch[0]->parent = r;
r->branch[0]->k = 0;
}
l->count--;
r->count++;
}
/* Combine node->branch[k] and node->branch[k+1]. */
static void combine(struct btree_node *node, unsigned int k)
{
struct btree_node *l = node->branch[k], *r = node->branch[k+1], *mv;
const void **o = &l->item[l->count];
unsigned int i;
//append node->item[k] followed by right node's items to left node
*o++ = node->item[k];
for (i=0; i<r->count; i++)
*o++ = r->item[i];
//if applicable, append right node's branches to left node
if (r->depth) {
for (i=0; i<=r->count; i++) {
mv = r->branch[i];
l->branch[l->count + i + 1] = mv;
mv->parent = l;
mv->k = l->count + i + 1;
}
}
//remove k and its right branch from parent node
for (i = k+1; i < node->count; i++) {
node->item[i-1] = node->item[i];
node->branch[i] = node->branch[i+1];
node->branch[i]->k = i;
}
//don't forget to update the left and parent node's counts and to free the right node
l->count += r->count + 1;
node->count--;
free(r);
}
static int node_walk_backward(const struct btree_node *node,
btree_action_t action, void *ctx)
{
unsigned int i, count = node->count;
if (!node->depth) {
for (i=count; i--;)
if (!action((void*)node->item[i], ctx))
return 0;
} else {
if (!node_walk_backward(node->branch[count], action, ctx))
return 0;
for (i=count; i--;) {
if (!action((void*)node->item[i], ctx))
return 0;
if (!node_walk_backward(node->branch[i], action, ctx))
return 0;
}
}
return 1;
}
static int node_walk_forward(const struct btree_node *node,
btree_action_t action, void *ctx)
{
unsigned int i, count = node->count;
if (!node->depth) {
for (i=0; i<count; i++)
if (!action((void*)node->item[i], ctx))
return 0;
} else {
for (i=0; i<count; i++) {
if (!node_walk_forward(node->branch[i], action, ctx))
return 0;
if (!action((void*)node->item[i], ctx))
return 0;
}
if (!node_walk_forward(node->branch[count], action, ctx))
return 0;
}
return 1;
}
|