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+
13 #include <linux/rbtree_augmented.h>
15 #include <linux/export.h>
17 #include <ubi_uboot.h>
20 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
22 * 1) A node is either red or black
23 * 2) The root is black
24 * 3) All leaves (NULL) are black
25 * 4) Both children of every red node are black
26 * 5) Every simple path from root to leaves contains the same number
29 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
30 * consecutive red nodes in a path and every red node is therefore followed by
31 * a black. So if B is the number of black nodes on every simple path (as per
32 * 5), then the longest possible path due to 4 is 2B.
34 * We shall indicate color with case, where black nodes are uppercase and red
35 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
36 * parentheses and have some accompanying text comment.
39 static inline void rb_set_black(struct rb_node *rb)
41 rb->__rb_parent_color |= RB_BLACK;
44 static inline struct rb_node *rb_red_parent(struct rb_node *red)
46 return (struct rb_node *)red->__rb_parent_color;
50 * Helper function for rotations:
51 * - old's parent and color get assigned to new
52 * - old gets assigned new as a parent and 'color' as a color.
55 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
56 struct rb_root *root, int color)
58 struct rb_node *parent = rb_parent(old);
59 new->__rb_parent_color = old->__rb_parent_color;
60 rb_set_parent_color(old, new, color);
61 __rb_change_child(old, new, parent, root);
64 static __always_inline void
65 __rb_insert(struct rb_node *node, struct rb_root *root,
66 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
68 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
72 * Loop invariant: node is red
74 * If there is a black parent, we are done.
75 * Otherwise, take some corrective action as we don't
76 * want a red root or two consecutive red nodes.
79 rb_set_parent_color(node, NULL, RB_BLACK);
81 } else if (rb_is_black(parent))
84 gparent = rb_red_parent(parent);
86 tmp = gparent->rb_right;
87 if (parent != tmp) { /* parent == gparent->rb_left */
88 if (tmp && rb_is_red(tmp)) {
90 * Case 1 - color flips
98 * However, since g's parent might be red, and
99 * 4) does not allow this, we need to recurse
102 rb_set_parent_color(tmp, gparent, RB_BLACK);
103 rb_set_parent_color(parent, gparent, RB_BLACK);
105 parent = rb_parent(node);
106 rb_set_parent_color(node, parent, RB_RED);
110 tmp = parent->rb_right;
113 * Case 2 - left rotate at parent
121 * This still leaves us in violation of 4), the
122 * continuation into Case 3 will fix that.
124 parent->rb_right = tmp = node->rb_left;
125 node->rb_left = parent;
127 rb_set_parent_color(tmp, parent,
129 rb_set_parent_color(parent, node, RB_RED);
130 augment_rotate(parent, node);
132 tmp = node->rb_right;
136 * Case 3 - right rotate at gparent
144 gparent->rb_left = tmp; /* == parent->rb_right */
145 parent->rb_right = gparent;
147 rb_set_parent_color(tmp, gparent, RB_BLACK);
148 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
149 augment_rotate(gparent, parent);
152 tmp = gparent->rb_left;
153 if (tmp && rb_is_red(tmp)) {
154 /* Case 1 - color flips */
155 rb_set_parent_color(tmp, gparent, RB_BLACK);
156 rb_set_parent_color(parent, gparent, RB_BLACK);
158 parent = rb_parent(node);
159 rb_set_parent_color(node, parent, RB_RED);
163 tmp = parent->rb_left;
165 /* Case 2 - right rotate at parent */
166 parent->rb_left = tmp = node->rb_right;
167 node->rb_right = parent;
169 rb_set_parent_color(tmp, parent,
171 rb_set_parent_color(parent, node, RB_RED);
172 augment_rotate(parent, node);
177 /* Case 3 - left rotate at gparent */
178 gparent->rb_right = tmp; /* == parent->rb_left */
179 parent->rb_left = gparent;
181 rb_set_parent_color(tmp, gparent, RB_BLACK);
182 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
183 augment_rotate(gparent, parent);
190 * Inline version for rb_erase() use - we want to be able to inline
191 * and eliminate the dummy_rotate callback there
193 static __always_inline void
194 ____rb_erase_color(struct rb_node *parent, struct rb_root *root,
195 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
197 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
202 * - node is black (or NULL on first iteration)
203 * - node is not the root (parent is not NULL)
204 * - All leaf paths going through parent and node have a
205 * black node count that is 1 lower than other leaf paths.
207 sibling = parent->rb_right;
208 if (node != sibling) { /* node == parent->rb_left */
209 if (rb_is_red(sibling)) {
211 * Case 1 - left rotate at parent
219 parent->rb_right = tmp1 = sibling->rb_left;
220 sibling->rb_left = parent;
221 rb_set_parent_color(tmp1, parent, RB_BLACK);
222 __rb_rotate_set_parents(parent, sibling, root,
224 augment_rotate(parent, sibling);
227 tmp1 = sibling->rb_right;
228 if (!tmp1 || rb_is_black(tmp1)) {
229 tmp2 = sibling->rb_left;
230 if (!tmp2 || rb_is_black(tmp2)) {
232 * Case 2 - sibling color flip
233 * (p could be either color here)
241 * This leaves us violating 5) which
242 * can be fixed by flipping p to black
243 * if it was red, or by recursing at p.
244 * p is red when coming from Case 1.
246 rb_set_parent_color(sibling, parent,
248 if (rb_is_red(parent))
249 rb_set_black(parent);
252 parent = rb_parent(node);
259 * Case 3 - right rotate at sibling
260 * (p could be either color here)
270 sibling->rb_left = tmp1 = tmp2->rb_right;
271 tmp2->rb_right = sibling;
272 parent->rb_right = tmp2;
274 rb_set_parent_color(tmp1, sibling,
276 augment_rotate(sibling, tmp2);
281 * Case 4 - left rotate at parent + color flips
282 * (p and sl could be either color here.
283 * After rotation, p becomes black, s acquires
284 * p's color, and sl keeps its color)
292 parent->rb_right = tmp2 = sibling->rb_left;
293 sibling->rb_left = parent;
294 rb_set_parent_color(tmp1, sibling, RB_BLACK);
296 rb_set_parent(tmp2, parent);
297 __rb_rotate_set_parents(parent, sibling, root,
299 augment_rotate(parent, sibling);
302 sibling = parent->rb_left;
303 if (rb_is_red(sibling)) {
304 /* Case 1 - right rotate at parent */
305 parent->rb_left = tmp1 = sibling->rb_right;
306 sibling->rb_right = parent;
307 rb_set_parent_color(tmp1, parent, RB_BLACK);
308 __rb_rotate_set_parents(parent, sibling, root,
310 augment_rotate(parent, sibling);
313 tmp1 = sibling->rb_left;
314 if (!tmp1 || rb_is_black(tmp1)) {
315 tmp2 = sibling->rb_right;
316 if (!tmp2 || rb_is_black(tmp2)) {
317 /* Case 2 - sibling color flip */
318 rb_set_parent_color(sibling, parent,
320 if (rb_is_red(parent))
321 rb_set_black(parent);
324 parent = rb_parent(node);
330 /* Case 3 - right rotate at sibling */
331 sibling->rb_right = tmp1 = tmp2->rb_left;
332 tmp2->rb_left = sibling;
333 parent->rb_left = tmp2;
335 rb_set_parent_color(tmp1, sibling,
337 augment_rotate(sibling, tmp2);
341 /* Case 4 - left rotate at parent + color flips */
342 parent->rb_left = tmp2 = sibling->rb_right;
343 sibling->rb_right = parent;
344 rb_set_parent_color(tmp1, sibling, RB_BLACK);
346 rb_set_parent(tmp2, parent);
347 __rb_rotate_set_parents(parent, sibling, root,
349 augment_rotate(parent, sibling);
355 /* Non-inline version for rb_erase_augmented() use */
356 void __rb_erase_color(struct rb_node *parent, struct rb_root *root,
357 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
359 ____rb_erase_color(parent, root, augment_rotate);
361 EXPORT_SYMBOL(__rb_erase_color);
364 * Non-augmented rbtree manipulation functions.
366 * We use dummy augmented callbacks here, and have the compiler optimize them
367 * out of the rb_insert_color() and rb_erase() function definitions.
370 static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
371 static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
372 static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
374 static const struct rb_augment_callbacks dummy_callbacks = {
375 dummy_propagate, dummy_copy, dummy_rotate
378 void rb_insert_color(struct rb_node *node, struct rb_root *root)
380 __rb_insert(node, root, dummy_rotate);
382 EXPORT_SYMBOL(rb_insert_color);
384 void rb_erase(struct rb_node *node, struct rb_root *root)
386 struct rb_node *rebalance;
387 rebalance = __rb_erase_augmented(node, root, &dummy_callbacks);
389 ____rb_erase_color(rebalance, root, dummy_rotate);
391 EXPORT_SYMBOL(rb_erase);
394 * Augmented rbtree manipulation functions.
396 * This instantiates the same __always_inline functions as in the non-augmented
397 * case, but this time with user-defined callbacks.
400 void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
401 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
403 __rb_insert(node, root, augment_rotate);
405 EXPORT_SYMBOL(__rb_insert_augmented);
408 * This function returns the first node (in sort order) of the tree.
410 struct rb_node *rb_first(const struct rb_root *root)
421 EXPORT_SYMBOL(rb_first);
423 struct rb_node *rb_last(const struct rb_root *root)
434 EXPORT_SYMBOL(rb_last);
436 struct rb_node *rb_next(const struct rb_node *node)
438 struct rb_node *parent;
440 if (RB_EMPTY_NODE(node))
444 * If we have a right-hand child, go down and then left as far
447 if (node->rb_right) {
448 node = node->rb_right;
449 while (node->rb_left)
451 return (struct rb_node *)node;
455 * No right-hand children. Everything down and left is smaller than us,
456 * so any 'next' node must be in the general direction of our parent.
457 * Go up the tree; any time the ancestor is a right-hand child of its
458 * parent, keep going up. First time it's a left-hand child of its
459 * parent, said parent is our 'next' node.
461 while ((parent = rb_parent(node)) && node == parent->rb_right)
466 EXPORT_SYMBOL(rb_next);
468 struct rb_node *rb_prev(const struct rb_node *node)
470 struct rb_node *parent;
472 if (RB_EMPTY_NODE(node))
476 * If we have a left-hand child, go down and then right as far
480 node = node->rb_left;
481 while (node->rb_right)
483 return (struct rb_node *)node;
487 * No left-hand children. Go up till we find an ancestor which
488 * is a right-hand child of its parent.
490 while ((parent = rb_parent(node)) && node == parent->rb_left)
495 EXPORT_SYMBOL(rb_prev);
497 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
498 struct rb_root *root)
500 struct rb_node *parent = rb_parent(victim);
502 /* Set the surrounding nodes to point to the replacement */
503 __rb_change_child(victim, new, parent, root);
505 rb_set_parent(victim->rb_left, new);
506 if (victim->rb_right)
507 rb_set_parent(victim->rb_right, new);
509 /* Copy the pointers/colour from the victim to the replacement */
512 EXPORT_SYMBOL(rb_replace_node);
514 static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
518 node = node->rb_left;
519 else if (node->rb_right)
520 node = node->rb_right;
522 return (struct rb_node *)node;
526 struct rb_node *rb_next_postorder(const struct rb_node *node)
528 const struct rb_node *parent;
531 parent = rb_parent(node);
533 /* If we're sitting on node, we've already seen our children */
534 if (parent && node == parent->rb_left && parent->rb_right) {
535 /* If we are the parent's left node, go to the parent's right
536 * node then all the way down to the left */
537 return rb_left_deepest_node(parent->rb_right);
539 /* Otherwise we are the parent's right node, and the parent
541 return (struct rb_node *)parent;
543 EXPORT_SYMBOL(rb_next_postorder);
545 struct rb_node *rb_first_postorder(const struct rb_root *root)
550 return rb_left_deepest_node(root->rb_node);
552 EXPORT_SYMBOL(rb_first_postorder);