Go to the documentation of this file.
67#define SPLAY_HEAD(name, type) \
69 struct type *sph_root; \
72#define SPLAY_INITIALIZER(root) \
75#define SPLAY_INIT(root) do { \
76 (root)->sph_root = NULL; \
79#define SPLAY_ENTRY(type) \
81 struct type *spe_left; \
82 struct type *spe_right; \
85#define SPLAY_LEFT(elm, field) (elm)->field.spe_left
86#define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
87#define SPLAY_ROOT(head) (head)->sph_root
88#define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
91#define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
92 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
93 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
94 (head)->sph_root = tmp; \
97#define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
98 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
99 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
100 (head)->sph_root = tmp; \
103#define SPLAY_LINKLEFT(head, tmp, field) do { \
104 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
105 tmp = (head)->sph_root; \
106 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
109#define SPLAY_LINKRIGHT(head, tmp, field) do { \
110 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
111 tmp = (head)->sph_root; \
112 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
115#define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
116 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
117 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
118 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
119 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
124#define SPLAY_PROTOTYPE(name, type, field, cmp) \
125void name##_SPLAY(struct name *, struct type *); \
126void name##_SPLAY_MINMAX(struct name *, int); \
127struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
128struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
131static __inline struct type * \
132name##_SPLAY_FIND(struct name *head, struct type *elm) \
134 if (SPLAY_EMPTY(head)) \
136 name##_SPLAY(head, elm); \
137 if ((cmp)(elm, (head)->sph_root) == 0) \
138 return (head->sph_root); \
142static __inline struct type * \
143name##_SPLAY_NEXT(struct name *head, struct type *elm) \
145 name##_SPLAY(head, elm); \
146 if (SPLAY_RIGHT(elm, field) != NULL) { \
147 elm = SPLAY_RIGHT(elm, field); \
148 while (SPLAY_LEFT(elm, field) != NULL) { \
149 elm = SPLAY_LEFT(elm, field); \
156static __inline struct type * \
157name##_SPLAY_MIN_MAX(struct name *head, int val) \
159 name##_SPLAY_MINMAX(head, val); \
160 return (SPLAY_ROOT(head)); \
166#define SPLAY_GENERATE(name, type, field, cmp) \
168name##_SPLAY_INSERT(struct name *head, struct type *elm) \
170 if (SPLAY_EMPTY(head)) { \
171 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
174 name##_SPLAY(head, elm); \
175 __comp = (cmp)(elm, (head)->sph_root); \
177 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
178 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
179 SPLAY_LEFT((head)->sph_root, field) = NULL; \
180 } else if (__comp > 0) { \
181 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
182 SPLAY_LEFT(elm, field) = (head)->sph_root; \
183 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
185 return ((head)->sph_root); \
187 (head)->sph_root = (elm); \
192name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
194 struct type *__tmp; \
195 if (SPLAY_EMPTY(head)) \
197 name##_SPLAY(head, elm); \
198 if ((cmp)(elm, (head)->sph_root) == 0) { \
199 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
200 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
202 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
203 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
204 name##_SPLAY(head, elm); \
205 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
213name##_SPLAY(struct name *head, struct type *elm) \
215 struct type __node, *__left, *__right, *__tmp; \
218 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
219 __left = __right = &__node; \
221 while ((__comp = (cmp)(elm, (head)->sph_root))) { \
223 __tmp = SPLAY_LEFT((head)->sph_root, field); \
226 if ((cmp)(elm, __tmp) < 0){ \
227 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
228 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
231 SPLAY_LINKLEFT(head, __right, field); \
232 } else if (__comp > 0) { \
233 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
236 if ((cmp)(elm, __tmp) > 0){ \
237 SPLAY_ROTATE_LEFT(head, __tmp, field); \
238 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
241 SPLAY_LINKRIGHT(head, __left, field); \
244 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
250void name##_SPLAY_MINMAX(struct name *head, int __comp) \
252 struct type __node, *__left, *__right, *__tmp; \
254 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
255 __left = __right = &__node; \
259 __tmp = SPLAY_LEFT((head)->sph_root, field); \
263 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
264 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
267 SPLAY_LINKLEFT(head, __right, field); \
268 } else if (__comp > 0) { \
269 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
273 SPLAY_ROTATE_LEFT(head, __tmp, field); \
274 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
277 SPLAY_LINKRIGHT(head, __left, field); \
280 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
283#define SPLAY_NEGINF -1
286#define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
287#define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
288#define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
289#define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
290#define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
291 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
292#define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
293 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
295#define SPLAY_FOREACH(x, name, head) \
296 for ((x) = SPLAY_MIN(name, head); \
298 (x) = SPLAY_NEXT(name, head, x))
301#define RB_HEAD(name, type) \
303 struct type *rbh_root; \
306#define RB_INITIALIZER(root) \
309#define RB_INIT(root) do { \
310 (root)->rbh_root = NULL; \
315#define RB_ENTRY(type) \
317 struct type *rbe_left; \
318 struct type *rbe_right; \
319 struct type *rbe_parent; \
323#define RB_LEFT(elm, field) (elm)->field.rbe_left
324#define RB_RIGHT(elm, field) (elm)->field.rbe_right
325#define RB_PARENT(elm, field) (elm)->field.rbe_parent
326#define RB_COLOR(elm, field) (elm)->field.rbe_color
327#define RB_ROOT(head) (head)->rbh_root
328#define RB_EMPTY(head) (RB_ROOT(head) == NULL)
330#define RB_SET(elm, parent, field) do { \
331 RB_PARENT(elm, field) = parent; \
332 RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
333 RB_COLOR(elm, field) = RB_RED; \
336#define RB_SET_BLACKRED(black, red, field) do { \
337 RB_COLOR(black, field) = RB_BLACK; \
338 RB_COLOR(red, field) = RB_RED; \
342#define RB_AUGMENT(x) do {} while (0)
345#define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
346 (tmp) = RB_RIGHT(elm, field); \
347 if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) { \
348 RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
351 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
352 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
353 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
355 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
357 (head)->rbh_root = (tmp); \
358 RB_LEFT(tmp, field) = (elm); \
359 RB_PARENT(elm, field) = (tmp); \
361 if ((RB_PARENT(tmp, field))) \
362 RB_AUGMENT(RB_PARENT(tmp, field)); \
365#define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
366 (tmp) = RB_LEFT(elm, field); \
367 if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) { \
368 RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
371 if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) { \
372 if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
373 RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
375 RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
377 (head)->rbh_root = (tmp); \
378 RB_RIGHT(tmp, field) = (elm); \
379 RB_PARENT(elm, field) = (tmp); \
381 if ((RB_PARENT(tmp, field))) \
382 RB_AUGMENT(RB_PARENT(tmp, field)); \
386#define RB_PROTOTYPE(name, type, field, cmp) \
387 RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
388#define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
389 RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
390#define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
391attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
392attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *);\
393attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
394attr struct type *name##_RB_INSERT(struct name *, struct type *); \
395attr struct type *name##_RB_FIND(struct name *, struct type *); \
396attr struct type *name##_RB_NFIND(struct name *, struct type *); \
397attr struct type *name##_RB_NEXT(struct type *); \
398attr struct type *name##_RB_PREV(struct type *); \
399attr struct type *name##_RB_MINMAX(struct name *, int); \
405#define RB_GENERATE(name, type, field, cmp) \
406 RB_GENERATE_INTERNAL(name, type, field, cmp,)
407#define RB_GENERATE_STATIC(name, type, field, cmp) \
408 RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
409#define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
411name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
413 struct type *parent, *gparent, *tmp; \
414 while ((parent = RB_PARENT(elm, field)) && \
415 RB_COLOR(parent, field) == RB_RED) { \
416 gparent = RB_PARENT(parent, field); \
417 if (parent == RB_LEFT(gparent, field)) { \
418 tmp = RB_RIGHT(gparent, field); \
419 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
420 RB_COLOR(tmp, field) = RB_BLACK; \
421 RB_SET_BLACKRED(parent, gparent, field);\
425 if (RB_RIGHT(parent, field) == elm) { \
426 RB_ROTATE_LEFT(head, parent, tmp, field);\
431 RB_SET_BLACKRED(parent, gparent, field); \
432 RB_ROTATE_RIGHT(head, gparent, tmp, field); \
434 tmp = RB_LEFT(gparent, field); \
435 if (tmp && RB_COLOR(tmp, field) == RB_RED) { \
436 RB_COLOR(tmp, field) = RB_BLACK; \
437 RB_SET_BLACKRED(parent, gparent, field);\
441 if (RB_LEFT(parent, field) == elm) { \
442 RB_ROTATE_RIGHT(head, parent, tmp, field);\
447 RB_SET_BLACKRED(parent, gparent, field); \
448 RB_ROTATE_LEFT(head, gparent, tmp, field); \
451 RB_COLOR(head->rbh_root, field) = RB_BLACK; \
455name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
458 while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
459 elm != RB_ROOT(head)) { \
460 if (RB_LEFT(parent, field) == elm) { \
461 tmp = RB_RIGHT(parent, field); \
462 if (RB_COLOR(tmp, field) == RB_RED) { \
463 RB_SET_BLACKRED(tmp, parent, field); \
464 RB_ROTATE_LEFT(head, parent, tmp, field);\
465 tmp = RB_RIGHT(parent, field); \
467 if ((RB_LEFT(tmp, field) == NULL || \
468 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
469 (RB_RIGHT(tmp, field) == NULL || \
470 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) {\
471 RB_COLOR(tmp, field) = RB_RED; \
473 parent = RB_PARENT(elm, field); \
475 if (RB_RIGHT(tmp, field) == NULL || \
476 RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) {\
477 struct type *oleft; \
478 if ((oleft = RB_LEFT(tmp, field)))\
479 RB_COLOR(oleft, field) = RB_BLACK;\
480 RB_COLOR(tmp, field) = RB_RED; \
481 RB_ROTATE_RIGHT(head, tmp, oleft, field);\
482 tmp = RB_RIGHT(parent, field); \
484 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
485 RB_COLOR(parent, field) = RB_BLACK; \
486 if (RB_RIGHT(tmp, field)) \
487 RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK;\
488 RB_ROTATE_LEFT(head, parent, tmp, field);\
489 elm = RB_ROOT(head); \
493 tmp = RB_LEFT(parent, field); \
494 if (RB_COLOR(tmp, field) == RB_RED) { \
495 RB_SET_BLACKRED(tmp, parent, field); \
496 RB_ROTATE_RIGHT(head, parent, tmp, field);\
497 tmp = RB_LEFT(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_LEFT(tmp, field) == NULL || \
508 RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) {\
509 struct type *oright; \
510 if ((oright = RB_RIGHT(tmp, field)))\
511 RB_COLOR(oright, field) = RB_BLACK;\
512 RB_COLOR(tmp, field) = RB_RED; \
513 RB_ROTATE_LEFT(head, tmp, oright, field);\
514 tmp = RB_LEFT(parent, field); \
516 RB_COLOR(tmp, field) = RB_COLOR(parent, field);\
517 RB_COLOR(parent, field) = RB_BLACK; \
518 if (RB_LEFT(tmp, field)) \
519 RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK;\
520 RB_ROTATE_RIGHT(head, parent, tmp, field);\
521 elm = RB_ROOT(head); \
527 RB_COLOR(elm, field) = RB_BLACK; \
531name##_RB_REMOVE(struct name *head, struct type *elm) \
533 struct type *child, *parent, *old = elm; \
535 if (RB_LEFT(elm, field) == NULL) \
536 child = RB_RIGHT(elm, field); \
537 else if (RB_RIGHT(elm, field) == NULL) \
538 child = RB_LEFT(elm, field); \
541 elm = RB_RIGHT(elm, field); \
542 while ((left = RB_LEFT(elm, field))) \
544 child = RB_RIGHT(elm, field); \
545 parent = RB_PARENT(elm, field); \
546 color = RB_COLOR(elm, field); \
548 RB_PARENT(child, field) = parent; \
550 if (RB_LEFT(parent, field) == elm) \
551 RB_LEFT(parent, field) = child; \
553 RB_RIGHT(parent, field) = child; \
554 RB_AUGMENT(parent); \
556 RB_ROOT(head) = child; \
557 if (RB_PARENT(elm, field) == old) \
559 (elm)->field = (old)->field; \
560 if (RB_PARENT(old, field)) { \
561 if (RB_LEFT(RB_PARENT(old, field), field) == old)\
562 RB_LEFT(RB_PARENT(old, field), field) = elm;\
564 RB_RIGHT(RB_PARENT(old, field), field) = elm;\
565 RB_AUGMENT(RB_PARENT(old, field)); \
567 RB_ROOT(head) = elm; \
568 RB_PARENT(RB_LEFT(old, field), field) = elm; \
569 if (RB_RIGHT(old, field)) \
570 RB_PARENT(RB_RIGHT(old, field), field) = elm; \
575 } while ((left = RB_PARENT(left, field))); \
579 parent = RB_PARENT(elm, field); \
580 color = RB_COLOR(elm, field); \
582 RB_PARENT(child, field) = parent; \
584 if (RB_LEFT(parent, field) == elm) \
585 RB_LEFT(parent, field) = child; \
587 RB_RIGHT(parent, field) = child; \
588 RB_AUGMENT(parent); \
590 RB_ROOT(head) = child; \
592 if (color == RB_BLACK) \
593 name##_RB_REMOVE_COLOR(head, parent, child); \
599name##_RB_INSERT(struct name *head, struct type *elm) \
602 struct type *parent = NULL; \
604 tmp = RB_ROOT(head); \
607 comp = (cmp)(elm, parent); \
609 tmp = RB_LEFT(tmp, field); \
611 tmp = RB_RIGHT(tmp, field); \
615 RB_SET(elm, parent, field); \
616 if (parent != NULL) { \
618 RB_LEFT(parent, field) = elm; \
620 RB_RIGHT(parent, field) = elm; \
621 RB_AUGMENT(parent); \
623 RB_ROOT(head) = elm; \
624 name##_RB_INSERT_COLOR(head, elm); \
630name##_RB_FIND(struct name *head, struct type *elm) \
632 struct type *tmp = RB_ROOT(head); \
635 comp = cmp(elm, tmp); \
637 tmp = RB_LEFT(tmp, field); \
639 tmp = RB_RIGHT(tmp, field); \
648name##_RB_NFIND(struct name *head, struct type *elm) \
650 struct type *tmp = RB_ROOT(head); \
651 struct type *res = NULL; \
654 comp = cmp(elm, tmp); \
657 tmp = RB_LEFT(tmp, field); \
660 tmp = RB_RIGHT(tmp, field); \
669name##_RB_NEXT(struct type *elm) \
671 if (RB_RIGHT(elm, field)) { \
672 elm = RB_RIGHT(elm, field); \
673 while (RB_LEFT(elm, field)) \
674 elm = RB_LEFT(elm, field); \
676 if (RB_PARENT(elm, field) && \
677 (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
678 elm = RB_PARENT(elm, field); \
680 while (RB_PARENT(elm, field) && \
681 (elm == RB_RIGHT(RB_PARENT(elm, field), field)))\
682 elm = RB_PARENT(elm, field); \
683 elm = RB_PARENT(elm, field); \
691name##_RB_PREV(struct type *elm) \
693 if (RB_LEFT(elm, field)) { \
694 elm = RB_LEFT(elm, field); \
695 while (RB_RIGHT(elm, field)) \
696 elm = RB_RIGHT(elm, field); \
698 if (RB_PARENT(elm, field) && \
699 (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
700 elm = RB_PARENT(elm, field); \
702 while (RB_PARENT(elm, field) && \
703 (elm == RB_LEFT(RB_PARENT(elm, field), field)))\
704 elm = RB_PARENT(elm, field); \
705 elm = RB_PARENT(elm, field); \
712name##_RB_MINMAX(struct name *head, int val) \
714 struct type *tmp = RB_ROOT(head); \
715 struct type *parent = NULL; \
719 tmp = RB_LEFT(tmp, field); \
721 tmp = RB_RIGHT(tmp, field); \
729#define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
730#define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
731#define RB_FIND(name, x, y) name##_RB_FIND(x, y)
732#define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
733#define RB_NEXT(name, x, y) name##_RB_NEXT(y)
734#define RB_PREV(name, x, y) name##_RB_PREV(y)
735#define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
736#define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
738#define RB_FOREACH(x, name, head) \
739 for ((x) = RB_MIN(name, head); \
741 (x) = name##_RB_NEXT(x))
743#define RB_FOREACH_SAFE(x, name, head, y) \
744 for ((x) = RB_MIN(name, head); \
745 ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
748#define RB_FOREACH_REVERSE(x, name, head) \
749 for ((x) = RB_MAX(name, head); \
751 (x) = name##_RB_PREV(x))
753#define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
754 for ((x) = RB_MAX(name, head); \
755 ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \