blob: 08926709b4f9f16c897c7f38d5290c49419a3c6e [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 Lespinasse5bc91882012-10-08 16:30:47 -070026/*
27 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
28 *
29 * 1) A node is either red or black
30 * 2) The root is black
31 * 3) All leaves (NULL) are black
32 * 4) Both children of every red node are black
33 * 5) Every simple path from root to leaves contains the same number
34 * of black nodes.
35 *
36 * 4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
37 * consecutive red nodes in a path and every red node is therefore followed by
38 * a black. So if B is the number of black nodes on every simple path (as per
39 * 5), then the longest possible path due to 4 is 2B.
40 *
41 * We shall indicate color with case, where black nodes are uppercase and red
Michel Lespinasse6280d232012-10-08 16:30:57 -070042 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
43 * parentheses and have some accompanying text comment.
Michel Lespinasse5bc91882012-10-08 16:30:47 -070044 */
45
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070046#define RB_RED 0
47#define RB_BLACK 1
48
49#define rb_color(r) ((r)->__rb_parent_color & 1)
50#define rb_is_red(r) (!rb_color(r))
51#define rb_is_black(r) rb_color(r)
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070052
53static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
54{
55 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
56}
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070057
Michel Lespinasse5bc91882012-10-08 16:30:47 -070058static inline void rb_set_parent_color(struct rb_node *rb,
59 struct rb_node *p, int color)
60{
61 rb->__rb_parent_color = (unsigned long)p | color;
62}
63
64static inline struct rb_node *rb_red_parent(struct rb_node *red)
65{
66 return (struct rb_node *)red->__rb_parent_color;
67}
68
Michel Lespinasse5bc91882012-10-08 16:30:47 -070069/*
70 * Helper function for rotations:
71 * - old's parent and color get assigned to new
72 * - old gets assigned new as a parent and 'color' as a color.
73 */
74static inline void
75__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
76 struct rb_root *root, int color)
77{
78 struct rb_node *parent = rb_parent(old);
79 new->__rb_parent_color = old->__rb_parent_color;
80 rb_set_parent_color(old, new, color);
81 if (parent) {
82 if (parent->rb_left == old)
83 parent->rb_left = new;
84 else
85 parent->rb_right = new;
86 } else
87 root->rb_node = new;
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090void rb_insert_color(struct rb_node *node, struct rb_root *root)
91{
Michel Lespinasse5bc91882012-10-08 16:30:47 -070092 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Michel Lespinasse6d584522012-10-08 16:30:44 -070094 while (true) {
95 /*
96 * Loop invariant: node is red
97 *
98 * If there is a black parent, we are done.
99 * Otherwise, take some corrective action as we don't
100 * want a red root or two consecutive red nodes.
101 */
Michel Lespinasse6d584522012-10-08 16:30:44 -0700102 if (!parent) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700103 rb_set_parent_color(node, NULL, RB_BLACK);
Michel Lespinasse6d584522012-10-08 16:30:44 -0700104 break;
105 } else if (rb_is_black(parent))
106 break;
107
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700108 gparent = rb_red_parent(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700110 if (parent == gparent->rb_left) {
111 tmp = gparent->rb_right;
112 if (tmp && rb_is_red(tmp)) {
113 /*
114 * Case 1 - color flips
115 *
116 * G g
117 * / \ / \
118 * p u --> P U
119 * / /
120 * n N
121 *
122 * However, since g's parent might be red, and
123 * 4) does not allow this, we need to recurse
124 * at g.
125 */
126 rb_set_parent_color(tmp, gparent, RB_BLACK);
127 rb_set_parent_color(parent, gparent, RB_BLACK);
128 node = gparent;
129 parent = rb_parent(node);
130 rb_set_parent_color(node, parent, RB_RED);
131 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Michel Lespinasse1f052862012-10-08 16:30:42 -0700134 if (parent->rb_right == node) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700135 /*
136 * Case 2 - left rotate at parent
137 *
138 * G G
139 * / \ / \
140 * p U --> n U
141 * \ /
142 * n p
143 *
144 * This still leaves us in violation of 4), the
145 * continuation into Case 3 will fix that.
146 */
147 parent->rb_right = tmp = node->rb_left;
148 node->rb_left = parent;
149 if (tmp)
150 rb_set_parent_color(tmp, parent,
151 RB_BLACK);
152 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 parent = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700156 /*
157 * Case 3 - right rotate at gparent
158 *
159 * G P
160 * / \ / \
161 * p U --> n g
162 * / \
163 * n U
164 */
165 gparent->rb_left = tmp = parent->rb_right;
166 parent->rb_right = gparent;
167 if (tmp)
168 rb_set_parent_color(tmp, gparent, RB_BLACK);
169 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } else {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700172 tmp = gparent->rb_left;
173 if (tmp && rb_is_red(tmp)) {
174 /* Case 1 - color flips */
175 rb_set_parent_color(tmp, gparent, RB_BLACK);
176 rb_set_parent_color(parent, gparent, RB_BLACK);
177 node = gparent;
178 parent = rb_parent(node);
179 rb_set_parent_color(node, parent, RB_RED);
180 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
Michel Lespinasse1f052862012-10-08 16:30:42 -0700183 if (parent->rb_left == node) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700184 /* Case 2 - right rotate at parent */
185 parent->rb_left = tmp = node->rb_right;
186 node->rb_right = parent;
187 if (tmp)
188 rb_set_parent_color(tmp, parent,
189 RB_BLACK);
190 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 parent = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700194 /* Case 3 - left rotate at gparent */
195 gparent->rb_right = tmp = parent->rb_left;
196 parent->rb_left = gparent;
197 if (tmp)
198 rb_set_parent_color(tmp, gparent, RB_BLACK);
199 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204EXPORT_SYMBOL(rb_insert_color);
205
206static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
207 struct rb_root *root)
208{
Michel Lespinasse6280d232012-10-08 16:30:57 -0700209 struct rb_node *sibling, *tmp1, *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700211 while (true) {
212 /*
213 * Loop invariant: all leaf paths going through node have a
214 * black node count that is 1 lower than other leaf paths.
215 *
216 * If node is red, we can flip it to black to adjust.
217 * If node is the root, all leaf paths go through it.
218 * Otherwise, we need to adjust the tree through color flips
219 * and tree rotations as per one of the 4 cases below.
220 */
221 if (node && rb_is_red(node)) {
Michel Lespinasse6280d232012-10-08 16:30:57 -0700222 rb_set_parent_color(node, parent, RB_BLACK);
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700223 break;
224 } else if (!parent) {
225 break;
226 } else if (parent->rb_left == node) {
Michel Lespinasse6280d232012-10-08 16:30:57 -0700227 sibling = parent->rb_right;
228 if (rb_is_red(sibling)) {
229 /*
230 * Case 1 - left rotate at parent
231 *
232 * P S
233 * / \ / \
234 * N s --> p Sr
235 * / \ / \
236 * Sl Sr N Sl
237 */
238 parent->rb_right = tmp1 = sibling->rb_left;
239 sibling->rb_left = parent;
240 rb_set_parent_color(tmp1, parent, RB_BLACK);
241 __rb_rotate_set_parents(parent, sibling, root,
242 RB_RED);
243 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700245 tmp1 = sibling->rb_right;
246 if (!tmp1 || rb_is_black(tmp1)) {
247 tmp2 = sibling->rb_left;
248 if (!tmp2 || rb_is_black(tmp2)) {
249 /*
250 * Case 2 - sibling color flip
251 * (p could be either color here)
252 *
253 * (p) (p)
254 * / \ / \
255 * N S --> N s
256 * / \ / \
257 * Sl Sr Sl Sr
258 *
259 * This leaves us violating 5), so
260 * recurse at p. If p is red, the
261 * recursion will just flip it to black
262 * and exit. If coming from Case 1,
263 * p is known to be red.
264 */
265 rb_set_parent_color(sibling, parent,
266 RB_RED);
Michel Lespinassee125d142012-10-08 16:30:54 -0700267 node = parent;
268 parent = rb_parent(node);
269 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700271 /*
272 * Case 3 - right rotate at sibling
273 * (p could be either color here)
274 *
275 * (p) (p)
276 * / \ / \
277 * N S --> N Sl
278 * / \ \
279 * sl Sr s
280 * \
281 * Sr
282 */
283 sibling->rb_left = tmp1 = tmp2->rb_right;
284 tmp2->rb_right = sibling;
285 parent->rb_right = tmp2;
286 if (tmp1)
287 rb_set_parent_color(tmp1, sibling,
288 RB_BLACK);
289 tmp1 = sibling;
290 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700292 /*
293 * Case 4 - left rotate at parent + color flips
294 * (p and sl could be either color here.
295 * After rotation, p becomes black, s acquires
296 * p's color, and sl keeps its color)
297 *
298 * (p) (s)
299 * / \ / \
300 * N S --> P Sr
301 * / \ / \
302 * (sl) sr N (sl)
303 */
304 parent->rb_right = tmp2 = sibling->rb_left;
305 sibling->rb_left = parent;
306 rb_set_parent_color(tmp1, sibling, RB_BLACK);
307 if (tmp2)
308 rb_set_parent(tmp2, parent);
309 __rb_rotate_set_parents(parent, sibling, root,
310 RB_BLACK);
Michel Lespinassee125d142012-10-08 16:30:54 -0700311 break;
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700312 } else {
Michel Lespinasse6280d232012-10-08 16:30:57 -0700313 sibling = parent->rb_left;
314 if (rb_is_red(sibling)) {
315 /* Case 1 - right rotate at parent */
316 parent->rb_left = tmp1 = sibling->rb_right;
317 sibling->rb_right = parent;
318 rb_set_parent_color(tmp1, parent, RB_BLACK);
319 __rb_rotate_set_parents(parent, sibling, root,
320 RB_RED);
321 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700323 tmp1 = sibling->rb_left;
324 if (!tmp1 || rb_is_black(tmp1)) {
325 tmp2 = sibling->rb_right;
326 if (!tmp2 || rb_is_black(tmp2)) {
327 /* Case 2 - sibling color flip */
328 rb_set_parent_color(sibling, parent,
329 RB_RED);
Michel Lespinassee125d142012-10-08 16:30:54 -0700330 node = parent;
331 parent = rb_parent(node);
332 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700334 /* Case 3 - right rotate at sibling */
335 sibling->rb_right = tmp1 = tmp2->rb_left;
336 tmp2->rb_left = sibling;
337 parent->rb_left = tmp2;
338 if (tmp1)
339 rb_set_parent_color(tmp1, sibling,
340 RB_BLACK);
341 tmp1 = sibling;
342 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700344 /* Case 4 - left rotate at parent + color flips */
345 parent->rb_left = tmp2 = sibling->rb_right;
346 sibling->rb_right = parent;
347 rb_set_parent_color(tmp1, sibling, RB_BLACK);
348 if (tmp2)
349 rb_set_parent(tmp2, parent);
350 __rb_rotate_set_parents(parent, sibling, root,
351 RB_BLACK);
Michel Lespinassee125d142012-10-08 16:30:54 -0700352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357void rb_erase(struct rb_node *node, struct rb_root *root)
358{
359 struct rb_node *child, *parent;
360 int color;
361
362 if (!node->rb_left)
363 child = node->rb_right;
364 else if (!node->rb_right)
365 child = node->rb_left;
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700366 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct rb_node *old = node, *left;
368
369 node = node->rb_right;
370 while ((left = node->rb_left) != NULL)
371 node = left;
Wolfram Strepp16c047a2009-06-16 15:34:11 -0700372
373 if (rb_parent(old)) {
374 if (rb_parent(old)->rb_left == old)
375 rb_parent(old)->rb_left = node;
376 else
377 rb_parent(old)->rb_right = node;
378 } else
379 root->rb_node = node;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 child = node->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100382 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100383 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David Woodhouse55a98102006-04-21 13:35:51 +0100385 if (parent == old) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 parent = node;
Wolfram Strepp4c601172009-06-16 15:34:12 -0700387 } else {
388 if (child)
389 rb_set_parent(child, parent);
David Woodhouse1975e592006-04-21 13:30:36 +0100390 parent->rb_left = child;
Wolfram Strepp4b324122009-06-16 15:34:13 -0700391
392 node->rb_right = old->rb_right;
393 rb_set_parent(old->rb_right, node);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700394 }
David Woodhouse1975e592006-04-21 13:30:36 +0100395
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -0700396 node->__rb_parent_color = old->__rb_parent_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 node->rb_left = old->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100398 rb_set_parent(old->rb_left, node);
Wolfram Strepp4b324122009-06-16 15:34:13 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 goto color;
401 }
402
David Woodhouse55a98102006-04-21 13:35:51 +0100403 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100404 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if (child)
David Woodhouse55a98102006-04-21 13:35:51 +0100407 rb_set_parent(child, parent);
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700408 if (parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (parent->rb_left == node)
410 parent->rb_left = child;
411 else
412 parent->rb_right = child;
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700413 } else
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200414 root->rb_node = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700416color:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (color == RB_BLACK)
418 __rb_erase_color(child, parent, root);
419}
420EXPORT_SYMBOL(rb_erase);
421
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200422static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
423{
424 struct rb_node *parent;
425
426up:
427 func(node, data);
428 parent = rb_parent(node);
429 if (!parent)
430 return;
431
432 if (node == parent->rb_left && parent->rb_right)
433 func(parent->rb_right, data);
434 else if (parent->rb_left)
435 func(parent->rb_left, data);
436
437 node = parent;
438 goto up;
439}
440
441/*
442 * after inserting @node into the tree, update the tree to account for
443 * both the new entry and any damage done by rebalance
444 */
445void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
446{
447 if (node->rb_left)
448 node = node->rb_left;
449 else if (node->rb_right)
450 node = node->rb_right;
451
452 rb_augment_path(node, func, data);
453}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100454EXPORT_SYMBOL(rb_augment_insert);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200455
456/*
457 * before removing the node, find the deepest node on the rebalance path
458 * that will still be there after @node gets removed
459 */
460struct rb_node *rb_augment_erase_begin(struct rb_node *node)
461{
462 struct rb_node *deepest;
463
464 if (!node->rb_right && !node->rb_left)
465 deepest = rb_parent(node);
466 else if (!node->rb_right)
467 deepest = node->rb_left;
468 else if (!node->rb_left)
469 deepest = node->rb_right;
470 else {
471 deepest = rb_next(node);
472 if (deepest->rb_right)
473 deepest = deepest->rb_right;
474 else if (rb_parent(deepest) != node)
475 deepest = rb_parent(deepest);
476 }
477
478 return deepest;
479}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100480EXPORT_SYMBOL(rb_augment_erase_begin);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200481
482/*
483 * after removal, update the tree to account for the removed entry
484 * and any rebalance damage.
485 */
486void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
487{
488 if (node)
489 rb_augment_path(node, func, data);
490}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100491EXPORT_SYMBOL(rb_augment_erase_end);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/*
494 * This function returns the first node (in sort order) of the tree.
495 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000496struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct rb_node *n;
499
500 n = root->rb_node;
501 if (!n)
502 return NULL;
503 while (n->rb_left)
504 n = n->rb_left;
505 return n;
506}
507EXPORT_SYMBOL(rb_first);
508
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000509struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct rb_node *n;
512
513 n = root->rb_node;
514 if (!n)
515 return NULL;
516 while (n->rb_right)
517 n = n->rb_right;
518 return n;
519}
520EXPORT_SYMBOL(rb_last);
521
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000522struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
David Woodhouse55a98102006-04-21 13:35:51 +0100524 struct rb_node *parent;
525
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700526 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200527 return NULL;
528
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700529 /*
530 * If we have a right-hand child, go down and then left as far
531 * as we can.
532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (node->rb_right) {
534 node = node->rb_right;
535 while (node->rb_left)
536 node=node->rb_left;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000537 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700540 /*
541 * No right-hand children. Everything down and left is smaller than us,
542 * so any 'next' node must be in the general direction of our parent.
543 * Go up the tree; any time the ancestor is a right-hand child of its
544 * parent, keep going up. First time it's a left-hand child of its
545 * parent, said parent is our 'next' node.
546 */
David Woodhouse55a98102006-04-21 13:35:51 +0100547 while ((parent = rb_parent(node)) && node == parent->rb_right)
548 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
David Woodhouse55a98102006-04-21 13:35:51 +0100550 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552EXPORT_SYMBOL(rb_next);
553
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000554struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
David Woodhouse55a98102006-04-21 13:35:51 +0100556 struct rb_node *parent;
557
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700558 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200559 return NULL;
560
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700561 /*
562 * If we have a left-hand child, go down and then right as far
563 * as we can.
564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (node->rb_left) {
566 node = node->rb_left;
567 while (node->rb_right)
568 node=node->rb_right;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000569 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700572 /*
573 * No left-hand children. Go up till we find an ancestor which
574 * is a right-hand child of its parent.
575 */
David Woodhouse55a98102006-04-21 13:35:51 +0100576 while ((parent = rb_parent(node)) && node == parent->rb_left)
577 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
David Woodhouse55a98102006-04-21 13:35:51 +0100579 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581EXPORT_SYMBOL(rb_prev);
582
583void rb_replace_node(struct rb_node *victim, struct rb_node *new,
584 struct rb_root *root)
585{
David Woodhouse55a98102006-04-21 13:35:51 +0100586 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Set the surrounding nodes to point to the replacement */
589 if (parent) {
590 if (victim == parent->rb_left)
591 parent->rb_left = new;
592 else
593 parent->rb_right = new;
594 } else {
595 root->rb_node = new;
596 }
597 if (victim->rb_left)
David Woodhouse55a98102006-04-21 13:35:51 +0100598 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100600 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Copy the pointers/colour from the victim to the replacement */
603 *new = *victim;
604}
605EXPORT_SYMBOL(rb_replace_node);