1 /* $NetBSD: prop_rb_impl.h,v 1.7 2008/06/30 20:14:09 matt Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
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
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 _PROP_RB_IMPL_H_
33 #define _PROP_RB_IMPL_H_
39 * Define local names for common rb_tree functions.
41 #define _prop_rb_tree_init rb_tree_init
42 #define _prop_rb_tree_insert_node rb_tree_insert_node
43 #define _prop_rb_tree_find rb_tree_find_node
44 #define _prop_rb_tree_remove_node rb_tree_remove_node
45 #define _prop_rb_tree_iterate rb_tree_iterate
47 #else /* __NetBSD__ */
49 #include <sys/types.h>
50 #include <sys/queue.h>
51 #include <machine/endian.h>
54 struct rb_node *rb_nodes[3];
55 #define RB_NODE_LEFT 0
56 #define RB_NODE_RIGHT 1
57 #define RB_NODE_OTHER 1
58 #define RB_NODE_PARENT 2
59 #define rb_left rb_nodes[RB_NODE_LEFT]
60 #define rb_right rb_nodes[RB_NODE_RIGHT]
61 #define rb_parent rb_nodes[RB_NODE_PARENT]
64 #if BYTE_ORDER == LITTLE_ENDIAN
66 unsigned int s_root : 1;
67 unsigned int s_position : 1;
68 unsigned int s_color : 1;
69 unsigned int s_sentinel : 1;
71 #if BYTE_ORDER == BIG_ENDIAN
72 unsigned int s_sentinel : 1;
73 unsigned int s_color : 1;
74 unsigned int s_position : 1;
75 unsigned int s_root : 1;
81 #define rb_root rb_u.u_s.s_root
82 #define rb_position rb_u.u_s.s_position
83 #define rb_color rb_u.u_s.s_color
84 #define rb_sentinel rb_u.u_s.s_sentinel
85 #define rb_properties rb_u.u_i
86 #define RB_SENTINEL_P(rb) ((rb)->rb_sentinel + 0)
87 #define RB_LEFT_SENTINEL_P(rb) ((rb)->rb_left->rb_sentinel + 0)
88 #define RB_RIGHT_SENTINEL_P(rb) ((rb)->rb_right->rb_sentinel + 0)
89 #define RB_PARENT_SENTINEL_P(rb) ((rb)->rb_parent->rb_sentinel + 0)
90 #define RB_CHILDLESS_P(rb) (RB_LEFT_SENTINEL_P(rb) \
91 && RB_RIGHT_SENTINEL_P(rb))
92 #define RB_TWOCHILDREN_P(rb) (!RB_LEFT_SENTINEL_P(rb) \
93 && !RB_RIGHT_SENTINEL_P(rb))
94 #define RB_ROOT_P(rb) ((rb)->rb_root != false)
95 #define RB_RED_P(rb) ((rb)->rb_color + 0)
96 #define RB_BLACK_P(rb) (!(rb)->rb_color)
97 #define RB_MARK_RED(rb) ((void)((rb)->rb_color = 1))
98 #define RB_MARK_BLACK(rb) ((void)((rb)->rb_color = 0))
99 #define RB_MARK_ROOT(rb) ((void)((rb)->rb_root = 1))
101 TAILQ_ENTRY(rb_node) rb_link;
106 TAILQ_HEAD(rb_node_qh, rb_node);
108 #define RB_TAILQ_REMOVE TAILQ_REMOVE
109 #define RB_TAILQ_INIT TAILQ_INIT
110 #define RB_TAILQ_INSERT_HEAD(a, b, c) TAILQ_INSERT_HEAD
111 #define RB_TAILQ_INSERT_BEFORE(a, b, c) TAILQ_INSERT_BEFORE
112 #define RB_TAILQ_INSERT_AFTER(a, b, c, d) TAILQ_INSERT_AFTER
114 #define RB_TAILQ_REMOVE(a, b, c) do { } while (/*CONSTCOND*/0)
115 #define RB_TAILQ_INIT(a) do { } while (/*CONSTCOND*/0)
116 #define RB_TAILQ_INSERT_HEAD(a, b, c) do { } while (/*CONSTCOND*/0)
117 #define RB_TAILQ_INSERT_BEFORE(a, b, c) do { } while (/*CONSTCOND*/0)
118 #define RB_TAILQ_INSERT_AFTER(a, b, c, d) do { } while (/*CONSTCOND*/0)
121 typedef int (*rb_compare_nodes_fn)(const struct rb_node *,
122 const struct rb_node *);
123 typedef int (*rb_compare_key_fn)(const struct rb_node *, const void *);
126 rb_compare_nodes_fn rbto_compare_nodes;
127 rb_compare_key_fn rbto_compare_key;
131 struct rb_node *rbt_root;
133 struct rb_node_qh rbt_nodes;
135 const struct rb_tree_ops *rbt_ops;
137 unsigned int rbt_count;
141 void _prop_rb_tree_init(struct rb_tree *, const struct rb_tree_ops *);
142 bool _prop_rb_tree_insert_node(struct rb_tree *, struct rb_node *);
144 _prop_rb_tree_find(struct rb_tree *, const void *);
145 void _prop_rb_tree_remove_node(struct rb_tree *, struct rb_node *);
147 void _prop_rb_tree_check(const struct rb_tree *, bool);
150 _prop_rb_tree_iterate(struct rb_tree *, struct rb_node *, unsigned int);
152 #endif /* __NetBSD__ */
154 #endif /* _PROP_RB_IMPL_H_*/