Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: rbtree.h,v 1.12 2024/05/12 10:34:56 rillig Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Matt Thomas <matt (at) 3am-software.com>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _SYS_RBTREE_H_
     33 #define	_SYS_RBTREE_H_
     34 
     35 #include <sys/types.h>
     36 #include <sys/queue.h>
     37 #include <sys/endian.h>
     38 
     39 #if !defined(_KERNEL) && !defined(_STANDALONE) && defined(RBDEBUG)
     40 #include <stdbool.h>
     41 #endif
     42 
     43 __BEGIN_DECLS
     44 
     45 typedef struct rb_node {
     46 	struct rb_node *rb_nodes[2];
     47 #define	RB_DIR_LEFT		0
     48 #define	RB_DIR_RIGHT		1
     49 #define	RB_DIR_OTHER		1
     50 #define	rb_left			rb_nodes[RB_DIR_LEFT]
     51 #define	rb_right		rb_nodes[RB_DIR_RIGHT]
     52 
     53 	/*
     54 	 * rb_info contains the two flags and the parent back pointer.
     55 	 * We put the two flags in the low two bits since we know that
     56 	 * rb_node will have an alignment of 4 or 8 bytes.
     57 	 */
     58 	uintptr_t rb_info;
     59 #define	RB_FLAG_POSITION	(uintptr_t)0x2
     60 #define	RB_FLAG_RED		(uintptr_t)0x1
     61 #define	RB_FLAG_MASK		(RB_FLAG_POSITION|RB_FLAG_RED)
     62 #define	RB_FATHER(rb) \
     63     ((struct rb_node *)((rb)->rb_info & ~RB_FLAG_MASK))
     64 #define	RB_SET_FATHER(rb, father) \
     65     ((void)((rb)->rb_info = (uintptr_t)(father)|((rb)->rb_info & RB_FLAG_MASK)))
     66 
     67 #define	RB_SENTINEL_P(rb)	((rb) == NULL)
     68 #define	RB_LEFT_SENTINEL_P(rb)	RB_SENTINEL_P((rb)->rb_left)
     69 #define	RB_RIGHT_SENTINEL_P(rb)	RB_SENTINEL_P((rb)->rb_right)
     70 #define	RB_FATHER_SENTINEL_P(rb) RB_SENTINEL_P(RB_FATHER((rb)))
     71 #define	RB_CHILDLESS_P(rb) \
     72     (RB_SENTINEL_P(rb) || (RB_LEFT_SENTINEL_P(rb) && RB_RIGHT_SENTINEL_P(rb)))
     73 #define	RB_TWOCHILDREN_P(rb) \
     74     (!RB_SENTINEL_P(rb) && !RB_LEFT_SENTINEL_P(rb) && !RB_RIGHT_SENTINEL_P(rb))
     75 
     76 #define	RB_POSITION(rb)	\
     77     (((rb)->rb_info & RB_FLAG_POSITION) ? RB_DIR_RIGHT : RB_DIR_LEFT)
     78 #define	RB_RIGHT_P(rb)		(RB_POSITION(rb) == RB_DIR_RIGHT)
     79 #define	RB_LEFT_P(rb)		(RB_POSITION(rb) == RB_DIR_LEFT)
     80 #define	RB_RED_P(rb) 		(!RB_SENTINEL_P(rb) && ((rb)->rb_info & RB_FLAG_RED) != 0)
     81 #define	RB_BLACK_P(rb) 		(RB_SENTINEL_P(rb) || ((rb)->rb_info & RB_FLAG_RED) == 0)
     82 #define	RB_MARK_RED(rb) 	((void)((rb)->rb_info |= RB_FLAG_RED))
     83 #define	RB_MARK_BLACK(rb) 	((void)((rb)->rb_info &= ~RB_FLAG_RED))
     84 #define	RB_INVERT_COLOR(rb) 	((void)((rb)->rb_info ^= RB_FLAG_RED))
     85 #define	RB_ROOT_P(rbt, rb)	((rbt)->rbt_root == (rb))
     86 #define	RB_SET_POSITION(rb, position) \
     87     ((void)((position) ? ((rb)->rb_info |= RB_FLAG_POSITION) : \
     88     ((rb)->rb_info &= ~RB_FLAG_POSITION)))
     89 #define	RB_ZERO_PROPERTIES(rb)	((void)((rb)->rb_info &= ~RB_FLAG_MASK))
     90 #define	RB_COPY_PROPERTIES(dst, src) \
     91     ((void)((dst)->rb_info ^= ((dst)->rb_info ^ (src)->rb_info) & RB_FLAG_MASK))
     92 #define RB_SWAP_PROPERTIES(a, b) do { \
     93     uintptr_t xorinfo = ((a)->rb_info ^ (b)->rb_info) & RB_FLAG_MASK; \
     94     (a)->rb_info ^= xorinfo; \
     95     (b)->rb_info ^= xorinfo; \
     96   } while (0)
     97 #ifdef RBDEBUG
     98 	TAILQ_ENTRY(rb_node) rb_link;
     99 #endif
    100 } rb_node_t;
    101 
    102 #define RB_TREE_MIN(T) rb_tree_iterate((T), NULL, RB_DIR_LEFT)
    103 #define RB_TREE_MAX(T) rb_tree_iterate((T), NULL, RB_DIR_RIGHT)
    104 #define RB_TREE_NEXT(T, N) rb_tree_iterate((T), (N), RB_DIR_RIGHT)
    105 #define RB_TREE_PREV(T, N) rb_tree_iterate((T), (N), RB_DIR_LEFT)
    106 #define RB_TREE_FOREACH(N, T) \
    107     for ((N) = RB_TREE_MIN(T); (N); (N) = RB_TREE_NEXT((T), (N)))
    108 #define RB_TREE_FOREACH_REVERSE(N, T) \
    109     for ((N) = RB_TREE_MAX(T); (N); (N) = RB_TREE_PREV((T), (N)))
    110 #define RB_TREE_FOREACH_SAFE(N, T, S) \
    111     for ((N) = RB_TREE_MIN(T); \
    112         (N) && ((S) = RB_TREE_NEXT((T), (N)), 1); \
    113         (N) = (S))
    114 #define RB_TREE_FOREACH_REVERSE_SAFE(N, T, S) \
    115     for ((N) = RB_TREE_MAX(T); \
    116         (N) && ((S) = RB_TREE_PREV((T), (N)), 1); \
    117         (N) = (S))
    118 
    119 #ifdef RBDEBUG
    120 TAILQ_HEAD(rb_node_qh, rb_node);
    121 
    122 #define	RB_TAILQ_REMOVE(a, b, c)		TAILQ_REMOVE(a, b, c)
    123 #define	RB_TAILQ_INIT(a)			TAILQ_INIT(a)
    124 #define	RB_TAILQ_INSERT_HEAD(a, b, c)		TAILQ_INSERT_HEAD(a, b, c)
    125 #define	RB_TAILQ_INSERT_BEFORE(a, b, c)		TAILQ_INSERT_BEFORE(a, b, c)
    126 #define	RB_TAILQ_INSERT_AFTER(a, b, c, d)	TAILQ_INSERT_AFTER(a, b, c, d)
    127 
    128 #define	RBDEBUG_TREE_INITIALIZER(t)					      \
    129 	.rbt_nodes = TAILQ_HEAD_INITIALIZER((t).rbt_nodes),
    130 #else
    131 #define	RB_TAILQ_REMOVE(a, b, c)		do { } while (0)
    132 #define	RB_TAILQ_INIT(a)			do { } while (0)
    133 #define	RB_TAILQ_INSERT_HEAD(a, b, c)		do { } while (0)
    134 #define	RB_TAILQ_INSERT_BEFORE(a, b, c)		do { } while (0)
    135 #define	RB_TAILQ_INSERT_AFTER(a, b, c, d)	do { } while (0)
    136 
    137 #define	RBDEBUG_TREE_INITIALIZER(t)		/* nothing */
    138 #endif /* RBDEBUG */
    139 
    140 /*
    141  * rbto_compare_nodes_fn:
    142  *	return a positive value if the first node > the second node.
    143  *	return a negative value if the first node < the second node.
    144  *	return 0 if they are considered same.
    145  *
    146  * rbto_compare_key_fn:
    147  *	return a positive value if the node > the key.
    148  *	return a negative value if the node < the key.
    149  *	return 0 if they are considered same.
    150  */
    151 
    152 typedef signed int (*rbto_compare_nodes_fn)(void *, const void *, const void *);
    153 typedef signed int (*rbto_compare_key_fn)(void *, const void *, const void *);
    154 
    155 typedef struct {
    156 	rbto_compare_nodes_fn rbto_compare_nodes;
    157 	rbto_compare_key_fn rbto_compare_key;
    158 	size_t rbto_node_offset;
    159 	void *rbto_context;
    160 } rb_tree_ops_t;
    161 
    162 typedef struct rb_tree {
    163 	struct rb_node *rbt_root;
    164 	const rb_tree_ops_t *rbt_ops;
    165 	struct rb_node *rbt_minmax[2];
    166 #ifdef RBDEBUG
    167 	struct rb_node_qh rbt_nodes;
    168 #endif
    169 #ifdef RBSTATS
    170 	unsigned int rbt_count;
    171 	unsigned int rbt_insertions;
    172 	unsigned int rbt_removals;
    173 	unsigned int rbt_insertion_rebalance_calls;
    174 	unsigned int rbt_insertion_rebalance_passes;
    175 	unsigned int rbt_removal_rebalance_calls;
    176 	unsigned int rbt_removal_rebalance_passes;
    177 #endif
    178 } rb_tree_t;
    179 
    180 #ifdef RBSTATS
    181 #define	RBSTAT_INC(v)	((void)((v)++))
    182 #define	RBSTAT_DEC(v)	((void)((v)--))
    183 #else
    184 #define	RBSTAT_INC(v)	do { } while (0)
    185 #define	RBSTAT_DEC(v)	do { } while (0)
    186 #endif
    187 
    188 #define	RB_TREE_INIT_TYPECHECK(t)					      \
    189 	0*sizeof(&(t) - (struct rb_tree *)0)
    190 
    191 #define	RB_TREE_INITIALIZER(t, ops)					      \
    192 {									      \
    193 	.rbt_ops = (ops) + RB_TREE_INIT_TYPECHECK(t),			      \
    194 	RBDEBUG_TREE_INITIALIZER(t)					      \
    195 }
    196 
    197 void	rb_tree_init(rb_tree_t *, const rb_tree_ops_t *);
    198 void *	rb_tree_insert_node(rb_tree_t *, void *);
    199 void *	rb_tree_find_node(rb_tree_t *, const void *);
    200 void *	rb_tree_find_node_geq(rb_tree_t *, const void *);
    201 void *	rb_tree_find_node_leq(rb_tree_t *, const void *);
    202 void	rb_tree_remove_node(rb_tree_t *, void *);
    203 void *	rb_tree_iterate(rb_tree_t *, void *, const unsigned int);
    204 #ifdef RBDEBUG
    205 void	rb_tree_check(const rb_tree_t *, bool);
    206 #endif
    207 #ifdef RBSTATS
    208 void	rb_tree_depths(const rb_tree_t *, size_t *);
    209 #endif
    210 
    211 __END_DECLS
    212 
    213 #endif	/* _SYS_RBTREE_H_*/
    214