]> git.ozlabs.org Git - ccan/blob - ccan/btree/btree.h
btree: spelling fix
[ccan] / ccan / btree / btree.h
1 /*
2         Copyright (c) 2010  Joseph A. Adams
3         All rights reserved.
4         
5         Redistribution and use in source and binary forms, with or without
6         modification, are permitted provided that the following conditions
7         are met:
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.
15         
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.
26 */
27
28 #ifndef CCAN_BTREE_H
29 #define CCAN_BTREE_H
30
31 /*
32 Note:  The following should work but are not well-tested yet:
33
34 btree_walk...
35 btree_cmp_iters
36 btree_insert
37 btree_remove
38 btree_lookup
39 */
40
41 #include <stdbool.h>
42 #include <stdint.h>
43 #include <string.h>
44
45 /*
46  * Maximum number of items per node.
47  * The maximum number of branches is BTREE_ITEM_MAX + 1.
48  */
49 #define BTREE_ITEM_MAX 20
50
51 struct btree_node {
52         struct btree_node *parent;
53         
54         /* Number of items (rather than branches). */
55         unsigned char count;
56         
57         /* 0 if node is a leaf, 1 if it has leaf children, etc. */
58         unsigned char depth;
59         
60         /* node->parent->branch[node->k] == this */
61         unsigned char k;
62         
63         const void *item[BTREE_ITEM_MAX];
64         
65         /*
66          * Allocated to BTREE_ITEM_MAX+1 items if this is
67          * an internal node, 0 items if it is a leaf.
68          */
69         struct btree_node *branch[];
70 };
71
72 typedef struct btree_iterator_s {
73         struct btree *btree;
74         struct btree_node *node;
75         unsigned int k;
76         
77         /*
78          * The relationship between item and (node, k) depends on what function
79          * set it.  It is mainly for convenience.
80          */
81         void *item;
82 } btree_iterator[1];
83
84 /*
85  * Instead of a compare function, this library accepts a binary search function
86  * to know how to order the items.
87  */
88 typedef unsigned int btree_search_proto(
89         const void *key,
90         const void * const *base,
91         unsigned int count,
92         int lr,
93         int *found
94 );
95 typedef btree_search_proto *btree_search_t;
96
97 btree_search_proto btree_strcmp;
98
99 /*
100  * Callback used by btree_delete() and btree_walk...().
101  *
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.
104  *
105  * Callback shall not insert/remove items from the btree being traversed,
106  * nor shall anything modify it during a walk.
107  */
108 typedef int (*btree_action_t)(void *item, void *ctx);
109
110 struct btree {
111         struct btree_node *root;
112         size_t count; /* Total number of items in B-tree */
113         
114         btree_search_t search;
115         bool multi;
116         
117         /*
118          * These are set to NULL by default.
119          *
120          * When destroy is not NULL, it is called on each item in order when
121          * btree_delete() is called.
122          *
123          * When destroy is NULL, btree_delete runs faster because it does not have
124          * to visit each and every item.
125          */
126         btree_action_t destroy;
127         void *destroy_ctx;
128 };
129
130 struct btree *btree_new(btree_search_t search);
131 void btree_delete(struct btree *btree);
132
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
139  *      its duplicates.
140  */
141 bool btree_insert(struct btree *btree, const void *item);
142
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.
145  *
146  * If btree->multi is set, all matching items are removed.
147  *
148  * Returns true if item was found and deleted, false if not found. */
149 bool btree_remove(struct btree *btree, const void *key);
150
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);
156
157
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);
162
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);
167
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)
170
171 int btree_prev(btree_iterator iter);
172 int btree_next(btree_iterator iter);
173
174 #define btree_walk(btree, action, ctx) btree_walk_forward(btree, action, ctx)
175
176 /*
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
179  * items.
180  *
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.
183  */
184 #define btree_find_first(btree, key, iter) btree_find_lr(btree, key, iter, 0)
185
186 /*
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
189  * items.
190  *
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.
193  */
194 #define btree_find_last(btree, key, iter) btree_find_lr(btree, key, iter, 1)
195
196 /* btree_find is an alias of btree_find_first. */
197 #define btree_find(btree, key, iter) btree_find_first(btree, key, iter)
198
199 /*
200  * If iter points to an item, btree_deref returns 1 and sets iter->item to the
201  * item it points to.
202  *
203  * Otherwise (if iter points to the end of the btree), btree_deref returns 0
204  * and leaves iter untouched.
205  */
206 int btree_deref(btree_iterator iter);
207
208 /*
209  * Inserts the item before the one pointed to by iter.
210  *
211  * Insertion invalidates all iterators to the btree, including the one
212  * passed to btree_insert_at.  Nevertheless, iter->item will be set to
213  * the item inserted.
214  */
215 void btree_insert_at(btree_iterator iter, const void *item);
216
217 /*
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.
221  *
222  * Removal invalidates all iterators to the btree, including the one
223  * passed to btree_remove_at.  Nevertheless, iter->item will be set to
224  * the item removed.
225  */
226 int btree_remove_at(btree_iterator iter);
227
228 /*
229  * Compares positions of two iterators.
230  *
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.
233  */
234 int btree_cmp_iters(const btree_iterator iter_a, const btree_iterator iter_b);
235
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) \
240 { \
241         unsigned int __start = 0; \
242         while (__count) { \
243                 unsigned int __middle = __count >> 1; \
244                 type a = (type)__key; \
245                 type b = (type)__base[__start + __middle]; \
246                 { \
247                         setup; \
248                         if (equals) \
249                                 goto __equals; \
250                         if (lessthan) \
251                                 goto __lessthan; \
252                 } \
253         __greaterthan: \
254                 __start += __middle + 1; \
255                 __count -= __middle + 1; \
256                 continue; \
257         __equals: \
258                 *__found = 1; \
259                 if (__lr) \
260                         goto __greaterthan; \
261                 /* else, fall through to __lessthan */ \
262         __lessthan: \
263                 __count = __middle; \
264                 continue; \
265         } \
266         return __start; \
267 }
268
269 #endif /* #ifndef CCAN_BTREE_H */