rbtree_augmented.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. Red Black Trees
  4. (C) 1999 Andrea Arcangeli <[email protected]>
  5. (C) 2002 David Woodhouse <[email protected]>
  6. (C) 2012 Michel Lespinasse <[email protected]>
  7. linux/include/linux/rbtree_augmented.h
  8. */
  9. #ifndef _LINUX_RBTREE_AUGMENTED_H
  10. #define _LINUX_RBTREE_AUGMENTED_H
  11. #include <linux/compiler.h>
  12. #include <linux/rbtree.h>
  13. #include <linux/rcupdate.h>
  14. /*
  15. * Please note - only struct rb_augment_callbacks and the prototypes for
  16. * rb_insert_augmented() and rb_erase_augmented() are intended to be public.
  17. * The rest are implementation details you are not expected to depend on.
  18. *
  19. * See Documentation/core-api/rbtree.rst for documentation and samples.
  20. */
  21. struct rb_augment_callbacks {
  22. void (*propagate)(struct rb_node *node, struct rb_node *stop);
  23. void (*copy)(struct rb_node *old, struct rb_node *new);
  24. void (*rotate)(struct rb_node *old, struct rb_node *new);
  25. };
  26. extern void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  27. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  28. /*
  29. * Fixup the rbtree and update the augmented information when rebalancing.
  30. *
  31. * On insertion, the user must update the augmented information on the path
  32. * leading to the inserted node, then call rb_link_node() as usual and
  33. * rb_insert_augmented() instead of the usual rb_insert_color() call.
  34. * If rb_insert_augmented() rebalances the rbtree, it will callback into
  35. * a user provided function to update the augmented information on the
  36. * affected subtrees.
  37. */
  38. static inline void
  39. rb_insert_augmented(struct rb_node *node, struct rb_root *root,
  40. const struct rb_augment_callbacks *augment)
  41. {
  42. __rb_insert_augmented(node, root, augment->rotate);
  43. }
  44. static inline void
  45. rb_insert_augmented_cached(struct rb_node *node,
  46. struct rb_root_cached *root, bool newleft,
  47. const struct rb_augment_callbacks *augment)
  48. {
  49. if (newleft)
  50. root->rb_leftmost = node;
  51. rb_insert_augmented(node, &root->rb_root, augment);
  52. }
  53. /*
  54. * Template for declaring augmented rbtree callbacks (generic case)
  55. *
  56. * RBSTATIC: 'static' or empty
  57. * RBNAME: name of the rb_augment_callbacks structure
  58. * RBSTRUCT: struct type of the tree nodes
  59. * RBFIELD: name of struct rb_node field within RBSTRUCT
  60. * RBAUGMENTED: name of field within RBSTRUCT holding data for subtree
  61. * RBCOMPUTE: name of function that recomputes the RBAUGMENTED data
  62. */
  63. #define RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
  64. RBSTRUCT, RBFIELD, RBAUGMENTED, RBCOMPUTE) \
  65. static inline void \
  66. RBNAME ## _propagate(struct rb_node *rb, struct rb_node *stop) \
  67. { \
  68. while (rb != stop) { \
  69. RBSTRUCT *node = rb_entry(rb, RBSTRUCT, RBFIELD); \
  70. if (RBCOMPUTE(node, true)) \
  71. break; \
  72. rb = rb_parent(&node->RBFIELD); \
  73. } \
  74. } \
  75. static inline void \
  76. RBNAME ## _copy(struct rb_node *rb_old, struct rb_node *rb_new) \
  77. { \
  78. RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
  79. RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
  80. new->RBAUGMENTED = old->RBAUGMENTED; \
  81. } \
  82. static void \
  83. RBNAME ## _rotate(struct rb_node *rb_old, struct rb_node *rb_new) \
  84. { \
  85. RBSTRUCT *old = rb_entry(rb_old, RBSTRUCT, RBFIELD); \
  86. RBSTRUCT *new = rb_entry(rb_new, RBSTRUCT, RBFIELD); \
  87. new->RBAUGMENTED = old->RBAUGMENTED; \
  88. RBCOMPUTE(old, false); \
  89. } \
  90. RBSTATIC const struct rb_augment_callbacks RBNAME = { \
  91. .propagate = RBNAME ## _propagate, \
  92. .copy = RBNAME ## _copy, \
  93. .rotate = RBNAME ## _rotate \
  94. };
  95. /*
  96. * Template for declaring augmented rbtree callbacks,
  97. * computing RBAUGMENTED scalar as max(RBCOMPUTE(node)) for all subtree nodes.
  98. *
  99. * RBSTATIC: 'static' or empty
  100. * RBNAME: name of the rb_augment_callbacks structure
  101. * RBSTRUCT: struct type of the tree nodes
  102. * RBFIELD: name of struct rb_node field within RBSTRUCT
  103. * RBTYPE: type of the RBAUGMENTED field
  104. * RBAUGMENTED: name of RBTYPE field within RBSTRUCT holding data for subtree
  105. * RBCOMPUTE: name of function that returns the per-node RBTYPE scalar
  106. */
  107. #define RB_DECLARE_CALLBACKS_MAX(RBSTATIC, RBNAME, RBSTRUCT, RBFIELD, \
  108. RBTYPE, RBAUGMENTED, RBCOMPUTE) \
  109. static inline bool RBNAME ## _compute_max(RBSTRUCT *node, bool exit) \
  110. { \
  111. RBSTRUCT *child; \
  112. RBTYPE max = RBCOMPUTE(node); \
  113. if (node->RBFIELD.rb_left) { \
  114. child = rb_entry(node->RBFIELD.rb_left, RBSTRUCT, RBFIELD); \
  115. if (child->RBAUGMENTED > max) \
  116. max = child->RBAUGMENTED; \
  117. } \
  118. if (node->RBFIELD.rb_right) { \
  119. child = rb_entry(node->RBFIELD.rb_right, RBSTRUCT, RBFIELD); \
  120. if (child->RBAUGMENTED > max) \
  121. max = child->RBAUGMENTED; \
  122. } \
  123. if (exit && node->RBAUGMENTED == max) \
  124. return true; \
  125. node->RBAUGMENTED = max; \
  126. return false; \
  127. } \
  128. RB_DECLARE_CALLBACKS(RBSTATIC, RBNAME, \
  129. RBSTRUCT, RBFIELD, RBAUGMENTED, RBNAME ## _compute_max)
  130. #define RB_RED 0
  131. #define RB_BLACK 1
  132. #define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
  133. #define __rb_color(pc) ((pc) & 1)
  134. #define __rb_is_black(pc) __rb_color(pc)
  135. #define __rb_is_red(pc) (!__rb_color(pc))
  136. #define rb_color(rb) __rb_color((rb)->__rb_parent_color)
  137. #define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
  138. #define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
  139. static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
  140. {
  141. rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
  142. }
  143. static inline void rb_set_parent_color(struct rb_node *rb,
  144. struct rb_node *p, int color)
  145. {
  146. rb->__rb_parent_color = (unsigned long)p | color;
  147. }
  148. static inline void
  149. __rb_change_child(struct rb_node *old, struct rb_node *new,
  150. struct rb_node *parent, struct rb_root *root)
  151. {
  152. if (parent) {
  153. if (parent->rb_left == old)
  154. WRITE_ONCE(parent->rb_left, new);
  155. else
  156. WRITE_ONCE(parent->rb_right, new);
  157. } else
  158. WRITE_ONCE(root->rb_node, new);
  159. }
  160. static inline void
  161. __rb_change_child_rcu(struct rb_node *old, struct rb_node *new,
  162. struct rb_node *parent, struct rb_root *root)
  163. {
  164. if (parent) {
  165. if (parent->rb_left == old)
  166. rcu_assign_pointer(parent->rb_left, new);
  167. else
  168. rcu_assign_pointer(parent->rb_right, new);
  169. } else
  170. rcu_assign_pointer(root->rb_node, new);
  171. }
  172. extern void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
  173. void (*augment_rotate)(struct rb_node *old, struct rb_node *new));
  174. static __always_inline struct rb_node *
  175. __rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  176. const struct rb_augment_callbacks *augment)
  177. {
  178. struct rb_node *child = node->rb_right;
  179. struct rb_node *tmp = node->rb_left;
  180. struct rb_node *parent, *rebalance;
  181. unsigned long pc;
  182. if (!tmp) {
  183. /*
  184. * Case 1: node to erase has no more than 1 child (easy!)
  185. *
  186. * Note that if there is one child it must be red due to 5)
  187. * and node must be black due to 4). We adjust colors locally
  188. * so as to bypass __rb_erase_color() later on.
  189. */
  190. pc = node->__rb_parent_color;
  191. parent = __rb_parent(pc);
  192. __rb_change_child(node, child, parent, root);
  193. if (child) {
  194. child->__rb_parent_color = pc;
  195. rebalance = NULL;
  196. } else
  197. rebalance = __rb_is_black(pc) ? parent : NULL;
  198. tmp = parent;
  199. } else if (!child) {
  200. /* Still case 1, but this time the child is node->rb_left */
  201. tmp->__rb_parent_color = pc = node->__rb_parent_color;
  202. parent = __rb_parent(pc);
  203. __rb_change_child(node, tmp, parent, root);
  204. rebalance = NULL;
  205. tmp = parent;
  206. } else {
  207. struct rb_node *successor = child, *child2;
  208. tmp = child->rb_left;
  209. if (!tmp) {
  210. /*
  211. * Case 2: node's successor is its right child
  212. *
  213. * (n) (s)
  214. * / \ / \
  215. * (x) (s) -> (x) (c)
  216. * \
  217. * (c)
  218. */
  219. parent = successor;
  220. child2 = successor->rb_right;
  221. augment->copy(node, successor);
  222. } else {
  223. /*
  224. * Case 3: node's successor is leftmost under
  225. * node's right child subtree
  226. *
  227. * (n) (s)
  228. * / \ / \
  229. * (x) (y) -> (x) (y)
  230. * / /
  231. * (p) (p)
  232. * / /
  233. * (s) (c)
  234. * \
  235. * (c)
  236. */
  237. do {
  238. parent = successor;
  239. successor = tmp;
  240. tmp = tmp->rb_left;
  241. } while (tmp);
  242. child2 = successor->rb_right;
  243. WRITE_ONCE(parent->rb_left, child2);
  244. WRITE_ONCE(successor->rb_right, child);
  245. rb_set_parent(child, successor);
  246. augment->copy(node, successor);
  247. augment->propagate(parent, successor);
  248. }
  249. tmp = node->rb_left;
  250. WRITE_ONCE(successor->rb_left, tmp);
  251. rb_set_parent(tmp, successor);
  252. pc = node->__rb_parent_color;
  253. tmp = __rb_parent(pc);
  254. __rb_change_child(node, successor, tmp, root);
  255. if (child2) {
  256. rb_set_parent_color(child2, parent, RB_BLACK);
  257. rebalance = NULL;
  258. } else {
  259. rebalance = rb_is_black(successor) ? parent : NULL;
  260. }
  261. successor->__rb_parent_color = pc;
  262. tmp = successor;
  263. }
  264. augment->propagate(tmp, NULL);
  265. return rebalance;
  266. }
  267. static __always_inline void
  268. rb_erase_augmented(struct rb_node *node, struct rb_root *root,
  269. const struct rb_augment_callbacks *augment)
  270. {
  271. struct rb_node *rebalance = __rb_erase_augmented(node, root, augment);
  272. if (rebalance)
  273. __rb_erase_color(rebalance, root, augment->rotate);
  274. }
  275. static __always_inline void
  276. rb_erase_augmented_cached(struct rb_node *node, struct rb_root_cached *root,
  277. const struct rb_augment_callbacks *augment)
  278. {
  279. if (root->rb_leftmost == node)
  280. root->rb_leftmost = rb_next(node);
  281. rb_erase_augmented(node, &root->rb_root, augment);
  282. }
  283. #endif /* _LINUX_RBTREE_AUGMENTED_H */