rbtree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (c) 2010-2015 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. *
  20. *
  21. * Red-black tree.
  22. */
  23. #ifndef KERN_RBTREE_H
  24. #define KERN_RBTREE_H
  25. #include <assert.h>
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. #include <kern/macros.h>
  29. /*
  30. * Indexes of the left and right nodes in the children array of a node.
  31. */
  32. #define RBTREE_LEFT 0
  33. #define RBTREE_RIGHT 1
  34. /*
  35. * Red-black node.
  36. */
  37. struct rbtree_node;
  38. /*
  39. * Red-black tree.
  40. */
  41. struct rbtree;
  42. /*
  43. * Insertion point identifier.
  44. */
  45. typedef uintptr_t rbtree_slot_t;
  46. /*
  47. * Static tree initializer.
  48. */
  49. #define RBTREE_INITIALIZER { NULL }
  50. #include <kern/rbtree_i.h>
  51. /*
  52. * Initialize a tree.
  53. */
  54. static inline void
  55. 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
  65. rbtree_node_init(struct rbtree_node *node)
  66. {
  67. assert(rbtree_node_check_alignment(node));
  68. node->parent = (uintptr_t)node | RBTREE_COLOR_RED;
  69. node->children[RBTREE_LEFT] = NULL;
  70. node->children[RBTREE_RIGHT] = NULL;
  71. }
  72. /*
  73. * Return true if node is in no tree.
  74. */
  75. static inline int
  76. rbtree_node_unlinked(const struct rbtree_node *node)
  77. {
  78. return rbtree_node_parent(node) == node;
  79. }
  80. /*
  81. * Macro that evaluates to the address of the structure containing the
  82. * given node based on the given type and member.
  83. */
  84. #define rbtree_entry(node, type, member) structof(node, type, member)
  85. /*
  86. * Return true if tree is empty.
  87. */
  88. static inline int
  89. rbtree_empty(const struct rbtree *tree)
  90. {
  91. return tree->root == NULL;
  92. }
  93. /*
  94. * Look up a node in a tree.
  95. *
  96. * Note that implementing the lookup algorithm as a macro gives two benefits:
  97. * First, it avoids the overhead of a callback function. Next, the type of the
  98. * cmp_fn parameter isn't rigid. The only guarantee offered by this
  99. * implementation is that the key parameter is the first parameter given to
  100. * cmp_fn. This way, users can pass only the value they need for comparison
  101. * instead of e.g. allocating a full structure on the stack.
  102. *
  103. * See rbtree_insert().
  104. */
  105. #define rbtree_lookup(tree, key, cmp_fn) \
  106. MACRO_BEGIN \
  107. struct rbtree_node *cur_; \
  108. int diff_; \
  109. \
  110. cur_ = (tree)->root; \
  111. \
  112. while (cur_ != NULL) { \
  113. diff_ = cmp_fn(key, cur_); \
  114. \
  115. if (diff_ == 0) { \
  116. break; \
  117. } \
  118. \
  119. cur_ = cur_->children[rbtree_d2i(diff_)]; \
  120. } \
  121. \
  122. cur_; \
  123. MACRO_END
  124. /*
  125. * Look up a node or one of its nearest nodes in a tree.
  126. *
  127. * This macro essentially acts as rbtree_lookup() but if no entry matched
  128. * the key, an additional step is performed to obtain the next or previous
  129. * node, depending on the direction (left or right).
  130. *
  131. * The constraints that apply to the key parameter are the same as for
  132. * rbtree_lookup().
  133. */
  134. #define rbtree_lookup_nearest(tree, key, cmp_fn, dir) \
  135. MACRO_BEGIN \
  136. struct rbtree_node *cur_, *prev_; \
  137. int diff_, index_; \
  138. \
  139. prev_ = NULL; \
  140. index_ = -1; \
  141. cur_ = (tree)->root; \
  142. \
  143. while (cur_ != NULL) { \
  144. diff_ = cmp_fn(key, cur_); \
  145. \
  146. if (diff_ == 0) { \
  147. break; \
  148. } \
  149. \
  150. prev_ = cur_; \
  151. index_ = rbtree_d2i(diff_); \
  152. cur_ = cur_->children[index_]; \
  153. } \
  154. \
  155. if (cur_ == NULL) { \
  156. cur_ = rbtree_nearest(prev_, index_, dir); \
  157. } \
  158. \
  159. cur_; \
  160. MACRO_END
  161. /*
  162. * Insert a node in a tree.
  163. *
  164. * This macro performs a standard lookup to obtain the insertion point of
  165. * the given node in the tree (it is assumed that the inserted node never
  166. * compares equal to any other entry in the tree) and links the node. It
  167. * then checks red-black rules violations, and rebalances the tree if
  168. * necessary.
  169. *
  170. * Unlike rbtree_lookup(), the cmp_fn parameter must compare two complete
  171. * entries, so it is suggested to use two different comparison inline
  172. * functions, such as myobj_cmp_lookup() and myobj_cmp_insert(). There is no
  173. * guarantee about the order of the nodes given to the comparison function.
  174. *
  175. * See rbtree_lookup().
  176. */
  177. #define rbtree_insert(tree, node, cmp_fn) \
  178. MACRO_BEGIN \
  179. struct rbtree_node *cur_, *prev_; \
  180. int diff_, index_; \
  181. \
  182. prev_ = NULL; \
  183. index_ = -1; \
  184. cur_ = (tree)->root; \
  185. \
  186. while (cur_ != NULL) { \
  187. diff_ = cmp_fn(node, cur_); \
  188. assert(diff_ != 0); \
  189. prev_ = cur_; \
  190. index_ = rbtree_d2i(diff_); \
  191. cur_ = cur_->children[index_]; \
  192. } \
  193. \
  194. rbtree_insert_rebalance(tree, prev_, index_, node); \
  195. MACRO_END
  196. /*
  197. * Look up a node/slot pair in a tree.
  198. *
  199. * This macro essentially acts as rbtree_lookup() but in addition to a node,
  200. * it also returns a slot, which identifies an insertion point in the tree.
  201. * If the returned node is NULL, the slot can be used by rbtree_insert_slot()
  202. * to insert without the overhead of an additional lookup.
  203. *
  204. * The constraints that apply to the key parameter are the same as for
  205. * rbtree_lookup().
  206. */
  207. #define rbtree_lookup_slot(tree, key, cmp_fn, slot) \
  208. MACRO_BEGIN \
  209. struct rbtree_node *cur_, *prev_; \
  210. int diff_, index_; \
  211. \
  212. prev_ = NULL; \
  213. index_ = 0; \
  214. cur_ = (tree)->root; \
  215. \
  216. while (cur_ != NULL) { \
  217. diff_ = cmp_fn(key, cur_); \
  218. \
  219. if (diff_ == 0) { \
  220. break; \
  221. } \
  222. \
  223. prev_ = cur_; \
  224. index_ = rbtree_d2i(diff_); \
  225. cur_ = cur_->children[index_]; \
  226. } \
  227. \
  228. (slot) = rbtree_slot(prev_, index_); \
  229. cur_; \
  230. MACRO_END
  231. /*
  232. * Insert a node at an insertion point in a tree.
  233. *
  234. * This macro essentially acts as rbtree_insert() except that it doesn't
  235. * obtain the insertion point with a standard lookup. The insertion point
  236. * is obtained by calling rbtree_lookup_slot(). In addition, the new node
  237. * must not compare equal to an existing node in the tree (i.e. the slot
  238. * must denote a NULL node).
  239. */
  240. static inline void
  241. rbtree_insert_slot(struct rbtree *tree, rbtree_slot_t slot,
  242. struct rbtree_node *node)
  243. {
  244. struct rbtree_node *parent;
  245. int index;
  246. parent = rbtree_slot_parent(slot);
  247. index = rbtree_slot_index(slot);
  248. rbtree_insert_rebalance(tree, parent, index, node);
  249. }
  250. /*
  251. * Replace a node at an insertion point in a tree.
  252. *
  253. * The given node must compare strictly equal to the previous node,
  254. * which is returned on completion.
  255. */
  256. void * rbtree_replace_slot(struct rbtree *tree, rbtree_slot_t slot,
  257. struct rbtree_node *node);
  258. /*
  259. * Remove a node from a tree.
  260. *
  261. * After completion, the node is stale.
  262. */
  263. void rbtree_remove(struct rbtree *tree, struct rbtree_node *node);
  264. /*
  265. * Return the first node of a tree.
  266. */
  267. #define rbtree_first(tree) rbtree_firstlast(tree, RBTREE_LEFT)
  268. /*
  269. * Return the last node of a tree.
  270. */
  271. #define rbtree_last(tree) rbtree_firstlast(tree, RBTREE_RIGHT)
  272. /*
  273. * Return the node previous to the given node.
  274. */
  275. #define rbtree_prev(node) rbtree_walk(node, RBTREE_LEFT)
  276. /*
  277. * Return the node next to the given node.
  278. */
  279. #define rbtree_next(node) rbtree_walk(node, RBTREE_RIGHT)
  280. /*
  281. * Forge a loop to process all nodes of a tree, removing them when visited.
  282. *
  283. * This macro can only be used to destroy a tree, so that the resources used
  284. * by the entries can be released by the user. It basically removes all nodes
  285. * without doing any color checking.
  286. *
  287. * After completion, all nodes and the tree root member are stale.
  288. */
  289. #define rbtree_for_each_remove(tree, node, tmp) \
  290. for (node = rbtree_postwalk_deepest(tree), \
  291. tmp = rbtree_postwalk_unlink(node); \
  292. node != NULL; \
  293. node = tmp, tmp = rbtree_postwalk_unlink(node))
  294. #endif /* KERN_RBTREE_H */