1 /*
2   Red Black Trees
3   (C) 1999  Andrea Arcangeli <andrea@suse.de>
4   (C) 2002  David Woodhouse <dwmw2@infradead.org>
5   (C) 2012  Michel Lespinasse <walken@google.com>
6 
7   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, see <http://www.gnu.org/licenses/>.
19 
20   linux/lib/rbtree.c
21 */
22 
23 #include <xen/types.h>
24 #include <xen/rbtree.h>
25 
26 /*
27  * red-black trees properties:  http://en.wikipedia.org/wiki/Rbtree
28  *
29  *  1) A node is either red or black
30  *  2) The root is black
31  *  3) All leaves (NULL) are black
32  *  4) Both children of every red node are black
33  *  5) Every simple path from root to leaves contains the same number
34  *     of black nodes.
35  *
36  *  4 and 5 give the O(log n) guarantee, since 4 implies you cannot have two
37  *  consecutive red nodes in a path and every red node is therefore followed by
38  *  a black. So if B is the number of black nodes on every simple path (as per
39  *  5), then the longest possible path due to 4 is 2B.
40  *
41  *  We shall indicate color with case, where black nodes are uppercase and red
42  *  nodes will be lowercase. Unknown color nodes shall be drawn as red within
43  *  parentheses and have some accompanying text comment.
44  */
45 
46 #define		RB_RED		0
47 #define		RB_BLACK	1
48 
49 #define __rb_parent(pc)    ((struct rb_node *)((pc) & ~3))
50 
51 #define __rb_color(pc)     ((pc) & 1)
52 #define __rb_is_black(pc)  __rb_color(pc)
53 #define __rb_is_red(pc)    (!__rb_color(pc))
54 #define rb_color(rb)       __rb_color((rb)->__rb_parent_color)
55 #define rb_is_red(rb)      __rb_is_red((rb)->__rb_parent_color)
56 #define rb_is_black(rb)    __rb_is_black((rb)->__rb_parent_color)
57 
rb_set_black(struct rb_node * rb)58 static inline void rb_set_black(struct rb_node *rb)
59 {
60 	rb->__rb_parent_color |= RB_BLACK;
61 }
62 
rb_set_parent(struct rb_node * rb,struct rb_node * p)63 static inline void rb_set_parent(struct rb_node *rb, struct rb_node *p)
64 {
65 	rb->__rb_parent_color = rb_color(rb) | (unsigned long)p;
66 }
67 
rb_set_parent_color(struct rb_node * rb,struct rb_node * p,int color)68 static inline void rb_set_parent_color(struct rb_node *rb,
69 				      struct rb_node *p, int color)
70 {
71 	rb->__rb_parent_color = (unsigned long)p | color;
72 }
73 
rb_red_parent(struct rb_node * red)74 static inline struct rb_node *rb_red_parent(struct rb_node *red)
75 {
76 	return (struct rb_node *)red->__rb_parent_color;
77 }
78 
79 static inline void
__rb_change_child(struct rb_node * old,struct rb_node * new,struct rb_node * parent,struct rb_root * root)80 __rb_change_child(struct rb_node *old, struct rb_node *new,
81 		  struct rb_node *parent, struct rb_root *root)
82 {
83 	if (parent) {
84 		if (parent->rb_left == old)
85 			parent->rb_left = new;
86 		else
87 			parent->rb_right = new;
88 	} else
89 		root->rb_node = new;
90 }
91 
92 /*
93  * Helper function for rotations:
94  * - old's parent and color get assigned to new
95  * - old gets assigned new as a parent and 'color' as a color.
96  */
97 static inline void
__rb_rotate_set_parents(struct rb_node * old,struct rb_node * new,struct rb_root * root,int color)98 __rb_rotate_set_parents(struct rb_node *old, struct rb_node *new,
99 			struct rb_root *root, int color)
100 {
101 	struct rb_node *parent = rb_parent(old);
102 	new->__rb_parent_color = old->__rb_parent_color;
103 	rb_set_parent_color(old, new, color);
104 	__rb_change_child(old, new, parent, root);
105 }
106 
rb_insert_color(struct rb_node * node,struct rb_root * root)107 void rb_insert_color(struct rb_node *node, struct rb_root *root)
108 {
109 	struct rb_node *parent = rb_red_parent(node), *gparent, *tmp;
110 
111 	while (true) {
112 		/*
113 		 * Loop invariant: node is red
114 		 *
115 		 * If there is a black parent, we are done.
116 		 * Otherwise, take some corrective action as we don't
117 		 * want a red root or two consecutive red nodes.
118 		 */
119 		if (!parent) {
120 			rb_set_parent_color(node, NULL, RB_BLACK);
121 			break;
122 		} else if (rb_is_black(parent))
123 			break;
124 
125 		gparent = rb_red_parent(parent);
126 
127 		tmp = gparent->rb_right;
128 		if (parent != tmp) {    /* parent == gparent->rb_left */
129 			if (tmp && rb_is_red(tmp)) {
130 				/*
131 				 * Case 1 - color flips
132 				 *
133 				 *       G            g
134 				 *      / \          / \
135 				 *     p   u  -->   P   U
136 				 *    /            /
137 				 *   n            n
138 				 *
139 				 * However, since g's parent might be red, and
140 				 * 4) does not allow this, we need to recurse
141 				 * at g.
142 				 */
143 				rb_set_parent_color(tmp, gparent, RB_BLACK);
144 				rb_set_parent_color(parent, gparent, RB_BLACK);
145 				node = gparent;
146 				parent = rb_parent(node);
147 				rb_set_parent_color(node, parent, RB_RED);
148 				continue;
149 			}
150 
151 			tmp = parent->rb_right;
152 			if (node == tmp) {
153 				/*
154 				 * Case 2 - left rotate at parent
155 				 *
156 				 *      G             G
157 				 *     / \           / \
158 				 *    p   U  -->    n   U
159 				 *     \           /
160 				 *      n         p
161 				 *
162 				 * This still leaves us in violation of 4), the
163 				 * continuation into Case 3 will fix that.
164 				 */
165 				parent->rb_right = tmp = node->rb_left;
166 				node->rb_left = parent;
167 				if (tmp)
168 					rb_set_parent_color(tmp, parent,
169 							    RB_BLACK);
170 				rb_set_parent_color(parent, node, RB_RED);
171 				parent = node;
172 				tmp = node->rb_right;
173 			}
174 
175 			/*
176 			 * Case 3 - right rotate at gparent
177 			 *
178 			 *        G           P
179 			 *       / \         / \
180 			 *      p   U  -->  n   g
181 			 *     /                 \
182 			 *    n                   U
183 			 */
184 			gparent->rb_left = tmp;  /* == parent->rb_right */
185 			parent->rb_right = gparent;
186 			if (tmp)
187 				rb_set_parent_color(tmp, gparent, RB_BLACK);
188 			__rb_rotate_set_parents(gparent, parent, root, RB_RED);
189 			break;
190 		} else {
191 			tmp = gparent->rb_left;
192 			if (tmp && rb_is_red(tmp)) {
193 				/* Case 1 - color flips */
194 				rb_set_parent_color(tmp, gparent, RB_BLACK);
195 				rb_set_parent_color(parent, gparent, RB_BLACK);
196 				node = gparent;
197 				parent = rb_parent(node);
198 				rb_set_parent_color(node, parent, RB_RED);
199 				continue;
200 			}
201 
202 			tmp = parent->rb_left;
203 			if (node == tmp) {
204 				/* Case 2 - right rotate at parent */
205 				parent->rb_left = tmp = node->rb_right;
206 				node->rb_right = parent;
207 				if (tmp)
208 					rb_set_parent_color(tmp, parent,
209 							    RB_BLACK);
210 				rb_set_parent_color(parent, node, RB_RED);
211 				parent = node;
212 				tmp = node->rb_left;
213 			}
214 
215 			/* Case 3 - left rotate at gparent */
216 			gparent->rb_right = tmp;  /* == parent->rb_left */
217 			parent->rb_left = gparent;
218 			if (tmp)
219 				rb_set_parent_color(tmp, gparent, RB_BLACK);
220 			__rb_rotate_set_parents(gparent, parent, root, RB_RED);
221 			break;
222 		}
223 	}
224 }
225 
__rb_erase_color(struct rb_node * parent,struct rb_root * root)226 static void __rb_erase_color(struct rb_node *parent, struct rb_root *root)
227 {
228 	struct rb_node *node = NULL, *sibling, *tmp1, *tmp2;
229 
230 	while (true) {
231 		/*
232 		 * Loop invariants:
233 		 * - node is black (or NULL on first iteration)
234 		 * - node is not the root (parent is not NULL)
235 		 * - All leaf paths going through parent and node have a
236 		 *   black node count that is 1 lower than other leaf paths.
237 		 */
238 		sibling = parent->rb_right;
239 		if (node != sibling) {  /* node == parent->rb_left */
240 			if (rb_is_red(sibling)) {
241 				/*
242 				 * Case 1 - left rotate at parent
243 				 *
244 				 *     P               S
245 				 *    / \             / \
246 				 *   N   s    -->    p   Sr
247 				 *      / \         / \
248 				 *     Sl  Sr      N   Sl
249 				 */
250 				parent->rb_right = tmp1 = sibling->rb_left;
251 				sibling->rb_left = parent;
252 				rb_set_parent_color(tmp1, parent, RB_BLACK);
253 				__rb_rotate_set_parents(parent, sibling, root,
254 							RB_RED);
255 				sibling = tmp1;
256 			}
257 			tmp1 = sibling->rb_right;
258 			if (!tmp1 || rb_is_black(tmp1)) {
259 				tmp2 = sibling->rb_left;
260 				if (!tmp2 || rb_is_black(tmp2)) {
261 					/*
262 					* Case 2 - sibling color flip
263 					* (p could be either color here)
264 					*
265 					*    (p)           (p)
266 					*    / \           / \
267 					*   N   S    -->  N   s
268 					*      / \           / \
269 					*     Sl  Sr        Sl  Sr
270 					*
271 					* This leaves us violating 5) which
272 					* can be fixed by flipping p to black
273 					* if it was red, or by recursing at p.
274 					* p is red when coming from Case 1.
275 					*/
276 					rb_set_parent_color(sibling, parent,
277 							    RB_RED);
278 					if (rb_is_red(parent))
279 						rb_set_black(parent);
280 					else {
281 						node = parent;
282 						parent = rb_parent(node);
283 						if (parent)
284 							continue;
285 					}
286 					break;
287 				}
288 				/*
289 				 * Case 3 - right rotate at sibling
290 				 * (p could be either color here)
291 				 *
292 				 *   (p)           (p)
293 				 *   / \           / \
294 				 *  N   S    -->  N   Sl
295 				 *     / \             \
296 				 *    sl  Sr            s
297 				 *                       \
298 				 *                        Sr
299 				 */
300 				sibling->rb_left = tmp1 = tmp2->rb_right;
301 				tmp2->rb_right = sibling;
302 				parent->rb_right = tmp2;
303 				if (tmp1)
304 					rb_set_parent_color(tmp1, sibling,
305 							    RB_BLACK);
306 				tmp1 = sibling;
307 				sibling = tmp2;
308 			}
309 			/*
310 			 * Case 4 - left rotate at parent + color flips
311 			 * (p and sl could be either color here.
312 			 *  After rotation, p becomes black, s acquires
313 			 *  p's color, and sl keeps its color)
314 			 *
315 			 *      (p)             (s)
316 			 *      / \             / \
317 			 *     N   S     -->   P   Sr
318 			 *        / \         / \
319 			 *      (sl) sr      N  (sl)
320 			 */
321 			parent->rb_right = tmp2 = sibling->rb_left;
322 			sibling->rb_left = parent;
323 			rb_set_parent_color(tmp1, sibling, RB_BLACK);
324 			if (tmp2)
325 				rb_set_parent(tmp2, parent);
326 			__rb_rotate_set_parents(parent, sibling, root,
327 						RB_BLACK);
328 			break;
329 		} else {
330 			sibling = parent->rb_left;
331 			if (rb_is_red(sibling)) {
332 				/* Case 1 - right rotate at parent */
333 				parent->rb_left = tmp1 = sibling->rb_right;
334 				sibling->rb_right = parent;
335 				rb_set_parent_color(tmp1, parent, RB_BLACK);
336 				__rb_rotate_set_parents(parent, sibling, root,
337 							RB_RED);
338 				sibling = tmp1;
339 			}
340 			tmp1 = sibling->rb_left;
341 			if (!tmp1 || rb_is_black(tmp1)) {
342 				tmp2 = sibling->rb_right;
343 				if (!tmp2 || rb_is_black(tmp2)) {
344 					/* Case 2 - sibling color flip */
345 					rb_set_parent_color(sibling, parent,
346 							    RB_RED);
347 					if (rb_is_red(parent))
348 						rb_set_black(parent);
349 					else {
350 						node = parent;
351 						parent = rb_parent(node);
352 						if (parent)
353 							continue;
354 					}
355 					break;
356 				}
357 				/* Case 3 - right rotate at sibling */
358 				sibling->rb_right = tmp1 = tmp2->rb_left;
359 				tmp2->rb_left = sibling;
360 				parent->rb_left = tmp2;
361 				if (tmp1)
362 					rb_set_parent_color(tmp1, sibling,
363 							    RB_BLACK);
364 				tmp1 = sibling;
365 				sibling = tmp2;
366 			}
367 			/* Case 4 - left rotate at parent + color flips */
368 			parent->rb_left = tmp2 = sibling->rb_right;
369 			sibling->rb_right = parent;
370 			rb_set_parent_color(tmp1, sibling, RB_BLACK);
371 			if (tmp2)
372 				rb_set_parent(tmp2, parent);
373 			__rb_rotate_set_parents(parent, sibling, root,
374 						RB_BLACK);
375 			break;
376 		}
377 	}
378 }
379 
rb_erase(struct rb_node * node,struct rb_root * root)380 void rb_erase(struct rb_node *node, struct rb_root *root)
381 {
382 	struct rb_node *child = node->rb_right, *tmp = node->rb_left;
383 	struct rb_node *parent, *rebalance;
384 	unsigned long pc;
385 
386 	if (!tmp) {
387 		/*
388 		 * Case 1: node to erase has no more than 1 child (easy!)
389 		 *
390 		 * Note that if there is one child it must be red due to 5)
391 		 * and node must be black due to 4). We adjust colors locally
392 		 * so as to bypass __rb_erase_color() later on.
393 		 */
394 		pc = node->__rb_parent_color;
395 		parent = __rb_parent(pc);
396 		__rb_change_child(node, child, parent, root);
397 		if (child) {
398 			child->__rb_parent_color = pc;
399 			rebalance = NULL;
400 		} else
401 			rebalance = __rb_is_black(pc) ? parent : NULL;
402 	} else if (!child) {
403 		/* Still case 1, but this time the child is node->rb_left */
404 		tmp->__rb_parent_color = pc = node->__rb_parent_color;
405 		parent = __rb_parent(pc);
406 		__rb_change_child(node, tmp, parent, root);
407 		rebalance = NULL;
408 	} else {
409 		struct rb_node *successor = child, *child2;
410 		tmp = child->rb_left;
411 		if (!tmp) {
412 			/*
413 			 * Case 2: node's successor is its right child
414 			 *
415 			 *    (n)          (s)
416 			 *    / \          / \
417 			 *  (x) (s)  ->  (x) (c)
418 			 *        \
419 			 *        (c)
420 			 */
421 			parent = child;
422 			child2 = child->rb_right;
423 		} else {
424 			/*
425 			 * Case 3: node's successor is leftmost under
426 			 * node's right child subtree
427 			 *
428 			 *    (n)          (s)
429 			 *    / \          / \
430 			 *  (x) (y)  ->  (x) (y)
431 			 *      /            /
432 			 *    (p)          (p)
433 			 *    /            /
434 			 *  (s)          (c)
435 			 *    \
436 			 *    (c)
437 			 */
438 			do {
439 				parent = successor;
440 				successor = tmp;
441 				tmp = tmp->rb_left;
442 			} while (tmp);
443 			parent->rb_left = child2 = successor->rb_right;
444 			successor->rb_right = child;
445 			rb_set_parent(child, successor);
446 		}
447 
448 		successor->rb_left = tmp = node->rb_left;
449 		rb_set_parent(tmp, successor);
450 
451 		pc = node->__rb_parent_color;
452 		tmp = __rb_parent(pc);
453 		__rb_change_child(node, successor, tmp, root);
454 		if (child2) {
455 			successor->__rb_parent_color = pc;
456 			rb_set_parent_color(child2, parent, RB_BLACK);
457 			rebalance = NULL;
458 		} else {
459 			unsigned long pc2 = successor->__rb_parent_color;
460 			successor->__rb_parent_color = pc;
461 			rebalance = __rb_is_black(pc2) ? parent : NULL;
462 		}
463 	}
464 
465 	if (rebalance)
466 		__rb_erase_color(rebalance, root);
467 }
468 
469 /*
470  * This function returns the first node (in sort order) of the tree.
471  */
rb_first(const struct rb_root * root)472 struct rb_node *rb_first(const struct rb_root *root)
473 {
474 	struct rb_node	*n;
475 
476 	n = root->rb_node;
477 	if (!n)
478 		return NULL;
479 	while (n->rb_left)
480 		n = n->rb_left;
481 	return n;
482 }
483 
rb_last(const struct rb_root * root)484 struct rb_node *rb_last(const struct rb_root *root)
485 {
486 	struct rb_node	*n;
487 
488 	n = root->rb_node;
489 	if (!n)
490 		return NULL;
491 	while (n->rb_right)
492 		n = n->rb_right;
493 	return n;
494 }
495 
rb_next(const struct rb_node * node)496 struct rb_node *rb_next(const struct rb_node *node)
497 {
498 	struct rb_node *parent;
499 
500 	if (RB_EMPTY_NODE(node))
501 		return NULL;
502 
503 	/*
504 	 * If we have a right-hand child, go down and then left as far
505 	 * as we can.
506 	 */
507 	if (node->rb_right) {
508 		node = node->rb_right;
509 		while (node->rb_left)
510 			node=node->rb_left;
511 		return (struct rb_node *)node;
512 	}
513 
514 	/*
515 	 * No right-hand children. Everything down and left is smaller than us,
516 	 * so any 'next' node must be in the general direction of our parent.
517 	 * Go up the tree; any time the ancestor is a right-hand child of its
518 	 * parent, keep going up. First time it's a left-hand child of its
519 	 * parent, said parent is our 'next' node.
520 	 */
521 	while ((parent = rb_parent(node)) && node == parent->rb_right)
522 		node = parent;
523 
524 	return parent;
525 }
526 
rb_prev(const struct rb_node * node)527 struct rb_node *rb_prev(const struct rb_node *node)
528 {
529 	struct rb_node *parent;
530 
531 	if (RB_EMPTY_NODE(node))
532 		return NULL;
533 
534 	/*
535 	 * If we have a left-hand child, go down and then right as far
536 	 * as we can.
537 	 */
538 	if (node->rb_left) {
539 		node = node->rb_left;
540 		while (node->rb_right)
541 			node=node->rb_right;
542 		return (struct rb_node *)node;
543 	}
544 
545 	/*
546 	 * No left-hand children. Go up till we find an ancestor which
547 	 * is a right-hand child of its parent
548 	 */
549 	while ((parent = rb_parent(node)) && node == parent->rb_left)
550 		node = parent;
551 
552 	return parent;
553 }
554 
rb_replace_node(struct rb_node * victim,struct rb_node * new,struct rb_root * root)555 void rb_replace_node(struct rb_node *victim, struct rb_node *new,
556 		     struct rb_root *root)
557 {
558 	struct rb_node *parent = rb_parent(victim);
559 
560 	/* Set the surrounding nodes to point to the replacement */
561 	__rb_change_child(victim, new, parent, root);
562 	if (victim->rb_left)
563 		rb_set_parent(victim->rb_left, new);
564 	if (victim->rb_right)
565 		rb_set_parent(victim->rb_right, new);
566 
567 	/* Copy the pointers/colour from the victim to the replacement */
568 	*new = *victim;
569 }
570