btree.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2010 Joseph Adams <joeyadams3.14159@gmail.com>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef CCAN_BTREE_H
  23. #define CCAN_BTREE_H
  24. /*
  25. Note: The following should work but are not well-tested yet:
  26. btree_walk...
  27. btree_cmp_iters
  28. btree_insert
  29. btree_remove
  30. btree_lookup
  31. */
  32. #include <stdbool.h>
  33. #include <stdint.h>
  34. #include <string.h>
  35. /*
  36. * Maximum number of items per node.
  37. * The maximum number of branches is BTREE_ITEM_MAX + 1.
  38. */
  39. #define BTREE_ITEM_MAX 20
  40. struct btree_node {
  41. struct btree_node *parent;
  42. /* Number of items (rather than branches). */
  43. unsigned char count;
  44. /* 0 if node is a leaf, 1 if it has leaf children, etc. */
  45. unsigned char depth;
  46. /* node->parent->branch[node->k] == this */
  47. unsigned char k;
  48. const void *item[BTREE_ITEM_MAX];
  49. /*
  50. * Allocated to BTREE_ITEM_MAX+1 items if this is
  51. * an internal node, 0 items if it is a leaf.
  52. */
  53. struct btree_node *branch[];
  54. };
  55. typedef struct btree_iterator_s {
  56. struct btree *btree;
  57. struct btree_node *node;
  58. unsigned int k;
  59. /*
  60. * The relationship between item and (node, k) depends on what function
  61. * set it. It is mainly for convenience.
  62. */
  63. void *item;
  64. } btree_iterator[1];
  65. /*
  66. * Instead of a compare function, this library accepts a binary search function
  67. * to know how to order the items.
  68. */
  69. typedef unsigned int btree_search_proto(
  70. const void *key,
  71. const void * const *base,
  72. unsigned int count,
  73. int lr,
  74. int *found
  75. );
  76. typedef btree_search_proto *btree_search_t;
  77. btree_search_proto btree_strcmp;
  78. /*
  79. * Callback used by btree_delete() and btree_walk...().
  80. *
  81. * If it returns 0, it causes btree_walk...() to stop traversing and return 0.
  82. * Thus, in normal circumstances, this callback should return 1.
  83. *
  84. * Callback shall not insert/remove items from the btree being traversed,
  85. * nor shall anything modify it during a walk.
  86. */
  87. typedef int (*btree_action_t)(void *item, void *ctx);
  88. struct btree {
  89. struct btree_node *root;
  90. size_t count; /* Total number of items in B-tree */
  91. btree_search_t search;
  92. bool multi;
  93. /*
  94. * These are set to NULL by default.
  95. *
  96. * When destroy is not NULL, it is called on each item in order when
  97. * btree_delete() is called.
  98. *
  99. * When destroy is NULL, btree_delete runs faster because it does not have
  100. * to visit each and every item.
  101. */
  102. btree_action_t destroy;
  103. void *destroy_ctx;
  104. };
  105. struct btree *btree_new(btree_search_t search);
  106. void btree_delete(struct btree *btree);
  107. /* Inserts an item into the btree. If an item already exists that is equal
  108. * to this one (as determined by the search function), behavior depends on the
  109. * btree->multi setting.
  110. * If btree->multi is false (default), returns false, and no item
  111. * is inserted (because it would be a duplicate).
  112. * If btree->multi is true, returns true, putting the item after
  113. * its duplicates.
  114. */
  115. bool btree_insert(struct btree *btree, const void *item);
  116. /* Removes an item from the btree. If an item exists that is equal to the
  117. * key (as determined by the search function), it is removed.
  118. *
  119. * If btree->multi is set, all matching items are removed.
  120. *
  121. * Returns true if item was found and deleted, false if not found. */
  122. bool btree_remove(struct btree *btree, const void *key);
  123. /* Finds the requested item.
  124. * Returns the item pointer on success, NULL on failure.
  125. * Note that NULL is a valid item value. If you need to put
  126. * NULLs in a btree, use btree_find instead. */
  127. void *btree_lookup(struct btree *btree, const void *key);
  128. /* lr must be 0 or 1, nothing else. */
  129. int btree_begin_end_lr(const struct btree *btree, btree_iterator iter, int lr);
  130. int btree_find_lr(const struct btree *btree, const void *key,
  131. btree_iterator iter, int lr);
  132. int btree_walk_backward(const struct btree *btree,
  133. btree_action_t action, void *ctx);
  134. int btree_walk_forward(const struct btree *btree,
  135. btree_action_t action, void *ctx);
  136. #define btree_begin(btree, iter) btree_begin_end_lr(btree, iter, 0)
  137. #define btree_end(btree, iter) btree_begin_end_lr(btree, iter, 1)
  138. int btree_prev(btree_iterator iter);
  139. int btree_next(btree_iterator iter);
  140. #define btree_walk(btree, action, ctx) btree_walk_forward(btree, action, ctx)
  141. /*
  142. * If key was found, btree_find_first will return 1, iter->item will be the
  143. * first matching item, and iter will point to the beginning of the matching
  144. * items.
  145. *
  146. * If key was not found, btree_find_first will return 0, iter->item will be
  147. * undefined, and iter will point to where the key should go if inserted.
  148. */
  149. #define btree_find_first(btree, key, iter) btree_find_lr(btree, key, iter, 0)
  150. /*
  151. * If key was found, btree_find_last will return 1, iter->item will be the
  152. * last matching item, and iter will point to the end of the matching
  153. * items.
  154. *
  155. * If key was not found, btree_find_last will return 0, iter->item will be
  156. * undefined, and iter will point to where the key should go if inserted.
  157. */
  158. #define btree_find_last(btree, key, iter) btree_find_lr(btree, key, iter, 1)
  159. /* btree_find is an alias of btree_find_first. */
  160. #define btree_find(btree, key, iter) btree_find_first(btree, key, iter)
  161. /*
  162. * If iter points to an item, btree_deref returns 1 and sets iter->item to the
  163. * item it points to.
  164. *
  165. * Otherwise (if iter points to the end of the btree), btree_deref returns 0
  166. * and leaves iter untouched.
  167. */
  168. int btree_deref(btree_iterator iter);
  169. /*
  170. * Inserts the item before the one pointed to by iter.
  171. *
  172. * Insertion invalidates all iterators to the btree, including the one
  173. * passed to btree_insert_at. Nevertheless, iter->item will be set to
  174. * the item inserted.
  175. */
  176. void btree_insert_at(btree_iterator iter, const void *item);
  177. /*
  178. * Removes the item pointed to by iter. Returns 1 if iter pointed
  179. * to an item. Returns 0 if iter pointed to the end, in which case
  180. * it leaves iter intact.
  181. *
  182. * Removal invalidates all iterators to the btree, including the one
  183. * passed to btree_remove_at. Nevertheless, iter->item will be set to
  184. * the item removed.
  185. */
  186. int btree_remove_at(btree_iterator iter);
  187. /*
  188. * Compares positions of two iterators.
  189. *
  190. * Returns -1 if a is before b, 0 if a is at the same position as b,
  191. * and +1 if a is after b.
  192. */
  193. int btree_cmp_iters(const btree_iterator iter_a, const btree_iterator iter_b);
  194. #define btree_search_implement(name, type, setup, equals, lessthan) \
  195. unsigned int name(const void *__key, \
  196. const void * const *__base, unsigned int __count, \
  197. int __lr, int *__found) \
  198. { \
  199. unsigned int __start = 0; \
  200. while (__count) { \
  201. unsigned int __middle = __count >> 1; \
  202. type a = (type)__key; \
  203. type b = (type)__base[__start + __middle]; \
  204. { \
  205. setup; \
  206. if (equals) \
  207. goto __equals; \
  208. if (lessthan) \
  209. goto __lessthan; \
  210. } \
  211. __greaterthan: \
  212. __start += __middle + 1; \
  213. __count -= __middle + 1; \
  214. continue; \
  215. __equals: \
  216. *__found = 1; \
  217. if (__lr) \
  218. goto __greaterthan; \
  219. /* else, fall through to __lessthan */ \
  220. __lessthan: \
  221. __count = __middle; \
  222. continue; \
  223. } \
  224. return __start; \
  225. }
  226. #endif /* #ifndef CCAN_BTREE_H */