blob: 80b092538fa94bc1e3d93a803e05497c6f299ebf [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>
Michel Lespinasse46b61352012-10-08 16:31:11 -07005 (C) 2012 Michel Lespinasse <walken@google.com>
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 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>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050025#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Michel Lespinasse5bc91882012-10-08 16:30:47 -070027/*
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
Michel Lespinasse6280d232012-10-08 16:30:57 -070043 * nodes will be lowercase. Unknown color nodes shall be drawn as red within
44 * parentheses and have some accompanying text comment.
Michel Lespinasse5bc91882012-10-08 16:30:47 -070045 */
46
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070047#define RB_RED 0
48#define RB_BLACK 1
49
50#define rb_color(r) ((r)->__rb_parent_color & 1)
51#define rb_is_red(r) (!rb_color(r))
52#define rb_is_black(r) rb_color(r)
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070053
Michel Lespinasse46b61352012-10-08 16:31:11 -070054static inline void rb_set_black(struct rb_node *rb)
55{
56 rb->__rb_parent_color |= RB_BLACK;
57}
58
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070059static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
60{
61 rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
62}
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070063
Michel Lespinasse5bc91882012-10-08 16:30:47 -070064static inline void rb_set_parent_color(struct rb_node *rb,
65 struct rb_node *p, int color)
66{
67 rb->__rb_parent_color = (unsigned long)p | color;
68}
69
70static inline struct rb_node *rb_red_parent(struct rb_node *red)
71{
72 return (struct rb_node *)red->__rb_parent_color;
73}
74
Michel Lespinasse7abc7042012-10-08 16:31:07 -070075static inline void
76__rb_change_child(struct rb_node *old, struct rb_node *new,
77 struct rb_node *parent, struct rb_root *root)
78{
79 if (parent) {
80 if (parent->rb_left == old)
81 parent->rb_left = new;
82 else
83 parent->rb_right = new;
84 } else
85 root->rb_node = new;
86}
87
Michel Lespinasse5bc91882012-10-08 16:30:47 -070088/*
89 * Helper function for rotations:
90 * - old's parent and color get assigned to new
91 * - old gets assigned new as a parent and 'color' as a color.
92 */
93static inline void
94__rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
95 struct rb_root *root, int color)
96{
97 struct rb_node *parent = rb_parent(old);
98 new->__rb_parent_color = old->__rb_parent_color;
99 rb_set_parent_color(old, new, color);
Michel Lespinasse7abc7042012-10-08 16:31:07 -0700100 __rb_change_child(old, new, parent, root);
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103void rb_insert_color(struct rb_node *node, struct rb_root *root)
104{
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700105 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Michel Lespinasse6d584522012-10-08 16:30:44 -0700107 while (true) {
108 /*
109 * Loop invariant: node is red
110 *
111 * If there is a black parent, we are done.
112 * Otherwise, take some corrective action as we don't
113 * want a red root or two consecutive red nodes.
114 */
Michel Lespinasse6d584522012-10-08 16:30:44 -0700115 if (!parent) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700116 rb_set_parent_color(node, NULL, RB_BLACK);
Michel Lespinasse6d584522012-10-08 16:30:44 -0700117 break;
118 } else if (rb_is_black(parent))
119 break;
120
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700121 gparent = rb_red_parent(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700123 tmp = gparent->rb_right;
124 if (parent != tmp) { /* parent == gparent->rb_left */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700125 if (tmp && rb_is_red(tmp)) {
126 /*
127 * Case 1 - color flips
128 *
129 * G g
130 * / \ / \
131 * p u --> P U
132 * / /
133 * n N
134 *
135 * However, since g's parent might be red, and
136 * 4) does not allow this, we need to recurse
137 * at g.
138 */
139 rb_set_parent_color(tmp, gparent, RB_BLACK);
140 rb_set_parent_color(parent, gparent, RB_BLACK);
141 node = gparent;
142 parent = rb_parent(node);
143 rb_set_parent_color(node, parent, RB_RED);
144 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700147 tmp = parent->rb_right;
148 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700149 /*
150 * Case 2 - left rotate at parent
151 *
152 * G G
153 * / \ / \
154 * p U --> n U
155 * \ /
156 * n p
157 *
158 * This still leaves us in violation of 4), the
159 * continuation into Case 3 will fix that.
160 */
161 parent->rb_right = tmp = node->rb_left;
162 node->rb_left = parent;
163 if (tmp)
164 rb_set_parent_color(tmp, parent,
165 RB_BLACK);
166 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700168 tmp = node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700171 /*
172 * Case 3 - right rotate at gparent
173 *
174 * G P
175 * / \ / \
176 * p U --> n g
177 * / \
178 * n U
179 */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700180 gparent->rb_left = tmp; /* == parent->rb_right */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700181 parent->rb_right = gparent;
182 if (tmp)
183 rb_set_parent_color(tmp, gparent, RB_BLACK);
184 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 } else {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700187 tmp = gparent->rb_left;
188 if (tmp && rb_is_red(tmp)) {
189 /* Case 1 - color flips */
190 rb_set_parent_color(tmp, gparent, RB_BLACK);
191 rb_set_parent_color(parent, gparent, RB_BLACK);
192 node = gparent;
193 parent = rb_parent(node);
194 rb_set_parent_color(node, parent, RB_RED);
195 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700198 tmp = parent->rb_left;
199 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700200 /* Case 2 - right rotate at parent */
201 parent->rb_left = tmp = node->rb_right;
202 node->rb_right = parent;
203 if (tmp)
204 rb_set_parent_color(tmp, parent,
205 RB_BLACK);
206 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700208 tmp = node->rb_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700211 /* Case 3 - left rotate at gparent */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700212 gparent->rb_right = tmp; /* == parent->rb_left */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700213 parent->rb_left = gparent;
214 if (tmp)
215 rb_set_parent_color(tmp, gparent, RB_BLACK);
216 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221EXPORT_SYMBOL(rb_insert_color);
222
Michel Lespinasse46b61352012-10-08 16:31:11 -0700223static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Michel Lespinasse46b61352012-10-08 16:31:11 -0700225 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700227 while (true) {
228 /*
Michel Lespinasse46b61352012-10-08 16:31:11 -0700229 * Loop invariants:
230 * - node is black (or NULL on first iteration)
231 * - node is not the root (parent is not NULL)
232 * - All leaf paths going through parent and node have a
233 * black node count that is 1 lower than other leaf paths.
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700234 */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700235 sibling = parent->rb_right;
236 if (node != sibling) { /* node == parent->rb_left */
Michel Lespinasse6280d232012-10-08 16:30:57 -0700237 if (rb_is_red(sibling)) {
238 /*
239 * Case 1 - left rotate at parent
240 *
241 * P S
242 * / \ / \
243 * N s --> p Sr
244 * / \ / \
245 * Sl Sr N Sl
246 */
247 parent->rb_right = tmp1 = sibling->rb_left;
248 sibling->rb_left = parent;
249 rb_set_parent_color(tmp1, parent, RB_BLACK);
250 __rb_rotate_set_parents(parent, sibling, root,
251 RB_RED);
252 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700254 tmp1 = sibling->rb_right;
255 if (!tmp1 || rb_is_black(tmp1)) {
256 tmp2 = sibling->rb_left;
257 if (!tmp2 || rb_is_black(tmp2)) {
258 /*
259 * Case 2 - sibling color flip
260 * (p could be either color here)
261 *
262 * (p) (p)
263 * / \ / \
264 * N S --> N s
265 * / \ / \
266 * Sl Sr Sl Sr
267 *
Michel Lespinasse46b61352012-10-08 16:31:11 -0700268 * This leaves us violating 5) which
269 * can be fixed by flipping p to black
270 * if it was red, or by recursing at p.
271 * p is red when coming from Case 1.
Michel Lespinasse6280d232012-10-08 16:30:57 -0700272 */
273 rb_set_parent_color(sibling, parent,
274 RB_RED);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700275 if (rb_is_red(parent))
276 rb_set_black(parent);
277 else {
278 node = parent;
279 parent = rb_parent(node);
280 if (parent)
281 continue;
282 }
283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700285 /*
286 * Case 3 - right rotate at sibling
287 * (p could be either color here)
288 *
289 * (p) (p)
290 * / \ / \
291 * N S --> N Sl
292 * / \ \
293 * sl Sr s
294 * \
295 * Sr
296 */
297 sibling->rb_left = tmp1 = tmp2->rb_right;
298 tmp2->rb_right = sibling;
299 parent->rb_right = tmp2;
300 if (tmp1)
301 rb_set_parent_color(tmp1, sibling,
302 RB_BLACK);
303 tmp1 = sibling;
304 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700306 /*
307 * Case 4 - left rotate at parent + color flips
308 * (p and sl could be either color here.
309 * After rotation, p becomes black, s acquires
310 * p's color, and sl keeps its color)
311 *
312 * (p) (s)
313 * / \ / \
314 * N S --> P Sr
315 * / \ / \
316 * (sl) sr N (sl)
317 */
318 parent->rb_right = tmp2 = sibling->rb_left;
319 sibling->rb_left = parent;
320 rb_set_parent_color(tmp1, sibling, RB_BLACK);
321 if (tmp2)
322 rb_set_parent(tmp2, parent);
323 __rb_rotate_set_parents(parent, sibling, root,
324 RB_BLACK);
Michel Lespinassee125d142012-10-08 16:30:54 -0700325 break;
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700326 } else {
Michel Lespinasse6280d232012-10-08 16:30:57 -0700327 sibling = parent->rb_left;
328 if (rb_is_red(sibling)) {
329 /* Case 1 - right rotate at parent */
330 parent->rb_left = tmp1 = sibling->rb_right;
331 sibling->rb_right = parent;
332 rb_set_parent_color(tmp1, parent, RB_BLACK);
333 __rb_rotate_set_parents(parent, sibling, root,
334 RB_RED);
335 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700337 tmp1 = sibling->rb_left;
338 if (!tmp1 || rb_is_black(tmp1)) {
339 tmp2 = sibling->rb_right;
340 if (!tmp2 || rb_is_black(tmp2)) {
341 /* Case 2 - sibling color flip */
342 rb_set_parent_color(sibling, parent,
343 RB_RED);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700344 if (rb_is_red(parent))
345 rb_set_black(parent);
346 else {
347 node = parent;
348 parent = rb_parent(node);
349 if (parent)
350 continue;
351 }
352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700354 /* Case 3 - right rotate at sibling */
355 sibling->rb_right = tmp1 = tmp2->rb_left;
356 tmp2->rb_left = sibling;
357 parent->rb_left = tmp2;
358 if (tmp1)
359 rb_set_parent_color(tmp1, sibling,
360 RB_BLACK);
361 tmp1 = sibling;
362 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700364 /* Case 4 - left rotate at parent + color flips */
365 parent->rb_left = tmp2 = sibling->rb_right;
366 sibling->rb_right = parent;
367 rb_set_parent_color(tmp1, sibling, RB_BLACK);
368 if (tmp2)
369 rb_set_parent(tmp2, parent);
370 __rb_rotate_set_parents(parent, sibling, root,
371 RB_BLACK);
Michel Lespinassee125d142012-10-08 16:30:54 -0700372 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377void rb_erase(struct rb_node *node, struct rb_root *root)
378{
Michel Lespinasse60670b82012-10-08 16:31:10 -0700379 struct rb_node *child = node->rb_right, *tmp = node->rb_left;
Michel Lespinasse46b61352012-10-08 16:31:11 -0700380 struct rb_node *parent, *rebalance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Michel Lespinasse60670b82012-10-08 16:31:10 -0700382 if (!tmp) {
Michel Lespinasse46b61352012-10-08 16:31:11 -0700383 /*
384 * Case 1: node to erase has no more than 1 child (easy!)
385 *
386 * Note that if there is one child it must be red due to 5)
387 * and node must be black due to 4). We adjust colors locally
388 * so as to bypass __rb_erase_color() later on.
389 */
Michel Lespinasse60670b82012-10-08 16:31:10 -0700390
391 parent = rb_parent(node);
Michel Lespinasse60670b82012-10-08 16:31:10 -0700392 __rb_change_child(node, child, parent, root);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700393 if (child) {
394 rb_set_parent_color(child, parent, RB_BLACK);
395 rebalance = NULL;
396 } else {
397 rebalance = rb_is_black(node) ? parent : NULL;
398 }
Michel Lespinasse60670b82012-10-08 16:31:10 -0700399 } else if (!child) {
400 /* Still case 1, but this time the child is node->rb_left */
Michel Lespinasse46b61352012-10-08 16:31:11 -0700401 parent = rb_parent(node);
402 __rb_change_child(node, tmp, parent, root);
403 rb_set_parent_color(tmp, parent, RB_BLACK);
404 rebalance = NULL;
Michel Lespinasse60670b82012-10-08 16:31:10 -0700405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct rb_node *old = node, *left;
407
Michel Lespinasse60670b82012-10-08 16:31:10 -0700408 node = child;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 while ((left = node->rb_left) != NULL)
410 node = left;
Wolfram Strepp16c047a2009-06-16 15:34:11 -0700411
Michel Lespinasse7abc7042012-10-08 16:31:07 -0700412 __rb_change_child(old, node, rb_parent(old), root);
Wolfram Strepp16c047a2009-06-16 15:34:11 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 child = node->rb_right;
David Woodhouse55a98102006-04-21 13:35:51 +0100415 parent = rb_parent(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Woodhouse55a98102006-04-21 13:35:51 +0100417 if (parent == old) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 parent = node;
Wolfram Strepp4c601172009-06-16 15:34:12 -0700419 } else {
David Woodhouse1975e592006-04-21 13:30:36 +0100420 parent->rb_left = child;
Wolfram Strepp4b324122009-06-16 15:34:13 -0700421
422 node->rb_right = old->rb_right;
423 rb_set_parent(old->rb_right, node);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700424 }
David Woodhouse1975e592006-04-21 13:30:36 +0100425
Michel Lespinasse46b61352012-10-08 16:31:11 -0700426 if (child) {
427 rb_set_parent_color(child, parent, RB_BLACK);
428 rebalance = NULL;
429 } else {
430 rebalance = rb_is_black(node) ? parent : NULL;
431 }
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -0700432 node->__rb_parent_color = old->__rb_parent_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 node->rb_left = old->rb_left;
David Woodhouse55a98102006-04-21 13:35:51 +0100434 rb_set_parent(old->rb_left, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Michel Lespinasse46b61352012-10-08 16:31:11 -0700437 if (rebalance)
438 __rb_erase_color(rebalance, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440EXPORT_SYMBOL(rb_erase);
441
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200442static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
443{
444 struct rb_node *parent;
445
446up:
447 func(node, data);
448 parent = rb_parent(node);
449 if (!parent)
450 return;
451
452 if (node == parent->rb_left && parent->rb_right)
453 func(parent->rb_right, data);
454 else if (parent->rb_left)
455 func(parent->rb_left, data);
456
457 node = parent;
458 goto up;
459}
460
461/*
462 * after inserting @node into the tree, update the tree to account for
463 * both the new entry and any damage done by rebalance
464 */
465void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
466{
467 if (node->rb_left)
468 node = node->rb_left;
469 else if (node->rb_right)
470 node = node->rb_right;
471
472 rb_augment_path(node, func, data);
473}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100474EXPORT_SYMBOL(rb_augment_insert);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200475
476/*
477 * before removing the node, find the deepest node on the rebalance path
478 * that will still be there after @node gets removed
479 */
480struct rb_node *rb_augment_erase_begin(struct rb_node *node)
481{
482 struct rb_node *deepest;
483
484 if (!node->rb_right && !node->rb_left)
485 deepest = rb_parent(node);
486 else if (!node->rb_right)
487 deepest = node->rb_left;
488 else if (!node->rb_left)
489 deepest = node->rb_right;
490 else {
491 deepest = rb_next(node);
492 if (deepest->rb_right)
493 deepest = deepest->rb_right;
494 else if (rb_parent(deepest) != node)
495 deepest = rb_parent(deepest);
496 }
497
498 return deepest;
499}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100500EXPORT_SYMBOL(rb_augment_erase_begin);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200501
502/*
503 * after removal, update the tree to account for the removed entry
504 * and any rebalance damage.
505 */
506void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
507{
508 if (node)
509 rb_augment_path(node, func, data);
510}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100511EXPORT_SYMBOL(rb_augment_erase_end);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/*
514 * This function returns the first node (in sort order) of the tree.
515 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000516struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct rb_node *n;
519
520 n = root->rb_node;
521 if (!n)
522 return NULL;
523 while (n->rb_left)
524 n = n->rb_left;
525 return n;
526}
527EXPORT_SYMBOL(rb_first);
528
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000529struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct rb_node *n;
532
533 n = root->rb_node;
534 if (!n)
535 return NULL;
536 while (n->rb_right)
537 n = n->rb_right;
538 return n;
539}
540EXPORT_SYMBOL(rb_last);
541
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000542struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
David Woodhouse55a98102006-04-21 13:35:51 +0100544 struct rb_node *parent;
545
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700546 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200547 return NULL;
548
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700549 /*
550 * If we have a right-hand child, go down and then left as far
551 * as we can.
552 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (node->rb_right) {
554 node = node->rb_right;
555 while (node->rb_left)
556 node=node->rb_left;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000557 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700560 /*
561 * No right-hand children. Everything down and left is smaller than us,
562 * so any 'next' node must be in the general direction of our parent.
563 * Go up the tree; any time the ancestor is a right-hand child of its
564 * parent, keep going up. First time it's a left-hand child of its
565 * parent, said parent is our 'next' node.
566 */
David Woodhouse55a98102006-04-21 13:35:51 +0100567 while ((parent = rb_parent(node)) && node == parent->rb_right)
568 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
David Woodhouse55a98102006-04-21 13:35:51 +0100570 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572EXPORT_SYMBOL(rb_next);
573
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000574struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
David Woodhouse55a98102006-04-21 13:35:51 +0100576 struct rb_node *parent;
577
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700578 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200579 return NULL;
580
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700581 /*
582 * If we have a left-hand child, go down and then right as far
583 * as we can.
584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (node->rb_left) {
586 node = node->rb_left;
587 while (node->rb_right)
588 node=node->rb_right;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000589 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700592 /*
593 * No left-hand children. Go up till we find an ancestor which
594 * is a right-hand child of its parent.
595 */
David Woodhouse55a98102006-04-21 13:35:51 +0100596 while ((parent = rb_parent(node)) && node == parent->rb_left)
597 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
David Woodhouse55a98102006-04-21 13:35:51 +0100599 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601EXPORT_SYMBOL(rb_prev);
602
603void rb_replace_node(struct rb_node *victim, struct rb_node *new,
604 struct rb_root *root)
605{
David Woodhouse55a98102006-04-21 13:35:51 +0100606 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* Set the surrounding nodes to point to the replacement */
Michel Lespinasse7abc7042012-10-08 16:31:07 -0700609 __rb_change_child(victim, new, parent, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (victim->rb_left)
David Woodhouse55a98102006-04-21 13:35:51 +0100611 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100613 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 /* Copy the pointers/colour from the victim to the replacement */
616 *new = *victim;
617}
618EXPORT_SYMBOL(rb_replace_node);