blob: a37ee7954b8f5eff08cda2b83d16d98ff3721c60 [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
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700108static __always_inline void
109__rb_insert(struct rb_node *node, struct rb_root *root,
110 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700112 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Michel Lespinasse6d584522012-10-08 16:30:44 -0700114 while (true) {
115 /*
116 * Loop invariant: node is red
117 *
118 * If there is a black parent, we are done.
119 * Otherwise, take some corrective action as we don't
120 * want a red root or two consecutive red nodes.
121 */
Michel Lespinasse6d584522012-10-08 16:30:44 -0700122 if (!parent) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700123 rb_set_parent_color(node, NULL, RB_BLACK);
Michel Lespinasse6d584522012-10-08 16:30:44 -0700124 break;
125 } else if (rb_is_black(parent))
126 break;
127
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700128 gparent = rb_red_parent(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700130 tmp = gparent->rb_right;
131 if (parent != tmp) { /* parent == gparent->rb_left */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700132 if (tmp && rb_is_red(tmp)) {
133 /*
134 * Case 1 - color flips
135 *
136 * G g
137 * / \ / \
138 * p u --> P U
139 * / /
140 * n N
141 *
142 * However, since g's parent might be red, and
143 * 4) does not allow this, we need to recurse
144 * at g.
145 */
146 rb_set_parent_color(tmp, gparent, RB_BLACK);
147 rb_set_parent_color(parent, gparent, RB_BLACK);
148 node = gparent;
149 parent = rb_parent(node);
150 rb_set_parent_color(node, parent, RB_RED);
151 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700154 tmp = parent->rb_right;
155 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700156 /*
157 * Case 2 - left rotate at parent
158 *
159 * G G
160 * / \ / \
161 * p U --> n U
162 * \ /
163 * n p
164 *
165 * This still leaves us in violation of 4), the
166 * continuation into Case 3 will fix that.
167 */
168 parent->rb_right = tmp = node->rb_left;
169 node->rb_left = parent;
170 if (tmp)
171 rb_set_parent_color(tmp, parent,
172 RB_BLACK);
173 rb_set_parent_color(parent, node, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700174 augment_rotate(parent, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700176 tmp = node->rb_right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700179 /*
180 * Case 3 - right rotate at gparent
181 *
182 * G P
183 * / \ / \
184 * p U --> n g
185 * / \
186 * n U
187 */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700188 gparent->rb_left = tmp; /* == parent->rb_right */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700189 parent->rb_right = gparent;
190 if (tmp)
191 rb_set_parent_color(tmp, gparent, RB_BLACK);
192 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700193 augment_rotate(gparent, parent);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 } else {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700196 tmp = gparent->rb_left;
197 if (tmp && rb_is_red(tmp)) {
198 /* Case 1 - color flips */
199 rb_set_parent_color(tmp, gparent, RB_BLACK);
200 rb_set_parent_color(parent, gparent, RB_BLACK);
201 node = gparent;
202 parent = rb_parent(node);
203 rb_set_parent_color(node, parent, RB_RED);
204 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700207 tmp = parent->rb_left;
208 if (node == tmp) {
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700209 /* Case 2 - right rotate at parent */
210 parent->rb_left = tmp = node->rb_right;
211 node->rb_right = parent;
212 if (tmp)
213 rb_set_parent_color(tmp, parent,
214 RB_BLACK);
215 rb_set_parent_color(parent, node, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700216 augment_rotate(parent, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 parent = node;
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700218 tmp = node->rb_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700221 /* Case 3 - left rotate at gparent */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700222 gparent->rb_right = tmp; /* == parent->rb_left */
Michel Lespinasse5bc91882012-10-08 16:30:47 -0700223 parent->rb_left = gparent;
224 if (tmp)
225 rb_set_parent_color(tmp, gparent, RB_BLACK);
226 __rb_rotate_set_parents(gparent, parent, root, RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700227 augment_rotate(gparent, parent);
Michel Lespinasse1f052862012-10-08 16:30:42 -0700228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700233static __always_inline void
234__rb_erase_color(struct rb_node *parent, struct rb_root *root,
235 const struct rb_augment_callbacks *augment)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Michel Lespinasse46b61352012-10-08 16:31:11 -0700237 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700239 while (true) {
240 /*
Michel Lespinasse46b61352012-10-08 16:31:11 -0700241 * Loop invariants:
242 * - node is black (or NULL on first iteration)
243 * - node is not the root (parent is not NULL)
244 * - All leaf paths going through parent and node have a
245 * black node count that is 1 lower than other leaf paths.
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700246 */
Michel Lespinasse59633ab2012-10-08 16:31:02 -0700247 sibling = parent->rb_right;
248 if (node != sibling) { /* node == parent->rb_left */
Michel Lespinasse6280d232012-10-08 16:30:57 -0700249 if (rb_is_red(sibling)) {
250 /*
251 * Case 1 - left rotate at parent
252 *
253 * P S
254 * / \ / \
255 * N s --> p Sr
256 * / \ / \
257 * Sl Sr N Sl
258 */
259 parent->rb_right = tmp1 = sibling->rb_left;
260 sibling->rb_left = parent;
261 rb_set_parent_color(tmp1, parent, RB_BLACK);
262 __rb_rotate_set_parents(parent, sibling, root,
263 RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700264 augment->rotate(parent, sibling);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700265 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700267 tmp1 = sibling->rb_right;
268 if (!tmp1 || rb_is_black(tmp1)) {
269 tmp2 = sibling->rb_left;
270 if (!tmp2 || rb_is_black(tmp2)) {
271 /*
272 * Case 2 - sibling color flip
273 * (p could be either color here)
274 *
275 * (p) (p)
276 * / \ / \
277 * N S --> N s
278 * / \ / \
279 * Sl Sr Sl Sr
280 *
Michel Lespinasse46b61352012-10-08 16:31:11 -0700281 * This leaves us violating 5) which
282 * can be fixed by flipping p to black
283 * if it was red, or by recursing at p.
284 * p is red when coming from Case 1.
Michel Lespinasse6280d232012-10-08 16:30:57 -0700285 */
286 rb_set_parent_color(sibling, parent,
287 RB_RED);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700288 if (rb_is_red(parent))
289 rb_set_black(parent);
290 else {
291 node = parent;
292 parent = rb_parent(node);
293 if (parent)
294 continue;
295 }
296 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700298 /*
299 * Case 3 - right rotate at sibling
300 * (p could be either color here)
301 *
302 * (p) (p)
303 * / \ / \
304 * N S --> N Sl
305 * / \ \
306 * sl Sr s
307 * \
308 * Sr
309 */
310 sibling->rb_left = tmp1 = tmp2->rb_right;
311 tmp2->rb_right = sibling;
312 parent->rb_right = tmp2;
313 if (tmp1)
314 rb_set_parent_color(tmp1, sibling,
315 RB_BLACK);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700316 augment->rotate(sibling, tmp2);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700317 tmp1 = sibling;
318 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700320 /*
321 * Case 4 - left rotate at parent + color flips
322 * (p and sl could be either color here.
323 * After rotation, p becomes black, s acquires
324 * p's color, and sl keeps its color)
325 *
326 * (p) (s)
327 * / \ / \
328 * N S --> P Sr
329 * / \ / \
330 * (sl) sr N (sl)
331 */
332 parent->rb_right = tmp2 = sibling->rb_left;
333 sibling->rb_left = parent;
334 rb_set_parent_color(tmp1, sibling, RB_BLACK);
335 if (tmp2)
336 rb_set_parent(tmp2, parent);
337 __rb_rotate_set_parents(parent, sibling, root,
338 RB_BLACK);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700339 augment->rotate(parent, sibling);
Michel Lespinassee125d142012-10-08 16:30:54 -0700340 break;
Michel Lespinassed6ff1272012-10-08 16:30:50 -0700341 } else {
Michel Lespinasse6280d232012-10-08 16:30:57 -0700342 sibling = parent->rb_left;
343 if (rb_is_red(sibling)) {
344 /* Case 1 - right rotate at parent */
345 parent->rb_left = tmp1 = sibling->rb_right;
346 sibling->rb_right = parent;
347 rb_set_parent_color(tmp1, parent, RB_BLACK);
348 __rb_rotate_set_parents(parent, sibling, root,
349 RB_RED);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700350 augment->rotate(parent, sibling);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700351 sibling = tmp1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700353 tmp1 = sibling->rb_left;
354 if (!tmp1 || rb_is_black(tmp1)) {
355 tmp2 = sibling->rb_right;
356 if (!tmp2 || rb_is_black(tmp2)) {
357 /* Case 2 - sibling color flip */
358 rb_set_parent_color(sibling, parent,
359 RB_RED);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700360 if (rb_is_red(parent))
361 rb_set_black(parent);
362 else {
363 node = parent;
364 parent = rb_parent(node);
365 if (parent)
366 continue;
367 }
368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700370 /* Case 3 - right rotate at sibling */
371 sibling->rb_right = tmp1 = tmp2->rb_left;
372 tmp2->rb_left = sibling;
373 parent->rb_left = tmp2;
374 if (tmp1)
375 rb_set_parent_color(tmp1, sibling,
376 RB_BLACK);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700377 augment->rotate(sibling, tmp2);
Michel Lespinasse6280d232012-10-08 16:30:57 -0700378 tmp1 = sibling;
379 sibling = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Michel Lespinasse6280d232012-10-08 16:30:57 -0700381 /* Case 4 - left rotate at parent + color flips */
382 parent->rb_left = tmp2 = sibling->rb_right;
383 sibling->rb_right = parent;
384 rb_set_parent_color(tmp1, sibling, RB_BLACK);
385 if (tmp2)
386 rb_set_parent(tmp2, parent);
387 __rb_rotate_set_parents(parent, sibling, root,
388 RB_BLACK);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700389 augment->rotate(parent, sibling);
Michel Lespinassee125d142012-10-08 16:30:54 -0700390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700395static __always_inline void
396__rb_erase(struct rb_node *node, struct rb_root *root,
397 const struct rb_augment_callbacks *augment)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Michel Lespinasse60670b82012-10-08 16:31:10 -0700399 struct rb_node *child = node->rb_right, *tmp = node->rb_left;
Michel Lespinasse46b61352012-10-08 16:31:11 -0700400 struct rb_node *parent, *rebalance;
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700401 unsigned long pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Michel Lespinasse60670b82012-10-08 16:31:10 -0700403 if (!tmp) {
Michel Lespinasse46b61352012-10-08 16:31:11 -0700404 /*
405 * Case 1: node to erase has no more than 1 child (easy!)
406 *
407 * Note that if there is one child it must be red due to 5)
408 * and node must be black due to 4). We adjust colors locally
409 * so as to bypass __rb_erase_color() later on.
410 */
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700411 pc = node->__rb_parent_color;
412 parent = __rb_parent(pc);
Michel Lespinasse60670b82012-10-08 16:31:10 -0700413 __rb_change_child(node, child, parent, root);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700414 if (child) {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700415 child->__rb_parent_color = pc;
Michel Lespinasse46b61352012-10-08 16:31:11 -0700416 rebalance = NULL;
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700417 } else
418 rebalance = __rb_is_black(pc) ? parent : NULL;
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700419 tmp = parent;
Michel Lespinasse60670b82012-10-08 16:31:10 -0700420 } else if (!child) {
421 /* Still case 1, but this time the child is node->rb_left */
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700422 tmp->__rb_parent_color = pc = node->__rb_parent_color;
423 parent = __rb_parent(pc);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700424 __rb_change_child(node, tmp, parent, root);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700425 rebalance = NULL;
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700426 tmp = parent;
Michel Lespinasse60670b82012-10-08 16:31:10 -0700427 } else {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700428 struct rb_node *successor = child, *child2;
429 tmp = child->rb_left;
430 if (!tmp) {
431 /*
432 * Case 2: node's successor is its right child
433 *
434 * (n) (s)
435 * / \ / \
436 * (x) (s) -> (x) (c)
437 * \
438 * (c)
439 */
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700440 parent = successor;
441 child2 = successor->rb_right;
442 augment->copy(node, successor);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700443 } else {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700444 /*
445 * Case 3: node's successor is leftmost under
446 * node's right child subtree
447 *
448 * (n) (s)
449 * / \ / \
450 * (x) (y) -> (x) (y)
451 * / /
452 * (p) (p)
453 * / /
454 * (s) (c)
455 * \
456 * (c)
457 */
458 do {
459 parent = successor;
460 successor = tmp;
461 tmp = tmp->rb_left;
462 } while (tmp);
463 parent->rb_left = child2 = successor->rb_right;
464 successor->rb_right = child;
465 rb_set_parent(child, successor);
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700466 augment->copy(node, successor);
467 augment->propagate(parent, successor);
Wolfram Strepp4c601172009-06-16 15:34:12 -0700468 }
David Woodhouse1975e592006-04-21 13:30:36 +0100469
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700470 successor->rb_left = tmp = node->rb_left;
471 rb_set_parent(tmp, successor);
472
473 pc = node->__rb_parent_color;
474 tmp = __rb_parent(pc);
475 __rb_change_child(node, successor, tmp, root);
476 if (child2) {
477 successor->__rb_parent_color = pc;
478 rb_set_parent_color(child2, parent, RB_BLACK);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700479 rebalance = NULL;
480 } else {
Michel Lespinasse4f035ad2012-10-08 16:31:13 -0700481 unsigned long pc2 = successor->__rb_parent_color;
482 successor->__rb_parent_color = pc;
483 rebalance = __rb_is_black(pc2) ? parent : NULL;
Michel Lespinasse46b61352012-10-08 16:31:11 -0700484 }
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700485 tmp = successor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700488 augment->propagate(tmp, NULL);
Michel Lespinasse46b61352012-10-08 16:31:11 -0700489 if (rebalance)
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700490 __rb_erase_color(rebalance, root, augment);
491}
492
493/*
494 * Non-augmented rbtree manipulation functions.
495 *
496 * We use dummy augmented callbacks here, and have the compiler optimize them
497 * out of the rb_insert_color() and rb_erase() function definitions.
498 */
499
500static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {}
501static inline void dummy_copy(struct rb_node *old, struct rb_node *new) {}
502static inline void dummy_rotate(struct rb_node *old, struct rb_node *new) {}
503
504static const struct rb_augment_callbacks dummy_callbacks = {
505 dummy_propagate, dummy_copy, dummy_rotate
506};
507
508void rb_insert_color(struct rb_node *node, struct rb_root *root)
509{
510 __rb_insert(node, root, dummy_rotate);
511}
512EXPORT_SYMBOL(rb_insert_color);
513
514void rb_erase(struct rb_node *node, struct rb_root *root)
515{
516 __rb_erase(node, root, &dummy_callbacks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518EXPORT_SYMBOL(rb_erase);
519
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700520/*
521 * Augmented rbtree manipulation functions.
522 *
523 * This instantiates the same __always_inline functions as in the non-augmented
524 * case, but this time with user-defined callbacks.
525 */
526
527void __rb_insert_augmented(struct rb_node *node, struct rb_root *root,
528 void (*augment_rotate)(struct rb_node *old, struct rb_node *new))
529{
530 __rb_insert(node, root, augment_rotate);
531}
532EXPORT_SYMBOL(__rb_insert_augmented);
533
534void rb_erase_augmented(struct rb_node *node, struct rb_root *root,
535 const struct rb_augment_callbacks *augment)
536{
537 __rb_erase(node, root, augment);
538}
539EXPORT_SYMBOL(rb_erase_augmented);
540
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200541static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
542{
543 struct rb_node *parent;
544
545up:
546 func(node, data);
547 parent = rb_parent(node);
548 if (!parent)
549 return;
550
551 if (node == parent->rb_left && parent->rb_right)
552 func(parent->rb_right, data);
553 else if (parent->rb_left)
554 func(parent->rb_left, data);
555
556 node = parent;
557 goto up;
558}
559
560/*
561 * after inserting @node into the tree, update the tree to account for
562 * both the new entry and any damage done by rebalance
563 */
564void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
565{
566 if (node->rb_left)
567 node = node->rb_left;
568 else if (node->rb_right)
569 node = node->rb_right;
570
571 rb_augment_path(node, func, data);
572}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100573EXPORT_SYMBOL(rb_augment_insert);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200574
575/*
576 * before removing the node, find the deepest node on the rebalance path
577 * that will still be there after @node gets removed
578 */
579struct rb_node *rb_augment_erase_begin(struct rb_node *node)
580{
581 struct rb_node *deepest;
582
583 if (!node->rb_right && !node->rb_left)
584 deepest = rb_parent(node);
585 else if (!node->rb_right)
586 deepest = node->rb_left;
587 else if (!node->rb_left)
588 deepest = node->rb_right;
589 else {
590 deepest = rb_next(node);
591 if (deepest->rb_right)
592 deepest = deepest->rb_right;
593 else if (rb_parent(deepest) != node)
594 deepest = rb_parent(deepest);
595 }
596
597 return deepest;
598}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100599EXPORT_SYMBOL(rb_augment_erase_begin);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200600
601/*
602 * after removal, update the tree to account for the removed entry
603 * and any rebalance damage.
604 */
605void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
606{
607 if (node)
608 rb_augment_path(node, func, data);
609}
Andreas Gruenbacher0b6bb662011-01-26 15:55:36 +0100610EXPORT_SYMBOL(rb_augment_erase_end);
Peter Zijlstrab945d6b2010-05-29 15:31:43 +0200611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612/*
613 * This function returns the first node (in sort order) of the tree.
614 */
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000615struct rb_node *rb_first(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct rb_node *n;
618
619 n = root->rb_node;
620 if (!n)
621 return NULL;
622 while (n->rb_left)
623 n = n->rb_left;
624 return n;
625}
626EXPORT_SYMBOL(rb_first);
627
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000628struct rb_node *rb_last(const struct rb_root *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct rb_node *n;
631
632 n = root->rb_node;
633 if (!n)
634 return NULL;
635 while (n->rb_right)
636 n = n->rb_right;
637 return n;
638}
639EXPORT_SYMBOL(rb_last);
640
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000641struct rb_node *rb_next(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
David Woodhouse55a98102006-04-21 13:35:51 +0100643 struct rb_node *parent;
644
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700645 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200646 return NULL;
647
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700648 /*
649 * If we have a right-hand child, go down and then left as far
650 * as we can.
651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (node->rb_right) {
653 node = node->rb_right;
654 while (node->rb_left)
655 node=node->rb_left;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000656 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
658
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700659 /*
660 * No right-hand children. Everything down and left is smaller than us,
661 * so any 'next' node must be in the general direction of our parent.
662 * Go up the tree; any time the ancestor is a right-hand child of its
663 * parent, keep going up. First time it's a left-hand child of its
664 * parent, said parent is our 'next' node.
665 */
David Woodhouse55a98102006-04-21 13:35:51 +0100666 while ((parent = rb_parent(node)) && node == parent->rb_right)
667 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
David Woodhouse55a98102006-04-21 13:35:51 +0100669 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671EXPORT_SYMBOL(rb_next);
672
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000673struct rb_node *rb_prev(const struct rb_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
David Woodhouse55a98102006-04-21 13:35:51 +0100675 struct rb_node *parent;
676
Michel Lespinasse4c199a92012-10-08 16:30:32 -0700677 if (RB_EMPTY_NODE(node))
Jens Axboe10fd48f2006-07-11 21:15:52 +0200678 return NULL;
679
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700680 /*
681 * If we have a left-hand child, go down and then right as far
682 * as we can.
683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (node->rb_left) {
685 node = node->rb_left;
686 while (node->rb_right)
687 node=node->rb_right;
Artem Bityutskiyf4b477c2009-01-10 11:12:09 +0000688 return (struct rb_node *)node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Michel Lespinasse7ce6ff92012-10-08 16:31:01 -0700691 /*
692 * No left-hand children. Go up till we find an ancestor which
693 * is a right-hand child of its parent.
694 */
David Woodhouse55a98102006-04-21 13:35:51 +0100695 while ((parent = rb_parent(node)) && node == parent->rb_left)
696 node = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
David Woodhouse55a98102006-04-21 13:35:51 +0100698 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700EXPORT_SYMBOL(rb_prev);
701
702void rb_replace_node(struct rb_node *victim, struct rb_node *new,
703 struct rb_root *root)
704{
David Woodhouse55a98102006-04-21 13:35:51 +0100705 struct rb_node *parent = rb_parent(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* Set the surrounding nodes to point to the replacement */
Michel Lespinasse7abc7042012-10-08 16:31:07 -0700708 __rb_change_child(victim, new, parent, root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (victim->rb_left)
David Woodhouse55a98102006-04-21 13:35:51 +0100710 rb_set_parent(victim->rb_left, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (victim->rb_right)
David Woodhouse55a98102006-04-21 13:35:51 +0100712 rb_set_parent(victim->rb_right, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 /* Copy the pointers/colour from the victim to the replacement */
715 *new = *victim;
716}
717EXPORT_SYMBOL(rb_replace_node);