Home | History | Annotate | Line # | Download | only in gen
rb.c revision 1.13.16.1
      1  1.13.16.1  christos /*	$NetBSD: rb.c,v 1.13.16.1 2019/06/10 21:41:07 christos Exp $	*/
      2        1.1      matt 
      3        1.1      matt /*-
      4        1.1      matt  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5        1.1      matt  * All rights reserved.
      6        1.1      matt  *
      7        1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8        1.1      matt  * by Matt Thomas <matt (at) 3am-software.com>.
      9        1.1      matt  *
     10        1.1      matt  * Redistribution and use in source and binary forms, with or without
     11        1.1      matt  * modification, are permitted provided that the following conditions
     12        1.1      matt  * are met:
     13        1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14        1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15        1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17        1.1      matt  *    documentation and/or other materials provided with the distribution.
     18        1.1      matt  *
     19        1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30        1.1      matt  */
     31        1.1      matt 
     32  1.13.16.1  christos #if HAVE_NBTOOL_CONFIG_H
     33  1.13.16.1  christos #include "nbtool_config.h"
     34  1.13.16.1  christos #endif
     35  1.13.16.1  christos 
     36        1.1      matt #if !defined(_KERNEL) && !defined(_STANDALONE)
     37        1.1      matt #include <sys/types.h>
     38        1.1      matt #include <stddef.h>
     39        1.1      matt #include <assert.h>
     40        1.1      matt #include <stdbool.h>
     41        1.1      matt #ifdef RBDEBUG
     42        1.1      matt #define	KASSERT(s)	assert(s)
     43  1.13.16.1  christos #define	__rbt_unused
     44        1.1      matt #else
     45        1.3      matt #define KASSERT(s)	do { } while (/*CONSTCOND*/ 0)
     46  1.13.16.1  christos #define	__rbt_unused	__unused
     47        1.1      matt #endif
     48  1.13.16.1  christos __RCSID("$NetBSD: rb.c,v 1.13.16.1 2019/06/10 21:41:07 christos Exp $");
     49        1.1      matt #else
     50        1.1      matt #include <lib/libkern/libkern.h>
     51  1.13.16.1  christos __KERNEL_RCSID(0, "$NetBSD: rb.c,v 1.13.16.1 2019/06/10 21:41:07 christos Exp $");
     52  1.13.16.1  christos #ifndef DIAGNOSTIC
     53  1.13.16.1  christos #define	__rbt_unused	__unused
     54  1.13.16.1  christos #else
     55  1.13.16.1  christos #define	__rbt_unused
     56  1.13.16.1  christos #endif
     57        1.1      matt #endif
     58        1.1      matt 
     59        1.1      matt #ifdef _LIBC
     60        1.1      matt __weak_alias(rb_tree_init, _rb_tree_init)
     61        1.1      matt __weak_alias(rb_tree_find_node, _rb_tree_find_node)
     62        1.1      matt __weak_alias(rb_tree_find_node_geq, _rb_tree_find_node_geq)
     63        1.1      matt __weak_alias(rb_tree_find_node_leq, _rb_tree_find_node_leq)
     64        1.1      matt __weak_alias(rb_tree_insert_node, _rb_tree_insert_node)
     65        1.1      matt __weak_alias(rb_tree_remove_node, _rb_tree_remove_node)
     66        1.1      matt __weak_alias(rb_tree_iterate, _rb_tree_iterate)
     67        1.1      matt #ifdef RBDEBUG
     68        1.1      matt __weak_alias(rb_tree_check, _rb_tree_check)
     69        1.1      matt __weak_alias(rb_tree_depths, _rb_tree_depths)
     70        1.1      matt #endif
     71        1.1      matt 
     72        1.9      tron #include "namespace.h"
     73        1.1      matt #endif
     74        1.1      matt 
     75        1.1      matt #ifdef RBTEST
     76        1.8      matt #include "rbtree.h"
     77        1.1      matt #else
     78        1.8      matt #include <sys/rbtree.h>
     79        1.1      matt #endif
     80        1.1      matt 
     81        1.1      matt static void rb_tree_insert_rebalance(struct rb_tree *, struct rb_node *);
     82        1.1      matt static void rb_tree_removal_rebalance(struct rb_tree *, struct rb_node *,
     83        1.1      matt 	unsigned int);
     84        1.1      matt #ifdef RBDEBUG
     85        1.1      matt static const struct rb_node *rb_tree_iterate_const(const struct rb_tree *,
     86        1.1      matt 	const struct rb_node *, const unsigned int);
     87        1.1      matt static bool rb_tree_check_node(const struct rb_tree *, const struct rb_node *,
     88        1.1      matt 	const struct rb_node *, bool);
     89        1.1      matt #else
     90        1.1      matt #define	rb_tree_check_node(a, b, c, d)	true
     91        1.1      matt #endif
     92        1.1      matt 
     93        1.7     rmind #define	RB_NODETOITEM(rbto, rbn)	\
     94        1.7     rmind     ((void *)((uintptr_t)(rbn) - (rbto)->rbto_node_offset))
     95        1.7     rmind #define	RB_ITEMTONODE(rbto, rbn)	\
     96        1.7     rmind     ((rb_node_t *)((uintptr_t)(rbn) + (rbto)->rbto_node_offset))
     97        1.7     rmind 
     98        1.1      matt #define	RB_SENTINEL_NODE	NULL
     99        1.1      matt 
    100        1.1      matt void
    101        1.7     rmind rb_tree_init(struct rb_tree *rbt, const rb_tree_ops_t *ops)
    102        1.1      matt {
    103        1.7     rmind 
    104        1.1      matt 	rbt->rbt_ops = ops;
    105       1.11       mrg 	rbt->rbt_root = RB_SENTINEL_NODE;
    106        1.1      matt 	RB_TAILQ_INIT(&rbt->rbt_nodes);
    107        1.1      matt #ifndef RBSMALL
    108        1.1      matt 	rbt->rbt_minmax[RB_DIR_LEFT] = rbt->rbt_root;	/* minimum node */
    109        1.1      matt 	rbt->rbt_minmax[RB_DIR_RIGHT] = rbt->rbt_root;	/* maximum node */
    110        1.1      matt #endif
    111        1.1      matt #ifdef RBSTATS
    112        1.1      matt 	rbt->rbt_count = 0;
    113        1.1      matt 	rbt->rbt_insertions = 0;
    114        1.1      matt 	rbt->rbt_removals = 0;
    115        1.1      matt 	rbt->rbt_insertion_rebalance_calls = 0;
    116        1.1      matt 	rbt->rbt_insertion_rebalance_passes = 0;
    117        1.1      matt 	rbt->rbt_removal_rebalance_calls = 0;
    118        1.1      matt 	rbt->rbt_removal_rebalance_passes = 0;
    119        1.1      matt #endif
    120        1.1      matt }
    121        1.1      matt 
    122        1.7     rmind void *
    123        1.1      matt rb_tree_find_node(struct rb_tree *rbt, const void *key)
    124        1.1      matt {
    125        1.7     rmind 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    126        1.7     rmind 	rbto_compare_key_fn compare_key = rbto->rbto_compare_key;
    127        1.1      matt 	struct rb_node *parent = rbt->rbt_root;
    128        1.1      matt 
    129        1.1      matt 	while (!RB_SENTINEL_P(parent)) {
    130        1.7     rmind 		void *pobj = RB_NODETOITEM(rbto, parent);
    131        1.7     rmind 		const signed int diff = (*compare_key)(rbto->rbto_context,
    132        1.7     rmind 		    pobj, key);
    133        1.1      matt 		if (diff == 0)
    134        1.7     rmind 			return pobj;
    135        1.7     rmind 		parent = parent->rb_nodes[diff < 0];
    136        1.1      matt 	}
    137        1.1      matt 
    138        1.1      matt 	return NULL;
    139        1.1      matt }
    140        1.7     rmind 
    141        1.7     rmind void *
    142        1.1      matt rb_tree_find_node_geq(struct rb_tree *rbt, const void *key)
    143        1.1      matt {
    144        1.7     rmind 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    145        1.7     rmind 	rbto_compare_key_fn compare_key = rbto->rbto_compare_key;
    146        1.7     rmind 	struct rb_node *parent = rbt->rbt_root, *last = NULL;
    147        1.1      matt 
    148        1.1      matt 	while (!RB_SENTINEL_P(parent)) {
    149        1.7     rmind 		void *pobj = RB_NODETOITEM(rbto, parent);
    150        1.7     rmind 		const signed int diff = (*compare_key)(rbto->rbto_context,
    151        1.7     rmind 		    pobj, key);
    152        1.1      matt 		if (diff == 0)
    153        1.7     rmind 			return pobj;
    154        1.7     rmind 		if (diff > 0)
    155        1.1      matt 			last = parent;
    156        1.7     rmind 		parent = parent->rb_nodes[diff < 0];
    157        1.1      matt 	}
    158        1.1      matt 
    159       1.13      matt 	return last == NULL ? NULL : RB_NODETOITEM(rbto, last);
    160        1.1      matt }
    161        1.7     rmind 
    162        1.7     rmind void *
    163        1.1      matt rb_tree_find_node_leq(struct rb_tree *rbt, const void *key)
    164        1.1      matt {
    165        1.7     rmind 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    166        1.7     rmind 	rbto_compare_key_fn compare_key = rbto->rbto_compare_key;
    167        1.7     rmind 	struct rb_node *parent = rbt->rbt_root, *last = NULL;
    168        1.1      matt 
    169        1.1      matt 	while (!RB_SENTINEL_P(parent)) {
    170        1.7     rmind 		void *pobj = RB_NODETOITEM(rbto, parent);
    171        1.7     rmind 		const signed int diff = (*compare_key)(rbto->rbto_context,
    172        1.7     rmind 		    pobj, key);
    173        1.1      matt 		if (diff == 0)
    174        1.7     rmind 			return pobj;
    175        1.7     rmind 		if (diff < 0)
    176        1.1      matt 			last = parent;
    177        1.7     rmind 		parent = parent->rb_nodes[diff < 0];
    178        1.1      matt 	}
    179        1.1      matt 
    180       1.13      matt 	return last == NULL ? NULL : RB_NODETOITEM(rbto, last);
    181        1.1      matt }
    182        1.7     rmind 
    183        1.7     rmind void *
    184        1.7     rmind rb_tree_insert_node(struct rb_tree *rbt, void *object)
    185        1.1      matt {
    186        1.7     rmind 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    187        1.7     rmind 	rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes;
    188        1.7     rmind 	struct rb_node *parent, *tmp, *self = RB_ITEMTONODE(rbto, object);
    189        1.1      matt 	unsigned int position;
    190        1.1      matt 	bool rebalance;
    191        1.1      matt 
    192        1.1      matt 	RBSTAT_INC(rbt->rbt_insertions);
    193        1.1      matt 
    194        1.1      matt 	tmp = rbt->rbt_root;
    195        1.1      matt 	/*
    196        1.1      matt 	 * This is a hack.  Because rbt->rbt_root is just a struct rb_node *,
    197        1.1      matt 	 * just like rb_node->rb_nodes[RB_DIR_LEFT], we can use this fact to
    198        1.1      matt 	 * avoid a lot of tests for root and know that even at root,
    199        1.1      matt 	 * updating RB_FATHER(rb_node)->rb_nodes[RB_POSITION(rb_node)] will
    200        1.1      matt 	 * update rbt->rbt_root.
    201        1.1      matt 	 */
    202        1.3      matt 	parent = (struct rb_node *)(void *)&rbt->rbt_root;
    203        1.1      matt 	position = RB_DIR_LEFT;
    204        1.1      matt 
    205        1.1      matt 	/*
    206        1.1      matt 	 * Find out where to place this new leaf.
    207        1.1      matt 	 */
    208        1.1      matt 	while (!RB_SENTINEL_P(tmp)) {
    209        1.7     rmind 		void *tobj = RB_NODETOITEM(rbto, tmp);
    210        1.7     rmind 		const signed int diff = (*compare_nodes)(rbto->rbto_context,
    211        1.7     rmind 		    tobj, object);
    212        1.1      matt 		if (__predict_false(diff == 0)) {
    213        1.1      matt 			/*
    214        1.7     rmind 			 * Node already exists; return it.
    215        1.1      matt 			 */
    216        1.7     rmind 			return tobj;
    217        1.1      matt 		}
    218        1.1      matt 		parent = tmp;
    219        1.7     rmind 		position = (diff < 0);
    220        1.1      matt 		tmp = parent->rb_nodes[position];
    221        1.1      matt 	}
    222        1.1      matt 
    223        1.1      matt #ifdef RBDEBUG
    224        1.1      matt 	{
    225        1.1      matt 		struct rb_node *prev = NULL, *next = NULL;
    226        1.1      matt 
    227        1.1      matt 		if (position == RB_DIR_RIGHT)
    228        1.1      matt 			prev = parent;
    229        1.1      matt 		else if (tmp != rbt->rbt_root)
    230        1.1      matt 			next = parent;
    231        1.1      matt 
    232        1.1      matt 		/*
    233        1.1      matt 		 * Verify our sequential position
    234        1.1      matt 		 */
    235        1.1      matt 		KASSERT(prev == NULL || !RB_SENTINEL_P(prev));
    236        1.1      matt 		KASSERT(next == NULL || !RB_SENTINEL_P(next));
    237        1.1      matt 		if (prev != NULL && next == NULL)
    238        1.1      matt 			next = TAILQ_NEXT(prev, rb_link);
    239        1.1      matt 		if (prev == NULL && next != NULL)
    240        1.1      matt 			prev = TAILQ_PREV(next, rb_node_qh, rb_link);
    241        1.1      matt 		KASSERT(prev == NULL || !RB_SENTINEL_P(prev));
    242        1.1      matt 		KASSERT(next == NULL || !RB_SENTINEL_P(next));
    243        1.7     rmind 		KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context,
    244        1.7     rmind 		    RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0);
    245        1.7     rmind 		KASSERT(next == NULL || (*compare_nodes)(rbto->rbto_context,
    246        1.7     rmind 		    RB_NODETOITEM(rbto, self), RB_NODETOITEM(rbto, next)) < 0);
    247        1.1      matt 	}
    248        1.1      matt #endif
    249        1.1      matt 
    250        1.1      matt 	/*
    251        1.1      matt 	 * Initialize the node and insert as a leaf into the tree.
    252        1.1      matt 	 */
    253        1.1      matt 	RB_SET_FATHER(self, parent);
    254        1.1      matt 	RB_SET_POSITION(self, position);
    255        1.3      matt 	if (__predict_false(parent == (struct rb_node *)(void *)&rbt->rbt_root)) {
    256        1.1      matt 		RB_MARK_BLACK(self);		/* root is always black */
    257        1.1      matt #ifndef RBSMALL
    258        1.1      matt 		rbt->rbt_minmax[RB_DIR_LEFT] = self;
    259        1.1      matt 		rbt->rbt_minmax[RB_DIR_RIGHT] = self;
    260        1.1      matt #endif
    261        1.1      matt 		rebalance = false;
    262        1.1      matt 	} else {
    263        1.1      matt 		KASSERT(position == RB_DIR_LEFT || position == RB_DIR_RIGHT);
    264        1.1      matt #ifndef RBSMALL
    265        1.1      matt 		/*
    266        1.1      matt 		 * Keep track of the minimum and maximum nodes.  If our
    267        1.1      matt 		 * parent is a minmax node and we on their min/max side,
    268        1.1      matt 		 * we must be the new min/max node.
    269        1.1      matt 		 */
    270        1.1      matt 		if (parent == rbt->rbt_minmax[position])
    271        1.1      matt 			rbt->rbt_minmax[position] = self;
    272        1.1      matt #endif /* !RBSMALL */
    273        1.1      matt 		/*
    274        1.1      matt 		 * All new nodes are colored red.  We only need to rebalance
    275        1.1      matt 		 * if our parent is also red.
    276        1.1      matt 		 */
    277        1.1      matt 		RB_MARK_RED(self);
    278        1.1      matt 		rebalance = RB_RED_P(parent);
    279        1.1      matt 	}
    280        1.1      matt 	KASSERT(RB_SENTINEL_P(parent->rb_nodes[position]));
    281        1.1      matt 	self->rb_left = parent->rb_nodes[position];
    282        1.1      matt 	self->rb_right = parent->rb_nodes[position];
    283        1.1      matt 	parent->rb_nodes[position] = self;
    284        1.1      matt 	KASSERT(RB_CHILDLESS_P(self));
    285        1.1      matt 
    286        1.1      matt 	/*
    287        1.1      matt 	 * Insert the new node into a sorted list for easy sequential access
    288        1.1      matt 	 */
    289        1.1      matt 	RBSTAT_INC(rbt->rbt_count);
    290        1.1      matt #ifdef RBDEBUG
    291        1.1      matt 	if (RB_ROOT_P(rbt, self)) {
    292        1.1      matt 		RB_TAILQ_INSERT_HEAD(&rbt->rbt_nodes, self, rb_link);
    293        1.1      matt 	} else if (position == RB_DIR_LEFT) {
    294        1.7     rmind 		KASSERT((*compare_nodes)(rbto->rbto_context,
    295        1.7     rmind 		    RB_NODETOITEM(rbto, self),
    296        1.7     rmind 		    RB_NODETOITEM(rbto, RB_FATHER(self))) < 0);
    297        1.1      matt 		RB_TAILQ_INSERT_BEFORE(RB_FATHER(self), self, rb_link);
    298        1.1      matt 	} else {
    299        1.7     rmind 		KASSERT((*compare_nodes)(rbto->rbto_context,
    300        1.7     rmind 		    RB_NODETOITEM(rbto, RB_FATHER(self)),
    301        1.7     rmind 		    RB_NODETOITEM(rbto, self)) < 0);
    302        1.1      matt 		RB_TAILQ_INSERT_AFTER(&rbt->rbt_nodes, RB_FATHER(self),
    303        1.1      matt 		    self, rb_link);
    304        1.1      matt 	}
    305        1.1      matt #endif
    306        1.1      matt 	KASSERT(rb_tree_check_node(rbt, self, NULL, !rebalance));
    307        1.1      matt 
    308        1.1      matt 	/*
    309        1.1      matt 	 * Rebalance tree after insertion
    310        1.1      matt 	 */
    311        1.1      matt 	if (rebalance) {
    312        1.1      matt 		rb_tree_insert_rebalance(rbt, self);
    313        1.1      matt 		KASSERT(rb_tree_check_node(rbt, self, NULL, true));
    314        1.1      matt 	}
    315        1.1      matt 
    316        1.7     rmind 	/* Succesfully inserted, return our node pointer. */
    317        1.7     rmind 	return object;
    318        1.1      matt }
    319        1.7     rmind 
    320        1.1      matt /*
    321        1.1      matt  * Swap the location and colors of 'self' and its child @ which.  The child
    322        1.1      matt  * can not be a sentinel node.  This is our rotation function.  However,
    323        1.1      matt  * since it preserves coloring, it great simplifies both insertion and
    324        1.1      matt  * removal since rotation almost always involves the exchanging of colors
    325        1.1      matt  * as a separate step.
    326        1.1      matt  */
    327        1.1      matt static void
    328  1.13.16.1  christos rb_tree_reparent_nodes(__rbt_unused struct rb_tree *rbt,
    329  1.13.16.1  christos 	struct rb_node *old_father, const unsigned int which)
    330        1.1      matt {
    331        1.1      matt 	const unsigned int other = which ^ RB_DIR_OTHER;
    332        1.1      matt 	struct rb_node * const grandpa = RB_FATHER(old_father);
    333        1.1      matt 	struct rb_node * const old_child = old_father->rb_nodes[which];
    334        1.1      matt 	struct rb_node * const new_father = old_child;
    335        1.1      matt 	struct rb_node * const new_child = old_father;
    336        1.1      matt 
    337        1.1      matt 	KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT);
    338        1.1      matt 
    339        1.1      matt 	KASSERT(!RB_SENTINEL_P(old_child));
    340        1.1      matt 	KASSERT(RB_FATHER(old_child) == old_father);
    341        1.1      matt 
    342        1.1      matt 	KASSERT(rb_tree_check_node(rbt, old_father, NULL, false));
    343        1.1      matt 	KASSERT(rb_tree_check_node(rbt, old_child, NULL, false));
    344        1.7     rmind 	KASSERT(RB_ROOT_P(rbt, old_father) ||
    345        1.7     rmind 	    rb_tree_check_node(rbt, grandpa, NULL, false));
    346        1.1      matt 
    347        1.1      matt 	/*
    348        1.1      matt 	 * Exchange descendant linkages.
    349        1.1      matt 	 */
    350        1.1      matt 	grandpa->rb_nodes[RB_POSITION(old_father)] = new_father;
    351        1.1      matt 	new_child->rb_nodes[which] = old_child->rb_nodes[other];
    352        1.1      matt 	new_father->rb_nodes[other] = new_child;
    353        1.1      matt 
    354        1.1      matt 	/*
    355        1.1      matt 	 * Update ancestor linkages
    356        1.1      matt 	 */
    357        1.1      matt 	RB_SET_FATHER(new_father, grandpa);
    358        1.1      matt 	RB_SET_FATHER(new_child, new_father);
    359        1.1      matt 
    360        1.1      matt 	/*
    361        1.1      matt 	 * Exchange properties between new_father and new_child.  The only
    362        1.1      matt 	 * change is that new_child's position is now on the other side.
    363        1.1      matt 	 */
    364        1.1      matt #if 0
    365        1.1      matt 	{
    366        1.1      matt 		struct rb_node tmp;
    367        1.1      matt 		tmp.rb_info = 0;
    368        1.1      matt 		RB_COPY_PROPERTIES(&tmp, old_child);
    369        1.1      matt 		RB_COPY_PROPERTIES(new_father, old_father);
    370        1.1      matt 		RB_COPY_PROPERTIES(new_child, &tmp);
    371        1.1      matt 	}
    372        1.1      matt #else
    373        1.1      matt 	RB_SWAP_PROPERTIES(new_father, new_child);
    374        1.1      matt #endif
    375        1.1      matt 	RB_SET_POSITION(new_child, other);
    376        1.1      matt 
    377        1.1      matt 	/*
    378        1.1      matt 	 * Make sure to reparent the new child to ourself.
    379        1.1      matt 	 */
    380        1.1      matt 	if (!RB_SENTINEL_P(new_child->rb_nodes[which])) {
    381        1.1      matt 		RB_SET_FATHER(new_child->rb_nodes[which], new_child);
    382        1.1      matt 		RB_SET_POSITION(new_child->rb_nodes[which], which);
    383        1.1      matt 	}
    384        1.1      matt 
    385        1.1      matt 	KASSERT(rb_tree_check_node(rbt, new_father, NULL, false));
    386        1.1      matt 	KASSERT(rb_tree_check_node(rbt, new_child, NULL, false));
    387        1.7     rmind 	KASSERT(RB_ROOT_P(rbt, new_father) ||
    388        1.7     rmind 	    rb_tree_check_node(rbt, grandpa, NULL, false));
    389        1.1      matt }
    390        1.7     rmind 
    391        1.1      matt static void
    392        1.1      matt rb_tree_insert_rebalance(struct rb_tree *rbt, struct rb_node *self)
    393        1.1      matt {
    394        1.1      matt 	struct rb_node * father = RB_FATHER(self);
    395        1.1      matt 	struct rb_node * grandpa = RB_FATHER(father);
    396        1.1      matt 	struct rb_node * uncle;
    397        1.1      matt 	unsigned int which;
    398        1.1      matt 	unsigned int other;
    399        1.1      matt 
    400        1.1      matt 	KASSERT(!RB_ROOT_P(rbt, self));
    401        1.1      matt 	KASSERT(RB_RED_P(self));
    402        1.1      matt 	KASSERT(RB_RED_P(father));
    403        1.1      matt 	RBSTAT_INC(rbt->rbt_insertion_rebalance_calls);
    404        1.1      matt 
    405        1.1      matt 	for (;;) {
    406        1.1      matt 		KASSERT(!RB_SENTINEL_P(self));
    407        1.1      matt 
    408        1.1      matt 		KASSERT(RB_RED_P(self));
    409        1.1      matt 		KASSERT(RB_RED_P(father));
    410        1.1      matt 		/*
    411        1.1      matt 		 * We are red and our parent is red, therefore we must have a
    412        1.1      matt 		 * grandfather and he must be black.
    413        1.1      matt 		 */
    414        1.1      matt 		grandpa = RB_FATHER(father);
    415        1.1      matt 		KASSERT(RB_BLACK_P(grandpa));
    416        1.1      matt 		KASSERT(RB_DIR_RIGHT == 1 && RB_DIR_LEFT == 0);
    417        1.1      matt 		which = (father == grandpa->rb_right);
    418        1.1      matt 		other = which ^ RB_DIR_OTHER;
    419        1.1      matt 		uncle = grandpa->rb_nodes[other];
    420        1.1      matt 
    421        1.1      matt 		if (RB_BLACK_P(uncle))
    422        1.1      matt 			break;
    423        1.1      matt 
    424        1.1      matt 		RBSTAT_INC(rbt->rbt_insertion_rebalance_passes);
    425        1.1      matt 		/*
    426        1.1      matt 		 * Case 1: our uncle is red
    427        1.1      matt 		 *   Simply invert the colors of our parent and
    428        1.1      matt 		 *   uncle and make our grandparent red.  And
    429        1.1      matt 		 *   then solve the problem up at his level.
    430        1.1      matt 		 */
    431        1.1      matt 		RB_MARK_BLACK(uncle);
    432        1.1      matt 		RB_MARK_BLACK(father);
    433        1.1      matt 		if (__predict_false(RB_ROOT_P(rbt, grandpa))) {
    434        1.1      matt 			/*
    435        1.1      matt 			 * If our grandpa is root, don't bother
    436        1.1      matt 			 * setting him to red, just return.
    437        1.1      matt 			 */
    438        1.1      matt 			KASSERT(RB_BLACK_P(grandpa));
    439        1.1      matt 			return;
    440        1.1      matt 		}
    441        1.1      matt 		RB_MARK_RED(grandpa);
    442        1.1      matt 		self = grandpa;
    443        1.1      matt 		father = RB_FATHER(self);
    444        1.1      matt 		KASSERT(RB_RED_P(self));
    445        1.1      matt 		if (RB_BLACK_P(father)) {
    446        1.1      matt 			/*
    447        1.1      matt 			 * If our greatgrandpa is black, we're done.
    448        1.1      matt 			 */
    449        1.1      matt 			KASSERT(RB_BLACK_P(rbt->rbt_root));
    450        1.1      matt 			return;
    451        1.1      matt 		}
    452        1.1      matt 	}
    453        1.1      matt 
    454        1.1      matt 	KASSERT(!RB_ROOT_P(rbt, self));
    455        1.1      matt 	KASSERT(RB_RED_P(self));
    456        1.1      matt 	KASSERT(RB_RED_P(father));
    457        1.1      matt 	KASSERT(RB_BLACK_P(uncle));
    458        1.1      matt 	KASSERT(RB_BLACK_P(grandpa));
    459        1.1      matt 	/*
    460        1.1      matt 	 * Case 2&3: our uncle is black.
    461        1.1      matt 	 */
    462        1.1      matt 	if (self == father->rb_nodes[other]) {
    463        1.1      matt 		/*
    464        1.1      matt 		 * Case 2: we are on the same side as our uncle
    465        1.1      matt 		 *   Swap ourselves with our parent so this case
    466        1.1      matt 		 *   becomes case 3.  Basically our parent becomes our
    467        1.1      matt 		 *   child.
    468        1.1      matt 		 */
    469        1.1      matt 		rb_tree_reparent_nodes(rbt, father, other);
    470        1.1      matt 		KASSERT(RB_FATHER(father) == self);
    471        1.1      matt 		KASSERT(self->rb_nodes[which] == father);
    472        1.1      matt 		KASSERT(RB_FATHER(self) == grandpa);
    473        1.1      matt 		self = father;
    474        1.1      matt 		father = RB_FATHER(self);
    475        1.1      matt 	}
    476        1.1      matt 	KASSERT(RB_RED_P(self) && RB_RED_P(father));
    477        1.1      matt 	KASSERT(grandpa->rb_nodes[which] == father);
    478        1.1      matt 	/*
    479        1.1      matt 	 * Case 3: we are opposite a child of a black uncle.
    480        1.1      matt 	 *   Swap our parent and grandparent.  Since our grandfather
    481        1.1      matt 	 *   is black, our father will become black and our new sibling
    482        1.1      matt 	 *   (former grandparent) will become red.
    483        1.1      matt 	 */
    484        1.1      matt 	rb_tree_reparent_nodes(rbt, grandpa, which);
    485        1.1      matt 	KASSERT(RB_FATHER(self) == father);
    486        1.1      matt 	KASSERT(RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER] == grandpa);
    487        1.1      matt 	KASSERT(RB_RED_P(self));
    488        1.1      matt 	KASSERT(RB_BLACK_P(father));
    489        1.1      matt 	KASSERT(RB_RED_P(grandpa));
    490        1.1      matt 
    491        1.1      matt 	/*
    492        1.1      matt 	 * Final step: Set the root to black.
    493        1.1      matt 	 */
    494        1.1      matt 	RB_MARK_BLACK(rbt->rbt_root);
    495        1.1      matt }
    496        1.7     rmind 
    497        1.1      matt static void
    498        1.1      matt rb_tree_prune_node(struct rb_tree *rbt, struct rb_node *self, bool rebalance)
    499        1.1      matt {
    500        1.1      matt 	const unsigned int which = RB_POSITION(self);
    501        1.1      matt 	struct rb_node *father = RB_FATHER(self);
    502        1.5     joerg #ifndef RBSMALL
    503        1.1      matt 	const bool was_root = RB_ROOT_P(rbt, self);
    504        1.5     joerg #endif
    505        1.1      matt 
    506        1.1      matt 	KASSERT(rebalance || (RB_ROOT_P(rbt, self) || RB_RED_P(self)));
    507        1.1      matt 	KASSERT(!rebalance || RB_BLACK_P(self));
    508        1.1      matt 	KASSERT(RB_CHILDLESS_P(self));
    509        1.1      matt 	KASSERT(rb_tree_check_node(rbt, self, NULL, false));
    510        1.1      matt 
    511        1.1      matt 	/*
    512        1.1      matt 	 * Since we are childless, we know that self->rb_left is pointing
    513        1.1      matt 	 * to the sentinel node.
    514        1.1      matt 	 */
    515        1.1      matt 	father->rb_nodes[which] = self->rb_left;
    516        1.1      matt 
    517        1.1      matt 	/*
    518        1.1      matt 	 * Remove ourselves from the node list, decrement the count,
    519        1.1      matt 	 * and update min/max.
    520        1.1      matt 	 */
    521        1.1      matt 	RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
    522        1.1      matt 	RBSTAT_DEC(rbt->rbt_count);
    523        1.1      matt #ifndef RBSMALL
    524        1.1      matt 	if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self)) {
    525        1.1      matt 		rbt->rbt_minmax[RB_POSITION(self)] = father;
    526        1.1      matt 		/*
    527        1.1      matt 		 * When removing the root, rbt->rbt_minmax[RB_DIR_LEFT] is
    528        1.1      matt 		 * updated automatically, but we also need to update
    529        1.1      matt 		 * rbt->rbt_minmax[RB_DIR_RIGHT];
    530        1.1      matt 		 */
    531        1.1      matt 		if (__predict_false(was_root)) {
    532        1.1      matt 			rbt->rbt_minmax[RB_DIR_RIGHT] = father;
    533        1.1      matt 		}
    534        1.1      matt 	}
    535        1.1      matt 	RB_SET_FATHER(self, NULL);
    536        1.1      matt #endif
    537        1.1      matt 
    538        1.1      matt 	/*
    539        1.1      matt 	 * Rebalance if requested.
    540        1.1      matt 	 */
    541        1.1      matt 	if (rebalance)
    542        1.1      matt 		rb_tree_removal_rebalance(rbt, father, which);
    543        1.1      matt 	KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true));
    544        1.1      matt }
    545        1.7     rmind 
    546        1.1      matt /*
    547        1.1      matt  * When deleting an interior node
    548        1.1      matt  */
    549        1.1      matt static void
    550        1.1      matt rb_tree_swap_prune_and_rebalance(struct rb_tree *rbt, struct rb_node *self,
    551        1.1      matt 	struct rb_node *standin)
    552        1.1      matt {
    553        1.1      matt 	const unsigned int standin_which = RB_POSITION(standin);
    554        1.1      matt 	unsigned int standin_other = standin_which ^ RB_DIR_OTHER;
    555        1.1      matt 	struct rb_node *standin_son;
    556        1.1      matt 	struct rb_node *standin_father = RB_FATHER(standin);
    557        1.1      matt 	bool rebalance = RB_BLACK_P(standin);
    558        1.1      matt 
    559        1.1      matt 	if (standin_father == self) {
    560        1.1      matt 		/*
    561        1.1      matt 		 * As a child of self, any childen would be opposite of
    562        1.1      matt 		 * our parent.
    563        1.1      matt 		 */
    564        1.1      matt 		KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other]));
    565        1.1      matt 		standin_son = standin->rb_nodes[standin_which];
    566        1.1      matt 	} else {
    567        1.1      matt 		/*
    568        1.1      matt 		 * Since we aren't a child of self, any childen would be
    569        1.1      matt 		 * on the same side as our parent.
    570        1.1      matt 		 */
    571        1.1      matt 		KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_which]));
    572        1.1      matt 		standin_son = standin->rb_nodes[standin_other];
    573        1.1      matt 	}
    574        1.1      matt 
    575        1.1      matt 	/*
    576        1.1      matt 	 * the node we are removing must have two children.
    577        1.1      matt 	 */
    578        1.1      matt 	KASSERT(RB_TWOCHILDREN_P(self));
    579        1.1      matt 	/*
    580        1.1      matt 	 * If standin has a child, it must be red.
    581        1.1      matt 	 */
    582        1.1      matt 	KASSERT(RB_SENTINEL_P(standin_son) || RB_RED_P(standin_son));
    583        1.1      matt 
    584        1.1      matt 	/*
    585        1.1      matt 	 * Verify things are sane.
    586        1.1      matt 	 */
    587        1.1      matt 	KASSERT(rb_tree_check_node(rbt, self, NULL, false));
    588        1.1      matt 	KASSERT(rb_tree_check_node(rbt, standin, NULL, false));
    589        1.1      matt 
    590        1.1      matt 	if (__predict_false(RB_RED_P(standin_son))) {
    591        1.1      matt 		/*
    592        1.1      matt 		 * We know we have a red child so if we flip it to black
    593        1.1      matt 		 * we don't have to rebalance.
    594        1.1      matt 		 */
    595        1.1      matt 		KASSERT(rb_tree_check_node(rbt, standin_son, NULL, true));
    596        1.1      matt 		RB_MARK_BLACK(standin_son);
    597        1.1      matt 		rebalance = false;
    598        1.1      matt 
    599        1.1      matt 		if (standin_father == self) {
    600        1.1      matt 			KASSERT(RB_POSITION(standin_son) == standin_which);
    601        1.1      matt 		} else {
    602        1.1      matt 			KASSERT(RB_POSITION(standin_son) == standin_other);
    603        1.1      matt 			/*
    604        1.1      matt 			 * Change the son's parentage to point to his grandpa.
    605        1.1      matt 			 */
    606        1.1      matt 			RB_SET_FATHER(standin_son, standin_father);
    607        1.1      matt 			RB_SET_POSITION(standin_son, standin_which);
    608        1.1      matt 		}
    609        1.1      matt 	}
    610        1.1      matt 
    611        1.1      matt 	if (standin_father == self) {
    612        1.1      matt 		/*
    613        1.1      matt 		 * If we are about to delete the standin's father, then when
    614        1.1      matt 		 * we call rebalance, we need to use ourselves as our father.
    615        1.1      matt 		 * Otherwise remember our original father.  Also, sincef we are
    616        1.1      matt 		 * our standin's father we only need to reparent the standin's
    617        1.1      matt 		 * brother.
    618        1.1      matt 		 *
    619        1.1      matt 		 * |    R      -->     S    |
    620        1.1      matt 		 * |  Q   S    -->   Q   T  |
    621        1.1      matt 		 * |        t  -->          |
    622        1.1      matt 		 */
    623        1.1      matt 		KASSERT(RB_SENTINEL_P(standin->rb_nodes[standin_other]));
    624        1.1      matt 		KASSERT(!RB_SENTINEL_P(self->rb_nodes[standin_other]));
    625        1.1      matt 		KASSERT(self->rb_nodes[standin_which] == standin);
    626        1.1      matt 		/*
    627        1.1      matt 		 * Have our son/standin adopt his brother as his new son.
    628        1.1      matt 		 */
    629        1.1      matt 		standin_father = standin;
    630        1.1      matt 	} else {
    631        1.1      matt 		/*
    632        1.1      matt 		 * |    R          -->    S       .  |
    633        1.1      matt 		 * |   / \  |   T  -->   / \  |  /   |
    634        1.1      matt 		 * |  ..... | S    -->  ..... | T    |
    635        1.1      matt 		 *
    636        1.1      matt 		 * Sever standin's connection to his father.
    637        1.1      matt 		 */
    638        1.1      matt 		standin_father->rb_nodes[standin_which] = standin_son;
    639        1.1      matt 		/*
    640        1.1      matt 		 * Adopt the far son.
    641        1.1      matt 		 */
    642        1.1      matt 		standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
    643        1.1      matt 		RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
    644        1.1      matt 		KASSERT(RB_POSITION(self->rb_nodes[standin_other]) == standin_other);
    645        1.1      matt 		/*
    646        1.1      matt 		 * Use standin_other because we need to preserve standin_which
    647        1.1      matt 		 * for the removal_rebalance.
    648        1.1      matt 		 */
    649        1.1      matt 		standin_other = standin_which;
    650        1.1      matt 	}
    651        1.1      matt 
    652        1.1      matt 	/*
    653        1.1      matt 	 * Move the only remaining son to our standin.  If our standin is our
    654        1.1      matt 	 * son, this will be the only son needed to be moved.
    655        1.1      matt 	 */
    656        1.1      matt 	KASSERT(standin->rb_nodes[standin_other] != self->rb_nodes[standin_other]);
    657        1.1      matt 	standin->rb_nodes[standin_other] = self->rb_nodes[standin_other];
    658        1.1      matt 	RB_SET_FATHER(standin->rb_nodes[standin_other], standin);
    659        1.1      matt 
    660        1.1      matt 	/*
    661        1.1      matt 	 * Now copy the result of self to standin and then replace
    662        1.1      matt 	 * self with standin in the tree.
    663        1.1      matt 	 */
    664        1.1      matt 	RB_COPY_PROPERTIES(standin, self);
    665        1.1      matt 	RB_SET_FATHER(standin, RB_FATHER(self));
    666        1.1      matt 	RB_FATHER(standin)->rb_nodes[RB_POSITION(standin)] = standin;
    667        1.1      matt 
    668        1.1      matt 	/*
    669        1.1      matt 	 * Remove ourselves from the node list, decrement the count,
    670        1.1      matt 	 * and update min/max.
    671        1.1      matt 	 */
    672        1.1      matt 	RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
    673        1.1      matt 	RBSTAT_DEC(rbt->rbt_count);
    674        1.1      matt #ifndef RBSMALL
    675        1.1      matt 	if (__predict_false(rbt->rbt_minmax[RB_POSITION(self)] == self))
    676        1.1      matt 		rbt->rbt_minmax[RB_POSITION(self)] = RB_FATHER(self);
    677        1.1      matt 	RB_SET_FATHER(self, NULL);
    678        1.1      matt #endif
    679        1.1      matt 
    680        1.1      matt 	KASSERT(rb_tree_check_node(rbt, standin, NULL, false));
    681        1.1      matt 	KASSERT(RB_FATHER_SENTINEL_P(standin)
    682        1.1      matt 		|| rb_tree_check_node(rbt, standin_father, NULL, false));
    683        1.1      matt 	KASSERT(RB_LEFT_SENTINEL_P(standin)
    684        1.1      matt 		|| rb_tree_check_node(rbt, standin->rb_left, NULL, false));
    685        1.1      matt 	KASSERT(RB_RIGHT_SENTINEL_P(standin)
    686        1.1      matt 		|| rb_tree_check_node(rbt, standin->rb_right, NULL, false));
    687        1.1      matt 
    688        1.1      matt 	if (!rebalance)
    689        1.1      matt 		return;
    690        1.1      matt 
    691        1.1      matt 	rb_tree_removal_rebalance(rbt, standin_father, standin_which);
    692        1.1      matt 	KASSERT(rb_tree_check_node(rbt, standin, NULL, true));
    693        1.1      matt }
    694        1.1      matt 
    695        1.1      matt /*
    696        1.1      matt  * We could do this by doing
    697        1.1      matt  *	rb_tree_node_swap(rbt, self, which);
    698        1.1      matt  *	rb_tree_prune_node(rbt, self, false);
    699        1.1      matt  *
    700        1.1      matt  * But it's more efficient to just evalate and recolor the child.
    701        1.1      matt  */
    702        1.1      matt static void
    703        1.1      matt rb_tree_prune_blackred_branch(struct rb_tree *rbt, struct rb_node *self,
    704        1.1      matt 	unsigned int which)
    705        1.1      matt {
    706        1.1      matt 	struct rb_node *father = RB_FATHER(self);
    707        1.1      matt 	struct rb_node *son = self->rb_nodes[which];
    708        1.5     joerg #ifndef RBSMALL
    709        1.1      matt 	const bool was_root = RB_ROOT_P(rbt, self);
    710        1.5     joerg #endif
    711        1.1      matt 
    712        1.1      matt 	KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT);
    713        1.1      matt 	KASSERT(RB_BLACK_P(self) && RB_RED_P(son));
    714        1.1      matt 	KASSERT(!RB_TWOCHILDREN_P(son));
    715        1.1      matt 	KASSERT(RB_CHILDLESS_P(son));
    716        1.1      matt 	KASSERT(rb_tree_check_node(rbt, self, NULL, false));
    717        1.1      matt 	KASSERT(rb_tree_check_node(rbt, son, NULL, false));
    718        1.1      matt 
    719        1.1      matt 	/*
    720        1.1      matt 	 * Remove ourselves from the tree and give our former child our
    721        1.1      matt 	 * properties (position, color, root).
    722        1.1      matt 	 */
    723        1.1      matt 	RB_COPY_PROPERTIES(son, self);
    724        1.1      matt 	father->rb_nodes[RB_POSITION(son)] = son;
    725        1.1      matt 	RB_SET_FATHER(son, father);
    726        1.1      matt 
    727        1.1      matt 	/*
    728        1.1      matt 	 * Remove ourselves from the node list, decrement the count,
    729        1.1      matt 	 * and update minmax.
    730        1.1      matt 	 */
    731        1.1      matt 	RB_TAILQ_REMOVE(&rbt->rbt_nodes, self, rb_link);
    732        1.1      matt 	RBSTAT_DEC(rbt->rbt_count);
    733        1.1      matt #ifndef RBSMALL
    734        1.1      matt 	if (__predict_false(was_root)) {
    735        1.1      matt 		KASSERT(rbt->rbt_minmax[which] == son);
    736        1.1      matt 		rbt->rbt_minmax[which ^ RB_DIR_OTHER] = son;
    737        1.1      matt 	} else if (rbt->rbt_minmax[RB_POSITION(self)] == self) {
    738        1.1      matt 		rbt->rbt_minmax[RB_POSITION(self)] = son;
    739        1.1      matt 	}
    740        1.1      matt 	RB_SET_FATHER(self, NULL);
    741        1.1      matt #endif
    742        1.1      matt 
    743        1.1      matt 	KASSERT(was_root || rb_tree_check_node(rbt, father, NULL, true));
    744        1.1      matt 	KASSERT(rb_tree_check_node(rbt, son, NULL, true));
    745        1.1      matt }
    746        1.7     rmind 
    747        1.1      matt void
    748        1.7     rmind rb_tree_remove_node(struct rb_tree *rbt, void *object)
    749        1.1      matt {
    750        1.7     rmind 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    751        1.7     rmind 	struct rb_node *standin, *self = RB_ITEMTONODE(rbto, object);
    752        1.1      matt 	unsigned int which;
    753        1.1      matt 
    754        1.1      matt 	KASSERT(!RB_SENTINEL_P(self));
    755        1.1      matt 	RBSTAT_INC(rbt->rbt_removals);
    756        1.1      matt 
    757        1.1      matt 	/*
    758        1.1      matt 	 * In the following diagrams, we (the node to be removed) are S.  Red
    759        1.1      matt 	 * nodes are lowercase.  T could be either red or black.
    760        1.1      matt 	 *
    761        1.1      matt 	 * Remember the major axiom of the red-black tree: the number of
    762        1.1      matt 	 * black nodes from the root to each leaf is constant across all
    763        1.1      matt 	 * leaves, only the number of red nodes varies.
    764        1.1      matt 	 *
    765        1.1      matt 	 * Thus removing a red leaf doesn't require any other changes to a
    766        1.1      matt 	 * red-black tree.  So if we must remove a node, attempt to rearrange
    767        1.1      matt 	 * the tree so we can remove a red node.
    768        1.1      matt 	 *
    769        1.1      matt 	 * The simpliest case is a childless red node or a childless root node:
    770        1.1      matt 	 *
    771        1.1      matt 	 * |    T  -->    T  |    or    |  R  -->  *  |
    772        1.1      matt 	 * |  s    -->  *    |
    773        1.1      matt 	 */
    774        1.1      matt 	if (RB_CHILDLESS_P(self)) {
    775        1.1      matt 		const bool rebalance = RB_BLACK_P(self) && !RB_ROOT_P(rbt, self);
    776        1.1      matt 		rb_tree_prune_node(rbt, self, rebalance);
    777        1.1      matt 		return;
    778        1.1      matt 	}
    779        1.1      matt 	KASSERT(!RB_CHILDLESS_P(self));
    780        1.1      matt 	if (!RB_TWOCHILDREN_P(self)) {
    781        1.1      matt 		/*
    782        1.1      matt 		 * The next simpliest case is the node we are deleting is
    783        1.1      matt 		 * black and has one red child.
    784        1.1      matt 		 *
    785        1.1      matt 		 * |      T  -->      T  -->      T  |
    786        1.1      matt 		 * |    S    -->  R      -->  R      |
    787        1.1      matt 		 * |  r      -->    s    -->    *    |
    788        1.1      matt 		 */
    789        1.1      matt 		which = RB_LEFT_SENTINEL_P(self) ? RB_DIR_RIGHT : RB_DIR_LEFT;
    790        1.1      matt 		KASSERT(RB_BLACK_P(self));
    791        1.1      matt 		KASSERT(RB_RED_P(self->rb_nodes[which]));
    792        1.1      matt 		KASSERT(RB_CHILDLESS_P(self->rb_nodes[which]));
    793        1.1      matt 		rb_tree_prune_blackred_branch(rbt, self, which);
    794        1.1      matt 		return;
    795        1.1      matt 	}
    796        1.1      matt 	KASSERT(RB_TWOCHILDREN_P(self));
    797        1.1      matt 
    798        1.1      matt 	/*
    799        1.1      matt 	 * We invert these because we prefer to remove from the inside of
    800        1.1      matt 	 * the tree.
    801        1.1      matt 	 */
    802        1.1      matt 	which = RB_POSITION(self) ^ RB_DIR_OTHER;
    803        1.1      matt 
    804        1.1      matt 	/*
    805        1.1      matt 	 * Let's find the node closes to us opposite of our parent
    806        1.1      matt 	 * Now swap it with ourself, "prune" it, and rebalance, if needed.
    807        1.1      matt 	 */
    808        1.7     rmind 	standin = RB_ITEMTONODE(rbto, rb_tree_iterate(rbt, object, which));
    809        1.1      matt 	rb_tree_swap_prune_and_rebalance(rbt, self, standin);
    810        1.1      matt }
    811        1.1      matt 
    812        1.1      matt static void
    813        1.1      matt rb_tree_removal_rebalance(struct rb_tree *rbt, struct rb_node *parent,
    814        1.1      matt 	unsigned int which)
    815        1.1      matt {
    816        1.1      matt 	KASSERT(!RB_SENTINEL_P(parent));
    817        1.1      matt 	KASSERT(RB_SENTINEL_P(parent->rb_nodes[which]));
    818        1.1      matt 	KASSERT(which == RB_DIR_LEFT || which == RB_DIR_RIGHT);
    819        1.1      matt 	RBSTAT_INC(rbt->rbt_removal_rebalance_calls);
    820        1.1      matt 
    821        1.1      matt 	while (RB_BLACK_P(parent->rb_nodes[which])) {
    822        1.1      matt 		unsigned int other = which ^ RB_DIR_OTHER;
    823        1.1      matt 		struct rb_node *brother = parent->rb_nodes[other];
    824        1.1      matt 
    825        1.1      matt 		RBSTAT_INC(rbt->rbt_removal_rebalance_passes);
    826        1.1      matt 
    827        1.1      matt 		KASSERT(!RB_SENTINEL_P(brother));
    828        1.1      matt 		/*
    829        1.1      matt 		 * For cases 1, 2a, and 2b, our brother's children must
    830        1.1      matt 		 * be black and our father must be black
    831        1.1      matt 		 */
    832        1.1      matt 		if (RB_BLACK_P(parent)
    833        1.1      matt 		    && RB_BLACK_P(brother->rb_left)
    834        1.1      matt 		    && RB_BLACK_P(brother->rb_right)) {
    835        1.1      matt 			if (RB_RED_P(brother)) {
    836        1.1      matt 				/*
    837        1.1      matt 				 * Case 1: Our brother is red, swap its
    838        1.1      matt 				 * position (and colors) with our parent.
    839        1.1      matt 				 * This should now be case 2b (unless C or E
    840        1.1      matt 				 * has a red child which is case 3; thus no
    841        1.1      matt 				 * explicit branch to case 2b).
    842        1.1      matt 				 *
    843        1.1      matt 				 *    B         ->        D
    844        1.1      matt 				 *  A     d     ->    b     E
    845        1.1      matt 				 *      C   E   ->  A   C
    846        1.1      matt 				 */
    847        1.1      matt 				KASSERT(RB_BLACK_P(parent));
    848        1.1      matt 				rb_tree_reparent_nodes(rbt, parent, other);
    849        1.1      matt 				brother = parent->rb_nodes[other];
    850        1.1      matt 				KASSERT(!RB_SENTINEL_P(brother));
    851        1.1      matt 				KASSERT(RB_RED_P(parent));
    852        1.1      matt 				KASSERT(RB_BLACK_P(brother));
    853        1.1      matt 				KASSERT(rb_tree_check_node(rbt, brother, NULL, false));
    854        1.1      matt 				KASSERT(rb_tree_check_node(rbt, parent, NULL, false));
    855        1.1      matt 			} else {
    856        1.1      matt 				/*
    857        1.1      matt 				 * Both our parent and brother are black.
    858        1.1      matt 				 * Change our brother to red, advance up rank
    859        1.1      matt 				 * and go through the loop again.
    860        1.1      matt 				 *
    861        1.1      matt 				 *    B         ->   *B
    862        1.1      matt 				 * *A     D     ->  A     d
    863        1.1      matt 				 *      C   E   ->      C   E
    864        1.1      matt 				 */
    865        1.1      matt 				RB_MARK_RED(brother);
    866        1.1      matt 				KASSERT(RB_BLACK_P(brother->rb_left));
    867        1.1      matt 				KASSERT(RB_BLACK_P(brother->rb_right));
    868        1.1      matt 				if (RB_ROOT_P(rbt, parent))
    869        1.1      matt 					return;	/* root == parent == black */
    870        1.1      matt 				KASSERT(rb_tree_check_node(rbt, brother, NULL, false));
    871        1.1      matt 				KASSERT(rb_tree_check_node(rbt, parent, NULL, false));
    872        1.1      matt 				which = RB_POSITION(parent);
    873        1.1      matt 				parent = RB_FATHER(parent);
    874        1.1      matt 				continue;
    875        1.1      matt 			}
    876        1.1      matt 		}
    877        1.1      matt 		/*
    878        1.1      matt 		 * Avoid an else here so that case 2a above can hit either
    879        1.1      matt 		 * case 2b, 3, or 4.
    880        1.1      matt 		 */
    881        1.1      matt 		if (RB_RED_P(parent)
    882        1.1      matt 		    && RB_BLACK_P(brother)
    883        1.1      matt 		    && RB_BLACK_P(brother->rb_left)
    884        1.1      matt 		    && RB_BLACK_P(brother->rb_right)) {
    885        1.1      matt 			KASSERT(RB_RED_P(parent));
    886        1.1      matt 			KASSERT(RB_BLACK_P(brother));
    887        1.1      matt 			KASSERT(RB_BLACK_P(brother->rb_left));
    888        1.1      matt 			KASSERT(RB_BLACK_P(brother->rb_right));
    889        1.1      matt 			/*
    890        1.1      matt 			 * We are black, our father is red, our brother and
    891        1.1      matt 			 * both nephews are black.  Simply invert/exchange the
    892        1.1      matt 			 * colors of our father and brother (to black and red
    893        1.1      matt 			 * respectively).
    894        1.1      matt 			 *
    895        1.1      matt 			 *	|    f        -->    F        |
    896        1.1      matt 			 *	|  *     B    -->  *     b    |
    897        1.1      matt 			 *	|      N   N  -->      N   N  |
    898        1.1      matt 			 */
    899        1.1      matt 			RB_MARK_BLACK(parent);
    900        1.1      matt 			RB_MARK_RED(brother);
    901        1.1      matt 			KASSERT(rb_tree_check_node(rbt, brother, NULL, true));
    902        1.1      matt 			break;		/* We're done! */
    903        1.1      matt 		} else {
    904        1.1      matt 			/*
    905        1.1      matt 			 * Our brother must be black and have at least one
    906        1.1      matt 			 * red child (it may have two).
    907        1.1      matt 			 */
    908        1.1      matt 			KASSERT(RB_BLACK_P(brother));
    909        1.1      matt 			KASSERT(RB_RED_P(brother->rb_nodes[which]) ||
    910        1.1      matt 				RB_RED_P(brother->rb_nodes[other]));
    911        1.1      matt 			if (RB_BLACK_P(brother->rb_nodes[other])) {
    912        1.1      matt 				/*
    913        1.1      matt 				 * Case 3: our brother is black, our near
    914        1.1      matt 				 * nephew is red, and our far nephew is black.
    915        1.1      matt 				 * Swap our brother with our near nephew.
    916        1.1      matt 				 * This result in a tree that matches case 4.
    917        1.1      matt 				 * (Our father could be red or black).
    918        1.1      matt 				 *
    919        1.1      matt 				 *	|    F      -->    F      |
    920        1.1      matt 				 *	|  x     B  -->  x   B    |
    921        1.1      matt 				 *	|      n    -->        n  |
    922        1.1      matt 				 */
    923        1.1      matt 				KASSERT(RB_RED_P(brother->rb_nodes[which]));
    924        1.1      matt 				rb_tree_reparent_nodes(rbt, brother, which);
    925        1.1      matt 				KASSERT(RB_FATHER(brother) == parent->rb_nodes[other]);
    926        1.1      matt 				brother = parent->rb_nodes[other];
    927        1.1      matt 				KASSERT(RB_RED_P(brother->rb_nodes[other]));
    928        1.1      matt 			}
    929        1.1      matt 			/*
    930        1.1      matt 			 * Case 4: our brother is black and our far nephew
    931        1.1      matt 			 * is red.  Swap our father and brother locations and
    932        1.1      matt 			 * change our far nephew to black.  (these can be
    933        1.1      matt 			 * done in either order so we change the color first).
    934        1.1      matt 			 * The result is a valid red-black tree and is a
    935        1.1      matt 			 * terminal case.  (again we don't care about the
    936        1.1      matt 			 * father's color)
    937        1.1      matt 			 *
    938        1.1      matt 			 * If the father is red, we will get a red-black-black
    939        1.1      matt 			 * tree:
    940        1.1      matt 			 *	|  f      ->  f      -->    b    |
    941        1.1      matt 			 *	|    B    ->    B    -->  F   N  |
    942        1.1      matt 			 *	|      n  ->      N  -->         |
    943        1.1      matt 			 *
    944        1.1      matt 			 * If the father is black, we will get an all black
    945        1.1      matt 			 * tree:
    946        1.1      matt 			 *	|  F      ->  F      -->    B    |
    947        1.1      matt 			 *	|    B    ->    B    -->  F   N  |
    948        1.1      matt 			 *	|      n  ->      N  -->         |
    949        1.1      matt 			 *
    950        1.1      matt 			 * If we had two red nephews, then after the swap,
    951        1.1      matt 			 * our former father would have a red grandson.
    952        1.1      matt 			 */
    953        1.1      matt 			KASSERT(RB_BLACK_P(brother));
    954        1.1      matt 			KASSERT(RB_RED_P(brother->rb_nodes[other]));
    955        1.1      matt 			RB_MARK_BLACK(brother->rb_nodes[other]);
    956        1.1      matt 			rb_tree_reparent_nodes(rbt, parent, other);
    957        1.1      matt 			break;		/* We're done! */
    958        1.1      matt 		}
    959        1.1      matt 	}
    960        1.1      matt 	KASSERT(rb_tree_check_node(rbt, parent, NULL, true));
    961        1.1      matt }
    962        1.1      matt 
    963        1.7     rmind void *
    964        1.7     rmind rb_tree_iterate(struct rb_tree *rbt, void *object, const unsigned int direction)
    965        1.1      matt {
    966        1.7     rmind 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
    967        1.1      matt 	const unsigned int other = direction ^ RB_DIR_OTHER;
    968        1.7     rmind 	struct rb_node *self;
    969        1.7     rmind 
    970        1.1      matt 	KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT);
    971        1.1      matt 
    972        1.7     rmind 	if (object == NULL) {
    973        1.1      matt #ifndef RBSMALL
    974        1.1      matt 		if (RB_SENTINEL_P(rbt->rbt_root))
    975        1.1      matt 			return NULL;
    976        1.7     rmind 		return RB_NODETOITEM(rbto, rbt->rbt_minmax[direction]);
    977        1.1      matt #else
    978        1.1      matt 		self = rbt->rbt_root;
    979        1.1      matt 		if (RB_SENTINEL_P(self))
    980        1.1      matt 			return NULL;
    981        1.6     joerg 		while (!RB_SENTINEL_P(self->rb_nodes[direction]))
    982        1.6     joerg 			self = self->rb_nodes[direction];
    983        1.7     rmind 		return RB_NODETOITEM(rbto, self);
    984        1.1      matt #endif /* !RBSMALL */
    985        1.1      matt 	}
    986        1.7     rmind 	self = RB_ITEMTONODE(rbto, object);
    987        1.1      matt 	KASSERT(!RB_SENTINEL_P(self));
    988        1.1      matt 	/*
    989        1.1      matt 	 * We can't go any further in this direction.  We proceed up in the
    990        1.1      matt 	 * opposite direction until our parent is in direction we want to go.
    991        1.1      matt 	 */
    992        1.1      matt 	if (RB_SENTINEL_P(self->rb_nodes[direction])) {
    993        1.1      matt 		while (!RB_ROOT_P(rbt, self)) {
    994        1.1      matt 			if (other == RB_POSITION(self))
    995        1.7     rmind 				return RB_NODETOITEM(rbto, RB_FATHER(self));
    996        1.1      matt 			self = RB_FATHER(self);
    997        1.1      matt 		}
    998        1.1      matt 		return NULL;
    999        1.1      matt 	}
   1000        1.1      matt 
   1001        1.1      matt 	/*
   1002        1.1      matt 	 * Advance down one in current direction and go down as far as possible
   1003        1.1      matt 	 * in the opposite direction.
   1004        1.1      matt 	 */
   1005        1.1      matt 	self = self->rb_nodes[direction];
   1006        1.1      matt 	KASSERT(!RB_SENTINEL_P(self));
   1007        1.1      matt 	while (!RB_SENTINEL_P(self->rb_nodes[other]))
   1008        1.1      matt 		self = self->rb_nodes[other];
   1009        1.7     rmind 	return RB_NODETOITEM(rbto, self);
   1010        1.1      matt }
   1011        1.1      matt 
   1012        1.1      matt #ifdef RBDEBUG
   1013        1.1      matt static const struct rb_node *
   1014        1.1      matt rb_tree_iterate_const(const struct rb_tree *rbt, const struct rb_node *self,
   1015        1.1      matt 	const unsigned int direction)
   1016        1.1      matt {
   1017        1.1      matt 	const unsigned int other = direction ^ RB_DIR_OTHER;
   1018        1.1      matt 	KASSERT(direction == RB_DIR_LEFT || direction == RB_DIR_RIGHT);
   1019        1.1      matt 
   1020        1.1      matt 	if (self == NULL) {
   1021        1.1      matt #ifndef RBSMALL
   1022        1.1      matt 		if (RB_SENTINEL_P(rbt->rbt_root))
   1023        1.1      matt 			return NULL;
   1024        1.1      matt 		return rbt->rbt_minmax[direction];
   1025        1.1      matt #else
   1026        1.1      matt 		self = rbt->rbt_root;
   1027        1.1      matt 		if (RB_SENTINEL_P(self))
   1028        1.1      matt 			return NULL;
   1029        1.6     joerg 		while (!RB_SENTINEL_P(self->rb_nodes[direction]))
   1030        1.6     joerg 			self = self->rb_nodes[direction];
   1031        1.1      matt 		return self;
   1032        1.1      matt #endif /* !RBSMALL */
   1033        1.1      matt 	}
   1034        1.1      matt 	KASSERT(!RB_SENTINEL_P(self));
   1035        1.1      matt 	/*
   1036        1.1      matt 	 * We can't go any further in this direction.  We proceed up in the
   1037        1.1      matt 	 * opposite direction until our parent is in direction we want to go.
   1038        1.1      matt 	 */
   1039        1.1      matt 	if (RB_SENTINEL_P(self->rb_nodes[direction])) {
   1040        1.1      matt 		while (!RB_ROOT_P(rbt, self)) {
   1041        1.1      matt 			if (other == RB_POSITION(self))
   1042        1.1      matt 				return RB_FATHER(self);
   1043        1.1      matt 			self = RB_FATHER(self);
   1044        1.1      matt 		}
   1045        1.1      matt 		return NULL;
   1046        1.1      matt 	}
   1047        1.1      matt 
   1048        1.1      matt 	/*
   1049        1.1      matt 	 * Advance down one in current direction and go down as far as possible
   1050        1.1      matt 	 * in the opposite direction.
   1051        1.1      matt 	 */
   1052        1.1      matt 	self = self->rb_nodes[direction];
   1053        1.1      matt 	KASSERT(!RB_SENTINEL_P(self));
   1054        1.1      matt 	while (!RB_SENTINEL_P(self->rb_nodes[other]))
   1055        1.1      matt 		self = self->rb_nodes[other];
   1056        1.1      matt 	return self;
   1057        1.1      matt }
   1058        1.1      matt 
   1059        1.1      matt static unsigned int
   1060        1.1      matt rb_tree_count_black(const struct rb_node *self)
   1061        1.1      matt {
   1062        1.1      matt 	unsigned int left, right;
   1063        1.1      matt 
   1064        1.1      matt 	if (RB_SENTINEL_P(self))
   1065        1.1      matt 		return 0;
   1066        1.1      matt 
   1067        1.1      matt 	left = rb_tree_count_black(self->rb_left);
   1068        1.1      matt 	right = rb_tree_count_black(self->rb_right);
   1069        1.1      matt 
   1070        1.1      matt 	KASSERT(left == right);
   1071        1.1      matt 
   1072        1.1      matt 	return left + RB_BLACK_P(self);
   1073        1.1      matt }
   1074        1.1      matt 
   1075        1.1      matt static bool
   1076        1.1      matt rb_tree_check_node(const struct rb_tree *rbt, const struct rb_node *self,
   1077        1.1      matt 	const struct rb_node *prev, bool red_check)
   1078        1.1      matt {
   1079        1.7     rmind 	const rb_tree_ops_t *rbto = rbt->rbt_ops;
   1080        1.7     rmind 	rbto_compare_nodes_fn compare_nodes = rbto->rbto_compare_nodes;
   1081        1.1      matt 
   1082        1.1      matt 	KASSERT(!RB_SENTINEL_P(self));
   1083        1.7     rmind 	KASSERT(prev == NULL || (*compare_nodes)(rbto->rbto_context,
   1084        1.7     rmind 	    RB_NODETOITEM(rbto, prev), RB_NODETOITEM(rbto, self)) < 0);
   1085        1.1      matt 
   1086        1.1      matt 	/*
   1087        1.1      matt 	 * Verify our relationship to our parent.
   1088        1.1      matt 	 */
   1089        1.1      matt 	if (RB_ROOT_P(rbt, self)) {
   1090        1.1      matt 		KASSERT(self == rbt->rbt_root);
   1091        1.1      matt 		KASSERT(RB_POSITION(self) == RB_DIR_LEFT);
   1092        1.1      matt 		KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self);
   1093        1.1      matt 		KASSERT(RB_FATHER(self) == (const struct rb_node *) &rbt->rbt_root);
   1094        1.1      matt 	} else {
   1095        1.7     rmind 		int diff = (*compare_nodes)(rbto->rbto_context,
   1096        1.7     rmind 		    RB_NODETOITEM(rbto, self),
   1097        1.7     rmind 		    RB_NODETOITEM(rbto, RB_FATHER(self)));
   1098        1.7     rmind 
   1099        1.1      matt 		KASSERT(self != rbt->rbt_root);
   1100        1.1      matt 		KASSERT(!RB_FATHER_SENTINEL_P(self));
   1101        1.1      matt 		if (RB_POSITION(self) == RB_DIR_LEFT) {
   1102        1.7     rmind 			KASSERT(diff < 0);
   1103        1.1      matt 			KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_LEFT] == self);
   1104        1.1      matt 		} else {
   1105        1.7     rmind 			KASSERT(diff > 0);
   1106        1.1      matt 			KASSERT(RB_FATHER(self)->rb_nodes[RB_DIR_RIGHT] == self);
   1107        1.1      matt 		}
   1108        1.1      matt 	}
   1109        1.1      matt 
   1110        1.1      matt 	/*
   1111        1.1      matt 	 * Verify our position in the linked list against the tree itself.
   1112        1.1      matt 	 */
   1113        1.1      matt 	{
   1114        1.1      matt 		const struct rb_node *prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT);
   1115        1.1      matt 		const struct rb_node *next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT);
   1116        1.1      matt 		KASSERT(prev0 == TAILQ_PREV(self, rb_node_qh, rb_link));
   1117        1.1      matt 		KASSERT(next0 == TAILQ_NEXT(self, rb_link));
   1118        1.1      matt #ifndef RBSMALL
   1119        1.1      matt 		KASSERT(prev0 != NULL || self == rbt->rbt_minmax[RB_DIR_LEFT]);
   1120        1.1      matt 		KASSERT(next0 != NULL || self == rbt->rbt_minmax[RB_DIR_RIGHT]);
   1121        1.1      matt #endif
   1122        1.1      matt 	}
   1123        1.1      matt 
   1124        1.1      matt 	/*
   1125        1.1      matt 	 * The root must be black.
   1126        1.1      matt 	 * There can never be two adjacent red nodes.
   1127        1.1      matt 	 */
   1128        1.1      matt 	if (red_check) {
   1129        1.1      matt 		KASSERT(!RB_ROOT_P(rbt, self) || RB_BLACK_P(self));
   1130        1.1      matt 		(void) rb_tree_count_black(self);
   1131        1.1      matt 		if (RB_RED_P(self)) {
   1132        1.1      matt 			const struct rb_node *brother;
   1133        1.1      matt 			KASSERT(!RB_ROOT_P(rbt, self));
   1134        1.1      matt 			brother = RB_FATHER(self)->rb_nodes[RB_POSITION(self) ^ RB_DIR_OTHER];
   1135        1.1      matt 			KASSERT(RB_BLACK_P(RB_FATHER(self)));
   1136        1.1      matt 			/*
   1137        1.1      matt 			 * I'm red and have no children, then I must either
   1138        1.1      matt 			 * have no brother or my brother also be red and
   1139        1.1      matt 			 * also have no children.  (black count == 0)
   1140        1.1      matt 			 */
   1141        1.1      matt 			KASSERT(!RB_CHILDLESS_P(self)
   1142        1.1      matt 				|| RB_SENTINEL_P(brother)
   1143        1.1      matt 				|| RB_RED_P(brother)
   1144        1.1      matt 				|| RB_CHILDLESS_P(brother));
   1145        1.1      matt 			/*
   1146        1.1      matt 			 * If I'm not childless, I must have two children
   1147        1.1      matt 			 * and they must be both be black.
   1148        1.1      matt 			 */
   1149        1.1      matt 			KASSERT(RB_CHILDLESS_P(self)
   1150        1.1      matt 				|| (RB_TWOCHILDREN_P(self)
   1151        1.1      matt 				    && RB_BLACK_P(self->rb_left)
   1152        1.1      matt 				    && RB_BLACK_P(self->rb_right)));
   1153        1.1      matt 			/*
   1154        1.1      matt 			 * If I'm not childless, thus I have black children,
   1155        1.1      matt 			 * then my brother must either be black or have two
   1156        1.1      matt 			 * black children.
   1157        1.1      matt 			 */
   1158        1.1      matt 			KASSERT(RB_CHILDLESS_P(self)
   1159        1.1      matt 				|| RB_BLACK_P(brother)
   1160        1.1      matt 				|| (RB_TWOCHILDREN_P(brother)
   1161        1.1      matt 				    && RB_BLACK_P(brother->rb_left)
   1162        1.1      matt 				    && RB_BLACK_P(brother->rb_right)));
   1163        1.1      matt 		} else {
   1164        1.1      matt 			/*
   1165        1.1      matt 			 * If I'm black and have one child, that child must
   1166        1.1      matt 			 * be red and childless.
   1167        1.1      matt 			 */
   1168        1.1      matt 			KASSERT(RB_CHILDLESS_P(self)
   1169        1.1      matt 				|| RB_TWOCHILDREN_P(self)
   1170        1.1      matt 				|| (!RB_LEFT_SENTINEL_P(self)
   1171        1.1      matt 				    && RB_RIGHT_SENTINEL_P(self)
   1172        1.1      matt 				    && RB_RED_P(self->rb_left)
   1173        1.1      matt 				    && RB_CHILDLESS_P(self->rb_left))
   1174        1.1      matt 				|| (!RB_RIGHT_SENTINEL_P(self)
   1175        1.1      matt 				    && RB_LEFT_SENTINEL_P(self)
   1176        1.1      matt 				    && RB_RED_P(self->rb_right)
   1177        1.1      matt 				    && RB_CHILDLESS_P(self->rb_right)));
   1178        1.1      matt 
   1179        1.1      matt 			/*
   1180        1.1      matt 			 * If I'm a childless black node and my parent is
   1181        1.1      matt 			 * black, my 2nd closet relative away from my parent
   1182        1.1      matt 			 * is either red or has a red parent or red children.
   1183        1.1      matt 			 */
   1184        1.1      matt 			if (!RB_ROOT_P(rbt, self)
   1185        1.1      matt 			    && RB_CHILDLESS_P(self)
   1186        1.1      matt 			    && RB_BLACK_P(RB_FATHER(self))) {
   1187        1.1      matt 				const unsigned int which = RB_POSITION(self);
   1188        1.1      matt 				const unsigned int other = which ^ RB_DIR_OTHER;
   1189        1.1      matt 				const struct rb_node *relative0, *relative;
   1190        1.1      matt 
   1191        1.1      matt 				relative0 = rb_tree_iterate_const(rbt,
   1192        1.1      matt 				    self, other);
   1193        1.1      matt 				KASSERT(relative0 != NULL);
   1194        1.1      matt 				relative = rb_tree_iterate_const(rbt,
   1195        1.1      matt 				    relative0, other);
   1196        1.1      matt 				KASSERT(relative != NULL);
   1197        1.1      matt 				KASSERT(RB_SENTINEL_P(relative->rb_nodes[which]));
   1198        1.1      matt #if 0
   1199        1.1      matt 				KASSERT(RB_RED_P(relative)
   1200        1.1      matt 					|| RB_RED_P(relative->rb_left)
   1201        1.1      matt 					|| RB_RED_P(relative->rb_right)
   1202        1.1      matt 					|| RB_RED_P(RB_FATHER(relative)));
   1203        1.1      matt #endif
   1204        1.1      matt 			}
   1205        1.1      matt 		}
   1206        1.1      matt 		/*
   1207        1.1      matt 		 * A grandparent's children must be real nodes and not
   1208        1.1      matt 		 * sentinels.  First check out grandparent.
   1209        1.1      matt 		 */
   1210        1.1      matt 		KASSERT(RB_ROOT_P(rbt, self)
   1211        1.1      matt 			|| RB_ROOT_P(rbt, RB_FATHER(self))
   1212        1.1      matt 			|| RB_TWOCHILDREN_P(RB_FATHER(RB_FATHER(self))));
   1213        1.1      matt 		/*
   1214        1.1      matt 		 * If we are have grandchildren on our left, then
   1215        1.1      matt 		 * we must have a child on our right.
   1216        1.1      matt 		 */
   1217        1.1      matt 		KASSERT(RB_LEFT_SENTINEL_P(self)
   1218        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left)
   1219        1.1      matt 			|| !RB_RIGHT_SENTINEL_P(self));
   1220        1.1      matt 		/*
   1221        1.1      matt 		 * If we are have grandchildren on our right, then
   1222        1.1      matt 		 * we must have a child on our left.
   1223        1.1      matt 		 */
   1224        1.1      matt 		KASSERT(RB_RIGHT_SENTINEL_P(self)
   1225        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right)
   1226        1.1      matt 			|| !RB_LEFT_SENTINEL_P(self));
   1227        1.1      matt 
   1228        1.1      matt 		/*
   1229        1.1      matt 		 * If we have a child on the left and it doesn't have two
   1230        1.1      matt 		 * children make sure we don't have great-great-grandchildren on
   1231        1.1      matt 		 * the right.
   1232        1.1      matt 		 */
   1233        1.1      matt 		KASSERT(RB_TWOCHILDREN_P(self->rb_left)
   1234        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right)
   1235        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right->rb_left)
   1236        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right->rb_left->rb_left)
   1237        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right->rb_left->rb_right)
   1238        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right->rb_right)
   1239        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right->rb_right->rb_left)
   1240        1.1      matt 			|| RB_CHILDLESS_P(self->rb_right->rb_right->rb_right));
   1241        1.1      matt 
   1242        1.1      matt 		/*
   1243        1.1      matt 		 * If we have a child on the right and it doesn't have two
   1244        1.1      matt 		 * children make sure we don't have great-great-grandchildren on
   1245        1.1      matt 		 * the left.
   1246        1.1      matt 		 */
   1247        1.1      matt 		KASSERT(RB_TWOCHILDREN_P(self->rb_right)
   1248        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left)
   1249        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left->rb_left)
   1250        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left->rb_left->rb_left)
   1251        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left->rb_left->rb_right)
   1252        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left->rb_right)
   1253        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left->rb_right->rb_left)
   1254        1.1      matt 			|| RB_CHILDLESS_P(self->rb_left->rb_right->rb_right));
   1255        1.1      matt 
   1256        1.1      matt 		/*
   1257        1.1      matt 		 * If we are fully interior node, then our predecessors and
   1258        1.1      matt 		 * successors must have no children in our direction.
   1259        1.1      matt 		 */
   1260        1.1      matt 		if (RB_TWOCHILDREN_P(self)) {
   1261        1.1      matt 			const struct rb_node *prev0;
   1262        1.1      matt 			const struct rb_node *next0;
   1263        1.1      matt 
   1264        1.1      matt 			prev0 = rb_tree_iterate_const(rbt, self, RB_DIR_LEFT);
   1265        1.1      matt 			KASSERT(prev0 != NULL);
   1266        1.1      matt 			KASSERT(RB_RIGHT_SENTINEL_P(prev0));
   1267        1.1      matt 
   1268        1.1      matt 			next0 = rb_tree_iterate_const(rbt, self, RB_DIR_RIGHT);
   1269        1.1      matt 			KASSERT(next0 != NULL);
   1270        1.1      matt 			KASSERT(RB_LEFT_SENTINEL_P(next0));
   1271        1.1      matt 		}
   1272        1.1      matt 	}
   1273        1.1      matt 
   1274        1.1      matt 	return true;
   1275        1.1      matt }
   1276        1.1      matt 
   1277        1.1      matt void
   1278        1.1      matt rb_tree_check(const struct rb_tree *rbt, bool red_check)
   1279        1.1      matt {
   1280        1.1      matt 	const struct rb_node *self;
   1281        1.1      matt 	const struct rb_node *prev;
   1282        1.1      matt #ifdef RBSTATS
   1283        1.1      matt 	unsigned int count = 0;
   1284        1.1      matt #endif
   1285        1.1      matt 
   1286        1.1      matt 	KASSERT(rbt->rbt_root != NULL);
   1287        1.1      matt 	KASSERT(RB_LEFT_P(rbt->rbt_root));
   1288        1.1      matt 
   1289        1.1      matt #if defined(RBSTATS) && !defined(RBSMALL)
   1290        1.1      matt 	KASSERT(rbt->rbt_count > 1
   1291        1.1      matt 	    || rbt->rbt_minmax[RB_DIR_LEFT] == rbt->rbt_minmax[RB_DIR_RIGHT]);
   1292        1.1      matt #endif
   1293        1.1      matt 
   1294        1.1      matt 	prev = NULL;
   1295        1.1      matt 	TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) {
   1296        1.1      matt 		rb_tree_check_node(rbt, self, prev, false);
   1297        1.1      matt #ifdef RBSTATS
   1298        1.1      matt 		count++;
   1299        1.1      matt #endif
   1300        1.1      matt 	}
   1301        1.1      matt #ifdef RBSTATS
   1302        1.1      matt 	KASSERT(rbt->rbt_count == count);
   1303        1.1      matt #endif
   1304        1.1      matt 	if (red_check) {
   1305        1.1      matt 		KASSERT(RB_BLACK_P(rbt->rbt_root));
   1306        1.1      matt 		KASSERT(RB_SENTINEL_P(rbt->rbt_root)
   1307        1.1      matt 			|| rb_tree_count_black(rbt->rbt_root));
   1308        1.1      matt 
   1309        1.1      matt 		/*
   1310        1.1      matt 		 * The root must be black.
   1311        1.1      matt 		 * There can never be two adjacent red nodes.
   1312        1.1      matt 		 */
   1313        1.1      matt 		TAILQ_FOREACH(self, &rbt->rbt_nodes, rb_link) {
   1314        1.1      matt 			rb_tree_check_node(rbt, self, NULL, true);
   1315        1.1      matt 		}
   1316        1.1      matt 	}
   1317        1.1      matt }
   1318        1.1      matt #endif /* RBDEBUG */
   1319        1.1      matt 
   1320        1.1      matt #ifdef RBSTATS
   1321        1.1      matt static void
   1322        1.1      matt rb_tree_mark_depth(const struct rb_tree *rbt, const struct rb_node *self,
   1323        1.1      matt 	size_t *depths, size_t depth)
   1324        1.1      matt {
   1325        1.1      matt 	if (RB_SENTINEL_P(self))
   1326        1.1      matt 		return;
   1327        1.1      matt 
   1328        1.1      matt 	if (RB_TWOCHILDREN_P(self)) {
   1329        1.1      matt 		rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1);
   1330        1.1      matt 		rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1);
   1331        1.1      matt 		return;
   1332        1.1      matt 	}
   1333        1.1      matt 	depths[depth]++;
   1334        1.1      matt 	if (!RB_LEFT_SENTINEL_P(self)) {
   1335        1.1      matt 		rb_tree_mark_depth(rbt, self->rb_left, depths, depth + 1);
   1336        1.1      matt 	}
   1337        1.1      matt 	if (!RB_RIGHT_SENTINEL_P(self)) {
   1338        1.1      matt 		rb_tree_mark_depth(rbt, self->rb_right, depths, depth + 1);
   1339        1.1      matt 	}
   1340        1.1      matt }
   1341        1.1      matt 
   1342        1.1      matt void
   1343        1.1      matt rb_tree_depths(const struct rb_tree *rbt, size_t *depths)
   1344        1.1      matt {
   1345        1.1      matt 	rb_tree_mark_depth(rbt, rbt->rbt_root, depths, 1);
   1346        1.1      matt }
   1347        1.1      matt #endif /* RBSTATS */
   1348