1 // SPDX-License-Identifier: GPL-2.0+
4 (C) 1999 Andrea Arcangeli <andrea@suse.de>
5 (C) 2002 David Woodhouse <dwmw2@infradead.org>
6 (C) 2012 Michel Lespinasse <walken@google.com>
11 #include <linux/rbtree_augmented.h>
13 #include <linux/export.h>
15 #include <ubi_uboot.h>
18 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
20 * 1) A node is either red or black
21 * 2) The root is black
22 * 3) All leaves (NULL) are black
23 * 4) Both children of every red node are black
24 * 5) Every simple path from root to leaves contains the same number
27 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
28 * consecutive red nodes in a path and every red node is therefore followed by
29 * a black. So if B is the number of black nodes on every simple path (as per
30 * 5), then the longest possible path due to 4 is 2B.
32 * We shall indicate color with case, where black nodes are uppercase and red
33 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
34 * parentheses and have some accompanying text comment.
37 static inline void rb_set_black(struct rb_node *rb)
39 rb->__rb_parent_color |= RB_BLACK;
42 static inline struct rb_node *rb_red_parent(struct rb_node *red)
44 return (struct rb_node *)red->__rb_parent_color;
48 * Helper function for rotations:
49 * - old's parent and color get assigned to new
50 * - old gets assigned new as a parent and 'color' as a color.
53 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
54 struct rb_root *root, int color)
56 struct rb_node *parent = rb_parent(old);
57 new->__rb_parent_color = old->__rb_parent_color;
58 rb_set_parent_color(old, new, color);
59 __rb_change_child(old, new, parent, root);
62 static __always_inline void
63 __rb_insert(struct rb_node *node, struct rb_root *root,
64 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
66 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
70 * Loop invariant: node is red
72 * If there is a black parent, we are done.
73 * Otherwise, take some corrective action as we don't
74 * want a red root or two consecutive red nodes.
77 rb_set_parent_color(node, NULL, RB_BLACK);
79 } else if (rb_is_black(parent))
82 gparent = rb_red_parent(parent);
84 tmp = gparent->rb_right;
85 if (parent != tmp) { /* parent == gparent->rb_left */
86 if (tmp && rb_is_red(tmp)) {
88 * Case 1 - color flips
96 * However, since g's parent might be red, and
97 * 4) does not allow this, we need to recurse
100 rb_set_parent_color(tmp, gparent, RB_BLACK);
101 rb_set_parent_color(parent, gparent, RB_BLACK);
103 parent = rb_parent(node);
104 rb_set_parent_color(node, parent, RB_RED);
108 tmp = parent->rb_right;
111 * Case 2 - left rotate at parent
119 * This still leaves us in violation of 4), the
120 * continuation into Case 3 will fix that.
122 parent->rb_right = tmp = node->rb_left;
123 node->rb_left = parent;
125 rb_set_parent_color(tmp, parent,
127 rb_set_parent_color(parent, node, RB_RED);
128 augment_rotate(parent, node);
130 tmp = node->rb_right;
134 * Case 3 - right rotate at gparent
142 gparent->rb_left = tmp; /* == parent->rb_right */
143 parent->rb_right = gparent;
145 rb_set_parent_color(tmp, gparent, RB_BLACK);
146 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
147 augment_rotate(gparent, parent);
150 tmp = gparent->rb_left;
151 if (tmp && rb_is_red(tmp)) {
152 /* Case 1 - color flips */
153 rb_set_parent_color(tmp, gparent, RB_BLACK);
154 rb_set_parent_color(parent, gparent, RB_BLACK);
156 parent = rb_parent(node);
157 rb_set_parent_color(node, parent, RB_RED);
161 tmp = parent->rb_left;
163 /* Case 2 - right rotate at parent */
164 parent->rb_left = tmp = node->rb_right;
165 node->rb_right = parent;
167 rb_set_parent_color(tmp, parent,
169 rb_set_parent_color(parent, node, RB_RED);
170 augment_rotate(parent, node);
175 /* Case 3 - left rotate at gparent */
176 gparent->rb_right = tmp; /* == parent->rb_left */
177 parent->rb_left = gparent;
179 rb_set_parent_color(tmp, gparent, RB_BLACK);
180 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
181 augment_rotate(gparent, parent);
188 * Inline version for rb_erase() use - we want to be able to inline
189 * and eliminate the dummy_rotate callback there
191 static __always_inline void
192 ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
193 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
195 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
200 * - node is black (or NULL on first iteration)
201 * - node is not the root (parent is not NULL)
202 * - All leaf paths going through parent and node have a
203 * black node count that is 1 lower than other leaf paths.
205 sibling = parent->rb_right;
206 if (node != sibling) { /* node == parent->rb_left */
207 if (rb_is_red(sibling)) {
209 * Case 1 - left rotate at parent
217 parent->rb_right = tmp1 = sibling->rb_left;
218 sibling->rb_left = parent;
219 rb_set_parent_color(tmp1, parent, RB_BLACK);
220 __rb_rotate_set_parents(parent, sibling, root,
222 augment_rotate(parent, sibling);
225 tmp1 = sibling->rb_right;
226 if (!tmp1 || rb_is_black(tmp1)) {
227 tmp2 = sibling->rb_left;
228 if (!tmp2 || rb_is_black(tmp2)) {
230 * Case 2 - sibling color flip
231 * (p could be either color here)
239 * This leaves us violating 5) which
240 * can be fixed by flipping p to black
241 * if it was red, or by recursing at p.
242 * p is red when coming from Case 1.
244 rb_set_parent_color(sibling, parent,
246 if (rb_is_red(parent))
247 rb_set_black(parent);
250 parent = rb_parent(node);
257 * Case 3 - right rotate at sibling
258 * (p could be either color here)
268 sibling->rb_left = tmp1 = tmp2->rb_right;
269 tmp2->rb_right = sibling;
270 parent->rb_right = tmp2;
272 rb_set_parent_color(tmp1, sibling,
274 augment_rotate(sibling, tmp2);
279 * Case 4 - left rotate at parent + color flips
280 * (p and sl could be either color here.
281 * After rotation, p becomes black, s acquires
282 * p's color, and sl keeps its color)
290 parent->rb_right = tmp2 = sibling->rb_left;
291 sibling->rb_left = parent;
292 rb_set_parent_color(tmp1, sibling, RB_BLACK);
294 rb_set_parent(tmp2, parent);
295 __rb_rotate_set_parents(parent, sibling, root,
297 augment_rotate(parent, sibling);
300 sibling = parent->rb_left;
301 if (rb_is_red(sibling)) {
302 /* Case 1 - right rotate at parent */
303 parent->rb_left = tmp1 = sibling->rb_right;
304 sibling->rb_right = parent;
305 rb_set_parent_color(tmp1, parent, RB_BLACK);
306 __rb_rotate_set_parents(parent, sibling, root,
308 augment_rotate(parent, sibling);
311 tmp1 = sibling->rb_left;
312 if (!tmp1 || rb_is_black(tmp1)) {
313 tmp2 = sibling->rb_right;
314 if (!tmp2 || rb_is_black(tmp2)) {
315 /* Case 2 - sibling color flip */
316 rb_set_parent_color(sibling, parent,
318 if (rb_is_red(parent))
319 rb_set_black(parent);
322 parent = rb_parent(node);
328 /* Case 3 - right rotate at sibling */
329 sibling->rb_right = tmp1 = tmp2->rb_left;
330 tmp2->rb_left = sibling;
331 parent->rb_left = tmp2;
333 rb_set_parent_color(tmp1, sibling,
335 augment_rotate(sibling, tmp2);
339 /* Case 4 - left rotate at parent + color flips */
340 parent->rb_left = tmp2 = sibling->rb_right;
341 sibling->rb_right = parent;
342 rb_set_parent_color(tmp1, sibling, RB_BLACK);
344 rb_set_parent(tmp2, parent);
345 __rb_rotate_set_parents(parent, sibling, root,
347 augment_rotate(parent, sibling);
353 /* Non-inline version for rb_erase_augmented() use */
354 void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
355 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
357 ____rb_erase_color(parent, root, augment_rotate);
359 EXPORT_SYMBOL(__rb_erase_color);
362 * Non-augmented rbtree manipulation functions.
364 * We use dummy augmented callbacks here, and have the compiler optimize them
365 * out of the rb_insert_color() and rb_erase() function definitions.
368 static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
369 static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
370 static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
372 static const struct rb_augment_callbacks dummy_callbacks = {
373 dummy_propagate, dummy_copy, dummy_rotate
376 void rb_insert_color(struct rb_node *node, struct rb_root *root)
378 __rb_insert(node, root, dummy_rotate);
380 EXPORT_SYMBOL(rb_insert_color);
382 void rb_erase(struct rb_node *node, struct rb_root *root)
384 struct rb_node *rebalance;
385 rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
387 ____rb_erase_color(rebalance, root, dummy_rotate);
389 EXPORT_SYMBOL(rb_erase);
392 * Augmented rbtree manipulation functions.
394 * This instantiates the same __always_inline functions as in the non-augmented
395 * case, but this time with user-defined callbacks.
398 void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
399 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
401 __rb_insert(node, root, augment_rotate);
403 EXPORT_SYMBOL(__rb_insert_augmented);
406 * This function returns the first node (in sort order) of the tree.
408 struct rb_node *rb_first(const struct rb_root *root)
419 EXPORT_SYMBOL(rb_first);
421 struct rb_node *rb_last(const struct rb_root *root)
432 EXPORT_SYMBOL(rb_last);
434 struct rb_node *rb_next(const struct rb_node *node)
436 struct rb_node *parent;
438 if (RB_EMPTY_NODE(node))
442 * If we have a right-hand child, go down and then left as far
445 if (node->rb_right) {
446 node = node->rb_right;
447 while (node->rb_left)
449 return (struct rb_node *)node;
453 * No right-hand children. Everything down and left is smaller than us,
454 * so any 'next' node must be in the general direction of our parent.
455 * Go up the tree; any time the ancestor is a right-hand child of its
456 * parent, keep going up. First time it's a left-hand child of its
457 * parent, said parent is our 'next' node.
459 while ((parent = rb_parent(node)) && node == parent->rb_right)
464 EXPORT_SYMBOL(rb_next);
466 struct rb_node *rb_prev(const struct rb_node *node)
468 struct rb_node *parent;
470 if (RB_EMPTY_NODE(node))
474 * If we have a left-hand child, go down and then right as far
478 node = node->rb_left;
479 while (node->rb_right)
481 return (struct rb_node *)node;
485 * No left-hand children. Go up till we find an ancestor which
486 * is a right-hand child of its parent.
488 while ((parent = rb_parent(node)) && node == parent->rb_left)
493 EXPORT_SYMBOL(rb_prev);
495 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
496 struct rb_root *root)
498 struct rb_node *parent = rb_parent(victim);
500 /* Set the surrounding nodes to point to the replacement */
501 __rb_change_child(victim, new, parent, root);
503 rb_set_parent(victim->rb_left, new);
504 if (victim->rb_right)
505 rb_set_parent(victim->rb_right, new);
507 /* Copy the pointers/colour from the victim to the replacement */
510 EXPORT_SYMBOL(rb_replace_node);
512 static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
516 node = node->rb_left;
517 else if (node->rb_right)
518 node = node->rb_right;
520 return (struct rb_node *)node;
524 struct rb_node *rb_next_postorder(const struct rb_node *node)
526 const struct rb_node *parent;
529 parent = rb_parent(node);
531 /* If we're sitting on node, we've already seen our children */
532 if (parent && node == parent->rb_left && parent->rb_right) {
533 /* If we are the parent's left node, go to the parent's right
534 * node then all the way down to the left */
535 return rb_left_deepest_node(parent->rb_right);
537 /* Otherwise we are the parent's right node, and the parent
539 return (struct rb_node *)parent;
541 EXPORT_SYMBOL(rb_next_postorder);
543 struct rb_node *rb_first_postorder(const struct rb_root *root)
548 return rb_left_deepest_node(root->rb_node);
550 EXPORT_SYMBOL(rb_first_postorder);