Blame


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