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 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/rbtree.h>
25 #include <linux/export.h>
28 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
30 * 1) A node is either red or black
31 * 2) The root is black
32 * 3) All leaves (NULL) are black
33 * 4) Both children of every red node are black
34 * 5) Every simple path from root to leaves contains the same number
37 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
38 * consecutive red nodes in a path and every red node is therefore followed by
39 * a black. So if B is the number of black nodes on every simple path (as per
40 * 5), then the longest possible path due to 4 is 2B.
42 * We shall indicate color with case, where black nodes are uppercase and red
43 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
44 * parentheses and have some accompanying text comment.
50 #define rb_color(r) ((r)->__rb_parent_color & 1)
51 #define rb_is_red(r) (!rb_color(r))
52 #define rb_is_black(r) rb_color(r)
54 static inline void rb_set_black(struct rb_node *rb)
56 rb->__rb_parent_color |= RB_BLACK;
59 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
61 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
64 static inline void rb_set_parent_color(struct rb_node *rb,
65 struct rb_node *p, int color)
67 rb->__rb_parent_color = (unsigned long)p | color;
70 static inline struct rb_node *rb_red_parent(struct rb_node *red)
72 return (struct rb_node *)red->__rb_parent_color;
76 __rb_change_child(struct rb_node *old, struct rb_node *new,
77 struct rb_node *parent, struct rb_root *root)
80 if (parent->rb_left == old)
81 parent->rb_left = new;
83 parent->rb_right = new;
89 * Helper function for rotations:
90 * - old's parent and color get assigned to new
91 * - old gets assigned new as a parent and 'color' as a color.
94 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
95 struct rb_root *root, int color)
97 struct rb_node *parent = rb_parent(old);
98 new->__rb_parent_color = old->__rb_parent_color;
99 rb_set_parent_color(old, new, color);
100 __rb_change_child(old, new, parent, root);
103 void rb_insert_color(struct rb_node *node, struct rb_root *root)
105 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
109 * Loop invariant: node is red
111 * If there is a black parent, we are done.
112 * Otherwise, take some corrective action as we don't
113 * want a red root or two consecutive red nodes.
116 rb_set_parent_color(node, NULL, RB_BLACK);
118 } else if (rb_is_black(parent))
121 gparent = rb_red_parent(parent);
123 tmp = gparent->rb_right;
124 if (parent != tmp) { /* parent == gparent->rb_left */
125 if (tmp && rb_is_red(tmp)) {
127 * Case 1 - color flips
135 * However, since g's parent might be red, and
136 * 4) does not allow this, we need to recurse
139 rb_set_parent_color(tmp, gparent, RB_BLACK);
140 rb_set_parent_color(parent, gparent, RB_BLACK);
142 parent = rb_parent(node);
143 rb_set_parent_color(node, parent, RB_RED);
147 tmp = parent->rb_right;
150 * Case 2 - left rotate at parent
158 * This still leaves us in violation of 4), the
159 * continuation into Case 3 will fix that.
161 parent->rb_right = tmp = node->rb_left;
162 node->rb_left = parent;
164 rb_set_parent_color(tmp, parent,
166 rb_set_parent_color(parent, node, RB_RED);
168 tmp = node->rb_right;
172 * Case 3 - right rotate at gparent
180 gparent->rb_left = tmp; /* == parent->rb_right */
181 parent->rb_right = gparent;
183 rb_set_parent_color(tmp, gparent, RB_BLACK);
184 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
187 tmp = gparent->rb_left;
188 if (tmp && rb_is_red(tmp)) {
189 /* Case 1 - color flips */
190 rb_set_parent_color(tmp, gparent, RB_BLACK);
191 rb_set_parent_color(parent, gparent, RB_BLACK);
193 parent = rb_parent(node);
194 rb_set_parent_color(node, parent, RB_RED);
198 tmp = parent->rb_left;
200 /* Case 2 - right rotate at parent */
201 parent->rb_left = tmp = node->rb_right;
202 node->rb_right = parent;
204 rb_set_parent_color(tmp, parent,
206 rb_set_parent_color(parent, node, RB_RED);
211 /* Case 3 - left rotate at gparent */
212 gparent->rb_right = tmp; /* == parent->rb_left */
213 parent->rb_left = gparent;
215 rb_set_parent_color(tmp, gparent, RB_BLACK);
216 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
221 EXPORT_SYMBOL(rb_insert_color);
223 static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
225 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
230 * - node is black (or NULL on first iteration)
231 * - node is not the root (parent is not NULL)
232 * - All leaf paths going through parent and node have a
233 * black node count that is 1 lower than other leaf paths.
235 sibling = parent->rb_right;
236 if (node != sibling) { /* node == parent->rb_left */
237 if (rb_is_red(sibling)) {
239 * Case 1 - left rotate at parent
247 parent->rb_right = tmp1 = sibling->rb_left;
248 sibling->rb_left = parent;
249 rb_set_parent_color(tmp1, parent, RB_BLACK);
250 __rb_rotate_set_parents(parent, sibling, root,
254 tmp1 = sibling->rb_right;
255 if (!tmp1 || rb_is_black(tmp1)) {
256 tmp2 = sibling->rb_left;
257 if (!tmp2 || rb_is_black(tmp2)) {
259 * Case 2 - sibling color flip
260 * (p could be either color here)
268 * This leaves us violating 5) which
269 * can be fixed by flipping p to black
270 * if it was red, or by recursing at p.
271 * p is red when coming from Case 1.
273 rb_set_parent_color(sibling, parent,
275 if (rb_is_red(parent))
276 rb_set_black(parent);
279 parent = rb_parent(node);
286 * Case 3 - right rotate at sibling
287 * (p could be either color here)
297 sibling->rb_left = tmp1 = tmp2->rb_right;
298 tmp2->rb_right = sibling;
299 parent->rb_right = tmp2;
301 rb_set_parent_color(tmp1, sibling,
307 * Case 4 - left rotate at parent + color flips
308 * (p and sl could be either color here.
309 * After rotation, p becomes black, s acquires
310 * p's color, and sl keeps its color)
318 parent->rb_right = tmp2 = sibling->rb_left;
319 sibling->rb_left = parent;
320 rb_set_parent_color(tmp1, sibling, RB_BLACK);
322 rb_set_parent(tmp2, parent);
323 __rb_rotate_set_parents(parent, sibling, root,
327 sibling = parent->rb_left;
328 if (rb_is_red(sibling)) {
329 /* Case 1 - right rotate at parent */
330 parent->rb_left = tmp1 = sibling->rb_right;
331 sibling->rb_right = parent;
332 rb_set_parent_color(tmp1, parent, RB_BLACK);
333 __rb_rotate_set_parents(parent, sibling, root,
337 tmp1 = sibling->rb_left;
338 if (!tmp1 || rb_is_black(tmp1)) {
339 tmp2 = sibling->rb_right;
340 if (!tmp2 || rb_is_black(tmp2)) {
341 /* Case 2 - sibling color flip */
342 rb_set_parent_color(sibling, parent,
344 if (rb_is_red(parent))
345 rb_set_black(parent);
348 parent = rb_parent(node);
354 /* Case 3 - right rotate at sibling */
355 sibling->rb_right = tmp1 = tmp2->rb_left;
356 tmp2->rb_left = sibling;
357 parent->rb_left = tmp2;
359 rb_set_parent_color(tmp1, sibling,
364 /* Case 4 - left rotate at parent + color flips */
365 parent->rb_left = tmp2 = sibling->rb_right;
366 sibling->rb_right = parent;
367 rb_set_parent_color(tmp1, sibling, RB_BLACK);
369 rb_set_parent(tmp2, parent);
370 __rb_rotate_set_parents(parent, sibling, root,
377 void rb_erase(struct rb_node *node, struct rb_root *root)
379 struct rb_node *child = node->rb_right, *tmp = node->rb_left;
380 struct rb_node *parent, *rebalance;
384 * Case 1: node to erase has no more than 1 child (easy!)
386 * Note that if there is one child it must be red due to 5)
387 * and node must be black due to 4). We adjust colors locally
388 * so as to bypass __rb_erase_color() later on.
391 parent = rb_parent(node);
392 __rb_change_child(node, child, parent, root);
394 rb_set_parent_color(child, parent, RB_BLACK);
397 rebalance = rb_is_black(node) ? parent : NULL;
400 /* Still case 1, but this time the child is node->rb_left */
401 parent = rb_parent(node);
402 __rb_change_child(node, tmp, parent, root);
403 rb_set_parent_color(tmp, parent, RB_BLACK);
406 struct rb_node *old = node, *left;
409 while ((left = node->rb_left) != NULL)
412 __rb_change_child(old, node, rb_parent(old), root);
414 child = node->rb_right;
415 parent = rb_parent(node);
420 parent->rb_left = child;
422 node->rb_right = old->rb_right;
423 rb_set_parent(old->rb_right, node);
427 rb_set_parent_color(child, parent, RB_BLACK);
430 rebalance = rb_is_black(node) ? parent : NULL;
432 node->__rb_parent_color = old->__rb_parent_color;
433 node->rb_left = old->rb_left;
434 rb_set_parent(old->rb_left, node);
438 __rb_erase_color(rebalance, root);
440 EXPORT_SYMBOL(rb_erase);
442 static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
444 struct rb_node *parent;
448 parent = rb_parent(node);
452 if (node == parent->rb_left && parent->rb_right)
453 func(parent->rb_right, data);
454 else if (parent->rb_left)
455 func(parent->rb_left, data);
462 * after inserting @node into the tree, update the tree to account for
463 * both the new entry and any damage done by rebalance
465 void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
468 node = node->rb_left;
469 else if (node->rb_right)
470 node = node->rb_right;
472 rb_augment_path(node, func, data);
474 EXPORT_SYMBOL(rb_augment_insert);
477 * before removing the node, find the deepest node on the rebalance path
478 * that will still be there after @node gets removed
480 struct rb_node *rb_augment_erase_begin(struct rb_node *node)
482 struct rb_node *deepest;
484 if (!node->rb_right && !node->rb_left)
485 deepest = rb_parent(node);
486 else if (!node->rb_right)
487 deepest = node->rb_left;
488 else if (!node->rb_left)
489 deepest = node->rb_right;
491 deepest = rb_next(node);
492 if (deepest->rb_right)
493 deepest = deepest->rb_right;
494 else if (rb_parent(deepest) != node)
495 deepest = rb_parent(deepest);
500 EXPORT_SYMBOL(rb_augment_erase_begin);
503 * after removal, update the tree to account for the removed entry
504 * and any rebalance damage.
506 void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
509 rb_augment_path(node, func, data);
511 EXPORT_SYMBOL(rb_augment_erase_end);
514 * This function returns the first node (in sort order) of the tree.
516 struct rb_node *rb_first(const struct rb_root *root)
527 EXPORT_SYMBOL(rb_first);
529 struct rb_node *rb_last(const struct rb_root *root)
540 EXPORT_SYMBOL(rb_last);
542 struct rb_node *rb_next(const struct rb_node *node)
544 struct rb_node *parent;
546 if (RB_EMPTY_NODE(node))
550 * If we have a right-hand child, go down and then left as far
553 if (node->rb_right) {
554 node = node->rb_right;
555 while (node->rb_left)
557 return (struct rb_node *)node;
561 * No right-hand children. Everything down and left is smaller than us,
562 * so any 'next' node must be in the general direction of our parent.
563 * Go up the tree; any time the ancestor is a right-hand child of its
564 * parent, keep going up. First time it's a left-hand child of its
565 * parent, said parent is our 'next' node.
567 while ((parent = rb_parent(node)) && node == parent->rb_right)
572 EXPORT_SYMBOL(rb_next);
574 struct rb_node *rb_prev(const struct rb_node *node)
576 struct rb_node *parent;
578 if (RB_EMPTY_NODE(node))
582 * If we have a left-hand child, go down and then right as far
586 node = node->rb_left;
587 while (node->rb_right)
589 return (struct rb_node *)node;
593 * No left-hand children. Go up till we find an ancestor which
594 * is a right-hand child of its parent.
596 while ((parent = rb_parent(node)) && node == parent->rb_left)
601 EXPORT_SYMBOL(rb_prev);
603 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
604 struct rb_root *root)
606 struct rb_node *parent = rb_parent(victim);
608 /* Set the surrounding nodes to point to the replacement */
609 __rb_change_child(victim, new, parent, root);
611 rb_set_parent(victim->rb_left, new);
612 if (victim->rb_right)
613 rb_set_parent(victim->rb_right, new);
615 /* Copy the pointers/colour from the victim to the replacement */
618 EXPORT_SYMBOL(rb_replace_node);