|
WPILibC++ 2025.2.1
|
Loading...
Searching...
No Matches
Go to the documentation of this file.
31# define UV__UNUSED __attribute__((unused))
51#define RB_HEAD(name, type) \
53 struct type *rbh_root; \
56#define RB_INITIALIZER(root) \
59#define RB_INIT(root) do { \
60 (root)->rbh_root = NULL; \
65#define RB_ENTRY(type) \
67 struct type *rbe_left; \
68 struct type *rbe_right; \
69 struct type *rbe_parent; \
73#define RB_LEFT(elm, field) (elm)->field.rbe_left
74#define RB_RIGHT(elm, field) (elm)->field.rbe_right
75#define RB_PARENT(elm, field) (elm)->field.rbe_parent
76#define RB_COLOR(elm, field) (elm)->field.rbe_color
77#define RB_ROOT(head) (head)->rbh_root
78#define RB_EMPTY(head) (RB_ROOT(head) == NULL)
80#define RB_SET(elm, parent, field) do { \
81 RB_PARENT(elm, field) = parent; \
82 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
83 RB_COLOR(elm, field) = RB_RED; \
86#define RB_SET_BLACKRED(black, red, field) do { \
87 RB_COLOR(black, field) = RB_BLACK; \
88 RB_COLOR(red, field) = RB_RED; \
92#define RB_AUGMENT(x) do {} while (0)
95#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
96 (tmp) = RB_RIGHT(elm, field); \
97 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
98 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
101 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
102 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
103 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
105 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
107 (head)->rbh_root = (tmp); \
108 RB_LEFT(tmp, field) = (elm); \
109 RB_PARENT(elm, field) = (tmp); \
111 if ((RB_PARENT(tmp, field))) \
112 RB_AUGMENT(RB_PARENT(tmp, field)); \
115#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
116 (tmp) = RB_LEFT(elm, field); \
117 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
118 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
121 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
122 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
123 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
125 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
127 (head)->rbh_root = (tmp); \
128 RB_RIGHT(tmp, field) = (elm); \
129 RB_PARENT(elm, field) = (tmp); \
131 if ((RB_PARENT(tmp, field))) \
132 RB_AUGMENT(RB_PARENT(tmp, field)); \
136#define RB_PROTOTYPE(name, type, field, cmp) \
137 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
138#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
139 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, UV__UNUSED static)
140#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
141attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
142attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
143attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
144attr struct type *name##_RB_INSERT(struct name *, struct type *); \
145attr struct type *name##_RB_FIND(struct name *, struct type *); \
146attr struct type *name##_RB_NFIND(struct name *, struct type *); \
147attr struct type *name##_RB_NEXT(struct type *); \
148attr struct type *name##_RB_PREV(struct type *); \
149attr struct type *name##_RB_MINMAX(struct name *, int); \
155#define RB_GENERATE(name, type, field, cmp) \
156 RB_GENERATE_INTERNAL(name, type, field, cmp,)
157#define RB_GENERATE_STATIC(name, type, field, cmp) \
158 RB_GENERATE_INTERNAL(name, type, field, cmp, UV__UNUSED static)
159#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
161name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
163 struct type *parent, *gparent, *tmp; \
164 while ((parent = RB_PARENT(elm, field)) != NULL && \
165 RB_COLOR(parent, field) == RB_RED) { \
166 gparent = RB_PARENT(parent, field); \
167 if (parent == RB_LEFT(gparent, field)) { \
168 tmp = RB_RIGHT(gparent, field); \
169 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
170 RB_COLOR(tmp, field) = RB_BLACK; \
171 RB_SET_BLACKRED(parent, gparent, field); \
175 if (RB_RIGHT(parent, field) == elm) { \
176 RB_ROTATE_LEFT(head, parent, tmp, field); \
181 RB_SET_BLACKRED(parent, gparent, field); \
182 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
184 tmp = RB_LEFT(gparent, field); \
185 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
186 RB_COLOR(tmp, field) = RB_BLACK; \
187 RB_SET_BLACKRED(parent, gparent, field); \
191 if (RB_LEFT(parent, field) == elm) { \
192 RB_ROTATE_RIGHT(head, parent, tmp, field); \
197 RB_SET_BLACKRED(parent, gparent, field); \
198 RB_ROTATE_LEFT(head, gparent, tmp, field); \
201 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
205name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, \
209 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
210 elm != RB_ROOT(head)) { \
211 if (RB_LEFT(parent, field) == elm) { \
212 tmp = RB_RIGHT(parent, field); \
213 if (RB_COLOR(tmp, field) == RB_RED) { \
214 RB_SET_BLACKRED(tmp, parent, field); \
215 RB_ROTATE_LEFT(head, parent, tmp, field); \
216 tmp = RB_RIGHT(parent, field); \
218 if ((RB_LEFT(tmp, field) == NULL || \
219 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) && \
220 (RB_RIGHT(tmp, field) == NULL || \
221 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) { \
222 RB_COLOR(tmp, field) = RB_RED; \
224 parent = RB_PARENT(elm, field); \
226 if (RB_RIGHT(tmp, field) == NULL || \
227 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) { \
228 struct type *oleft; \
229 if ((oleft = RB_LEFT(tmp, field)) \
231 RB_COLOR(oleft, field) = RB_BLACK; \
232 RB_COLOR(tmp, field) = RB_RED; \
233 RB_ROTATE_RIGHT(head, tmp, oleft, field); \
234 tmp = RB_RIGHT(parent, field); \
236 RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
237 RB_COLOR(parent, field) = RB_BLACK; \
238 if (RB_RIGHT(tmp, field)) \
239 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \
240 RB_ROTATE_LEFT(head, parent, tmp, field); \
241 elm = RB_ROOT(head); \
245 tmp = RB_LEFT(parent, field); \
246 if (RB_COLOR(tmp, field) == RB_RED) { \
247 RB_SET_BLACKRED(tmp, parent, field); \
248 RB_ROTATE_RIGHT(head, parent, tmp, field); \
249 tmp = RB_LEFT(parent, field); \
251 if ((RB_LEFT(tmp, field) == NULL || \
252 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) && \
253 (RB_RIGHT(tmp, field) == NULL || \
254 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) { \
255 RB_COLOR(tmp, field) = RB_RED; \
257 parent = RB_PARENT(elm, field); \
259 if (RB_LEFT(tmp, field) == NULL || \
260 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) { \
261 struct type *oright; \
262 if ((oright = RB_RIGHT(tmp, field)) \
264 RB_COLOR(oright, field) = RB_BLACK; \
265 RB_COLOR(tmp, field) = RB_RED; \
266 RB_ROTATE_LEFT(head, tmp, oright, field); \
267 tmp = RB_LEFT(parent, field); \
269 RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
270 RB_COLOR(parent, field) = RB_BLACK; \
271 if (RB_LEFT(tmp, field)) \
272 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \
273 RB_ROTATE_RIGHT(head, parent, tmp, field); \
274 elm = RB_ROOT(head); \
280 RB_COLOR(elm, field) = RB_BLACK; \
284name##_RB_REMOVE(struct name *head, struct type *elm) \
286 struct type *child, *parent, *old = elm; \
288 if (RB_LEFT(elm, field) == NULL) \
289 child = RB_RIGHT(elm, field); \
290 else if (RB_RIGHT(elm, field) == NULL) \
291 child = RB_LEFT(elm, field); \
294 elm = RB_RIGHT(elm, field); \
295 while ((left = RB_LEFT(elm, field)) != NULL) \
297 child = RB_RIGHT(elm, field); \
298 parent = RB_PARENT(elm, field); \
299 color = RB_COLOR(elm, field); \
301 RB_PARENT(child, field) = parent; \
303 if (RB_LEFT(parent, field) == elm) \
304 RB_LEFT(parent, field) = child; \
306 RB_RIGHT(parent, field) = child; \
307 RB_AUGMENT(parent); \
309 RB_ROOT(head) = child; \
310 if (RB_PARENT(elm, field) == old) \
312 (elm)->field = (old)->field; \
313 if (RB_PARENT(old, field)) { \
314 if (RB_LEFT(RB_PARENT(old, field), field) == old) \
315 RB_LEFT(RB_PARENT(old, field), field) = elm; \
317 RB_RIGHT(RB_PARENT(old, field), field) = elm; \
318 RB_AUGMENT(RB_PARENT(old, field)); \
320 RB_ROOT(head) = elm; \
321 RB_PARENT(RB_LEFT(old, field), field) = elm; \
322 if (RB_RIGHT(old, field)) \
323 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
328 } while ((left = RB_PARENT(left, field)) != NULL); \
332 parent = RB_PARENT(elm, field); \
333 color = RB_COLOR(elm, field); \
335 RB_PARENT(child, field) = parent; \
337 if (RB_LEFT(parent, field) == elm) \
338 RB_LEFT(parent, field) = child; \
340 RB_RIGHT(parent, field) = child; \
341 RB_AUGMENT(parent); \
343 RB_ROOT(head) = child; \
345 if (color == RB_BLACK) \
346 name##_RB_REMOVE_COLOR(head, parent, child); \
352name##_RB_INSERT(struct name *head, struct type *elm) \
355 struct type *parent = NULL; \
357 tmp = RB_ROOT(head); \
360 comp = (cmp)(elm, parent); \
362 tmp = RB_LEFT(tmp, field); \
364 tmp = RB_RIGHT(tmp, field); \
368 RB_SET(elm, parent, field); \
369 if (parent != NULL) { \
371 RB_LEFT(parent, field) = elm; \
373 RB_RIGHT(parent, field) = elm; \
374 RB_AUGMENT(parent); \
376 RB_ROOT(head) = elm; \
377 name##_RB_INSERT_COLOR(head, elm); \
383name##_RB_FIND(struct name *head, struct type *elm) \
385 struct type *tmp = RB_ROOT(head); \
388 comp = cmp(elm, tmp); \
390 tmp = RB_LEFT(tmp, field); \
392 tmp = RB_RIGHT(tmp, field); \
401name##_RB_NFIND(struct name *head, struct type *elm) \
403 struct type *tmp = RB_ROOT(head); \
404 struct type *res = NULL; \
407 comp = cmp(elm, tmp); \
410 tmp = RB_LEFT(tmp, field); \
413 tmp = RB_RIGHT(tmp, field); \
422name##_RB_NEXT(struct type *elm) \
424 if (RB_RIGHT(elm, field)) { \
425 elm = RB_RIGHT(elm, field); \
426 while (RB_LEFT(elm, field)) \
427 elm = RB_LEFT(elm, field); \
429 if (RB_PARENT(elm, field) && \
430 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
431 elm = RB_PARENT(elm, field); \
433 while (RB_PARENT(elm, field) && \
434 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
435 elm = RB_PARENT(elm, field); \
436 elm = RB_PARENT(elm, field); \
444name##_RB_PREV(struct type *elm) \
446 if (RB_LEFT(elm, field)) { \
447 elm = RB_LEFT(elm, field); \
448 while (RB_RIGHT(elm, field)) \
449 elm = RB_RIGHT(elm, field); \
451 if (RB_PARENT(elm, field) && \
452 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
453 elm = RB_PARENT(elm, field); \
455 while (RB_PARENT(elm, field) && \
456 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
457 elm = RB_PARENT(elm, field); \
458 elm = RB_PARENT(elm, field); \
465name##_RB_MINMAX(struct name *head, int val) \
467 struct type *tmp = RB_ROOT(head); \
468 struct type *parent = NULL; \
472 tmp = RB_LEFT(tmp, field); \
474 tmp = RB_RIGHT(tmp, field); \
482#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
483#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
484#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
485#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
486#define RB_NEXT(name, x) name##_RB_NEXT(x)
487#define RB_PREV(name, x) name##_RB_PREV(x)
488#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
489#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
491#define RB_FOREACH(x, name, head) \
492 for ((x) = RB_MIN(name, head); \
494 (x) = name##_RB_NEXT(x))
496#define RB_FOREACH_FROM(x, name, y) \
498 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
501#define RB_FOREACH_SAFE(x, name, head, y) \
502 for ((x) = RB_MIN(name, head); \
503 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
506#define RB_FOREACH_REVERSE(x, name, head) \
507 for ((x) = RB_MAX(name, head); \
509 (x) = name##_RB_PREV(x))
511#define RB_FOREACH_REVERSE_FROM(x, name, y) \
513 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
516#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
517 for ((x) = RB_MAX(name, head); \
518 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \