rbtree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (c) 2010, 2011 Richard Braun.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. *
  26. * Red-black tree.
  27. */
  28. #ifndef _KERN_RBTREE_H
  29. #define _KERN_RBTREE_H
  30. #include <stddef.h>
  31. #include <kern/assert.h>
  32. #include <kern/macros.h>
  33. #include <sys/types.h>
  34. /*
  35. * Indexes of the left and right nodes in the children array of a node.
  36. */
  37. #define RBTREE_LEFT 0
  38. #define RBTREE_RIGHT 1
  39. /*
  40. * Red-black node.
  41. */
  42. struct rbtree_node;
  43. /*
  44. * Red-black tree.
  45. */
  46. struct rbtree;
  47. /*
  48. * Static tree initializer.
  49. */
  50. #define RBTREE_INITIALIZER { NULL }
  51. #include "rbtree_i.h"
  52. /*
  53. * Initialize a tree.
  54. */
  55. static inline void rbtree_init(struct rbtree *tree)
  56. {
  57. tree->root = NULL;
  58. }
  59. /*
  60. * Initialize a node.
  61. *
  62. * A node is in no tree when its parent points to itself.
  63. */
  64. static inline void rbtree_node_init(struct rbtree_node *node)
  65. {
  66. assert(rbtree_check_alignment(node));
  67. node->parent = (unsigned long)node | RBTREE_COLOR_RED;
  68. node->children[RBTREE_LEFT] = NULL;
  69. node->children[RBTREE_RIGHT] = NULL;
  70. }
  71. /*
  72. * Return true if node is in no tree.
  73. */
  74. static inline int rbtree_node_unlinked(const struct rbtree_node *node)
  75. {
  76. return rbtree_parent(node) == node;
  77. }
  78. /*
  79. * Macro that evaluates to the address of the structure containing the
  80. * given node based on the given type and member.
  81. */
  82. #define rbtree_entry(node, type, member) structof(node, type, member)
  83. /*
  84. * Return true if tree is empty.
  85. */
  86. static inline int rbtree_empty(const struct rbtree *tree)
  87. {
  88. return tree->root == NULL;
  89. }
  90. /*
  91. * Look up a node in a tree.
  92. *
  93. * Note that implementing the lookup algorithm as a macro gives two benefits:
  94. * First, it avoids the overhead of a callback function. Next, the type of the
  95. * cmp_fn parameter isn't rigid. The only guarantee offered by this
  96. * implementation is that the key parameter is the first parameter given to
  97. * cmp_fn. This way, users can pass only the value they need for comparison
  98. * instead of e.g. allocating a full structure on the stack.
  99. *
  100. * See rbtree_insert().
  101. */
  102. #define rbtree_lookup(tree, key, cmp_fn) \
  103. MACRO_BEGIN \
  104. struct rbtree_node *___cur; \
  105. int ___diff; \
  106. \
  107. ___cur = (tree)->root; \
  108. \
  109. while (___cur != NULL) { \
  110. ___diff = cmp_fn(key, ___cur); \
  111. \
  112. if (___diff == 0) \
  113. break; \
  114. \
  115. ___cur = ___cur->children[rbtree_d2i(___diff)]; \
  116. } \
  117. \
  118. ___cur; \
  119. MACRO_END
  120. /*
  121. * Look up a node or one of its nearest nodes in a tree.
  122. *
  123. * This macro essentially acts as rbtree_lookup() but if no entry matched
  124. * the key, an additional step is performed to obtain the next or previous
  125. * node, depending on the direction (left or right).
  126. *
  127. * The constraints that apply to the key parameter are the same as for
  128. * rbtree_lookup().
  129. */
  130. #define rbtree_lookup_nearest(tree, key, cmp_fn, dir) \
  131. MACRO_BEGIN \
  132. struct rbtree_node *___cur, *___prev; \
  133. int ___diff, ___index; \
  134. \
  135. ___prev = NULL; \
  136. ___index = -1; \
  137. ___cur = (tree)->root; \
  138. \
  139. while (___cur != NULL) { \
  140. ___diff = cmp_fn(key, ___cur); \
  141. \
  142. if (___diff == 0) \
  143. break; \
  144. \
  145. ___prev = ___cur; \
  146. ___index = rbtree_d2i(___diff); \
  147. ___cur = ___cur->children[___index]; \
  148. } \
  149. \
  150. if (___cur == NULL) \
  151. ___cur = rbtree_nearest(___prev, ___index, dir); \
  152. \
  153. ___cur; \
  154. MACRO_END
  155. /*
  156. * Insert a node in a tree.
  157. *
  158. * This macro performs a standard lookup to obtain the insertion point of
  159. * the given node in the tree (it is assumed that the inserted node never
  160. * compares equal to any other entry in the tree) and links the node. It
  161. * then checks red-black rules violations, and rebalances the tree if
  162. * necessary.
  163. *
  164. * Unlike rbtree_lookup(), the cmp_fn parameter must compare two complete
  165. * entries, so it is suggested to use two different comparison inline
  166. * functions, such as myobj_cmp_lookup() and myobj_cmp_insert(). There is no
  167. * guarantee about the order of the nodes given to the comparison function.
  168. *
  169. * See rbtree_lookup().
  170. */
  171. #define rbtree_insert(tree, node, cmp_fn) \
  172. MACRO_BEGIN \
  173. struct rbtree_node *___cur, *___prev; \
  174. int ___diff, ___index; \
  175. \
  176. ___prev = NULL; \
  177. ___index = -1; \
  178. ___cur = (tree)->root; \
  179. \
  180. while (___cur != NULL) { \
  181. ___diff = cmp_fn(node, ___cur); \
  182. assert(___diff != 0); \
  183. ___prev = ___cur; \
  184. ___index = rbtree_d2i(___diff); \
  185. ___cur = ___cur->children[___index]; \
  186. } \
  187. \
  188. rbtree_insert_rebalance(tree, ___prev, ___index, node); \
  189. MACRO_END
  190. /*
  191. * Look up a node/slot pair in a tree.
  192. *
  193. * This macro essentially acts as rbtree_lookup() but in addition to a node,
  194. * it also returns a slot, which identifies an insertion point in the tree.
  195. * If the returned node is null, the slot can be used by rbtree_insert_slot()
  196. * to insert without the overhead of an additional lookup. The slot is a
  197. * simple unsigned long integer.
  198. *
  199. * The constraints that apply to the key parameter are the same as for
  200. * rbtree_lookup().
  201. */
  202. #define rbtree_lookup_slot(tree, key, cmp_fn, slot) \
  203. MACRO_BEGIN \
  204. struct rbtree_node *___cur, *___prev; \
  205. int ___diff, ___index; \
  206. \
  207. ___prev = NULL; \
  208. ___index = 0; \
  209. ___cur = (tree)->root; \
  210. \
  211. while (___cur != NULL) { \
  212. ___diff = cmp_fn(key, ___cur); \
  213. \
  214. if (___diff == 0) \
  215. break; \
  216. \
  217. ___prev = ___cur; \
  218. ___index = rbtree_d2i(___diff); \
  219. ___cur = ___cur->children[___index]; \
  220. } \
  221. \
  222. (slot) = rbtree_slot(___prev, ___index); \
  223. ___cur; \
  224. MACRO_END
  225. /*
  226. * Insert a node at an insertion point in a tree.
  227. *
  228. * This macro essentially acts as rbtree_insert() except that it doesn't
  229. * obtain the insertion point with a standard lookup. The insertion point
  230. * is obtained by calling rbtree_lookup_slot(). In addition, the new node
  231. * must not compare equal to an existing node in the tree (i.e. the slot
  232. * must denote a null node).
  233. */
  234. static inline void
  235. rbtree_insert_slot(struct rbtree *tree, unsigned long slot,
  236. struct rbtree_node *node)
  237. {
  238. struct rbtree_node *parent;
  239. int index;
  240. parent = rbtree_slot_parent(slot);
  241. index = rbtree_slot_index(slot);
  242. rbtree_insert_rebalance(tree, parent, index, node);
  243. }
  244. /*
  245. * Remove a node from a tree.
  246. *
  247. * After completion, the node is stale.
  248. */
  249. void rbtree_remove(struct rbtree *tree, struct rbtree_node *node);
  250. /*
  251. * Return the first node of a tree.
  252. */
  253. #define rbtree_first(tree) rbtree_firstlast(tree, RBTREE_LEFT)
  254. /*
  255. * Return the last node of a tree.
  256. */
  257. #define rbtree_last(tree) rbtree_firstlast(tree, RBTREE_RIGHT)
  258. /*
  259. * Return the node previous to the given node.
  260. */
  261. #define rbtree_prev(node) rbtree_walk(node, RBTREE_LEFT)
  262. /*
  263. * Return the node next to the given node.
  264. */
  265. #define rbtree_next(node) rbtree_walk(node, RBTREE_RIGHT)
  266. /*
  267. * Forge a loop to process all nodes of a tree, removing them when visited.
  268. *
  269. * This macro can only be used to destroy a tree, so that the resources used
  270. * by the entries can be released by the user. It basically removes all nodes
  271. * without doing any color checking.
  272. *
  273. * After completion, all nodes and the tree root member are stale.
  274. */
  275. #define rbtree_for_each_remove(tree, node, tmp) \
  276. for (node = rbtree_postwalk_deepest(tree), \
  277. tmp = rbtree_postwalk_unlink(node); \
  278. node != NULL; \
  279. node = tmp, tmp = rbtree_postwalk_unlink(node))
  280. #endif /* _KERN_RBTREE_H */