blob: 938061ecbe61d08c09654d0ec9e6740630c6e68c [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
Michel Lespinasse4f035ad2012-10-08 16:31:13 -070050#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)
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070058
Michel Lespinasse46b61352012-10-08 16:31:11 -070059static inline void rb_set_black(struct rb_node *rb)
60{
61 rb->__rb_parent_color |= RB_BLACK;
62}
63
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070064static 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}
Michel Lespinassebf7ad8e2012-10-08 16:30:37 -070068
Michel Lespinasse5bc91882012-10-08 16:30:47 -070069static 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
Michel Lespinasse7abc7042012-10-08 16:31:07 -070080static 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
Michel Lespinasse5bc91882012-10-08 16:30:47 -070093/*
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);
Michel Lespinasse7abc7042012-10-08 16:31:07 -0700105 __rb_change_child(old, new, parent, root);
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108void rb_insert_color(struct rb_node *node, struct rb_root *root)
109{
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700110 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Michel Lespinasse6d584522012-10-08 16:30:44 -0700112 while (true) {
113 /*
114 * Loop invariant: node is red
115 *
116 * If there is a black parent, we are done.
117 * Otherwise, take some corrective action as we don't
118 * want a red root or two consecutive red nodes.
119 */
Michel Lespinasse6d584522012-10-08 16:30:44 -0700120 if (!parent) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700121 rb_set_parent_color(node, NULL, RB_BLACK);
Michel Lespinasse6d584522012-10-08 16:30:44 -0700122 break;
123 } else if (rb_is_black(parent))
124 break;
125
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700126 gparent = rb_red_parent(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700128 tmp = gparent->rb_right;
129 if (parent != tmp) { /* parent == gparent->rb_left */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700130 if (tmp && rb_is_red(tmp)) {
131 /*
132 * Case 1 - color flips
133 *
134 * G g
135 * / \ / \
136 * p u --> P U
137 * / /
138 * n N
139 *
140 * However, since g's parent might be red, and
141 * 4) does not allow this, we need to recurse
142 * at g.
143 */
144 rb_set_parent_color(tmp, gparent, RB_BLACK);
145 rb_set_parent_color(parent, gparent, RB_BLACK);
146 node = gparent;
147 parent = rb_parent(node);
148 rb_set_parent_color(node, parent, RB_RED);
149 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700152 tmp = parent->rb_right;
153 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700154 /*
155 * Case 2 - left rotate at parent
156 *
157 * G G
158 * / \ / \
159 * p U --> n U
160 * \ /
161 * n p
162 *
163 * This still leaves us in violation of 4), the
164 * continuation into Case 3 will fix that.
165 */
166 parent->rb_right = tmp = node->rb_left;
167 node->rb_left = parent;
168 if (tmp)
169 rb_set_parent_color(tmp, parent,
170 RB_BLACK);
171 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700173 tmp = node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700176 /*
177 * Case 3 - right rotate at gparent
178 *
179 * G P
180 * / \ / \
181 * p U --> n g
182 * / \
183 * n U
184 */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700185 gparent->rb_left = tmp; /* == parent->rb_right */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700186 parent->rb_right = gparent;
187 if (tmp)
188 rb_set_parent_color(tmp, gparent, RB_BLACK);
189 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 } else {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700192 tmp = gparent->rb_left;
193 if (tmp && rb_is_red(tmp)) {
194 /* Case 1 - color flips */
195 rb_set_parent_color(tmp, gparent, RB_BLACK);
196 rb_set_parent_color(parent, gparent, RB_BLACK);
197 node = gparent;
198 parent = rb_parent(node);
199 rb_set_parent_color(node, parent, RB_RED);
200 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700203 tmp = parent->rb_left;
204 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700205 /* Case 2 - right rotate at parent */
206 parent->rb_left = tmp = node->rb_right;
207 node->rb_right = parent;
208 if (tmp)
209 rb_set_parent_color(tmp, parent,
210 RB_BLACK);
211 rb_set_parent_color(parent, node, RB_RED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700213 tmp = node->rb_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700216 /* Case 3 - left rotate at gparent */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700217 gparent->rb_right = tmp; /* == parent->rb_left */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700218 parent->rb_left = gparent;
219 if (tmp)
220 rb_set_parent_color(tmp, gparent, RB_BLACK);
221 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700222 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226EXPORT_SYMBOL(rb_insert_color);
227
Michel Lespinasse46b61352012-10-08 16:31:11 -0700228static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Michel Lespinasse46b61352012-10-08 16:31:11 -0700230 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700232 while (true) {
233 /*
Michel Lespinasse46b61352012-10-08 16:31:11 -0700234 * Loop invariants:
235 * - node is black (or NULL on first iteration)
236 * - node is not the root (parent is not NULL)
237 * - All leaf paths going through parent and node have a
238 * black node count that is 1 lower than other leaf paths.
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700239 */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700240 sibling = parent->rb_right;
241 if (node != sibling) { /* node == parent->rb_left */
Michel Lespinasse6280d232012-10-08 16:30:57 -0700242 if (rb_is_red(sibling)) {
243 /*
244 * Case 1 - left rotate at parent
245 *
246 * P S
247 * / \ / \
248 * N s --> p Sr
249 * / \ / \
250 * Sl Sr N Sl
251 */
252 parent->rb_right = tmp1 = sibling->rb_left;
253 sibling->rb_left = parent;
254 rb_set_parent_color(tmp1, parent, RB_BLACK);
255 __rb_rotate_set_parents(parent, sibling, root,
256 RB_RED);
257 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700259 tmp1 = sibling->rb_right;
260 if (!tmp1 || rb_is_black(tmp1)) {
261 tmp2 = sibling->rb_left;
262 if (!tmp2 || rb_is_black(tmp2)) {
263 /*
264 * Case 2 - sibling color flip
265 * (p could be either color here)
266 *
267 * (p) (p)
268 * / \ / \
269 * N S --> N s
270 * / \ / \
271 * Sl Sr Sl Sr
272 *
Michel Lespinasse46b61352012-10-08 16:31:11 -0700273 * This leaves us violating 5) which
274 * can be fixed by flipping p to black
275 * if it was red, or by recursing at p.
276 * p is red when coming from Case 1.
Michel Lespinasse6280d232012-10-08 16:30:57 -0700277 */
278 rb_set_parent_color(sibling, parent,
279 RB_RED);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700280 if (rb_is_red(parent))
281 rb_set_black(parent);
282 else {
283 node = parent;
284 parent = rb_parent(node);
285 if (parent)
286 continue;
287 }
288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700290 /*
291 * Case 3 - right rotate at sibling
292 * (p could be either color here)
293 *
294 * (p) (p)
295 * / \ / \
296 * N S --> N Sl
297 * / \ \
298 * sl Sr s
299 * \
300 * Sr
301 */
302 sibling->rb_left = tmp1 = tmp2->rb_right;
303 tmp2->rb_right = sibling;
304 parent->rb_right = tmp2;
305 if (tmp1)
306 rb_set_parent_color(tmp1, sibling,
307 RB_BLACK);
308 tmp1 = sibling;
309 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700311 /*
312 * Case 4 - left rotate at parent + color flips
313 * (p and sl could be either color here.
314 * After rotation, p becomes black, s acquires
315 * p's color, and sl keeps its color)
316 *
317 * (p) (s)
318 * / \ / \
319 * N S --> P Sr
320 * / \ / \
321 * (sl) sr N (sl)
322 */
323 parent->rb_right = tmp2 = sibling->rb_left;
324 sibling->rb_left = parent;
325 rb_set_parent_color(tmp1, sibling, RB_BLACK);
326 if (tmp2)
327 rb_set_parent(tmp2, parent);
328 __rb_rotate_set_parents(parent, sibling, root,
329 RB_BLACK);
Michel Lespinassee125d142012-10-08 16:30:54 -0700330 break;
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700331 } else {
Michel Lespinasse6280d232012-10-08 16:30:57 -0700332 sibling = parent->rb_left;
333 if (rb_is_red(sibling)) {
334 /* Case 1 - right rotate at parent */
335 parent->rb_left = tmp1 = sibling->rb_right;
336 sibling->rb_right = parent;
337 rb_set_parent_color(tmp1, parent, RB_BLACK);
338 __rb_rotate_set_parents(parent, sibling, root,
339 RB_RED);
340 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700342 tmp1 = sibling->rb_left;
343 if (!tmp1 || rb_is_black(tmp1)) {
344 tmp2 = sibling->rb_right;
345 if (!tmp2 || rb_is_black(tmp2)) {
346 /* Case 2 - sibling color flip */
347 rb_set_parent_color(sibling, parent,
348 RB_RED);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700349 if (rb_is_red(parent))
350 rb_set_black(parent);
351 else {
352 node = parent;
353 parent = rb_parent(node);
354 if (parent)
355 continue;
356 }
357 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700359 /* Case 3 - right rotate at sibling */
360 sibling->rb_right = tmp1 = tmp2->rb_left;
361 tmp2->rb_left = sibling;
362 parent->rb_left = tmp2;
363 if (tmp1)
364 rb_set_parent_color(tmp1, sibling,
365 RB_BLACK);
366 tmp1 = sibling;
367 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700369 /* Case 4 - left rotate at parent + color flips */
370 parent->rb_left = tmp2 = sibling->rb_right;
371 sibling->rb_right = parent;
372 rb_set_parent_color(tmp1, sibling, RB_BLACK);
373 if (tmp2)
374 rb_set_parent(tmp2, parent);
375 __rb_rotate_set_parents(parent, sibling, root,
376 RB_BLACK);
Michel Lespinassee125d142012-10-08 16:30:54 -0700377 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382void rb_erase(struct rb_node *node, struct rb_root *root)
383{
Michel Lespinasse60670b82012-10-08 16:31:10 -0700384 struct rb_node *child = node->rb_right, *tmp = node->rb_left;
Michel Lespinasse46b61352012-10-08 16:31:11 -0700385 struct rb_node *parent, *rebalance;
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700386 unsigned long pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Michel Lespinasse60670b82012-10-08 16:31:10 -0700388 if (!tmp) {
Michel Lespinasse46b61352012-10-08 16:31:11 -0700389 /*
390 * Case 1: node to erase has no more than 1 child (easy!)
391 *
392 * Note that if there is one child it must be red due to 5)
393 * and node must be black due to 4). We adjust colors locally
394 * so as to bypass __rb_erase_color() later on.
395 */
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700396 pc = node->__rb_parent_color;
397 parent = __rb_parent(pc);
Michel Lespinasse60670b82012-10-08 16:31:10 -0700398 __rb_change_child(node, child, parent, root);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700399 if (child) {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700400 child->__rb_parent_color = pc;
Michel Lespinasse46b61352012-10-08 16:31:11 -0700401 rebalance = NULL;
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700402 } else
403 rebalance = __rb_is_black(pc) ? parent : NULL;
Michel Lespinasse60670b82012-10-08 16:31:10 -0700404 } else if (!child) {
405 /* Still case 1, but this time the child is node->rb_left */
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700406 tmp->__rb_parent_color = pc = node->__rb_parent_color;
407 parent = __rb_parent(pc);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700408 __rb_change_child(node, tmp, parent, root);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700409 rebalance = NULL;
Michel Lespinasse60670b82012-10-08 16:31:10 -0700410 } else {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700411 struct rb_node *successor = child, *child2;
412 tmp = child->rb_left;
413 if (!tmp) {
414 /*
415 * Case 2: node's successor is its right child
416 *
417 * (n) (s)
418 * / \ / \
419 * (x) (s) -> (x) (c)
420 * \
421 * (c)
422 */
423 parent = child;
424 child2 = child->rb_right;
Wolfram Strepp4c601172009-06-16 15:34:12 -0700425 } else {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700426 /*
427 * Case 3: node's successor is leftmost under
428 * node's right child subtree
429 *
430 * (n) (s)
431 * / \ / \
432 * (x) (y) -> (x) (y)
433 * / /
434 * (p) (p)
435 * / /
436 * (s) (c)
437 * \
438 * (c)
439 */
440 do {
441 parent = successor;
442 successor = tmp;
443 tmp = tmp->rb_left;
444 } while (tmp);
445 parent->rb_left = child2 = successor->rb_right;
446 successor->rb_right = child;
447 rb_set_parent(child, successor);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700448 }
David Woodhouse1975e592006-04-21 13:30:36 +0100449
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700450 successor->rb_left = tmp = node->rb_left;
451 rb_set_parent(tmp, successor);
452
453 pc = node->__rb_parent_color;
454 tmp = __rb_parent(pc);
455 __rb_change_child(node, successor, tmp, root);
456 if (child2) {
457 successor->__rb_parent_color = pc;
458 rb_set_parent_color(child2, parent, RB_BLACK);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700459 rebalance = NULL;
460 } else {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700461 unsigned long pc2 = successor->__rb_parent_color;
462 successor->__rb_parent_color = pc;
463 rebalance = __rb_is_black(pc2) ? parent : NULL;
Michel Lespinasse46b61352012-10-08 16:31:11 -0700464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
Michel Lespinasse46b61352012-10-08 16:31:11 -0700467 if (rebalance)
468 __rb_erase_color(rebalance, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470EXPORT_SYMBOL(rb_erase);
471
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200472static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
473{
474 struct rb_node *parent;
475
476up:
477 func(node, data);
478 parent = rb_parent(node);
479 if (!parent)
480 return;
481
482 if (node == parent->rb_left && parent->rb_right)
483 func(parent->rb_right, data);
484 else if (parent->rb_left)
485 func(parent->rb_left, data);
486
487 node = parent;
488 goto up;
489}
490
491/*
492 * after inserting @node into the tree, update the tree to account for
493 * both the new entry and any damage done by rebalance
494 */
495void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
496{
497 if (node->rb_left)
498 node = node->rb_left;
499 else if (node->rb_right)
500 node = node->rb_right;
501
502 rb_augment_path(node, func, data);
503}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100504EXPORT_SYMBOL(rb_augment_insert);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200505
506/*
507 * before removing the node, find the deepest node on the rebalance path
508 * that will still be there after @node gets removed
509 */
510struct rb_node *rb_augment_erase_begin(struct rb_node *node)
511{
512 struct rb_node *deepest;
513
514 if (!node->rb_right && !node->rb_left)
515 deepest = rb_parent(node);
516 else if (!node->rb_right)
517 deepest = node->rb_left;
518 else if (!node->rb_left)
519 deepest = node->rb_right;
520 else {
521 deepest = rb_next(node);
522 if (deepest->rb_right)
523 deepest = deepest->rb_right;
524 else if (rb_parent(deepest) != node)
525 deepest = rb_parent(deepest);
526 }
527
528 return deepest;
529}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100530EXPORT_SYMBOL(rb_augment_erase_begin);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200531
532/*
533 * after removal, update the tree to account for the removed entry
534 * and any rebalance damage.
535 */
536void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
537{
538 if (node)
539 rb_augment_path(node, func, data);
540}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100541EXPORT_SYMBOL(rb_augment_erase_end);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/*
544 * This function returns the first node (in sort order) of the tree.
545 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000546struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct rb_node *n;
549
550 n = root->rb_node;
551 if (!n)
552 return NULL;
553 while (n->rb_left)
554 n = n->rb_left;
555 return n;
556}
557EXPORT_SYMBOL(rb_first);
558
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000559struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct rb_node *n;
562
563 n = root->rb_node;
564 if (!n)
565 return NULL;
566 while (n->rb_right)
567 n = n->rb_right;
568 return n;
569}
570EXPORT_SYMBOL(rb_last);
571
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000572struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
David Woodhouse55a98102006-04-21 13:35:51 +0100574 struct rb_node *parent;
575
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700576 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200577 return NULL;
578
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700579 /*
580 * If we have a right-hand child, go down and then left as far
581 * as we can.
582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (node->rb_right) {
584 node = node->rb_right;
585 while (node->rb_left)
586 node=node->rb_left;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000587 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700590 /*
591 * No right-hand children. Everything down and left is smaller than us,
592 * so any 'next' node must be in the general direction of our parent.
593 * Go up the tree; any time the ancestor is a right-hand child of its
594 * parent, keep going up. First time it's a left-hand child of its
595 * parent, said parent is our 'next' node.
596 */
David Woodhouse55a98102006-04-21 13:35:51 +0100597 while ((parent = rb_parent(node)) && node == parent->rb_right)
598 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
David Woodhouse55a98102006-04-21 13:35:51 +0100600 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602EXPORT_SYMBOL(rb_next);
603
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000604struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
David Woodhouse55a98102006-04-21 13:35:51 +0100606 struct rb_node *parent;
607
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700608 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200609 return NULL;
610
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700611 /*
612 * If we have a left-hand child, go down and then right as far
613 * as we can.
614 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (node->rb_left) {
616 node = node->rb_left;
617 while (node->rb_right)
618 node=node->rb_right;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000619 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700622 /*
623 * No left-hand children. Go up till we find an ancestor which
624 * is a right-hand child of its parent.
625 */
David Woodhouse55a98102006-04-21 13:35:51 +0100626 while ((parent = rb_parent(node)) && node == parent->rb_left)
627 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
David Woodhouse55a98102006-04-21 13:35:51 +0100629 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631EXPORT_SYMBOL(rb_prev);
632
633void rb_replace_node(struct rb_node *victim, struct rb_node *new,
634 struct rb_root *root)
635{
David Woodhouse55a98102006-04-21 13:35:51 +0100636 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 /* Set the surrounding nodes to point to the replacement */
Michel Lespinasse7abc7042012-10-08 16:31:07 -0700639 __rb_change_child(victim, new, parent, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (victim->rb_left)
David Woodhouse55a98102006-04-21 13:35:51 +0100641 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100643 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* Copy the pointers/colour from the victim to the replacement */
646 *new = *victim;
647}
648EXPORT_SYMBOL(rb_replace_node);