3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/rbtree.h>
24 #include <linux/export.h>
27 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
29 * 1) A node is either red or black
30 * 2) The root is black
31 * 3) All leaves (NULL) are black
32 * 4) Both children of every red node are black
33 * 5) Every simple path from root to leaves contains the same number
36 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
37 * consecutive red nodes in a path and every red node is therefore followed by
38 * a black. So if B is the number of black nodes on every simple path (as per
39 * 5), then the longest possible path due to 4 is 2B.
41 * We shall indicate color with case, where black nodes are uppercase and red
42 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
43 * parentheses and have some accompanying text comment.
49 #define rb_color(r) ((r)->__rb_parent_color & 1)
50 #define rb_is_red(r) (!rb_color(r))
51 #define rb_is_black(r) rb_color(r)
53 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
55 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
58 static inline void rb_set_parent_color(struct rb_node *rb,
59 struct rb_node *p, int color)
61 rb->__rb_parent_color = (unsigned long)p | color;
64 static inline struct rb_node *rb_red_parent(struct rb_node *red)
66 return (struct rb_node *)red->__rb_parent_color;
70 * Helper function for rotations:
71 * - old's parent and color get assigned to new
72 * - old gets assigned new as a parent and 'color' as a color.
75 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
76 struct rb_root *root, int color)
78 struct rb_node *parent = rb_parent(old);
79 new->__rb_parent_color = old->__rb_parent_color;
80 rb_set_parent_color(old, new, color);
82 if (parent->rb_left == old)
83 parent->rb_left = new;
85 parent->rb_right = new;
90 void rb_insert_color(struct rb_node *node, struct rb_root *root)
92 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
96 * Loop invariant: node is red
98 * If there is a black parent, we are done.
99 * Otherwise, take some corrective action as we don't
100 * want a red root or two consecutive red nodes.
103 rb_set_parent_color(node, NULL, RB_BLACK);
105 } else if (rb_is_black(parent))
108 gparent = rb_red_parent(parent);
110 if (parent == gparent->rb_left) {
111 tmp = gparent->rb_right;
112 if (tmp && rb_is_red(tmp)) {
114 * Case 1 - color flips
122 * However, since g's parent might be red, and
123 * 4) does not allow this, we need to recurse
126 rb_set_parent_color(tmp, gparent, RB_BLACK);
127 rb_set_parent_color(parent, gparent, RB_BLACK);
129 parent = rb_parent(node);
130 rb_set_parent_color(node, parent, RB_RED);
134 if (parent->rb_right == node) {
136 * Case 2 - left rotate at parent
144 * This still leaves us in violation of 4), the
145 * continuation into Case 3 will fix that.
147 parent->rb_right = tmp = node->rb_left;
148 node->rb_left = parent;
150 rb_set_parent_color(tmp, parent,
152 rb_set_parent_color(parent, node, RB_RED);
157 * Case 3 - right rotate at gparent
165 gparent->rb_left = tmp = parent->rb_right;
166 parent->rb_right = gparent;
168 rb_set_parent_color(tmp, gparent, RB_BLACK);
169 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
172 tmp = gparent->rb_left;
173 if (tmp && rb_is_red(tmp)) {
174 /* Case 1 - color flips */
175 rb_set_parent_color(tmp, gparent, RB_BLACK);
176 rb_set_parent_color(parent, gparent, RB_BLACK);
178 parent = rb_parent(node);
179 rb_set_parent_color(node, parent, RB_RED);
183 if (parent->rb_left == node) {
184 /* Case 2 - right rotate at parent */
185 parent->rb_left = tmp = node->rb_right;
186 node->rb_right = parent;
188 rb_set_parent_color(tmp, parent,
190 rb_set_parent_color(parent, node, RB_RED);
194 /* Case 3 - left rotate at gparent */
195 gparent->rb_right = tmp = parent->rb_left;
196 parent->rb_left = gparent;
198 rb_set_parent_color(tmp, gparent, RB_BLACK);
199 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
204 EXPORT_SYMBOL(rb_insert_color);
206 static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
207 struct rb_root *root)
209 struct rb_node *sibling, *tmp1, *tmp2;
213 * Loop invariant: all leaf paths going through node have a
214 * black node count that is 1 lower than other leaf paths.
216 * If node is red, we can flip it to black to adjust.
217 * If node is the root, all leaf paths go through it.
218 * Otherwise, we need to adjust the tree through color flips
219 * and tree rotations as per one of the 4 cases below.
221 if (node && rb_is_red(node)) {
222 rb_set_parent_color(node, parent, RB_BLACK);
224 } else if (!parent) {
226 } else if (parent->rb_left == node) {
227 sibling = parent->rb_right;
228 if (rb_is_red(sibling)) {
230 * Case 1 - left rotate at parent
238 parent->rb_right = tmp1 = sibling->rb_left;
239 sibling->rb_left = parent;
240 rb_set_parent_color(tmp1, parent, RB_BLACK);
241 __rb_rotate_set_parents(parent, sibling, root,
245 tmp1 = sibling->rb_right;
246 if (!tmp1 || rb_is_black(tmp1)) {
247 tmp2 = sibling->rb_left;
248 if (!tmp2 || rb_is_black(tmp2)) {
250 * Case 2 - sibling color flip
251 * (p could be either color here)
259 * This leaves us violating 5), so
260 * recurse at p. If p is red, the
261 * recursion will just flip it to black
262 * and exit. If coming from Case 1,
263 * p is known to be red.
265 rb_set_parent_color(sibling, parent,
268 parent = rb_parent(node);
272 * Case 3 - right rotate at sibling
273 * (p could be either color here)
283 sibling->rb_left = tmp1 = tmp2->rb_right;
284 tmp2->rb_right = sibling;
285 parent->rb_right = tmp2;
287 rb_set_parent_color(tmp1, sibling,
293 * Case 4 - left rotate at parent + color flips
294 * (p and sl could be either color here.
295 * After rotation, p becomes black, s acquires
296 * p's color, and sl keeps its color)
304 parent->rb_right = tmp2 = sibling->rb_left;
305 sibling->rb_left = parent;
306 rb_set_parent_color(tmp1, sibling, RB_BLACK);
308 rb_set_parent(tmp2, parent);
309 __rb_rotate_set_parents(parent, sibling, root,
313 sibling = parent->rb_left;
314 if (rb_is_red(sibling)) {
315 /* Case 1 - right rotate at parent */
316 parent->rb_left = tmp1 = sibling->rb_right;
317 sibling->rb_right = parent;
318 rb_set_parent_color(tmp1, parent, RB_BLACK);
319 __rb_rotate_set_parents(parent, sibling, root,
323 tmp1 = sibling->rb_left;
324 if (!tmp1 || rb_is_black(tmp1)) {
325 tmp2 = sibling->rb_right;
326 if (!tmp2 || rb_is_black(tmp2)) {
327 /* Case 2 - sibling color flip */
328 rb_set_parent_color(sibling, parent,
331 parent = rb_parent(node);
334 /* Case 3 - right rotate at sibling */
335 sibling->rb_right = tmp1 = tmp2->rb_left;
336 tmp2->rb_left = sibling;
337 parent->rb_left = tmp2;
339 rb_set_parent_color(tmp1, sibling,
344 /* Case 4 - left rotate at parent + color flips */
345 parent->rb_left = tmp2 = sibling->rb_right;
346 sibling->rb_right = parent;
347 rb_set_parent_color(tmp1, sibling, RB_BLACK);
349 rb_set_parent(tmp2, parent);
350 __rb_rotate_set_parents(parent, sibling, root,
357 void rb_erase(struct rb_node *node, struct rb_root *root)
359 struct rb_node *child, *parent;
363 child = node->rb_right;
364 else if (!node->rb_right)
365 child = node->rb_left;
368 struct rb_node *old = node, *left;
370 node = node->rb_right;
371 while ((left = node->rb_left) != NULL)
374 if (rb_parent(old)) {
375 if (rb_parent(old)->rb_left == old)
376 rb_parent(old)->rb_left = node;
378 rb_parent(old)->rb_right = node;
380 root->rb_node = node;
382 child = node->rb_right;
383 parent = rb_parent(node);
384 color = rb_color(node);
390 rb_set_parent(child, parent);
391 parent->rb_left = child;
393 node->rb_right = old->rb_right;
394 rb_set_parent(old->rb_right, node);
397 node->__rb_parent_color = old->__rb_parent_color;
398 node->rb_left = old->rb_left;
399 rb_set_parent(old->rb_left, node);
404 parent = rb_parent(node);
405 color = rb_color(node);
408 rb_set_parent(child, parent);
411 if (parent->rb_left == node)
412 parent->rb_left = child;
414 parent->rb_right = child;
417 root->rb_node = child;
420 if (color == RB_BLACK)
421 __rb_erase_color(child, parent, root);
423 EXPORT_SYMBOL(rb_erase);
425 static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
427 struct rb_node *parent;
431 parent = rb_parent(node);
435 if (node == parent->rb_left && parent->rb_right)
436 func(parent->rb_right, data);
437 else if (parent->rb_left)
438 func(parent->rb_left, data);
445 * after inserting @node into the tree, update the tree to account for
446 * both the new entry and any damage done by rebalance
448 void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
451 node = node->rb_left;
452 else if (node->rb_right)
453 node = node->rb_right;
455 rb_augment_path(node, func, data);
457 EXPORT_SYMBOL(rb_augment_insert);
460 * before removing the node, find the deepest node on the rebalance path
461 * that will still be there after @node gets removed
463 struct rb_node *rb_augment_erase_begin(struct rb_node *node)
465 struct rb_node *deepest;
467 if (!node->rb_right && !node->rb_left)
468 deepest = rb_parent(node);
469 else if (!node->rb_right)
470 deepest = node->rb_left;
471 else if (!node->rb_left)
472 deepest = node->rb_right;
474 deepest = rb_next(node);
475 if (deepest->rb_right)
476 deepest = deepest->rb_right;
477 else if (rb_parent(deepest) != node)
478 deepest = rb_parent(deepest);
483 EXPORT_SYMBOL(rb_augment_erase_begin);
486 * after removal, update the tree to account for the removed entry
487 * and any rebalance damage.
489 void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
492 rb_augment_path(node, func, data);
494 EXPORT_SYMBOL(rb_augment_erase_end);
497 * This function returns the first node (in sort order) of the tree.
499 struct rb_node *rb_first(const struct rb_root *root)
510 EXPORT_SYMBOL(rb_first);
512 struct rb_node *rb_last(const struct rb_root *root)
523 EXPORT_SYMBOL(rb_last);
525 struct rb_node *rb_next(const struct rb_node *node)
527 struct rb_node *parent;
529 if (RB_EMPTY_NODE(node))
532 /* If we have a right-hand child, go down and then left as far
534 if (node->rb_right) {
535 node = node->rb_right;
536 while (node->rb_left)
538 return (struct rb_node *)node;
541 /* No right-hand children. Everything down and left is
542 smaller than us, so any 'next' node must be in the general
543 direction of our parent. Go up the tree; any time the
544 ancestor is a right-hand child of its parent, keep going
545 up. First time it's a left-hand child of its parent, said
546 parent is our 'next' node. */
547 while ((parent = rb_parent(node)) && node == parent->rb_right)
552 EXPORT_SYMBOL(rb_next);
554 struct rb_node *rb_prev(const struct rb_node *node)
556 struct rb_node *parent;
558 if (RB_EMPTY_NODE(node))
561 /* If we have a left-hand child, go down and then right as far
564 node = node->rb_left;
565 while (node->rb_right)
567 return (struct rb_node *)node;
570 /* No left-hand children. Go up till we find an ancestor which
571 is a right-hand child of its parent */
572 while ((parent = rb_parent(node)) && node == parent->rb_left)
577 EXPORT_SYMBOL(rb_prev);
579 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
580 struct rb_root *root)
582 struct rb_node *parent = rb_parent(victim);
584 /* Set the surrounding nodes to point to the replacement */
586 if (victim == parent->rb_left)
587 parent->rb_left = new;
589 parent->rb_right = new;
594 rb_set_parent(victim->rb_left, new);
595 if (victim->rb_right)
596 rb_set_parent(victim->rb_right, new);
598 /* Copy the pointers/colour from the victim to the replacement */
601 EXPORT_SYMBOL(rb_replace_node);