]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/rbtree.c
rbtree: handle 1-child recoloring in rb_erase() instead of rb_erase_color()
[mirror_ubuntu-artful-kernel.git] / lib / rbtree.c
CommitLineData
1da177e4
LT
1/*
2 Red Black Trees
3 (C) 1999 Andrea Arcangeli <andrea@suse.de>
4 (C) 2002 David Woodhouse <dwmw2@infradead.org>
46b6135a
ML
5 (C) 2012 Michel Lespinasse <walken@google.com>
6
1da177e4
LT
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.
11
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.
16
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
20
21 linux/lib/rbtree.c
22*/
23
24#include <linux/rbtree.h>
8bc3bcc9 25#include <linux/export.h>
1da177e4 26
5bc9188a
ML
27/*
28 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
29 *
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
35 * of black nodes.
36 *
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.
41 *
42 * We shall indicate color with case, where black nodes are uppercase and red
6280d235
ML
43 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
44 * parentheses and have some accompanying text comment.
5bc9188a
ML
45 */
46
bf7ad8ee
ML
47#define RB_RED 0
48#define RB_BLACK 1
49
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)
bf7ad8ee 53
46b6135a
ML
54static inline void rb_set_black(struct rb_node *rb)
55{
56 rb->__rb_parent_color |= RB_BLACK;
57}
58
bf7ad8ee
ML
59static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
60{
61 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
62}
bf7ad8ee 63
5bc9188a
ML
64static inline void rb_set_parent_color(struct rb_node *rb,
65 struct rb_node *p, int color)
66{
67 rb->__rb_parent_color = (unsigned long)p | color;
68}
69
70static inline struct rb_node *rb_red_parent(struct rb_node *red)
71{
72 return (struct rb_node *)red->__rb_parent_color;
73}
74
7abc704a
ML
75static inline void
76__rb_change_child(struct rb_node *old, struct rb_node *new,
77 struct rb_node *parent, struct rb_root *root)
78{
79 if (parent) {
80 if (parent->rb_left == old)
81 parent->rb_left = new;
82 else
83 parent->rb_right = new;
84 } else
85 root->rb_node = new;
86}
87
5bc9188a
ML
88/*
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.
92 */
93static inline void
94__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
95 struct rb_root *root, int color)
96{
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);
7abc704a 100 __rb_change_child(old, new, parent, root);
5bc9188a
ML
101}
102
1da177e4
LT
103void rb_insert_color(struct rb_node *node, struct rb_root *root)
104{
5bc9188a 105 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
1da177e4 106
6d58452d
ML
107 while (true) {
108 /*
109 * Loop invariant: node is red
110 *
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.
114 */
6d58452d 115 if (!parent) {
5bc9188a 116 rb_set_parent_color(node, NULL, RB_BLACK);
6d58452d
ML
117 break;
118 } else if (rb_is_black(parent))
119 break;
120
5bc9188a
ML
121 gparent = rb_red_parent(parent);
122
59633abf
ML
123 tmp = gparent->rb_right;
124 if (parent != tmp) { /* parent == gparent->rb_left */
5bc9188a
ML
125 if (tmp && rb_is_red(tmp)) {
126 /*
127 * Case 1 - color flips
128 *
129 * G g
130 * / \ / \
131 * p u --> P U
132 * / /
133 * n N
134 *
135 * However, since g's parent might be red, and
136 * 4) does not allow this, we need to recurse
137 * at g.
138 */
139 rb_set_parent_color(tmp, gparent, RB_BLACK);
140 rb_set_parent_color(parent, gparent, RB_BLACK);
141 node = gparent;
142 parent = rb_parent(node);
143 rb_set_parent_color(node, parent, RB_RED);
144 continue;
1da177e4
LT
145 }
146
59633abf
ML
147 tmp = parent->rb_right;
148 if (node == tmp) {
5bc9188a
ML
149 /*
150 * Case 2 - left rotate at parent
151 *
152 * G G
153 * / \ / \
154 * p U --> n U
155 * \ /
156 * n p
157 *
158 * This still leaves us in violation of 4), the
159 * continuation into Case 3 will fix that.
160 */
161 parent->rb_right = tmp = node->rb_left;
162 node->rb_left = parent;
163 if (tmp)
164 rb_set_parent_color(tmp, parent,
165 RB_BLACK);
166 rb_set_parent_color(parent, node, RB_RED);
1da177e4 167 parent = node;
59633abf 168 tmp = node->rb_right;
1da177e4
LT
169 }
170
5bc9188a
ML
171 /*
172 * Case 3 - right rotate at gparent
173 *
174 * G P
175 * / \ / \
176 * p U --> n g
177 * / \
178 * n U
179 */
59633abf 180 gparent->rb_left = tmp; /* == parent->rb_right */
5bc9188a
ML
181 parent->rb_right = gparent;
182 if (tmp)
183 rb_set_parent_color(tmp, gparent, RB_BLACK);
184 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
1f052865 185 break;
1da177e4 186 } else {
5bc9188a
ML
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);
192 node = gparent;
193 parent = rb_parent(node);
194 rb_set_parent_color(node, parent, RB_RED);
195 continue;
1da177e4
LT
196 }
197
59633abf
ML
198 tmp = parent->rb_left;
199 if (node == tmp) {
5bc9188a
ML
200 /* Case 2 - right rotate at parent */
201 parent->rb_left = tmp = node->rb_right;
202 node->rb_right = parent;
203 if (tmp)
204 rb_set_parent_color(tmp, parent,
205 RB_BLACK);
206 rb_set_parent_color(parent, node, RB_RED);
1da177e4 207 parent = node;
59633abf 208 tmp = node->rb_left;
1da177e4
LT
209 }
210
5bc9188a 211 /* Case 3 - left rotate at gparent */
59633abf 212 gparent->rb_right = tmp; /* == parent->rb_left */
5bc9188a
ML
213 parent->rb_left = gparent;
214 if (tmp)
215 rb_set_parent_color(tmp, gparent, RB_BLACK);
216 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
1f052865 217 break;
1da177e4
LT
218 }
219 }
1da177e4
LT
220}
221EXPORT_SYMBOL(rb_insert_color);
222
46b6135a 223static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
1da177e4 224{
46b6135a 225 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
1da177e4 226
d6ff1273
ML
227 while (true) {
228 /*
46b6135a
ML
229 * Loop invariants:
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.
d6ff1273 234 */
59633abf
ML
235 sibling = parent->rb_right;
236 if (node != sibling) { /* node == parent->rb_left */
6280d235
ML
237 if (rb_is_red(sibling)) {
238 /*
239 * Case 1 - left rotate at parent
240 *
241 * P S
242 * / \ / \
243 * N s --> p Sr
244 * / \ / \
245 * Sl Sr N Sl
246 */
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,
251 RB_RED);
252 sibling = tmp1;
1da177e4 253 }
6280d235
ML
254 tmp1 = sibling->rb_right;
255 if (!tmp1 || rb_is_black(tmp1)) {
256 tmp2 = sibling->rb_left;
257 if (!tmp2 || rb_is_black(tmp2)) {
258 /*
259 * Case 2 - sibling color flip
260 * (p could be either color here)
261 *
262 * (p) (p)
263 * / \ / \
264 * N S --> N s
265 * / \ / \
266 * Sl Sr Sl Sr
267 *
46b6135a
ML
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.
6280d235
ML
272 */
273 rb_set_parent_color(sibling, parent,
274 RB_RED);
46b6135a
ML
275 if (rb_is_red(parent))
276 rb_set_black(parent);
277 else {
278 node = parent;
279 parent = rb_parent(node);
280 if (parent)
281 continue;
282 }
283 break;
1da177e4 284 }
6280d235
ML
285 /*
286 * Case 3 - right rotate at sibling
287 * (p could be either color here)
288 *
289 * (p) (p)
290 * / \ / \
291 * N S --> N Sl
292 * / \ \
293 * sl Sr s
294 * \
295 * Sr
296 */
297 sibling->rb_left = tmp1 = tmp2->rb_right;
298 tmp2->rb_right = sibling;
299 parent->rb_right = tmp2;
300 if (tmp1)
301 rb_set_parent_color(tmp1, sibling,
302 RB_BLACK);
303 tmp1 = sibling;
304 sibling = tmp2;
1da177e4 305 }
6280d235
ML
306 /*
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)
311 *
312 * (p) (s)
313 * / \ / \
314 * N S --> P Sr
315 * / \ / \
316 * (sl) sr N (sl)
317 */
318 parent->rb_right = tmp2 = sibling->rb_left;
319 sibling->rb_left = parent;
320 rb_set_parent_color(tmp1, sibling, RB_BLACK);
321 if (tmp2)
322 rb_set_parent(tmp2, parent);
323 __rb_rotate_set_parents(parent, sibling, root,
324 RB_BLACK);
e125d147 325 break;
d6ff1273 326 } else {
6280d235
ML
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,
334 RB_RED);
335 sibling = tmp1;
1da177e4 336 }
6280d235
ML
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,
343 RB_RED);
46b6135a
ML
344 if (rb_is_red(parent))
345 rb_set_black(parent);
346 else {
347 node = parent;
348 parent = rb_parent(node);
349 if (parent)
350 continue;
351 }
352 break;
1da177e4 353 }
6280d235
ML
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;
358 if (tmp1)
359 rb_set_parent_color(tmp1, sibling,
360 RB_BLACK);
361 tmp1 = sibling;
362 sibling = tmp2;
1da177e4 363 }
6280d235
ML
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);
368 if (tmp2)
369 rb_set_parent(tmp2, parent);
370 __rb_rotate_set_parents(parent, sibling, root,
371 RB_BLACK);
e125d147 372 break;
1da177e4
LT
373 }
374 }
1da177e4
LT
375}
376
377void rb_erase(struct rb_node *node, struct rb_root *root)
378{
60670b80 379 struct rb_node *child = node->rb_right, *tmp = node->rb_left;
46b6135a 380 struct rb_node *parent, *rebalance;
1da177e4 381
60670b80 382 if (!tmp) {
46b6135a
ML
383 /*
384 * Case 1: node to erase has no more than 1 child (easy!)
385 *
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.
389 */
60670b80
ML
390
391 parent = rb_parent(node);
60670b80 392 __rb_change_child(node, child, parent, root);
46b6135a
ML
393 if (child) {
394 rb_set_parent_color(child, parent, RB_BLACK);
395 rebalance = NULL;
396 } else {
397 rebalance = rb_is_black(node) ? parent : NULL;
398 }
60670b80
ML
399 } else if (!child) {
400 /* Still case 1, but this time the child is node->rb_left */
46b6135a
ML
401 parent = rb_parent(node);
402 __rb_change_child(node, tmp, parent, root);
403 rb_set_parent_color(tmp, parent, RB_BLACK);
404 rebalance = NULL;
60670b80 405 } else {
1da177e4
LT
406 struct rb_node *old = node, *left;
407
60670b80 408 node = child;
1da177e4
LT
409 while ((left = node->rb_left) != NULL)
410 node = left;
16c047ad 411
7abc704a 412 __rb_change_child(old, node, rb_parent(old), root);
16c047ad 413
1da177e4 414 child = node->rb_right;
55a98102 415 parent = rb_parent(node);
1da177e4 416
55a98102 417 if (parent == old) {
1da177e4 418 parent = node;
4c601178 419 } else {
1975e593 420 parent->rb_left = child;
4b324126
WS
421
422 node->rb_right = old->rb_right;
423 rb_set_parent(old->rb_right, node);
4c601178 424 }
1975e593 425
46b6135a
ML
426 if (child) {
427 rb_set_parent_color(child, parent, RB_BLACK);
428 rebalance = NULL;
429 } else {
430 rebalance = rb_is_black(node) ? parent : NULL;
431 }
bf7ad8ee 432 node->__rb_parent_color = old->__rb_parent_color;
1da177e4 433 node->rb_left = old->rb_left;
55a98102 434 rb_set_parent(old->rb_left, node);
1da177e4
LT
435 }
436
46b6135a
ML
437 if (rebalance)
438 __rb_erase_color(rebalance, root);
1da177e4
LT
439}
440EXPORT_SYMBOL(rb_erase);
441
b945d6b2
PZ
442static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
443{
444 struct rb_node *parent;
445
446up:
447 func(node, data);
448 parent = rb_parent(node);
449 if (!parent)
450 return;
451
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);
456
457 node = parent;
458 goto up;
459}
460
461/*
462 * after inserting @node into the tree, update the tree to account for
463 * both the new entry and any damage done by rebalance
464 */
465void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
466{
467 if (node->rb_left)
468 node = node->rb_left;
469 else if (node->rb_right)
470 node = node->rb_right;
471
472 rb_augment_path(node, func, data);
473}
0b6bb66d 474EXPORT_SYMBOL(rb_augment_insert);
b945d6b2
PZ
475
476/*
477 * before removing the node, find the deepest node on the rebalance path
478 * that will still be there after @node gets removed
479 */
480struct rb_node *rb_augment_erase_begin(struct rb_node *node)
481{
482 struct rb_node *deepest;
483
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;
490 else {
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);
496 }
497
498 return deepest;
499}
0b6bb66d 500EXPORT_SYMBOL(rb_augment_erase_begin);
b945d6b2
PZ
501
502/*
503 * after removal, update the tree to account for the removed entry
504 * and any rebalance damage.
505 */
506void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
507{
508 if (node)
509 rb_augment_path(node, func, data);
510}
0b6bb66d 511EXPORT_SYMBOL(rb_augment_erase_end);
b945d6b2 512
1da177e4
LT
513/*
514 * This function returns the first node (in sort order) of the tree.
515 */
f4b477c4 516struct rb_node *rb_first(const struct rb_root *root)
1da177e4
LT
517{
518 struct rb_node *n;
519
520 n = root->rb_node;
521 if (!n)
522 return NULL;
523 while (n->rb_left)
524 n = n->rb_left;
525 return n;
526}
527EXPORT_SYMBOL(rb_first);
528
f4b477c4 529struct rb_node *rb_last(const struct rb_root *root)
1da177e4
LT
530{
531 struct rb_node *n;
532
533 n = root->rb_node;
534 if (!n)
535 return NULL;
536 while (n->rb_right)
537 n = n->rb_right;
538 return n;
539}
540EXPORT_SYMBOL(rb_last);
541
f4b477c4 542struct rb_node *rb_next(const struct rb_node *node)
1da177e4 543{
55a98102
DW
544 struct rb_node *parent;
545
4c199a93 546 if (RB_EMPTY_NODE(node))
10fd48f2
JA
547 return NULL;
548
7ce6ff9e
ML
549 /*
550 * If we have a right-hand child, go down and then left as far
551 * as we can.
552 */
1da177e4
LT
553 if (node->rb_right) {
554 node = node->rb_right;
555 while (node->rb_left)
556 node=node->rb_left;
f4b477c4 557 return (struct rb_node *)node;
1da177e4
LT
558 }
559
7ce6ff9e
ML
560 /*
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.
566 */
55a98102
DW
567 while ((parent = rb_parent(node)) && node == parent->rb_right)
568 node = parent;
1da177e4 569
55a98102 570 return parent;
1da177e4
LT
571}
572EXPORT_SYMBOL(rb_next);
573
f4b477c4 574struct rb_node *rb_prev(const struct rb_node *node)
1da177e4 575{
55a98102
DW
576 struct rb_node *parent;
577
4c199a93 578 if (RB_EMPTY_NODE(node))
10fd48f2
JA
579 return NULL;
580
7ce6ff9e
ML
581 /*
582 * If we have a left-hand child, go down and then right as far
583 * as we can.
584 */
1da177e4
LT
585 if (node->rb_left) {
586 node = node->rb_left;
587 while (node->rb_right)
588 node=node->rb_right;
f4b477c4 589 return (struct rb_node *)node;
1da177e4
LT
590 }
591
7ce6ff9e
ML
592 /*
593 * No left-hand children. Go up till we find an ancestor which
594 * is a right-hand child of its parent.
595 */
55a98102
DW
596 while ((parent = rb_parent(node)) && node == parent->rb_left)
597 node = parent;
1da177e4 598
55a98102 599 return parent;
1da177e4
LT
600}
601EXPORT_SYMBOL(rb_prev);
602
603void rb_replace_node(struct rb_node *victim, struct rb_node *new,
604 struct rb_root *root)
605{
55a98102 606 struct rb_node *parent = rb_parent(victim);
1da177e4
LT
607
608 /* Set the surrounding nodes to point to the replacement */
7abc704a 609 __rb_change_child(victim, new, parent, root);
1da177e4 610 if (victim->rb_left)
55a98102 611 rb_set_parent(victim->rb_left, new);
1da177e4 612 if (victim->rb_right)
55a98102 613 rb_set_parent(victim->rb_right, new);
1da177e4
LT
614
615 /* Copy the pointers/colour from the victim to the replacement */
616 *new = *victim;
617}
618EXPORT_SYMBOL(rb_replace_node);