tree234.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * Delete an element e in a 2-3-4 tree. Does not free the element,
  127. * merely removes all links to it from the tree nodes.
  128. *
  129. * delpos234 deletes the element at a particular tree index: it
  130. * works on both sorted and unsorted trees.
  131. *
  132. * del234 deletes the element passed to it, so it only works on
  133. * sorted trees. (It's equivalent to using findpos234 to determine
  134. * the index of an element, and then passing that index to
  135. * delpos234.)
  136. *
  137. * Both functions return a pointer to the element they delete, for
  138. * the user to free or pass on elsewhere or whatever. If the index
  139. * is out of range (delpos234) or the element is already not in the
  140. * tree (del234) then they return NULL.
  141. */
  142. void *del234(tree234 * t, void *e);
  143. void *delpos234(tree234 * t, int index);
  144. /*
  145. * Return the total element count of a tree234.
  146. */
  147. int count234(tree234 * t);
  148. #endif /* TREE234_H */