Blame


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