tree234.h 7.6 KB

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