Blame


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