blob: 41cf19b2fe51e6f59044ac43e5e04748e1e2642a [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
42 * nodes will be lowercase.
43 */
44
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070045#define RB_RED 0
46#define RB_BLACK 1
47
48#define rb_color(r) ((r)->__rb_parent_color & 1)
49#define rb_is_red(r) (!rb_color(r))
50#define rb_is_black(r) rb_color(r)
51#define rb_set_red(r) do { (r)->__rb_parent_color &= ~1; } while (0)
52#define rb_set_black(r) do { (r)->__rb_parent_color |= 1; } while (0)
53
54static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
55{
56 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
57}
58static inline void rb_set_color(struct rb_node *rb, int color)
59{
60 rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color;
61}
62
Michel Lespinasse5bc91882012-10-08 16:30:47 -070063static inline void rb_set_parent_color(struct rb_node *rb,
64 struct rb_node *p, int color)
65{
66 rb->__rb_parent_color = (unsigned long)p | color;
67}
68
69static inline struct rb_node *rb_red_parent(struct rb_node *red)
70{
71 return (struct rb_node *)red->__rb_parent_color;
72}
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
75{
76 struct rb_node *right = node->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +010077 struct rb_node *parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 if ((node->rb_right = right->rb_left))
David Woodhouse55a98102006-04-21 13:35:51 +010080 rb_set_parent(right->rb_left, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 right->rb_left = node;
82
David Woodhouse55a98102006-04-21 13:35:51 +010083 rb_set_parent(right, parent);
84
85 if (parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 {
David Woodhouse55a98102006-04-21 13:35:51 +010087 if (node == parent->rb_left)
88 parent->rb_left = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 else
David Woodhouse55a98102006-04-21 13:35:51 +010090 parent->rb_right = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92 else
93 root->rb_node = right;
David Woodhouse55a98102006-04-21 13:35:51 +010094 rb_set_parent(node, right);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
97static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
98{
99 struct rb_node *left = node->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100100 struct rb_node *parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if ((node->rb_left = left->rb_right))
David Woodhouse55a98102006-04-21 13:35:51 +0100103 rb_set_parent(left->rb_right, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 left->rb_right = node;
105
David Woodhouse55a98102006-04-21 13:35:51 +0100106 rb_set_parent(left, parent);
107
108 if (parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 {
David Woodhouse55a98102006-04-21 13:35:51 +0100110 if (node == parent->rb_right)
111 parent->rb_right = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 else
David Woodhouse55a98102006-04-21 13:35:51 +0100113 parent->rb_left = left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115 else
116 root->rb_node = left;
David Woodhouse55a98102006-04-21 13:35:51 +0100117 rb_set_parent(node, left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700120/*
121 * Helper function for rotations:
122 * - old's parent and color get assigned to new
123 * - old gets assigned new as a parent and 'color' as a color.
124 */
125static inline void
126__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
127 struct rb_root *root, int color)
128{
129 struct rb_node *parent = rb_parent(old);
130 new->__rb_parent_color = old->__rb_parent_color;
131 rb_set_parent_color(old, new, color);
132 if (parent) {
133 if (parent->rb_left == old)
134 parent->rb_left = new;
135 else
136 parent->rb_right = new;
137 } else
138 root->rb_node = new;
139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141void rb_insert_color(struct rb_node *node, struct rb_root *root)
142{
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700143 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Michel Lespinasse6d584522012-10-08 16:30:44 -0700145 while (true) {
146 /*
147 * Loop invariant: node is red
148 *
149 * If there is a black parent, we are done.
150 * Otherwise, take some corrective action as we don't
151 * want a red root or two consecutive red nodes.
152 */
Michel Lespinasse6d584522012-10-08 16:30:44 -0700153 if (!parent) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700154 rb_set_parent_color(node, NULL, RB_BLACK);
Michel Lespinasse6d584522012-10-08 16:30:44 -0700155 break;
156 } else if (rb_is_black(parent))
157 break;
158
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700159 gparent = rb_red_parent(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700161 if (parent == gparent->rb_left) {
162 tmp = gparent->rb_right;
163 if (tmp && rb_is_red(tmp)) {
164 /*
165 * Case 1 - color flips
166 *
167 * G g
168 * / \ / \
169 * p u --> P U
170 * / /
171 * n N
172 *
173 * However, since g's parent might be red, and
174 * 4) does not allow this, we need to recurse
175 * at g.
176 */
177 rb_set_parent_color(tmp, gparent, RB_BLACK);
178 rb_set_parent_color(parent, gparent, RB_BLACK);
179 node = gparent;
180 parent = rb_parent(node);
181 rb_set_parent_color(node, parent, RB_RED);
182 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
184
Michel Lespinasse1f052862012-10-08 16:30:42 -0700185 if (parent->rb_right == node) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700186 /*
187 * Case 2 - left rotate at parent
188 *
189 * G G
190 * / \ / \
191 * p U --> n U
192 * \ /
193 * n p
194 *
195 * This still leaves us in violation of 4), the
196 * continuation into Case 3 will fix that.
197 */
198 parent->rb_right = tmp = node->rb_left;
199 node->rb_left = parent;
200 if (tmp)
201 rb_set_parent_color(tmp, parent,
202 RB_BLACK);
203 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 parent = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700207 /*
208 * Case 3 - right rotate at gparent
209 *
210 * G P
211 * / \ / \
212 * p U --> n g
213 * / \
214 * n U
215 */
216 gparent->rb_left = tmp = parent->rb_right;
217 parent->rb_right = gparent;
218 if (tmp)
219 rb_set_parent_color(tmp, gparent, RB_BLACK);
220 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 } else {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700223 tmp = gparent->rb_left;
224 if (tmp && rb_is_red(tmp)) {
225 /* Case 1 - color flips */
226 rb_set_parent_color(tmp, gparent, RB_BLACK);
227 rb_set_parent_color(parent, gparent, RB_BLACK);
228 node = gparent;
229 parent = rb_parent(node);
230 rb_set_parent_color(node, parent, RB_RED);
231 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Michel Lespinasse1f052862012-10-08 16:30:42 -0700234 if (parent->rb_left == node) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700235 /* Case 2 - right rotate at parent */
236 parent->rb_left = tmp = node->rb_right;
237 node->rb_right = parent;
238 if (tmp)
239 rb_set_parent_color(tmp, parent,
240 RB_BLACK);
241 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 parent = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700245 /* Case 3 - left rotate at gparent */
246 gparent->rb_right = tmp = parent->rb_left;
247 parent->rb_left = gparent;
248 if (tmp)
249 rb_set_parent_color(tmp, gparent, RB_BLACK);
250 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255EXPORT_SYMBOL(rb_insert_color);
256
257static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
258 struct rb_root *root)
259{
260 struct rb_node *other;
261
David Woodhouse55a98102006-04-21 13:35:51 +0100262 while ((!node || rb_is_black(node)) && node != root->rb_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 {
264 if (parent->rb_left == node)
265 {
266 other = parent->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100267 if (rb_is_red(other))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 {
David Woodhouse55a98102006-04-21 13:35:51 +0100269 rb_set_black(other);
270 rb_set_red(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 __rb_rotate_left(parent, root);
272 other = parent->rb_right;
273 }
David Woodhouse55a98102006-04-21 13:35:51 +0100274 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
275 (!other->rb_right || rb_is_black(other->rb_right)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 {
David Woodhouse55a98102006-04-21 13:35:51 +0100277 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 node = parent;
David Woodhouse55a98102006-04-21 13:35:51 +0100279 parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 else
282 {
David Woodhouse55a98102006-04-21 13:35:51 +0100283 if (!other->rb_right || rb_is_black(other->rb_right))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 {
Wolfram Strepp55a63992009-03-31 15:23:45 -0700285 rb_set_black(other->rb_left);
David Woodhouse55a98102006-04-21 13:35:51 +0100286 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 __rb_rotate_right(other, root);
288 other = parent->rb_right;
289 }
David Woodhouse2f3243a2006-06-05 20:19:05 +0100290 rb_set_color(other, rb_color(parent));
David Woodhouse55a98102006-04-21 13:35:51 +0100291 rb_set_black(parent);
Wolfram Strepp55a63992009-03-31 15:23:45 -0700292 rb_set_black(other->rb_right);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 __rb_rotate_left(parent, root);
294 node = root->rb_node;
295 break;
296 }
297 }
298 else
299 {
300 other = parent->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100301 if (rb_is_red(other))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 {
David Woodhouse55a98102006-04-21 13:35:51 +0100303 rb_set_black(other);
304 rb_set_red(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 __rb_rotate_right(parent, root);
306 other = parent->rb_left;
307 }
David Woodhouse55a98102006-04-21 13:35:51 +0100308 if ((!other->rb_left || rb_is_black(other->rb_left)) &&
309 (!other->rb_right || rb_is_black(other->rb_right)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 {
David Woodhouse55a98102006-04-21 13:35:51 +0100311 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 node = parent;
David Woodhouse55a98102006-04-21 13:35:51 +0100313 parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 else
316 {
David Woodhouse55a98102006-04-21 13:35:51 +0100317 if (!other->rb_left || rb_is_black(other->rb_left))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 {
Wolfram Strepp55a63992009-03-31 15:23:45 -0700319 rb_set_black(other->rb_right);
David Woodhouse55a98102006-04-21 13:35:51 +0100320 rb_set_red(other);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 __rb_rotate_left(other, root);
322 other = parent->rb_left;
323 }
David Woodhouse2f3243a2006-06-05 20:19:05 +0100324 rb_set_color(other, rb_color(parent));
David Woodhouse55a98102006-04-21 13:35:51 +0100325 rb_set_black(parent);
Wolfram Strepp55a63992009-03-31 15:23:45 -0700326 rb_set_black(other->rb_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 __rb_rotate_right(parent, root);
328 node = root->rb_node;
329 break;
330 }
331 }
332 }
333 if (node)
David Woodhouse55a98102006-04-21 13:35:51 +0100334 rb_set_black(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337void rb_erase(struct rb_node *node, struct rb_root *root)
338{
339 struct rb_node *child, *parent;
340 int color;
341
342 if (!node->rb_left)
343 child = node->rb_right;
344 else if (!node->rb_right)
345 child = node->rb_left;
346 else
347 {
348 struct rb_node *old = node, *left;
349
350 node = node->rb_right;
351 while ((left = node->rb_left) != NULL)
352 node = left;
Wolfram Strepp16c047a2009-06-16 15:34:11 -0700353
354 if (rb_parent(old)) {
355 if (rb_parent(old)->rb_left == old)
356 rb_parent(old)->rb_left = node;
357 else
358 rb_parent(old)->rb_right = node;
359 } else
360 root->rb_node = node;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 child = node->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100363 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100364 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
David Woodhouse55a98102006-04-21 13:35:51 +0100366 if (parent == old) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 parent = node;
Wolfram Strepp4c601172009-06-16 15:34:12 -0700368 } else {
369 if (child)
370 rb_set_parent(child, parent);
David Woodhouse1975e592006-04-21 13:30:36 +0100371 parent->rb_left = child;
Wolfram Strepp4b324122009-06-16 15:34:13 -0700372
373 node->rb_right = old->rb_right;
374 rb_set_parent(old->rb_right, node);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700375 }
David Woodhouse1975e592006-04-21 13:30:36 +0100376
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -0700377 node->__rb_parent_color = old->__rb_parent_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 node->rb_left = old->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100379 rb_set_parent(old->rb_left, node);
Wolfram Strepp4b324122009-06-16 15:34:13 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto color;
382 }
383
David Woodhouse55a98102006-04-21 13:35:51 +0100384 parent = rb_parent(node);
David Woodhouse2f3243a2006-06-05 20:19:05 +0100385 color = rb_color(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (child)
David Woodhouse55a98102006-04-21 13:35:51 +0100388 rb_set_parent(child, parent);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200389 if (parent)
390 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (parent->rb_left == node)
392 parent->rb_left = child;
393 else
394 parent->rb_right = child;
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800395 }
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200396 else
397 root->rb_node = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 color:
400 if (color == RB_BLACK)
401 __rb_erase_color(child, parent, root);
402}
403EXPORT_SYMBOL(rb_erase);
404
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200405static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
406{
407 struct rb_node *parent;
408
409up:
410 func(node, data);
411 parent = rb_parent(node);
412 if (!parent)
413 return;
414
415 if (node == parent->rb_left && parent->rb_right)
416 func(parent->rb_right, data);
417 else if (parent->rb_left)
418 func(parent->rb_left, data);
419
420 node = parent;
421 goto up;
422}
423
424/*
425 * after inserting @node into the tree, update the tree to account for
426 * both the new entry and any damage done by rebalance
427 */
428void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
429{
430 if (node->rb_left)
431 node = node->rb_left;
432 else if (node->rb_right)
433 node = node->rb_right;
434
435 rb_augment_path(node, func, data);
436}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100437EXPORT_SYMBOL(rb_augment_insert);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200438
439/*
440 * before removing the node, find the deepest node on the rebalance path
441 * that will still be there after @node gets removed
442 */
443struct rb_node *rb_augment_erase_begin(struct rb_node *node)
444{
445 struct rb_node *deepest;
446
447 if (!node->rb_right && !node->rb_left)
448 deepest = rb_parent(node);
449 else if (!node->rb_right)
450 deepest = node->rb_left;
451 else if (!node->rb_left)
452 deepest = node->rb_right;
453 else {
454 deepest = rb_next(node);
455 if (deepest->rb_right)
456 deepest = deepest->rb_right;
457 else if (rb_parent(deepest) != node)
458 deepest = rb_parent(deepest);
459 }
460
461 return deepest;
462}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100463EXPORT_SYMBOL(rb_augment_erase_begin);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200464
465/*
466 * after removal, update the tree to account for the removed entry
467 * and any rebalance damage.
468 */
469void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
470{
471 if (node)
472 rb_augment_path(node, func, data);
473}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100474EXPORT_SYMBOL(rb_augment_erase_end);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/*
477 * This function returns the first node (in sort order) of the tree.
478 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000479struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct rb_node *n;
482
483 n = root->rb_node;
484 if (!n)
485 return NULL;
486 while (n->rb_left)
487 n = n->rb_left;
488 return n;
489}
490EXPORT_SYMBOL(rb_first);
491
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000492struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct rb_node *n;
495
496 n = root->rb_node;
497 if (!n)
498 return NULL;
499 while (n->rb_right)
500 n = n->rb_right;
501 return n;
502}
503EXPORT_SYMBOL(rb_last);
504
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000505struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
David Woodhouse55a98102006-04-21 13:35:51 +0100507 struct rb_node *parent;
508
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700509 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200510 return NULL;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* If we have a right-hand child, go down and then left as far
513 as we can. */
514 if (node->rb_right) {
515 node = node->rb_right;
516 while (node->rb_left)
517 node=node->rb_left;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000518 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 /* No right-hand children. Everything down and left is
522 smaller than us, so any 'next' node must be in the general
523 direction of our parent. Go up the tree; any time the
524 ancestor is a right-hand child of its parent, keep going
525 up. First time it's a left-hand child of its parent, said
526 parent is our 'next' node. */
David Woodhouse55a98102006-04-21 13:35:51 +0100527 while ((parent = rb_parent(node)) && node == parent->rb_right)
528 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
David Woodhouse55a98102006-04-21 13:35:51 +0100530 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532EXPORT_SYMBOL(rb_next);
533
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000534struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
David Woodhouse55a98102006-04-21 13:35:51 +0100536 struct rb_node *parent;
537
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700538 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200539 return NULL;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* If we have a left-hand child, go down and then right as far
542 as we can. */
543 if (node->rb_left) {
544 node = node->rb_left;
545 while (node->rb_right)
546 node=node->rb_right;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000547 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 /* No left-hand children. Go up till we find an ancestor which
551 is a right-hand child of its parent */
David Woodhouse55a98102006-04-21 13:35:51 +0100552 while ((parent = rb_parent(node)) && node == parent->rb_left)
553 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
David Woodhouse55a98102006-04-21 13:35:51 +0100555 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557EXPORT_SYMBOL(rb_prev);
558
559void rb_replace_node(struct rb_node *victim, struct rb_node *new,
560 struct rb_root *root)
561{
David Woodhouse55a98102006-04-21 13:35:51 +0100562 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Set the surrounding nodes to point to the replacement */
565 if (parent) {
566 if (victim == parent->rb_left)
567 parent->rb_left = new;
568 else
569 parent->rb_right = new;
570 } else {
571 root->rb_node = new;
572 }
573 if (victim->rb_left)
David Woodhouse55a98102006-04-21 13:35:51 +0100574 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100576 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* Copy the pointers/colour from the victim to the replacement */
579 *new = *victim;
580}
581EXPORT_SYMBOL(rb_replace_node);