rbtree: handle 1-child recoloring in rb_erase() instead of rb_erase_color()
[linux-2.6-microblaze.git] / lib / rbtree.c
1 /*
2   Red Black Trees
3   (C) 1999  Andrea Arcangeli <andrea@suse.de>
4   (C) 2002  David Woodhouse <dwmw2@infradead.org>
5   (C) 2012  Michel Lespinasse <walken@google.com>
6
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>
25 #include <linux/export.h>
26
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
43  *  nodes will be lowercase. Unknown color nodes shall be drawn as red within
44  *  parentheses and have some accompanying text comment.
45  */
46
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)
53
54 static inline void rb_set_black(struct rb_node *rb)
55 {
56         rb->__rb_parent_color |= RB_BLACK;
57 }
58
59 static 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 }
63
64 static 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
70 static inline struct rb_node *rb_red_parent(struct rb_node *red)
71 {
72         return (struct rb_node *)red->__rb_parent_color;
73 }
74
75 static 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
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  */
93 static 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);
100         __rb_change_child(old, new, parent, root);
101 }
102
103 void rb_insert_color(struct rb_node *node, struct rb_root *root)
104 {
105         struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
106
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                  */
115                 if (!parent) {
116                         rb_set_parent_color(node, NULL, RB_BLACK);
117                         break;
118                 } else if (rb_is_black(parent))
119                         break;
120
121                 gparent = rb_red_parent(parent);
122
123                 tmp = gparent->rb_right;
124                 if (parent != tmp) {    /* parent == gparent->rb_left */
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;
145                         }
146
147                         tmp = parent->rb_right;
148                         if (node == tmp) {
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);
167                                 parent = node;
168                                 tmp = node->rb_right;
169                         }
170
171                         /*
172                          * Case 3 - right rotate at gparent
173                          *
174                          *        G           P
175                          *       / \         / \
176                          *      p   U  -->  n   g
177                          *     /                 \
178                          *    n                   U
179                          */
180                         gparent->rb_left = tmp;  /* == parent->rb_right */
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);
185                         break;
186                 } else {
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;
196                         }
197
198                         tmp = parent->rb_left;
199                         if (node == tmp) {
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);
207                                 parent = node;
208                                 tmp = node->rb_left;
209                         }
210
211                         /* Case 3 - left rotate at gparent */
212                         gparent->rb_right = tmp;  /* == parent->rb_left */
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);
217                         break;
218                 }
219         }
220 }
221 EXPORT_SYMBOL(rb_insert_color);
222
223 static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
224 {
225         struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
226
227         while (true) {
228                 /*
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.
234                  */
235                 sibling = parent->rb_right;
236                 if (node != sibling) {  /* node == parent->rb_left */
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;
253                         }
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                                          *
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.
272                                          */
273                                         rb_set_parent_color(sibling, parent,
274                                                             RB_RED);
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;
284                                 }
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;
305                         }
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);
325                         break;
326                 } else {
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;
336                         }
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);
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;
353                                 }
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;
363                         }
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);
372                         break;
373                 }
374         }
375 }
376
377 void rb_erase(struct rb_node *node, struct rb_root *root)
378 {
379         struct rb_node *child = node->rb_right, *tmp = node->rb_left;
380         struct rb_node *parent, *rebalance;
381
382         if (!tmp) {
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                  */
390
391                 parent = rb_parent(node);
392                 __rb_change_child(node, child, parent, root);
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                 }
399         } else if (!child) {
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);
404                 rebalance = NULL;
405         } else {
406                 struct rb_node *old = node, *left;
407
408                 node = child;
409                 while ((left = node->rb_left) != NULL)
410                         node = left;
411
412                 __rb_change_child(old, node, rb_parent(old), root);
413
414                 child = node->rb_right;
415                 parent = rb_parent(node);
416
417                 if (parent == old) {
418                         parent = node;
419                 } else {
420                         parent->rb_left = child;
421
422                         node->rb_right = old->rb_right;
423                         rb_set_parent(old->rb_right, node);
424                 }
425
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                 }
432                 node->__rb_parent_color = old->__rb_parent_color;
433                 node->rb_left = old->rb_left;
434                 rb_set_parent(old->rb_left, node);
435         }
436
437         if (rebalance)
438                 __rb_erase_color(rebalance, root);
439 }
440 EXPORT_SYMBOL(rb_erase);
441
442 static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
443 {
444         struct rb_node *parent;
445
446 up:
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  */
465 void 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 }
474 EXPORT_SYMBOL(rb_augment_insert);
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  */
480 struct 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 }
500 EXPORT_SYMBOL(rb_augment_erase_begin);
501
502 /*
503  * after removal, update the tree to account for the removed entry
504  * and any rebalance damage.
505  */
506 void 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 }
511 EXPORT_SYMBOL(rb_augment_erase_end);
512
513 /*
514  * This function returns the first node (in sort order) of the tree.
515  */
516 struct rb_node *rb_first(const struct rb_root *root)
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 }
527 EXPORT_SYMBOL(rb_first);
528
529 struct rb_node *rb_last(const struct rb_root *root)
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 }
540 EXPORT_SYMBOL(rb_last);
541
542 struct rb_node *rb_next(const struct rb_node *node)
543 {
544         struct rb_node *parent;
545
546         if (RB_EMPTY_NODE(node))
547                 return NULL;
548
549         /*
550          * If we have a right-hand child, go down and then left as far
551          * as we can.
552          */
553         if (node->rb_right) {
554                 node = node->rb_right; 
555                 while (node->rb_left)
556                         node=node->rb_left;
557                 return (struct rb_node *)node;
558         }
559
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          */
567         while ((parent = rb_parent(node)) && node == parent->rb_right)
568                 node = parent;
569
570         return parent;
571 }
572 EXPORT_SYMBOL(rb_next);
573
574 struct rb_node *rb_prev(const struct rb_node *node)
575 {
576         struct rb_node *parent;
577
578         if (RB_EMPTY_NODE(node))
579                 return NULL;
580
581         /*
582          * If we have a left-hand child, go down and then right as far
583          * as we can.
584          */
585         if (node->rb_left) {
586                 node = node->rb_left; 
587                 while (node->rb_right)
588                         node=node->rb_right;
589                 return (struct rb_node *)node;
590         }
591
592         /*
593          * No left-hand children. Go up till we find an ancestor which
594          * is a right-hand child of its parent.
595          */
596         while ((parent = rb_parent(node)) && node == parent->rb_left)
597                 node = parent;
598
599         return parent;
600 }
601 EXPORT_SYMBOL(rb_prev);
602
603 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
604                      struct rb_root *root)
605 {
606         struct rb_node *parent = rb_parent(victim);
607
608         /* Set the surrounding nodes to point to the replacement */
609         __rb_change_child(victim, new, parent, root);
610         if (victim->rb_left)
611                 rb_set_parent(victim->rb_left, new);
612         if (victim->rb_right)
613                 rb_set_parent(victim->rb_right, new);
614
615         /* Copy the pointers/colour from the victim to the replacement */
616         *new = *victim;
617 }
618 EXPORT_SYMBOL(rb_replace_node);