rbtree: break out of rb_insert_color loop after tree rotation
[cascardo/linux.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   
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.
10
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.
15
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
19
20   linux/lib/rbtree.c
21 */
22
23 #include <linux/rbtree.h>
24 #include <linux/export.h>
25
26 #define RB_RED          0
27 #define RB_BLACK        1
28
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)
34
35 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
36 {
37         rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
38 }
39 static inline void rb_set_color(struct rb_node *rb, int color)
40 {
41         rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color;
42 }
43
44 static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
45 {
46         struct rb_node *right = node->rb_right;
47         struct rb_node *parent = rb_parent(node);
48
49         if ((node->rb_right = right->rb_left))
50                 rb_set_parent(right->rb_left, node);
51         right->rb_left = node;
52
53         rb_set_parent(right, parent);
54
55         if (parent)
56         {
57                 if (node == parent->rb_left)
58                         parent->rb_left = right;
59                 else
60                         parent->rb_right = right;
61         }
62         else
63                 root->rb_node = right;
64         rb_set_parent(node, right);
65 }
66
67 static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
68 {
69         struct rb_node *left = node->rb_left;
70         struct rb_node *parent = rb_parent(node);
71
72         if ((node->rb_left = left->rb_right))
73                 rb_set_parent(left->rb_right, node);
74         left->rb_right = node;
75
76         rb_set_parent(left, parent);
77
78         if (parent)
79         {
80                 if (node == parent->rb_right)
81                         parent->rb_right = left;
82                 else
83                         parent->rb_left = left;
84         }
85         else
86                 root->rb_node = left;
87         rb_set_parent(node, left);
88 }
89
90 void rb_insert_color(struct rb_node *node, struct rb_root *root)
91 {
92         struct rb_node *parent, *gparent;
93
94         while ((parent = rb_parent(node)) && rb_is_red(parent))
95         {
96                 gparent = rb_parent(parent);
97
98                 if (parent == gparent->rb_left)
99                 {
100                         {
101                                 register struct rb_node *uncle = gparent->rb_right;
102                                 if (uncle && rb_is_red(uncle))
103                                 {
104                                         rb_set_black(uncle);
105                                         rb_set_black(parent);
106                                         rb_set_red(gparent);
107                                         node = gparent;
108                                         continue;
109                                 }
110                         }
111
112                         if (parent->rb_right == node) {
113                                 __rb_rotate_left(parent, root);
114                                 parent = node;
115                         }
116
117                         rb_set_black(parent);
118                         rb_set_red(gparent);
119                         __rb_rotate_right(gparent, root);
120                         break;
121                 } else {
122                         {
123                                 register struct rb_node *uncle = gparent->rb_left;
124                                 if (uncle && rb_is_red(uncle))
125                                 {
126                                         rb_set_black(uncle);
127                                         rb_set_black(parent);
128                                         rb_set_red(gparent);
129                                         node = gparent;
130                                         continue;
131                                 }
132                         }
133
134                         if (parent->rb_left == node) {
135                                 __rb_rotate_right(parent, root);
136                                 parent = node;
137                         }
138
139                         rb_set_black(parent);
140                         rb_set_red(gparent);
141                         __rb_rotate_left(gparent, root);
142                         break;
143                 }
144         }
145
146         rb_set_black(root->rb_node);
147 }
148 EXPORT_SYMBOL(rb_insert_color);
149
150 static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
151                              struct rb_root *root)
152 {
153         struct rb_node *other;
154
155         while ((!node || rb_is_black(node)) && node != root->rb_node)
156         {
157                 if (parent->rb_left == node)
158                 {
159                         other = parent->rb_right;
160                         if (rb_is_red(other))
161                         {
162                                 rb_set_black(other);
163                                 rb_set_red(parent);
164                                 __rb_rotate_left(parent, root);
165                                 other = parent->rb_right;
166                         }
167                         if ((!other->rb_left || rb_is_black(other->rb_left)) &&
168                             (!other->rb_right || rb_is_black(other->rb_right)))
169                         {
170                                 rb_set_red(other);
171                                 node = parent;
172                                 parent = rb_parent(node);
173                         }
174                         else
175                         {
176                                 if (!other->rb_right || rb_is_black(other->rb_right))
177                                 {
178                                         rb_set_black(other->rb_left);
179                                         rb_set_red(other);
180                                         __rb_rotate_right(other, root);
181                                         other = parent->rb_right;
182                                 }
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;
188                                 break;
189                         }
190                 }
191                 else
192                 {
193                         other = parent->rb_left;
194                         if (rb_is_red(other))
195                         {
196                                 rb_set_black(other);
197                                 rb_set_red(parent);
198                                 __rb_rotate_right(parent, root);
199                                 other = parent->rb_left;
200                         }
201                         if ((!other->rb_left || rb_is_black(other->rb_left)) &&
202                             (!other->rb_right || rb_is_black(other->rb_right)))
203                         {
204                                 rb_set_red(other);
205                                 node = parent;
206                                 parent = rb_parent(node);
207                         }
208                         else
209                         {
210                                 if (!other->rb_left || rb_is_black(other->rb_left))
211                                 {
212                                         rb_set_black(other->rb_right);
213                                         rb_set_red(other);
214                                         __rb_rotate_left(other, root);
215                                         other = parent->rb_left;
216                                 }
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;
222                                 break;
223                         }
224                 }
225         }
226         if (node)
227                 rb_set_black(node);
228 }
229
230 void rb_erase(struct rb_node *node, struct rb_root *root)
231 {
232         struct rb_node *child, *parent;
233         int color;
234
235         if (!node->rb_left)
236                 child = node->rb_right;
237         else if (!node->rb_right)
238                 child = node->rb_left;
239         else
240         {
241                 struct rb_node *old = node, *left;
242
243                 node = node->rb_right;
244                 while ((left = node->rb_left) != NULL)
245                         node = left;
246
247                 if (rb_parent(old)) {
248                         if (rb_parent(old)->rb_left == old)
249                                 rb_parent(old)->rb_left = node;
250                         else
251                                 rb_parent(old)->rb_right = node;
252                 } else
253                         root->rb_node = node;
254
255                 child = node->rb_right;
256                 parent = rb_parent(node);
257                 color = rb_color(node);
258
259                 if (parent == old) {
260                         parent = node;
261                 } else {
262                         if (child)
263                                 rb_set_parent(child, parent);
264                         parent->rb_left = child;
265
266                         node->rb_right = old->rb_right;
267                         rb_set_parent(old->rb_right, node);
268                 }
269
270                 node->__rb_parent_color = old->__rb_parent_color;
271                 node->rb_left = old->rb_left;
272                 rb_set_parent(old->rb_left, node);
273
274                 goto color;
275         }
276
277         parent = rb_parent(node);
278         color = rb_color(node);
279
280         if (child)
281                 rb_set_parent(child, parent);
282         if (parent)
283         {
284                 if (parent->rb_left == node)
285                         parent->rb_left = child;
286                 else
287                         parent->rb_right = child;
288         }
289         else
290                 root->rb_node = child;
291
292  color:
293         if (color == RB_BLACK)
294                 __rb_erase_color(child, parent, root);
295 }
296 EXPORT_SYMBOL(rb_erase);
297
298 static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
299 {
300         struct rb_node *parent;
301
302 up:
303         func(node, data);
304         parent = rb_parent(node);
305         if (!parent)
306                 return;
307
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);
312
313         node = parent;
314         goto up;
315 }
316
317 /*
318  * after inserting @node into the tree, update the tree to account for
319  * both the new entry and any damage done by rebalance
320  */
321 void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
322 {
323         if (node->rb_left)
324                 node = node->rb_left;
325         else if (node->rb_right)
326                 node = node->rb_right;
327
328         rb_augment_path(node, func, data);
329 }
330 EXPORT_SYMBOL(rb_augment_insert);
331
332 /*
333  * before removing the node, find the deepest node on the rebalance path
334  * that will still be there after @node gets removed
335  */
336 struct rb_node *rb_augment_erase_begin(struct rb_node *node)
337 {
338         struct rb_node *deepest;
339
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;
346         else {
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);
352         }
353
354         return deepest;
355 }
356 EXPORT_SYMBOL(rb_augment_erase_begin);
357
358 /*
359  * after removal, update the tree to account for the removed entry
360  * and any rebalance damage.
361  */
362 void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
363 {
364         if (node)
365                 rb_augment_path(node, func, data);
366 }
367 EXPORT_SYMBOL(rb_augment_erase_end);
368
369 /*
370  * This function returns the first node (in sort order) of the tree.
371  */
372 struct rb_node *rb_first(const struct rb_root *root)
373 {
374         struct rb_node  *n;
375
376         n = root->rb_node;
377         if (!n)
378                 return NULL;
379         while (n->rb_left)
380                 n = n->rb_left;
381         return n;
382 }
383 EXPORT_SYMBOL(rb_first);
384
385 struct rb_node *rb_last(const struct rb_root *root)
386 {
387         struct rb_node  *n;
388
389         n = root->rb_node;
390         if (!n)
391                 return NULL;
392         while (n->rb_right)
393                 n = n->rb_right;
394         return n;
395 }
396 EXPORT_SYMBOL(rb_last);
397
398 struct rb_node *rb_next(const struct rb_node *node)
399 {
400         struct rb_node *parent;
401
402         if (RB_EMPTY_NODE(node))
403                 return NULL;
404
405         /* If we have a right-hand child, go down and then left as far
406            as we can. */
407         if (node->rb_right) {
408                 node = node->rb_right; 
409                 while (node->rb_left)
410                         node=node->rb_left;
411                 return (struct rb_node *)node;
412         }
413
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)
421                 node = parent;
422
423         return parent;
424 }
425 EXPORT_SYMBOL(rb_next);
426
427 struct rb_node *rb_prev(const struct rb_node *node)
428 {
429         struct rb_node *parent;
430
431         if (RB_EMPTY_NODE(node))
432                 return NULL;
433
434         /* If we have a left-hand child, go down and then right as far
435            as we can. */
436         if (node->rb_left) {
437                 node = node->rb_left; 
438                 while (node->rb_right)
439                         node=node->rb_right;
440                 return (struct rb_node *)node;
441         }
442
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)
446                 node = parent;
447
448         return parent;
449 }
450 EXPORT_SYMBOL(rb_prev);
451
452 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
453                      struct rb_root *root)
454 {
455         struct rb_node *parent = rb_parent(victim);
456
457         /* Set the surrounding nodes to point to the replacement */
458         if (parent) {
459                 if (victim == parent->rb_left)
460                         parent->rb_left = new;
461                 else
462                         parent->rb_right = new;
463         } else {
464                 root->rb_node = new;
465         }
466         if (victim->rb_left)
467                 rb_set_parent(victim->rb_left, new);
468         if (victim->rb_right)
469                 rb_set_parent(victim->rb_right, new);
470
471         /* Copy the pointers/colour from the victim to the replacement */
472         *new = *victim;
473 }
474 EXPORT_SYMBOL(rb_replace_node);