rbtree_augmented.h 7.5 KB

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