Blame


1 83f0f95a 2022-09-29 op /* $OpenBSD: tree.h,v 1.30 2020/10/10 18:03:41 otto Exp $ */
2 83f0f95a 2022-09-29 op /*
3 83f0f95a 2022-09-29 op * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 83f0f95a 2022-09-29 op * All rights reserved.
5 83f0f95a 2022-09-29 op *
6 83f0f95a 2022-09-29 op * Redistribution and use in source and binary forms, with or without
7 83f0f95a 2022-09-29 op * modification, are permitted provided that the following conditions
8 83f0f95a 2022-09-29 op * are met:
9 83f0f95a 2022-09-29 op * 1. Redistributions of source code must retain the above copyright
10 83f0f95a 2022-09-29 op * notice, this list of conditions and the following disclaimer.
11 83f0f95a 2022-09-29 op * 2. Redistributions in binary form must reproduce the above copyright
12 83f0f95a 2022-09-29 op * notice, this list of conditions and the following disclaimer in the
13 83f0f95a 2022-09-29 op * documentation and/or other materials provided with the distribution.
14 83f0f95a 2022-09-29 op *
15 83f0f95a 2022-09-29 op * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 83f0f95a 2022-09-29 op * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 83f0f95a 2022-09-29 op * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 83f0f95a 2022-09-29 op * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 83f0f95a 2022-09-29 op * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 83f0f95a 2022-09-29 op * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 83f0f95a 2022-09-29 op * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 83f0f95a 2022-09-29 op * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 83f0f95a 2022-09-29 op * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 83f0f95a 2022-09-29 op * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 83f0f95a 2022-09-29 op */
26 83f0f95a 2022-09-29 op
27 83f0f95a 2022-09-29 op #ifndef _SYS_TREE_H_
28 83f0f95a 2022-09-29 op #define _SYS_TREE_H_
29 83f0f95a 2022-09-29 op
30 83f0f95a 2022-09-29 op #include <stddef.h>
31 83f0f95a 2022-09-29 op
32 83f0f95a 2022-09-29 op /*
33 83f0f95a 2022-09-29 op * Local modifications:
34 83f0f95a 2022-09-29 op * - dropped __unused
35 83f0f95a 2022-09-29 op * - __inline -> inline
36 83f0f95a 2022-09-29 op */
37 83f0f95a 2022-09-29 op
38 83f0f95a 2022-09-29 op /*
39 83f0f95a 2022-09-29 op * This file defines data structures for different types of trees:
40 83f0f95a 2022-09-29 op * splay trees and red-black trees.
41 83f0f95a 2022-09-29 op *
42 83f0f95a 2022-09-29 op * A splay tree is a self-organizing data structure. Every operation
43 83f0f95a 2022-09-29 op * on the tree causes a splay to happen. The splay moves the requested
44 83f0f95a 2022-09-29 op * node to the root of the tree and partly rebalances it.
45 83f0f95a 2022-09-29 op *
46 83f0f95a 2022-09-29 op * This has the benefit that request locality causes faster lookups as
47 83f0f95a 2022-09-29 op * the requested nodes move to the top of the tree. On the other hand,
48 83f0f95a 2022-09-29 op * every lookup causes memory writes.
49 83f0f95a 2022-09-29 op *
50 83f0f95a 2022-09-29 op * The Balance Theorem bounds the total access time for m operations
51 83f0f95a 2022-09-29 op * and n inserts on an initially empty tree as O((m + n)lg n). The
52 83f0f95a 2022-09-29 op * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
53 83f0f95a 2022-09-29 op *
54 83f0f95a 2022-09-29 op * A red-black tree is a binary search tree with the node color as an
55 83f0f95a 2022-09-29 op * extra attribute. It fulfills a set of conditions:
56 83f0f95a 2022-09-29 op * - every search path from the root to a leaf consists of the
57 83f0f95a 2022-09-29 op * same number of black nodes,
58 83f0f95a 2022-09-29 op * - each red node (except for the root) has a black parent,
59 83f0f95a 2022-09-29 op * - each leaf node is black.
60 83f0f95a 2022-09-29 op *
61 83f0f95a 2022-09-29 op * Every operation on a red-black tree is bounded as O(lg n).
62 83f0f95a 2022-09-29 op * The maximum height of a red-black tree is 2lg (n+1).
63 83f0f95a 2022-09-29 op */
64 83f0f95a 2022-09-29 op
65 83f0f95a 2022-09-29 op #define SPLAY_HEAD(name, type) \
66 83f0f95a 2022-09-29 op struct name { \
67 83f0f95a 2022-09-29 op struct type *sph_root; /* root of the tree */ \
68 83f0f95a 2022-09-29 op }
69 83f0f95a 2022-09-29 op
70 83f0f95a 2022-09-29 op #define SPLAY_INITIALIZER(root) \
71 83f0f95a 2022-09-29 op { NULL }
72 83f0f95a 2022-09-29 op
73 83f0f95a 2022-09-29 op #define SPLAY_INIT(root) do { \
74 83f0f95a 2022-09-29 op (root)->sph_root = NULL; \
75 83f0f95a 2022-09-29 op } while (0)
76 83f0f95a 2022-09-29 op
77 83f0f95a 2022-09-29 op #define SPLAY_ENTRY(type) \
78 83f0f95a 2022-09-29 op struct { \
79 83f0f95a 2022-09-29 op struct type *spe_left; /* left element */ \
80 83f0f95a 2022-09-29 op struct type *spe_right; /* right element */ \
81 83f0f95a 2022-09-29 op }
82 83f0f95a 2022-09-29 op
83 83f0f95a 2022-09-29 op #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
84 83f0f95a 2022-09-29 op #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
85 83f0f95a 2022-09-29 op #define SPLAY_ROOT(head) (head)->sph_root
86 83f0f95a 2022-09-29 op #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
87 83f0f95a 2022-09-29 op
88 83f0f95a 2022-09-29 op /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
89 83f0f95a 2022-09-29 op #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
90 83f0f95a 2022-09-29 op SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
91 83f0f95a 2022-09-29 op SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
92 83f0f95a 2022-09-29 op (head)->sph_root = tmp; \
93 83f0f95a 2022-09-29 op } while (0)
94 83f0f95a 2022-09-29 op
95 83f0f95a 2022-09-29 op #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
96 83f0f95a 2022-09-29 op SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
97 83f0f95a 2022-09-29 op SPLAY_LEFT(tmp, field) = (head)->sph_root; \
98 83f0f95a 2022-09-29 op (head)->sph_root = tmp; \
99 83f0f95a 2022-09-29 op } while (0)
100 83f0f95a 2022-09-29 op
101 83f0f95a 2022-09-29 op #define SPLAY_LINKLEFT(head, tmp, field) do { \
102 83f0f95a 2022-09-29 op SPLAY_LEFT(tmp, field) = (head)->sph_root; \
103 83f0f95a 2022-09-29 op tmp = (head)->sph_root; \
104 83f0f95a 2022-09-29 op (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
105 83f0f95a 2022-09-29 op } while (0)
106 83f0f95a 2022-09-29 op
107 83f0f95a 2022-09-29 op #define SPLAY_LINKRIGHT(head, tmp, field) do { \
108 83f0f95a 2022-09-29 op SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
109 83f0f95a 2022-09-29 op tmp = (head)->sph_root; \
110 83f0f95a 2022-09-29 op (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
111 83f0f95a 2022-09-29 op } while (0)
112 83f0f95a 2022-09-29 op
113 83f0f95a 2022-09-29 op #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
114 83f0f95a 2022-09-29 op SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
115 83f0f95a 2022-09-29 op SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
116 83f0f95a 2022-09-29 op SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
117 83f0f95a 2022-09-29 op SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
118 83f0f95a 2022-09-29 op } while (0)
119 83f0f95a 2022-09-29 op
120 83f0f95a 2022-09-29 op /* Generates prototypes and inline functions */
121 83f0f95a 2022-09-29 op
122 83f0f95a 2022-09-29 op #define SPLAY_PROTOTYPE(name, type, field, cmp) \
123 83f0f95a 2022-09-29 op void name##_SPLAY(struct name *, struct type *); \
124 83f0f95a 2022-09-29 op void name##_SPLAY_MINMAX(struct name *, int); \
125 83f0f95a 2022-09-29 op struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
126 83f0f95a 2022-09-29 op struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
127 83f0f95a 2022-09-29 op \
128 83f0f95a 2022-09-29 op /* Finds the node with the same key as elm */ \
129 83f0f95a 2022-09-29 op static inline struct type * \
130 83f0f95a 2022-09-29 op name##_SPLAY_FIND(struct name *head, struct type *elm) \
131 83f0f95a 2022-09-29 op { \
132 83f0f95a 2022-09-29 op if (SPLAY_EMPTY(head)) \
133 83f0f95a 2022-09-29 op return(NULL); \
134 83f0f95a 2022-09-29 op name##_SPLAY(head, elm); \
135 83f0f95a 2022-09-29 op if ((cmp)(elm, (head)->sph_root) == 0) \
136 83f0f95a 2022-09-29 op return (head->sph_root); \
137 83f0f95a 2022-09-29 op return (NULL); \
138 83f0f95a 2022-09-29 op } \
139 83f0f95a 2022-09-29 op \
140 83f0f95a 2022-09-29 op static inline struct type * \
141 83f0f95a 2022-09-29 op name##_SPLAY_NEXT(struct name *head, struct type *elm) \
142 83f0f95a 2022-09-29 op { \
143 83f0f95a 2022-09-29 op name##_SPLAY(head, elm); \
144 83f0f95a 2022-09-29 op if (SPLAY_RIGHT(elm, field) != NULL) { \
145 83f0f95a 2022-09-29 op elm = SPLAY_RIGHT(elm, field); \
146 83f0f95a 2022-09-29 op while (SPLAY_LEFT(elm, field) != NULL) { \
147 83f0f95a 2022-09-29 op elm = SPLAY_LEFT(elm, field); \
148 83f0f95a 2022-09-29 op } \
149 83f0f95a 2022-09-29 op } else \
150 83f0f95a 2022-09-29 op elm = NULL; \
151 83f0f95a 2022-09-29 op return (elm); \
152 83f0f95a 2022-09-29 op } \
153 83f0f95a 2022-09-29 op \
154 83f0f95a 2022-09-29 op static inline struct type * \
155 83f0f95a 2022-09-29 op name##_SPLAY_MIN_MAX(struct name *head, int val) \
156 83f0f95a 2022-09-29 op { \
157 83f0f95a 2022-09-29 op name##_SPLAY_MINMAX(head, val); \
158 83f0f95a 2022-09-29 op return (SPLAY_ROOT(head)); \
159 83f0f95a 2022-09-29 op }
160 83f0f95a 2022-09-29 op
161 83f0f95a 2022-09-29 op /* Main splay operation.
162 83f0f95a 2022-09-29 op * Moves node close to the key of elm to top
163 83f0f95a 2022-09-29 op */
164 83f0f95a 2022-09-29 op #define SPLAY_GENERATE(name, type, field, cmp) \
165 83f0f95a 2022-09-29 op struct type * \
166 83f0f95a 2022-09-29 op name##_SPLAY_INSERT(struct name *head, struct type *elm) \
167 83f0f95a 2022-09-29 op { \
168 83f0f95a 2022-09-29 op if (SPLAY_EMPTY(head)) { \
169 83f0f95a 2022-09-29 op SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
170 83f0f95a 2022-09-29 op } else { \
171 83f0f95a 2022-09-29 op int __comp; \
172 83f0f95a 2022-09-29 op name##_SPLAY(head, elm); \
173 83f0f95a 2022-09-29 op __comp = (cmp)(elm, (head)->sph_root); \
174 83f0f95a 2022-09-29 op if(__comp < 0) { \
175 83f0f95a 2022-09-29 op SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
176 83f0f95a 2022-09-29 op SPLAY_RIGHT(elm, field) = (head)->sph_root; \
177 83f0f95a 2022-09-29 op SPLAY_LEFT((head)->sph_root, field) = NULL; \
178 83f0f95a 2022-09-29 op } else if (__comp > 0) { \
179 83f0f95a 2022-09-29 op SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
180 83f0f95a 2022-09-29 op SPLAY_LEFT(elm, field) = (head)->sph_root; \
181 83f0f95a 2022-09-29 op SPLAY_RIGHT((head)->sph_root, field) = NULL; \
182 83f0f95a 2022-09-29 op } else \
183 83f0f95a 2022-09-29 op return ((head)->sph_root); \
184 83f0f95a 2022-09-29 op } \
185 83f0f95a 2022-09-29 op (head)->sph_root = (elm); \
186 83f0f95a 2022-09-29 op return (NULL); \
187 83f0f95a 2022-09-29 op } \
188 83f0f95a 2022-09-29 op \
189 83f0f95a 2022-09-29 op struct type * \
190 83f0f95a 2022-09-29 op name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
191 83f0f95a 2022-09-29 op { \
192 83f0f95a 2022-09-29 op struct type *__tmp; \
193 83f0f95a 2022-09-29 op if (SPLAY_EMPTY(head)) \
194 83f0f95a 2022-09-29 op return (NULL); \
195 83f0f95a 2022-09-29 op name##_SPLAY(head, elm); \
196 83f0f95a 2022-09-29 op if ((cmp)(elm, (head)->sph_root) == 0) { \
197 83f0f95a 2022-09-29 op if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
198 83f0f95a 2022-09-29 op (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
199 83f0f95a 2022-09-29 op } else { \
200 83f0f95a 2022-09-29 op __tmp = SPLAY_RIGHT((head)->sph_root, field); \
201 83f0f95a 2022-09-29 op (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
202 83f0f95a 2022-09-29 op name##_SPLAY(head, elm); \
203 83f0f95a 2022-09-29 op SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
204 83f0f95a 2022-09-29 op } \
205 83f0f95a 2022-09-29 op return (elm); \
206 83f0f95a 2022-09-29 op } \
207 83f0f95a 2022-09-29 op return (NULL); \
208 83f0f95a 2022-09-29 op } \
209 83f0f95a 2022-09-29 op \
210 83f0f95a 2022-09-29 op void \
211 83f0f95a 2022-09-29 op name##_SPLAY(struct name *head, struct type *elm) \
212 83f0f95a 2022-09-29 op { \
213 83f0f95a 2022-09-29 op struct type __node, *__left, *__right, *__tmp; \
214 83f0f95a 2022-09-29 op int __comp; \
215 83f0f95a 2022-09-29 op \
216 83f0f95a 2022-09-29 op SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
217 83f0f95a 2022-09-29 op __left = __right = &__node; \
218 83f0f95a 2022-09-29 op \
219 83f0f95a 2022-09-29 op while ((__comp = (cmp)(elm, (head)->sph_root))) { \
220 83f0f95a 2022-09-29 op if (__comp < 0) { \
221 83f0f95a 2022-09-29 op __tmp = SPLAY_LEFT((head)->sph_root, field); \
222 83f0f95a 2022-09-29 op if (__tmp == NULL) \
223 83f0f95a 2022-09-29 op break; \
224 83f0f95a 2022-09-29 op if ((cmp)(elm, __tmp) < 0){ \
225 83f0f95a 2022-09-29 op SPLAY_ROTATE_RIGHT(head, __tmp, field); \
226 83f0f95a 2022-09-29 op if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
227 83f0f95a 2022-09-29 op break; \
228 83f0f95a 2022-09-29 op } \
229 83f0f95a 2022-09-29 op SPLAY_LINKLEFT(head, __right, field); \
230 83f0f95a 2022-09-29 op } else if (__comp > 0) { \
231 83f0f95a 2022-09-29 op __tmp = SPLAY_RIGHT((head)->sph_root, field); \
232 83f0f95a 2022-09-29 op if (__tmp == NULL) \
233 83f0f95a 2022-09-29 op break; \
234 83f0f95a 2022-09-29 op if ((cmp)(elm, __tmp) > 0){ \
235 83f0f95a 2022-09-29 op SPLAY_ROTATE_LEFT(head, __tmp, field); \
236 83f0f95a 2022-09-29 op if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
237 83f0f95a 2022-09-29 op break; \
238 83f0f95a 2022-09-29 op } \
239 83f0f95a 2022-09-29 op SPLAY_LINKRIGHT(head, __left, field); \
240 83f0f95a 2022-09-29 op } \
241 83f0f95a 2022-09-29 op } \
242 83f0f95a 2022-09-29 op SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
243 83f0f95a 2022-09-29 op } \
244 83f0f95a 2022-09-29 op \
245 83f0f95a 2022-09-29 op /* Splay with either the minimum or the maximum element \
246 83f0f95a 2022-09-29 op * Used to find minimum or maximum element in tree. \
247 83f0f95a 2022-09-29 op */ \
248 83f0f95a 2022-09-29 op void name##_SPLAY_MINMAX(struct name *head, int __comp) \
249 83f0f95a 2022-09-29 op { \
250 83f0f95a 2022-09-29 op struct type __node, *__left, *__right, *__tmp; \
251 83f0f95a 2022-09-29 op \
252 83f0f95a 2022-09-29 op SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
253 83f0f95a 2022-09-29 op __left = __right = &__node; \
254 83f0f95a 2022-09-29 op \
255 83f0f95a 2022-09-29 op while (1) { \
256 83f0f95a 2022-09-29 op if (__comp < 0) { \
257 83f0f95a 2022-09-29 op __tmp = SPLAY_LEFT((head)->sph_root, field); \
258 83f0f95a 2022-09-29 op if (__tmp == NULL) \
259 83f0f95a 2022-09-29 op break; \
260 83f0f95a 2022-09-29 op if (__comp < 0){ \
261 83f0f95a 2022-09-29 op SPLAY_ROTATE_RIGHT(head, __tmp, field); \
262 83f0f95a 2022-09-29 op if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
263 83f0f95a 2022-09-29 op break; \
264 83f0f95a 2022-09-29 op } \
265 83f0f95a 2022-09-29 op SPLAY_LINKLEFT(head, __right, field); \
266 83f0f95a 2022-09-29 op } else if (__comp > 0) { \
267 83f0f95a 2022-09-29 op __tmp = SPLAY_RIGHT((head)->sph_root, field); \
268 83f0f95a 2022-09-29 op if (__tmp == NULL) \
269 83f0f95a 2022-09-29 op break; \
270 83f0f95a 2022-09-29 op if (__comp > 0) { \
271 83f0f95a 2022-09-29 op SPLAY_ROTATE_LEFT(head, __tmp, field); \
272 83f0f95a 2022-09-29 op if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
273 83f0f95a 2022-09-29 op break; \
274 83f0f95a 2022-09-29 op } \
275 83f0f95a 2022-09-29 op SPLAY_LINKRIGHT(head, __left, field); \
276 83f0f95a 2022-09-29 op } \
277 83f0f95a 2022-09-29 op } \
278 83f0f95a 2022-09-29 op SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
279 83f0f95a 2022-09-29 op }
280 83f0f95a 2022-09-29 op
281 83f0f95a 2022-09-29 op #define SPLAY_NEGINF -1
282 83f0f95a 2022-09-29 op #define SPLAY_INF 1
283 83f0f95a 2022-09-29 op
284 83f0f95a 2022-09-29 op #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
285 83f0f95a 2022-09-29 op #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
286 83f0f95a 2022-09-29 op #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
287 83f0f95a 2022-09-29 op #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
288 83f0f95a 2022-09-29 op #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
289 83f0f95a 2022-09-29 op : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
290 83f0f95a 2022-09-29 op #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
291 83f0f95a 2022-09-29 op : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
292 83f0f95a 2022-09-29 op
293 83f0f95a 2022-09-29 op #define SPLAY_FOREACH(x, name, head) \
294 83f0f95a 2022-09-29 op for ((x) = SPLAY_MIN(name, head); \
295 83f0f95a 2022-09-29 op (x) != NULL; \
296 83f0f95a 2022-09-29 op (x) = SPLAY_NEXT(name, head, x))
297 83f0f95a 2022-09-29 op
298 83f0f95a 2022-09-29 op /* Macros that define a red-black tree */
299 83f0f95a 2022-09-29 op #define RB_HEAD(name, type) \
300 83f0f95a 2022-09-29 op struct name { \
301 83f0f95a 2022-09-29 op struct type *rbh_root; /* root of the tree */ \
302 83f0f95a 2022-09-29 op }
303 83f0f95a 2022-09-29 op
304 83f0f95a 2022-09-29 op #define RB_INITIALIZER(root) \
305 83f0f95a 2022-09-29 op { NULL }
306 83f0f95a 2022-09-29 op
307 83f0f95a 2022-09-29 op #define RB_INIT(root) do { \
308 83f0f95a 2022-09-29 op (root)->rbh_root = NULL; \
309 83f0f95a 2022-09-29 op } while (0)
310 83f0f95a 2022-09-29 op
311 83f0f95a 2022-09-29 op #define RB_BLACK 0
312 83f0f95a 2022-09-29 op #define RB_RED 1
313 83f0f95a 2022-09-29 op #define RB_ENTRY(type) \
314 83f0f95a 2022-09-29 op struct { \
315 83f0f95a 2022-09-29 op struct type *rbe_left; /* left element */ \
316 83f0f95a 2022-09-29 op struct type *rbe_right; /* right element */ \
317 83f0f95a 2022-09-29 op struct type *rbe_parent; /* parent element */ \
318 83f0f95a 2022-09-29 op int rbe_color; /* node color */ \
319 83f0f95a 2022-09-29 op }
320 83f0f95a 2022-09-29 op
321 83f0f95a 2022-09-29 op #define RB_LEFT(elm, field) (elm)->field.rbe_left
322 83f0f95a 2022-09-29 op #define RB_RIGHT(elm, field) (elm)->field.rbe_right
323 83f0f95a 2022-09-29 op #define RB_PARENT(elm, field) (elm)->field.rbe_parent
324 83f0f95a 2022-09-29 op #define RB_COLOR(elm, field) (elm)->field.rbe_color
325 83f0f95a 2022-09-29 op #define RB_ROOT(head) (head)->rbh_root
326 83f0f95a 2022-09-29 op #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
327 83f0f95a 2022-09-29 op
328 83f0f95a 2022-09-29 op #define RB_SET(elm, parent, field) do { \
329 83f0f95a 2022-09-29 op RB_PARENT(elm, field) = parent; \
330 83f0f95a 2022-09-29 op RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
331 83f0f95a 2022-09-29 op RB_COLOR(elm, field) = RB_RED; \
332 83f0f95a 2022-09-29 op } while (0)
333 83f0f95a 2022-09-29 op
334 83f0f95a 2022-09-29 op #define RB_SET_BLACKRED(black, red, field) do { \
335 83f0f95a 2022-09-29 op RB_COLOR(black, field) = RB_BLACK; \
336 83f0f95a 2022-09-29 op RB_COLOR(red, field) = RB_RED; \
337 83f0f95a 2022-09-29 op } while (0)
338 83f0f95a 2022-09-29 op
339 83f0f95a 2022-09-29 op #ifndef RB_AUGMENT
340 83f0f95a 2022-09-29 op #define RB_AUGMENT(x) do {} while (0)
341 83f0f95a 2022-09-29 op #endif
342 83f0f95a 2022-09-29 op
343 83f0f95a 2022-09-29 op #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
344 83f0f95a 2022-09-29 op (tmp) = RB_RIGHT(elm, field); \
345 83f0f95a 2022-09-29 op if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
346 83f0f95a 2022-09-29 op RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
347 83f0f95a 2022-09-29 op } \
348 83f0f95a 2022-09-29 op RB_AUGMENT(elm); \
349 83f0f95a 2022-09-29 op if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
350 83f0f95a 2022-09-29 op if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
351 83f0f95a 2022-09-29 op RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
352 83f0f95a 2022-09-29 op else \
353 83f0f95a 2022-09-29 op RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
354 83f0f95a 2022-09-29 op } else \
355 83f0f95a 2022-09-29 op (head)->rbh_root = (tmp); \
356 83f0f95a 2022-09-29 op RB_LEFT(tmp, field) = (elm); \
357 83f0f95a 2022-09-29 op RB_PARENT(elm, field) = (tmp); \
358 83f0f95a 2022-09-29 op RB_AUGMENT(tmp); \
359 83f0f95a 2022-09-29 op if ((RB_PARENT(tmp, field))) \
360 83f0f95a 2022-09-29 op RB_AUGMENT(RB_PARENT(tmp, field)); \
361 83f0f95a 2022-09-29 op } while (0)
362 83f0f95a 2022-09-29 op
363 83f0f95a 2022-09-29 op #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
364 83f0f95a 2022-09-29 op (tmp) = RB_LEFT(elm, field); \
365 83f0f95a 2022-09-29 op if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
366 83f0f95a 2022-09-29 op RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
367 83f0f95a 2022-09-29 op } \
368 83f0f95a 2022-09-29 op RB_AUGMENT(elm); \
369 83f0f95a 2022-09-29 op if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
370 83f0f95a 2022-09-29 op if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
371 83f0f95a 2022-09-29 op RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
372 83f0f95a 2022-09-29 op else \
373 83f0f95a 2022-09-29 op RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
374 83f0f95a 2022-09-29 op } else \
375 83f0f95a 2022-09-29 op (head)->rbh_root = (tmp); \
376 83f0f95a 2022-09-29 op RB_RIGHT(tmp, field) = (elm); \
377 83f0f95a 2022-09-29 op RB_PARENT(elm, field) = (tmp); \
378 83f0f95a 2022-09-29 op RB_AUGMENT(tmp); \
379 83f0f95a 2022-09-29 op if ((RB_PARENT(tmp, field))) \
380 83f0f95a 2022-09-29 op RB_AUGMENT(RB_PARENT(tmp, field)); \
381 83f0f95a 2022-09-29 op } while (0)
382 83f0f95a 2022-09-29 op
383 83f0f95a 2022-09-29 op /* Generates prototypes and inline functions */
384 83f0f95a 2022-09-29 op #define RB_PROTOTYPE(name, type, field, cmp) \
385 83f0f95a 2022-09-29 op RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
386 83f0f95a 2022-09-29 op #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
387 83f0f95a 2022-09-29 op RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
388 83f0f95a 2022-09-29 op #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
389 83f0f95a 2022-09-29 op attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
390 83f0f95a 2022-09-29 op attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
391 83f0f95a 2022-09-29 op attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
392 83f0f95a 2022-09-29 op attr struct type *name##_RB_INSERT(struct name *, struct type *); \
393 83f0f95a 2022-09-29 op attr struct type *name##_RB_FIND(struct name *, struct type *); \
394 83f0f95a 2022-09-29 op attr struct type *name##_RB_NFIND(struct name *, struct type *); \
395 83f0f95a 2022-09-29 op attr struct type *name##_RB_NEXT(struct type *); \
396 83f0f95a 2022-09-29 op attr struct type *name##_RB_PREV(struct type *); \
397 83f0f95a 2022-09-29 op attr struct type *name##_RB_MINMAX(struct name *, int); \
398 83f0f95a 2022-09-29 op \
399 83f0f95a 2022-09-29 op
400 83f0f95a 2022-09-29 op /* Main rb operation.
401 83f0f95a 2022-09-29 op * Moves node close to the key of elm to top
402 83f0f95a 2022-09-29 op */
403 83f0f95a 2022-09-29 op #define RB_GENERATE(name, type, field, cmp) \
404 83f0f95a 2022-09-29 op RB_GENERATE_INTERNAL(name, type, field, cmp,)
405 83f0f95a 2022-09-29 op #define RB_GENERATE_STATIC(name, type, field, cmp) \
406 83f0f95a 2022-09-29 op RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
407 83f0f95a 2022-09-29 op #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
408 83f0f95a 2022-09-29 op attr void \
409 83f0f95a 2022-09-29 op name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
410 83f0f95a 2022-09-29 op { \
411 83f0f95a 2022-09-29 op struct type *parent, *gparent, *tmp; \
412 83f0f95a 2022-09-29 op while ((parent = RB_PARENT(elm, field)) && \
413 83f0f95a 2022-09-29 op RB_COLOR(parent, field) == RB_RED) { \
414 83f0f95a 2022-09-29 op gparent = RB_PARENT(parent, field); \
415 83f0f95a 2022-09-29 op if (parent == RB_LEFT(gparent, field)) { \
416 83f0f95a 2022-09-29 op tmp = RB_RIGHT(gparent, field); \
417 83f0f95a 2022-09-29 op if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
418 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_BLACK; \
419 83f0f95a 2022-09-29 op RB_SET_BLACKRED(parent, gparent, field);\
420 83f0f95a 2022-09-29 op elm = gparent; \
421 83f0f95a 2022-09-29 op continue; \
422 83f0f95a 2022-09-29 op } \
423 83f0f95a 2022-09-29 op if (RB_RIGHT(parent, field) == elm) { \
424 83f0f95a 2022-09-29 op RB_ROTATE_LEFT(head, parent, tmp, field);\
425 83f0f95a 2022-09-29 op tmp = parent; \
426 83f0f95a 2022-09-29 op parent = elm; \
427 83f0f95a 2022-09-29 op elm = tmp; \
428 83f0f95a 2022-09-29 op } \
429 83f0f95a 2022-09-29 op RB_SET_BLACKRED(parent, gparent, field); \
430 83f0f95a 2022-09-29 op RB_ROTATE_RIGHT(head, gparent, tmp, field); \
431 83f0f95a 2022-09-29 op } else { \
432 83f0f95a 2022-09-29 op tmp = RB_LEFT(gparent, field); \
433 83f0f95a 2022-09-29 op if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
434 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_BLACK; \
435 83f0f95a 2022-09-29 op RB_SET_BLACKRED(parent, gparent, field);\
436 83f0f95a 2022-09-29 op elm = gparent; \
437 83f0f95a 2022-09-29 op continue; \
438 83f0f95a 2022-09-29 op } \
439 83f0f95a 2022-09-29 op if (RB_LEFT(parent, field) == elm) { \
440 83f0f95a 2022-09-29 op RB_ROTATE_RIGHT(head, parent, tmp, field);\
441 83f0f95a 2022-09-29 op tmp = parent; \
442 83f0f95a 2022-09-29 op parent = elm; \
443 83f0f95a 2022-09-29 op elm = tmp; \
444 83f0f95a 2022-09-29 op } \
445 83f0f95a 2022-09-29 op RB_SET_BLACKRED(parent, gparent, field); \
446 83f0f95a 2022-09-29 op RB_ROTATE_LEFT(head, gparent, tmp, field); \
447 83f0f95a 2022-09-29 op } \
448 83f0f95a 2022-09-29 op } \
449 83f0f95a 2022-09-29 op RB_COLOR(head->rbh_root, field) = RB_BLACK; \
450 83f0f95a 2022-09-29 op } \
451 83f0f95a 2022-09-29 op \
452 83f0f95a 2022-09-29 op attr void \
453 83f0f95a 2022-09-29 op name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
454 83f0f95a 2022-09-29 op { \
455 83f0f95a 2022-09-29 op struct type *tmp; \
456 83f0f95a 2022-09-29 op while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
457 83f0f95a 2022-09-29 op elm != RB_ROOT(head)) { \
458 83f0f95a 2022-09-29 op if (RB_LEFT(parent, field) == elm) { \
459 83f0f95a 2022-09-29 op tmp = RB_RIGHT(parent, field); \
460 83f0f95a 2022-09-29 op if (RB_COLOR(tmp, field) == RB_RED) { \
461 83f0f95a 2022-09-29 op RB_SET_BLACKRED(tmp, parent, field); \
462 83f0f95a 2022-09-29 op RB_ROTATE_LEFT(head, parent, tmp, field);\
463 83f0f95a 2022-09-29 op tmp = RB_RIGHT(parent, field); \
464 83f0f95a 2022-09-29 op } \
465 83f0f95a 2022-09-29 op if ((RB_LEFT(tmp, field) == NULL || \
466 83f0f95a 2022-09-29 op RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
467 83f0f95a 2022-09-29 op (RB_RIGHT(tmp, field) == NULL || \
468 83f0f95a 2022-09-29 op RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
469 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_RED; \
470 83f0f95a 2022-09-29 op elm = parent; \
471 83f0f95a 2022-09-29 op parent = RB_PARENT(elm, field); \
472 83f0f95a 2022-09-29 op } else { \
473 83f0f95a 2022-09-29 op if (RB_RIGHT(tmp, field) == NULL || \
474 83f0f95a 2022-09-29 op RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
475 83f0f95a 2022-09-29 op struct type *oleft; \
476 83f0f95a 2022-09-29 op if ((oleft = RB_LEFT(tmp, field)))\
477 83f0f95a 2022-09-29 op RB_COLOR(oleft, field) = RB_BLACK;\
478 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_RED; \
479 83f0f95a 2022-09-29 op RB_ROTATE_RIGHT(head, tmp, oleft, field);\
480 83f0f95a 2022-09-29 op tmp = RB_RIGHT(parent, field); \
481 83f0f95a 2022-09-29 op } \
482 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
483 83f0f95a 2022-09-29 op RB_COLOR(parent, field) = RB_BLACK; \
484 83f0f95a 2022-09-29 op if (RB_RIGHT(tmp, field)) \
485 83f0f95a 2022-09-29 op RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
486 83f0f95a 2022-09-29 op RB_ROTATE_LEFT(head, parent, tmp, field);\
487 83f0f95a 2022-09-29 op elm = RB_ROOT(head); \
488 83f0f95a 2022-09-29 op break; \
489 83f0f95a 2022-09-29 op } \
490 83f0f95a 2022-09-29 op } else { \
491 83f0f95a 2022-09-29 op tmp = RB_LEFT(parent, field); \
492 83f0f95a 2022-09-29 op if (RB_COLOR(tmp, field) == RB_RED) { \
493 83f0f95a 2022-09-29 op RB_SET_BLACKRED(tmp, parent, field); \
494 83f0f95a 2022-09-29 op RB_ROTATE_RIGHT(head, parent, tmp, field);\
495 83f0f95a 2022-09-29 op tmp = RB_LEFT(parent, field); \
496 83f0f95a 2022-09-29 op } \
497 83f0f95a 2022-09-29 op if ((RB_LEFT(tmp, field) == NULL || \
498 83f0f95a 2022-09-29 op RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
499 83f0f95a 2022-09-29 op (RB_RIGHT(tmp, field) == NULL || \
500 83f0f95a 2022-09-29 op RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
501 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_RED; \
502 83f0f95a 2022-09-29 op elm = parent; \
503 83f0f95a 2022-09-29 op parent = RB_PARENT(elm, field); \
504 83f0f95a 2022-09-29 op } else { \
505 83f0f95a 2022-09-29 op if (RB_LEFT(tmp, field) == NULL || \
506 83f0f95a 2022-09-29 op RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
507 83f0f95a 2022-09-29 op struct type *oright; \
508 83f0f95a 2022-09-29 op if ((oright = RB_RIGHT(tmp, field)))\
509 83f0f95a 2022-09-29 op RB_COLOR(oright, field) = RB_BLACK;\
510 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_RED; \
511 83f0f95a 2022-09-29 op RB_ROTATE_LEFT(head, tmp, oright, field);\
512 83f0f95a 2022-09-29 op tmp = RB_LEFT(parent, field); \
513 83f0f95a 2022-09-29 op } \
514 83f0f95a 2022-09-29 op RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
515 83f0f95a 2022-09-29 op RB_COLOR(parent, field) = RB_BLACK; \
516 83f0f95a 2022-09-29 op if (RB_LEFT(tmp, field)) \
517 83f0f95a 2022-09-29 op RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
518 83f0f95a 2022-09-29 op RB_ROTATE_RIGHT(head, parent, tmp, field);\
519 83f0f95a 2022-09-29 op elm = RB_ROOT(head); \
520 83f0f95a 2022-09-29 op break; \
521 83f0f95a 2022-09-29 op } \
522 83f0f95a 2022-09-29 op } \
523 83f0f95a 2022-09-29 op } \
524 83f0f95a 2022-09-29 op if (elm) \
525 83f0f95a 2022-09-29 op RB_COLOR(elm, field) = RB_BLACK; \
526 83f0f95a 2022-09-29 op } \
527 83f0f95a 2022-09-29 op \
528 83f0f95a 2022-09-29 op attr struct type * \
529 83f0f95a 2022-09-29 op name##_RB_REMOVE(struct name *head, struct type *elm) \
530 83f0f95a 2022-09-29 op { \
531 83f0f95a 2022-09-29 op struct type *child, *parent, *old = elm; \
532 83f0f95a 2022-09-29 op int color; \
533 83f0f95a 2022-09-29 op if (RB_LEFT(elm, field) == NULL) \
534 83f0f95a 2022-09-29 op child = RB_RIGHT(elm, field); \
535 83f0f95a 2022-09-29 op else if (RB_RIGHT(elm, field) == NULL) \
536 83f0f95a 2022-09-29 op child = RB_LEFT(elm, field); \
537 83f0f95a 2022-09-29 op else { \
538 83f0f95a 2022-09-29 op struct type *left; \
539 83f0f95a 2022-09-29 op elm = RB_RIGHT(elm, field); \
540 83f0f95a 2022-09-29 op while ((left = RB_LEFT(elm, field))) \
541 83f0f95a 2022-09-29 op elm = left; \
542 83f0f95a 2022-09-29 op child = RB_RIGHT(elm, field); \
543 83f0f95a 2022-09-29 op parent = RB_PARENT(elm, field); \
544 83f0f95a 2022-09-29 op color = RB_COLOR(elm, field); \
545 83f0f95a 2022-09-29 op if (child) \
546 83f0f95a 2022-09-29 op RB_PARENT(child, field) = parent; \
547 83f0f95a 2022-09-29 op if (parent) { \
548 83f0f95a 2022-09-29 op if (RB_LEFT(parent, field) == elm) \
549 83f0f95a 2022-09-29 op RB_LEFT(parent, field) = child; \
550 83f0f95a 2022-09-29 op else \
551 83f0f95a 2022-09-29 op RB_RIGHT(parent, field) = child; \
552 83f0f95a 2022-09-29 op RB_AUGMENT(parent); \
553 83f0f95a 2022-09-29 op } else \
554 83f0f95a 2022-09-29 op RB_ROOT(head) = child; \
555 83f0f95a 2022-09-29 op if (RB_PARENT(elm, field) == old) \
556 83f0f95a 2022-09-29 op parent = elm; \
557 83f0f95a 2022-09-29 op (elm)->field = (old)->field; \
558 83f0f95a 2022-09-29 op if (RB_PARENT(old, field)) { \
559 83f0f95a 2022-09-29 op if (RB_LEFT(RB_PARENT(old, field), field) == old)\
560 83f0f95a 2022-09-29 op RB_LEFT(RB_PARENT(old, field), field) = elm;\
561 83f0f95a 2022-09-29 op else \
562 83f0f95a 2022-09-29 op RB_RIGHT(RB_PARENT(old, field), field) = elm;\
563 83f0f95a 2022-09-29 op RB_AUGMENT(RB_PARENT(old, field)); \
564 83f0f95a 2022-09-29 op } else \
565 83f0f95a 2022-09-29 op RB_ROOT(head) = elm; \
566 83f0f95a 2022-09-29 op RB_PARENT(RB_LEFT(old, field), field) = elm; \
567 83f0f95a 2022-09-29 op if (RB_RIGHT(old, field)) \
568 83f0f95a 2022-09-29 op RB_PARENT(RB_RIGHT(old, field), field) = elm; \
569 83f0f95a 2022-09-29 op if (parent) { \
570 83f0f95a 2022-09-29 op left = parent; \
571 83f0f95a 2022-09-29 op do { \
572 83f0f95a 2022-09-29 op RB_AUGMENT(left); \
573 83f0f95a 2022-09-29 op } while ((left = RB_PARENT(left, field))); \
574 83f0f95a 2022-09-29 op } \
575 83f0f95a 2022-09-29 op goto color; \
576 83f0f95a 2022-09-29 op } \
577 83f0f95a 2022-09-29 op parent = RB_PARENT(elm, field); \
578 83f0f95a 2022-09-29 op color = RB_COLOR(elm, field); \
579 83f0f95a 2022-09-29 op if (child) \
580 83f0f95a 2022-09-29 op RB_PARENT(child, field) = parent; \
581 83f0f95a 2022-09-29 op if (parent) { \
582 83f0f95a 2022-09-29 op if (RB_LEFT(parent, field) == elm) \
583 83f0f95a 2022-09-29 op RB_LEFT(parent, field) = child; \
584 83f0f95a 2022-09-29 op else \
585 83f0f95a 2022-09-29 op RB_RIGHT(parent, field) = child; \
586 83f0f95a 2022-09-29 op RB_AUGMENT(parent); \
587 83f0f95a 2022-09-29 op } else \
588 83f0f95a 2022-09-29 op RB_ROOT(head) = child; \
589 83f0f95a 2022-09-29 op color: \
590 83f0f95a 2022-09-29 op if (color == RB_BLACK) \
591 83f0f95a 2022-09-29 op name##_RB_REMOVE_COLOR(head, parent, child); \
592 83f0f95a 2022-09-29 op return (old); \
593 83f0f95a 2022-09-29 op } \
594 83f0f95a 2022-09-29 op \
595 83f0f95a 2022-09-29 op /* Inserts a node into the RB tree */ \
596 83f0f95a 2022-09-29 op attr struct type * \
597 83f0f95a 2022-09-29 op name##_RB_INSERT(struct name *head, struct type *elm) \
598 83f0f95a 2022-09-29 op { \
599 83f0f95a 2022-09-29 op struct type *tmp; \
600 83f0f95a 2022-09-29 op struct type *parent = NULL; \
601 83f0f95a 2022-09-29 op int comp = 0; \
602 83f0f95a 2022-09-29 op tmp = RB_ROOT(head); \
603 83f0f95a 2022-09-29 op while (tmp) { \
604 83f0f95a 2022-09-29 op parent = tmp; \
605 83f0f95a 2022-09-29 op comp = (cmp)(elm, parent); \
606 83f0f95a 2022-09-29 op if (comp < 0) \
607 83f0f95a 2022-09-29 op tmp = RB_LEFT(tmp, field); \
608 83f0f95a 2022-09-29 op else if (comp > 0) \
609 83f0f95a 2022-09-29 op tmp = RB_RIGHT(tmp, field); \
610 83f0f95a 2022-09-29 op else \
611 83f0f95a 2022-09-29 op return (tmp); \
612 83f0f95a 2022-09-29 op } \
613 83f0f95a 2022-09-29 op RB_SET(elm, parent, field); \
614 83f0f95a 2022-09-29 op if (parent != NULL) { \
615 83f0f95a 2022-09-29 op if (comp < 0) \
616 83f0f95a 2022-09-29 op RB_LEFT(parent, field) = elm; \
617 83f0f95a 2022-09-29 op else \
618 83f0f95a 2022-09-29 op RB_RIGHT(parent, field) = elm; \
619 83f0f95a 2022-09-29 op RB_AUGMENT(parent); \
620 83f0f95a 2022-09-29 op } else \
621 83f0f95a 2022-09-29 op RB_ROOT(head) = elm; \
622 83f0f95a 2022-09-29 op name##_RB_INSERT_COLOR(head, elm); \
623 83f0f95a 2022-09-29 op return (NULL); \
624 83f0f95a 2022-09-29 op } \
625 83f0f95a 2022-09-29 op \
626 83f0f95a 2022-09-29 op /* Finds the node with the same key as elm */ \
627 83f0f95a 2022-09-29 op attr struct type * \
628 83f0f95a 2022-09-29 op name##_RB_FIND(struct name *head, struct type *elm) \
629 83f0f95a 2022-09-29 op { \
630 83f0f95a 2022-09-29 op struct type *tmp = RB_ROOT(head); \
631 83f0f95a 2022-09-29 op int comp; \
632 83f0f95a 2022-09-29 op while (tmp) { \
633 83f0f95a 2022-09-29 op comp = cmp(elm, tmp); \
634 83f0f95a 2022-09-29 op if (comp < 0) \
635 83f0f95a 2022-09-29 op tmp = RB_LEFT(tmp, field); \
636 83f0f95a 2022-09-29 op else if (comp > 0) \
637 83f0f95a 2022-09-29 op tmp = RB_RIGHT(tmp, field); \
638 83f0f95a 2022-09-29 op else \
639 83f0f95a 2022-09-29 op return (tmp); \
640 83f0f95a 2022-09-29 op } \
641 83f0f95a 2022-09-29 op return (NULL); \
642 83f0f95a 2022-09-29 op } \
643 83f0f95a 2022-09-29 op \
644 83f0f95a 2022-09-29 op /* Finds the first node greater than or equal to the search key */ \
645 83f0f95a 2022-09-29 op attr struct type * \
646 83f0f95a 2022-09-29 op name##_RB_NFIND(struct name *head, struct type *elm) \
647 83f0f95a 2022-09-29 op { \
648 83f0f95a 2022-09-29 op struct type *tmp = RB_ROOT(head); \
649 83f0f95a 2022-09-29 op struct type *res = NULL; \
650 83f0f95a 2022-09-29 op int comp; \
651 83f0f95a 2022-09-29 op while (tmp) { \
652 83f0f95a 2022-09-29 op comp = cmp(elm, tmp); \
653 83f0f95a 2022-09-29 op if (comp < 0) { \
654 83f0f95a 2022-09-29 op res = tmp; \
655 83f0f95a 2022-09-29 op tmp = RB_LEFT(tmp, field); \
656 83f0f95a 2022-09-29 op } \
657 83f0f95a 2022-09-29 op else if (comp > 0) \
658 83f0f95a 2022-09-29 op tmp = RB_RIGHT(tmp, field); \
659 83f0f95a 2022-09-29 op else \
660 83f0f95a 2022-09-29 op return (tmp); \
661 83f0f95a 2022-09-29 op } \
662 83f0f95a 2022-09-29 op return (res); \
663 83f0f95a 2022-09-29 op } \
664 83f0f95a 2022-09-29 op \
665 83f0f95a 2022-09-29 op /* ARGSUSED */ \
666 83f0f95a 2022-09-29 op attr struct type * \
667 83f0f95a 2022-09-29 op name##_RB_NEXT(struct type *elm) \
668 83f0f95a 2022-09-29 op { \
669 83f0f95a 2022-09-29 op if (RB_RIGHT(elm, field)) { \
670 83f0f95a 2022-09-29 op elm = RB_RIGHT(elm, field); \
671 83f0f95a 2022-09-29 op while (RB_LEFT(elm, field)) \
672 83f0f95a 2022-09-29 op elm = RB_LEFT(elm, field); \
673 83f0f95a 2022-09-29 op } else { \
674 83f0f95a 2022-09-29 op if (RB_PARENT(elm, field) && \
675 83f0f95a 2022-09-29 op (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
676 83f0f95a 2022-09-29 op elm = RB_PARENT(elm, field); \
677 83f0f95a 2022-09-29 op else { \
678 83f0f95a 2022-09-29 op while (RB_PARENT(elm, field) && \
679 83f0f95a 2022-09-29 op (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
680 83f0f95a 2022-09-29 op elm = RB_PARENT(elm, field); \
681 83f0f95a 2022-09-29 op elm = RB_PARENT(elm, field); \
682 83f0f95a 2022-09-29 op } \
683 83f0f95a 2022-09-29 op } \
684 83f0f95a 2022-09-29 op return (elm); \
685 83f0f95a 2022-09-29 op } \
686 83f0f95a 2022-09-29 op \
687 83f0f95a 2022-09-29 op /* ARGSUSED */ \
688 83f0f95a 2022-09-29 op attr struct type * \
689 83f0f95a 2022-09-29 op name##_RB_PREV(struct type *elm) \
690 83f0f95a 2022-09-29 op { \
691 83f0f95a 2022-09-29 op if (RB_LEFT(elm, field)) { \
692 83f0f95a 2022-09-29 op elm = RB_LEFT(elm, field); \
693 83f0f95a 2022-09-29 op while (RB_RIGHT(elm, field)) \
694 83f0f95a 2022-09-29 op elm = RB_RIGHT(elm, field); \
695 83f0f95a 2022-09-29 op } else { \
696 83f0f95a 2022-09-29 op if (RB_PARENT(elm, field) && \
697 83f0f95a 2022-09-29 op (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
698 83f0f95a 2022-09-29 op elm = RB_PARENT(elm, field); \
699 83f0f95a 2022-09-29 op else { \
700 83f0f95a 2022-09-29 op while (RB_PARENT(elm, field) && \
701 83f0f95a 2022-09-29 op (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
702 83f0f95a 2022-09-29 op elm = RB_PARENT(elm, field); \
703 83f0f95a 2022-09-29 op elm = RB_PARENT(elm, field); \
704 83f0f95a 2022-09-29 op } \
705 83f0f95a 2022-09-29 op } \
706 83f0f95a 2022-09-29 op return (elm); \
707 83f0f95a 2022-09-29 op } \
708 83f0f95a 2022-09-29 op \
709 83f0f95a 2022-09-29 op attr struct type * \
710 83f0f95a 2022-09-29 op name##_RB_MINMAX(struct name *head, int val) \
711 83f0f95a 2022-09-29 op { \
712 83f0f95a 2022-09-29 op struct type *tmp = RB_ROOT(head); \
713 83f0f95a 2022-09-29 op struct type *parent = NULL; \
714 83f0f95a 2022-09-29 op while (tmp) { \
715 83f0f95a 2022-09-29 op parent = tmp; \
716 83f0f95a 2022-09-29 op if (val < 0) \
717 83f0f95a 2022-09-29 op tmp = RB_LEFT(tmp, field); \
718 83f0f95a 2022-09-29 op else \
719 83f0f95a 2022-09-29 op tmp = RB_RIGHT(tmp, field); \
720 83f0f95a 2022-09-29 op } \
721 83f0f95a 2022-09-29 op return (parent); \
722 83f0f95a 2022-09-29 op }
723 83f0f95a 2022-09-29 op
724 83f0f95a 2022-09-29 op #define RB_NEGINF -1
725 83f0f95a 2022-09-29 op #define RB_INF 1
726 83f0f95a 2022-09-29 op
727 83f0f95a 2022-09-29 op #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
728 83f0f95a 2022-09-29 op #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
729 83f0f95a 2022-09-29 op #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
730 83f0f95a 2022-09-29 op #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
731 83f0f95a 2022-09-29 op #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
732 83f0f95a 2022-09-29 op #define RB_PREV(name, x, y) name##_RB_PREV(y)
733 83f0f95a 2022-09-29 op #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
734 83f0f95a 2022-09-29 op #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
735 83f0f95a 2022-09-29 op
736 83f0f95a 2022-09-29 op #define RB_FOREACH(x, name, head) \
737 83f0f95a 2022-09-29 op for ((x) = RB_MIN(name, head); \
738 83f0f95a 2022-09-29 op (x) != NULL; \
739 83f0f95a 2022-09-29 op (x) = name##_RB_NEXT(x))
740 83f0f95a 2022-09-29 op
741 83f0f95a 2022-09-29 op #define RB_FOREACH_SAFE(x, name, head, y) \
742 83f0f95a 2022-09-29 op for ((x) = RB_MIN(name, head); \
743 83f0f95a 2022-09-29 op ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
744 83f0f95a 2022-09-29 op (x) = (y))
745 83f0f95a 2022-09-29 op
746 83f0f95a 2022-09-29 op #define RB_FOREACH_REVERSE(x, name, head) \
747 83f0f95a 2022-09-29 op for ((x) = RB_MAX(name, head); \
748 83f0f95a 2022-09-29 op (x) != NULL; \
749 83f0f95a 2022-09-29 op (x) = name##_RB_PREV(x))
750 83f0f95a 2022-09-29 op
751 83f0f95a 2022-09-29 op #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
752 83f0f95a 2022-09-29 op for ((x) = RB_MAX(name, head); \
753 83f0f95a 2022-09-29 op ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
754 83f0f95a 2022-09-29 op (x) = (y))
755 83f0f95a 2022-09-29 op
756 83f0f95a 2022-09-29 op
757 83f0f95a 2022-09-29 op /*
758 83f0f95a 2022-09-29 op * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
759 83f0f95a 2022-09-29 op *
760 83f0f95a 2022-09-29 op * Permission to use, copy, modify, and distribute this software for any
761 83f0f95a 2022-09-29 op * purpose with or without fee is hereby granted, provided that the above
762 83f0f95a 2022-09-29 op * copyright notice and this permission notice appear in all copies.
763 83f0f95a 2022-09-29 op *
764 83f0f95a 2022-09-29 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
765 83f0f95a 2022-09-29 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
766 83f0f95a 2022-09-29 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
767 83f0f95a 2022-09-29 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
768 83f0f95a 2022-09-29 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
769 83f0f95a 2022-09-29 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
770 83f0f95a 2022-09-29 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
771 83f0f95a 2022-09-29 op */
772 83f0f95a 2022-09-29 op
773 83f0f95a 2022-09-29 op struct rb_type {
774 83f0f95a 2022-09-29 op int (*t_compare)(const void *, const void *);
775 83f0f95a 2022-09-29 op void (*t_augment)(void *);
776 83f0f95a 2022-09-29 op unsigned int t_offset; /* offset of rb_entry in type */
777 83f0f95a 2022-09-29 op };
778 83f0f95a 2022-09-29 op
779 83f0f95a 2022-09-29 op struct rb_tree {
780 83f0f95a 2022-09-29 op struct rb_entry *rbt_root;
781 83f0f95a 2022-09-29 op };
782 83f0f95a 2022-09-29 op
783 83f0f95a 2022-09-29 op struct rb_entry {
784 83f0f95a 2022-09-29 op struct rb_entry *rbt_parent;
785 83f0f95a 2022-09-29 op struct rb_entry *rbt_left;
786 83f0f95a 2022-09-29 op struct rb_entry *rbt_right;
787 83f0f95a 2022-09-29 op unsigned int rbt_color;
788 83f0f95a 2022-09-29 op };
789 83f0f95a 2022-09-29 op
790 83f0f95a 2022-09-29 op #define RBT_HEAD(_name, _type) \
791 83f0f95a 2022-09-29 op struct _name { \
792 83f0f95a 2022-09-29 op struct rb_tree rbh_root; \
793 83f0f95a 2022-09-29 op }
794 83f0f95a 2022-09-29 op
795 83f0f95a 2022-09-29 op #define RBT_ENTRY(_type) struct rb_entry
796 83f0f95a 2022-09-29 op
797 83f0f95a 2022-09-29 op static inline void
798 83f0f95a 2022-09-29 op _rb_init(struct rb_tree *rbt)
799 83f0f95a 2022-09-29 op {
800 83f0f95a 2022-09-29 op rbt->rbt_root = NULL;
801 83f0f95a 2022-09-29 op }
802 83f0f95a 2022-09-29 op
803 83f0f95a 2022-09-29 op static inline int
804 83f0f95a 2022-09-29 op _rb_empty(struct rb_tree *rbt)
805 83f0f95a 2022-09-29 op {
806 83f0f95a 2022-09-29 op return (rbt->rbt_root == NULL);
807 83f0f95a 2022-09-29 op }
808 83f0f95a 2022-09-29 op
809 83f0f95a 2022-09-29 op void *_rb_insert(const struct rb_type *, struct rb_tree *, void *);
810 83f0f95a 2022-09-29 op void *_rb_remove(const struct rb_type *, struct rb_tree *, void *);
811 83f0f95a 2022-09-29 op void *_rb_find(const struct rb_type *, struct rb_tree *, const void *);
812 83f0f95a 2022-09-29 op void *_rb_nfind(const struct rb_type *, struct rb_tree *, const void *);
813 83f0f95a 2022-09-29 op void *_rb_root(const struct rb_type *, struct rb_tree *);
814 83f0f95a 2022-09-29 op void *_rb_min(const struct rb_type *, struct rb_tree *);
815 83f0f95a 2022-09-29 op void *_rb_max(const struct rb_type *, struct rb_tree *);
816 83f0f95a 2022-09-29 op void *_rb_next(const struct rb_type *, void *);
817 83f0f95a 2022-09-29 op void *_rb_prev(const struct rb_type *, void *);
818 83f0f95a 2022-09-29 op void *_rb_left(const struct rb_type *, void *);
819 83f0f95a 2022-09-29 op void *_rb_right(const struct rb_type *, void *);
820 83f0f95a 2022-09-29 op void *_rb_parent(const struct rb_type *, void *);
821 83f0f95a 2022-09-29 op void _rb_set_left(const struct rb_type *, void *, void *);
822 83f0f95a 2022-09-29 op void _rb_set_right(const struct rb_type *, void *, void *);
823 83f0f95a 2022-09-29 op void _rb_set_parent(const struct rb_type *, void *, void *);
824 83f0f95a 2022-09-29 op void _rb_poison(const struct rb_type *, void *, unsigned long);
825 83f0f95a 2022-09-29 op int _rb_check(const struct rb_type *, void *, unsigned long);
826 83f0f95a 2022-09-29 op
827 83f0f95a 2022-09-29 op #define RBT_INITIALIZER(_head) { { NULL } }
828 83f0f95a 2022-09-29 op
829 83f0f95a 2022-09-29 op #define RBT_PROTOTYPE(_name, _type, _field, _cmp) \
830 83f0f95a 2022-09-29 op extern const struct rb_type *const _name##_RBT_TYPE; \
831 83f0f95a 2022-09-29 op \
832 83f0f95a 2022-09-29 op static inline void \
833 83f0f95a 2022-09-29 op _name##_RBT_INIT(struct _name *head) \
834 83f0f95a 2022-09-29 op { \
835 83f0f95a 2022-09-29 op _rb_init(&head->rbh_root); \
836 83f0f95a 2022-09-29 op } \
837 83f0f95a 2022-09-29 op \
838 83f0f95a 2022-09-29 op static inline struct _type * \
839 83f0f95a 2022-09-29 op _name##_RBT_INSERT(struct _name *head, struct _type *elm) \
840 83f0f95a 2022-09-29 op { \
841 83f0f95a 2022-09-29 op return _rb_insert(_name##_RBT_TYPE, &head->rbh_root, elm); \
842 83f0f95a 2022-09-29 op } \
843 83f0f95a 2022-09-29 op \
844 83f0f95a 2022-09-29 op static inline struct _type * \
845 83f0f95a 2022-09-29 op _name##_RBT_REMOVE(struct _name *head, struct _type *elm) \
846 83f0f95a 2022-09-29 op { \
847 83f0f95a 2022-09-29 op return _rb_remove(_name##_RBT_TYPE, &head->rbh_root, elm); \
848 83f0f95a 2022-09-29 op } \
849 83f0f95a 2022-09-29 op \
850 83f0f95a 2022-09-29 op static inline struct _type * \
851 83f0f95a 2022-09-29 op _name##_RBT_FIND(struct _name *head, const struct _type *key) \
852 83f0f95a 2022-09-29 op { \
853 83f0f95a 2022-09-29 op return _rb_find(_name##_RBT_TYPE, &head->rbh_root, key); \
854 83f0f95a 2022-09-29 op } \
855 83f0f95a 2022-09-29 op \
856 83f0f95a 2022-09-29 op static inline struct _type * \
857 83f0f95a 2022-09-29 op _name##_RBT_NFIND(struct _name *head, const struct _type *key) \
858 83f0f95a 2022-09-29 op { \
859 83f0f95a 2022-09-29 op return _rb_nfind(_name##_RBT_TYPE, &head->rbh_root, key); \
860 83f0f95a 2022-09-29 op } \
861 83f0f95a 2022-09-29 op \
862 83f0f95a 2022-09-29 op static inline struct _type * \
863 83f0f95a 2022-09-29 op _name##_RBT_ROOT(struct _name *head) \
864 83f0f95a 2022-09-29 op { \
865 83f0f95a 2022-09-29 op return _rb_root(_name##_RBT_TYPE, &head->rbh_root); \
866 83f0f95a 2022-09-29 op } \
867 83f0f95a 2022-09-29 op \
868 83f0f95a 2022-09-29 op static inline int \
869 83f0f95a 2022-09-29 op _name##_RBT_EMPTY(struct _name *head) \
870 83f0f95a 2022-09-29 op { \
871 83f0f95a 2022-09-29 op return _rb_empty(&head->rbh_root); \
872 83f0f95a 2022-09-29 op } \
873 83f0f95a 2022-09-29 op \
874 83f0f95a 2022-09-29 op static inline struct _type * \
875 83f0f95a 2022-09-29 op _name##_RBT_MIN(struct _name *head) \
876 83f0f95a 2022-09-29 op { \
877 83f0f95a 2022-09-29 op return _rb_min(_name##_RBT_TYPE, &head->rbh_root); \
878 83f0f95a 2022-09-29 op } \
879 83f0f95a 2022-09-29 op \
880 83f0f95a 2022-09-29 op static inline struct _type * \
881 83f0f95a 2022-09-29 op _name##_RBT_MAX(struct _name *head) \
882 83f0f95a 2022-09-29 op { \
883 83f0f95a 2022-09-29 op return _rb_max(_name##_RBT_TYPE, &head->rbh_root); \
884 83f0f95a 2022-09-29 op } \
885 83f0f95a 2022-09-29 op \
886 83f0f95a 2022-09-29 op static inline struct _type * \
887 83f0f95a 2022-09-29 op _name##_RBT_NEXT(struct _type *elm) \
888 83f0f95a 2022-09-29 op { \
889 83f0f95a 2022-09-29 op return _rb_next(_name##_RBT_TYPE, elm); \
890 83f0f95a 2022-09-29 op } \
891 83f0f95a 2022-09-29 op \
892 83f0f95a 2022-09-29 op static inline struct _type * \
893 83f0f95a 2022-09-29 op _name##_RBT_PREV(struct _type *elm) \
894 83f0f95a 2022-09-29 op { \
895 83f0f95a 2022-09-29 op return _rb_prev(_name##_RBT_TYPE, elm); \
896 83f0f95a 2022-09-29 op } \
897 83f0f95a 2022-09-29 op \
898 83f0f95a 2022-09-29 op static inline struct _type * \
899 83f0f95a 2022-09-29 op _name##_RBT_LEFT(struct _type *elm) \
900 83f0f95a 2022-09-29 op { \
901 83f0f95a 2022-09-29 op return _rb_left(_name##_RBT_TYPE, elm); \
902 83f0f95a 2022-09-29 op } \
903 83f0f95a 2022-09-29 op \
904 83f0f95a 2022-09-29 op static inline struct _type * \
905 83f0f95a 2022-09-29 op _name##_RBT_RIGHT(struct _type *elm) \
906 83f0f95a 2022-09-29 op { \
907 83f0f95a 2022-09-29 op return _rb_right(_name##_RBT_TYPE, elm); \
908 83f0f95a 2022-09-29 op } \
909 83f0f95a 2022-09-29 op \
910 83f0f95a 2022-09-29 op static inline struct _type * \
911 83f0f95a 2022-09-29 op _name##_RBT_PARENT(struct _type *elm) \
912 83f0f95a 2022-09-29 op { \
913 83f0f95a 2022-09-29 op return _rb_parent(_name##_RBT_TYPE, elm); \
914 83f0f95a 2022-09-29 op } \
915 83f0f95a 2022-09-29 op \
916 83f0f95a 2022-09-29 op static inline void \
917 83f0f95a 2022-09-29 op _name##_RBT_SET_LEFT(struct _type *elm, struct _type *left) \
918 83f0f95a 2022-09-29 op { \
919 83f0f95a 2022-09-29 op _rb_set_left(_name##_RBT_TYPE, elm, left); \
920 83f0f95a 2022-09-29 op } \
921 83f0f95a 2022-09-29 op \
922 83f0f95a 2022-09-29 op static inline void \
923 83f0f95a 2022-09-29 op _name##_RBT_SET_RIGHT(struct _type *elm, struct _type *right) \
924 83f0f95a 2022-09-29 op { \
925 83f0f95a 2022-09-29 op _rb_set_right(_name##_RBT_TYPE, elm, right); \
926 83f0f95a 2022-09-29 op } \
927 83f0f95a 2022-09-29 op \
928 83f0f95a 2022-09-29 op static inline void \
929 83f0f95a 2022-09-29 op _name##_RBT_SET_PARENT(struct _type *elm, struct _type *parent) \
930 83f0f95a 2022-09-29 op { \
931 83f0f95a 2022-09-29 op _rb_set_parent(_name##_RBT_TYPE, elm, parent); \
932 83f0f95a 2022-09-29 op } \
933 83f0f95a 2022-09-29 op \
934 83f0f95a 2022-09-29 op static inline void \
935 83f0f95a 2022-09-29 op _name##_RBT_POISON(struct _type *elm, unsigned long poison) \
936 83f0f95a 2022-09-29 op { \
937 83f0f95a 2022-09-29 op _rb_poison(_name##_RBT_TYPE, elm, poison); \
938 83f0f95a 2022-09-29 op } \
939 83f0f95a 2022-09-29 op \
940 83f0f95a 2022-09-29 op static inline int \
941 83f0f95a 2022-09-29 op _name##_RBT_CHECK(struct _type *elm, unsigned long poison) \
942 83f0f95a 2022-09-29 op { \
943 83f0f95a 2022-09-29 op return _rb_check(_name##_RBT_TYPE, elm, poison); \
944 83f0f95a 2022-09-29 op }
945 83f0f95a 2022-09-29 op
946 83f0f95a 2022-09-29 op #define RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _aug) \
947 83f0f95a 2022-09-29 op static int \
948 83f0f95a 2022-09-29 op _name##_RBT_COMPARE(const void *lptr, const void *rptr) \
949 83f0f95a 2022-09-29 op { \
950 83f0f95a 2022-09-29 op const struct _type *l = lptr, *r = rptr; \
951 83f0f95a 2022-09-29 op return _cmp(l, r); \
952 83f0f95a 2022-09-29 op } \
953 83f0f95a 2022-09-29 op static const struct rb_type _name##_RBT_INFO = { \
954 83f0f95a 2022-09-29 op _name##_RBT_COMPARE, \
955 83f0f95a 2022-09-29 op _aug, \
956 83f0f95a 2022-09-29 op offsetof(struct _type, _field), \
957 83f0f95a 2022-09-29 op }; \
958 83f0f95a 2022-09-29 op const struct rb_type *const _name##_RBT_TYPE = &_name##_RBT_INFO
959 83f0f95a 2022-09-29 op
960 83f0f95a 2022-09-29 op #define RBT_GENERATE_AUGMENT(_name, _type, _field, _cmp, _aug) \
961 83f0f95a 2022-09-29 op static void \
962 83f0f95a 2022-09-29 op _name##_RBT_AUGMENT(void *ptr) \
963 83f0f95a 2022-09-29 op { \
964 83f0f95a 2022-09-29 op struct _type *p = ptr; \
965 83f0f95a 2022-09-29 op return _aug(p); \
966 83f0f95a 2022-09-29 op } \
967 83f0f95a 2022-09-29 op RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, _name##_RBT_AUGMENT)
968 83f0f95a 2022-09-29 op
969 83f0f95a 2022-09-29 op #define RBT_GENERATE(_name, _type, _field, _cmp) \
970 83f0f95a 2022-09-29 op RBT_GENERATE_INTERNAL(_name, _type, _field, _cmp, NULL)
971 83f0f95a 2022-09-29 op
972 83f0f95a 2022-09-29 op #define RBT_INIT(_name, _head) _name##_RBT_INIT(_head)
973 83f0f95a 2022-09-29 op #define RBT_INSERT(_name, _head, _elm) _name##_RBT_INSERT(_head, _elm)
974 83f0f95a 2022-09-29 op #define RBT_REMOVE(_name, _head, _elm) _name##_RBT_REMOVE(_head, _elm)
975 83f0f95a 2022-09-29 op #define RBT_FIND(_name, _head, _key) _name##_RBT_FIND(_head, _key)
976 83f0f95a 2022-09-29 op #define RBT_NFIND(_name, _head, _key) _name##_RBT_NFIND(_head, _key)
977 83f0f95a 2022-09-29 op #define RBT_ROOT(_name, _head) _name##_RBT_ROOT(_head)
978 83f0f95a 2022-09-29 op #define RBT_EMPTY(_name, _head) _name##_RBT_EMPTY(_head)
979 83f0f95a 2022-09-29 op #define RBT_MIN(_name, _head) _name##_RBT_MIN(_head)
980 83f0f95a 2022-09-29 op #define RBT_MAX(_name, _head) _name##_RBT_MAX(_head)
981 83f0f95a 2022-09-29 op #define RBT_NEXT(_name, _elm) _name##_RBT_NEXT(_elm)
982 83f0f95a 2022-09-29 op #define RBT_PREV(_name, _elm) _name##_RBT_PREV(_elm)
983 83f0f95a 2022-09-29 op #define RBT_LEFT(_name, _elm) _name##_RBT_LEFT(_elm)
984 83f0f95a 2022-09-29 op #define RBT_RIGHT(_name, _elm) _name##_RBT_RIGHT(_elm)
985 83f0f95a 2022-09-29 op #define RBT_PARENT(_name, _elm) _name##_RBT_PARENT(_elm)
986 83f0f95a 2022-09-29 op #define RBT_SET_LEFT(_name, _elm, _l) _name##_RBT_SET_LEFT(_elm, _l)
987 83f0f95a 2022-09-29 op #define RBT_SET_RIGHT(_name, _elm, _r) _name##_RBT_SET_RIGHT(_elm, _r)
988 83f0f95a 2022-09-29 op #define RBT_SET_PARENT(_name, _elm, _p) _name##_RBT_SET_PARENT(_elm, _p)
989 83f0f95a 2022-09-29 op #define RBT_POISON(_name, _elm, _p) _name##_RBT_POISON(_elm, _p)
990 83f0f95a 2022-09-29 op #define RBT_CHECK(_name, _elm, _p) _name##_RBT_CHECK(_elm, _p)
991 83f0f95a 2022-09-29 op
992 83f0f95a 2022-09-29 op #define RBT_FOREACH(_e, _name, _head) \
993 83f0f95a 2022-09-29 op for ((_e) = RBT_MIN(_name, (_head)); \
994 83f0f95a 2022-09-29 op (_e) != NULL; \
995 83f0f95a 2022-09-29 op (_e) = RBT_NEXT(_name, (_e)))
996 83f0f95a 2022-09-29 op
997 83f0f95a 2022-09-29 op #define RBT_FOREACH_SAFE(_e, _name, _head, _n) \
998 83f0f95a 2022-09-29 op for ((_e) = RBT_MIN(_name, (_head)); \
999 83f0f95a 2022-09-29 op (_e) != NULL && ((_n) = RBT_NEXT(_name, (_e)), 1); \
1000 83f0f95a 2022-09-29 op (_e) = (_n))
1001 83f0f95a 2022-09-29 op
1002 83f0f95a 2022-09-29 op #define RBT_FOREACH_REVERSE(_e, _name, _head) \
1003 83f0f95a 2022-09-29 op for ((_e) = RBT_MAX(_name, (_head)); \
1004 83f0f95a 2022-09-29 op (_e) != NULL; \
1005 83f0f95a 2022-09-29 op (_e) = RBT_PREV(_name, (_e)))
1006 83f0f95a 2022-09-29 op
1007 83f0f95a 2022-09-29 op #define RBT_FOREACH_REVERSE_SAFE(_e, _name, _head, _n) \
1008 83f0f95a 2022-09-29 op for ((_e) = RBT_MAX(_name, (_head)); \
1009 83f0f95a 2022-09-29 op (_e) != NULL && ((_n) = RBT_PREV(_name, (_e)), 1); \
1010 83f0f95a 2022-09-29 op (_e) = (_n))
1011 83f0f95a 2022-09-29 op
1012 83f0f95a 2022-09-29 op #endif /* _SYS_TREE_H_ */