| 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 | /* |
| 39 | * Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and |
| 40 | * lookups by key will fail: you can only look things up by numeric |
| 41 | * index, and you have to use addpos234() and delpos234(). |
| 42 | */ |
| 43 | tree234 *newtree234(cmpfn234 cmp); |
| 44 | |
| 45 | /* |
| 46 | * Free a 2-3-4 tree (not including freeing the elements). |
| 47 | */ |
| 48 | void freetree234(tree234 *t); |
| 49 | |
| 50 | /* |
| 51 | * Add an element e to a sorted 2-3-4 tree t. Returns e on success, |
| 52 | * or if an existing element compares equal, returns that. |
| 53 | */ |
| 54 | void *add234(tree234 *t, void *e); |
| 55 | |
| 56 | /* |
| 57 | * Add an element e to an unsorted 2-3-4 tree t. Returns e on |
| 58 | * success, NULL on failure. (Failure should only occur if the |
| 59 | * index is out of range or the tree is sorted.) |
| 60 | * |
| 61 | * Index range can be from 0 to the tree's current element count, |
| 62 | * inclusive. |
| 63 | */ |
| 64 | void *addpos234(tree234 *t, void *e, int index); |
| 65 | |
| 66 | /* |
| 67 | * Look up the element at a given numeric index in a 2-3-4 tree. |
| 68 | * Returns NULL if the index is out of range. |
| 69 | * |
| 70 | * One obvious use for this function is in iterating over the whole |
| 71 | * of a tree (sorted or unsorted): |
| 72 | * |
| 73 | * for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p); |
| 74 | * |
| 75 | * or |
| 76 | * |
| 77 | * int maxcount = count234(tree); |
| 78 | * for (i = 0; i < maxcount; i++) { |
| 79 | * p = index234(tree, i); |
| 80 | * assert(p != NULL); |
| 81 | * consume(p); |
| 82 | * } |
| 83 | */ |
| 84 | void *index234(tree234 *t, int index); |
| 85 | |
| 86 | /* |
| 87 | * Find an element e in a sorted 2-3-4 tree t. Returns NULL if not |
| 88 | * found. e is always passed as the first argument to cmp, so cmp |
| 89 | * can be an asymmetric function if desired. cmp can also be passed |
| 90 | * as NULL, in which case the compare function from the tree proper |
| 91 | * will be used. |
| 92 | * |
| 93 | * Three of these functions are special cases of findrelpos234. The |
| 94 | * non-`pos' variants lack the `index' parameter: if the parameter |
| 95 | * is present and non-NULL, it must point to an integer variable |
| 96 | * which will be filled with the numeric index of the returned |
| 97 | * element. |
| 98 | * |
| 99 | * The non-`rel' variants lack the `relation' parameter. This |
| 100 | * parameter allows you to specify what relation the element you |
| 101 | * provide has to the element you're looking for. This parameter |
| 102 | * can be: |
| 103 | * |
| 104 | * REL234_EQ - find only an element that compares equal to e |
| 105 | * REL234_LT - find the greatest element that compares < e |
| 106 | * REL234_LE - find the greatest element that compares <= e |
| 107 | * REL234_GT - find the smallest element that compares > e |
| 108 | * REL234_GE - find the smallest element that compares >= e |
| 109 | * |
| 110 | * Non-`rel' variants assume REL234_EQ. |
| 111 | * |
| 112 | * If `rel' is REL234_GT or REL234_LT, the `e' parameter may be |
| 113 | * NULL. In this case, REL234_GT will return the smallest element |
| 114 | * in the tree, and REL234_LT will return the greatest. This gives |
| 115 | * an alternative means of iterating over a sorted tree, instead of |
| 116 | * using index234: |
| 117 | * |
| 118 | * // to loop forwards |
| 119 | * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;) |
| 120 | * consume(p); |
| 121 | * |
| 122 | * // to loop backwards |
| 123 | * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;) |
| 124 | * consume(p); |
| 125 | */ |
| 126 | enum { |
| 127 | REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE |
| 128 | }; |
| 129 | void *find234(tree234 *t, void *e, cmpfn234 cmp); |
| 130 | void *findrel234(tree234 *t, void *e, cmpfn234 cmp, int relation); |
| 131 | void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index); |
| 132 | void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp, int relation, |
| 133 | int *index); |
| 134 | |
| 135 | /* |
| 136 | * Delete an element e in a 2-3-4 tree. Does not free the element, |
| 137 | * merely removes all links to it from the tree nodes. |
| 138 | * |
| 139 | * delpos234 deletes the element at a particular tree index: it |
| 140 | * works on both sorted and unsorted trees. |
| 141 | * |
| 142 | * del234 deletes the element passed to it, so it only works on |
| 143 | * sorted trees. (It's equivalent to using findpos234 to determine |
| 144 | * the index of an element, and then passing that index to |
| 145 | * delpos234.) |
| 146 | * |
| 147 | * Both functions return a pointer to the element they delete, for |
| 148 | * the user to free or pass on elsewhere or whatever. If the index |
| 149 | * is out of range (delpos234) or the element is already not in the |
| 150 | * tree (del234) then they return NULL. |
| 151 | */ |
| 152 | void *del234(tree234 *t, void *e); |
| 153 | void *delpos234(tree234 *t, int index); |
| 154 | |
| 155 | /* |
| 156 | * Return the total element count of a tree234. |
| 157 | */ |
| 158 | int count234(tree234 *t); |
| 159 | |
| 160 | #endif /* TREE234_H */ |