Go to the documentation of this file.
35 #if defined(__clang_analyzer__)
36 #define _T_ASSERT(a) assert((a))
68 #define SPLAY_HEAD(name, type) \
70 struct type *sph_root; \
73 #define SPLAY_INITIALIZER(root) \
76 #define SPLAY_INIT(root) do { \
77 (root)->sph_root = NULL; \
80 #define SPLAY_ENTRY(type) \
82 struct type *spe_left; \
83 struct type *spe_right; \
86 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
87 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
88 #define SPLAY_ROOT(head) (head)->sph_root
89 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
92 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
93 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
94 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
95 (head)->sph_root = tmp; \
98 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
99 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
100 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
101 (head)->sph_root = tmp; \
104 #define SPLAY_LINKLEFT(head, tmp, field) do { \
105 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
106 tmp = (head)->sph_root; \
107 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
110 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
111 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
112 tmp = (head)->sph_root; \
113 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
116 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
117 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
118 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
119 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
120 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
125 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
126 void name##_SPLAY(struct name *, struct type *); \
127 void name##_SPLAY_MINMAX(struct name *, int); \
128 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
129 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
132 static __inline struct type * \
133 name##_SPLAY_FIND(struct name *head, struct type *elm) \
135 if (SPLAY_EMPTY(head)) \
137 name##_SPLAY(head, elm); \
138 if ((cmp)(elm, (head)->sph_root) == 0) \
139 return (head->sph_root); \
143 static __inline struct type * \
144 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
146 name##_SPLAY(head, elm); \
147 if (SPLAY_RIGHT(elm, field) != NULL) { \
148 elm = SPLAY_RIGHT(elm, field); \
149 while (SPLAY_LEFT(elm, field) != NULL) { \
150 elm = SPLAY_LEFT(elm, field); \
157 static __inline struct type * \
158 name##_SPLAY_MIN_MAX(struct name *head, int val) \
160 name##_SPLAY_MINMAX(head, val); \
161 return (SPLAY_ROOT(head)); \
167 #define SPLAY_GENERATE(name, type, field, cmp) \
169 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
171 if (SPLAY_EMPTY(head)) { \
172 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
175 name##_SPLAY(head, elm); \
176 __comp = (cmp)(elm, (head)->sph_root); \
178 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
179 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
180 SPLAY_LEFT((head)->sph_root, field) = NULL; \
181 } else if (__comp > 0) { \
182 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
183 SPLAY_LEFT(elm, field) = (head)->sph_root; \
184 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
186 return ((head)->sph_root); \
188 (head)->sph_root = (elm); \
193 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
195 struct type *__tmp; \
196 if (SPLAY_EMPTY(head)) \
198 name##_SPLAY(head, elm); \
199 if ((cmp)(elm, (head)->sph_root) == 0) { \
200 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
201 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
203 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
204 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
205 name##_SPLAY(head, elm); \
206 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
214 name##_SPLAY(struct name *head, struct type *elm) \
216 struct type __node, *__left, *__right, *__tmp; \
219 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
220 __left = __right = &__node; \
222 while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
224 __tmp = SPLAY_LEFT((head)->sph_root, field); \
227 if ((cmp)(elm, __tmp) < 0){ \
228 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
229 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
232 SPLAY_LINKLEFT(head, __right, field); \
233 } else if (__comp > 0) { \
234 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
237 if ((cmp)(elm, __tmp) > 0){ \
238 SPLAY_ROTATE_LEFT(head, __tmp, field); \
239 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
242 SPLAY_LINKRIGHT(head, __left, field); \
245 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
251 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
253 struct type __node, *__left, *__right, *__tmp; \
255 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
256 __left = __right = &__node; \
260 __tmp = SPLAY_LEFT((head)->sph_root, field); \
264 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
265 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
268 SPLAY_LINKLEFT(head, __right, field); \
269 } else if (__comp > 0) { \
270 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
274 SPLAY_ROTATE_LEFT(head, __tmp, field); \
275 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
278 SPLAY_LINKRIGHT(head, __left, field); \
281 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
284 #define SPLAY_NEGINF -1
287 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
288 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
289 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
290 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
291 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
292 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
293 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
294 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
296 #define SPLAY_FOREACH(x, name, head) \
297 for ((x) = SPLAY_MIN(name, head); \
299 (x) = SPLAY_NEXT(name, head, x))
302 #define RB_HEAD(name, type) \
304 struct type *rbh_root; \
307 #define RB_INITIALIZER(root) \
310 #define RB_INIT(root) do { \
311 (root)->rbh_root = NULL; \
316 #define RB_ENTRY(type) \
318 struct type *rbe_left; \
319 struct type *rbe_right; \
320 struct type *rbe_parent; \
324 #define RB_LEFT(elm, field) (elm)->field.rbe_left
325 #define RB_RIGHT(elm, field) (elm)->field.rbe_right
326 #define RB_PARENT(elm, field) (elm)->field.rbe_parent
327 #define RB_COLOR(elm, field) (elm)->field.rbe_color
328 #define RB_ROOT(head) (head)->rbh_root
329 #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
331 #define RB_SET(elm, parent, field) do { \
332 RB_PARENT(elm, field) = parent; \
333 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
334 RB_COLOR(elm, field) = RB_RED; \
337 #define RB_SET_BLACKRED(black, red, field) do { \
338 RB_COLOR(black, field) = RB_BLACK; \
339 RB_COLOR(red, field) = RB_RED; \
343 #define RB_AUGMENT(x) do {} while (0)
346 #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
347 (tmp) = RB_RIGHT(elm, field); \
348 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != NULL) { \
349 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
352 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
353 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
354 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
356 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
358 (head)->rbh_root = (tmp); \
359 RB_LEFT(tmp, field) = (elm); \
360 RB_PARENT(elm, field) = (tmp); \
362 if ((RB_PARENT(tmp, field))) \
363 RB_AUGMENT(RB_PARENT(tmp, field)); \
366 #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
367 (tmp) = RB_LEFT(elm, field); \
368 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != NULL) { \
369 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
372 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field)) != NULL) { \
373 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
374 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
376 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
378 (head)->rbh_root = (tmp); \
379 RB_RIGHT(tmp, field) = (elm); \
380 RB_PARENT(elm, field) = (tmp); \
382 if ((RB_PARENT(tmp, field))) \
383 RB_AUGMENT(RB_PARENT(tmp, field)); \
387 #define RB_PROTOTYPE(name, type, field, cmp) \
388 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
389 #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
390 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
391 #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
392 RB_PROTOTYPE_INSERT_COLOR(name, type, attr); \
393 RB_PROTOTYPE_REMOVE_COLOR(name, type, attr); \
394 RB_PROTOTYPE_INSERT(name, type, attr); \
395 RB_PROTOTYPE_REMOVE(name, type, attr); \
396 RB_PROTOTYPE_FIND(name, type, attr); \
397 RB_PROTOTYPE_NFIND(name, type, attr); \
398 RB_PROTOTYPE_NEXT(name, type, attr); \
399 RB_PROTOTYPE_PREV(name, type, attr); \
400 RB_PROTOTYPE_MINMAX(name, type, attr);
401 #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \
402 attr void name##_RB_INSERT_COLOR(struct name *, struct type *)
403 #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \
404 attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *)
405 #define RB_PROTOTYPE_REMOVE(name, type, attr) \
406 attr struct type *name##_RB_REMOVE(struct name *, struct type *)
407 #define RB_PROTOTYPE_INSERT(name, type, attr) \
408 attr struct type *name##_RB_INSERT(struct name *, struct type *)
409 #define RB_PROTOTYPE_FIND(name, type, attr) \
410 attr struct type *name##_RB_FIND(struct name *, struct type *)
411 #define RB_PROTOTYPE_NFIND(name, type, attr) \
412 attr struct type *name##_RB_NFIND(struct name *, struct type *)
413 #define RB_PROTOTYPE_NEXT(name, type, attr) \
414 attr struct type *name##_RB_NEXT(struct type *)
415 #define RB_PROTOTYPE_PREV(name, type, attr) \
416 attr struct type *name##_RB_PREV(struct type *)
417 #define RB_PROTOTYPE_MINMAX(name, type, attr) \
418 attr struct type *name##_RB_MINMAX(struct name *, int)
423 #define RB_GENERATE(name, type, field, cmp) \
424 RB_GENERATE_INTERNAL(name, type, field, cmp,)
425 #define RB_GENERATE_STATIC(name, type, field, cmp) \
426 RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
427 #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
428 RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
429 RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
430 RB_GENERATE_INSERT(name, type, field, cmp, attr) \
431 RB_GENERATE_REMOVE(name, type, field, attr) \
432 RB_GENERATE_FIND(name, type, field, cmp, attr) \
433 RB_GENERATE_NFIND(name, type, field, cmp, attr) \
434 RB_GENERATE_NEXT(name, type, field, attr) \
435 RB_GENERATE_PREV(name, type, field, attr) \
436 RB_GENERATE_MINMAX(name, type, field, attr)
438 #define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
440 name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
442 struct type *parent, *gparent, *tmp; \
443 while ((parent = RB_PARENT(elm, field)) != NULL && \
444 RB_COLOR(parent, field) == RB_RED) { \
445 gparent = RB_PARENT(parent, field); \
446 _T_ASSERT(gparent); \
447 if (parent == RB_LEFT(gparent, field)) { \
448 tmp = RB_RIGHT(gparent, field); \
449 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
450 RB_COLOR(tmp, field) = RB_BLACK; \
451 RB_SET_BLACKRED(parent, gparent, field);\
455 if (RB_RIGHT(parent, field) == elm) { \
456 RB_ROTATE_LEFT(head, parent, tmp, field);\
461 RB_SET_BLACKRED(parent, gparent, field); \
462 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
464 tmp = RB_LEFT(gparent, field); \
465 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
466 RB_COLOR(tmp, field) = RB_BLACK; \
467 RB_SET_BLACKRED(parent, gparent, field);\
471 if (RB_LEFT(parent, field) == elm) { \
472 RB_ROTATE_RIGHT(head, parent, tmp, field);\
477 RB_SET_BLACKRED(parent, gparent, field); \
478 RB_ROTATE_LEFT(head, gparent, tmp, field); \
481 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
484 #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
486 name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
489 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
490 elm != RB_ROOT(head)) { \
491 if (RB_LEFT(parent, field) == elm) { \
492 tmp = RB_RIGHT(parent, field); \
493 if (RB_COLOR(tmp, field) == RB_RED) { \
494 RB_SET_BLACKRED(tmp, parent, field); \
495 RB_ROTATE_LEFT(head, parent, tmp, field);\
496 tmp = RB_RIGHT(parent, field); \
499 if ((RB_LEFT(tmp, field) == NULL || \
500 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
501 (RB_RIGHT(tmp, field) == NULL || \
502 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
503 RB_COLOR(tmp, field) = RB_RED; \
505 parent = RB_PARENT(elm, field); \
507 if (RB_RIGHT(tmp, field) == NULL || \
508 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
509 struct type *oleft; \
510 if ((oleft = RB_LEFT(tmp, field)) \
512 RB_COLOR(oleft, field) = RB_BLACK;\
513 RB_COLOR(tmp, field) = RB_RED; \
514 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
515 tmp = RB_RIGHT(parent, field); \
517 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
518 RB_COLOR(parent, field) = RB_BLACK; \
519 if (RB_RIGHT(tmp, field)) \
520 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
521 RB_ROTATE_LEFT(head, parent, tmp, field);\
522 elm = RB_ROOT(head); \
526 tmp = RB_LEFT(parent, field); \
527 if (RB_COLOR(tmp, field) == RB_RED) { \
528 RB_SET_BLACKRED(tmp, parent, field); \
529 RB_ROTATE_RIGHT(head, parent, tmp, field);\
530 tmp = RB_LEFT(parent, field); \
533 if ((RB_LEFT(tmp, field) == NULL || \
534 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
535 (RB_RIGHT(tmp, field) == NULL || \
536 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
537 RB_COLOR(tmp, field) = RB_RED; \
539 parent = RB_PARENT(elm, field); \
541 if (RB_LEFT(tmp, field) == NULL || \
542 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
543 struct type *oright; \
544 if ((oright = RB_RIGHT(tmp, field)) \
546 RB_COLOR(oright, field) = RB_BLACK;\
547 RB_COLOR(tmp, field) = RB_RED; \
548 RB_ROTATE_LEFT(head, tmp, oright, field);\
549 tmp = RB_LEFT(parent, field); \
551 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
552 RB_COLOR(parent, field) = RB_BLACK; \
553 if (RB_LEFT(tmp, field)) \
554 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
555 RB_ROTATE_RIGHT(head, parent, tmp, field);\
556 elm = RB_ROOT(head); \
562 RB_COLOR(elm, field) = RB_BLACK; \
565 #define RB_GENERATE_REMOVE(name, type, field, attr) \
567 name##_RB_REMOVE(struct name *head, struct type *elm) \
569 struct type *child, *parent, *old = elm; \
571 if (RB_LEFT(elm, field) == NULL) \
572 child = RB_RIGHT(elm, field); \
573 else if (RB_RIGHT(elm, field) == NULL) \
574 child = RB_LEFT(elm, field); \
577 elm = RB_RIGHT(elm, field); \
578 while ((left = RB_LEFT(elm, field)) != NULL) \
580 child = RB_RIGHT(elm, field); \
581 parent = RB_PARENT(elm, field); \
582 color = RB_COLOR(elm, field); \
584 RB_PARENT(child, field) = parent; \
586 if (RB_LEFT(parent, field) == elm) \
587 RB_LEFT(parent, field) = child; \
589 RB_RIGHT(parent, field) = child; \
590 RB_AUGMENT(parent); \
592 RB_ROOT(head) = child; \
593 if (RB_PARENT(elm, field) == old) \
596 (elm)->field = (old)->field; \
597 if (RB_PARENT(old, field)) { \
598 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
599 RB_LEFT(RB_PARENT(old, field), field) = elm;\
601 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
602 RB_AUGMENT(RB_PARENT(old, field)); \
604 RB_ROOT(head) = elm; \
606 _T_ASSERT(RB_LEFT(old, field)); \
607 RB_PARENT(RB_LEFT(old, field), field) = elm; \
608 if (RB_RIGHT(old, field)) \
609 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
614 } while ((left = RB_PARENT(left, field)) != NULL); \
618 parent = RB_PARENT(elm, field); \
619 color = RB_COLOR(elm, field); \
621 RB_PARENT(child, field) = parent; \
623 if (RB_LEFT(parent, field) == elm) \
624 RB_LEFT(parent, field) = child; \
626 RB_RIGHT(parent, field) = child; \
627 RB_AUGMENT(parent); \
629 RB_ROOT(head) = child; \
631 if (color == RB_BLACK) \
632 name##_RB_REMOVE_COLOR(head, parent, child); \
636 #define RB_GENERATE_INSERT(name, type, field, cmp, attr) \
639 name##_RB_INSERT(struct name *head, struct type *elm) \
642 struct type *parent = NULL; \
644 tmp = RB_ROOT(head); \
647 comp = (cmp)(elm, parent); \
649 tmp = RB_LEFT(tmp, field); \
651 tmp = RB_RIGHT(tmp, field); \
655 RB_SET(elm, parent, field); \
656 if (parent != NULL) { \
658 RB_LEFT(parent, field) = elm; \
660 RB_RIGHT(parent, field) = elm; \
661 RB_AUGMENT(parent); \
663 RB_ROOT(head) = elm; \
664 name##_RB_INSERT_COLOR(head, elm); \
668 #define RB_GENERATE_FIND(name, type, field, cmp, attr) \
671 name##_RB_FIND(struct name *head, struct type *elm) \
673 struct type *tmp = RB_ROOT(head); \
676 comp = cmp(elm, tmp); \
678 tmp = RB_LEFT(tmp, field); \
680 tmp = RB_RIGHT(tmp, field); \
687 #define RB_GENERATE_NFIND(name, type, field, cmp, attr) \
690 name##_RB_NFIND(struct name *head, struct type *elm) \
692 struct type *tmp = RB_ROOT(head); \
693 struct type *res = NULL; \
696 comp = cmp(elm, tmp); \
699 tmp = RB_LEFT(tmp, field); \
702 tmp = RB_RIGHT(tmp, field); \
709 #define RB_GENERATE_NEXT(name, type, field, attr) \
712 name##_RB_NEXT(struct type *elm) \
714 if (RB_RIGHT(elm, field)) { \
715 elm = RB_RIGHT(elm, field); \
716 while (RB_LEFT(elm, field)) \
717 elm = RB_LEFT(elm, field); \
719 if (RB_PARENT(elm, field) && \
720 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
721 elm = RB_PARENT(elm, field); \
723 while (RB_PARENT(elm, field) && \
724 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
725 elm = RB_PARENT(elm, field); \
726 elm = RB_PARENT(elm, field); \
732 #define RB_GENERATE_PREV(name, type, field, attr) \
735 name##_RB_PREV(struct type *elm) \
737 if (RB_LEFT(elm, field)) { \
738 elm = RB_LEFT(elm, field); \
739 while (RB_RIGHT(elm, field)) \
740 elm = RB_RIGHT(elm, field); \
742 if (RB_PARENT(elm, field) && \
743 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
744 elm = RB_PARENT(elm, field); \
746 while (RB_PARENT(elm, field) && \
747 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
748 elm = RB_PARENT(elm, field); \
749 elm = RB_PARENT(elm, field); \
755 #define RB_GENERATE_MINMAX(name, type, field, attr) \
757 name##_RB_MINMAX(struct name *head, int val) \
759 struct type *tmp = RB_ROOT(head); \
760 struct type *parent = NULL; \
764 tmp = RB_LEFT(tmp, field); \
766 tmp = RB_RIGHT(tmp, field); \
774 #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
775 #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
776 #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
777 #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
778 #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
779 #define RB_PREV(name, x, y) name##_RB_PREV(y)
780 #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
781 #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
783 #define RB_FOREACH(x, name, head) \
784 for ((x) = RB_MIN(name, head); \
786 (x) = name##_RB_NEXT(x))
788 #define RB_FOREACH_FROM(x, name, y) \
790 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
793 #define RB_FOREACH_SAFE(x, name, head, y) \
794 for ((x) = RB_MIN(name, head); \
795 ((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
798 #define RB_FOREACH_REVERSE(x, name, head) \
799 for ((x) = RB_MAX(name, head); \
801 (x) = name##_RB_PREV(x))
803 #define RB_FOREACH_REVERSE_FROM(x, name, y) \
805 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \
808 #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
809 for ((x) = RB_MAX(name, head); \
810 ((x) != NULL) && ((y) = name##_RB_PREV(x), (x) != NULL); \