tree234.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #ifndef TREE234_H
  28. #define TREE234_H
  29. /*
  30. * This typedef is opaque outside tree234.c itself.
  31. */
  32. typedef struct tree234_Tag tree234;
  33. typedef int (*cmpfn234) (void *, void *);
  34. /*
  35. * Create a 2-3-4 tree. If `cmp' is NULL, the tree is unsorted, and
  36. * lookups by key will fail: you can only look things up by numeric
  37. * index, and you have to use addpos234() and delpos234().
  38. */
  39. tree234 *newtree234(cmpfn234 cmp);
  40. /*
  41. * Free a 2-3-4 tree (not including freeing the elements).
  42. */
  43. void freetree234(tree234 *t);
  44. /*
  45. * Add an element e to a sorted 2-3-4 tree t. Returns e on success,
  46. * or if an existing element compares equal, returns that.
  47. */
  48. void *add234(tree234 *t, void *e);
  49. /*
  50. * Add an element e to an unsorted 2-3-4 tree t. Returns e on
  51. * success, NULL on failure. (Failure should only occur if the
  52. * index is out of range or the tree is sorted.)
  53. *
  54. * Index range can be from 0 to the tree's current element count,
  55. * inclusive.
  56. */
  57. void *addpos234(tree234 *t, void *e, int index);
  58. /*
  59. * Look up the element at a given numeric index in a 2-3-4 tree.
  60. * Returns NULL if the index is out of range.
  61. *
  62. * One obvious use for this function is in iterating over the whole
  63. * of a tree (sorted or unsorted):
  64. *
  65. * for (i = 0; (p = index234(tree, i)) != NULL; i++) consume(p);
  66. *
  67. * or
  68. *
  69. * int maxcount = count234(tree);
  70. * for (i = 0; i < maxcount; i++) {
  71. * p = index234(tree, i);
  72. * assert(p != NULL);
  73. * consume(p);
  74. * }
  75. */
  76. void *index234(tree234 *t, int index);
  77. /*
  78. * Find an element e in a sorted 2-3-4 tree t. Returns NULL if not
  79. * found. e is always passed as the first argument to cmp, so cmp
  80. * can be an asymmetric function if desired. cmp can also be passed
  81. * as NULL, in which case the compare function from the tree proper
  82. * will be used.
  83. *
  84. * Three of these functions are special cases of findrelpos234. The
  85. * non-`pos' variants lack the `index' parameter: if the parameter
  86. * is present and non-NULL, it must point to an integer variable
  87. * which will be filled with the numeric index of the returned
  88. * element.
  89. *
  90. * The non-`rel' variants lack the `relation' parameter. This
  91. * parameter allows you to specify what relation the element you
  92. * provide has to the element you're looking for. This parameter
  93. * can be:
  94. *
  95. * REL234_EQ - find only an element that compares equal to e
  96. * REL234_LT - find the greatest element that compares < e
  97. * REL234_LE - find the greatest element that compares <= e
  98. * REL234_GT - find the smallest element that compares > e
  99. * REL234_GE - find the smallest element that compares >= e
  100. *
  101. * Non-`rel' variants assume REL234_EQ.
  102. *
  103. * If `rel' is REL234_GT or REL234_LT, the `e' parameter may be
  104. * NULL. In this case, REL234_GT will return the smallest element
  105. * in the tree, and REL234_LT will return the greatest. This gives
  106. * an alternative means of iterating over a sorted tree, instead of
  107. * using index234:
  108. *
  109. * // to loop forwards
  110. * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_GT)) != NULL ;)
  111. * consume(p);
  112. *
  113. * // to loop backwards
  114. * for (p = NULL; (p = findrel234(tree, p, NULL, REL234_LT)) != NULL ;)
  115. * consume(p);
  116. */
  117. enum {
  118. REL234_EQ, REL234_LT, REL234_LE, REL234_GT, REL234_GE
  119. };
  120. void *find234(tree234 *t, void *e, cmpfn234 cmp);
  121. void *findrel234(tree234 *t, void *e, cmpfn234 cmp, int relation);
  122. void *findpos234(tree234 *t, void *e, cmpfn234 cmp, int *index);
  123. void *findrelpos234(tree234 *t, void *e, cmpfn234 cmp, int relation,
  124. int *index);
  125. /*
  126. * A more general search type still. Use search234_start() to
  127. * initialise one of these state structures; it will fill in
  128. * state->element with an element of the tree, and state->index with
  129. * the index of that element. If you don't like that element, call
  130. * search234_step, with direction == -1 if you want an element earlier
  131. * in the tree, or +1 if you want a later one.
  132. *
  133. * If either function returns state->element == NULL, then you've
  134. * narrowed the search to a point between two adjacent elements, so
  135. * there are no further elements left to return consistent with the
  136. * constraints you've imposed. In this case, state->index tells you
  137. * how many elements come before the point you narrowed down to. After
  138. * this, you mustn't call search234_step again (unless the state
  139. * structure is first reinitialised).
  140. *
  141. * The use of this search system is that you get both the candidate
  142. * element _and_ its index at every stage, so you can use both of them
  143. * to make your decision. Also, you can remember element pointers from
  144. * earlier in the search.
  145. *
  146. * The fields beginning with underscores are private to the
  147. * implementation, and only exposed so that clients can know how much
  148. * space to allocate for the structure as a whole. Don't modify them.
  149. * (Except that it's safe to copy the whole structure.)
  150. */
  151. typedef struct search234_state {
  152. void *element;
  153. int index;
  154. int _lo, _hi, _last, _base;
  155. void *_node;
  156. } search234_state;
  157. void search234_start(search234_state *state, tree234 *t);
  158. void search234_step(search234_state *state, int direction);
  159. /*
  160. * Delete an element e in a 2-3-4 tree. Does not free the element,
  161. * merely removes all links to it from the tree nodes.
  162. *
  163. * delpos234 deletes the element at a particular tree index: it
  164. * works on both sorted and unsorted trees.
  165. *
  166. * del234 deletes the element passed to it, so it only works on
  167. * sorted trees. (It's equivalent to using findpos234 to determine
  168. * the index of an element, and then passing that index to
  169. * delpos234.)
  170. *
  171. * Both functions return a pointer to the element they delete, for
  172. * the user to free or pass on elsewhere or whatever. If the index
  173. * is out of range (delpos234) or the element is already not in the
  174. * tree (del234) then they return NULL.
  175. */
  176. void *del234(tree234 *t, void *e);
  177. void *delpos234(tree234 *t, int index);
  178. /*
  179. * Return the total element count of a tree234.
  180. */
  181. int count234(tree234 *t);
  182. #endif /* TREE234_H */