rbtree_augmented.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. /*
  24. * Please note - only struct rb_augment_callbacks and the prototypes for
  25. * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
  26. * The rest are implementation details you are not expected to depend on.
  27. *
  28. * See Documentation/rbtree.txt for documentation and samples.
  29. */
  30. struct rb_augment_callbacks {
  31. void (*propagate)(struct rb_node *node, struct rb_node *stop);
  32. void (*copy)(struct rb_node *old, struct rb_node *new);
  33. void (*rotate)(struct rb_node *old, struct rb_node *new);
  34. };
  35. extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  36. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  37. /*
  38. * Fixup the rbtree and update the augmented information when rebalancing.
  39. *
  40. * On insertion, the user must update the augmented information on the path
  41. * leading to the inserted node, then call rb_link_node() as usual and
  42. * rb_augment_inserted() instead of the usual rb_insert_color() call.
  43. * If rb_augment_inserted() rebalances the rbtree, it will callback into
  44. * a user provided function to update the augmented information on the
  45. * affected subtrees.
  46. */
  47. static inline void
  48. rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  49. const struct rb_augment_callbacks *augment)
  50. {
  51. __rb_insert_augmented(node, root, augment->rotate);
  52. }
  53. #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
  54. rbtype, rbaugmented, rbcompute) \
  55. static inline void \
  56. rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
  57. { \
  58. while (rb != stop) { \
  59. rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
  60. rbtype augmented = rbcompute(node); \
  61. if (node->rbaugmented == augmented) \
  62. break; \
  63. node->rbaugmented = augmented; \
  64. rb = rb_parent(&node->rbfield); \
  65. } \
  66. } \
  67. static inline void \
  68. rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
  69. { \
  70. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  71. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  72. new->rbaugmented = old->rbaugmented; \
  73. } \
  74. static void \
  75. rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
  76. { \
  77. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  78. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  79. new->rbaugmented = old->rbaugmented; \
  80. old->rbaugmented = rbcompute(old); \
  81. } \
  82. rbstatic const struct rb_augment_callbacks rbname = { \
  83. rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
  84. };
  85. #define RB_RED 0
  86. #define RB_BLACK 1
  87. #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
  88. #define __rb_color(pc) ((pc) & 1)
  89. #define __rb_is_black(pc) __rb_color(pc)
  90. #define __rb_is_red(pc) (!__rb_color(pc))
  91. #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
  92. #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
  93. #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
  94. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  95. {
  96. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  97. }
  98. static inline void rb_set_parent_color(struct rb_node *rb,
  99. struct rb_node *p, int color)
  100. {
  101. rb->__rb_parent_color = (unsigned long)p | color;
  102. }
  103. static inline void
  104. __rb_change_child(struct rb_node *old, struct rb_node *new,
  105. struct rb_node *parent, struct rb_root *root)
  106. {
  107. if (parent) {
  108. if (parent->rb_left == old)
  109. WRITE_ONCE(parent->rb_left, new);
  110. else
  111. WRITE_ONCE(parent->rb_right, new);
  112. } else
  113. WRITE_ONCE(root->rb_node, new);
  114. }
  115. static inline void
  116. __rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
  117. struct rb_node *parent, struct rb_root *root)
  118. {
  119. if (parent) {
  120. if (parent->rb_left == old)
  121. rcu_assign_pointer(parent->rb_left, new);
  122. else
  123. rcu_assign_pointer(parent->rb_right, new);
  124. } else
  125. rcu_assign_pointer(root->rb_node, new);
  126. }
  127. extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  128. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  129. static __always_inline struct rb_node *
  130. __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  131. const struct rb_augment_callbacks *augment)
  132. {
  133. struct rb_node *child = node->rb_right;
  134. struct rb_node *tmp = node->rb_left;
  135. struct rb_node *parent, *rebalance;
  136. unsigned long pc;
  137. if (!tmp) {
  138. /*
  139. * Case 1: node to erase has no more than 1 child (easy!)
  140. *
  141. * Note that if there is one child it must be red due to 5)
  142. * and node must be black due to 4). We adjust colors locally
  143. * so as to bypass __rb_erase_color() later on.
  144. */
  145. pc = node->__rb_parent_color;
  146. parent = __rb_parent(pc);
  147. __rb_change_child(node, child, parent, root);
  148. if (child) {
  149. child->__rb_parent_color = pc;
  150. rebalance = NULL;
  151. } else
  152. rebalance = __rb_is_black(pc) ? parent : NULL;
  153. tmp = parent;
  154. } else if (!child) {
  155. /* Still case 1, but this time the child is node->rb_left */
  156. tmp->__rb_parent_color = pc = node->__rb_parent_color;
  157. parent = __rb_parent(pc);
  158. __rb_change_child(node, tmp, parent, root);
  159. rebalance = NULL;
  160. tmp = parent;
  161. } else {
  162. struct rb_node *successor = child, *child2;
  163. tmp = child->rb_left;
  164. if (!tmp) {
  165. /*
  166. * Case 2: node's successor is its right child
  167. *
  168. * (n) (s)
  169. * / \ / \
  170. * (x) (s) -> (x) (c)
  171. * \
  172. * (c)
  173. */
  174. parent = successor;
  175. child2 = successor->rb_right;
  176. augment->copy(node, successor);
  177. } else {
  178. /*
  179. * Case 3: node's successor is leftmost under
  180. * node's right child subtree
  181. *
  182. * (n) (s)
  183. * / \ / \
  184. * (x) (y) -> (x) (y)
  185. * / /
  186. * (p) (p)
  187. * / /
  188. * (s) (c)
  189. * \
  190. * (c)
  191. */
  192. do {
  193. parent = successor;
  194. successor = tmp;
  195. tmp = tmp->rb_left;
  196. } while (tmp);
  197. child2 = successor->rb_right;
  198. WRITE_ONCE(parent->rb_left, child2);
  199. WRITE_ONCE(successor->rb_right, child);
  200. rb_set_parent(child, successor);
  201. augment->copy(node, successor);
  202. augment->propagate(parent, successor);
  203. }
  204. tmp = node->rb_left;
  205. WRITE_ONCE(successor->rb_left, tmp);
  206. rb_set_parent(tmp, successor);
  207. pc = node->__rb_parent_color;
  208. tmp = __rb_parent(pc);
  209. __rb_change_child(node, successor, tmp, root);
  210. if (child2) {
  211. successor->__rb_parent_color = pc;
  212. rb_set_parent_color(child2, parent, RB_BLACK);
  213. rebalance = NULL;
  214. } else {
  215. unsigned long pc2 = successor->__rb_parent_color;
  216. successor->__rb_parent_color = pc;
  217. rebalance = __rb_is_black(pc2) ? parent : NULL;
  218. }
  219. tmp = successor;
  220. }
  221. augment->propagate(tmp, NULL);
  222. return rebalance;
  223. }
  224. static __always_inline void
  225. rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  226. const struct rb_augment_callbacks *augment)
  227. {
  228. struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
  229. if (rebalance)
  230. __rb_erase_color(rebalance, root, augment->rotate);
  231. }
  232. #endif /* _LINUX_RBTREE_AUGMENTED_H */