2 * tree234.h: header defining functions in tree234.c.
4 * This file is copyright 1999-2001 Simon Tatham.
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * This typedef is opaque outside tree234.c itself.
36 typedef struct tree234_Tag tree234;
38 typedef int (*cmpfn234)(void *, void *);
40 typedef void *(*copyfn234)(void *state, void *element);
43 * Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and
44 * lookups by key will fail: you can only look things up by numeric
45 * index, and you have to use addpos234() and delpos234().
47 tree234 *newtree234(cmpfn234 cmp);
50 * Free a 2-3-4 tree (not including freeing the elements).
52 void freetree234(tree234 *t);
55 * Add an element e to a sorted 2-3-4 tree t. Returns e on success,
56 * or if an existing element compares equal, returns that.
58 void *add234(tree234 *t, void *e);
61 * Add an element e to an unsorted 2-3-4 tree t. Returns e on
62 * success, NULL on failure. (Failure should only occur if the
63 * index is out of range or the tree is sorted.)
65 * Index range can be from 0 to the tree's current element count,
68 void *addpos234(tree234 *t, void *e, int index);
71 * Look up the element at a given numeric index in a 2-3-4 tree.
72 * Returns NULL if the index is out of range.
74 * One obvious use for this function is in iterating over the whole
75 * of a tree (sorted or unsorted):
77 * for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p);
81 * int maxcount = count234(tree);
82 * for (i = 0; i < maxcount; i++) {
83 * p = index234(tree, i);
88 void *index234(tree234 *t, int index);
91 * Find an element e in a sorted 2-3-4 tree t. Returns NULL if not
92 * found. e is always passed as the first argument to cmp, so cmp
93 * can be an asymmetric function if desired. cmp can also be passed
94 * as NULL, in which case the compare function from the tree proper
97 * Three of these functions are special cases of findrelpos234. The
98 * non-`pos' variants lack the `index' parameter: if the parameter
99 * is present and non-NULL, it must point to an integer variable
100 * which will be filled with the numeric index of the returned
103 * The non-`rel' variants lack the `relation' parameter. This
104 * parameter allows you to specify what relation the element you
105 * provide has to the element you're looking for. This parameter
108 * REL234_EQ - find only an element that compares equal to e
109 * REL234_LT - find the greatest element that compares < e
110 * REL234_LE - find the greatest element that compares <= e
111 * REL234_GT - find the smallest element that compares > e
112 * REL234_GE - find the smallest element that compares >= e
114 * Non-`rel' variants assume REL234_EQ.
116 * If `rel' is REL234_GT or REL234_LT, the `e' parameter may be
117 * NULL. In this case, REL234_GT will return the smallest element
118 * in the tree, and REL234_LT will return the greatest. This gives
119 * an alternative means of iterating over a sorted tree, instead of
122 * // to loop forwards
123 * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;)
126 * // to loop backwards
127 * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;)
131 REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE
133 void *find234(tree234 *t, void *e, cmpfn234 cmp);
134 void *findrel234(tree234 *t, void *e, cmpfn234 cmp, int relation);
135 void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index);
136 void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp, int relation,
140 * Delete an element e in a 2-3-4 tree. Does not free the element,
141 * merely removes all links to it from the tree nodes.
143 * delpos234 deletes the element at a particular tree index: it
144 * works on both sorted and unsorted trees.
146 * del234 deletes the element passed to it, so it only works on
147 * sorted trees. (It's equivalent to using findpos234 to determine
148 * the index of an element, and then passing that index to
151 * Both functions return a pointer to the element they delete, for
152 * the user to free or pass on elsewhere or whatever. If the index
153 * is out of range (delpos234) or the element is already not in the
154 * tree (del234) then they return NULL.
156 void *del234(tree234 *t, void *e);
157 void *delpos234(tree234 *t, int index);
160 * Return the total element count of a tree234.
162 int count234(tree234 *t);
165 * Split a tree234 into two valid tree234s.
167 * splitpos234 splits at a given index. If `before' is true, the
168 * items at and after that index are left in t and the ones before
169 * are returned; if `before' is false, the items before that index
170 * are left in t and the rest are returned.
172 * split234 splits at a given key. You can pass any of the
173 * relations used with findrel234, except for REL234_EQ. The items
174 * in the tree that satisfy the relation are returned; the
175 * remainder are left.
177 tree234 *splitpos234(tree234 *t, int index, bool before);
178 tree234 *split234(tree234 *t, void *e, cmpfn234 cmp, int rel);
181 * Join two tree234s together into a single one.
183 * All the elements in t1 are placed to the left of all the
184 * elements in t2. If the trees are sorted, there will be a test to
185 * ensure that this satisfies the ordering criterion, and NULL will
186 * be returned otherwise. If the trees are unsorted, there is no
187 * restriction on the use of join234.
189 * The tree returned is t1 (join234) or t2 (join234r), if the
190 * operation is successful.
192 tree234 *join234(tree234 *t1, tree234 *t2);
193 tree234 *join234r(tree234 *t1, tree234 *t2);
196 * Make a complete copy of a tree234. Element pointers will be
197 * reused unless copyfn is non-NULL, in which case it will be used
198 * to copy each element. (copyfn takes two `void *' parameters; the
199 * first is private state and the second is the element. A simple
200 * copy routine probably won't need private state.)
202 tree234 *copytree234(tree234 *t, copyfn234 copyfn, void *copyfnstate);
204 #endif /* TREE234_H */