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 <libprop/proplib.h>
34 #include "prop_object_impl.h"
35 #include "prop_rb_impl.h"
39 #define KASSERT(x) _PROP_ASSERT(x)
41 #define KASSERT(x) /* nothing */
44 #ifndef __predict_false
45 #define __predict_false(x) (x)
48 static void rb_tree_insert_rebalance(struct rb_tree
*, struct rb_node
*);
49 static void rb_tree_removal_rebalance(struct rb_tree
*, struct rb_node
*,
52 static const struct rb_node
*rb_tree_iterate_const(const struct rb_tree
*,
53 const struct rb_node
*, const unsigned int);
54 static bool rb_tree_check_node(const struct rb_tree
*, const struct rb_node
*,
55 const struct rb_node
*, bool);
57 #define rb_tree_check_node(a, b, c, d) true
61 #define RBT_COUNT_INCR(rbt) (rbt)->rbt_count++
62 #define RBT_COUNT_DECR(rbt) (rbt)->rbt_count--
64 #define RBT_COUNT_INCR(rbt) /* nothing */
65 #define RBT_COUNT_DECR(rbt) /* nothing */
68 #define RBUNCONST(a) ((void *)(unsigned long)(const void *)(a))
70 #define RB_NODETOITEM(rbto, rbn) \
71 ((void *)((uintptr_t)(rbn) - (rbto)->rbto_node_offset))
72 #define RB_ITEMTONODE(rbto, rbn) \
73 ((rb_node_t *)((uintptr_t)(rbn) + (rbto)->rbto_node_offset))
75 #define RB_SENTINEL_NODE NULL
78 _prop_rb_tree_init(struct rb_tree
*rbt
, const rb_tree_ops_t
*ops
)
80 RB_TAILQ_INIT(&rbt
->rbt_nodes
);
85 rbt
->rbt_root
= RB_SENTINEL_NODE
;
90 _prop_rb_tree_find(struct rb_tree
*rbt
, const void *key
)
92 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
93 rbto_compare_key_fn compare_key
= rbto
->rbto_compare_key
;
94 struct rb_node
*parent
= rbt
->rbt_root
;
96 while (!RB_SENTINEL_P(parent
)) {
97 void *pobj
= RB_NODETOITEM(rbto
, parent
);
98 const signed int diff
= (*compare_key
)(rbto
->rbto_context
,
102 parent
= parent
->rb_nodes
[diff
< 0];
109 _prop_rb_tree_insert_node(struct rb_tree
*rbt
, void *object
)
111 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
112 rbto_compare_nodes_fn compare_nodes
= rbto
->rbto_compare_nodes
;
113 struct rb_node
*parent
, *tmp
, *self
= RB_ITEMTONODE(rbto
, object
);
114 unsigned int position
;
117 RBSTAT_INC(rbt
->rbt_insertions
);
121 * This is a hack. Because rbt->rbt_root is just a struct rb_node *,
122 * just like rb_node->rb_nodes[RB_DIR_LEFT], we can use this fact to
123 * avoid a lot of tests for root and know that even at root,
124 * updating RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will
125 * update rbt->rbt_root.
127 parent
= (struct rb_node
*)(void *)&rbt
->rbt_root
;
128 position
= RB_DIR_LEFT
;
131 * Find out where to place this new leaf.
133 while (!RB_SENTINEL_P(tmp
)) {
134 void *tobj
= RB_NODETOITEM(rbto
, tmp
);
135 const signed int diff
= (*compare_nodes
)(rbto
->rbto_context
,
137 if (__predict_false(diff
== 0)) {
139 * Node already exists; return it.
144 position
= (diff
< 0);
145 tmp
= parent
->rb_nodes
[position
];
150 struct rb_node
*prev
= NULL
, *next
= NULL
;
152 if (position
== RB_DIR_RIGHT
)
154 else if (tmp
!= rbt
->rbt_root
)
158 * Verify our sequential position
160 KASSERT(prev
== NULL
|| !RB_SENTINEL_P(prev
));
161 KASSERT(next
== NULL
|| !RB_SENTINEL_P(next
));
162 if (prev
!= NULL
&& next
== NULL
)
163 next
= TAILQ_NEXT(prev
, rb_link
);
164 if (prev
== NULL
&& next
!= NULL
)
165 prev
= TAILQ_PREV(next
, rb_node_qh
, rb_link
);
166 KASSERT(prev
== NULL
|| !RB_SENTINEL_P(prev
));
167 KASSERT(next
== NULL
|| !RB_SENTINEL_P(next
));
168 KASSERT(prev
== NULL
|| (*compare_nodes
)(rbto
->rbto_context
,
169 RB_NODETOITEM(rbto
, prev
), RB_NODETOITEM(rbto
, self
)) < 0);
170 KASSERT(next
== NULL
|| (*compare_nodes
)(rbto
->rbto_context
,
171 RB_NODETOITEM(rbto
, self
), RB_NODETOITEM(rbto
, next
)) < 0);
176 * Initialize the node and insert as a leaf into the tree.
178 RB_SET_FATHER(self
, parent
);
179 RB_SET_POSITION(self
, position
);
180 if (__predict_false(parent
== (struct rb_node
*)(void *)&rbt
->rbt_root
)) {
181 RB_MARK_BLACK(self
); /* root is always black */
183 rbt
->rbt_minmax
[RB_DIR_LEFT
] = self
;
184 rbt
->rbt_minmax
[RB_DIR_RIGHT
] = self
;
188 KASSERT(position
== RB_DIR_LEFT
|| position
== RB_DIR_RIGHT
);
191 * Keep track of the minimum and maximum nodes. If our
192 * parent is a minmax node and we on their min/max side,
193 * we must be the new min/max node.
195 if (parent
== rbt
->rbt_minmax
[position
])
196 rbt
->rbt_minmax
[position
] = self
;
197 #endif /* !RBSMALL */
199 * All new nodes are colored red. We only need to rebalance
200 * if our parent is also red.
203 rebalance
= RB_RED_P(parent
);
205 KASSERT(RB_SENTINEL_P(parent
->rb_nodes
[position
]));
206 self
->rb_left
= parent
->rb_nodes
[position
];
207 self
->rb_right
= parent
->rb_nodes
[position
];
208 parent
->rb_nodes
[position
] = self
;
209 KASSERT(RB_CHILDLESS_P(self
));
212 * Insert the new node into a sorted list for easy sequential access
214 RBSTAT_INC(rbt
->rbt_count
);
216 if (RB_ROOT_P(rbt
, self
)) {
217 RB_TAILQ_INSERT_HEAD(&rbt
->rbt_nodes
, self
, rb_link
);
218 } else if (position
== RB_DIR_LEFT
) {
219 KASSERT((*compare_nodes
)(rbto
->rbto_context
,
220 RB_NODETOITEM(rbto
, self
),
221 RB_NODETOITEM(rbto
, RB_FATHER(self
))) < 0);
222 RB_TAILQ_INSERT_BEFORE(RB_FATHER(self
), self
, rb_link
);
224 KASSERT((*compare_nodes
)(rbto
->rbto_context
,
225 RB_NODETOITEM(rbto
, RB_FATHER(self
)),
226 RB_NODETOITEM(rbto
, self
)) < 0);
227 RB_TAILQ_INSERT_AFTER(&rbt
->rbt_nodes
, RB_FATHER(self
),
231 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, !rebalance
));
234 * Rebalance tree after insertion
237 rb_tree_insert_rebalance(rbt
, self
);
238 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, true));
241 /* Succesfully inserted, return our node pointer. */
246 * Swap the location and colors of 'self' and its child @ which. The child
247 * can not be a sentinel node. This is our rotation function. However,
248 * since it preserves coloring, it great simplifies both insertion and
249 * removal since rotation almost always involves the exchanging of colors
250 * as a separate step.
254 rb_tree_reparent_nodes(struct rb_tree
*rbt
, struct rb_node
*old_father
,
255 const unsigned int which
)
257 const unsigned int other
= which
^ RB_DIR_OTHER
;
258 struct rb_node
* const grandpa
= RB_FATHER(old_father
);
259 struct rb_node
* const old_child
= old_father
->rb_nodes
[which
];
260 struct rb_node
* const new_father
= old_child
;
261 struct rb_node
* const new_child
= old_father
;
263 KASSERT(which
== RB_DIR_LEFT
|| which
== RB_DIR_RIGHT
);
265 KASSERT(!RB_SENTINEL_P(old_child
));
266 KASSERT(RB_FATHER(old_child
) == old_father
);
268 KASSERT(rb_tree_check_node(rbt
, old_father
, NULL
, false));
269 KASSERT(rb_tree_check_node(rbt
, old_child
, NULL
, false));
270 KASSERT(RB_ROOT_P(rbt
, old_father
) ||
271 rb_tree_check_node(rbt
, grandpa
, NULL
, false));
274 * Exchange descendant linkages.
276 grandpa
->rb_nodes
[RB_POSITION(old_father
)] = new_father
;
277 new_child
->rb_nodes
[which
] = old_child
->rb_nodes
[other
];
278 new_father
->rb_nodes
[other
] = new_child
;
281 * Update ancestor linkages
283 RB_SET_FATHER(new_father
, grandpa
);
284 RB_SET_FATHER(new_child
, new_father
);
287 * Exchange properties between new_father and new_child. The only
288 * change is that new_child's position is now on the other side.
294 RB_COPY_PROPERTIES(&tmp
, old_child
);
295 RB_COPY_PROPERTIES(new_father
, old_father
);
296 RB_COPY_PROPERTIES(new_child
, &tmp
);
299 RB_SWAP_PROPERTIES(new_father
, new_child
);
301 RB_SET_POSITION(new_child
, other
);
304 * Make sure to reparent the new child to ourself.
306 if (!RB_SENTINEL_P(new_child
->rb_nodes
[which
])) {
307 RB_SET_FATHER(new_child
->rb_nodes
[which
], new_child
);
308 RB_SET_POSITION(new_child
->rb_nodes
[which
], which
);
311 KASSERT(rb_tree_check_node(rbt
, new_father
, NULL
, false));
312 KASSERT(rb_tree_check_node(rbt
, new_child
, NULL
, false));
313 KASSERT(RB_ROOT_P(rbt
, new_father
) ||
314 rb_tree_check_node(rbt
, grandpa
, NULL
, false));
318 rb_tree_insert_rebalance(struct rb_tree
*rbt
, struct rb_node
*self
)
320 struct rb_node
* father
= RB_FATHER(self
);
321 struct rb_node
* grandpa
= RB_FATHER(father
);
322 struct rb_node
* uncle
;
326 KASSERT(!RB_ROOT_P(rbt
, self
));
327 KASSERT(RB_RED_P(self
));
328 KASSERT(RB_RED_P(father
));
329 RBSTAT_INC(rbt
->rbt_insertion_rebalance_calls
);
332 KASSERT(!RB_SENTINEL_P(self
));
334 KASSERT(RB_RED_P(self
));
335 KASSERT(RB_RED_P(father
));
337 * We are red and our parent is red, therefore we must have a
338 * grandfather and he must be black.
340 grandpa
= RB_FATHER(father
);
341 KASSERT(RB_BLACK_P(grandpa
));
342 KASSERT(RB_DIR_RIGHT
== 1 && RB_DIR_LEFT
== 0);
343 which
= (father
== grandpa
->rb_right
);
344 other
= which
^ RB_DIR_OTHER
;
345 uncle
= grandpa
->rb_nodes
[other
];
347 if (RB_BLACK_P(uncle
))
350 RBSTAT_INC(rbt
->rbt_insertion_rebalance_passes
);
352 * Case 1: our uncle is red
353 * Simply invert the colors of our parent and
354 * uncle and make our grandparent red. And
355 * then solve the problem up at his level.
357 RB_MARK_BLACK(uncle
);
358 RB_MARK_BLACK(father
);
359 if (__predict_false(RB_ROOT_P(rbt
, grandpa
))) {
361 * If our grandpa is root, don't bother
362 * setting him to red, just return.
364 KASSERT(RB_BLACK_P(grandpa
));
367 RB_MARK_RED(grandpa
);
369 father
= RB_FATHER(self
);
370 KASSERT(RB_RED_P(self
));
371 if (RB_BLACK_P(father
)) {
373 * If our greatgrandpa is black, we're done.
375 KASSERT(RB_BLACK_P(rbt
->rbt_root
));
380 KASSERT(!RB_ROOT_P(rbt
, self
));
381 KASSERT(RB_RED_P(self
));
382 KASSERT(RB_RED_P(father
));
383 KASSERT(RB_BLACK_P(uncle
));
384 KASSERT(RB_BLACK_P(grandpa
));
386 * Case 2&3: our uncle is black.
388 if (self
== father
->rb_nodes
[other
]) {
390 * Case 2: we are on the same side as our uncle
391 * Swap ourselves with our parent so this case
392 * becomes case 3. Basically our parent becomes our
395 rb_tree_reparent_nodes(rbt
, father
, other
);
396 KASSERT(RB_FATHER(father
) == self
);
397 KASSERT(self
->rb_nodes
[which
] == father
);
398 KASSERT(RB_FATHER(self
) == grandpa
);
400 father
= RB_FATHER(self
);
402 KASSERT(RB_RED_P(self
) && RB_RED_P(father
));
403 KASSERT(grandpa
->rb_nodes
[which
] == father
);
405 * Case 3: we are opposite a child of a black uncle.
406 * Swap our parent and grandparent. Since our grandfather
407 * is black, our father will become black and our new sibling
408 * (former grandparent) will become red.
410 rb_tree_reparent_nodes(rbt
, grandpa
, which
);
411 KASSERT(RB_FATHER(self
) == father
);
412 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_POSITION(self
) ^ RB_DIR_OTHER
] == grandpa
);
413 KASSERT(RB_RED_P(self
));
414 KASSERT(RB_BLACK_P(father
));
415 KASSERT(RB_RED_P(grandpa
));
418 * Final step: Set the root to black.
420 RB_MARK_BLACK(rbt
->rbt_root
);
424 rb_tree_prune_node(struct rb_tree
*rbt
, struct rb_node
*self
, bool rebalance
)
426 const unsigned int which
= RB_POSITION(self
);
427 struct rb_node
*father
= RB_FATHER(self
);
429 const bool was_root
= RB_ROOT_P(rbt
, self
);
432 KASSERT(rebalance
|| (RB_ROOT_P(rbt
, self
) || RB_RED_P(self
)));
433 KASSERT(!rebalance
|| RB_BLACK_P(self
));
434 KASSERT(RB_CHILDLESS_P(self
));
435 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
438 * Since we are childless, we know that self->rb_left is pointing
439 * to the sentinel node.
441 father
->rb_nodes
[which
] = self
->rb_left
;
444 * Remove ourselves from the node list, decrement the count,
445 * and update min/max.
447 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
448 RBSTAT_DEC(rbt
->rbt_count
);
450 if (__predict_false(rbt
->rbt_minmax
[RB_POSITION(self
)] == self
)) {
451 rbt
->rbt_minmax
[RB_POSITION(self
)] = father
;
453 * When removing the root, rbt->rbt_minmax[RB_DIR_LEFT] is
454 * updated automatically, but we also need to update
455 * rbt->rbt_minmax[RB_DIR_RIGHT];
457 if (__predict_false(was_root
)) {
458 rbt
->rbt_minmax
[RB_DIR_RIGHT
] = father
;
461 RB_SET_FATHER(self
, NULL
);
465 * Rebalance if requested.
468 rb_tree_removal_rebalance(rbt
, father
, which
);
469 KASSERT(was_root
|| rb_tree_check_node(rbt
, father
, NULL
, true));
473 * When deleting an interior node
476 rb_tree_swap_prune_and_rebalance(struct rb_tree
*rbt
, struct rb_node
*self
,
477 struct rb_node
*standin
)
479 const unsigned int standin_which
= RB_POSITION(standin
);
480 unsigned int standin_other
= standin_which
^ RB_DIR_OTHER
;
481 struct rb_node
*standin_son
;
482 struct rb_node
*standin_father
= RB_FATHER(standin
);
483 bool rebalance
= RB_BLACK_P(standin
);
485 if (standin_father
== self
) {
487 * As a child of self, any childen would be opposite of
490 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_other
]));
491 standin_son
= standin
->rb_nodes
[standin_which
];
494 * Since we aren't a child of self, any childen would be
495 * on the same side as our parent.
497 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_which
]));
498 standin_son
= standin
->rb_nodes
[standin_other
];
502 * the node we are removing must have two children.
504 KASSERT(RB_TWOCHILDREN_P(self
));
506 * If standin has a child, it must be red.
508 KASSERT(RB_SENTINEL_P(standin_son
) || RB_RED_P(standin_son
));
511 * Verify things are sane.
513 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
514 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, false));
516 if (__predict_false(RB_RED_P(standin_son
))) {
518 * We know we have a red child so if we flip it to black
519 * we don't have to rebalance.
521 KASSERT(rb_tree_check_node(rbt
, standin_son
, NULL
, true));
522 RB_MARK_BLACK(standin_son
);
525 if (standin_father
== self
) {
526 KASSERT(RB_POSITION(standin_son
) == standin_which
);
528 KASSERT(RB_POSITION(standin_son
) == standin_other
);
530 * Change the son's parentage to point to his grandpa.
532 RB_SET_FATHER(standin_son
, standin_father
);
533 RB_SET_POSITION(standin_son
, standin_which
);
537 if (standin_father
== self
) {
539 * If we are about to delete the standin's father, then when
540 * we call rebalance, we need to use ourselves as our father.
541 * Otherwise remember our original father. Also, sincef we are
542 * our standin's father we only need to reparent the standin's
549 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_other
]));
550 KASSERT(!RB_SENTINEL_P(self
->rb_nodes
[standin_other
]));
551 KASSERT(self
->rb_nodes
[standin_which
] == standin
);
553 * Have our son/standin adopt his brother as his new son.
555 standin_father
= standin
;
559 * | / \ | T --> / \ | / |
560 * | ..... | S --> ..... | T |
562 * Sever standin's connection to his father.
564 standin_father
->rb_nodes
[standin_which
] = standin_son
;
568 standin
->rb_nodes
[standin_other
] = self
->rb_nodes
[standin_other
];
569 RB_SET_FATHER(standin
->rb_nodes
[standin_other
], standin
);
570 KASSERT(RB_POSITION(self
->rb_nodes
[standin_other
]) == standin_other
);
572 * Use standin_other because we need to preserve standin_which
573 * for the removal_rebalance.
575 standin_other
= standin_which
;
579 * Move the only remaining son to our standin. If our standin is our
580 * son, this will be the only son needed to be moved.
582 KASSERT(standin
->rb_nodes
[standin_other
] != self
->rb_nodes
[standin_other
]);
583 standin
->rb_nodes
[standin_other
] = self
->rb_nodes
[standin_other
];
584 RB_SET_FATHER(standin
->rb_nodes
[standin_other
], standin
);
587 * Now copy the result of self to standin and then replace
588 * self with standin in the tree.
590 RB_COPY_PROPERTIES(standin
, self
);
591 RB_SET_FATHER(standin
, RB_FATHER(self
));
592 RB_FATHER(standin
)->rb_nodes
[RB_POSITION(standin
)] = standin
;
595 * Remove ourselves from the node list, decrement the count,
596 * and update min/max.
598 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
599 RBSTAT_DEC(rbt
->rbt_count
);
601 if (__predict_false(rbt
->rbt_minmax
[RB_POSITION(self
)] == self
))
602 rbt
->rbt_minmax
[RB_POSITION(self
)] = RB_FATHER(self
);
603 RB_SET_FATHER(self
, NULL
);
606 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, false));
607 KASSERT(RB_FATHER_SENTINEL_P(standin
)
608 || rb_tree_check_node(rbt
, standin_father
, NULL
, false));
609 KASSERT(RB_LEFT_SENTINEL_P(standin
)
610 || rb_tree_check_node(rbt
, standin
->rb_left
, NULL
, false));
611 KASSERT(RB_RIGHT_SENTINEL_P(standin
)
612 || rb_tree_check_node(rbt
, standin
->rb_right
, NULL
, false));
617 rb_tree_removal_rebalance(rbt
, standin_father
, standin_which
);
618 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, true));
622 * We could do this by doing
623 * rb_tree_node_swap(rbt, self, which);
624 * rb_tree_prune_node(rbt, self, false);
626 * But it's more efficient to just evalate and recolor the child.
629 rb_tree_prune_blackred_branch(struct rb_tree
*rbt
, struct rb_node
*self
,
632 struct rb_node
*father
= RB_FATHER(self
);
633 struct rb_node
*son
= self
->rb_nodes
[which
];
635 const bool was_root
= RB_ROOT_P(rbt
, self
);
638 KASSERT(which
== RB_DIR_LEFT
|| which
== RB_DIR_RIGHT
);
639 KASSERT(RB_BLACK_P(self
) && RB_RED_P(son
));
640 KASSERT(!RB_TWOCHILDREN_P(son
));
641 KASSERT(RB_CHILDLESS_P(son
));
642 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
643 KASSERT(rb_tree_check_node(rbt
, son
, NULL
, false));
646 * Remove ourselves from the tree and give our former child our
647 * properties (position, color, root).
649 RB_COPY_PROPERTIES(son
, self
);
650 father
->rb_nodes
[RB_POSITION(son
)] = son
;
651 RB_SET_FATHER(son
, father
);
654 * Remove ourselves from the node list, decrement the count,
657 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
658 RBSTAT_DEC(rbt
->rbt_count
);
660 if (__predict_false(was_root
)) {
661 KASSERT(rbt
->rbt_minmax
[which
] == son
);
662 rbt
->rbt_minmax
[which
^ RB_DIR_OTHER
] = son
;
663 } else if (rbt
->rbt_minmax
[RB_POSITION(self
)] == self
) {
664 rbt
->rbt_minmax
[RB_POSITION(self
)] = son
;
666 RB_SET_FATHER(self
, NULL
);
669 KASSERT(was_root
|| rb_tree_check_node(rbt
, father
, NULL
, true));
670 KASSERT(rb_tree_check_node(rbt
, son
, NULL
, true));
674 _prop_rb_tree_remove_node(struct rb_tree
*rbt
, void *object
)
676 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
677 struct rb_node
*standin
, *self
= RB_ITEMTONODE(rbto
, object
);
680 KASSERT(!RB_SENTINEL_P(self
));
681 RBSTAT_INC(rbt
->rbt_removals
);
684 * In the following diagrams, we (the node to be removed) are S. Red
685 * nodes are lowercase. T could be either red or black.
687 * Remember the major axiom of the red-black tree: the number of
688 * black nodes from the root to each leaf is constant across all
689 * leaves, only the number of red nodes varies.
691 * Thus removing a red leaf doesn't require any other changes to a
692 * red-black tree. So if we must remove a node, attempt to rearrange
693 * the tree so we can remove a red node.
695 * The simpliest case is a childless red node or a childless root node:
697 * | T --> T | or | R --> * |
700 if (RB_CHILDLESS_P(self
)) {
701 const bool rebalance
= RB_BLACK_P(self
) && !RB_ROOT_P(rbt
, self
);
702 rb_tree_prune_node(rbt
, self
, rebalance
);
705 KASSERT(!RB_CHILDLESS_P(self
));
706 if (!RB_TWOCHILDREN_P(self
)) {
708 * The next simpliest case is the node we are deleting is
709 * black and has one red child.
715 which
= RB_LEFT_SENTINEL_P(self
) ? RB_DIR_RIGHT
: RB_DIR_LEFT
;
716 KASSERT(RB_BLACK_P(self
));
717 KASSERT(RB_RED_P(self
->rb_nodes
[which
]));
718 KASSERT(RB_CHILDLESS_P(self
->rb_nodes
[which
]));
719 rb_tree_prune_blackred_branch(rbt
, self
, which
);
722 KASSERT(RB_TWOCHILDREN_P(self
));
725 * We invert these because we prefer to remove from the inside of
728 which
= RB_POSITION(self
) ^ RB_DIR_OTHER
;
731 * Let's find the node closes to us opposite of our parent
732 * Now swap it with ourself, "prune" it, and rebalance, if needed.
734 standin
= RB_ITEMTONODE(rbto
, _prop_rb_tree_iterate(rbt
, object
, which
));
735 rb_tree_swap_prune_and_rebalance(rbt
, self
, standin
);
739 rb_tree_removal_rebalance(struct rb_tree
*rbt
, struct rb_node
*parent
,
742 KASSERT(!RB_SENTINEL_P(parent
));
743 KASSERT(RB_SENTINEL_P(parent
->rb_nodes
[which
]));
744 KASSERT(which
== RB_DIR_LEFT
|| which
== RB_DIR_RIGHT
);
745 RBSTAT_INC(rbt
->rbt_removal_rebalance_calls
);
747 while (RB_BLACK_P(parent
->rb_nodes
[which
])) {
748 unsigned int other
= which
^ RB_DIR_OTHER
;
749 struct rb_node
*brother
= parent
->rb_nodes
[other
];
751 RBSTAT_INC(rbt
->rbt_removal_rebalance_passes
);
753 KASSERT(!RB_SENTINEL_P(brother
));
755 * For cases 1, 2a, and 2b, our brother's children must
756 * be black and our father must be black
758 if (RB_BLACK_P(parent
)
759 && RB_BLACK_P(brother
->rb_left
)
760 && RB_BLACK_P(brother
->rb_right
)) {
761 if (RB_RED_P(brother
)) {
763 * Case 1: Our brother is red, swap its
764 * position (and colors) with our parent.
765 * This should now be case 2b (unless C or E
766 * has a red child which is case 3; thus no
767 * explicit branch to case 2b).
773 KASSERT(RB_BLACK_P(parent
));
774 rb_tree_reparent_nodes(rbt
, parent
, other
);
775 brother
= parent
->rb_nodes
[other
];
776 KASSERT(!RB_SENTINEL_P(brother
));
777 KASSERT(RB_RED_P(parent
));
778 KASSERT(RB_BLACK_P(brother
));
779 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, false));
780 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, false));
783 * Both our parent and brother are black.
784 * Change our brother to red, advance up rank
785 * and go through the loop again.
791 RB_MARK_RED(brother
);
792 KASSERT(RB_BLACK_P(brother
->rb_left
));
793 KASSERT(RB_BLACK_P(brother
->rb_right
));
794 if (RB_ROOT_P(rbt
, parent
))
795 return; /* root == parent == black */
796 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, false));
797 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, false));
798 which
= RB_POSITION(parent
);
799 parent
= RB_FATHER(parent
);
804 * Avoid an else here so that case 2a above can hit either
808 && RB_BLACK_P(brother
)
809 && RB_BLACK_P(brother
->rb_left
)
810 && RB_BLACK_P(brother
->rb_right
)) {
811 KASSERT(RB_RED_P(parent
));
812 KASSERT(RB_BLACK_P(brother
));
813 KASSERT(RB_BLACK_P(brother
->rb_left
));
814 KASSERT(RB_BLACK_P(brother
->rb_right
));
816 * We are black, our father is red, our brother and
817 * both nephews are black. Simply invert/exchange the
818 * colors of our father and brother (to black and red
825 RB_MARK_BLACK(parent
);
826 RB_MARK_RED(brother
);
827 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, true));
828 break; /* We're done! */
831 * Our brother must be black and have at least one
832 * red child (it may have two).
834 KASSERT(RB_BLACK_P(brother
));
835 KASSERT(RB_RED_P(brother
->rb_nodes
[which
]) ||
836 RB_RED_P(brother
->rb_nodes
[other
]));
837 if (RB_BLACK_P(brother
->rb_nodes
[other
])) {
839 * Case 3: our brother is black, our near
840 * nephew is red, and our far nephew is black.
841 * Swap our brother with our near nephew.
842 * This result in a tree that matches case 4.
843 * (Our father could be red or black).
849 KASSERT(RB_RED_P(brother
->rb_nodes
[which
]));
850 rb_tree_reparent_nodes(rbt
, brother
, which
);
851 KASSERT(RB_FATHER(brother
) == parent
->rb_nodes
[other
]);
852 brother
= parent
->rb_nodes
[other
];
853 KASSERT(RB_RED_P(brother
->rb_nodes
[other
]));
856 * Case 4: our brother is black and our far nephew
857 * is red. Swap our father and brother locations and
858 * change our far nephew to black. (these can be
859 * done in either order so we change the color first).
860 * The result is a valid red-black tree and is a
861 * terminal case. (again we don't care about the
864 * If the father is red, we will get a red-black-black
870 * If the father is black, we will get an all black
876 * If we had two red nephews, then after the swap,
877 * our former father would have a red grandson.
879 KASSERT(RB_BLACK_P(brother
));
880 KASSERT(RB_RED_P(brother
->rb_nodes
[other
]));
881 RB_MARK_BLACK(brother
->rb_nodes
[other
]);
882 rb_tree_reparent_nodes(rbt
, parent
, other
);
883 break; /* We're done! */
886 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, true));
890 _prop_rb_tree_iterate(struct rb_tree
*rbt
, void *object
, const unsigned int direction
)
892 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
893 const unsigned int other
= direction
^ RB_DIR_OTHER
;
894 struct rb_node
*self
;
896 KASSERT(direction
== RB_DIR_LEFT
|| direction
== RB_DIR_RIGHT
);
898 if (object
== NULL
) {
900 if (RB_SENTINEL_P(rbt
->rbt_root
))
902 return RB_NODETOITEM(rbto
, rbt
->rbt_minmax
[direction
]);
904 self
= rbt
->rbt_root
;
905 if (RB_SENTINEL_P(self
))
907 while (!RB_SENTINEL_P(self
->rb_nodes
[direction
]))
908 self
= self
->rb_nodes
[direction
];
909 return RB_NODETOITEM(rbto
, self
);
910 #endif /* !RBSMALL */
912 self
= RB_ITEMTONODE(rbto
, object
);
913 KASSERT(!RB_SENTINEL_P(self
));
915 * We can't go any further in this direction. We proceed up in the
916 * opposite direction until our parent is in direction we want to go.
918 if (RB_SENTINEL_P(self
->rb_nodes
[direction
])) {
919 while (!RB_ROOT_P(rbt
, self
)) {
920 if (other
== RB_POSITION(self
))
921 return RB_NODETOITEM(rbto
, RB_FATHER(self
));
922 self
= RB_FATHER(self
);
928 * Advance down one in current direction and go down as far as possible
929 * in the opposite direction.
931 self
= self
->rb_nodes
[direction
];
932 KASSERT(!RB_SENTINEL_P(self
));
933 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
934 self
= self
->rb_nodes
[other
];
935 return RB_NODETOITEM(rbto
, self
);
939 static const struct rb_node
*
940 rb_tree_iterate_const(const struct rb_tree
*rbt
, const struct rb_node
*self
,
941 const unsigned int direction
)
943 const unsigned int other
= direction
^ RB_DIR_OTHER
;
944 KASSERT(direction
== RB_DIR_LEFT
|| direction
== RB_DIR_RIGHT
);
948 if (RB_SENTINEL_P(rbt
->rbt_root
))
950 return rbt
->rbt_minmax
[direction
];
952 self
= rbt
->rbt_root
;
953 if (RB_SENTINEL_P(self
))
955 while (!RB_SENTINEL_P(self
->rb_nodes
[direction
]))
956 self
= self
->rb_nodes
[direction
];
958 #endif /* !RBSMALL */
960 KASSERT(!RB_SENTINEL_P(self
));
962 * We can't go any further in this direction. We proceed up in the
963 * opposite direction until our parent is in direction we want to go.
965 if (RB_SENTINEL_P(self
->rb_nodes
[direction
])) {
966 while (!RB_ROOT_P(rbt
, self
)) {
967 if (other
== RB_POSITION(self
))
968 return RB_FATHER(self
);
969 self
= RB_FATHER(self
);
975 * Advance down one in current direction and go down as far as possible
976 * in the opposite direction.
978 self
= self
->rb_nodes
[direction
];
979 KASSERT(!RB_SENTINEL_P(self
));
980 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
981 self
= self
->rb_nodes
[other
];
986 rb_tree_count_black(const struct rb_node
*self
)
988 unsigned int left
, right
;
990 if (RB_SENTINEL_P(self
))
993 left
= rb_tree_count_black(self
->rb_left
);
994 right
= rb_tree_count_black(self
->rb_right
);
996 KASSERT(left
== right
);
998 return left
+ RB_BLACK_P(self
);
1002 rb_tree_check_node(const struct rb_tree
*rbt
, const struct rb_node
*self
,
1003 const struct rb_node
*prev
, bool red_check
)
1005 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
1006 rbto_compare_nodes_fn compare_nodes
= rbto
->rbto_compare_nodes
;
1008 KASSERT(!RB_SENTINEL_P(self
));
1009 KASSERT(prev
== NULL
|| (*compare_nodes
)(rbto
->rbto_context
,
1010 RB_NODETOITEM(rbto
, prev
), RB_NODETOITEM(rbto
, self
)) < 0);
1013 * Verify our relationship to our parent.
1015 if (RB_ROOT_P(rbt
, self
)) {
1016 KASSERT(self
== rbt
->rbt_root
);
1017 KASSERT(RB_POSITION(self
) == RB_DIR_LEFT
);
1018 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_DIR_LEFT
] == self
);
1019 KASSERT(RB_FATHER(self
) == (const struct rb_node
*) &rbt
->rbt_root
);
1021 int diff
= (*compare_nodes
)(rbto
->rbto_context
,
1022 RB_NODETOITEM(rbto
, self
),
1023 RB_NODETOITEM(rbto
, RB_FATHER(self
)));
1025 KASSERT(self
!= rbt
->rbt_root
);
1026 KASSERT(!RB_FATHER_SENTINEL_P(self
));
1027 if (RB_POSITION(self
) == RB_DIR_LEFT
) {
1029 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_DIR_LEFT
] == self
);
1032 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_DIR_RIGHT
] == self
);
1037 * Verify our position in the linked list against the tree itself.
1040 const struct rb_node
*prev0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_LEFT
);
1041 const struct rb_node
*next0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_RIGHT
);
1042 KASSERT(prev0
== TAILQ_PREV(self
, rb_node_qh
, rb_link
));
1043 KASSERT(next0
== TAILQ_NEXT(self
, rb_link
));
1045 KASSERT(prev0
!= NULL
|| self
== rbt
->rbt_minmax
[RB_DIR_LEFT
]);
1046 KASSERT(next0
!= NULL
|| self
== rbt
->rbt_minmax
[RB_DIR_RIGHT
]);
1051 * The root must be black.
1052 * There can never be two adjacent red nodes.
1055 KASSERT(!RB_ROOT_P(rbt
, self
) || RB_BLACK_P(self
));
1056 (void) rb_tree_count_black(self
);
1057 if (RB_RED_P(self
)) {
1058 const struct rb_node
*brother
;
1059 KASSERT(!RB_ROOT_P(rbt
, self
));
1060 brother
= RB_FATHER(self
)->rb_nodes
[RB_POSITION(self
) ^ RB_DIR_OTHER
];
1061 KASSERT(RB_BLACK_P(RB_FATHER(self
)));
1063 * I'm red and have no children, then I must either
1064 * have no brother or my brother also be red and
1065 * also have no children. (black count == 0)
1067 KASSERT(!RB_CHILDLESS_P(self
)
1068 || RB_SENTINEL_P(brother
)
1069 || RB_RED_P(brother
)
1070 || RB_CHILDLESS_P(brother
));
1072 * If I'm not childless, I must have two children
1073 * and they must be both be black.
1075 KASSERT(RB_CHILDLESS_P(self
)
1076 || (RB_TWOCHILDREN_P(self
)
1077 && RB_BLACK_P(self
->rb_left
)
1078 && RB_BLACK_P(self
->rb_right
)));
1080 * If I'm not childless, thus I have black children,
1081 * then my brother must either be black or have two
1084 KASSERT(RB_CHILDLESS_P(self
)
1085 || RB_BLACK_P(brother
)
1086 || (RB_TWOCHILDREN_P(brother
)
1087 && RB_BLACK_P(brother
->rb_left
)
1088 && RB_BLACK_P(brother
->rb_right
)));
1091 * If I'm black and have one child, that child must
1092 * be red and childless.
1094 KASSERT(RB_CHILDLESS_P(self
)
1095 || RB_TWOCHILDREN_P(self
)
1096 || (!RB_LEFT_SENTINEL_P(self
)
1097 && RB_RIGHT_SENTINEL_P(self
)
1098 && RB_RED_P(self
->rb_left
)
1099 && RB_CHILDLESS_P(self
->rb_left
))
1100 || (!RB_RIGHT_SENTINEL_P(self
)
1101 && RB_LEFT_SENTINEL_P(self
)
1102 && RB_RED_P(self
->rb_right
)
1103 && RB_CHILDLESS_P(self
->rb_right
)));
1106 * If I'm a childless black node and my parent is
1107 * black, my 2nd closet relative away from my parent
1108 * is either red or has a red parent or red children.
1110 if (!RB_ROOT_P(rbt
, self
)
1111 && RB_CHILDLESS_P(self
)
1112 && RB_BLACK_P(RB_FATHER(self
))) {
1113 const unsigned int which
= RB_POSITION(self
);
1114 const unsigned int other
= which
^ RB_DIR_OTHER
;
1115 const struct rb_node
*relative0
, *relative
;
1117 relative0
= rb_tree_iterate_const(rbt
,
1119 KASSERT(relative0
!= NULL
);
1120 relative
= rb_tree_iterate_const(rbt
,
1122 KASSERT(relative
!= NULL
);
1123 KASSERT(RB_SENTINEL_P(relative
->rb_nodes
[which
]));
1125 KASSERT(RB_RED_P(relative
)
1126 || RB_RED_P(relative
->rb_left
)
1127 || RB_RED_P(relative
->rb_right
)
1128 || RB_RED_P(RB_FATHER(relative
)));
1133 * A grandparent's children must be real nodes and not
1134 * sentinels. First check out grandparent.
1136 KASSERT(RB_ROOT_P(rbt
, self
)
1137 || RB_ROOT_P(rbt
, RB_FATHER(self
))
1138 || RB_TWOCHILDREN_P(RB_FATHER(RB_FATHER(self
))));
1140 * If we are have grandchildren on our left, then
1141 * we must have a child on our right.
1143 KASSERT(RB_LEFT_SENTINEL_P(self
)
1144 || RB_CHILDLESS_P(self
->rb_left
)
1145 || !RB_RIGHT_SENTINEL_P(self
));
1147 * If we are have grandchildren on our right, then
1148 * we must have a child on our left.
1150 KASSERT(RB_RIGHT_SENTINEL_P(self
)
1151 || RB_CHILDLESS_P(self
->rb_right
)
1152 || !RB_LEFT_SENTINEL_P(self
));
1155 * If we have a child on the left and it doesn't have two
1156 * children make sure we don't have great-great-grandchildren on
1159 KASSERT(RB_TWOCHILDREN_P(self
->rb_left
)
1160 || RB_CHILDLESS_P(self
->rb_right
)
1161 || RB_CHILDLESS_P(self
->rb_right
->rb_left
)
1162 || RB_CHILDLESS_P(self
->rb_right
->rb_left
->rb_left
)
1163 || RB_CHILDLESS_P(self
->rb_right
->rb_left
->rb_right
)
1164 || RB_CHILDLESS_P(self
->rb_right
->rb_right
)
1165 || RB_CHILDLESS_P(self
->rb_right
->rb_right
->rb_left
)
1166 || RB_CHILDLESS_P(self
->rb_right
->rb_right
->rb_right
));
1169 * If we have a child on the right and it doesn't have two
1170 * children make sure we don't have great-great-grandchildren on
1173 KASSERT(RB_TWOCHILDREN_P(self
->rb_right
)
1174 || RB_CHILDLESS_P(self
->rb_left
)
1175 || RB_CHILDLESS_P(self
->rb_left
->rb_left
)
1176 || RB_CHILDLESS_P(self
->rb_left
->rb_left
->rb_left
)
1177 || RB_CHILDLESS_P(self
->rb_left
->rb_left
->rb_right
)
1178 || RB_CHILDLESS_P(self
->rb_left
->rb_right
)
1179 || RB_CHILDLESS_P(self
->rb_left
->rb_right
->rb_left
)
1180 || RB_CHILDLESS_P(self
->rb_left
->rb_right
->rb_right
));
1183 * If we are fully interior node, then our predecessors and
1184 * successors must have no children in our direction.
1186 if (RB_TWOCHILDREN_P(self
)) {
1187 const struct rb_node
*prev0
;
1188 const struct rb_node
*next0
;
1190 prev0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_LEFT
);
1191 KASSERT(prev0
!= NULL
);
1192 KASSERT(RB_RIGHT_SENTINEL_P(prev0
));
1194 next0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_RIGHT
);
1195 KASSERT(next0
!= NULL
);
1196 KASSERT(RB_LEFT_SENTINEL_P(next0
));
1204 rb_tree_check(const struct rb_tree
*rbt
, bool red_check
)
1206 const struct rb_node
*self
;
1207 const struct rb_node
*prev
;
1209 unsigned int count
= 0;
1212 KASSERT(rbt
->rbt_root
!= NULL
);
1213 KASSERT(RB_LEFT_P(rbt
->rbt_root
));
1215 #if defined(RBSTATS) && !defined(RBSMALL)
1216 KASSERT(rbt
->rbt_count
> 1
1217 || rbt
->rbt_minmax
[RB_DIR_LEFT
] == rbt
->rbt_minmax
[RB_DIR_RIGHT
]);
1221 TAILQ_FOREACH(self
, &rbt
->rbt_nodes
, rb_link
) {
1222 rb_tree_check_node(rbt
, self
, prev
, false);
1228 KASSERT(rbt
->rbt_count
== count
);
1231 KASSERT(RB_BLACK_P(rbt
->rbt_root
));
1232 KASSERT(RB_SENTINEL_P(rbt
->rbt_root
)
1233 || rb_tree_count_black(rbt
->rbt_root
));
1236 * The root must be black.
1237 * There can never be two adjacent red nodes.
1239 TAILQ_FOREACH(self
, &rbt
->rbt_nodes
, rb_link
) {
1240 rb_tree_check_node(rbt
, self
, NULL
, true);
1244 #endif /* RBDEBUG */
1248 rb_tree_mark_depth(const struct rb_tree
*rbt
, const struct rb_node
*self
,
1249 size_t *depths
, size_t depth
)
1251 if (RB_SENTINEL_P(self
))
1254 if (RB_TWOCHILDREN_P(self
)) {
1255 rb_tree_mark_depth(rbt
, self
->rb_left
, depths
, depth
+ 1);
1256 rb_tree_mark_depth(rbt
, self
->rb_right
, depths
, depth
+ 1);
1260 if (!RB_LEFT_SENTINEL_P(self
)) {
1261 rb_tree_mark_depth(rbt
, self
->rb_left
, depths
, depth
+ 1);
1263 if (!RB_RIGHT_SENTINEL_P(self
)) {
1264 rb_tree_mark_depth(rbt
, self
->rb_right
, depths
, depth
+ 1);
1269 rb_tree_depths(const struct rb_tree
*rbt
, size_t *depths
)
1271 rb_tree_mark_depth(rbt
, rbt
->rbt_root
, depths
, 1);
1273 #endif /* RBSTATS */