1 /* $NetBSD: prop_rb.c,v 1.9 2008/06/17 21:29:47 thorpej Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Matt Thomas <matt@3am-software.com>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <prop/proplib.h>
33 #include "prop_object_impl.h"
34 #include "prop_rb_impl.h"
38 #define KASSERT(x) _PROP_ASSERT(x)
40 #define KASSERT(x) /* nothing */
43 #ifndef __predict_false
44 #define __predict_false(x) (x)
47 static void rb_tree_reparent_nodes(struct rb_tree
*, struct rb_node
*,
49 static void rb_tree_insert_rebalance(struct rb_tree
*, struct rb_node
*);
50 static void rb_tree_removal_rebalance(struct rb_tree
*, struct rb_node
*,
53 static const struct rb_node
*rb_tree_iterate_const(const struct rb_tree
*,
54 const struct rb_node
*, unsigned int);
55 static bool rb_tree_check_node(const struct rb_tree
*, const struct rb_node
*,
56 const struct rb_node
*, bool);
60 #define RBT_COUNT_INCR(rbt) (rbt)->rbt_count++
61 #define RBT_COUNT_DECR(rbt) (rbt)->rbt_count--
63 #define RBT_COUNT_INCR(rbt) /* nothing */
64 #define RBT_COUNT_DECR(rbt) /* nothing */
67 #define RBUNCONST(a) ((void *)(unsigned long)(const void *)(a))
70 * Rather than testing for the NULL everywhere, all terminal leaves are
71 * pointed to this node (and that includes itself). Note that by setting
72 * it to be const, that on some architectures trying to write to it will
75 static const struct rb_node sentinel_node
= {
76 { RBUNCONST(&sentinel_node
),
77 RBUNCONST(&sentinel_node
),
79 #if BYTE_ORDER == LITTLE_ENDIAN
82 #if BYTE_ORDER == BIG_ENDIAN
88 _prop_rb_tree_init(struct rb_tree
*rbt
, const struct rb_tree_ops
*ops
)
90 RB_TAILQ_INIT(&rbt
->rbt_nodes
);
95 *((const struct rb_node
**)&rbt
->rbt_root
) = &sentinel_node
;
99 * Swap the location and colors of 'self' and its child @ which. The child
100 * can not be a sentinel node.
104 rb_tree_reparent_nodes(struct rb_tree
*rbt _PROP_ARG_UNUSED
,
105 struct rb_node
*old_father
, unsigned int which
)
107 const unsigned int other
= which
^ RB_NODE_OTHER
;
108 struct rb_node
* const grandpa
= old_father
->rb_parent
;
109 struct rb_node
* const old_child
= old_father
->rb_nodes
[which
];
110 struct rb_node
* const new_father
= old_child
;
111 struct rb_node
* const new_child
= old_father
;
112 unsigned int properties
;
114 KASSERT(which
== RB_NODE_LEFT
|| which
== RB_NODE_RIGHT
);
116 KASSERT(!RB_SENTINEL_P(old_child
));
117 KASSERT(old_child
->rb_parent
== old_father
);
119 KASSERT(rb_tree_check_node(rbt
, old_father
, NULL
, false));
120 KASSERT(rb_tree_check_node(rbt
, old_child
, NULL
, false));
121 KASSERT(RB_ROOT_P(old_father
) || rb_tree_check_node(rbt
, grandpa
, NULL
, false));
124 * Exchange descendant linkages.
126 grandpa
->rb_nodes
[old_father
->rb_position
] = new_father
;
127 new_child
->rb_nodes
[which
] = old_child
->rb_nodes
[other
];
128 new_father
->rb_nodes
[other
] = new_child
;
131 * Update ancestor linkages
133 new_father
->rb_parent
= grandpa
;
134 new_child
->rb_parent
= new_father
;
137 * Exchange properties between new_father and new_child. The only
138 * change is that new_child's position is now on the other side.
140 properties
= old_child
->rb_properties
;
141 new_father
->rb_properties
= old_father
->rb_properties
;
142 new_child
->rb_properties
= properties
;
143 new_child
->rb_position
= other
;
146 * Make sure to reparent the new child to ourself.
148 if (!RB_SENTINEL_P(new_child
->rb_nodes
[which
])) {
149 new_child
->rb_nodes
[which
]->rb_parent
= new_child
;
150 new_child
->rb_nodes
[which
]->rb_position
= which
;
153 KASSERT(rb_tree_check_node(rbt
, new_father
, NULL
, false));
154 KASSERT(rb_tree_check_node(rbt
, new_child
, NULL
, false));
155 KASSERT(RB_ROOT_P(new_father
) || rb_tree_check_node(rbt
, grandpa
, NULL
, false));
159 _prop_rb_tree_insert_node(struct rb_tree
*rbt
, struct rb_node
*self
)
161 struct rb_node
*parent
, *tmp
;
162 rb_compare_nodes_fn compare_nodes
= rbt
->rbt_ops
->rbto_compare_nodes
;
163 unsigned int position
;
165 self
->rb_properties
= 0;
168 * This is a hack. Because rbt->rbt_root is just a struct rb_node *,
169 * just like rb_node->rb_nodes[RB_NODE_LEFT], we can use this fact to
170 * avoid a lot of tests for root and know that even at root,
171 * updating rb_node->rb_parent->rb_nodes[rb_node->rb_position] will
174 /* LINTED: see above */
175 parent
= (struct rb_node
*)&rbt
->rbt_root
;
176 position
= RB_NODE_LEFT
;
179 * Find out where to place this new leaf.
181 while (!RB_SENTINEL_P(tmp
)) {
182 const int diff
= (*compare_nodes
)(tmp
, self
);
183 if (__predict_false(diff
== 0)) {
185 * Node already exists; don't insert.
192 position
= RB_NODE_LEFT
;
194 position
= RB_NODE_RIGHT
;
196 tmp
= parent
->rb_nodes
[position
];
201 struct rb_node
*prev
= NULL
, *next
= NULL
;
203 if (position
== RB_NODE_RIGHT
)
205 else if (tmp
!= rbt
->rbt_root
)
209 * Verify our sequential position
211 KASSERT(prev
== NULL
|| !RB_SENTINEL_P(prev
));
212 KASSERT(next
== NULL
|| !RB_SENTINEL_P(next
));
213 if (prev
!= NULL
&& next
== NULL
)
214 next
= TAILQ_NEXT(prev
, rb_link
);
215 if (prev
== NULL
&& next
!= NULL
)
216 prev
= TAILQ_PREV(next
, rb_node_qh
, rb_link
);
217 KASSERT(prev
== NULL
|| !RB_SENTINEL_P(prev
));
218 KASSERT(next
== NULL
|| !RB_SENTINEL_P(next
));
220 || (*compare_nodes
)(prev
, self
) > 0);
222 || (*compare_nodes
)(self
, next
) > 0);
227 * Initialize the node and insert as a leaf into the tree.
229 self
->rb_parent
= parent
;
230 self
->rb_position
= position
;
231 /* LINTED: rbt_root hack */
232 if (__predict_false(parent
== (struct rb_node
*) &rbt
->rbt_root
)) {
235 KASSERT(position
== RB_NODE_LEFT
|| position
== RB_NODE_RIGHT
);
236 KASSERT(!RB_ROOT_P(self
)); /* Already done */
238 KASSERT(RB_SENTINEL_P(parent
->rb_nodes
[position
]));
239 self
->rb_left
= parent
->rb_nodes
[position
];
240 self
->rb_right
= parent
->rb_nodes
[position
];
241 parent
->rb_nodes
[position
] = self
;
242 KASSERT(self
->rb_left
== &sentinel_node
&&
243 self
->rb_right
== &sentinel_node
);
246 * Insert the new node into a sorted list for easy sequential access
250 if (RB_ROOT_P(self
)) {
251 RB_TAILQ_INSERT_HEAD(&rbt
->rbt_nodes
, self
, rb_link
);
252 } else if (position
== RB_NODE_LEFT
) {
253 KASSERT((*compare_nodes
)(self
, self
->rb_parent
) > 0);
254 RB_TAILQ_INSERT_BEFORE(self
->rb_parent
, self
, rb_link
);
256 KASSERT((*compare_nodes
)(self
->rb_parent
, self
) > 0);
257 RB_TAILQ_INSERT_AFTER(&rbt
->rbt_nodes
, self
->rb_parent
,
264 * Validate the tree before we rebalance
266 _prop_rb_tree_check(rbt
, false);
270 * Rebalance tree after insertion
272 rb_tree_insert_rebalance(rbt
, self
);
276 * Validate the tree after we rebalanced
278 _prop_rb_tree_check(rbt
, true);
285 rb_tree_insert_rebalance(struct rb_tree
*rbt
, struct rb_node
*self
)
289 while (!RB_ROOT_P(self
) && RB_RED_P(self
->rb_parent
)) {
290 const unsigned int which
=
291 (self
->rb_parent
== self
->rb_parent
->rb_parent
->rb_left
294 const unsigned int other
= which
^ RB_NODE_OTHER
;
295 struct rb_node
* father
= self
->rb_parent
;
296 struct rb_node
* grandpa
= father
->rb_parent
;
297 struct rb_node
* const uncle
= grandpa
->rb_nodes
[other
];
299 KASSERT(!RB_SENTINEL_P(self
));
301 * We are red and our parent is red, therefore we must have a
302 * grandfather and he must be black.
304 KASSERT(RB_RED_P(self
)
306 && RB_BLACK_P(grandpa
));
308 if (RB_RED_P(uncle
)) {
310 * Case 1: our uncle is red
311 * Simply invert the colors of our parent and
312 * uncle and make our grandparent red. And
313 * then solve the problem up at his level.
315 RB_MARK_BLACK(uncle
);
316 RB_MARK_BLACK(father
);
317 RB_MARK_RED(grandpa
);
322 * Case 2&3: our uncle is black.
324 if (self
== father
->rb_nodes
[other
]) {
326 * Case 2: we are on the same side as our uncle
327 * Swap ourselves with our parent so this case
328 * becomes case 3. Basically our parent becomes our
331 rb_tree_reparent_nodes(rbt
, father
, other
);
332 KASSERT(father
->rb_parent
== self
);
333 KASSERT(self
->rb_nodes
[which
] == father
);
334 KASSERT(self
->rb_parent
== grandpa
);
336 KASSERT(RB_RED_P(self
) && RB_RED_P(father
));
337 KASSERT(grandpa
->rb_nodes
[which
] == father
);
339 * Case 3: we are opposite a child of a black uncle.
340 * Swap our parent and grandparent. Since our grandfather
341 * is black, our father will become black and our new sibling
342 * (former grandparent) will become red.
344 rb_tree_reparent_nodes(rbt
, grandpa
, which
);
345 KASSERT(self
->rb_parent
== father
);
346 KASSERT(self
->rb_parent
->rb_nodes
[self
->rb_position
^ RB_NODE_OTHER
] == grandpa
);
347 KASSERT(RB_RED_P(self
));
348 KASSERT(RB_BLACK_P(father
));
349 KASSERT(RB_RED_P(grandpa
));
354 * Final step: Set the root to black.
356 RB_MARK_BLACK(rbt
->rbt_root
);
360 _prop_rb_tree_find(struct rb_tree
*rbt
, const void *key
)
362 struct rb_node
*parent
= rbt
->rbt_root
;
363 rb_compare_key_fn compare_key
= rbt
->rbt_ops
->rbto_compare_key
;
365 while (!RB_SENTINEL_P(parent
)) {
366 const int diff
= (*compare_key
)(parent
, key
);
369 parent
= parent
->rb_nodes
[diff
> 0];
376 rb_tree_prune_node(struct rb_tree
*rbt
, struct rb_node
*self
, int rebalance
)
378 const unsigned int which
= self
->rb_position
;
379 struct rb_node
*father
= self
->rb_parent
;
381 KASSERT(rebalance
|| (RB_ROOT_P(self
) || RB_RED_P(self
)));
382 KASSERT(!rebalance
|| RB_BLACK_P(self
));
383 KASSERT(RB_CHILDLESS_P(self
));
384 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
386 father
->rb_nodes
[which
] = self
->rb_left
;
389 * Remove ourselves from the node list and decrement the count.
391 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
395 rb_tree_removal_rebalance(rbt
, father
, which
);
396 KASSERT(RB_ROOT_P(self
) || rb_tree_check_node(rbt
, father
, NULL
, true));
400 rb_tree_swap_prune_and_rebalance(struct rb_tree
*rbt
, struct rb_node
*self
,
401 struct rb_node
*standin
)
403 unsigned int standin_which
= standin
->rb_position
;
404 unsigned int standin_other
= standin_which
^ RB_NODE_OTHER
;
405 struct rb_node
*standin_child
;
406 struct rb_node
*standin_father
;
407 bool rebalance
= RB_BLACK_P(standin
);
409 if (standin
->rb_parent
== self
) {
411 * As a child of self, any childen would be opposite of
414 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_other
]));
415 standin_child
= standin
->rb_nodes
[standin_which
];
418 * Since we aren't a child of self, any childen would be
419 * on the same side as our parent (self).
421 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_which
]));
422 standin_child
= standin
->rb_nodes
[standin_other
];
426 * the node we are removing must have two children.
428 KASSERT(RB_TWOCHILDREN_P(self
));
430 * If standin has a child, it must be red.
432 KASSERT(RB_SENTINEL_P(standin_child
) || RB_RED_P(standin_child
));
435 * Verify things are sane.
437 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
438 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, false));
440 if (!RB_SENTINEL_P(standin_child
)) {
442 * We know we have a red child so if we swap them we can
443 * void flipping standin's child to black afterwards.
445 KASSERT(rb_tree_check_node(rbt
, standin_child
, NULL
, true));
446 rb_tree_reparent_nodes(rbt
, standin
,
447 standin_child
->rb_position
);
448 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, true));
449 KASSERT(rb_tree_check_node(rbt
, standin_child
, NULL
, true));
451 * Since we are removing a red leaf, no need to rebalance.
455 * We know that standin can not be a child of self, so
456 * update before of that.
458 KASSERT(standin
->rb_parent
!= self
);
459 standin_which
= standin
->rb_position
;
460 standin_other
= standin_which
^ RB_NODE_OTHER
;
462 KASSERT(RB_CHILDLESS_P(standin
));
465 * If we are about to delete the standin's father, then when we call
466 * rebalance, we need to use ourselves as our father. Otherwise
467 * remember our original father. Also, if we are our standin's father
468 * we only need to reparent the standin's brother.
470 if (standin
->rb_parent
== self
) {
476 standin_father
= standin
;
477 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_other
]));
478 KASSERT(!RB_SENTINEL_P(self
->rb_nodes
[standin_other
]));
479 KASSERT(self
->rb_nodes
[standin_which
] == standin
);
481 * Make our brother our son.
483 standin
->rb_nodes
[standin_other
] = self
->rb_nodes
[standin_other
];
484 standin
->rb_nodes
[standin_other
]->rb_parent
= standin
;
485 KASSERT(standin
->rb_nodes
[standin_other
]->rb_position
== standin_other
);
492 standin_father
= standin
->rb_parent
;
493 standin_father
->rb_nodes
[standin_which
] =
494 standin
->rb_nodes
[standin_which
];
495 standin
->rb_left
= self
->rb_left
;
496 standin
->rb_right
= self
->rb_right
;
497 standin
->rb_left
->rb_parent
= standin
;
498 standin
->rb_right
->rb_parent
= standin
;
502 * Now copy the result of self to standin and then replace
503 * self with standin in the tree.
505 standin
->rb_parent
= self
->rb_parent
;
506 standin
->rb_properties
= self
->rb_properties
;
507 standin
->rb_parent
->rb_nodes
[standin
->rb_position
] = standin
;
510 * Remove ourselves from the node list and decrement the count.
512 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
515 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, false));
516 KASSERT(rb_tree_check_node(rbt
, standin_father
, NULL
, false));
521 rb_tree_removal_rebalance(rbt
, standin_father
, standin_which
);
522 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, true));
526 * We could do this by doing
527 * rb_tree_node_swap(rbt, self, which);
528 * rb_tree_prune_node(rbt, self, false);
530 * But it's more efficient to just evalate and recolor the child.
534 rb_tree_prune_blackred_branch(struct rb_tree
*rbt _PROP_ARG_UNUSED
,
535 struct rb_node
*self
, unsigned int which
)
537 struct rb_node
*parent
= self
->rb_parent
;
538 struct rb_node
*child
= self
->rb_nodes
[which
];
540 KASSERT(which
== RB_NODE_LEFT
|| which
== RB_NODE_RIGHT
);
541 KASSERT(RB_BLACK_P(self
) && RB_RED_P(child
));
542 KASSERT(!RB_TWOCHILDREN_P(child
));
543 KASSERT(RB_CHILDLESS_P(child
));
544 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
545 KASSERT(rb_tree_check_node(rbt
, child
, NULL
, false));
548 * Remove ourselves from the tree and give our former child our
549 * properties (position, color, root).
551 parent
->rb_nodes
[self
->rb_position
] = child
;
552 child
->rb_parent
= parent
;
553 child
->rb_properties
= self
->rb_properties
;
556 * Remove ourselves from the node list and decrement the count.
558 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
561 KASSERT(RB_ROOT_P(self
) || rb_tree_check_node(rbt
, parent
, NULL
, true));
562 KASSERT(rb_tree_check_node(rbt
, child
, NULL
, true));
568 _prop_rb_tree_remove_node(struct rb_tree
*rbt
, struct rb_node
*self
)
570 struct rb_node
*standin
;
573 * In the following diagrams, we (the node to be removed) are S. Red
574 * nodes are lowercase. T could be either red or black.
576 * Remember the major axiom of the red-black tree: the number of
577 * black nodes from the root to each leaf is constant across all
578 * leaves, only the number of red nodes varies.
580 * Thus removing a red leaf doesn't require any other changes to a
581 * red-black tree. So if we must remove a node, attempt to rearrange
582 * the tree so we can remove a red node.
584 * The simpliest case is a childless red node or a childless root node:
586 * | T --> T | or | R --> * |
589 if (RB_CHILDLESS_P(self
)) {
590 if (RB_RED_P(self
) || RB_ROOT_P(self
)) {
591 rb_tree_prune_node(rbt
, self
, false);
594 rb_tree_prune_node(rbt
, self
, true);
597 KASSERT(!RB_CHILDLESS_P(self
));
598 if (!RB_TWOCHILDREN_P(self
)) {
600 * The next simpliest case is the node we are deleting is
601 * black and has one red child.
607 which
= RB_LEFT_SENTINEL_P(self
) ? RB_NODE_RIGHT
: RB_NODE_LEFT
;
608 KASSERT(RB_BLACK_P(self
));
609 KASSERT(RB_RED_P(self
->rb_nodes
[which
]));
610 KASSERT(RB_CHILDLESS_P(self
->rb_nodes
[which
]));
611 rb_tree_prune_blackred_branch(rbt
, self
, which
);
614 KASSERT(RB_TWOCHILDREN_P(self
));
617 * We invert these because we prefer to remove from the inside of
620 which
= self
->rb_position
^ RB_NODE_OTHER
;
623 * Let's find the node closes to us opposite of our parent
624 * Now swap it with ourself, "prune" it, and rebalance, if needed.
626 standin
= _prop_rb_tree_iterate(rbt
, self
, which
);
627 rb_tree_swap_prune_and_rebalance(rbt
, self
, standin
);
631 rb_tree_removal_rebalance(struct rb_tree
*rbt
, struct rb_node
*parent
,
634 KASSERT(!RB_SENTINEL_P(parent
));
635 KASSERT(RB_SENTINEL_P(parent
->rb_nodes
[which
]));
636 KASSERT(which
== RB_NODE_LEFT
|| which
== RB_NODE_RIGHT
);
638 while (RB_BLACK_P(parent
->rb_nodes
[which
])) {
639 unsigned int other
= which
^ RB_NODE_OTHER
;
640 struct rb_node
*brother
= parent
->rb_nodes
[other
];
642 KASSERT(!RB_SENTINEL_P(brother
));
644 * For cases 1, 2a, and 2b, our brother's children must
645 * be black and our father must be black
647 if (RB_BLACK_P(parent
)
648 && RB_BLACK_P(brother
->rb_left
)
649 && RB_BLACK_P(brother
->rb_right
)) {
651 * Case 1: Our brother is red, swap its position
652 * (and colors) with our parent. This is now case 2b.
658 if (RB_RED_P(brother
)) {
659 KASSERT(RB_BLACK_P(parent
));
660 rb_tree_reparent_nodes(rbt
, parent
, other
);
661 KASSERT(!RB_SENTINEL_P(brother
));
662 KASSERT(RB_BLACK_P(brother
));
663 KASSERT(RB_RED_P(parent
));
664 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, false));
665 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, false));
668 * Both our parent and brother are black.
669 * Change our brother to red, advance up rank
670 * and go through the loop again.
676 RB_MARK_RED(brother
);
677 KASSERT(RB_BLACK_P(brother
->rb_left
));
678 KASSERT(RB_BLACK_P(brother
->rb_right
));
679 if (RB_ROOT_P(parent
))
681 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, false));
682 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, false));
683 which
= parent
->rb_position
;
684 parent
= parent
->rb_parent
;
686 } else if (RB_RED_P(parent
)
687 && RB_BLACK_P(brother
)
688 && RB_BLACK_P(brother
->rb_left
)
689 && RB_BLACK_P(brother
->rb_right
)) {
690 KASSERT(RB_BLACK_P(brother
));
691 KASSERT(RB_BLACK_P(brother
->rb_left
));
692 KASSERT(RB_BLACK_P(brother
->rb_right
));
693 RB_MARK_BLACK(parent
);
694 RB_MARK_RED(brother
);
695 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, true));
696 break; /* We're done! */
698 KASSERT(RB_BLACK_P(brother
));
699 KASSERT(!RB_CHILDLESS_P(brother
));
701 * Case 3: our brother is black, our left nephew is
702 * red, and our right nephew is black. Swap our
703 * brother with our left nephew. This result in a
704 * tree that matches case 4.
710 if (RB_BLACK_P(brother
->rb_nodes
[other
])) {
711 KASSERT(RB_RED_P(brother
->rb_nodes
[which
]));
712 rb_tree_reparent_nodes(rbt
, brother
, which
);
713 KASSERT(brother
->rb_parent
== parent
->rb_nodes
[other
]);
714 brother
= parent
->rb_nodes
[other
];
715 KASSERT(RB_RED_P(brother
->rb_nodes
[other
]));
718 * Case 4: our brother is black and our right nephew
719 * is red. Swap our parent and brother locations and
720 * change our right nephew to black. (these can be
721 * done in either order so we change the color first).
722 * The result is a valid red-black tree and is a
729 RB_MARK_BLACK(brother
->rb_nodes
[other
]);
730 rb_tree_reparent_nodes(rbt
, parent
, other
);
731 break; /* We're done! */
734 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, true));
738 _prop_rb_tree_iterate(struct rb_tree
*rbt
, struct rb_node
*self
,
739 unsigned int direction
)
741 const unsigned int other
= direction
^ RB_NODE_OTHER
;
742 KASSERT(direction
== RB_NODE_LEFT
|| direction
== RB_NODE_RIGHT
);
745 self
= rbt
->rbt_root
;
746 if (RB_SENTINEL_P(self
))
748 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
749 self
= self
->rb_nodes
[other
];
752 KASSERT(!RB_SENTINEL_P(self
));
754 * We can't go any further in this direction. We proceed up in the
755 * opposite direction until our parent is in direction we want to go.
757 if (RB_SENTINEL_P(self
->rb_nodes
[direction
])) {
758 while (!RB_ROOT_P(self
)) {
759 if (other
== self
->rb_position
)
760 return self
->rb_parent
;
761 self
= self
->rb_parent
;
767 * Advance down one in current direction and go down as far as possible
768 * in the opposite direction.
770 self
= self
->rb_nodes
[direction
];
771 KASSERT(!RB_SENTINEL_P(self
));
772 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
773 self
= self
->rb_nodes
[other
];
778 static const struct rb_node
*
779 rb_tree_iterate_const(const struct rb_tree
*rbt
, const struct rb_node
*self
,
780 unsigned int direction
)
782 const unsigned int other
= direction
^ RB_NODE_OTHER
;
783 KASSERT(direction
== RB_NODE_LEFT
|| direction
== RB_NODE_RIGHT
);
786 self
= rbt
->rbt_root
;
787 if (RB_SENTINEL_P(self
))
789 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
790 self
= self
->rb_nodes
[other
];
793 KASSERT(!RB_SENTINEL_P(self
));
795 * We can't go any further in this direction. We proceed up in the
796 * opposite direction until our parent is in direction we want to go.
798 if (RB_SENTINEL_P(self
->rb_nodes
[direction
])) {
799 while (!RB_ROOT_P(self
)) {
800 if (other
== self
->rb_position
)
801 return self
->rb_parent
;
802 self
= self
->rb_parent
;
808 * Advance down one in current direction and go down as far as possible
809 * in the opposite direction.
811 self
= self
->rb_nodes
[direction
];
812 KASSERT(!RB_SENTINEL_P(self
));
813 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
814 self
= self
->rb_nodes
[other
];
819 rb_tree_check_node(const struct rb_tree
*rbt
, const struct rb_node
*self
,
820 const struct rb_node
*prev
, bool red_check
)
822 KASSERT(!self
->rb_sentinel
);
823 KASSERT(self
->rb_left
);
824 KASSERT(self
->rb_right
);
825 KASSERT(prev
== NULL
||
826 (*rbt
->rbt_ops
->rbto_compare_nodes
)(prev
, self
) > 0);
829 * Verify our relationship to our parent.
831 if (RB_ROOT_P(self
)) {
832 KASSERT(self
== rbt
->rbt_root
);
833 KASSERT(self
->rb_position
== RB_NODE_LEFT
);
834 KASSERT(self
->rb_parent
->rb_nodes
[RB_NODE_LEFT
] == self
);
835 KASSERT(self
->rb_parent
== (const struct rb_node
*) &rbt
->rbt_root
);
837 KASSERT(self
!= rbt
->rbt_root
);
838 KASSERT(!RB_PARENT_SENTINEL_P(self
));
839 if (self
->rb_position
== RB_NODE_LEFT
) {
840 KASSERT((*rbt
->rbt_ops
->rbto_compare_nodes
)(self
, self
->rb_parent
) > 0);
841 KASSERT(self
->rb_parent
->rb_nodes
[RB_NODE_LEFT
] == self
);
843 KASSERT((*rbt
->rbt_ops
->rbto_compare_nodes
)(self
, self
->rb_parent
) < 0);
844 KASSERT(self
->rb_parent
->rb_nodes
[RB_NODE_RIGHT
] == self
);
849 * Verify our position in the linked list against the tree itself.
852 const struct rb_node
*prev0
= rb_tree_iterate_const(rbt
, self
, RB_NODE_LEFT
);
853 const struct rb_node
*next0
= rb_tree_iterate_const(rbt
, self
, RB_NODE_RIGHT
);
854 KASSERT(prev0
== TAILQ_PREV(self
, rb_node_qh
, rb_link
));
855 if (next0
!= TAILQ_NEXT(self
, rb_link
))
856 next0
= rb_tree_iterate_const(rbt
, self
, RB_NODE_RIGHT
);
857 KASSERT(next0
== TAILQ_NEXT(self
, rb_link
));
861 * The root must be black.
862 * There can never be two adjacent red nodes.
865 KASSERT(!RB_ROOT_P(self
) || RB_BLACK_P(self
));
866 if (RB_RED_P(self
)) {
867 const struct rb_node
*brother
;
868 KASSERT(!RB_ROOT_P(self
));
869 brother
= self
->rb_parent
->rb_nodes
[self
->rb_position
^ RB_NODE_OTHER
];
870 KASSERT(RB_BLACK_P(self
->rb_parent
));
872 * I'm red and have no children, then I must either
873 * have no brother or my brother also be red and
874 * also have no children. (black count == 0)
876 KASSERT(!RB_CHILDLESS_P(self
)
877 || RB_SENTINEL_P(brother
)
879 || RB_CHILDLESS_P(brother
));
881 * If I'm not childless, I must have two children
882 * and they must be both be black.
884 KASSERT(RB_CHILDLESS_P(self
)
885 || (RB_TWOCHILDREN_P(self
)
886 && RB_BLACK_P(self
->rb_left
)
887 && RB_BLACK_P(self
->rb_right
)));
889 * If I'm not childless, thus I have black children,
890 * then my brother must either be black or have two
893 KASSERT(RB_CHILDLESS_P(self
)
894 || RB_BLACK_P(brother
)
895 || (RB_TWOCHILDREN_P(brother
)
896 && RB_BLACK_P(brother
->rb_left
)
897 && RB_BLACK_P(brother
->rb_right
)));
900 * If I'm black and have one child, that child must
901 * be red and childless.
903 KASSERT(RB_CHILDLESS_P(self
)
904 || RB_TWOCHILDREN_P(self
)
905 || (!RB_LEFT_SENTINEL_P(self
)
906 && RB_RIGHT_SENTINEL_P(self
)
907 && RB_RED_P(self
->rb_left
)
908 && RB_CHILDLESS_P(self
->rb_left
))
909 || (!RB_RIGHT_SENTINEL_P(self
)
910 && RB_LEFT_SENTINEL_P(self
)
911 && RB_RED_P(self
->rb_right
)
912 && RB_CHILDLESS_P(self
->rb_right
)));
915 * If I'm a childless black node and my parent is
916 * black, my 2nd closet relative away from my parent
917 * is either red or has a red parent or red children.
920 && RB_CHILDLESS_P(self
)
921 && RB_BLACK_P(self
->rb_parent
)) {
922 const unsigned int which
= self
->rb_position
;
923 const unsigned int other
= which
^ RB_NODE_OTHER
;
924 const struct rb_node
*relative0
, *relative
;
926 relative0
= rb_tree_iterate_const(rbt
,
928 KASSERT(relative0
!= NULL
);
929 relative
= rb_tree_iterate_const(rbt
,
931 KASSERT(relative
!= NULL
);
932 KASSERT(RB_SENTINEL_P(relative
->rb_nodes
[which
]));
934 KASSERT(RB_RED_P(relative
)
935 || RB_RED_P(relative
->rb_left
)
936 || RB_RED_P(relative
->rb_right
)
937 || RB_RED_P(relative
->rb_parent
));
942 * A grandparent's children must be real nodes and not
943 * sentinels. First check out grandparent.
945 KASSERT(RB_ROOT_P(self
)
946 || RB_ROOT_P(self
->rb_parent
)
947 || RB_TWOCHILDREN_P(self
->rb_parent
->rb_parent
));
949 * If we are have grandchildren on our left, then
950 * we must have a child on our right.
952 KASSERT(RB_LEFT_SENTINEL_P(self
)
953 || RB_CHILDLESS_P(self
->rb_left
)
954 || !RB_RIGHT_SENTINEL_P(self
));
956 * If we are have grandchildren on our right, then
957 * we must have a child on our left.
959 KASSERT(RB_RIGHT_SENTINEL_P(self
)
960 || RB_CHILDLESS_P(self
->rb_right
)
961 || !RB_LEFT_SENTINEL_P(self
));
964 * If we have a child on the left and it doesn't have two
965 * children make sure we don't have great-great-grandchildren on
968 KASSERT(RB_TWOCHILDREN_P(self
->rb_left
)
969 || RB_CHILDLESS_P(self
->rb_right
)
970 || RB_CHILDLESS_P(self
->rb_right
->rb_left
)
971 || RB_CHILDLESS_P(self
->rb_right
->rb_left
->rb_left
)
972 || RB_CHILDLESS_P(self
->rb_right
->rb_left
->rb_right
)
973 || RB_CHILDLESS_P(self
->rb_right
->rb_right
)
974 || RB_CHILDLESS_P(self
->rb_right
->rb_right
->rb_left
)
975 || RB_CHILDLESS_P(self
->rb_right
->rb_right
->rb_right
));
978 * If we have a child on the right and it doesn't have two
979 * children make sure we don't have great-great-grandchildren on
982 KASSERT(RB_TWOCHILDREN_P(self
->rb_right
)
983 || RB_CHILDLESS_P(self
->rb_left
)
984 || RB_CHILDLESS_P(self
->rb_left
->rb_left
)
985 || RB_CHILDLESS_P(self
->rb_left
->rb_left
->rb_left
)
986 || RB_CHILDLESS_P(self
->rb_left
->rb_left
->rb_right
)
987 || RB_CHILDLESS_P(self
->rb_left
->rb_right
)
988 || RB_CHILDLESS_P(self
->rb_left
->rb_right
->rb_left
)
989 || RB_CHILDLESS_P(self
->rb_left
->rb_right
->rb_right
));
992 * If we are fully interior node, then our predecessors and
993 * successors must have no children in our direction.
995 if (RB_TWOCHILDREN_P(self
)) {
996 const struct rb_node
*prev0
;
997 const struct rb_node
*next0
;
999 prev0
= rb_tree_iterate_const(rbt
, self
, RB_NODE_LEFT
);
1000 KASSERT(prev0
!= NULL
);
1001 KASSERT(RB_RIGHT_SENTINEL_P(prev0
));
1003 next0
= rb_tree_iterate_const(rbt
, self
, RB_NODE_RIGHT
);
1004 KASSERT(next0
!= NULL
);
1005 KASSERT(RB_LEFT_SENTINEL_P(next0
));
1013 rb_tree_count_black(const struct rb_node
*self
)
1015 unsigned int left
, right
;
1017 if (RB_SENTINEL_P(self
))
1020 left
= rb_tree_count_black(self
->rb_left
);
1021 right
= rb_tree_count_black(self
->rb_right
);
1023 KASSERT(left
== right
);
1025 return left
+ RB_BLACK_P(self
);
1029 _prop_rb_tree_check(const struct rb_tree
*rbt
, bool red_check
)
1031 const struct rb_node
*self
;
1032 const struct rb_node
*prev
;
1035 KASSERT(rbt
->rbt_root
== NULL
|| rbt
->rbt_root
->rb_position
== RB_NODE_LEFT
);
1039 TAILQ_FOREACH(self
, &rbt
->rbt_nodes
, rb_link
) {
1040 rb_tree_check_node(rbt
, self
, prev
, false);
1043 KASSERT(rbt
->rbt_count
== count
);
1044 KASSERT(RB_SENTINEL_P(rbt
->rbt_root
)
1045 || rb_tree_count_black(rbt
->rbt_root
));
1048 * The root must be black.
1049 * There can never be two adjacent red nodes.
1052 KASSERT(rbt
->rbt_root
== NULL
|| RB_BLACK_P(rbt
->rbt_root
));
1053 TAILQ_FOREACH(self
, &rbt
->rbt_nodes
, rb_link
) {
1054 rb_tree_check_node(rbt
, self
, NULL
, true);
1058 #endif /* RBDEBUG */