2 Copyright (c) 2010 Joseph A. Adams
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 Note: The following should work but are not well-tested yet:
46 * Maximum number of items per node.
47 * The maximum number of branches is BTREE_ITEM_MAX + 1.
49 #define BTREE_ITEM_MAX 20
52 struct btree_node *parent;
54 /* Number of items (rather than branches). */
57 /* 0 if node is a leaf, 1 if it has leaf children, etc. */
60 /* node->parent->branch[node->k] == this */
63 const void *item[BTREE_ITEM_MAX];
66 * Allocated to BTREE_ITEM_MAX+1 items if this is
67 * an internal node, 0 items if it is a leaf.
69 struct btree_node *branch[];
72 typedef struct btree_iterator_s {
74 struct btree_node *node;
78 * The relationship between item and (node, k) depends on what function
79 * set it. It is mainly for convenience.
85 * Instead of a compare function, this library accepts a binary search function
86 * to know how to order the items.
88 typedef unsigned int btree_search_proto(
90 const void * const *base,
95 typedef btree_search_proto *btree_search_t;
97 btree_search_proto btree_strcmp;
100 * Callback used by btree_delete() and btree_walk...().
102 * If it returns 0, it causes btree_walk...() to stop traversing and return 0.
103 * Thus, in normal circumstances, this callback should return 1.
105 * Callback shall not insert/remove items from the btree being traversed,
106 * nor shall anything modify it during a walk.
108 typedef int (*btree_action_t)(void *item, void *ctx);
111 struct btree_node *root;
112 size_t count; /* Total number of items in B-tree */
114 btree_search_t search;
118 * These are set to NULL by default.
120 * When destroy is not NULL, it is called on each item in order when
121 * btree_delete() is called.
123 * When destroy is NULL, btree_delete runs faster because it does not have
124 * to visit each and every item.
126 btree_action_t destroy;
130 struct btree *btree_new(btree_search_t search);
131 void btree_delete(struct btree *btree);
133 /* Inserts an item into the btree. If an item already exists that is equal
134 * to this one (as determined by the search function), behavior depends on the
135 * btree->multi setting.
136 * If btree->multi is false (default), returns false, and no item
137 * is inserted (because it would be a duplicate).
138 * If btree->multi is true, returns true, putting the item after
141 bool btree_insert(struct btree *btree, const void *item);
143 /* Removes an item from the btree. If an item exists that is equal to the
144 * key (as determined by the search function), it is removed.
146 * If btree->multi is set, all matching items are removed.
148 * Returns true if item was found and deleted, false if not found. */
149 bool btree_remove(struct btree *btree, const void *key);
151 /* Finds the requested item.
152 * Returns the item pointer on success, NULL on failure.
153 * Note that NULL is a valid item value. If you need to put
154 * NULLs in a btree, use btree_find instead. */
155 void *btree_lookup(struct btree *btree, const void *key);
158 /* lr must be 0 or 1, nothing else. */
159 int btree_begin_end_lr(const struct btree *btree, btree_iterator iter, int lr);
160 int btree_find_lr(const struct btree *btree, const void *key,
161 btree_iterator iter, int lr);
163 int btree_walk_backward(const struct btree *btree,
164 btree_action_t action, void *ctx);
165 int btree_walk_forward(const struct btree *btree,
166 btree_action_t action, void *ctx);
168 #define btree_begin(btree, iter) btree_begin_end_lr(btree, iter, 0)
169 #define btree_end(btree, iter) btree_begin_end_lr(btree, iter, 1)
171 int btree_prev(btree_iterator iter);
172 int btree_next(btree_iterator iter);
174 #define btree_walk(btree, action, ctx) btree_walk_forward(btree, action, ctx)
177 * If key was found, btree_find_first will return 1, iter->item will be the
178 * first matching item, and iter will point to the beginning of the matching
181 * If key was not found, btree_find_first will return 0, iter->item will be
182 * undefined, and iter will point to where the key should go if inserted.
184 #define btree_find_first(btree, key, iter) btree_find_lr(btree, key, iter, 0)
187 * If key was found, btree_find_last will return 1, iter->item will be the
188 * last matching item, and iter will point to the end of the matching
191 * If key was not found, btree_find_last will return 0, iter->item will be
192 * undefined, and iter will point to where the key should go if inserted.
194 #define btree_find_last(btree, key, iter) btree_find_lr(btree, key, iter, 1)
196 /* btree_find is an alias of btree_find_first. */
197 #define btree_find(btree, key, iter) btree_find_first(btree, key, iter)
200 * If iter points to an item, btree_deref returns 1 and sets iter->item to the
203 * Otherwise (if iter points to the end of the btree), btree_deref returns 0
204 * and leaves iter untouched.
206 int btree_deref(btree_iterator iter);
209 * Inserts the item before the one pointed to by iter.
211 * Insertion invalidates all iterators to the btree, including the one
212 * passed to btree_insert_at. Nevertheless, iter->item will be set to
215 void btree_insert_at(btree_iterator iter, const void *item);
218 * Removes the item pointed to by iter. Returns 1 if iter pointed
219 * to an item. Returns 0 if iter pointed to the end, in which case
220 * it leaves iter intact.
222 * Removal invalidates all iterators to the btree, including the one
223 * passed to btree_remove_at. Nevertheless, iter->item will be set to
226 int btree_remove_at(btree_iterator iter);
229 * Compares positions of two iterators.
231 * Returns -1 if a is before b, 0 if a is at the same position as b,
232 * and +1 if a is after b.
234 int btree_cmp_iters(const btree_iterator iter_a, const btree_iterator iter_b);
236 #define btree_search_implement(name, type, setup, equals, lessthan) \
237 unsigned int name(const void *__key, \
238 const void * const *__base, unsigned int __count, \
239 int __lr, int *__found) \
241 unsigned int __start = 0; \
243 unsigned int __middle = __count >> 1; \
244 type a = (type)__key; \
245 type b = (type)__base[__start + __middle]; \
254 __start += __middle + 1; \
255 __count -= __middle + 1; \
260 goto __greaterthan; \
261 /* else, fall through to __lessthan */ \
263 __count = __middle; \
269 #endif /* #ifndef CCAN_BTREE_H */