rbtree.h 12 KB

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