rbtree.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 <assert.h>
  32. #include <stdint.h>
  33. #include <sys/types.h>
  34. #define MACRO_BEGIN ({
  35. #define MACRO_END })
  36. /*
  37. * Indexes of the left and right nodes in the children array of a node.
  38. */
  39. #define STARPU_RBTREE_LEFT 0
  40. #define STARPU_RBTREE_RIGHT 1
  41. /*
  42. * Red-black node.
  43. */
  44. struct starpu_rbtree_node;
  45. /*
  46. * Red-black tree.
  47. */
  48. struct starpu_rbtree;
  49. /*
  50. * Static tree initializer.
  51. */
  52. #define STARPU_RBTREE_INITIALIZER { NULL }
  53. #include "rbtree_i.h"
  54. /*
  55. * Initialize a tree.
  56. */
  57. static inline void starpu_rbtree_init(struct starpu_rbtree *tree)
  58. {
  59. tree->root = NULL;
  60. }
  61. /*
  62. * Initialize a node.
  63. *
  64. * A node is in no tree when its parent points to itself.
  65. */
  66. static inline void starpu_rbtree_node_init(struct starpu_rbtree_node *node)
  67. {
  68. assert(starpu_rbtree_check_alignment(node));
  69. node->parent = (uintptr_t)node | STARPU_RBTREE_COLOR_RED;
  70. node->children[STARPU_RBTREE_LEFT] = NULL;
  71. node->children[STARPU_RBTREE_RIGHT] = NULL;
  72. }
  73. /*
  74. * Return true if node is in no tree.
  75. */
  76. static inline int starpu_rbtree_node_unlinked(const struct starpu_rbtree_node *node)
  77. {
  78. return starpu_rbtree_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 starpu_rbtree_entry(node, type, member) structof(node, type, member)
  85. /*
  86. * Return true if tree is empty.
  87. */
  88. static inline int starpu_rbtree_empty(const struct starpu_rbtree *tree)
  89. {
  90. return tree->root == NULL;
  91. }
  92. /*
  93. * Look up a node in a tree.
  94. *
  95. * Note that implementing the lookup algorithm as a macro gives two benefits:
  96. * First, it avoids the overhead of a callback function. Next, the type of the
  97. * cmp_fn parameter isn't rigid. The only guarantee offered by this
  98. * implementation is that the key parameter is the first parameter given to
  99. * cmp_fn. This way, users can pass only the value they need for comparison
  100. * instead of e.g. allocating a full structure on the stack.
  101. *
  102. * See starpu_rbtree_insert().
  103. */
  104. #define starpu_rbtree_lookup(tree, key, cmp_fn) \
  105. MACRO_BEGIN \
  106. struct starpu_rbtree_node *___cur; \
  107. int ___diff; \
  108. \
  109. ___cur = (tree)->root; \
  110. \
  111. while (___cur != NULL) { \
  112. ___diff = cmp_fn(key, ___cur); \
  113. \
  114. if (___diff == 0) \
  115. break; \
  116. \
  117. ___cur = ___cur->children[starpu_rbtree_d2i(___diff)]; \
  118. } \
  119. \
  120. ___cur; \
  121. MACRO_END
  122. /*
  123. * Look up a node or one of its nearest nodes in a tree.
  124. *
  125. * This macro essentially acts as starpu_rbtree_lookup() but if no entry matched
  126. * the key, an additional step is performed to obtain the next or previous
  127. * node, depending on the direction (left or right).
  128. *
  129. * The constraints that apply to the key parameter are the same as for
  130. * starpu_rbtree_lookup().
  131. */
  132. #define starpu_rbtree_lookup_nearest(tree, key, cmp_fn, dir) \
  133. MACRO_BEGIN \
  134. struct starpu_rbtree_node *___cur, *___prev; \
  135. int ___diff, ___index; \
  136. \
  137. ___prev = NULL; \
  138. ___index = -1; \
  139. ___cur = (tree)->root; \
  140. \
  141. while (___cur != NULL) { \
  142. ___diff = cmp_fn(key, ___cur); \
  143. \
  144. if (___diff == 0) \
  145. break; \
  146. \
  147. ___prev = ___cur; \
  148. ___index = starpu_rbtree_d2i(___diff); \
  149. ___cur = ___cur->children[___index]; \
  150. } \
  151. \
  152. if (___cur == NULL) \
  153. ___cur = starpu_rbtree_nearest(___prev, ___index, dir); \
  154. \
  155. ___cur; \
  156. MACRO_END
  157. /*
  158. * Insert a node in a tree.
  159. *
  160. * This macro performs a standard lookup to obtain the insertion point of
  161. * the given node in the tree (it is assumed that the inserted node never
  162. * compares equal to any other entry in the tree) and links the node. It
  163. * then checks red-black rules violations, and rebalances the tree if
  164. * necessary.
  165. *
  166. * Unlike starpu_rbtree_lookup(), the cmp_fn parameter must compare two complete
  167. * entries, so it is suggested to use two different comparison inline
  168. * functions, such as myobj_cmp_lookup() and myobj_cmp_insert(). There is no
  169. * guarantee about the order of the nodes given to the comparison function.
  170. *
  171. * See starpu_rbtree_lookup().
  172. */
  173. #define starpu_rbtree_insert(tree, node, cmp_fn) \
  174. MACRO_BEGIN \
  175. struct starpu_rbtree_node *___cur, *___prev; \
  176. int ___diff, ___index; \
  177. \
  178. ___prev = NULL; \
  179. ___index = -1; \
  180. ___cur = (tree)->root; \
  181. \
  182. while (___cur != NULL) { \
  183. ___diff = cmp_fn(node, ___cur); \
  184. assert(___diff != 0); \
  185. ___prev = ___cur; \
  186. ___index = starpu_rbtree_d2i(___diff); \
  187. ___cur = ___cur->children[___index]; \
  188. } \
  189. \
  190. starpu_rbtree_insert_rebalance(tree, ___prev, ___index, node); \
  191. MACRO_END
  192. /*
  193. * Look up a node/slot pair in a tree.
  194. *
  195. * This macro essentially acts as starpu_rbtree_lookup() but in addition to a node,
  196. * it also returns a slot, which identifies an insertion point in the tree.
  197. * If the returned node is null, the slot can be used by starpu_rbtree_insert_slot()
  198. * to insert without the overhead of an additional lookup. The slot is a
  199. * simple uintptr_t integer.
  200. *
  201. * The constraints that apply to the key parameter are the same as for
  202. * starpu_rbtree_lookup().
  203. */
  204. #define starpu_rbtree_lookup_slot(tree, key, cmp_fn, slot) \
  205. MACRO_BEGIN \
  206. struct starpu_rbtree_node *___cur, *___prev; \
  207. int ___diff, ___index; \
  208. \
  209. ___prev = NULL; \
  210. ___index = 0; \
  211. ___cur = (tree)->root; \
  212. \
  213. while (___cur != NULL) { \
  214. ___diff = cmp_fn(key, ___cur); \
  215. \
  216. if (___diff == 0) \
  217. break; \
  218. \
  219. ___prev = ___cur; \
  220. ___index = starpu_rbtree_d2i(___diff); \
  221. ___cur = ___cur->children[___index]; \
  222. } \
  223. \
  224. (slot) = starpu_rbtree_slot(___prev, ___index); \
  225. ___cur; \
  226. MACRO_END
  227. /*
  228. * Insert a node at an insertion point in a tree.
  229. *
  230. * This macro essentially acts as starpu_rbtree_insert() except that it doesn't
  231. * obtain the insertion point with a standard lookup. The insertion point
  232. * is obtained by calling starpu_rbtree_lookup_slot(). In addition, the new node
  233. * must not compare equal to an existing node in the tree (i.e. the slot
  234. * must denote a null node).
  235. */
  236. static inline void starpu_rbtree_insert_slot(struct starpu_rbtree *tree, uintptr_t slot,
  237. struct starpu_rbtree_node *node)
  238. {
  239. struct starpu_rbtree_node *parent;
  240. int index;
  241. parent = starpu_rbtree_slot_parent(slot);
  242. index = starpu_rbtree_slot_index(slot);
  243. starpu_rbtree_insert_rebalance(tree, parent, index, node);
  244. }
  245. /*
  246. * Remove a node from a tree.
  247. *
  248. * After completion, the node is stale.
  249. */
  250. void starpu_rbtree_remove(struct starpu_rbtree *tree, struct starpu_rbtree_node *node);
  251. /*
  252. * Return the first node of a tree.
  253. */
  254. /* TODO: optimize by maintaining the first node of the tree */
  255. #define starpu_rbtree_first(tree) starpu_rbtree_firstlast(tree, STARPU_RBTREE_LEFT)
  256. /*
  257. * Return the last node of a tree.
  258. */
  259. /* TODO: optimize by maintaining the first node of the tree */
  260. /* TODO: could be useful to optimize the case when the key being inserted is
  261. * bigger that the biggest node */
  262. #define starpu_rbtree_last(tree) starpu_rbtree_firstlast(tree, STARPU_RBTREE_RIGHT)
  263. /*
  264. * Return the node previous to the given node.
  265. */
  266. #define starpu_rbtree_prev(node) starpu_rbtree_walk(node, STARPU_RBTREE_LEFT)
  267. /*
  268. * Return the node next to the given node.
  269. */
  270. #define starpu_rbtree_next(node) starpu_rbtree_walk(node, STARPU_RBTREE_RIGHT)
  271. /*
  272. * Forge a loop to process all nodes of a tree, removing them when visited.
  273. *
  274. * This macro can only be used to destroy a tree, so that the resources used
  275. * by the entries can be released by the user. It basically removes all nodes
  276. * without doing any color checking.
  277. *
  278. * After completion, all nodes and the tree root member are stale.
  279. */
  280. #define starpu_rbtree_for_each_remove(tree, node, tmp) \
  281. for (node = starpu_rbtree_postwalk_deepest(tree), \
  282. tmp = starpu_rbtree_postwalk_unlink(node); \
  283. node != NULL; \
  284. node = tmp, tmp = starpu_rbtree_postwalk_unlink(node)) \
  285. #endif /* _KERN_RBTREE_H */