1 /* $NetBSD: rb.c,v 1.13 2014/08/22 17:19:48 matt Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * Portions Copyright (c) 2012 Apple Inc. All rights reserved.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Matt Thomas <matt@3am-software.com>.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/types.h>
45 #define _RBTREE_NO_OPAQUE_STRUCTS_
50 #include <sys/rbtree.h>
53 #ifndef __predict_false
55 #define __predict_false(x) ((typeof(x))__builtin_expect((long)(x), 0l))
57 #define __predict_false(x) (x)
61 #define RB_DIR_OTHER RB_DIR_RIGHT
63 #define rb_left rb_nodes[RB_DIR_LEFT]
64 #define rb_right rb_nodes[RB_DIR_RIGHT]
66 #define RB_FLAG_POSITION 0x2
67 #define RB_FLAG_RED 0x1
68 #define RB_FLAG_MASK (RB_FLAG_POSITION|RB_FLAG_RED)
69 #define RB_FATHER(rb) \
70 ((struct rb_node *)((rb)->rb_info & ~RB_FLAG_MASK))
71 #define RB_SET_FATHER(rb, father) \
72 ((void)((rb)->rb_info = (uintptr_t)(father)|((rb)->rb_info & RB_FLAG_MASK)))
74 #define RB_SENTINEL_P(rb) ((rb) == NULL)
75 #define RB_LEFT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_left)
76 #define RB_RIGHT_SENTINEL_P(rb) RB_SENTINEL_P((rb)->rb_right)
77 #define RB_FATHER_SENTINEL_P(rb) RB_SENTINEL_P(RB_FATHER((rb)))
78 #define RB_CHILDLESS_P(rb) \
79 (RB_SENTINEL_P(rb) || (RB_LEFT_SENTINEL_P(rb) && RB_RIGHT_SENTINEL_P(rb)))
80 #define RB_TWOCHILDREN_P(rb) \
81 (!RB_SENTINEL_P(rb) && !RB_LEFT_SENTINEL_P(rb) && !RB_RIGHT_SENTINEL_P(rb))
83 #define RB_POSITION(rb) \
84 (((rb)->rb_info & RB_FLAG_POSITION) ? RB_DIR_RIGHT : RB_DIR_LEFT)
85 #define RB_RIGHT_P(rb) (RB_POSITION(rb) == RB_DIR_RIGHT)
86 #define RB_LEFT_P(rb) (RB_POSITION(rb) == RB_DIR_LEFT)
87 #define RB_RED_P(rb) (!RB_SENTINEL_P(rb) && ((rb)->rb_info & RB_FLAG_RED) != 0)
88 #define RB_BLACK_P(rb) (RB_SENTINEL_P(rb) || ((rb)->rb_info & RB_FLAG_RED) == 0)
89 #define RB_MARK_RED(rb) ((void)((rb)->rb_info |= RB_FLAG_RED))
90 #define RB_MARK_BLACK(rb) ((void)((rb)->rb_info &= ~RB_FLAG_RED))
91 #define RB_INVERT_COLOR(rb) ((void)((rb)->rb_info ^= RB_FLAG_RED))
92 #define RB_ROOT_P(rbt, rb) ((rbt)->rbt_root == (rb))
93 #define RB_SET_POSITION(rb, position) \
94 ((void)((position) ? ((rb)->rb_info |= RB_FLAG_POSITION) : \
95 ((rb)->rb_info &= ~RB_FLAG_POSITION)))
96 #define RB_ZERO_PROPERTIES(rb) ((void)((rb)->rb_info &= ~RB_FLAG_MASK))
97 #define RB_COPY_PROPERTIES(dst, src) \
98 ((void)((dst)->rb_info ^= ((dst)->rb_info ^ (src)->rb_info) & RB_FLAG_MASK))
99 #define RB_SWAP_PROPERTIES(a, b) do { \
100 uintptr_t xorinfo = ((a)->rb_info ^ (b)->rb_info) & RB_FLAG_MASK; \
101 (a)->rb_info ^= xorinfo; \
102 (b)->rb_info ^= xorinfo; \
103 } while (/*CONSTCOND*/ 0)
105 #ifndef static_assert
106 #define _static_assert_concat_(a,b) a##b
107 #define _static_assert_concat(a,b) _static_assert_concat_(a,b)
108 #define static_assert(c, m) struct _static_assert_concat(static_assert_failure_, __LINE__) { int _static_assert_concat(static_assert_failure_, __LINE__)[(c)? 1 : -1]; }
111 /* The size of struct_rbnode must match:
112 * sizeof(struct rb_node { void * opaque[3] })
114 typedef struct rb_node
{
115 struct rb_node
*rb_nodes
[2];
118 * rb_info contains the two flags and the parent back pointer.
119 * We put the two flags in the low two bits since we know that
120 * rb_node will have an alignment of 4 or 8 bytes.
125 static_assert(sizeof(struct { void * opaque
[3]; }) == sizeof(rb_node_t
),
126 "Mismatch in size between opaque and internal rb_node_t");
128 typedef struct rb_tree
{
129 struct rb_node
*rbt_root
;
130 const rb_tree_ops_t
*rbt_ops
;
131 struct rb_node
*rbt_minmax
[2];
133 void *unused
[3]; // Unused padding for possible future use
136 static_assert(sizeof(struct { void * opaque
[8]; }) == sizeof(rb_tree_t
),
137 "Mismatch in size between opaque and internal rb_tree_t");
139 static void rb_tree_insert_rebalance(struct rb_tree
*, struct rb_node
*);
140 static void rb_tree_removal_rebalance(struct rb_tree
*, struct rb_node
*,
143 static const struct rb_node
*rb_tree_iterate_const(const struct rb_tree
*,
144 const struct rb_node
*, const unsigned int);
145 static bool rb_tree_check_node(const struct rb_tree
*, const struct rb_node
*,
146 const struct rb_node
*, bool);
148 TAILQ_HEAD(rb_node_qh
, rb_node
);
150 #define RB_TAILQ_REMOVE(a, b, c) TAILQ_REMOVE(a, b, c)
151 #define RB_TAILQ_INIT(a) TAILQ_INIT(a)
152 #define RB_TAILQ_INSERT_HEAD(a, b, c) TAILQ_INSERT_HEAD(a, b, c)
153 #define RB_TAILQ_INSERT_BEFORE(a, b, c) TAILQ_INSERT_BEFORE(a, b, c)
154 #define RB_TAILQ_INSERT_AFTER(a, b, c, d) TAILQ_INSERT_AFTER(a, b, c, d)
156 #define KASSERT(s) assert(s)
159 #define rb_tree_check_node(a, b, c, d) true
161 #define RB_TAILQ_REMOVE(a, b, c) do { } while (/*CONSTCOND*/0)
162 #define RB_TAILQ_INIT(a) do { } while (/*CONSTCOND*/0)
163 #define RB_TAILQ_INSERT_HEAD(a, b, c) do { } while (/*CONSTCOND*/0)
164 #define RB_TAILQ_INSERT_BEFORE(a, b, c) do { } while (/*CONSTCOND*/0)
165 #define RB_TAILQ_INSERT_AFTER(a, b, c, d) do { } while (/*CONSTCOND*/0)
167 #define KASSERT(s) do { } while (/*CONSTCOND*/ 0)
171 #define RBSTAT_INC(v) ((void)((v)++))
172 #define RBSTAT_DEC(v) ((void)((v)--))
174 #define RBSTAT_INC(v) do { } while (/*CONSTCOND*/0)
175 #define RBSTAT_DEC(v) do { } while (/*CONSTCOND*/0)
178 #define RB_NODETOITEM(rbto, rbn) \
179 ((void *)((uintptr_t)(rbn) - (rbto)->rbto_node_offset))
180 #define RB_ITEMTONODE(rbto, rbn) \
181 ((rb_node_t *)((uintptr_t)(rbn) + (rbto)->rbto_node_offset))
183 #define RB_SENTINEL_NODE NULL
186 rb_tree_init(struct rb_tree
*rbt
, const rb_tree_ops_t
*ops
)
190 rbt
->rbt_root
= RB_SENTINEL_NODE
;
191 RB_TAILQ_INIT(&rbt
->rbt_nodes
);
193 rbt
->rbt_minmax
[RB_DIR_LEFT
] = rbt
->rbt_root
; /* minimum node */
194 rbt
->rbt_minmax
[RB_DIR_RIGHT
] = rbt
->rbt_root
; /* maximum node */
198 rbt
->rbt_insertions
= 0;
199 rbt
->rbt_removals
= 0;
200 rbt
->rbt_insertion_rebalance_calls
= 0;
201 rbt
->rbt_insertion_rebalance_passes
= 0;
202 rbt
->rbt_removal_rebalance_calls
= 0;
203 rbt
->rbt_removal_rebalance_passes
= 0;
208 rb_tree_find_node(struct rb_tree
*rbt
, const void *key
)
210 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
211 rbto_compare_key_fn compare_key
= rbto
->rbto_compare_key
;
212 struct rb_node
*parent
= rbt
->rbt_root
;
214 while (!RB_SENTINEL_P(parent
)) {
215 void *pobj
= RB_NODETOITEM(rbto
, parent
);
216 const signed int diff
= (*compare_key
)(rbto
->rbto_context
,
220 parent
= parent
->rb_nodes
[diff
< 0];
227 rb_tree_find_node_geq(struct rb_tree
*rbt
, const void *key
)
229 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
230 rbto_compare_key_fn compare_key
= rbto
->rbto_compare_key
;
231 struct rb_node
*parent
= rbt
->rbt_root
, *last
= NULL
;
233 while (!RB_SENTINEL_P(parent
)) {
234 void *pobj
= RB_NODETOITEM(rbto
, parent
);
235 const signed int diff
= (*compare_key
)(rbto
->rbto_context
,
241 parent
= parent
->rb_nodes
[diff
< 0];
244 return last
== NULL
? NULL
: RB_NODETOITEM(rbto
, last
);
248 rb_tree_find_node_leq(struct rb_tree
*rbt
, const void *key
)
250 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
251 rbto_compare_key_fn compare_key
= rbto
->rbto_compare_key
;
252 struct rb_node
*parent
= rbt
->rbt_root
, *last
= NULL
;
254 while (!RB_SENTINEL_P(parent
)) {
255 void *pobj
= RB_NODETOITEM(rbto
, parent
);
256 const signed int diff
= (*compare_key
)(rbto
->rbto_context
,
262 parent
= parent
->rb_nodes
[diff
< 0];
265 return last
== NULL
? NULL
: RB_NODETOITEM(rbto
, last
);
269 rb_tree_insert_node(struct rb_tree
*rbt
, void *object
)
271 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
272 rbto_compare_nodes_fn compare_nodes
= rbto
->rbto_compare_nodes
;
273 struct rb_node
*parent
, *tmp
, *self
= RB_ITEMTONODE(rbto
, object
);
274 unsigned int position
;
277 RBSTAT_INC(rbt
->rbt_insertions
);
281 * This is a hack. Because rbt->rbt_root is just a struct rb_node *,
282 * just like rb_node->rb_nodes[RB_DIR_LEFT], we can use this fact to
283 * avoid a lot of tests for root and know that even at root,
284 * updating RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will
285 * update rbt->rbt_root.
287 parent
= (struct rb_node
*)(void *)&rbt
->rbt_root
;
288 position
= RB_DIR_LEFT
;
291 * Find out where to place this new leaf.
293 while (!RB_SENTINEL_P(tmp
)) {
294 void *tobj
= RB_NODETOITEM(rbto
, tmp
);
295 const signed int diff
= (*compare_nodes
)(rbto
->rbto_context
,
297 if (__predict_false(diff
== 0)) {
299 * Node already exists; return it.
304 position
= (diff
< 0);
305 tmp
= parent
->rb_nodes
[position
];
310 struct rb_node
*prev
= NULL
, *next
= NULL
;
312 if (position
== RB_DIR_RIGHT
)
314 else if (tmp
!= rbt
->rbt_root
)
318 * Verify our sequential position
320 KASSERT(prev
== NULL
|| !RB_SENTINEL_P(prev
));
321 KASSERT(next
== NULL
|| !RB_SENTINEL_P(next
));
322 if (prev
!= NULL
&& next
== NULL
)
323 next
= TAILQ_NEXT(prev
, rb_link
);
324 if (prev
== NULL
&& next
!= NULL
)
325 prev
= TAILQ_PREV(next
, rb_node_qh
, rb_link
);
326 KASSERT(prev
== NULL
|| !RB_SENTINEL_P(prev
));
327 KASSERT(next
== NULL
|| !RB_SENTINEL_P(next
));
328 KASSERT(prev
== NULL
|| (*compare_nodes
)(rbto
->rbto_context
,
329 RB_NODETOITEM(rbto
, prev
), RB_NODETOITEM(rbto
, self
)) < 0);
330 KASSERT(next
== NULL
|| (*compare_nodes
)(rbto
->rbto_context
,
331 RB_NODETOITEM(rbto
, self
), RB_NODETOITEM(rbto
, next
)) < 0);
336 * Initialize the node and insert as a leaf into the tree.
338 RB_SET_FATHER(self
, parent
);
339 RB_SET_POSITION(self
, position
);
340 if (__predict_false(parent
== (struct rb_node
*)(void *)&rbt
->rbt_root
)) {
341 RB_MARK_BLACK(self
); /* root is always black */
343 rbt
->rbt_minmax
[RB_DIR_LEFT
] = self
;
344 rbt
->rbt_minmax
[RB_DIR_RIGHT
] = self
;
348 KASSERT(position
== RB_DIR_LEFT
|| position
== RB_DIR_RIGHT
);
351 * Keep track of the minimum and maximum nodes. If our
352 * parent is a minmax node and we on their min/max side,
353 * we must be the new min/max node.
355 if (parent
== rbt
->rbt_minmax
[position
])
356 rbt
->rbt_minmax
[position
] = self
;
357 #endif /* !RBSMALL */
359 * All new nodes are colored red. We only need to rebalance
360 * if our parent is also red.
363 rebalance
= RB_RED_P(parent
);
365 KASSERT(RB_SENTINEL_P(parent
->rb_nodes
[position
]));
366 self
->rb_left
= parent
->rb_nodes
[position
];
367 self
->rb_right
= parent
->rb_nodes
[position
];
368 parent
->rb_nodes
[position
] = self
;
369 KASSERT(RB_CHILDLESS_P(self
));
372 * Insert the new node into a sorted list for easy sequential access
376 if (RB_ROOT_P(rbt
, self
)) {
377 RB_TAILQ_INSERT_HEAD(&rbt
->rbt_nodes
, self
, rb_link
);
378 } else if (position
== RB_DIR_LEFT
) {
379 KASSERT((*compare_nodes
)(rbto
->rbto_context
,
380 RB_NODETOITEM(rbto
, self
),
381 RB_NODETOITEM(rbto
, RB_FATHER(self
))) < 0);
382 RB_TAILQ_INSERT_BEFORE(RB_FATHER(self
), self
, rb_link
);
384 KASSERT((*compare_nodes
)(rbto
->rbto_context
,
385 RB_NODETOITEM(rbto
, RB_FATHER(self
)),
386 RB_NODETOITEM(rbto
, self
)) < 0);
387 RB_TAILQ_INSERT_AFTER(&rbt
->rbt_nodes
, RB_FATHER(self
),
391 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, !rebalance
));
394 * Rebalance tree after insertion
397 rb_tree_insert_rebalance(rbt
, self
);
398 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, true));
401 /* Succesfully inserted, return our node pointer. */
406 * Swap the location and colors of 'self' and its child @ which. The child
407 * can not be a sentinel node. This is our rotation function. However,
408 * since it preserves coloring, it great simplifies both insertion and
409 * removal since rotation almost always involves the exchanging of colors
410 * as a separate step.
414 rb_tree_reparent_nodes(struct rb_tree
*rbt
, struct rb_node
*old_father
,
415 const unsigned int which
)
417 const unsigned int other
= which
^ RB_DIR_OTHER
;
418 struct rb_node
* const grandpa
= RB_FATHER(old_father
);
419 struct rb_node
* const old_child
= old_father
->rb_nodes
[which
];
420 struct rb_node
* const new_father
= old_child
;
421 struct rb_node
* const new_child
= old_father
;
423 KASSERT(which
== RB_DIR_LEFT
|| which
== RB_DIR_RIGHT
);
425 KASSERT(!RB_SENTINEL_P(old_child
));
426 KASSERT(RB_FATHER(old_child
) == old_father
);
428 KASSERT(rb_tree_check_node(rbt
, old_father
, NULL
, false));
429 KASSERT(rb_tree_check_node(rbt
, old_child
, NULL
, false));
430 KASSERT(RB_ROOT_P(rbt
, old_father
) ||
431 rb_tree_check_node(rbt
, grandpa
, NULL
, false));
434 * Exchange descendant linkages.
436 grandpa
->rb_nodes
[RB_POSITION(old_father
)] = new_father
;
437 new_child
->rb_nodes
[which
] = old_child
->rb_nodes
[other
];
438 new_father
->rb_nodes
[other
] = new_child
;
441 * Update ancestor linkages
443 RB_SET_FATHER(new_father
, grandpa
);
444 RB_SET_FATHER(new_child
, new_father
);
447 * Exchange properties between new_father and new_child. The only
448 * change is that new_child's position is now on the other side.
454 RB_COPY_PROPERTIES(&tmp
, old_child
);
455 RB_COPY_PROPERTIES(new_father
, old_father
);
456 RB_COPY_PROPERTIES(new_child
, &tmp
);
459 RB_SWAP_PROPERTIES(new_father
, new_child
);
461 RB_SET_POSITION(new_child
, other
);
464 * Make sure to reparent the new child to ourself.
466 if (!RB_SENTINEL_P(new_child
->rb_nodes
[which
])) {
467 RB_SET_FATHER(new_child
->rb_nodes
[which
], new_child
);
468 RB_SET_POSITION(new_child
->rb_nodes
[which
], which
);
471 KASSERT(rb_tree_check_node(rbt
, new_father
, NULL
, false));
472 KASSERT(rb_tree_check_node(rbt
, new_child
, NULL
, false));
473 KASSERT(RB_ROOT_P(rbt
, new_father
) ||
474 rb_tree_check_node(rbt
, grandpa
, NULL
, false));
478 rb_tree_insert_rebalance(struct rb_tree
*rbt
, struct rb_node
*self
)
480 struct rb_node
* father
= RB_FATHER(self
);
481 struct rb_node
* grandpa
= RB_FATHER(father
);
482 struct rb_node
* uncle
;
486 KASSERT(!RB_ROOT_P(rbt
, self
));
487 KASSERT(RB_RED_P(self
));
488 KASSERT(RB_RED_P(father
));
489 RBSTAT_INC(rbt
->rbt_insertion_rebalance_calls
);
492 KASSERT(!RB_SENTINEL_P(self
));
494 KASSERT(RB_RED_P(self
));
495 KASSERT(RB_RED_P(father
));
497 * We are red and our parent is red, therefore we must have a
498 * grandfather and he must be black.
500 grandpa
= RB_FATHER(father
);
501 KASSERT(RB_BLACK_P(grandpa
));
502 KASSERT(RB_DIR_RIGHT
== 1 && RB_DIR_LEFT
== 0);
503 which
= (father
== grandpa
->rb_right
);
504 other
= which
^ RB_DIR_OTHER
;
505 uncle
= grandpa
->rb_nodes
[other
];
507 if (RB_BLACK_P(uncle
))
510 RBSTAT_INC(rbt
->rbt_insertion_rebalance_passes
);
512 * Case 1: our uncle is red
513 * Simply invert the colors of our parent and
514 * uncle and make our grandparent red. And
515 * then solve the problem up at his level.
517 RB_MARK_BLACK(uncle
);
518 RB_MARK_BLACK(father
);
519 if (__predict_false(RB_ROOT_P(rbt
, grandpa
))) {
521 * If our grandpa is root, don't bother
522 * setting him to red, just return.
524 KASSERT(RB_BLACK_P(grandpa
));
527 RB_MARK_RED(grandpa
);
529 father
= RB_FATHER(self
);
530 KASSERT(RB_RED_P(self
));
531 if (RB_BLACK_P(father
)) {
533 * If our greatgrandpa is black, we're done.
535 KASSERT(RB_BLACK_P(rbt
->rbt_root
));
540 KASSERT(!RB_ROOT_P(rbt
, self
));
541 KASSERT(RB_RED_P(self
));
542 KASSERT(RB_RED_P(father
));
543 KASSERT(RB_BLACK_P(uncle
));
544 KASSERT(RB_BLACK_P(grandpa
));
546 * Case 2&3: our uncle is black.
548 if (self
== father
->rb_nodes
[other
]) {
550 * Case 2: we are on the same side as our uncle
551 * Swap ourselves with our parent so this case
552 * becomes case 3. Basically our parent becomes our
555 rb_tree_reparent_nodes(rbt
, father
, other
);
556 KASSERT(RB_FATHER(father
) == self
);
557 KASSERT(self
->rb_nodes
[which
] == father
);
558 KASSERT(RB_FATHER(self
) == grandpa
);
560 father
= RB_FATHER(self
);
562 KASSERT(RB_RED_P(self
) && RB_RED_P(father
));
563 KASSERT(grandpa
->rb_nodes
[which
] == father
);
565 * Case 3: we are opposite a child of a black uncle.
566 * Swap our parent and grandparent. Since our grandfather
567 * is black, our father will become black and our new sibling
568 * (former grandparent) will become red.
570 rb_tree_reparent_nodes(rbt
, grandpa
, which
);
571 KASSERT(RB_FATHER(self
) == father
);
572 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_POSITION(self
) ^ RB_DIR_OTHER
] == grandpa
);
573 KASSERT(RB_RED_P(self
));
574 KASSERT(RB_BLACK_P(father
));
575 KASSERT(RB_RED_P(grandpa
));
578 * Final step: Set the root to black.
580 RB_MARK_BLACK(rbt
->rbt_root
);
584 rb_tree_prune_node(struct rb_tree
*rbt
, struct rb_node
*self
, bool rebalance
)
586 const unsigned int which
= RB_POSITION(self
);
587 struct rb_node
*father
= RB_FATHER(self
);
589 const bool was_root
= RB_ROOT_P(rbt
, self
);
592 KASSERT(rebalance
|| (RB_ROOT_P(rbt
, self
) || RB_RED_P(self
)));
593 KASSERT(!rebalance
|| RB_BLACK_P(self
));
594 KASSERT(RB_CHILDLESS_P(self
));
595 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
598 * Since we are childless, we know that self->rb_left is pointing
599 * to the sentinel node.
601 father
->rb_nodes
[which
] = self
->rb_left
;
604 * Remove ourselves from the node list, decrement the count,
605 * and update min/max.
607 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
610 if (__predict_false(rbt
->rbt_minmax
[RB_POSITION(self
)] == self
)) {
611 rbt
->rbt_minmax
[RB_POSITION(self
)] = father
;
613 * When removing the root, rbt->rbt_minmax[RB_DIR_LEFT] is
614 * updated automatically, but we also need to update
615 * rbt->rbt_minmax[RB_DIR_RIGHT];
617 if (__predict_false(was_root
)) {
618 rbt
->rbt_minmax
[RB_DIR_RIGHT
] = father
;
621 RB_SET_FATHER(self
, NULL
);
625 * Rebalance if requested.
628 rb_tree_removal_rebalance(rbt
, father
, which
);
629 KASSERT(was_root
|| rb_tree_check_node(rbt
, father
, NULL
, true));
633 * When deleting an interior node
636 rb_tree_swap_prune_and_rebalance(struct rb_tree
*rbt
, struct rb_node
*self
,
637 struct rb_node
*standin
)
639 const unsigned int standin_which
= RB_POSITION(standin
);
640 unsigned int standin_other
= standin_which
^ RB_DIR_OTHER
;
641 struct rb_node
*standin_son
;
642 struct rb_node
*standin_father
= RB_FATHER(standin
);
643 bool rebalance
= RB_BLACK_P(standin
);
645 if (standin_father
== self
) {
647 * As a child of self, any childen would be opposite of
650 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_other
]));
651 standin_son
= standin
->rb_nodes
[standin_which
];
654 * Since we aren't a child of self, any childen would be
655 * on the same side as our parent.
657 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_which
]));
658 standin_son
= standin
->rb_nodes
[standin_other
];
662 * the node we are removing must have two children.
664 KASSERT(RB_TWOCHILDREN_P(self
));
666 * If standin has a child, it must be red.
668 KASSERT(RB_SENTINEL_P(standin_son
) || RB_RED_P(standin_son
));
671 * Verify things are sane.
673 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
674 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, false));
676 if (__predict_false(RB_RED_P(standin_son
))) {
678 * We know we have a red child so if we flip it to black
679 * we don't have to rebalance.
681 KASSERT(rb_tree_check_node(rbt
, standin_son
, NULL
, true));
682 RB_MARK_BLACK(standin_son
);
685 if (standin_father
== self
) {
686 KASSERT(RB_POSITION(standin_son
) == standin_which
);
688 KASSERT(RB_POSITION(standin_son
) == standin_other
);
690 * Change the son's parentage to point to his grandpa.
692 RB_SET_FATHER(standin_son
, standin_father
);
693 RB_SET_POSITION(standin_son
, standin_which
);
697 if (standin_father
== self
) {
699 * If we are about to delete the standin's father, then when
700 * we call rebalance, we need to use ourselves as our father.
701 * Otherwise remember our original father. Also, sincef we are
702 * our standin's father we only need to reparent the standin's
709 KASSERT(RB_SENTINEL_P(standin
->rb_nodes
[standin_other
]));
710 KASSERT(!RB_SENTINEL_P(self
->rb_nodes
[standin_other
]));
711 KASSERT(self
->rb_nodes
[standin_which
] == standin
);
713 * Have our son/standin adopt his brother as his new son.
715 standin_father
= standin
;
719 * | / \ | T --> / \ | / |
720 * | ..... | S --> ..... | T |
722 * Sever standin's connection to his father.
724 standin_father
->rb_nodes
[standin_which
] = standin_son
;
728 standin
->rb_nodes
[standin_other
] = self
->rb_nodes
[standin_other
];
729 RB_SET_FATHER(standin
->rb_nodes
[standin_other
], standin
);
730 KASSERT(RB_POSITION(self
->rb_nodes
[standin_other
]) == standin_other
);
732 * Use standin_other because we need to preserve standin_which
733 * for the removal_rebalance.
735 standin_other
= standin_which
;
739 * Move the only remaining son to our standin. If our standin is our
740 * son, this will be the only son needed to be moved.
742 KASSERT(standin
->rb_nodes
[standin_other
] != self
->rb_nodes
[standin_other
]);
743 standin
->rb_nodes
[standin_other
] = self
->rb_nodes
[standin_other
];
744 RB_SET_FATHER(standin
->rb_nodes
[standin_other
], standin
);
747 * Now copy the result of self to standin and then replace
748 * self with standin in the tree.
750 RB_COPY_PROPERTIES(standin
, self
);
751 RB_SET_FATHER(standin
, RB_FATHER(self
));
752 RB_FATHER(standin
)->rb_nodes
[RB_POSITION(standin
)] = standin
;
755 * Remove ourselves from the node list, decrement the count,
756 * and update min/max.
758 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
761 if (__predict_false(rbt
->rbt_minmax
[RB_POSITION(self
)] == self
))
762 rbt
->rbt_minmax
[RB_POSITION(self
)] = RB_FATHER(self
);
763 RB_SET_FATHER(self
, NULL
);
766 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, false));
767 KASSERT(RB_FATHER_SENTINEL_P(standin
)
768 || rb_tree_check_node(rbt
, standin_father
, NULL
, false));
769 KASSERT(RB_LEFT_SENTINEL_P(standin
)
770 || rb_tree_check_node(rbt
, standin
->rb_left
, NULL
, false));
771 KASSERT(RB_RIGHT_SENTINEL_P(standin
)
772 || rb_tree_check_node(rbt
, standin
->rb_right
, NULL
, false));
777 rb_tree_removal_rebalance(rbt
, standin_father
, standin_which
);
778 KASSERT(rb_tree_check_node(rbt
, standin
, NULL
, true));
782 * We could do this by doing
783 * rb_tree_node_swap(rbt, self, which);
784 * rb_tree_prune_node(rbt, self, false);
786 * But it's more efficient to just evalate and recolor the child.
789 rb_tree_prune_blackred_branch(struct rb_tree
*rbt
, struct rb_node
*self
,
792 struct rb_node
*father
= RB_FATHER(self
);
793 struct rb_node
*son
= self
->rb_nodes
[which
];
795 const bool was_root
= RB_ROOT_P(rbt
, self
);
798 KASSERT(which
== RB_DIR_LEFT
|| which
== RB_DIR_RIGHT
);
799 KASSERT(RB_BLACK_P(self
) && RB_RED_P(son
));
800 KASSERT(!RB_TWOCHILDREN_P(son
));
801 KASSERT(RB_CHILDLESS_P(son
));
802 KASSERT(rb_tree_check_node(rbt
, self
, NULL
, false));
803 KASSERT(rb_tree_check_node(rbt
, son
, NULL
, false));
806 * Remove ourselves from the tree and give our former child our
807 * properties (position, color, root).
809 RB_COPY_PROPERTIES(son
, self
);
810 father
->rb_nodes
[RB_POSITION(son
)] = son
;
811 RB_SET_FATHER(son
, father
);
814 * Remove ourselves from the node list, decrement the count,
817 RB_TAILQ_REMOVE(&rbt
->rbt_nodes
, self
, rb_link
);
820 if (__predict_false(was_root
)) {
821 KASSERT(rbt
->rbt_minmax
[which
] == son
);
822 rbt
->rbt_minmax
[which
^ RB_DIR_OTHER
] = son
;
823 } else if (rbt
->rbt_minmax
[RB_POSITION(self
)] == self
) {
824 rbt
->rbt_minmax
[RB_POSITION(self
)] = son
;
826 RB_SET_FATHER(self
, NULL
);
829 KASSERT(was_root
|| rb_tree_check_node(rbt
, father
, NULL
, true));
830 KASSERT(rb_tree_check_node(rbt
, son
, NULL
, true));
834 rb_tree_remove_node(struct rb_tree
*rbt
, void *object
)
836 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
837 struct rb_node
*standin
, *self
= RB_ITEMTONODE(rbto
, object
);
840 KASSERT(!RB_SENTINEL_P(self
));
841 RBSTAT_INC(rbt
->rbt_removals
);
844 * In the following diagrams, we (the node to be removed) are S. Red
845 * nodes are lowercase. T could be either red or black.
847 * Remember the major axiom of the red-black tree: the number of
848 * black nodes from the root to each leaf is constant across all
849 * leaves, only the number of red nodes varies.
851 * Thus removing a red leaf doesn't require any other changes to a
852 * red-black tree. So if we must remove a node, attempt to rearrange
853 * the tree so we can remove a red node.
855 * The simpliest case is a childless red node or a childless root node:
857 * | T --> T | or | R --> * |
860 if (RB_CHILDLESS_P(self
)) {
861 const bool rebalance
= RB_BLACK_P(self
) && !RB_ROOT_P(rbt
, self
);
862 rb_tree_prune_node(rbt
, self
, rebalance
);
865 KASSERT(!RB_CHILDLESS_P(self
));
866 if (!RB_TWOCHILDREN_P(self
)) {
868 * The next simpliest case is the node we are deleting is
869 * black and has one red child.
875 which
= RB_LEFT_SENTINEL_P(self
) ? RB_DIR_RIGHT
: RB_DIR_LEFT
;
876 KASSERT(RB_BLACK_P(self
));
877 KASSERT(RB_RED_P(self
->rb_nodes
[which
]));
878 KASSERT(RB_CHILDLESS_P(self
->rb_nodes
[which
]));
879 rb_tree_prune_blackred_branch(rbt
, self
, which
);
882 KASSERT(RB_TWOCHILDREN_P(self
));
885 * We invert these because we prefer to remove from the inside of
888 which
= RB_POSITION(self
) ^ RB_DIR_OTHER
;
891 * Let's find the node closes to us opposite of our parent
892 * Now swap it with ourself, "prune" it, and rebalance, if needed.
894 standin
= RB_ITEMTONODE(rbto
, rb_tree_iterate(rbt
, object
, which
));
895 rb_tree_swap_prune_and_rebalance(rbt
, self
, standin
);
899 rb_tree_removal_rebalance(struct rb_tree
*rbt
, struct rb_node
*parent
,
902 KASSERT(!RB_SENTINEL_P(parent
));
903 KASSERT(RB_SENTINEL_P(parent
->rb_nodes
[which
]));
904 KASSERT(which
== RB_DIR_LEFT
|| which
== RB_DIR_RIGHT
);
905 RBSTAT_INC(rbt
->rbt_removal_rebalance_calls
);
907 while (RB_BLACK_P(parent
->rb_nodes
[which
])) {
908 unsigned int other
= which
^ RB_DIR_OTHER
;
909 struct rb_node
*brother
= parent
->rb_nodes
[other
];
911 RBSTAT_INC(rbt
->rbt_removal_rebalance_passes
);
913 KASSERT(!RB_SENTINEL_P(brother
));
915 * For cases 1, 2a, and 2b, our brother's children must
916 * be black and our father must be black
918 if (RB_BLACK_P(parent
)
919 && RB_BLACK_P(brother
->rb_left
)
920 && RB_BLACK_P(brother
->rb_right
)) {
921 if (RB_RED_P(brother
)) {
923 * Case 1: Our brother is red, swap its
924 * position (and colors) with our parent.
925 * This should now be case 2b (unless C or E
926 * has a red child which is case 3; thus no
927 * explicit branch to case 2b).
933 KASSERT(RB_BLACK_P(parent
));
934 rb_tree_reparent_nodes(rbt
, parent
, other
);
935 brother
= parent
->rb_nodes
[other
];
936 KASSERT(!RB_SENTINEL_P(brother
));
937 KASSERT(RB_RED_P(parent
));
938 KASSERT(RB_BLACK_P(brother
));
939 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, false));
940 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, false));
943 * Both our parent and brother are black.
944 * Change our brother to red, advance up rank
945 * and go through the loop again.
951 RB_MARK_RED(brother
);
952 KASSERT(RB_BLACK_P(brother
->rb_left
));
953 KASSERT(RB_BLACK_P(brother
->rb_right
));
954 if (RB_ROOT_P(rbt
, parent
))
955 return; /* root == parent == black */
956 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, false));
957 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, false));
958 which
= RB_POSITION(parent
);
959 parent
= RB_FATHER(parent
);
964 * Avoid an else here so that case 2a above can hit either
968 && RB_BLACK_P(brother
)
969 && RB_BLACK_P(brother
->rb_left
)
970 && RB_BLACK_P(brother
->rb_right
)) {
971 KASSERT(RB_RED_P(parent
));
972 KASSERT(RB_BLACK_P(brother
));
973 KASSERT(RB_BLACK_P(brother
->rb_left
));
974 KASSERT(RB_BLACK_P(brother
->rb_right
));
976 * We are black, our father is red, our brother and
977 * both nephews are black. Simply invert/exchange the
978 * colors of our father and brother (to black and red
985 RB_MARK_BLACK(parent
);
986 RB_MARK_RED(brother
);
987 KASSERT(rb_tree_check_node(rbt
, brother
, NULL
, true));
988 break; /* We're done! */
991 * Our brother must be black and have at least one
992 * red child (it may have two).
994 KASSERT(RB_BLACK_P(brother
));
995 KASSERT(RB_RED_P(brother
->rb_nodes
[which
]) ||
996 RB_RED_P(brother
->rb_nodes
[other
]));
997 if (RB_BLACK_P(brother
->rb_nodes
[other
])) {
999 * Case 3: our brother is black, our near
1000 * nephew is red, and our far nephew is black.
1001 * Swap our brother with our near nephew.
1002 * This result in a tree that matches case 4.
1003 * (Our father could be red or black).
1009 KASSERT(RB_RED_P(brother
->rb_nodes
[which
]));
1010 rb_tree_reparent_nodes(rbt
, brother
, which
);
1011 KASSERT(RB_FATHER(brother
) == parent
->rb_nodes
[other
]);
1012 brother
= parent
->rb_nodes
[other
];
1013 KASSERT(RB_RED_P(brother
->rb_nodes
[other
]));
1016 * Case 4: our brother is black and our far nephew
1017 * is red. Swap our father and brother locations and
1018 * change our far nephew to black. (these can be
1019 * done in either order so we change the color first).
1020 * The result is a valid red-black tree and is a
1021 * terminal case. (again we don't care about the
1024 * If the father is red, we will get a red-black-black
1027 * | B -> B --> F N |
1030 * If the father is black, we will get an all black
1033 * | B -> B --> F N |
1036 * If we had two red nephews, then after the swap,
1037 * our former father would have a red grandson.
1039 KASSERT(RB_BLACK_P(brother
));
1040 KASSERT(RB_RED_P(brother
->rb_nodes
[other
]));
1041 RB_MARK_BLACK(brother
->rb_nodes
[other
]);
1042 rb_tree_reparent_nodes(rbt
, parent
, other
);
1043 break; /* We're done! */
1046 KASSERT(rb_tree_check_node(rbt
, parent
, NULL
, true));
1050 rb_tree_iterate(struct rb_tree
*rbt
, void *object
, const unsigned int direction
)
1052 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
1053 const unsigned int other
= direction
^ RB_DIR_OTHER
;
1054 struct rb_node
*self
;
1056 KASSERT(direction
== RB_DIR_LEFT
|| direction
== RB_DIR_RIGHT
);
1058 if (object
== NULL
) {
1060 if (RB_SENTINEL_P(rbt
->rbt_root
))
1062 return RB_NODETOITEM(rbto
, rbt
->rbt_minmax
[direction
== RB_DIR_LEFT
? RB_DIR_RIGHT
: RB_DIR_LEFT
]);
1064 self
= rbt
->rbt_root
;
1065 if (RB_SENTINEL_P(self
))
1067 while (!RB_SENTINEL_P(self
->rb_nodes
[direction
== RB_DIR_LEFT
? RB_DIR_RIGHT
: RB_DIR_LEFT
]))
1068 self
= self
->rb_nodes
[direction
== RB_DIR_LEFT
? RB_DIR_RIGHT
: RB_DIR_LEFT
];
1069 return RB_NODETOITEM(rbto
, self
);
1070 #endif /* !RBSMALL */
1072 self
= RB_ITEMTONODE(rbto
, object
);
1073 KASSERT(!RB_SENTINEL_P(self
));
1075 * We can't go any further in this direction. We proceed up in the
1076 * opposite direction until our parent is in direction we want to go.
1078 if (RB_SENTINEL_P(self
->rb_nodes
[direction
])) {
1079 while (!RB_ROOT_P(rbt
, self
)) {
1080 if (other
== RB_POSITION(self
))
1081 return RB_NODETOITEM(rbto
, RB_FATHER(self
));
1082 self
= RB_FATHER(self
);
1088 * Advance down one in current direction and go down as far as possible
1089 * in the opposite direction.
1091 self
= self
->rb_nodes
[direction
];
1092 KASSERT(!RB_SENTINEL_P(self
));
1093 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
1094 self
= self
->rb_nodes
[other
];
1095 return RB_NODETOITEM(rbto
, self
);
1099 static const struct rb_node
*
1100 rb_tree_iterate_const(const struct rb_tree
*rbt
, const struct rb_node
*self
,
1101 const unsigned int direction
)
1103 const unsigned int other
= direction
^ RB_DIR_OTHER
;
1104 KASSERT(direction
== RB_DIR_LEFT
|| direction
== RB_DIR_RIGHT
);
1108 if (RB_SENTINEL_P(rbt
->rbt_root
))
1110 return rbt
->rbt_minmax
[direction
];
1112 self
= rbt
->rbt_root
;
1113 if (RB_SENTINEL_P(self
))
1115 while (!RB_SENTINEL_P(self
->rb_nodes
[direction
]))
1116 self
= self
->rb_nodes
[direction
];
1118 #endif /* !RBSMALL */
1120 KASSERT(!RB_SENTINEL_P(self
));
1122 * We can't go any further in this direction. We proceed up in the
1123 * opposite direction until our parent is in direction we want to go.
1125 if (RB_SENTINEL_P(self
->rb_nodes
[direction
])) {
1126 while (!RB_ROOT_P(rbt
, self
)) {
1127 if (other
== RB_POSITION(self
))
1128 return RB_FATHER(self
);
1129 self
= RB_FATHER(self
);
1135 * Advance down one in current direction and go down as far as possible
1136 * in the opposite direction.
1138 self
= self
->rb_nodes
[direction
];
1139 KASSERT(!RB_SENTINEL_P(self
));
1140 while (!RB_SENTINEL_P(self
->rb_nodes
[other
]))
1141 self
= self
->rb_nodes
[other
];
1146 rb_tree_count_black(const struct rb_node
*self
)
1148 unsigned int left
, right
;
1150 if (RB_SENTINEL_P(self
))
1153 left
= rb_tree_count_black(self
->rb_left
);
1154 right
= rb_tree_count_black(self
->rb_right
);
1156 KASSERT(left
== right
);
1158 return left
+ RB_BLACK_P(self
);
1162 rb_tree_check_node(const struct rb_tree
*rbt
, const struct rb_node
*self
,
1163 const struct rb_node
*prev
, bool red_check
)
1165 const rb_tree_ops_t
*rbto
= rbt
->rbt_ops
;
1166 rbto_compare_nodes_fn compare_nodes
= rbto
->rbto_compare_nodes
;
1168 KASSERT(!RB_SENTINEL_P(self
));
1169 KASSERT(prev
== NULL
|| (*compare_nodes
)(rbto
->rbto_context
,
1170 RB_NODETOITEM(rbto
, prev
), RB_NODETOITEM(rbto
, self
)) < 0);
1173 * Verify our relationship to our parent.
1175 if (RB_ROOT_P(rbt
, self
)) {
1176 KASSERT(self
== rbt
->rbt_root
);
1177 KASSERT(RB_POSITION(self
) == RB_DIR_LEFT
);
1178 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_DIR_LEFT
] == self
);
1179 KASSERT(RB_FATHER(self
) == (const struct rb_node
*) &rbt
->rbt_root
);
1181 int diff
= (*compare_nodes
)(rbto
->rbto_context
,
1182 RB_NODETOITEM(rbto
, self
),
1183 RB_NODETOITEM(rbto
, RB_FATHER(self
)));
1185 KASSERT(self
!= rbt
->rbt_root
);
1186 KASSERT(!RB_FATHER_SENTINEL_P(self
));
1187 if (RB_POSITION(self
) == RB_DIR_LEFT
) {
1189 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_DIR_LEFT
] == self
);
1192 KASSERT(RB_FATHER(self
)->rb_nodes
[RB_DIR_RIGHT
] == self
);
1197 * Verify our position in the linked list against the tree itself.
1200 const struct rb_node
*prev0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_LEFT
);
1201 const struct rb_node
*next0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_RIGHT
);
1202 KASSERT(prev0
== TAILQ_PREV(self
, rb_node_qh
, rb_link
));
1203 KASSERT(next0
== TAILQ_NEXT(self
, rb_link
));
1205 KASSERT(prev0
!= NULL
|| self
== rbt
->rbt_minmax
[RB_DIR_LEFT
]);
1206 KASSERT(next0
!= NULL
|| self
== rbt
->rbt_minmax
[RB_DIR_RIGHT
]);
1211 * The root must be black.
1212 * There can never be two adjacent red nodes.
1215 KASSERT(!RB_ROOT_P(rbt
, self
) || RB_BLACK_P(self
));
1216 (void) rb_tree_count_black(self
);
1217 if (RB_RED_P(self
)) {
1218 const struct rb_node
*brother
;
1219 KASSERT(!RB_ROOT_P(rbt
, self
));
1220 brother
= RB_FATHER(self
)->rb_nodes
[RB_POSITION(self
) ^ RB_DIR_OTHER
];
1221 KASSERT(RB_BLACK_P(RB_FATHER(self
)));
1223 * I'm red and have no children, then I must either
1224 * have no brother or my brother also be red and
1225 * also have no children. (black count == 0)
1227 KASSERT(!RB_CHILDLESS_P(self
)
1228 || RB_SENTINEL_P(brother
)
1229 || RB_RED_P(brother
)
1230 || RB_CHILDLESS_P(brother
));
1232 * If I'm not childless, I must have two children
1233 * and they must be both be black.
1235 KASSERT(RB_CHILDLESS_P(self
)
1236 || (RB_TWOCHILDREN_P(self
)
1237 && RB_BLACK_P(self
->rb_left
)
1238 && RB_BLACK_P(self
->rb_right
)));
1240 * If I'm not childless, thus I have black children,
1241 * then my brother must either be black or have two
1244 KASSERT(RB_CHILDLESS_P(self
)
1245 || RB_BLACK_P(brother
)
1246 || (RB_TWOCHILDREN_P(brother
)
1247 && RB_BLACK_P(brother
->rb_left
)
1248 && RB_BLACK_P(brother
->rb_right
)));
1251 * If I'm black and have one child, that child must
1252 * be red and childless.
1254 KASSERT(RB_CHILDLESS_P(self
)
1255 || RB_TWOCHILDREN_P(self
)
1256 || (!RB_LEFT_SENTINEL_P(self
)
1257 && RB_RIGHT_SENTINEL_P(self
)
1258 && RB_RED_P(self
->rb_left
)
1259 && RB_CHILDLESS_P(self
->rb_left
))
1260 || (!RB_RIGHT_SENTINEL_P(self
)
1261 && RB_LEFT_SENTINEL_P(self
)
1262 && RB_RED_P(self
->rb_right
)
1263 && RB_CHILDLESS_P(self
->rb_right
)));
1266 * If I'm a childless black node and my parent is
1267 * black, my 2nd closet relative away from my parent
1268 * is either red or has a red parent or red children.
1270 if (!RB_ROOT_P(rbt
, self
)
1271 && RB_CHILDLESS_P(self
)
1272 && RB_BLACK_P(RB_FATHER(self
))) {
1273 const unsigned int which
= RB_POSITION(self
);
1274 const unsigned int other
= which
^ RB_DIR_OTHER
;
1275 const struct rb_node
*relative0
, *relative
;
1277 relative0
= rb_tree_iterate_const(rbt
,
1279 KASSERT(relative0
!= NULL
);
1280 relative
= rb_tree_iterate_const(rbt
,
1282 KASSERT(relative
!= NULL
);
1283 KASSERT(RB_SENTINEL_P(relative
->rb_nodes
[which
]));
1285 KASSERT(RB_RED_P(relative
)
1286 || RB_RED_P(relative
->rb_left
)
1287 || RB_RED_P(relative
->rb_right
)
1288 || RB_RED_P(RB_FATHER(relative
)));
1293 * A grandparent's children must be real nodes and not
1294 * sentinels. First check out grandparent.
1296 KASSERT(RB_ROOT_P(rbt
, self
)
1297 || RB_ROOT_P(rbt
, RB_FATHER(self
))
1298 || RB_TWOCHILDREN_P(RB_FATHER(RB_FATHER(self
))));
1300 * If we are have grandchildren on our left, then
1301 * we must have a child on our right.
1303 KASSERT(RB_LEFT_SENTINEL_P(self
)
1304 || RB_CHILDLESS_P(self
->rb_left
)
1305 || !RB_RIGHT_SENTINEL_P(self
));
1307 * If we are have grandchildren on our right, then
1308 * we must have a child on our left.
1310 KASSERT(RB_RIGHT_SENTINEL_P(self
)
1311 || RB_CHILDLESS_P(self
->rb_right
)
1312 || !RB_LEFT_SENTINEL_P(self
));
1315 * If we have a child on the left and it doesn't have two
1316 * children make sure we don't have great-great-grandchildren on
1319 KASSERT(RB_TWOCHILDREN_P(self
->rb_left
)
1320 || RB_CHILDLESS_P(self
->rb_right
)
1321 || RB_CHILDLESS_P(self
->rb_right
->rb_left
)
1322 || RB_CHILDLESS_P(self
->rb_right
->rb_left
->rb_left
)
1323 || RB_CHILDLESS_P(self
->rb_right
->rb_left
->rb_right
)
1324 || RB_CHILDLESS_P(self
->rb_right
->rb_right
)
1325 || RB_CHILDLESS_P(self
->rb_right
->rb_right
->rb_left
)
1326 || RB_CHILDLESS_P(self
->rb_right
->rb_right
->rb_right
));
1329 * If we have a child on the right and it doesn't have two
1330 * children make sure we don't have great-great-grandchildren on
1333 KASSERT(RB_TWOCHILDREN_P(self
->rb_right
)
1334 || RB_CHILDLESS_P(self
->rb_left
)
1335 || RB_CHILDLESS_P(self
->rb_left
->rb_left
)
1336 || RB_CHILDLESS_P(self
->rb_left
->rb_left
->rb_left
)
1337 || RB_CHILDLESS_P(self
->rb_left
->rb_left
->rb_right
)
1338 || RB_CHILDLESS_P(self
->rb_left
->rb_right
)
1339 || RB_CHILDLESS_P(self
->rb_left
->rb_right
->rb_left
)
1340 || RB_CHILDLESS_P(self
->rb_left
->rb_right
->rb_right
));
1343 * If we are fully interior node, then our predecessors and
1344 * successors must have no children in our direction.
1346 if (RB_TWOCHILDREN_P(self
)) {
1347 const struct rb_node
*prev0
;
1348 const struct rb_node
*next0
;
1350 prev0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_LEFT
);
1351 KASSERT(prev0
!= NULL
);
1352 KASSERT(RB_RIGHT_SENTINEL_P(prev0
));
1354 next0
= rb_tree_iterate_const(rbt
, self
, RB_DIR_RIGHT
);
1355 KASSERT(next0
!= NULL
);
1356 KASSERT(RB_LEFT_SENTINEL_P(next0
));
1364 rb_tree_check(const struct rb_tree
*rbt
, bool red_check
)
1366 const struct rb_node
*self
;
1367 const struct rb_node
*prev
;
1369 unsigned int count
= 0;
1372 KASSERT(rbt
->rbt_root
!= NULL
);
1373 KASSERT(RB_LEFT_P(rbt
->rbt_root
));
1375 #if defined(RBSTATS) && !defined(RBSMALL)
1376 KASSERT(rbt
->rbt_count
> 1
1377 || rbt
->rbt_minmax
[RB_DIR_LEFT
] == rbt
->rbt_minmax
[RB_DIR_RIGHT
]);
1381 TAILQ_FOREACH(self
, &rbt
->rbt_nodes
, rb_link
) {
1382 rb_tree_check_node(rbt
, self
, prev
, false);
1388 KASSERT(rbt
->rbt_count
== count
);
1391 KASSERT(RB_BLACK_P(rbt
->rbt_root
));
1392 KASSERT(RB_SENTINEL_P(rbt
->rbt_root
)
1393 || rb_tree_count_black(rbt
->rbt_root
));
1396 * The root must be black.
1397 * There can never be two adjacent red nodes.
1399 TAILQ_FOREACH(self
, &rbt
->rbt_nodes
, rb_link
) {
1400 rb_tree_check_node(rbt
, self
, NULL
, true);
1404 #endif /* RBDEBUG */
1408 rb_tree_mark_depth(const struct rb_tree
*rbt
, const struct rb_node
*self
,
1409 size_t *depths
, size_t depth
)
1411 if (RB_SENTINEL_P(self
))
1414 if (RB_TWOCHILDREN_P(self
)) {
1415 rb_tree_mark_depth(rbt
, self
->rb_left
, depths
, depth
+ 1);
1416 rb_tree_mark_depth(rbt
, self
->rb_right
, depths
, depth
+ 1);
1420 if (!RB_LEFT_SENTINEL_P(self
)) {
1421 rb_tree_mark_depth(rbt
, self
->rb_left
, depths
, depth
+ 1);
1423 if (!RB_RIGHT_SENTINEL_P(self
)) {
1424 rb_tree_mark_depth(rbt
, self
->rb_right
, depths
, depth
+ 1);
1429 rb_tree_depths(const struct rb_tree
*rbt
, size_t *depths
)
1431 rb_tree_mark_depth(rbt
, rbt
->rbt_root
, depths
, 1);
1433 #endif /* RBSTATS */
1435 size_t rb_tree_count(rb_tree_t
*rbt
) {
1436 if (__predict_false(rbt
== NULL
))
1439 return rbt
->rbt_count
;