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