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