btree.c 18 KB

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