]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - lib/rbtree.c
prio_tree: remove
[mirror_ubuntu-bionic-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
4f035ad6
ML
50#define __rb_parent(pc) ((struct rb_node *)(pc & ~3))
51
52#define __rb_color(pc) ((pc) & 1)
53#define __rb_is_black(pc) __rb_color(pc)
54#define __rb_is_red(pc) (!__rb_color(pc))
55#define rb_color(rb) __rb_color((rb)->__rb_parent_color)
56#define rb_is_red(rb) __rb_is_red((rb)->__rb_parent_color)
57#define rb_is_black(rb) __rb_is_black((rb)->__rb_parent_color)
bf7ad8ee 58
46b6135a
ML
59static inline void rb_set_black(struct rb_node *rb)
60{
61 rb->__rb_parent_color |= RB_BLACK;
62}
63
bf7ad8ee
ML
64static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
65{
66 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
67}
bf7ad8ee 68
5bc9188a
ML
69static inline void rb_set_parent_color(struct rb_node *rb,
70 struct rb_node *p, int color)
71{
72 rb->__rb_parent_color = (unsigned long)p | color;
73}
74
75static inline struct rb_node *rb_red_parent(struct rb_node *red)
76{
77 return (struct rb_node *)red->__rb_parent_color;
78}
79
7abc704a
ML
80static inline void
81__rb_change_child(struct rb_node *old, struct rb_node *new,
82 struct rb_node *parent, struct rb_root *root)
83{
84 if (parent) {
85 if (parent->rb_left == old)
86 parent->rb_left = new;
87 else
88 parent->rb_right = new;
89 } else
90 root->rb_node = new;
91}
92
5bc9188a
ML
93/*
94 * Helper function for rotations:
95 * - old's parent and color get assigned to new
96 * - old gets assigned new as a parent and 'color' as a color.
97 */
98static inline void
99__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
100 struct rb_root *root, int color)
101{
102 struct rb_node *parent = rb_parent(old);
103 new->__rb_parent_color = old->__rb_parent_color;
104 rb_set_parent_color(old, new, color);
7abc704a 105 __rb_change_child(old, new, parent, root);
5bc9188a
ML
106}
107
14b94af0
ML
108static __always_inline void
109__rb_insert(struct rb_node *node, struct rb_root *root,
110 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
1da177e4 111{
5bc9188a 112 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
1da177e4 113
6d58452d
ML
114 while (true) {
115 /*
116 * Loop invariant: node is red
117 *
118 * If there is a black parent, we are done.
119 * Otherwise, take some corrective action as we don't
120 * want a red root or two consecutive red nodes.
121 */
6d58452d 122 if (!parent) {
5bc9188a 123 rb_set_parent_color(node, NULL, RB_BLACK);
6d58452d
ML
124 break;
125 } else if (rb_is_black(parent))
126 break;
127
5bc9188a
ML
128 gparent = rb_red_parent(parent);
129
59633abf
ML
130 tmp = gparent->rb_right;
131 if (parent != tmp) { /* parent == gparent->rb_left */
5bc9188a
ML
132 if (tmp && rb_is_red(tmp)) {
133 /*
134 * Case 1 - color flips
135 *
136 * G g
137 * / \ / \
138 * p u --> P U
139 * / /
140 * n N
141 *
142 * However, since g's parent might be red, and
143 * 4) does not allow this, we need to recurse
144 * at g.
145 */
146 rb_set_parent_color(tmp, gparent, RB_BLACK);
147 rb_set_parent_color(parent, gparent, RB_BLACK);
148 node = gparent;
149 parent = rb_parent(node);
150 rb_set_parent_color(node, parent, RB_RED);
151 continue;
1da177e4
LT
152 }
153
59633abf
ML
154 tmp = parent->rb_right;
155 if (node == tmp) {
5bc9188a
ML
156 /*
157 * Case 2 - left rotate at parent
158 *
159 * G G
160 * / \ / \
161 * p U --> n U
162 * \ /
163 * n p
164 *
165 * This still leaves us in violation of 4), the
166 * continuation into Case 3 will fix that.
167 */
168 parent->rb_right = tmp = node->rb_left;
169 node->rb_left = parent;
170 if (tmp)
171 rb_set_parent_color(tmp, parent,
172 RB_BLACK);
173 rb_set_parent_color(parent, node, RB_RED);
14b94af0 174 augment_rotate(parent, node);
1da177e4 175 parent = node;
59633abf 176 tmp = node->rb_right;
1da177e4
LT
177 }
178
5bc9188a
ML
179 /*
180 * Case 3 - right rotate at gparent
181 *
182 * G P
183 * / \ / \
184 * p U --> n g
185 * / \
186 * n U
187 */
59633abf 188 gparent->rb_left = tmp; /* == parent->rb_right */
5bc9188a
ML
189 parent->rb_right = gparent;
190 if (tmp)
191 rb_set_parent_color(tmp, gparent, RB_BLACK);
192 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
14b94af0 193 augment_rotate(gparent, parent);
1f052865 194 break;
1da177e4 195 } else {
5bc9188a
ML
196 tmp = gparent->rb_left;
197 if (tmp && rb_is_red(tmp)) {
198 /* Case 1 - color flips */
199 rb_set_parent_color(tmp, gparent, RB_BLACK);
200 rb_set_parent_color(parent, gparent, RB_BLACK);
201 node = gparent;
202 parent = rb_parent(node);
203 rb_set_parent_color(node, parent, RB_RED);
204 continue;
1da177e4
LT
205 }
206
59633abf
ML
207 tmp = parent->rb_left;
208 if (node == tmp) {
5bc9188a
ML
209 /* Case 2 - right rotate at parent */
210 parent->rb_left = tmp = node->rb_right;
211 node->rb_right = parent;
212 if (tmp)
213 rb_set_parent_color(tmp, parent,
214 RB_BLACK);
215 rb_set_parent_color(parent, node, RB_RED);
14b94af0 216 augment_rotate(parent, node);
1da177e4 217 parent = node;
59633abf 218 tmp = node->rb_left;
1da177e4
LT
219 }
220
5bc9188a 221 /* Case 3 - left rotate at gparent */
59633abf 222 gparent->rb_right = tmp; /* == parent->rb_left */
5bc9188a
ML
223 parent->rb_left = gparent;
224 if (tmp)
225 rb_set_parent_color(tmp, gparent, RB_BLACK);
226 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
14b94af0 227 augment_rotate(gparent, parent);
1f052865 228 break;
1da177e4
LT
229 }
230 }
1da177e4 231}
1da177e4 232
14b94af0
ML
233static __always_inline void
234__rb_erase_color(struct rb_node *parent, struct rb_root *root,
235 const struct rb_augment_callbacks *augment)
1da177e4 236{
46b6135a 237 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
1da177e4 238
d6ff1273
ML
239 while (true) {
240 /*
46b6135a
ML
241 * Loop invariants:
242 * - node is black (or NULL on first iteration)
243 * - node is not the root (parent is not NULL)
244 * - All leaf paths going through parent and node have a
245 * black node count that is 1 lower than other leaf paths.
d6ff1273 246 */
59633abf
ML
247 sibling = parent->rb_right;
248 if (node != sibling) { /* node == parent->rb_left */
6280d235
ML
249 if (rb_is_red(sibling)) {
250 /*
251 * Case 1 - left rotate at parent
252 *
253 * P S
254 * / \ / \
255 * N s --> p Sr
256 * / \ / \
257 * Sl Sr N Sl
258 */
259 parent->rb_right = tmp1 = sibling->rb_left;
260 sibling->rb_left = parent;
261 rb_set_parent_color(tmp1, parent, RB_BLACK);
262 __rb_rotate_set_parents(parent, sibling, root,
263 RB_RED);
14b94af0 264 augment->rotate(parent, sibling);
6280d235 265 sibling = tmp1;
1da177e4 266 }
6280d235
ML
267 tmp1 = sibling->rb_right;
268 if (!tmp1 || rb_is_black(tmp1)) {
269 tmp2 = sibling->rb_left;
270 if (!tmp2 || rb_is_black(tmp2)) {
271 /*
272 * Case 2 - sibling color flip
273 * (p could be either color here)
274 *
275 * (p) (p)
276 * / \ / \
277 * N S --> N s
278 * / \ / \
279 * Sl Sr Sl Sr
280 *
46b6135a
ML
281 * This leaves us violating 5) which
282 * can be fixed by flipping p to black
283 * if it was red, or by recursing at p.
284 * p is red when coming from Case 1.
6280d235
ML
285 */
286 rb_set_parent_color(sibling, parent,
287 RB_RED);
46b6135a
ML
288 if (rb_is_red(parent))
289 rb_set_black(parent);
290 else {
291 node = parent;
292 parent = rb_parent(node);
293 if (parent)
294 continue;
295 }
296 break;
1da177e4 297 }
6280d235
ML
298 /*
299 * Case 3 - right rotate at sibling
300 * (p could be either color here)
301 *
302 * (p) (p)
303 * / \ / \
304 * N S --> N Sl
305 * / \ \
306 * sl Sr s
307 * \
308 * Sr
309 */
310 sibling->rb_left = tmp1 = tmp2->rb_right;
311 tmp2->rb_right = sibling;
312 parent->rb_right = tmp2;
313 if (tmp1)
314 rb_set_parent_color(tmp1, sibling,
315 RB_BLACK);
14b94af0 316 augment->rotate(sibling, tmp2);
6280d235
ML
317 tmp1 = sibling;
318 sibling = tmp2;
1da177e4 319 }
6280d235
ML
320 /*
321 * Case 4 - left rotate at parent + color flips
322 * (p and sl could be either color here.
323 * After rotation, p becomes black, s acquires
324 * p's color, and sl keeps its color)
325 *
326 * (p) (s)
327 * / \ / \
328 * N S --> P Sr
329 * / \ / \
330 * (sl) sr N (sl)
331 */
332 parent->rb_right = tmp2 = sibling->rb_left;
333 sibling->rb_left = parent;
334 rb_set_parent_color(tmp1, sibling, RB_BLACK);
335 if (tmp2)
336 rb_set_parent(tmp2, parent);
337 __rb_rotate_set_parents(parent, sibling, root,
338 RB_BLACK);
14b94af0 339 augment->rotate(parent, sibling);
e125d147 340 break;
d6ff1273 341 } else {
6280d235
ML
342 sibling = parent->rb_left;
343 if (rb_is_red(sibling)) {
344 /* Case 1 - right rotate at parent */
345 parent->rb_left = tmp1 = sibling->rb_right;
346 sibling->rb_right = parent;
347 rb_set_parent_color(tmp1, parent, RB_BLACK);
348 __rb_rotate_set_parents(parent, sibling, root,
349 RB_RED);
14b94af0 350 augment->rotate(parent, sibling);
6280d235 351 sibling = tmp1;
1da177e4 352 }
6280d235
ML
353 tmp1 = sibling->rb_left;
354 if (!tmp1 || rb_is_black(tmp1)) {
355 tmp2 = sibling->rb_right;
356 if (!tmp2 || rb_is_black(tmp2)) {
357 /* Case 2 - sibling color flip */
358 rb_set_parent_color(sibling, parent,
359 RB_RED);
46b6135a
ML
360 if (rb_is_red(parent))
361 rb_set_black(parent);
362 else {
363 node = parent;
364 parent = rb_parent(node);
365 if (parent)
366 continue;
367 }
368 break;
1da177e4 369 }
6280d235
ML
370 /* Case 3 - right rotate at sibling */
371 sibling->rb_right = tmp1 = tmp2->rb_left;
372 tmp2->rb_left = sibling;
373 parent->rb_left = tmp2;
374 if (tmp1)
375 rb_set_parent_color(tmp1, sibling,
376 RB_BLACK);
14b94af0 377 augment->rotate(sibling, tmp2);
6280d235
ML
378 tmp1 = sibling;
379 sibling = tmp2;
1da177e4 380 }
6280d235
ML
381 /* Case 4 - left rotate at parent + color flips */
382 parent->rb_left = tmp2 = sibling->rb_right;
383 sibling->rb_right = parent;
384 rb_set_parent_color(tmp1, sibling, RB_BLACK);
385 if (tmp2)
386 rb_set_parent(tmp2, parent);
387 __rb_rotate_set_parents(parent, sibling, root,
388 RB_BLACK);
14b94af0 389 augment->rotate(parent, sibling);
e125d147 390 break;
1da177e4
LT
391 }
392 }
1da177e4
LT
393}
394
14b94af0
ML
395static __always_inline void
396__rb_erase(struct rb_node *node, struct rb_root *root,
397 const struct rb_augment_callbacks *augment)
1da177e4 398{
60670b80 399 struct rb_node *child = node->rb_right, *tmp = node->rb_left;
46b6135a 400 struct rb_node *parent, *rebalance;
4f035ad6 401 unsigned long pc;
1da177e4 402
60670b80 403 if (!tmp) {
46b6135a
ML
404 /*
405 * Case 1: node to erase has no more than 1 child (easy!)
406 *
407 * Note that if there is one child it must be red due to 5)
408 * and node must be black due to 4). We adjust colors locally
409 * so as to bypass __rb_erase_color() later on.
410 */
4f035ad6
ML
411 pc = node->__rb_parent_color;
412 parent = __rb_parent(pc);
60670b80 413 __rb_change_child(node, child, parent, root);
46b6135a 414 if (child) {
4f035ad6 415 child->__rb_parent_color = pc;
46b6135a 416 rebalance = NULL;
4f035ad6
ML
417 } else
418 rebalance = __rb_is_black(pc) ? parent : NULL;
14b94af0 419 tmp = parent;
60670b80
ML
420 } else if (!child) {
421 /* Still case 1, but this time the child is node->rb_left */
4f035ad6
ML
422 tmp->__rb_parent_color = pc = node->__rb_parent_color;
423 parent = __rb_parent(pc);
46b6135a 424 __rb_change_child(node, tmp, parent, root);
46b6135a 425 rebalance = NULL;
14b94af0 426 tmp = parent;
60670b80 427 } else {
4f035ad6
ML
428 struct rb_node *successor = child, *child2;
429 tmp = child->rb_left;
430 if (!tmp) {
431 /*
432 * Case 2: node's successor is its right child
433 *
434 * (n) (s)
435 * / \ / \
436 * (x) (s) -> (x) (c)
437 * \
438 * (c)
439 */
14b94af0
ML
440 parent = successor;
441 child2 = successor->rb_right;
442 augment->copy(node, successor);
4c601178 443 } else {
4f035ad6
ML
444 /*
445 * Case 3: node's successor is leftmost under
446 * node's right child subtree
447 *
448 * (n) (s)
449 * / \ / \
450 * (x) (y) -> (x) (y)
451 * / /
452 * (p) (p)
453 * / /
454 * (s) (c)
455 * \
456 * (c)
457 */
458 do {
459 parent = successor;
460 successor = tmp;
461 tmp = tmp->rb_left;
462 } while (tmp);
463 parent->rb_left = child2 = successor->rb_right;
464 successor->rb_right = child;
465 rb_set_parent(child, successor);
14b94af0
ML
466 augment->copy(node, successor);
467 augment->propagate(parent, successor);
4c601178 468 }
1975e593 469
4f035ad6
ML
470 successor->rb_left = tmp = node->rb_left;
471 rb_set_parent(tmp, successor);
472
473 pc = node->__rb_parent_color;
474 tmp = __rb_parent(pc);
475 __rb_change_child(node, successor, tmp, root);
476 if (child2) {
477 successor->__rb_parent_color = pc;
478 rb_set_parent_color(child2, parent, RB_BLACK);
46b6135a
ML
479 rebalance = NULL;
480 } else {
4f035ad6
ML
481 unsigned long pc2 = successor->__rb_parent_color;
482 successor->__rb_parent_color = pc;
483 rebalance = __rb_is_black(pc2) ? parent : NULL;
46b6135a 484 }
14b94af0 485 tmp = successor;
1da177e4
LT
486 }
487
14b94af0 488 augment->propagate(tmp, NULL);
46b6135a 489 if (rebalance)
14b94af0
ML
490 __rb_erase_color(rebalance, root, augment);
491}
492
493/*
494 * Non-augmented rbtree manipulation functions.
495 *
496 * We use dummy augmented callbacks here, and have the compiler optimize them
497 * out of the rb_insert_color() and rb_erase() function definitions.
498 */
499
500static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
501static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
502static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
503
504static const struct rb_augment_callbacks dummy_callbacks = {
505 dummy_propagate, dummy_copy, dummy_rotate
506};
507
508void rb_insert_color(struct rb_node *node, struct rb_root *root)
509{
510 __rb_insert(node, root, dummy_rotate);
511}
512EXPORT_SYMBOL(rb_insert_color);
513
514void rb_erase(struct rb_node *node, struct rb_root *root)
515{
516 __rb_erase(node, root, &dummy_callbacks);
1da177e4
LT
517}
518EXPORT_SYMBOL(rb_erase);
519
14b94af0
ML
520/*
521 * Augmented rbtree manipulation functions.
522 *
523 * This instantiates the same __always_inline functions as in the non-augmented
524 * case, but this time with user-defined callbacks.
525 */
526
527void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
528 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
529{
530 __rb_insert(node, root, augment_rotate);
531}
532EXPORT_SYMBOL(__rb_insert_augmented);
533
534void rb_erase_augmented(struct rb_node *node, struct rb_root *root,
535 const struct rb_augment_callbacks *augment)
536{
537 __rb_erase(node, root, augment);
538}
539EXPORT_SYMBOL(rb_erase_augmented);
540
1da177e4
LT
541/*
542 * This function returns the first node (in sort order) of the tree.
543 */
f4b477c4 544struct rb_node *rb_first(const struct rb_root *root)
1da177e4
LT
545{
546 struct rb_node *n;
547
548 n = root->rb_node;
549 if (!n)
550 return NULL;
551 while (n->rb_left)
552 n = n->rb_left;
553 return n;
554}
555EXPORT_SYMBOL(rb_first);
556
f4b477c4 557struct rb_node *rb_last(const struct rb_root *root)
1da177e4
LT
558{
559 struct rb_node *n;
560
561 n = root->rb_node;
562 if (!n)
563 return NULL;
564 while (n->rb_right)
565 n = n->rb_right;
566 return n;
567}
568EXPORT_SYMBOL(rb_last);
569
f4b477c4 570struct rb_node *rb_next(const struct rb_node *node)
1da177e4 571{
55a98102
DW
572 struct rb_node *parent;
573
4c199a93 574 if (RB_EMPTY_NODE(node))
10fd48f2
JA
575 return NULL;
576
7ce6ff9e
ML
577 /*
578 * If we have a right-hand child, go down and then left as far
579 * as we can.
580 */
1da177e4
LT
581 if (node->rb_right) {
582 node = node->rb_right;
583 while (node->rb_left)
584 node=node->rb_left;
f4b477c4 585 return (struct rb_node *)node;
1da177e4
LT
586 }
587
7ce6ff9e
ML
588 /*
589 * No right-hand children. Everything down and left is smaller than us,
590 * so any 'next' node must be in the general direction of our parent.
591 * Go up the tree; any time the ancestor is a right-hand child of its
592 * parent, keep going up. First time it's a left-hand child of its
593 * parent, said parent is our 'next' node.
594 */
55a98102
DW
595 while ((parent = rb_parent(node)) && node == parent->rb_right)
596 node = parent;
1da177e4 597
55a98102 598 return parent;
1da177e4
LT
599}
600EXPORT_SYMBOL(rb_next);
601
f4b477c4 602struct rb_node *rb_prev(const struct rb_node *node)
1da177e4 603{
55a98102
DW
604 struct rb_node *parent;
605
4c199a93 606 if (RB_EMPTY_NODE(node))
10fd48f2
JA
607 return NULL;
608
7ce6ff9e
ML
609 /*
610 * If we have a left-hand child, go down and then right as far
611 * as we can.
612 */
1da177e4
LT
613 if (node->rb_left) {
614 node = node->rb_left;
615 while (node->rb_right)
616 node=node->rb_right;
f4b477c4 617 return (struct rb_node *)node;
1da177e4
LT
618 }
619
7ce6ff9e
ML
620 /*
621 * No left-hand children. Go up till we find an ancestor which
622 * is a right-hand child of its parent.
623 */
55a98102
DW
624 while ((parent = rb_parent(node)) && node == parent->rb_left)
625 node = parent;
1da177e4 626
55a98102 627 return parent;
1da177e4
LT
628}
629EXPORT_SYMBOL(rb_prev);
630
631void rb_replace_node(struct rb_node *victim, struct rb_node *new,
632 struct rb_root *root)
633{
55a98102 634 struct rb_node *parent = rb_parent(victim);
1da177e4
LT
635
636 /* Set the surrounding nodes to point to the replacement */
7abc704a 637 __rb_change_child(victim, new, parent, root);
1da177e4 638 if (victim->rb_left)
55a98102 639 rb_set_parent(victim->rb_left, new);
1da177e4 640 if (victim->rb_right)
55a98102 641 rb_set_parent(victim->rb_right, new);
1da177e4
LT
642
643 /* Copy the pointers/colour from the victim to the replacement */
644 *new = *victim;
645}
646EXPORT_SYMBOL(rb_replace_node);