blob: ccada9abe6f53b75da664d0718fefc6ea80b6ea8 [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
112 if (parent->rb_right == node)
113 {
114 register struct rb_node *tmp;
115 __rb_rotate_left(parent, root);
116 tmp = parent;
117 parent = node;
118 node = tmp;
119 }
120
David Woodhouse55a98102006-04-21 13:35:51 +0100121 rb_set_black(parent);
122 rb_set_red(gparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 __rb_rotate_right(gparent, root);
124 } else {
125 {
126 register struct rb_node *uncle = gparent->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100127 if (uncle && rb_is_red(uncle))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 {
David Woodhouse55a98102006-04-21 13:35:51 +0100129 rb_set_black(uncle);
130 rb_set_black(parent);
131 rb_set_red(gparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 node = gparent;
133 continue;
134 }
135 }
136
137 if (parent->rb_left == node)
138 {
139 register struct rb_node *tmp;
140 __rb_rotate_right(parent, root);
141 tmp = parent;
142 parent = node;
143 node = tmp;
144 }
145
David Woodhouse55a98102006-04-21 13:35:51 +0100146 rb_set_black(parent);
147 rb_set_red(gparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 __rb_rotate_left(gparent, root);
149 }
150 }
151
David Woodhouse55a98102006-04-21 13:35:51 +0100152 rb_set_black(root->rb_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154EXPORT_SYMBOL(rb_insert_color);
155
156static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
157 struct rb_root *root)
158{
159 struct rb_node *other;
160
David Woodhouse55a98102006-04-21 13:35:51 +0100161 while ((!node || rb_is_black(node)) && node != root->rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 {
163 if (parent->rb_left == node)
164 {
165 other = parent->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100166 if (rb_is_red(other))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 {
David Woodhouse55a98102006-04-21 13:35:51 +0100168 rb_set_black(other);
169 rb_set_red(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 __rb_rotate_left(parent, root);
171 other = parent->rb_right;
172 }
David Woodhouse55a98102006-04-21 13:35:51 +0100173 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
174 (!other->rb_right || rb_is_black(other->rb_right)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 {
David Woodhouse55a98102006-04-21 13:35:51 +0100176 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 node = parent;
David Woodhouse55a98102006-04-21 13:35:51 +0100178 parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 else
181 {
David Woodhouse55a98102006-04-21 13:35:51 +0100182 if (!other->rb_right || rb_is_black(other->rb_right))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 {
Wolfram Strepp55a63992009-03-31 15:23:45 -0700184 rb_set_black(other->rb_left);
David Woodhouse55a98102006-04-21 13:35:51 +0100185 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 __rb_rotate_right(other, root);
187 other = parent->rb_right;
188 }
David Woodhouse2f3243a2006-06-05 20:19:05 +0100189 rb_set_color(other, rb_color(parent));
David Woodhouse55a98102006-04-21 13:35:51 +0100190 rb_set_black(parent);
Wolfram Strepp55a63992009-03-31 15:23:45 -0700191 rb_set_black(other->rb_right);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 __rb_rotate_left(parent, root);
193 node = root->rb_node;
194 break;
195 }
196 }
197 else
198 {
199 other = parent->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100200 if (rb_is_red(other))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 {
David Woodhouse55a98102006-04-21 13:35:51 +0100202 rb_set_black(other);
203 rb_set_red(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 __rb_rotate_right(parent, root);
205 other = parent->rb_left;
206 }
David Woodhouse55a98102006-04-21 13:35:51 +0100207 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
208 (!other->rb_right || rb_is_black(other->rb_right)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 {
David Woodhouse55a98102006-04-21 13:35:51 +0100210 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 node = parent;
David Woodhouse55a98102006-04-21 13:35:51 +0100212 parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 else
215 {
David Woodhouse55a98102006-04-21 13:35:51 +0100216 if (!other->rb_left || rb_is_black(other->rb_left))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 {
Wolfram Strepp55a63992009-03-31 15:23:45 -0700218 rb_set_black(other->rb_right);
David Woodhouse55a98102006-04-21 13:35:51 +0100219 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 __rb_rotate_left(other, root);
221 other = parent->rb_left;
222 }
David Woodhouse2f3243a2006-06-05 20:19:05 +0100223 rb_set_color(other, rb_color(parent));
David Woodhouse55a98102006-04-21 13:35:51 +0100224 rb_set_black(parent);
Wolfram Strepp55a63992009-03-31 15:23:45 -0700225 rb_set_black(other->rb_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 __rb_rotate_right(parent, root);
227 node = root->rb_node;
228 break;
229 }
230 }
231 }
232 if (node)
David Woodhouse55a98102006-04-21 13:35:51 +0100233 rb_set_black(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236void rb_erase(struct rb_node *node, struct rb_root *root)
237{
238 struct rb_node *child, *parent;
239 int color;
240
241 if (!node->rb_left)
242 child = node->rb_right;
243 else if (!node->rb_right)
244 child = node->rb_left;
245 else
246 {
247 struct rb_node *old = node, *left;
248
249 node = node->rb_right;
250 while ((left = node->rb_left) != NULL)
251 node = left;
Wolfram Strepp16c047a2009-06-16 15:34:11 -0700252
253 if (rb_parent(old)) {
254 if (rb_parent(old)->rb_left == old)
255 rb_parent(old)->rb_left = node;
256 else
257 rb_parent(old)->rb_right = node;
258 } else
259 root->rb_node = node;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 child = node->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100262 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100263 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
David Woodhouse55a98102006-04-21 13:35:51 +0100265 if (parent == old) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 parent = node;
Wolfram Strepp4c601172009-06-16 15:34:12 -0700267 } else {
268 if (child)
269 rb_set_parent(child, parent);
David Woodhouse1975e592006-04-21 13:30:36 +0100270 parent->rb_left = child;
Wolfram Strepp4b324122009-06-16 15:34:13 -0700271
272 node->rb_right = old->rb_right;
273 rb_set_parent(old->rb_right, node);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700274 }
David Woodhouse1975e592006-04-21 13:30:36 +0100275
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -0700276 node->__rb_parent_color = old->__rb_parent_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 node->rb_left = old->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100278 rb_set_parent(old->rb_left, node);
Wolfram Strepp4b324122009-06-16 15:34:13 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto color;
281 }
282
David Woodhouse55a98102006-04-21 13:35:51 +0100283 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100284 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (child)
David Woodhouse55a98102006-04-21 13:35:51 +0100287 rb_set_parent(child, parent);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200288 if (parent)
289 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (parent->rb_left == node)
291 parent->rb_left = child;
292 else
293 parent->rb_right = child;
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800294 }
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200295 else
296 root->rb_node = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 color:
299 if (color == RB_BLACK)
300 __rb_erase_color(child, parent, root);
301}
302EXPORT_SYMBOL(rb_erase);
303
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200304static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
305{
306 struct rb_node *parent;
307
308up:
309 func(node, data);
310 parent = rb_parent(node);
311 if (!parent)
312 return;
313
314 if (node == parent->rb_left && parent->rb_right)
315 func(parent->rb_right, data);
316 else if (parent->rb_left)
317 func(parent->rb_left, data);
318
319 node = parent;
320 goto up;
321}
322
323/*
324 * after inserting @node into the tree, update the tree to account for
325 * both the new entry and any damage done by rebalance
326 */
327void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
328{
329 if (node->rb_left)
330 node = node->rb_left;
331 else if (node->rb_right)
332 node = node->rb_right;
333
334 rb_augment_path(node, func, data);
335}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100336EXPORT_SYMBOL(rb_augment_insert);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200337
338/*
339 * before removing the node, find the deepest node on the rebalance path
340 * that will still be there after @node gets removed
341 */
342struct rb_node *rb_augment_erase_begin(struct rb_node *node)
343{
344 struct rb_node *deepest;
345
346 if (!node->rb_right && !node->rb_left)
347 deepest = rb_parent(node);
348 else if (!node->rb_right)
349 deepest = node->rb_left;
350 else if (!node->rb_left)
351 deepest = node->rb_right;
352 else {
353 deepest = rb_next(node);
354 if (deepest->rb_right)
355 deepest = deepest->rb_right;
356 else if (rb_parent(deepest) != node)
357 deepest = rb_parent(deepest);
358 }
359
360 return deepest;
361}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100362EXPORT_SYMBOL(rb_augment_erase_begin);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200363
364/*
365 * after removal, update the tree to account for the removed entry
366 * and any rebalance damage.
367 */
368void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
369{
370 if (node)
371 rb_augment_path(node, func, data);
372}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100373EXPORT_SYMBOL(rb_augment_erase_end);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * This function returns the first node (in sort order) of the tree.
377 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000378struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct rb_node *n;
381
382 n = root->rb_node;
383 if (!n)
384 return NULL;
385 while (n->rb_left)
386 n = n->rb_left;
387 return n;
388}
389EXPORT_SYMBOL(rb_first);
390
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000391struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct rb_node *n;
394
395 n = root->rb_node;
396 if (!n)
397 return NULL;
398 while (n->rb_right)
399 n = n->rb_right;
400 return n;
401}
402EXPORT_SYMBOL(rb_last);
403
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000404struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
David Woodhouse55a98102006-04-21 13:35:51 +0100406 struct rb_node *parent;
407
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700408 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200409 return NULL;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* If we have a right-hand child, go down and then left as far
412 as we can. */
413 if (node->rb_right) {
414 node = node->rb_right;
415 while (node->rb_left)
416 node=node->rb_left;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000417 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 /* No right-hand children. Everything down and left is
421 smaller than us, so any 'next' node must be in the general
422 direction of our parent. Go up the tree; any time the
423 ancestor is a right-hand child of its parent, keep going
424 up. First time it's a left-hand child of its parent, said
425 parent is our 'next' node. */
David Woodhouse55a98102006-04-21 13:35:51 +0100426 while ((parent = rb_parent(node)) && node == parent->rb_right)
427 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David Woodhouse55a98102006-04-21 13:35:51 +0100429 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431EXPORT_SYMBOL(rb_next);
432
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000433struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
David Woodhouse55a98102006-04-21 13:35:51 +0100435 struct rb_node *parent;
436
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700437 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200438 return NULL;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 /* If we have a left-hand child, go down and then right as far
441 as we can. */
442 if (node->rb_left) {
443 node = node->rb_left;
444 while (node->rb_right)
445 node=node->rb_right;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000446 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
448
449 /* No left-hand children. Go up till we find an ancestor which
450 is a right-hand child of its parent */
David Woodhouse55a98102006-04-21 13:35:51 +0100451 while ((parent = rb_parent(node)) && node == parent->rb_left)
452 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
David Woodhouse55a98102006-04-21 13:35:51 +0100454 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456EXPORT_SYMBOL(rb_prev);
457
458void rb_replace_node(struct rb_node *victim, struct rb_node *new,
459 struct rb_root *root)
460{
David Woodhouse55a98102006-04-21 13:35:51 +0100461 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 /* Set the surrounding nodes to point to the replacement */
464 if (parent) {
465 if (victim == parent->rb_left)
466 parent->rb_left = new;
467 else
468 parent->rb_right = new;
469 } else {
470 root->rb_node = new;
471 }
472 if (victim->rb_left)
David Woodhouse55a98102006-04-21 13:35:51 +0100473 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100475 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 /* Copy the pointers/colour from the victim to the replacement */
478 *new = *victim;
479}
480EXPORT_SYMBOL(rb_replace_node);