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>
29 #define rb_color(r) ((r)->__rb_parent_color & 1)
30 #define rb_is_red(r) (!rb_color(r))
31 #define rb_is_black(r) rb_color(r)
32 #define rb_set_red(r) do { (r)->__rb_parent_color &= ~1; } while (0)
33 #define rb_set_black(r) do { (r)->__rb_parent_color |= 1; } while (0)
35 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
37 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
39 static inline void rb_set_color(struct rb_node *rb, int color)
41 rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color;
44 static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
46 struct rb_node *right = node->rb_right;
47 struct rb_node *parent = rb_parent(node);
49 if ((node->rb_right = right->rb_left))
50 rb_set_parent(right->rb_left, node);
51 right->rb_left = node;
53 rb_set_parent(right, parent);
57 if (node == parent->rb_left)
58 parent->rb_left = right;
60 parent->rb_right = right;
63 root->rb_node = right;
64 rb_set_parent(node, right);
67 static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
69 struct rb_node *left = node->rb_left;
70 struct rb_node *parent = rb_parent(node);
72 if ((node->rb_left = left->rb_right))
73 rb_set_parent(left->rb_right, node);
74 left->rb_right = node;
76 rb_set_parent(left, parent);
80 if (node == parent->rb_right)
81 parent->rb_right = left;
83 parent->rb_left = left;
87 rb_set_parent(node, left);
90 void rb_insert_color(struct rb_node *node, struct rb_root *root)
92 struct rb_node *parent, *gparent;
94 while ((parent = rb_parent(node)) && rb_is_red(parent))
96 gparent = rb_parent(parent);
98 if (parent == gparent->rb_left)
101 register struct rb_node *uncle = gparent->rb_right;
102 if (uncle && rb_is_red(uncle))
105 rb_set_black(parent);
112 if (parent->rb_right == node) {
113 __rb_rotate_left(parent, root);
117 rb_set_black(parent);
119 __rb_rotate_right(gparent, root);
123 register struct rb_node *uncle = gparent->rb_left;
124 if (uncle && rb_is_red(uncle))
127 rb_set_black(parent);
134 if (parent->rb_left == node) {
135 __rb_rotate_right(parent, root);
139 rb_set_black(parent);
141 __rb_rotate_left(gparent, root);
146 rb_set_black(root->rb_node);
148 EXPORT_SYMBOL(rb_insert_color);
150 static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
151 struct rb_root *root)
153 struct rb_node *other;
155 while ((!node || rb_is_black(node)) && node != root->rb_node)
157 if (parent->rb_left == node)
159 other = parent->rb_right;
160 if (rb_is_red(other))
164 __rb_rotate_left(parent, root);
165 other = parent->rb_right;
167 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
168 (!other->rb_right || rb_is_black(other->rb_right)))
172 parent = rb_parent(node);
176 if (!other->rb_right || rb_is_black(other->rb_right))
178 rb_set_black(other->rb_left);
180 __rb_rotate_right(other, root);
181 other = parent->rb_right;
183 rb_set_color(other, rb_color(parent));
184 rb_set_black(parent);
185 rb_set_black(other->rb_right);
186 __rb_rotate_left(parent, root);
187 node = root->rb_node;
193 other = parent->rb_left;
194 if (rb_is_red(other))
198 __rb_rotate_right(parent, root);
199 other = parent->rb_left;
201 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
202 (!other->rb_right || rb_is_black(other->rb_right)))
206 parent = rb_parent(node);
210 if (!other->rb_left || rb_is_black(other->rb_left))
212 rb_set_black(other->rb_right);
214 __rb_rotate_left(other, root);
215 other = parent->rb_left;
217 rb_set_color(other, rb_color(parent));
218 rb_set_black(parent);
219 rb_set_black(other->rb_left);
220 __rb_rotate_right(parent, root);
221 node = root->rb_node;
230 void rb_erase(struct rb_node *node, struct rb_root *root)
232 struct rb_node *child, *parent;
236 child = node->rb_right;
237 else if (!node->rb_right)
238 child = node->rb_left;
241 struct rb_node *old = node, *left;
243 node = node->rb_right;
244 while ((left = node->rb_left) != NULL)
247 if (rb_parent(old)) {
248 if (rb_parent(old)->rb_left == old)
249 rb_parent(old)->rb_left = node;
251 rb_parent(old)->rb_right = node;
253 root->rb_node = node;
255 child = node->rb_right;
256 parent = rb_parent(node);
257 color = rb_color(node);
263 rb_set_parent(child, parent);
264 parent->rb_left = child;
266 node->rb_right = old->rb_right;
267 rb_set_parent(old->rb_right, node);
270 node->__rb_parent_color = old->__rb_parent_color;
271 node->rb_left = old->rb_left;
272 rb_set_parent(old->rb_left, node);
277 parent = rb_parent(node);
278 color = rb_color(node);
281 rb_set_parent(child, parent);
284 if (parent->rb_left == node)
285 parent->rb_left = child;
287 parent->rb_right = child;
290 root->rb_node = child;
293 if (color == RB_BLACK)
294 __rb_erase_color(child, parent, root);
296 EXPORT_SYMBOL(rb_erase);
298 static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
300 struct rb_node *parent;
304 parent = rb_parent(node);
308 if (node == parent->rb_left && parent->rb_right)
309 func(parent->rb_right, data);
310 else if (parent->rb_left)
311 func(parent->rb_left, data);
318 * after inserting @node into the tree, update the tree to account for
319 * both the new entry and any damage done by rebalance
321 void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
324 node = node->rb_left;
325 else if (node->rb_right)
326 node = node->rb_right;
328 rb_augment_path(node, func, data);
330 EXPORT_SYMBOL(rb_augment_insert);
333 * before removing the node, find the deepest node on the rebalance path
334 * that will still be there after @node gets removed
336 struct rb_node *rb_augment_erase_begin(struct rb_node *node)
338 struct rb_node *deepest;
340 if (!node->rb_right && !node->rb_left)
341 deepest = rb_parent(node);
342 else if (!node->rb_right)
343 deepest = node->rb_left;
344 else if (!node->rb_left)
345 deepest = node->rb_right;
347 deepest = rb_next(node);
348 if (deepest->rb_right)
349 deepest = deepest->rb_right;
350 else if (rb_parent(deepest) != node)
351 deepest = rb_parent(deepest);
356 EXPORT_SYMBOL(rb_augment_erase_begin);
359 * after removal, update the tree to account for the removed entry
360 * and any rebalance damage.
362 void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
365 rb_augment_path(node, func, data);
367 EXPORT_SYMBOL(rb_augment_erase_end);
370 * This function returns the first node (in sort order) of the tree.
372 struct rb_node *rb_first(const struct rb_root *root)
383 EXPORT_SYMBOL(rb_first);
385 struct rb_node *rb_last(const struct rb_root *root)
396 EXPORT_SYMBOL(rb_last);
398 struct rb_node *rb_next(const struct rb_node *node)
400 struct rb_node *parent;
402 if (RB_EMPTY_NODE(node))
405 /* If we have a right-hand child, go down and then left as far
407 if (node->rb_right) {
408 node = node->rb_right;
409 while (node->rb_left)
411 return (struct rb_node *)node;
414 /* No right-hand children. Everything down and left is
415 smaller than us, so any 'next' node must be in the general
416 direction of our parent. Go up the tree; any time the
417 ancestor is a right-hand child of its parent, keep going
418 up. First time it's a left-hand child of its parent, said
419 parent is our 'next' node. */
420 while ((parent = rb_parent(node)) && node == parent->rb_right)
425 EXPORT_SYMBOL(rb_next);
427 struct rb_node *rb_prev(const struct rb_node *node)
429 struct rb_node *parent;
431 if (RB_EMPTY_NODE(node))
434 /* If we have a left-hand child, go down and then right as far
437 node = node->rb_left;
438 while (node->rb_right)
440 return (struct rb_node *)node;
443 /* No left-hand children. Go up till we find an ancestor which
444 is a right-hand child of its parent */
445 while ((parent = rb_parent(node)) && node == parent->rb_left)
450 EXPORT_SYMBOL(rb_prev);
452 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
453 struct rb_root *root)
455 struct rb_node *parent = rb_parent(victim);
457 /* Set the surrounding nodes to point to the replacement */
459 if (victim == parent->rb_left)
460 parent->rb_left = new;
462 parent->rb_right = new;
467 rb_set_parent(victim->rb_left, new);
468 if (victim->rb_right)
469 rb_set_parent(victim->rb_right, new);
471 /* Copy the pointers/colour from the victim to the replacement */
474 EXPORT_SYMBOL(rb_replace_node);