Blame


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