rbtree_augmented.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. Red Black Trees
  3. (C) 1999 Andrea Arcangeli <andrea@suse.de>
  4. (C) 2002 David Woodhouse <dwmw2@infradead.org>
  5. (C) 2012 Michel Lespinasse <walken@google.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. linux/include/linux/rbtree_augmented.h
  18. */
  19. #ifndef _LINUX_RBTREE_AUGMENTED_H
  20. #define _LINUX_RBTREE_AUGMENTED_H
  21. #include <linux/compiler.h>
  22. #include <linux/rbtree.h>
  23. #include <linux/rcupdate.h>
  24. /*
  25. * Please note - only struct rb_augment_callbacks and the prototypes for
  26. * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
  27. * The rest are implementation details you are not expected to depend on.
  28. *
  29. * See Documentation/rbtree.txt for documentation and samples.
  30. */
  31. struct rb_augment_callbacks {
  32. void (*propagate)(struct rb_node *node, struct rb_node *stop);
  33. void (*copy)(struct rb_node *old, struct rb_node *new);
  34. void (*rotate)(struct rb_node *old, struct rb_node *new);
  35. };
  36. extern void __rb_insert_augmented(struct rb_node *node,
  37. struct rb_root *root,
  38. bool newleft, struct rb_node **leftmost,
  39. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  40. /*
  41. * Fixup the rbtree and update the augmented information when rebalancing.
  42. *
  43. * On insertion, the user must update the augmented information on the path
  44. * leading to the inserted node, then call rb_link_node() as usual and
  45. * rb_augment_inserted() instead of the usual rb_insert_color() call.
  46. * If rb_augment_inserted() rebalances the rbtree, it will callback into
  47. * a user provided function to update the augmented information on the
  48. * affected subtrees.
  49. */
  50. static inline void
  51. rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  52. const struct rb_augment_callbacks *augment)
  53. {
  54. __rb_insert_augmented(node, root, false, NULL, augment->rotate);
  55. }
  56. static inline void
  57. rb_insert_augmented_cached(struct rb_node *node,
  58. struct rb_root_cached *root, bool newleft,
  59. const struct rb_augment_callbacks *augment)
  60. {
  61. __rb_insert_augmented(node, &root->rb_root,
  62. newleft, &root->rb_leftmost, augment->rotate);
  63. }
  64. #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
  65. rbtype, rbaugmented, rbcompute) \
  66. static inline void \
  67. rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
  68. { \
  69. while (rb != stop) { \
  70. rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
  71. rbtype augmented = rbcompute(node); \
  72. if (node->rbaugmented == augmented) \
  73. break; \
  74. node->rbaugmented = augmented; \
  75. rb = rb_parent(&node->rbfield); \
  76. } \
  77. } \
  78. static inline void \
  79. rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
  80. { \
  81. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  82. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  83. new->rbaugmented = old->rbaugmented; \
  84. } \
  85. static void \
  86. rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
  87. { \
  88. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  89. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  90. new->rbaugmented = old->rbaugmented; \
  91. old->rbaugmented = rbcompute(old); \
  92. } \
  93. rbstatic const struct rb_augment_callbacks rbname = { \
  94. .propagate = rbname ## _propagate, \
  95. .copy = rbname ## _copy, \
  96. .rotate = rbname ## _rotate \
  97. };
  98. #define RB_RED 0
  99. #define RB_BLACK 1
  100. #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
  101. #define __rb_color(pc) ((pc) & 1)
  102. #define __rb_is_black(pc) __rb_color(pc)
  103. #define __rb_is_red(pc) (!__rb_color(pc))
  104. #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
  105. #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
  106. #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
  107. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  108. {
  109. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  110. }
  111. static inline void rb_set_parent_color(struct rb_node *rb,
  112. struct rb_node *p, int color)
  113. {
  114. rb->__rb_parent_color = (unsigned long)p | color;
  115. }
  116. static inline void
  117. __rb_change_child(struct rb_node *old, struct rb_node *new,
  118. struct rb_node *parent, struct rb_root *root)
  119. {
  120. if (parent) {
  121. if (parent->rb_left == old)
  122. WRITE_ONCE(parent->rb_left, new);
  123. else
  124. WRITE_ONCE(parent->rb_right, new);
  125. } else
  126. WRITE_ONCE(root->rb_node, new);
  127. }
  128. static inline void
  129. __rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
  130. struct rb_node *parent, struct rb_root *root)
  131. {
  132. if (parent) {
  133. if (parent->rb_left == old)
  134. rcu_assign_pointer(parent->rb_left, new);
  135. else
  136. rcu_assign_pointer(parent->rb_right, new);
  137. } else
  138. rcu_assign_pointer(root->rb_node, new);
  139. }
  140. extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  141. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  142. static __always_inline struct rb_node *
  143. __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  144. struct rb_node **leftmost,
  145. const struct rb_augment_callbacks *augment)
  146. {
  147. struct rb_node *child = node->rb_right;
  148. struct rb_node *tmp = node->rb_left;
  149. struct rb_node *parent, *rebalance;
  150. unsigned long pc;
  151. if (leftmost && node == *leftmost)
  152. *leftmost = rb_next(node);
  153. if (!tmp) {
  154. /*
  155. * Case 1: node to erase has no more than 1 child (easy!)
  156. *
  157. * Note that if there is one child it must be red due to 5)
  158. * and node must be black due to 4). We adjust colors locally
  159. * so as to bypass __rb_erase_color() later on.
  160. */
  161. pc = node->__rb_parent_color;
  162. parent = __rb_parent(pc);
  163. __rb_change_child(node, child, parent, root);
  164. if (child) {
  165. child->__rb_parent_color = pc;
  166. rebalance = NULL;
  167. } else
  168. rebalance = __rb_is_black(pc) ? parent : NULL;
  169. tmp = parent;
  170. } else if (!child) {
  171. /* Still case 1, but this time the child is node->rb_left */
  172. tmp->__rb_parent_color = pc = node->__rb_parent_color;
  173. parent = __rb_parent(pc);
  174. __rb_change_child(node, tmp, parent, root);
  175. rebalance = NULL;
  176. tmp = parent;
  177. } else {
  178. struct rb_node *successor = child, *child2;
  179. tmp = child->rb_left;
  180. if (!tmp) {
  181. /*
  182. * Case 2: node's successor is its right child
  183. *
  184. * (n) (s)
  185. * / \ / \
  186. * (x) (s) -> (x) (c)
  187. * \
  188. * (c)
  189. */
  190. parent = successor;
  191. child2 = successor->rb_right;
  192. augment->copy(node, successor);
  193. } else {
  194. /*
  195. * Case 3: node's successor is leftmost under
  196. * node's right child subtree
  197. *
  198. * (n) (s)
  199. * / \ / \
  200. * (x) (y) -> (x) (y)
  201. * / /
  202. * (p) (p)
  203. * / /
  204. * (s) (c)
  205. * \
  206. * (c)
  207. */
  208. do {
  209. parent = successor;
  210. successor = tmp;
  211. tmp = tmp->rb_left;
  212. } while (tmp);
  213. child2 = successor->rb_right;
  214. WRITE_ONCE(parent->rb_left, child2);
  215. WRITE_ONCE(successor->rb_right, child);
  216. rb_set_parent(child, successor);
  217. augment->copy(node, successor);
  218. augment->propagate(parent, successor);
  219. }
  220. tmp = node->rb_left;
  221. WRITE_ONCE(successor->rb_left, tmp);
  222. rb_set_parent(tmp, successor);
  223. pc = node->__rb_parent_color;
  224. tmp = __rb_parent(pc);
  225. __rb_change_child(node, successor, tmp, root);
  226. if (child2) {
  227. successor->__rb_parent_color = pc;
  228. rb_set_parent_color(child2, parent, RB_BLACK);
  229. rebalance = NULL;
  230. } else {
  231. unsigned long pc2 = successor->__rb_parent_color;
  232. successor->__rb_parent_color = pc;
  233. rebalance = __rb_is_black(pc2) ? parent : NULL;
  234. }
  235. tmp = successor;
  236. }
  237. augment->propagate(tmp, NULL);
  238. return rebalance;
  239. }
  240. static __always_inline void
  241. rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  242. const struct rb_augment_callbacks *augment)
  243. {
  244. struct rb_node *rebalance = __rb_erase_augmented(node, root,
  245. NULL, augment);
  246. if (rebalance)
  247. __rb_erase_color(rebalance, root, augment->rotate);
  248. }
  249. static __always_inline void
  250. rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
  251. const struct rb_augment_callbacks *augment)
  252. {
  253. struct rb_node *rebalance = __rb_erase_augmented(node, &root->rb_root,
  254. &root->rb_leftmost,
  255. augment);
  256. if (rebalance)
  257. __rb_erase_color(rebalance, &root->rb_root, augment->rotate);
  258. }
  259. #endif /* _LINUX_RBTREE_AUGMENTED_H */