| 1 |
/* |
| 2 |
* tree234.h: header defining functions in tree234.c. |
| 3 |
* |
| 4 |
* This file is copyright 1999-2001 Simon Tatham. |
| 5 |
* |
| 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 |
| 13 |
* conditions: |
| 14 |
* |
| 15 |
* The above copyright notice and this permission notice shall be |
| 16 |
* included in all copies or substantial portions of the Software. |
| 17 |
* |
| 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 |
| 25 |
* SOFTWARE. |
| 26 |
*/ |
| 27 |
|
| 28 |
#ifndef TREE234_H |
| 29 |
#define TREE234_H |
| 30 |
|
| 31 |
/* |
| 32 |
* This typedef is opaque outside tree234.c itself. |
| 33 |
*/ |
| 34 |
typedef struct tree234_Tag tree234; |
| 35 |
|
| 36 |
typedef int (*cmpfn234) (void *, void *); |
| 37 |
|
| 38 |
typedef void *(*copyfn234) (void *state, void *element); |
| 39 |
|
| 40 |
/* |
| 41 |
* Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and |
| 42 |
* lookups by key will fail: you can only look things up by numeric |
| 43 |
* index, and you have to use addpos234() and delpos234(). |
| 44 |
*/ |
| 45 |
tree234 *newtree234(cmpfn234 cmp); |
| 46 |
|
| 47 |
/* |
| 48 |
* Free a 2-3-4 tree (not including freeing the elements). |
| 49 |
*/ |
| 50 |
void freetree234(tree234 * t); |
| 51 |
|
| 52 |
/* |
| 53 |
* Add an element e to a sorted 2-3-4 tree t. Returns e on success, |
| 54 |
* or if an existing element compares equal, returns that. |
| 55 |
*/ |
| 56 |
void *add234(tree234 * t, void *e); |
| 57 |
|
| 58 |
/* |
| 59 |
* Add an element e to an unsorted 2-3-4 tree t. Returns e on |
| 60 |
* success, NULL on failure. (Failure should only occur if the |
| 61 |
* index is out of range or the tree is sorted.) |
| 62 |
* |
| 63 |
* Index range can be from 0 to the tree's current element count, |
| 64 |
* inclusive. |
| 65 |
*/ |
| 66 |
void *addpos234(tree234 * t, void *e, int index); |
| 67 |
|
| 68 |
/* |
| 69 |
* Look up the element at a given numeric index in a 2-3-4 tree. |
| 70 |
* Returns NULL if the index is out of range. |
| 71 |
* |
| 72 |
* One obvious use for this function is in iterating over the whole |
| 73 |
* of a tree (sorted or unsorted): |
| 74 |
* |
| 75 |
* for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p); |
| 76 |
* |
| 77 |
* or |
| 78 |
* |
| 79 |
* int maxcount = count234(tree); |
| 80 |
* for (i = 0; i < maxcount; i++) { |
| 81 |
* p = index234(tree, i); |
| 82 |
* assert(p != NULL); |
| 83 |
* consume(p); |
| 84 |
* } |
| 85 |
*/ |
| 86 |
void *index234(tree234 * t, int index); |
| 87 |
|
| 88 |
/* |
| 89 |
* Find an element e in a sorted 2-3-4 tree t. Returns NULL if not |
| 90 |
* found. e is always passed as the first argument to cmp, so cmp |
| 91 |
* can be an asymmetric function if desired. cmp can also be passed |
| 92 |
* as NULL, in which case the compare function from the tree proper |
| 93 |
* will be used. |
| 94 |
* |
| 95 |
* Three of these functions are special cases of findrelpos234. The |
| 96 |
* non-`pos' variants lack the `index' parameter: if the parameter |
| 97 |
* is present and non-NULL, it must point to an integer variable |
| 98 |
* which will be filled with the numeric index of the returned |
| 99 |
* element. |
| 100 |
* |
| 101 |
* The non-`rel' variants lack the `relation' parameter. This |
| 102 |
* parameter allows you to specify what relation the element you |
| 103 |
* provide has to the element you're looking for. This parameter |
| 104 |
* can be: |
| 105 |
* |
| 106 |
* REL234_EQ - find only an element that compares equal to e |
| 107 |
* REL234_LT - find the greatest element that compares < e |
| 108 |
* REL234_LE - find the greatest element that compares <= e |
| 109 |
* REL234_GT - find the smallest element that compares > e |
| 110 |
* REL234_GE - find the smallest element that compares >= e |
| 111 |
* |
| 112 |
* Non-`rel' variants assume REL234_EQ. |
| 113 |
* |
| 114 |
* If `rel' is REL234_GT or REL234_LT, the `e' parameter may be |
| 115 |
* NULL. In this case, REL234_GT will return the smallest element |
| 116 |
* in the tree, and REL234_LT will return the greatest. This gives |
| 117 |
* an alternative means of iterating over a sorted tree, instead of |
| 118 |
* using index234: |
| 119 |
* |
| 120 |
* // to loop forwards |
| 121 |
* for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;) |
| 122 |
* consume(p); |
| 123 |
* |
| 124 |
* // to loop backwards |
| 125 |
* for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;) |
| 126 |
* consume(p); |
| 127 |
*/ |
| 128 |
enum { |
| 129 |
REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE |
| 130 |
}; |
| 131 |
void *find234(tree234 * t, void *e, cmpfn234 cmp); |
| 132 |
void *findrel234(tree234 * t, void *e, cmpfn234 cmp, int relation); |
| 133 |
void *findpos234(tree234 * t, void *e, cmpfn234 cmp, int *index); |
| 134 |
void *findrelpos234(tree234 * t, void *e, cmpfn234 cmp, int relation, |
| 135 |
int *index); |
| 136 |
|
| 137 |
/* |
| 138 |
* Delete an element e in a 2-3-4 tree. Does not free the element, |
| 139 |
* merely removes all links to it from the tree nodes. |
| 140 |
* |
| 141 |
* delpos234 deletes the element at a particular tree index: it |
| 142 |
* works on both sorted and unsorted trees. |
| 143 |
* |
| 144 |
* del234 deletes the element passed to it, so it only works on |
| 145 |
* sorted trees. (It's equivalent to using findpos234 to determine |
| 146 |
* the index of an element, and then passing that index to |
| 147 |
* delpos234.) |
| 148 |
* |
| 149 |
* Both functions return a pointer to the element they delete, for |
| 150 |
* the user to free or pass on elsewhere or whatever. If the index |
| 151 |
* is out of range (delpos234) or the element is already not in the |
| 152 |
* tree (del234) then they return NULL. |
| 153 |
*/ |
| 154 |
void *del234(tree234 * t, void *e); |
| 155 |
void *delpos234(tree234 * t, int index); |
| 156 |
|
| 157 |
/* |
| 158 |
* Return the total element count of a tree234. |
| 159 |
*/ |
| 160 |
int count234(tree234 * t); |
| 161 |
|
| 162 |
/* |
| 163 |
* Split a tree234 into two valid tree234s. |
| 164 |
* |
| 165 |
* splitpos234 splits at a given index. If `before' is TRUE, the |
| 166 |
* items at and after that index are left in t and the ones before |
| 167 |
* are returned; if `before' is FALSE, the items before that index |
| 168 |
* are left in t and the rest are returned. |
| 169 |
* |
| 170 |
* split234 splits at a given key. You can pass any of the |
| 171 |
* relations used with findrel234, except for REL234_EQ. The items |
| 172 |
* in the tree that satisfy the relation are returned; the |
| 173 |
* remainder are left. |
| 174 |
*/ |
| 175 |
tree234 *splitpos234(tree234 * t, int index, int before); |
| 176 |
tree234 *split234(tree234 * t, void *e, cmpfn234 cmp, int rel); |
| 177 |
|
| 178 |
/* |
| 179 |
* Join two tree234s together into a single one. |
| 180 |
* |
| 181 |
* All the elements in t1 are placed to the left of all the |
| 182 |
* elements in t2. If the trees are sorted, there will be a test to |
| 183 |
* ensure that this satisfies the ordering criterion, and NULL will |
| 184 |
* be returned otherwise. If the trees are unsorted, there is no |
| 185 |
* restriction on the use of join234. |
| 186 |
* |
| 187 |
* The tree returned is t1 (join234) or t2 (join234r), if the |
| 188 |
* operation is successful. |
| 189 |
*/ |
| 190 |
tree234 *join234(tree234 * t1, tree234 * t2); |
| 191 |
tree234 *join234r(tree234 * t1, tree234 * t2); |
| 192 |
|
| 193 |
/* |
| 194 |
* Make a complete copy of a tree234. Element pointers will be |
| 195 |
* reused unless copyfn is non-NULL, in which case it will be used |
| 196 |
* to copy each element. (copyfn takes two `void *' parameters; the |
| 197 |
* first is private state and the second is the element. A simple |
| 198 |
* copy routine probably won't need private state.) |
| 199 |
*/ |
| 200 |
tree234 *copytree234(tree234 * t, copyfn234 copyfn, void *copyfnstate); |
| 201 |
|
| 202 |
#endif /* TREE234_H */ |