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