3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
5 (C) 2012 Michel Lespinasse <walken@google.com>
7 * SPDX-License-Identifier: GPL-2.0+
12 #include <linux/rbtree_augmented.h>
14 #include <linux/export.h>
16 #include <ubi_uboot.h>
19 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
21 * 1) A node is either red or black
22 * 2) The root is black
23 * 3) All leaves (NULL) are black
24 * 4) Both children of every red node are black
25 * 5) Every simple path from root to leaves contains the same number
28 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
29 * consecutive red nodes in a path and every red node is therefore followed by
30 * a black. So if B is the number of black nodes on every simple path (as per
31 * 5), then the longest possible path due to 4 is 2B.
33 * We shall indicate color with case, where black nodes are uppercase and red
34 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
35 * parentheses and have some accompanying text comment.
38 static inline void rb_set_black(struct rb_node *rb)
40 rb->__rb_parent_color |= RB_BLACK;
43 static inline struct rb_node *rb_red_parent(struct rb_node *red)
45 return (struct rb_node *)red->__rb_parent_color;
49 * Helper function for rotations:
50 * - old's parent and color get assigned to new
51 * - old gets assigned new as a parent and 'color' as a color.
54 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
55 struct rb_root *root, int color)
57 struct rb_node *parent = rb_parent(old);
58 new->__rb_parent_color = old->__rb_parent_color;
59 rb_set_parent_color(old, new, color);
60 __rb_change_child(old, new, parent, root);
63 static __always_inline void
64 __rb_insert(struct rb_node *node, struct rb_root *root,
65 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
67 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
71 * Loop invariant: node is red
73 * If there is a black parent, we are done.
74 * Otherwise, take some corrective action as we don't
75 * want a red root or two consecutive red nodes.
78 rb_set_parent_color(node, NULL, RB_BLACK);
80 } else if (rb_is_black(parent))
83 gparent = rb_red_parent(parent);
85 tmp = gparent->rb_right;
86 if (parent != tmp) { /* parent == gparent->rb_left */
87 if (tmp && rb_is_red(tmp)) {
89 * Case 1 - color flips
97 * However, since g's parent might be red, and
98 * 4) does not allow this, we need to recurse
101 rb_set_parent_color(tmp, gparent, RB_BLACK);
102 rb_set_parent_color(parent, gparent, RB_BLACK);
104 parent = rb_parent(node);
105 rb_set_parent_color(node, parent, RB_RED);
109 tmp = parent->rb_right;
112 * Case 2 - left rotate at parent
120 * This still leaves us in violation of 4), the
121 * continuation into Case 3 will fix that.
123 parent->rb_right = tmp = node->rb_left;
124 node->rb_left = parent;
126 rb_set_parent_color(tmp, parent,
128 rb_set_parent_color(parent, node, RB_RED);
129 augment_rotate(parent, node);
131 tmp = node->rb_right;
135 * Case 3 - right rotate at gparent
143 gparent->rb_left = tmp; /* == parent->rb_right */
144 parent->rb_right = gparent;
146 rb_set_parent_color(tmp, gparent, RB_BLACK);
147 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
148 augment_rotate(gparent, parent);
151 tmp = gparent->rb_left;
152 if (tmp && rb_is_red(tmp)) {
153 /* Case 1 - color flips */
154 rb_set_parent_color(tmp, gparent, RB_BLACK);
155 rb_set_parent_color(parent, gparent, RB_BLACK);
157 parent = rb_parent(node);
158 rb_set_parent_color(node, parent, RB_RED);
162 tmp = parent->rb_left;
164 /* Case 2 - right rotate at parent */
165 parent->rb_left = tmp = node->rb_right;
166 node->rb_right = parent;
168 rb_set_parent_color(tmp, parent,
170 rb_set_parent_color(parent, node, RB_RED);
171 augment_rotate(parent, node);
176 /* Case 3 - left rotate at gparent */
177 gparent->rb_right = tmp; /* == parent->rb_left */
178 parent->rb_left = gparent;
180 rb_set_parent_color(tmp, gparent, RB_BLACK);
181 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
182 augment_rotate(gparent, parent);
189 * Inline version for rb_erase() use - we want to be able to inline
190 * and eliminate the dummy_rotate callback there
192 static __always_inline void
193 ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
194 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
196 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
201 * - node is black (or NULL on first iteration)
202 * - node is not the root (parent is not NULL)
203 * - All leaf paths going through parent and node have a
204 * black node count that is 1 lower than other leaf paths.
206 sibling = parent->rb_right;
207 if (node != sibling) { /* node == parent->rb_left */
208 if (rb_is_red(sibling)) {
210 * Case 1 - left rotate at parent
218 parent->rb_right = tmp1 = sibling->rb_left;
219 sibling->rb_left = parent;
220 rb_set_parent_color(tmp1, parent, RB_BLACK);
221 __rb_rotate_set_parents(parent, sibling, root,
223 augment_rotate(parent, sibling);
226 tmp1 = sibling->rb_right;
227 if (!tmp1 || rb_is_black(tmp1)) {
228 tmp2 = sibling->rb_left;
229 if (!tmp2 || rb_is_black(tmp2)) {
231 * Case 2 - sibling color flip
232 * (p could be either color here)
240 * This leaves us violating 5) which
241 * can be fixed by flipping p to black
242 * if it was red, or by recursing at p.
243 * p is red when coming from Case 1.
245 rb_set_parent_color(sibling, parent,
247 if (rb_is_red(parent))
248 rb_set_black(parent);
251 parent = rb_parent(node);
258 * Case 3 - right rotate at sibling
259 * (p could be either color here)
269 sibling->rb_left = tmp1 = tmp2->rb_right;
270 tmp2->rb_right = sibling;
271 parent->rb_right = tmp2;
273 rb_set_parent_color(tmp1, sibling,
275 augment_rotate(sibling, tmp2);
280 * Case 4 - left rotate at parent + color flips
281 * (p and sl could be either color here.
282 * After rotation, p becomes black, s acquires
283 * p's color, and sl keeps its color)
291 parent->rb_right = tmp2 = sibling->rb_left;
292 sibling->rb_left = parent;
293 rb_set_parent_color(tmp1, sibling, RB_BLACK);
295 rb_set_parent(tmp2, parent);
296 __rb_rotate_set_parents(parent, sibling, root,
298 augment_rotate(parent, sibling);
301 sibling = parent->rb_left;
302 if (rb_is_red(sibling)) {
303 /* Case 1 - right rotate at parent */
304 parent->rb_left = tmp1 = sibling->rb_right;
305 sibling->rb_right = parent;
306 rb_set_parent_color(tmp1, parent, RB_BLACK);
307 __rb_rotate_set_parents(parent, sibling, root,
309 augment_rotate(parent, sibling);
312 tmp1 = sibling->rb_left;
313 if (!tmp1 || rb_is_black(tmp1)) {
314 tmp2 = sibling->rb_right;
315 if (!tmp2 || rb_is_black(tmp2)) {
316 /* Case 2 - sibling color flip */
317 rb_set_parent_color(sibling, parent,
319 if (rb_is_red(parent))
320 rb_set_black(parent);
323 parent = rb_parent(node);
329 /* Case 3 - right rotate at sibling */
330 sibling->rb_right = tmp1 = tmp2->rb_left;
331 tmp2->rb_left = sibling;
332 parent->rb_left = tmp2;
334 rb_set_parent_color(tmp1, sibling,
336 augment_rotate(sibling, tmp2);
340 /* Case 4 - left rotate at parent + color flips */
341 parent->rb_left = tmp2 = sibling->rb_right;
342 sibling->rb_right = parent;
343 rb_set_parent_color(tmp1, sibling, RB_BLACK);
345 rb_set_parent(tmp2, parent);
346 __rb_rotate_set_parents(parent, sibling, root,
348 augment_rotate(parent, sibling);
354 /* Non-inline version for rb_erase_augmented() use */
355 void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
356 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
358 ____rb_erase_color(parent, root, augment_rotate);
360 EXPORT_SYMBOL(__rb_erase_color);
363 * Non-augmented rbtree manipulation functions.
365 * We use dummy augmented callbacks here, and have the compiler optimize them
366 * out of the rb_insert_color() and rb_erase() function definitions.
369 static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
370 static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
371 static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
373 static const struct rb_augment_callbacks dummy_callbacks = {
374 dummy_propagate, dummy_copy, dummy_rotate
377 void rb_insert_color(struct rb_node *node, struct rb_root *root)
379 __rb_insert(node, root, dummy_rotate);
381 EXPORT_SYMBOL(rb_insert_color);
383 void rb_erase(struct rb_node *node, struct rb_root *root)
385 struct rb_node *rebalance;
386 rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
388 ____rb_erase_color(rebalance, root, dummy_rotate);
390 EXPORT_SYMBOL(rb_erase);
393 * Augmented rbtree manipulation functions.
395 * This instantiates the same __always_inline functions as in the non-augmented
396 * case, but this time with user-defined callbacks.
399 void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
400 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
402 __rb_insert(node, root, augment_rotate);
404 EXPORT_SYMBOL(__rb_insert_augmented);
407 * This function returns the first node (in sort order) of the tree.
409 struct rb_node *rb_first(const struct rb_root *root)
420 EXPORT_SYMBOL(rb_first);
422 struct rb_node *rb_last(const struct rb_root *root)
433 EXPORT_SYMBOL(rb_last);
435 struct rb_node *rb_next(const struct rb_node *node)
437 struct rb_node *parent;
439 if (RB_EMPTY_NODE(node))
443 * If we have a right-hand child, go down and then left as far
446 if (node->rb_right) {
447 node = node->rb_right;
448 while (node->rb_left)
450 return (struct rb_node *)node;
454 * No right-hand children. Everything down and left is smaller than us,
455 * so any 'next' node must be in the general direction of our parent.
456 * Go up the tree; any time the ancestor is a right-hand child of its
457 * parent, keep going up. First time it's a left-hand child of its
458 * parent, said parent is our 'next' node.
460 while ((parent = rb_parent(node)) && node == parent->rb_right)
465 EXPORT_SYMBOL(rb_next);
467 struct rb_node *rb_prev(const struct rb_node *node)
469 struct rb_node *parent;
471 if (RB_EMPTY_NODE(node))
475 * If we have a left-hand child, go down and then right as far
479 node = node->rb_left;
480 while (node->rb_right)
482 return (struct rb_node *)node;
486 * No left-hand children. Go up till we find an ancestor which
487 * is a right-hand child of its parent.
489 while ((parent = rb_parent(node)) && node == parent->rb_left)
494 EXPORT_SYMBOL(rb_prev);
496 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
497 struct rb_root *root)
499 struct rb_node *parent = rb_parent(victim);
501 /* Set the surrounding nodes to point to the replacement */
502 __rb_change_child(victim, new, parent, root);
504 rb_set_parent(victim->rb_left, new);
505 if (victim->rb_right)
506 rb_set_parent(victim->rb_right, new);
508 /* Copy the pointers/colour from the victim to the replacement */
511 EXPORT_SYMBOL(rb_replace_node);
513 static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
517 node = node->rb_left;
518 else if (node->rb_right)
519 node = node->rb_right;
521 return (struct rb_node *)node;
525 struct rb_node *rb_next_postorder(const struct rb_node *node)
527 const struct rb_node *parent;
530 parent = rb_parent(node);
532 /* If we're sitting on node, we've already seen our children */
533 if (parent && node == parent->rb_left && parent->rb_right) {
534 /* If we are the parent's left node, go to the parent's right
535 * node then all the way down to the left */
536 return rb_left_deepest_node(parent->rb_right);
538 /* Otherwise we are the parent's right node, and the parent
540 return (struct rb_node *)parent;
542 EXPORT_SYMBOL(rb_next_postorder);
544 struct rb_node *rb_first_postorder(const struct rb_root *root)
549 return rb_left_deepest_node(root->rb_node);
551 EXPORT_SYMBOL(rb_first_postorder);