btree.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #ifndef CCAN_BTREE_H
  26. #define CCAN_BTREE_H
  27. /*
  28. Note: The following should work but are not well-tested yet:
  29. btree_walk...
  30. btree_cmp_iters
  31. btree_insert
  32. btree_remove
  33. btree_lookup
  34. */
  35. #include <stdbool.h>
  36. #include <stdint.h>
  37. #include <string.h>
  38. /*
  39. * Maximum number of items per node.
  40. * The maximum number of branches is BTREE_ITEM_MAX + 1.
  41. */
  42. #define BTREE_ITEM_MAX 20
  43. struct btree_node {
  44. struct btree_node *parent;
  45. /* Number of items (rather than branches). */
  46. unsigned char count;
  47. /* 0 if node is a leaf, 1 if it has leaf children, etc. */
  48. unsigned char depth;
  49. /* node->parent->branch[node->k] == this */
  50. unsigned char k;
  51. const void *item[BTREE_ITEM_MAX];
  52. /*
  53. * Allocated to BTREE_ITEM_MAX+1 items if this is
  54. * an internal node, 0 items if it is a leaf.
  55. */
  56. struct btree_node *branch[];
  57. };
  58. typedef struct btree_iterator_s {
  59. struct btree *btree;
  60. struct btree_node *node;
  61. unsigned int k;
  62. /*
  63. * The relationship between item and (node, k) depends on what function
  64. * set it. It is mainly for convenience.
  65. */
  66. void *item;
  67. } btree_iterator[1];
  68. /*
  69. * Instead of a compare function, this library accepts a binary search function
  70. * to know how to order the items.
  71. */
  72. typedef unsigned int btree_search_proto(
  73. const void *key,
  74. const void * const *base,
  75. unsigned int count,
  76. int lr,
  77. int *found
  78. );
  79. typedef btree_search_proto *btree_search_t;
  80. btree_search_proto btree_strcmp;
  81. /*
  82. * Callback used by btree_delete() and btree_walk...().
  83. *
  84. * If it returns 0, it causes btree_walk...() to stop traversing and return 0.
  85. * Thus, in normal circumstances, this callback should return 1.
  86. *
  87. * Callback shall not insert/remove items from the btree being traversed,
  88. * nor shall anything modify it during a walk.
  89. */
  90. typedef int (*btree_action_t)(void *item, void *ctx);
  91. struct btree {
  92. struct btree_node *root;
  93. size_t count; /* Total number of items in B-tree */
  94. btree_search_t search;
  95. bool multi;
  96. /*
  97. * These are set to NULL by default.
  98. *
  99. * When destroy is not NULL, it is called on each item in order when
  100. * btree_delete() is called.
  101. *
  102. * When destroy is NULL, btree_delete runs faster because it does not have
  103. * to visit each and every item.
  104. */
  105. btree_action_t destroy;
  106. void *destroy_ctx;
  107. };
  108. struct btree *btree_new(btree_search_t search);
  109. void btree_delete(struct btree *btree);
  110. /* Inserts an item into the btree. If an item already exists that is equal
  111. * to this one (as determined by the search function), behavior depends on the
  112. * btree->multi setting.
  113. * If btree->multi is false (default), returns false, and no item
  114. * is inserted (because it would be a duplicate).
  115. * If btree->multi is true, returns true, putting the item after
  116. * its duplicates.
  117. */
  118. bool btree_insert(struct btree *btree, const void *item);
  119. /* Removes an item from the btree. If an item exists that is equal to the
  120. * key (as determined by the search function), it is removed.
  121. *
  122. * If btree->multi is set, all matching items are removed.
  123. *
  124. * Returns true if item was found and deleted, false if not found. */
  125. bool btree_remove(struct btree *btree, const void *key);
  126. /* Finds the requested item.
  127. * Returns the item pointer on success, NULL on failure.
  128. * Note that NULL is a valid item value. If you need to put
  129. * NULLs in a btree, use btree_find instead. */
  130. void *btree_lookup(struct btree *btree, const void *key);
  131. /* lr must be 0 or 1, nothing else. */
  132. int btree_begin_end_lr(const struct btree *btree, btree_iterator iter, int lr);
  133. int btree_find_lr(const struct btree *btree, const void *key,
  134. btree_iterator iter, int lr);
  135. int btree_walk_backward(const struct btree *btree,
  136. btree_action_t action, void *ctx);
  137. int btree_walk_forward(const struct btree *btree,
  138. btree_action_t action, void *ctx);
  139. #define btree_begin(btree, iter) btree_begin_end_lr(btree, iter, 0)
  140. #define btree_end(btree, iter) btree_begin_end_lr(btree, iter, 1)
  141. int btree_prev(btree_iterator iter);
  142. int btree_next(btree_iterator iter);
  143. #define btree_walk(btree, action, ctx) btree_walk_forward(btree, action, ctx)
  144. /*
  145. * If key was found, btree_find_first will return 1, iter->item will be the
  146. * first matching item, and iter will point to the beginning of the matching
  147. * items.
  148. *
  149. * If key was not found, btree_find_first will return 0, iter->item will be
  150. * undefined, and iter will point to where the key should go if inserted.
  151. */
  152. #define btree_find_first(btree, key, iter) btree_find_lr(btree, key, iter, 0)
  153. /*
  154. * If key was found, btree_find_last will return 1, iter->item will be the
  155. * last matching item, and iter will point to the end of the matching
  156. * items.
  157. *
  158. * If key was not found, btree_find_last will return 0, iter->item will be
  159. * undefined, and iter will point to where the key should go if inserted.
  160. */
  161. #define btree_find_last(btree, key, iter) btree_find_lr(btree, key, iter, 1)
  162. /* btree_find is an alias of btree_find_first. */
  163. #define btree_find(btree, key, iter) btree_find_first(btree, key, iter)
  164. /*
  165. * If iter points to an item, btree_deref returns 1 and sets iter->item to the
  166. * item it points to.
  167. *
  168. * Otherwise (if iter points to the end of the btree), btree_deref returns 0
  169. * and leaves iter untouched.
  170. */
  171. int btree_deref(btree_iterator iter);
  172. /*
  173. * Inserts the item before the one pointed to by iter.
  174. *
  175. * Insertion invalidates all iterators to the btree, including the one
  176. * passed to btree_insert_at. Nevertheless, iter->item will be set to
  177. * the item inserted.
  178. */
  179. void btree_insert_at(btree_iterator iter, const void *item);
  180. /*
  181. * Removes the item pointed to by iter. Returns 1 if iter pointed
  182. * to an item. Returns 0 if iter pointed to the end, in which case
  183. * it leaves iter intact.
  184. *
  185. * Removal invalidates all iterators to the btree, including the one
  186. * passed to btree_remove_at. Nevertheless, iter->item will be set to
  187. * the item removed.
  188. */
  189. int btree_remove_at(btree_iterator iter);
  190. /*
  191. * Compares positions of two iterators.
  192. *
  193. * Returns -1 if a is before b, 0 if a is at the same position as b,
  194. * and +1 if a is after b.
  195. */
  196. int btree_cmp_iters(const btree_iterator iter_a, const btree_iterator iter_b);
  197. #define btree_search_implement(name, type, setup, equals, lessthan) \
  198. unsigned int name(const void *__key, \
  199. const void * const *__base, unsigned int __count, \
  200. int __lr, int *__found) \
  201. { \
  202. unsigned int __start = 0; \
  203. while (__count) { \
  204. unsigned int __middle = __count >> 1; \
  205. type a = (type)__key; \
  206. type b = (type)__base[__start + __middle]; \
  207. { \
  208. setup; \
  209. if (equals) \
  210. goto __equals; \
  211. if (lessthan) \
  212. goto __lessthan; \
  213. } \
  214. __greaterthan: \
  215. __start += __middle + 1; \
  216. __count -= __middle + 1; \
  217. continue; \
  218. __equals: \
  219. *__found = 1; \
  220. if (__lr) \
  221. goto __greaterthan; \
  222. /* else, fall through to __lessthan */ \
  223. __lessthan: \
  224. __count = __middle; \
  225. continue; \
  226. } \
  227. return __start; \
  228. }
  229. #endif /* #ifndef CCAN_BTREE_H */