2 * tree234.h: header defining functions in tree234.c.
\r
4 * This file is copyright 1999-2001 Simon Tatham.
\r
6 * Permission is hereby granted, free of charge, to any person
\r
7 * obtaining a copy of this software and associated documentation
\r
8 * files (the "Software"), to deal in the Software without
\r
9 * restriction, including without limitation the rights to use,
\r
10 * copy, modify, merge, publish, distribute, sublicense, and/or
\r
11 * sell copies of the Software, and to permit persons to whom the
\r
12 * Software is furnished to do so, subject to the following
\r
15 * The above copyright notice and this permission notice shall be
\r
16 * included in all copies or substantial portions of the Software.
\r
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
\r
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
21 * NONINFRINGEMENT. IN NO EVENT SHALL SIMON TATHAM BE LIABLE FOR
\r
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
\r
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
\r
32 * This typedef is opaque outside tree234.c itself.
\r
34 typedef struct tree234_Tag tree234;
\r
36 typedef int (*cmpfn234) (void *, void *);
\r
39 * Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and
\r
40 * lookups by key will fail: you can only look things up by numeric
\r
41 * index, and you have to use addpos234() and delpos234().
\r
43 tree234 *newtree234(cmpfn234 cmp);
\r
46 * Free a 2-3-4 tree (not including freeing the elements).
\r
48 void freetree234(tree234 * t);
\r
51 * Add an element e to a sorted 2-3-4 tree t. Returns e on success,
\r
52 * or if an existing element compares equal, returns that.
\r
54 void *add234(tree234 * t, void *e);
\r
57 * Add an element e to an unsorted 2-3-4 tree t. Returns e on
\r
58 * success, NULL on failure. (Failure should only occur if the
\r
59 * index is out of range or the tree is sorted.)
\r
61 * Index range can be from 0 to the tree's current element count,
\r
64 void *addpos234(tree234 * t, void *e, int index);
\r
67 * Look up the element at a given numeric index in a 2-3-4 tree.
\r
68 * Returns NULL if the index is out of range.
\r
70 * One obvious use for this function is in iterating over the whole
\r
71 * of a tree (sorted or unsorted):
\r
73 * for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p);
\r
77 * int maxcount = count234(tree);
\r
78 * for (i = 0; i < maxcount; i++) {
\r
79 * p = index234(tree, i);
\r
80 * assert(p != NULL);
\r
84 void *index234(tree234 * t, int index);
\r
87 * Find an element e in a sorted 2-3-4 tree t. Returns NULL if not
\r
88 * found. e is always passed as the first argument to cmp, so cmp
\r
89 * can be an asymmetric function if desired. cmp can also be passed
\r
90 * as NULL, in which case the compare function from the tree proper
\r
93 * Three of these functions are special cases of findrelpos234. The
\r
94 * non-`pos' variants lack the `index' parameter: if the parameter
\r
95 * is present and non-NULL, it must point to an integer variable
\r
96 * which will be filled with the numeric index of the returned
\r
99 * The non-`rel' variants lack the `relation' parameter. This
\r
100 * parameter allows you to specify what relation the element you
\r
101 * provide has to the element you're looking for. This parameter
\r
104 * REL234_EQ - find only an element that compares equal to e
\r
105 * REL234_LT - find the greatest element that compares < e
\r
106 * REL234_LE - find the greatest element that compares <= e
\r
107 * REL234_GT - find the smallest element that compares > e
\r
108 * REL234_GE - find the smallest element that compares >= e
\r
110 * Non-`rel' variants assume REL234_EQ.
\r
112 * If `rel' is REL234_GT or REL234_LT, the `e' parameter may be
\r
113 * NULL. In this case, REL234_GT will return the smallest element
\r
114 * in the tree, and REL234_LT will return the greatest. This gives
\r
115 * an alternative means of iterating over a sorted tree, instead of
\r
118 * // to loop forwards
\r
119 * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;)
\r
122 * // to loop backwards
\r
123 * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;)
\r
127 REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE
\r
129 void *find234(tree234 * t, void *e, cmpfn234 cmp);
\r
130 void *findrel234(tree234 * t, void *e, cmpfn234 cmp, int relation);
\r
131 void *findpos234(tree234 * t, void *e, cmpfn234 cmp, int *index);
\r
132 void *findrelpos234(tree234 * t, void *e, cmpfn234 cmp, int relation,
\r
136 * Delete an element e in a 2-3-4 tree. Does not free the element,
\r
137 * merely removes all links to it from the tree nodes.
\r
139 * delpos234 deletes the element at a particular tree index: it
\r
140 * works on both sorted and unsorted trees.
\r
142 * del234 deletes the element passed to it, so it only works on
\r
143 * sorted trees. (It's equivalent to using findpos234 to determine
\r
144 * the index of an element, and then passing that index to
\r
147 * Both functions return a pointer to the element they delete, for
\r
148 * the user to free or pass on elsewhere or whatever. If the index
\r
149 * is out of range (delpos234) or the element is already not in the
\r
150 * tree (del234) then they return NULL.
\r
152 void *del234(tree234 * t, void *e);
\r
153 void *delpos234(tree234 * t, int index);
\r
156 * Return the total element count of a tree234.
\r
158 int count234(tree234 * t);
\r
160 #endif /* TREE234_H */
\r