rbtree_augmented.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  116. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  117. static __always_inline struct rb_node *
  118. __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  119. const struct rb_augment_callbacks *augment)
  120. {
  121. struct rb_node *child = node->rb_right;
  122. struct rb_node *tmp = node->rb_left;
  123. struct rb_node *parent, *rebalance;
  124. unsigned long pc;
  125. if (!tmp) {
  126. /*
  127. * Case 1: node to erase has no more than 1 child (easy!)
  128. *
  129. * Note that if there is one child it must be red due to 5)
  130. * and node must be black due to 4). We adjust colors locally
  131. * so as to bypass __rb_erase_color() later on.
  132. */
  133. pc = node->__rb_parent_color;
  134. parent = __rb_parent(pc);
  135. __rb_change_child(node, child, parent, root);
  136. if (child) {
  137. child->__rb_parent_color = pc;
  138. rebalance = NULL;
  139. } else
  140. rebalance = __rb_is_black(pc) ? parent : NULL;
  141. tmp = parent;
  142. } else if (!child) {
  143. /* Still case 1, but this time the child is node->rb_left */
  144. tmp->__rb_parent_color = pc = node->__rb_parent_color;
  145. parent = __rb_parent(pc);
  146. __rb_change_child(node, tmp, parent, root);
  147. rebalance = NULL;
  148. tmp = parent;
  149. } else {
  150. struct rb_node *successor = child, *child2;
  151. tmp = child->rb_left;
  152. if (!tmp) {
  153. /*
  154. * Case 2: node's successor is its right child
  155. *
  156. * (n) (s)
  157. * / \ / \
  158. * (x) (s) -> (x) (c)
  159. * \
  160. * (c)
  161. */
  162. parent = successor;
  163. child2 = successor->rb_right;
  164. augment->copy(node, successor);
  165. } else {
  166. /*
  167. * Case 3: node's successor is leftmost under
  168. * node's right child subtree
  169. *
  170. * (n) (s)
  171. * / \ / \
  172. * (x) (y) -> (x) (y)
  173. * / /
  174. * (p) (p)
  175. * / /
  176. * (s) (c)
  177. * \
  178. * (c)
  179. */
  180. do {
  181. parent = successor;
  182. successor = tmp;
  183. tmp = tmp->rb_left;
  184. } while (tmp);
  185. child2 = successor->rb_right;
  186. WRITE_ONCE(parent->rb_left, child2);
  187. WRITE_ONCE(successor->rb_right, child);
  188. rb_set_parent(child, successor);
  189. augment->copy(node, successor);
  190. augment->propagate(parent, successor);
  191. }
  192. tmp = node->rb_left;
  193. WRITE_ONCE(successor->rb_left, tmp);
  194. rb_set_parent(tmp, successor);
  195. pc = node->__rb_parent_color;
  196. tmp = __rb_parent(pc);
  197. __rb_change_child(node, successor, tmp, root);
  198. if (child2) {
  199. successor->__rb_parent_color = pc;
  200. rb_set_parent_color(child2, parent, RB_BLACK);
  201. rebalance = NULL;
  202. } else {
  203. unsigned long pc2 = successor->__rb_parent_color;
  204. successor->__rb_parent_color = pc;
  205. rebalance = __rb_is_black(pc2) ? parent : NULL;
  206. }
  207. tmp = successor;
  208. }
  209. augment->propagate(tmp, NULL);
  210. return rebalance;
  211. }
  212. static __always_inline void
  213. rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  214. const struct rb_augment_callbacks *augment)
  215. {
  216. struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
  217. if (rebalance)
  218. __rb_erase_color(rebalance, root, augment->rotate);
  219. }
  220. #endif /* _LINUX_RBTREE_AUGMENTED_H */