blob: 12abb8abf442244be5b459253259fe6e8ee6135f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050024#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070026#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
35static 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}
39static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
45{
46 struct rb_node *right = node->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +010047 struct rb_node *parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 if ((node->rb_right = right->rb_left))
David Woodhouse55a98102006-04-21 13:35:51 +010050 rb_set_parent(right->rb_left, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 right->rb_left = node;
52
David Woodhouse55a98102006-04-21 13:35:51 +010053 rb_set_parent(right, parent);
54
55 if (parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 {
David Woodhouse55a98102006-04-21 13:35:51 +010057 if (node == parent->rb_left)
58 parent->rb_left = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 else
David Woodhouse55a98102006-04-21 13:35:51 +010060 parent->rb_right = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62 else
63 root->rb_node = right;
David Woodhouse55a98102006-04-21 13:35:51 +010064 rb_set_parent(node, right);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
68{
69 struct rb_node *left = node->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +010070 struct rb_node *parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 if ((node->rb_left = left->rb_right))
David Woodhouse55a98102006-04-21 13:35:51 +010073 rb_set_parent(left->rb_right, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 left->rb_right = node;
75
David Woodhouse55a98102006-04-21 13:35:51 +010076 rb_set_parent(left, parent);
77
78 if (parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 {
David Woodhouse55a98102006-04-21 13:35:51 +010080 if (node == parent->rb_right)
81 parent->rb_right = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 else
David Woodhouse55a98102006-04-21 13:35:51 +010083 parent->rb_left = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
85 else
86 root->rb_node = left;
David Woodhouse55a98102006-04-21 13:35:51 +010087 rb_set_parent(node, left);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90void rb_insert_color(struct rb_node *node, struct rb_root *root)
91{
92 struct rb_node *parent, *gparent;
93
David Woodhouse55a98102006-04-21 13:35:51 +010094 while ((parent = rb_parent(node)) && rb_is_red(parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 {
David Woodhouse55a98102006-04-21 13:35:51 +010096 gparent = rb_parent(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (parent == gparent->rb_left)
99 {
100 {
101 register struct rb_node *uncle = gparent->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100102 if (uncle && rb_is_red(uncle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 {
David Woodhouse55a98102006-04-21 13:35:51 +0100104 rb_set_black(uncle);
105 rb_set_black(parent);
106 rb_set_red(gparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 node = gparent;
108 continue;
109 }
110 }
111
Michel Lespinasse1f052862012-10-08 16:30:42 -0700112 if (parent->rb_right == node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 __rb_rotate_left(parent, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 parent = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
David Woodhouse55a98102006-04-21 13:35:51 +0100117 rb_set_black(parent);
118 rb_set_red(gparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 __rb_rotate_right(gparent, root);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 } else {
122 {
123 register struct rb_node *uncle = gparent->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100124 if (uncle && rb_is_red(uncle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 {
David Woodhouse55a98102006-04-21 13:35:51 +0100126 rb_set_black(uncle);
127 rb_set_black(parent);
128 rb_set_red(gparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 node = gparent;
130 continue;
131 }
132 }
133
Michel Lespinasse1f052862012-10-08 16:30:42 -0700134 if (parent->rb_left == node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 __rb_rotate_right(parent, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 parent = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
David Woodhouse55a98102006-04-21 13:35:51 +0100139 rb_set_black(parent);
140 rb_set_red(gparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 __rb_rotate_left(gparent, root);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700142 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144 }
145
David Woodhouse55a98102006-04-21 13:35:51 +0100146 rb_set_black(root->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148EXPORT_SYMBOL(rb_insert_color);
149
150static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
151 struct rb_root *root)
152{
153 struct rb_node *other;
154
David Woodhouse55a98102006-04-21 13:35:51 +0100155 while ((!node || rb_is_black(node)) && node != root->rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 {
157 if (parent->rb_left == node)
158 {
159 other = parent->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100160 if (rb_is_red(other))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 {
David Woodhouse55a98102006-04-21 13:35:51 +0100162 rb_set_black(other);
163 rb_set_red(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 __rb_rotate_left(parent, root);
165 other = parent->rb_right;
166 }
David Woodhouse55a98102006-04-21 13:35:51 +0100167 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
168 (!other->rb_right || rb_is_black(other->rb_right)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 {
David Woodhouse55a98102006-04-21 13:35:51 +0100170 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 node = parent;
David Woodhouse55a98102006-04-21 13:35:51 +0100172 parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 else
175 {
David Woodhouse55a98102006-04-21 13:35:51 +0100176 if (!other->rb_right || rb_is_black(other->rb_right))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 {
Wolfram Strepp55a63992009-03-31 15:23:45 -0700178 rb_set_black(other->rb_left);
David Woodhouse55a98102006-04-21 13:35:51 +0100179 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 __rb_rotate_right(other, root);
181 other = parent->rb_right;
182 }
David Woodhouse2f3243a2006-06-05 20:19:05 +0100183 rb_set_color(other, rb_color(parent));
David Woodhouse55a98102006-04-21 13:35:51 +0100184 rb_set_black(parent);
Wolfram Strepp55a63992009-03-31 15:23:45 -0700185 rb_set_black(other->rb_right);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 __rb_rotate_left(parent, root);
187 node = root->rb_node;
188 break;
189 }
190 }
191 else
192 {
193 other = parent->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100194 if (rb_is_red(other))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 {
David Woodhouse55a98102006-04-21 13:35:51 +0100196 rb_set_black(other);
197 rb_set_red(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 __rb_rotate_right(parent, root);
199 other = parent->rb_left;
200 }
David Woodhouse55a98102006-04-21 13:35:51 +0100201 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
202 (!other->rb_right || rb_is_black(other->rb_right)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 {
David Woodhouse55a98102006-04-21 13:35:51 +0100204 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 node = parent;
David Woodhouse55a98102006-04-21 13:35:51 +0100206 parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 else
209 {
David Woodhouse55a98102006-04-21 13:35:51 +0100210 if (!other->rb_left || rb_is_black(other->rb_left))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 {
Wolfram Strepp55a63992009-03-31 15:23:45 -0700212 rb_set_black(other->rb_right);
David Woodhouse55a98102006-04-21 13:35:51 +0100213 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 __rb_rotate_left(other, root);
215 other = parent->rb_left;
216 }
David Woodhouse2f3243a2006-06-05 20:19:05 +0100217 rb_set_color(other, rb_color(parent));
David Woodhouse55a98102006-04-21 13:35:51 +0100218 rb_set_black(parent);
Wolfram Strepp55a63992009-03-31 15:23:45 -0700219 rb_set_black(other->rb_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 __rb_rotate_right(parent, root);
221 node = root->rb_node;
222 break;
223 }
224 }
225 }
226 if (node)
David Woodhouse55a98102006-04-21 13:35:51 +0100227 rb_set_black(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230void 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;
Wolfram Strepp16c047a2009-06-16 15:34:11 -0700246
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 child = node->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100256 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100257 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David Woodhouse55a98102006-04-21 13:35:51 +0100259 if (parent == old) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 parent = node;
Wolfram Strepp4c601172009-06-16 15:34:12 -0700261 } else {
262 if (child)
263 rb_set_parent(child, parent);
David Woodhouse1975e592006-04-21 13:30:36 +0100264 parent->rb_left = child;
Wolfram Strepp4b324122009-06-16 15:34:13 -0700265
266 node->rb_right = old->rb_right;
267 rb_set_parent(old->rb_right, node);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700268 }
David Woodhouse1975e592006-04-21 13:30:36 +0100269
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -0700270 node->__rb_parent_color = old->__rb_parent_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 node->rb_left = old->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100272 rb_set_parent(old->rb_left, node);
Wolfram Strepp4b324122009-06-16 15:34:13 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 goto color;
275 }
276
David Woodhouse55a98102006-04-21 13:35:51 +0100277 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100278 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 if (child)
David Woodhouse55a98102006-04-21 13:35:51 +0100281 rb_set_parent(child, parent);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200282 if (parent)
283 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (parent->rb_left == node)
285 parent->rb_left = child;
286 else
287 parent->rb_right = child;
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800288 }
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200289 else
290 root->rb_node = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 color:
293 if (color == RB_BLACK)
294 __rb_erase_color(child, parent, root);
295}
296EXPORT_SYMBOL(rb_erase);
297
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200298static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
299{
300 struct rb_node *parent;
301
302up:
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 */
321void 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}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100330EXPORT_SYMBOL(rb_augment_insert);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200331
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 */
336struct 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}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100356EXPORT_SYMBOL(rb_augment_erase_begin);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200357
358/*
359 * after removal, update the tree to account for the removed entry
360 * and any rebalance damage.
361 */
362void 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}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100367EXPORT_SYMBOL(rb_augment_erase_end);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
370 * This function returns the first node (in sort order) of the tree.
371 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000372struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
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}
383EXPORT_SYMBOL(rb_first);
384
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000385struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
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}
396EXPORT_SYMBOL(rb_last);
397
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000398struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
David Woodhouse55a98102006-04-21 13:35:51 +0100400 struct rb_node *parent;
401
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700402 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200403 return NULL;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* 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;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000411 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
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. */
David Woodhouse55a98102006-04-21 13:35:51 +0100420 while ((parent = rb_parent(node)) && node == parent->rb_right)
421 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
David Woodhouse55a98102006-04-21 13:35:51 +0100423 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425EXPORT_SYMBOL(rb_next);
426
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000427struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
David Woodhouse55a98102006-04-21 13:35:51 +0100429 struct rb_node *parent;
430
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700431 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200432 return NULL;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* 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;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000440 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
443 /* No left-hand children. Go up till we find an ancestor which
444 is a right-hand child of its parent */
David Woodhouse55a98102006-04-21 13:35:51 +0100445 while ((parent = rb_parent(node)) && node == parent->rb_left)
446 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
David Woodhouse55a98102006-04-21 13:35:51 +0100448 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450EXPORT_SYMBOL(rb_prev);
451
452void rb_replace_node(struct rb_node *victim, struct rb_node *new,
453 struct rb_root *root)
454{
David Woodhouse55a98102006-04-21 13:35:51 +0100455 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
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)
David Woodhouse55a98102006-04-21 13:35:51 +0100467 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100469 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* Copy the pointers/colour from the victim to the replacement */
472 *new = *victim;
473}
474EXPORT_SYMBOL(rb_replace_node);