rbtree_augmented.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. static inline void
  38. rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  39. const struct rb_augment_callbacks *augment)
  40. {
  41. __rb_insert_augmented(node, root, augment->rotate);
  42. }
  43. #define RB_DECLARE_CALLBACKS(rbstatic, rbname, rbstruct, rbfield, \
  44. rbtype, rbaugmented, rbcompute) \
  45. static inline void \
  46. rbname ## _propagate(struct rb_node *rb, struct rb_node *stop) \
  47. { \
  48. while (rb != stop) { \
  49. rbstruct *node = rb_entry(rb, rbstruct, rbfield); \
  50. rbtype augmented = rbcompute(node); \
  51. if (node->rbaugmented == augmented) \
  52. break; \
  53. node->rbaugmented = augmented; \
  54. rb = rb_parent(&node->rbfield); \
  55. } \
  56. } \
  57. static inline void \
  58. rbname ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
  59. { \
  60. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  61. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  62. new->rbaugmented = old->rbaugmented; \
  63. } \
  64. static void \
  65. rbname ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
  66. { \
  67. rbstruct *old = rb_entry(rb_old, rbstruct, rbfield); \
  68. rbstruct *new = rb_entry(rb_new, rbstruct, rbfield); \
  69. new->rbaugmented = old->rbaugmented; \
  70. old->rbaugmented = rbcompute(old); \
  71. } \
  72. rbstatic const struct rb_augment_callbacks rbname = { \
  73. rbname ## _propagate, rbname ## _copy, rbname ## _rotate \
  74. };
  75. #define RB_RED 0
  76. #define RB_BLACK 1
  77. #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
  78. #define __rb_color(pc) ((pc) & 1)
  79. #define __rb_is_black(pc) __rb_color(pc)
  80. #define __rb_is_red(pc) (!__rb_color(pc))
  81. #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
  82. #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
  83. #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
  84. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  85. {
  86. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  87. }
  88. static inline void rb_set_parent_color(struct rb_node *rb,
  89. struct rb_node *p, int color)
  90. {
  91. rb->__rb_parent_color = (unsigned long)p | color;
  92. }
  93. static inline void
  94. __rb_change_child(struct rb_node *old, struct rb_node *new,
  95. struct rb_node *parent, struct rb_root *root)
  96. {
  97. if (parent) {
  98. if (parent->rb_left == old)
  99. parent->rb_left = new;
  100. else
  101. parent->rb_right = new;
  102. } else
  103. root->rb_node = new;
  104. }
  105. extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  106. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  107. static __always_inline struct rb_node *
  108. __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  109. const struct rb_augment_callbacks *augment)
  110. {
  111. struct rb_node *child = node->rb_right, *tmp = node->rb_left;
  112. struct rb_node *parent, *rebalance;
  113. unsigned long pc;
  114. if (!tmp) {
  115. /*
  116. * Case 1: node to erase has no more than 1 child (easy!)
  117. *
  118. * Note that if there is one child it must be red due to 5)
  119. * and node must be black due to 4). We adjust colors locally
  120. * so as to bypass __rb_erase_color() later on.
  121. */
  122. pc = node->__rb_parent_color;
  123. parent = __rb_parent(pc);
  124. __rb_change_child(node, child, parent, root);
  125. if (child) {
  126. child->__rb_parent_color = pc;
  127. rebalance = NULL;
  128. } else
  129. rebalance = __rb_is_black(pc) ? parent : NULL;
  130. tmp = parent;
  131. } else if (!child) {
  132. /* Still case 1, but this time the child is node->rb_left */
  133. tmp->__rb_parent_color = pc = node->__rb_parent_color;
  134. parent = __rb_parent(pc);
  135. __rb_change_child(node, tmp, parent, root);
  136. rebalance = NULL;
  137. tmp = parent;
  138. } else {
  139. struct rb_node *successor = child, *child2;
  140. tmp = child->rb_left;
  141. if (!tmp) {
  142. /*
  143. * Case 2: node's successor is its right child
  144. *
  145. * (n) (s)
  146. * / \ / \
  147. * (x) (s) -> (x) (c)
  148. * \
  149. * (c)
  150. */
  151. parent = successor;
  152. child2 = successor->rb_right;
  153. augment->copy(node, successor);
  154. } else {
  155. /*
  156. * Case 3: node's successor is leftmost under
  157. * node's right child subtree
  158. *
  159. * (n) (s)
  160. * / \ / \
  161. * (x) (y) -> (x) (y)
  162. * / /
  163. * (p) (p)
  164. * / /
  165. * (s) (c)
  166. * \
  167. * (c)
  168. */
  169. do {
  170. parent = successor;
  171. successor = tmp;
  172. tmp = tmp->rb_left;
  173. } while (tmp);
  174. parent->rb_left = child2 = successor->rb_right;
  175. successor->rb_right = child;
  176. rb_set_parent(child, successor);
  177. augment->copy(node, successor);
  178. augment->propagate(parent, successor);
  179. }
  180. successor->rb_left = tmp = node->rb_left;
  181. rb_set_parent(tmp, successor);
  182. pc = node->__rb_parent_color;
  183. tmp = __rb_parent(pc);
  184. __rb_change_child(node, successor, tmp, root);
  185. if (child2) {
  186. successor->__rb_parent_color = pc;
  187. rb_set_parent_color(child2, parent, RB_BLACK);
  188. rebalance = NULL;
  189. } else {
  190. unsigned long pc2 = successor->__rb_parent_color;
  191. successor->__rb_parent_color = pc;
  192. rebalance = __rb_is_black(pc2) ? parent : NULL;
  193. }
  194. tmp = successor;
  195. }
  196. augment->propagate(tmp, NULL);
  197. return rebalance;
  198. }
  199. static __always_inline void
  200. rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  201. const struct rb_augment_callbacks *augment)
  202. {
  203. struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
  204. if (rebalance)
  205. __rb_erase_color(rebalance, root, augment->rotate);
  206. }
  207. #endif /* _LINUX_RBTREE_AUGMENTED_H */