xref: /linux/lib/rbtree.c (revision 5bc9188aa207dafd47eab57df7c4fe5b3d3f636a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds   Red Black Trees
31da177e4SLinus Torvalds   (C) 1999  Andrea Arcangeli <andrea@suse.de>
41da177e4SLinus Torvalds   (C) 2002  David Woodhouse <dwmw2@infradead.org>
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds   This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds   it under the terms of the GNU General Public License as published by
81da177e4SLinus Torvalds   the Free Software Foundation; either version 2 of the License, or
91da177e4SLinus Torvalds   (at your option) any later version.
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds   This program is distributed in the hope that it will be useful,
121da177e4SLinus Torvalds   but WITHOUT ANY WARRANTY; without even the implied warranty of
131da177e4SLinus Torvalds   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
141da177e4SLinus Torvalds   GNU General Public License for more details.
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds   You should have received a copy of the GNU General Public License
171da177e4SLinus Torvalds   along with this program; if not, write to the Free Software
181da177e4SLinus Torvalds   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds   linux/lib/rbtree.c
211da177e4SLinus Torvalds */
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/rbtree.h>
248bc3bcc9SPaul Gortmaker #include <linux/export.h>
251da177e4SLinus Torvalds 
26*5bc9188aSMichel Lespinasse /*
27*5bc9188aSMichel Lespinasse  * red-black trees properties:  http://en.wikipedia.org/wiki/Rbtree
28*5bc9188aSMichel Lespinasse  *
29*5bc9188aSMichel Lespinasse  *  1) A node is either red or black
30*5bc9188aSMichel Lespinasse  *  2) The root is black
31*5bc9188aSMichel Lespinasse  *  3) All leaves (NULL) are black
32*5bc9188aSMichel Lespinasse  *  4) Both children of every red node are black
33*5bc9188aSMichel Lespinasse  *  5) Every simple path from root to leaves contains the same number
34*5bc9188aSMichel Lespinasse  *     of black nodes.
35*5bc9188aSMichel Lespinasse  *
36*5bc9188aSMichel Lespinasse  *  4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
37*5bc9188aSMichel Lespinasse  *  consecutive red nodes in a path and every red node is therefore followed by
38*5bc9188aSMichel Lespinasse  *  a black. So if B is the number of black nodes on every simple path (as per
39*5bc9188aSMichel Lespinasse  *  5), then the longest possible path due to 4 is 2B.
40*5bc9188aSMichel Lespinasse  *
41*5bc9188aSMichel Lespinasse  *  We shall indicate color with case, where black nodes are uppercase and red
42*5bc9188aSMichel Lespinasse  *  nodes will be lowercase.
43*5bc9188aSMichel Lespinasse  */
44*5bc9188aSMichel Lespinasse 
45bf7ad8eeSMichel Lespinasse #define	RB_RED		0
46bf7ad8eeSMichel Lespinasse #define	RB_BLACK	1
47bf7ad8eeSMichel Lespinasse 
48bf7ad8eeSMichel Lespinasse #define rb_color(r)   ((r)->__rb_parent_color & 1)
49bf7ad8eeSMichel Lespinasse #define rb_is_red(r)   (!rb_color(r))
50bf7ad8eeSMichel Lespinasse #define rb_is_black(r) rb_color(r)
51bf7ad8eeSMichel Lespinasse #define rb_set_red(r)  do { (r)->__rb_parent_color &= ~1; } while (0)
52bf7ad8eeSMichel Lespinasse #define rb_set_black(r)  do { (r)->__rb_parent_color |= 1; } while (0)
53bf7ad8eeSMichel Lespinasse 
54bf7ad8eeSMichel Lespinasse static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
55bf7ad8eeSMichel Lespinasse {
56bf7ad8eeSMichel Lespinasse 	rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
57bf7ad8eeSMichel Lespinasse }
58bf7ad8eeSMichel Lespinasse static inline void rb_set_color(struct rb_node *rb, int color)
59bf7ad8eeSMichel Lespinasse {
60bf7ad8eeSMichel Lespinasse 	rb->__rb_parent_color = (rb->__rb_parent_color & ~1) | color;
61bf7ad8eeSMichel Lespinasse }
62bf7ad8eeSMichel Lespinasse 
63*5bc9188aSMichel Lespinasse static inline void rb_set_parent_color(struct rb_node *rb,
64*5bc9188aSMichel Lespinasse 				       struct rb_node *p, int color)
65*5bc9188aSMichel Lespinasse {
66*5bc9188aSMichel Lespinasse 	rb->__rb_parent_color = (unsigned long)p | color;
67*5bc9188aSMichel Lespinasse }
68*5bc9188aSMichel Lespinasse 
69*5bc9188aSMichel Lespinasse static inline struct rb_node *rb_red_parent(struct rb_node *red)
70*5bc9188aSMichel Lespinasse {
71*5bc9188aSMichel Lespinasse 	return (struct rb_node *)red->__rb_parent_color;
72*5bc9188aSMichel Lespinasse }
73*5bc9188aSMichel Lespinasse 
741da177e4SLinus Torvalds static void __rb_rotate_left(struct rb_node *node, struct rb_root *root)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	struct rb_node *right = node->rb_right;
7755a98102SDavid Woodhouse 	struct rb_node *parent = rb_parent(node);
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	if ((node->rb_right = right->rb_left))
8055a98102SDavid Woodhouse 		rb_set_parent(right->rb_left, node);
811da177e4SLinus Torvalds 	right->rb_left = node;
821da177e4SLinus Torvalds 
8355a98102SDavid Woodhouse 	rb_set_parent(right, parent);
8455a98102SDavid Woodhouse 
8555a98102SDavid Woodhouse 	if (parent)
861da177e4SLinus Torvalds 	{
8755a98102SDavid Woodhouse 		if (node == parent->rb_left)
8855a98102SDavid Woodhouse 			parent->rb_left = right;
891da177e4SLinus Torvalds 		else
9055a98102SDavid Woodhouse 			parent->rb_right = right;
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 	else
931da177e4SLinus Torvalds 		root->rb_node = right;
9455a98102SDavid Woodhouse 	rb_set_parent(node, right);
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds static void __rb_rotate_right(struct rb_node *node, struct rb_root *root)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	struct rb_node *left = node->rb_left;
10055a98102SDavid Woodhouse 	struct rb_node *parent = rb_parent(node);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	if ((node->rb_left = left->rb_right))
10355a98102SDavid Woodhouse 		rb_set_parent(left->rb_right, node);
1041da177e4SLinus Torvalds 	left->rb_right = node;
1051da177e4SLinus Torvalds 
10655a98102SDavid Woodhouse 	rb_set_parent(left, parent);
10755a98102SDavid Woodhouse 
10855a98102SDavid Woodhouse 	if (parent)
1091da177e4SLinus Torvalds 	{
11055a98102SDavid Woodhouse 		if (node == parent->rb_right)
11155a98102SDavid Woodhouse 			parent->rb_right = left;
1121da177e4SLinus Torvalds 		else
11355a98102SDavid Woodhouse 			parent->rb_left = left;
1141da177e4SLinus Torvalds 	}
1151da177e4SLinus Torvalds 	else
1161da177e4SLinus Torvalds 		root->rb_node = left;
11755a98102SDavid Woodhouse 	rb_set_parent(node, left);
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
120*5bc9188aSMichel Lespinasse /*
121*5bc9188aSMichel Lespinasse  * Helper function for rotations:
122*5bc9188aSMichel Lespinasse  * - old's parent and color get assigned to new
123*5bc9188aSMichel Lespinasse  * - old gets assigned new as a parent and 'color' as a color.
124*5bc9188aSMichel Lespinasse  */
125*5bc9188aSMichel Lespinasse static inline void
126*5bc9188aSMichel Lespinasse __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
127*5bc9188aSMichel Lespinasse 			struct rb_root *root, int color)
128*5bc9188aSMichel Lespinasse {
129*5bc9188aSMichel Lespinasse 	struct rb_node *parent = rb_parent(old);
130*5bc9188aSMichel Lespinasse 	new->__rb_parent_color = old->__rb_parent_color;
131*5bc9188aSMichel Lespinasse 	rb_set_parent_color(old, new, color);
132*5bc9188aSMichel Lespinasse 	if (parent) {
133*5bc9188aSMichel Lespinasse 		if (parent->rb_left == old)
134*5bc9188aSMichel Lespinasse 			parent->rb_left = new;
135*5bc9188aSMichel Lespinasse 		else
136*5bc9188aSMichel Lespinasse 			parent->rb_right = new;
137*5bc9188aSMichel Lespinasse 	} else
138*5bc9188aSMichel Lespinasse 		root->rb_node = new;
139*5bc9188aSMichel Lespinasse }
140*5bc9188aSMichel Lespinasse 
1411da177e4SLinus Torvalds void rb_insert_color(struct rb_node *node, struct rb_root *root)
1421da177e4SLinus Torvalds {
143*5bc9188aSMichel Lespinasse 	struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
1441da177e4SLinus Torvalds 
1456d58452dSMichel Lespinasse 	while (true) {
1466d58452dSMichel Lespinasse 		/*
1476d58452dSMichel Lespinasse 		 * Loop invariant: node is red
1486d58452dSMichel Lespinasse 		 *
1496d58452dSMichel Lespinasse 		 * If there is a black parent, we are done.
1506d58452dSMichel Lespinasse 		 * Otherwise, take some corrective action as we don't
1516d58452dSMichel Lespinasse 		 * want a red root or two consecutive red nodes.
1526d58452dSMichel Lespinasse 		 */
1536d58452dSMichel Lespinasse 		if (!parent) {
154*5bc9188aSMichel Lespinasse 			rb_set_parent_color(node, NULL, RB_BLACK);
1556d58452dSMichel Lespinasse 			break;
1566d58452dSMichel Lespinasse 		} else if (rb_is_black(parent))
1576d58452dSMichel Lespinasse 			break;
1586d58452dSMichel Lespinasse 
159*5bc9188aSMichel Lespinasse 		gparent = rb_red_parent(parent);
1601da177e4SLinus Torvalds 
161*5bc9188aSMichel Lespinasse 		if (parent == gparent->rb_left) {
162*5bc9188aSMichel Lespinasse 			tmp = gparent->rb_right;
163*5bc9188aSMichel Lespinasse 			if (tmp && rb_is_red(tmp)) {
164*5bc9188aSMichel Lespinasse 				/*
165*5bc9188aSMichel Lespinasse 				 * Case 1 - color flips
166*5bc9188aSMichel Lespinasse 				 *
167*5bc9188aSMichel Lespinasse 				 *       G            g
168*5bc9188aSMichel Lespinasse 				 *      / \          / \
169*5bc9188aSMichel Lespinasse 				 *     p   u  -->   P   U
170*5bc9188aSMichel Lespinasse 				 *    /            /
171*5bc9188aSMichel Lespinasse 				 *   n            N
172*5bc9188aSMichel Lespinasse 				 *
173*5bc9188aSMichel Lespinasse 				 * However, since g's parent might be red, and
174*5bc9188aSMichel Lespinasse 				 * 4) does not allow this, we need to recurse
175*5bc9188aSMichel Lespinasse 				 * at g.
176*5bc9188aSMichel Lespinasse 				 */
177*5bc9188aSMichel Lespinasse 				rb_set_parent_color(tmp, gparent, RB_BLACK);
178*5bc9188aSMichel Lespinasse 				rb_set_parent_color(parent, gparent, RB_BLACK);
1791da177e4SLinus Torvalds 				node = gparent;
180*5bc9188aSMichel Lespinasse 				parent = rb_parent(node);
181*5bc9188aSMichel Lespinasse 				rb_set_parent_color(node, parent, RB_RED);
1821da177e4SLinus Torvalds 				continue;
1831da177e4SLinus Torvalds 			}
1841da177e4SLinus Torvalds 
1851f052865SMichel Lespinasse 			if (parent->rb_right == node) {
186*5bc9188aSMichel Lespinasse 				/*
187*5bc9188aSMichel Lespinasse 				 * Case 2 - left rotate at parent
188*5bc9188aSMichel Lespinasse 				 *
189*5bc9188aSMichel Lespinasse 				 *      G             G
190*5bc9188aSMichel Lespinasse 				 *     / \           / \
191*5bc9188aSMichel Lespinasse 				 *    p   U  -->    n   U
192*5bc9188aSMichel Lespinasse 				 *     \           /
193*5bc9188aSMichel Lespinasse 				 *      n         p
194*5bc9188aSMichel Lespinasse 				 *
195*5bc9188aSMichel Lespinasse 				 * This still leaves us in violation of 4), the
196*5bc9188aSMichel Lespinasse 				 * continuation into Case 3 will fix that.
197*5bc9188aSMichel Lespinasse 				 */
198*5bc9188aSMichel Lespinasse 				parent->rb_right = tmp = node->rb_left;
199*5bc9188aSMichel Lespinasse 				node->rb_left = parent;
200*5bc9188aSMichel Lespinasse 				if (tmp)
201*5bc9188aSMichel Lespinasse 					rb_set_parent_color(tmp, parent,
202*5bc9188aSMichel Lespinasse 							    RB_BLACK);
203*5bc9188aSMichel Lespinasse 				rb_set_parent_color(parent, node, RB_RED);
2041da177e4SLinus Torvalds 				parent = node;
2051da177e4SLinus Torvalds 			}
2061da177e4SLinus Torvalds 
207*5bc9188aSMichel Lespinasse 			/*
208*5bc9188aSMichel Lespinasse 			 * Case 3 - right rotate at gparent
209*5bc9188aSMichel Lespinasse 			 *
210*5bc9188aSMichel Lespinasse 			 *        G           P
211*5bc9188aSMichel Lespinasse 			 *       / \         / \
212*5bc9188aSMichel Lespinasse 			 *      p   U  -->  n   g
213*5bc9188aSMichel Lespinasse 			 *     /                 \
214*5bc9188aSMichel Lespinasse 			 *    n                   U
215*5bc9188aSMichel Lespinasse 			 */
216*5bc9188aSMichel Lespinasse 			gparent->rb_left = tmp = parent->rb_right;
217*5bc9188aSMichel Lespinasse 			parent->rb_right = gparent;
218*5bc9188aSMichel Lespinasse 			if (tmp)
219*5bc9188aSMichel Lespinasse 				rb_set_parent_color(tmp, gparent, RB_BLACK);
220*5bc9188aSMichel Lespinasse 			__rb_rotate_set_parents(gparent, parent, root, RB_RED);
2211f052865SMichel Lespinasse 			break;
2221da177e4SLinus Torvalds 		} else {
223*5bc9188aSMichel Lespinasse 			tmp = gparent->rb_left;
224*5bc9188aSMichel Lespinasse 			if (tmp && rb_is_red(tmp)) {
225*5bc9188aSMichel Lespinasse 				/* Case 1 - color flips */
226*5bc9188aSMichel Lespinasse 				rb_set_parent_color(tmp, gparent, RB_BLACK);
227*5bc9188aSMichel Lespinasse 				rb_set_parent_color(parent, gparent, RB_BLACK);
2281da177e4SLinus Torvalds 				node = gparent;
229*5bc9188aSMichel Lespinasse 				parent = rb_parent(node);
230*5bc9188aSMichel Lespinasse 				rb_set_parent_color(node, parent, RB_RED);
2311da177e4SLinus Torvalds 				continue;
2321da177e4SLinus Torvalds 			}
2331da177e4SLinus Torvalds 
2341f052865SMichel Lespinasse 			if (parent->rb_left == node) {
235*5bc9188aSMichel Lespinasse 				/* Case 2 - right rotate at parent */
236*5bc9188aSMichel Lespinasse 				parent->rb_left = tmp = node->rb_right;
237*5bc9188aSMichel Lespinasse 				node->rb_right = parent;
238*5bc9188aSMichel Lespinasse 				if (tmp)
239*5bc9188aSMichel Lespinasse 					rb_set_parent_color(tmp, parent,
240*5bc9188aSMichel Lespinasse 							    RB_BLACK);
241*5bc9188aSMichel Lespinasse 				rb_set_parent_color(parent, node, RB_RED);
2421da177e4SLinus Torvalds 				parent = node;
2431da177e4SLinus Torvalds 			}
2441da177e4SLinus Torvalds 
245*5bc9188aSMichel Lespinasse 			/* Case 3 - left rotate at gparent */
246*5bc9188aSMichel Lespinasse 			gparent->rb_right = tmp = parent->rb_left;
247*5bc9188aSMichel Lespinasse 			parent->rb_left = gparent;
248*5bc9188aSMichel Lespinasse 			if (tmp)
249*5bc9188aSMichel Lespinasse 				rb_set_parent_color(tmp, gparent, RB_BLACK);
250*5bc9188aSMichel Lespinasse 			__rb_rotate_set_parents(gparent, parent, root, RB_RED);
2511f052865SMichel Lespinasse 			break;
2521da177e4SLinus Torvalds 		}
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds EXPORT_SYMBOL(rb_insert_color);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds static void __rb_erase_color(struct rb_node *node, struct rb_node *parent,
2581da177e4SLinus Torvalds 			     struct rb_root *root)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	struct rb_node *other;
2611da177e4SLinus Torvalds 
26255a98102SDavid Woodhouse 	while ((!node || rb_is_black(node)) && node != root->rb_node)
2631da177e4SLinus Torvalds 	{
2641da177e4SLinus Torvalds 		if (parent->rb_left == node)
2651da177e4SLinus Torvalds 		{
2661da177e4SLinus Torvalds 			other = parent->rb_right;
26755a98102SDavid Woodhouse 			if (rb_is_red(other))
2681da177e4SLinus Torvalds 			{
26955a98102SDavid Woodhouse 				rb_set_black(other);
27055a98102SDavid Woodhouse 				rb_set_red(parent);
2711da177e4SLinus Torvalds 				__rb_rotate_left(parent, root);
2721da177e4SLinus Torvalds 				other = parent->rb_right;
2731da177e4SLinus Torvalds 			}
27455a98102SDavid Woodhouse 			if ((!other->rb_left || rb_is_black(other->rb_left)) &&
27555a98102SDavid Woodhouse 			    (!other->rb_right || rb_is_black(other->rb_right)))
2761da177e4SLinus Torvalds 			{
27755a98102SDavid Woodhouse 				rb_set_red(other);
2781da177e4SLinus Torvalds 				node = parent;
27955a98102SDavid Woodhouse 				parent = rb_parent(node);
2801da177e4SLinus Torvalds 			}
2811da177e4SLinus Torvalds 			else
2821da177e4SLinus Torvalds 			{
28355a98102SDavid Woodhouse 				if (!other->rb_right || rb_is_black(other->rb_right))
2841da177e4SLinus Torvalds 				{
28555a63998SWolfram Strepp 					rb_set_black(other->rb_left);
28655a98102SDavid Woodhouse 					rb_set_red(other);
2871da177e4SLinus Torvalds 					__rb_rotate_right(other, root);
2881da177e4SLinus Torvalds 					other = parent->rb_right;
2891da177e4SLinus Torvalds 				}
2902f3243aeSDavid Woodhouse 				rb_set_color(other, rb_color(parent));
29155a98102SDavid Woodhouse 				rb_set_black(parent);
29255a98102SDavid Woodhouse 				rb_set_black(other->rb_right);
2931da177e4SLinus Torvalds 				__rb_rotate_left(parent, root);
2941da177e4SLinus Torvalds 				node = root->rb_node;
2951da177e4SLinus Torvalds 				break;
2961da177e4SLinus Torvalds 			}
2971da177e4SLinus Torvalds 		}
2981da177e4SLinus Torvalds 		else
2991da177e4SLinus Torvalds 		{
3001da177e4SLinus Torvalds 			other = parent->rb_left;
30155a98102SDavid Woodhouse 			if (rb_is_red(other))
3021da177e4SLinus Torvalds 			{
30355a98102SDavid Woodhouse 				rb_set_black(other);
30455a98102SDavid Woodhouse 				rb_set_red(parent);
3051da177e4SLinus Torvalds 				__rb_rotate_right(parent, root);
3061da177e4SLinus Torvalds 				other = parent->rb_left;
3071da177e4SLinus Torvalds 			}
30855a98102SDavid Woodhouse 			if ((!other->rb_left || rb_is_black(other->rb_left)) &&
30955a98102SDavid Woodhouse 			    (!other->rb_right || rb_is_black(other->rb_right)))
3101da177e4SLinus Torvalds 			{
31155a98102SDavid Woodhouse 				rb_set_red(other);
3121da177e4SLinus Torvalds 				node = parent;
31355a98102SDavid Woodhouse 				parent = rb_parent(node);
3141da177e4SLinus Torvalds 			}
3151da177e4SLinus Torvalds 			else
3161da177e4SLinus Torvalds 			{
31755a98102SDavid Woodhouse 				if (!other->rb_left || rb_is_black(other->rb_left))
3181da177e4SLinus Torvalds 				{
31955a63998SWolfram Strepp 					rb_set_black(other->rb_right);
32055a98102SDavid Woodhouse 					rb_set_red(other);
3211da177e4SLinus Torvalds 					__rb_rotate_left(other, root);
3221da177e4SLinus Torvalds 					other = parent->rb_left;
3231da177e4SLinus Torvalds 				}
3242f3243aeSDavid Woodhouse 				rb_set_color(other, rb_color(parent));
32555a98102SDavid Woodhouse 				rb_set_black(parent);
32655a98102SDavid Woodhouse 				rb_set_black(other->rb_left);
3271da177e4SLinus Torvalds 				__rb_rotate_right(parent, root);
3281da177e4SLinus Torvalds 				node = root->rb_node;
3291da177e4SLinus Torvalds 				break;
3301da177e4SLinus Torvalds 			}
3311da177e4SLinus Torvalds 		}
3321da177e4SLinus Torvalds 	}
3331da177e4SLinus Torvalds 	if (node)
33455a98102SDavid Woodhouse 		rb_set_black(node);
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds void rb_erase(struct rb_node *node, struct rb_root *root)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	struct rb_node *child, *parent;
3401da177e4SLinus Torvalds 	int color;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	if (!node->rb_left)
3431da177e4SLinus Torvalds 		child = node->rb_right;
3441da177e4SLinus Torvalds 	else if (!node->rb_right)
3451da177e4SLinus Torvalds 		child = node->rb_left;
3461da177e4SLinus Torvalds 	else
3471da177e4SLinus Torvalds 	{
3481da177e4SLinus Torvalds 		struct rb_node *old = node, *left;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 		node = node->rb_right;
3511da177e4SLinus Torvalds 		while ((left = node->rb_left) != NULL)
3521da177e4SLinus Torvalds 			node = left;
35316c047adSWolfram Strepp 
35416c047adSWolfram Strepp 		if (rb_parent(old)) {
35516c047adSWolfram Strepp 			if (rb_parent(old)->rb_left == old)
35616c047adSWolfram Strepp 				rb_parent(old)->rb_left = node;
35716c047adSWolfram Strepp 			else
35816c047adSWolfram Strepp 				rb_parent(old)->rb_right = node;
35916c047adSWolfram Strepp 		} else
36016c047adSWolfram Strepp 			root->rb_node = node;
36116c047adSWolfram Strepp 
3621da177e4SLinus Torvalds 		child = node->rb_right;
36355a98102SDavid Woodhouse 		parent = rb_parent(node);
3642f3243aeSDavid Woodhouse 		color = rb_color(node);
3651da177e4SLinus Torvalds 
3664c601178SWolfram Strepp 		if (parent == old) {
3674c601178SWolfram Strepp 			parent = node;
3684c601178SWolfram Strepp 		} else {
3691da177e4SLinus Torvalds 			if (child)
37055a98102SDavid Woodhouse 				rb_set_parent(child, parent);
3711975e593SDavid Woodhouse 			parent->rb_left = child;
3724b324126SWolfram Strepp 
3734b324126SWolfram Strepp 			node->rb_right = old->rb_right;
3744b324126SWolfram Strepp 			rb_set_parent(old->rb_right, node);
3754c601178SWolfram Strepp 		}
3761975e593SDavid Woodhouse 
377bf7ad8eeSMichel Lespinasse 		node->__rb_parent_color = old->__rb_parent_color;
3781da177e4SLinus Torvalds 		node->rb_left = old->rb_left;
37955a98102SDavid Woodhouse 		rb_set_parent(old->rb_left, node);
3804b324126SWolfram Strepp 
3811da177e4SLinus Torvalds 		goto color;
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds 
38455a98102SDavid Woodhouse 	parent = rb_parent(node);
3852f3243aeSDavid Woodhouse 	color = rb_color(node);
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	if (child)
38855a98102SDavid Woodhouse 		rb_set_parent(child, parent);
389b945d6b2SPeter Zijlstra 	if (parent)
390b945d6b2SPeter Zijlstra 	{
3911da177e4SLinus Torvalds 		if (parent->rb_left == node)
3921da177e4SLinus Torvalds 			parent->rb_left = child;
3931da177e4SLinus Torvalds 		else
3941da177e4SLinus Torvalds 			parent->rb_right = child;
39517d9ddc7SPallipadi, Venkatesh 	}
396b945d6b2SPeter Zijlstra 	else
397b945d6b2SPeter Zijlstra 		root->rb_node = child;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds  color:
4001da177e4SLinus Torvalds 	if (color == RB_BLACK)
4011da177e4SLinus Torvalds 		__rb_erase_color(child, parent, root);
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds EXPORT_SYMBOL(rb_erase);
4041da177e4SLinus Torvalds 
405b945d6b2SPeter Zijlstra static void rb_augment_path(struct rb_node *node, rb_augment_f func, void *data)
406b945d6b2SPeter Zijlstra {
407b945d6b2SPeter Zijlstra 	struct rb_node *parent;
408b945d6b2SPeter Zijlstra 
409b945d6b2SPeter Zijlstra up:
410b945d6b2SPeter Zijlstra 	func(node, data);
411b945d6b2SPeter Zijlstra 	parent = rb_parent(node);
412b945d6b2SPeter Zijlstra 	if (!parent)
413b945d6b2SPeter Zijlstra 		return;
414b945d6b2SPeter Zijlstra 
415b945d6b2SPeter Zijlstra 	if (node == parent->rb_left && parent->rb_right)
416b945d6b2SPeter Zijlstra 		func(parent->rb_right, data);
417b945d6b2SPeter Zijlstra 	else if (parent->rb_left)
418b945d6b2SPeter Zijlstra 		func(parent->rb_left, data);
419b945d6b2SPeter Zijlstra 
420b945d6b2SPeter Zijlstra 	node = parent;
421b945d6b2SPeter Zijlstra 	goto up;
422b945d6b2SPeter Zijlstra }
423b945d6b2SPeter Zijlstra 
424b945d6b2SPeter Zijlstra /*
425b945d6b2SPeter Zijlstra  * after inserting @node into the tree, update the tree to account for
426b945d6b2SPeter Zijlstra  * both the new entry and any damage done by rebalance
427b945d6b2SPeter Zijlstra  */
428b945d6b2SPeter Zijlstra void rb_augment_insert(struct rb_node *node, rb_augment_f func, void *data)
429b945d6b2SPeter Zijlstra {
430b945d6b2SPeter Zijlstra 	if (node->rb_left)
431b945d6b2SPeter Zijlstra 		node = node->rb_left;
432b945d6b2SPeter Zijlstra 	else if (node->rb_right)
433b945d6b2SPeter Zijlstra 		node = node->rb_right;
434b945d6b2SPeter Zijlstra 
435b945d6b2SPeter Zijlstra 	rb_augment_path(node, func, data);
436b945d6b2SPeter Zijlstra }
4370b6bb66dSAndreas Gruenbacher EXPORT_SYMBOL(rb_augment_insert);
438b945d6b2SPeter Zijlstra 
439b945d6b2SPeter Zijlstra /*
440b945d6b2SPeter Zijlstra  * before removing the node, find the deepest node on the rebalance path
441b945d6b2SPeter Zijlstra  * that will still be there after @node gets removed
442b945d6b2SPeter Zijlstra  */
443b945d6b2SPeter Zijlstra struct rb_node *rb_augment_erase_begin(struct rb_node *node)
444b945d6b2SPeter Zijlstra {
445b945d6b2SPeter Zijlstra 	struct rb_node *deepest;
446b945d6b2SPeter Zijlstra 
447b945d6b2SPeter Zijlstra 	if (!node->rb_right && !node->rb_left)
448b945d6b2SPeter Zijlstra 		deepest = rb_parent(node);
449b945d6b2SPeter Zijlstra 	else if (!node->rb_right)
450b945d6b2SPeter Zijlstra 		deepest = node->rb_left;
451b945d6b2SPeter Zijlstra 	else if (!node->rb_left)
452b945d6b2SPeter Zijlstra 		deepest = node->rb_right;
453b945d6b2SPeter Zijlstra 	else {
454b945d6b2SPeter Zijlstra 		deepest = rb_next(node);
455b945d6b2SPeter Zijlstra 		if (deepest->rb_right)
456b945d6b2SPeter Zijlstra 			deepest = deepest->rb_right;
457b945d6b2SPeter Zijlstra 		else if (rb_parent(deepest) != node)
458b945d6b2SPeter Zijlstra 			deepest = rb_parent(deepest);
459b945d6b2SPeter Zijlstra 	}
460b945d6b2SPeter Zijlstra 
461b945d6b2SPeter Zijlstra 	return deepest;
462b945d6b2SPeter Zijlstra }
4630b6bb66dSAndreas Gruenbacher EXPORT_SYMBOL(rb_augment_erase_begin);
464b945d6b2SPeter Zijlstra 
465b945d6b2SPeter Zijlstra /*
466b945d6b2SPeter Zijlstra  * after removal, update the tree to account for the removed entry
467b945d6b2SPeter Zijlstra  * and any rebalance damage.
468b945d6b2SPeter Zijlstra  */
469b945d6b2SPeter Zijlstra void rb_augment_erase_end(struct rb_node *node, rb_augment_f func, void *data)
470b945d6b2SPeter Zijlstra {
471b945d6b2SPeter Zijlstra 	if (node)
472b945d6b2SPeter Zijlstra 		rb_augment_path(node, func, data);
473b945d6b2SPeter Zijlstra }
4740b6bb66dSAndreas Gruenbacher EXPORT_SYMBOL(rb_augment_erase_end);
475b945d6b2SPeter Zijlstra 
4761da177e4SLinus Torvalds /*
4771da177e4SLinus Torvalds  * This function returns the first node (in sort order) of the tree.
4781da177e4SLinus Torvalds  */
479f4b477c4SArtem Bityutskiy struct rb_node *rb_first(const struct rb_root *root)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	struct rb_node	*n;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	n = root->rb_node;
4841da177e4SLinus Torvalds 	if (!n)
4851da177e4SLinus Torvalds 		return NULL;
4861da177e4SLinus Torvalds 	while (n->rb_left)
4871da177e4SLinus Torvalds 		n = n->rb_left;
4881da177e4SLinus Torvalds 	return n;
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds EXPORT_SYMBOL(rb_first);
4911da177e4SLinus Torvalds 
492f4b477c4SArtem Bityutskiy struct rb_node *rb_last(const struct rb_root *root)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds 	struct rb_node	*n;
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	n = root->rb_node;
4971da177e4SLinus Torvalds 	if (!n)
4981da177e4SLinus Torvalds 		return NULL;
4991da177e4SLinus Torvalds 	while (n->rb_right)
5001da177e4SLinus Torvalds 		n = n->rb_right;
5011da177e4SLinus Torvalds 	return n;
5021da177e4SLinus Torvalds }
5031da177e4SLinus Torvalds EXPORT_SYMBOL(rb_last);
5041da177e4SLinus Torvalds 
505f4b477c4SArtem Bityutskiy struct rb_node *rb_next(const struct rb_node *node)
5061da177e4SLinus Torvalds {
50755a98102SDavid Woodhouse 	struct rb_node *parent;
50855a98102SDavid Woodhouse 
5094c199a93SMichel Lespinasse 	if (RB_EMPTY_NODE(node))
51010fd48f2SJens Axboe 		return NULL;
51110fd48f2SJens Axboe 
5121da177e4SLinus Torvalds 	/* If we have a right-hand child, go down and then left as far
5131da177e4SLinus Torvalds 	   as we can. */
5141da177e4SLinus Torvalds 	if (node->rb_right) {
5151da177e4SLinus Torvalds 		node = node->rb_right;
5161da177e4SLinus Torvalds 		while (node->rb_left)
5171da177e4SLinus Torvalds 			node=node->rb_left;
518f4b477c4SArtem Bityutskiy 		return (struct rb_node *)node;
5191da177e4SLinus Torvalds 	}
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	/* No right-hand children.  Everything down and left is
5221da177e4SLinus Torvalds 	   smaller than us, so any 'next' node must be in the general
5231da177e4SLinus Torvalds 	   direction of our parent. Go up the tree; any time the
5241da177e4SLinus Torvalds 	   ancestor is a right-hand child of its parent, keep going
5251da177e4SLinus Torvalds 	   up. First time it's a left-hand child of its parent, said
5261da177e4SLinus Torvalds 	   parent is our 'next' node. */
52755a98102SDavid Woodhouse 	while ((parent = rb_parent(node)) && node == parent->rb_right)
52855a98102SDavid Woodhouse 		node = parent;
5291da177e4SLinus Torvalds 
53055a98102SDavid Woodhouse 	return parent;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds EXPORT_SYMBOL(rb_next);
5331da177e4SLinus Torvalds 
534f4b477c4SArtem Bityutskiy struct rb_node *rb_prev(const struct rb_node *node)
5351da177e4SLinus Torvalds {
53655a98102SDavid Woodhouse 	struct rb_node *parent;
53755a98102SDavid Woodhouse 
5384c199a93SMichel Lespinasse 	if (RB_EMPTY_NODE(node))
53910fd48f2SJens Axboe 		return NULL;
54010fd48f2SJens Axboe 
5411da177e4SLinus Torvalds 	/* If we have a left-hand child, go down and then right as far
5421da177e4SLinus Torvalds 	   as we can. */
5431da177e4SLinus Torvalds 	if (node->rb_left) {
5441da177e4SLinus Torvalds 		node = node->rb_left;
5451da177e4SLinus Torvalds 		while (node->rb_right)
5461da177e4SLinus Torvalds 			node=node->rb_right;
547f4b477c4SArtem Bityutskiy 		return (struct rb_node *)node;
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	/* No left-hand children. Go up till we find an ancestor which
5511da177e4SLinus Torvalds 	   is a right-hand child of its parent */
55255a98102SDavid Woodhouse 	while ((parent = rb_parent(node)) && node == parent->rb_left)
55355a98102SDavid Woodhouse 		node = parent;
5541da177e4SLinus Torvalds 
55555a98102SDavid Woodhouse 	return parent;
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds EXPORT_SYMBOL(rb_prev);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds void rb_replace_node(struct rb_node *victim, struct rb_node *new,
5601da177e4SLinus Torvalds 		     struct rb_root *root)
5611da177e4SLinus Torvalds {
56255a98102SDavid Woodhouse 	struct rb_node *parent = rb_parent(victim);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* Set the surrounding nodes to point to the replacement */
5651da177e4SLinus Torvalds 	if (parent) {
5661da177e4SLinus Torvalds 		if (victim == parent->rb_left)
5671da177e4SLinus Torvalds 			parent->rb_left = new;
5681da177e4SLinus Torvalds 		else
5691da177e4SLinus Torvalds 			parent->rb_right = new;
5701da177e4SLinus Torvalds 	} else {
5711da177e4SLinus Torvalds 		root->rb_node = new;
5721da177e4SLinus Torvalds 	}
5731da177e4SLinus Torvalds 	if (victim->rb_left)
57455a98102SDavid Woodhouse 		rb_set_parent(victim->rb_left, new);
5751da177e4SLinus Torvalds 	if (victim->rb_right)
57655a98102SDavid Woodhouse 		rb_set_parent(victim->rb_right, new);
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	/* Copy the pointers/colour from the victim to the replacement */
5791da177e4SLinus Torvalds 	*new = *victim;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds EXPORT_SYMBOL(rb_replace_node);
582