]>
git.saurik.com Git - redis.git/blob - deps/jemalloc.orig/include/jemalloc/internal/rb.h
2 *******************************************************************************
4 * cpp macro implementation of left-leaning 2-3 red-black trees. Parent
5 * pointers are not used, and color bits are stored in the least significant
6 * bit of right-child pointers (if RB_COMPACT is defined), thus making node
7 * linkage as compact as is possible for red-black trees.
12 * #include <stdbool.h>
13 * #define NDEBUG // (Optional, see assert(3).)
15 * #define RB_COMPACT // (Optional, embed color bits in right-child pointers.)
19 *******************************************************************************
26 __FBSDID("$FreeBSD: head/lib/libc/stdlib/rb.h 204493 2010-02-28 22:57:13Z jasone $");
31 #define rb_node(a_type) \
34 a_type *rbn_right_red; \
37 #define rb_node(a_type) \
46 #define rb_tree(a_type) \
53 #define rbtn_left_get(a_type, a_field, a_node) \
54 ((a_node)->a_field.rbn_left)
55 #define rbtn_left_set(a_type, a_field, a_node, a_left) do { \
56 (a_node)->a_field.rbn_left = a_left; \
60 /* Right accessors. */
61 #define rbtn_right_get(a_type, a_field, a_node) \
62 ((a_type *) (((intptr_t) (a_node)->a_field.rbn_right_red) \
64 #define rbtn_right_set(a_type, a_field, a_node, a_right) do { \
65 (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t) a_right) \
66 | (((uintptr_t) (a_node)->a_field.rbn_right_red) & ((size_t)1))); \
69 /* Color accessors. */
70 #define rbtn_red_get(a_type, a_field, a_node) \
71 ((bool) (((uintptr_t) (a_node)->a_field.rbn_right_red) \
73 #define rbtn_color_set(a_type, a_field, a_node, a_red) do { \
74 (a_node)->a_field.rbn_right_red = (a_type *) ((((intptr_t) \
75 (a_node)->a_field.rbn_right_red) & ((ssize_t)-2)) \
76 | ((ssize_t)a_red)); \
78 #define rbtn_red_set(a_type, a_field, a_node) do { \
79 (a_node)->a_field.rbn_right_red = (a_type *) (((uintptr_t) \
80 (a_node)->a_field.rbn_right_red) | ((size_t)1)); \
82 #define rbtn_black_set(a_type, a_field, a_node) do { \
83 (a_node)->a_field.rbn_right_red = (a_type *) (((intptr_t) \
84 (a_node)->a_field.rbn_right_red) & ((ssize_t)-2)); \
87 /* Right accessors. */
88 #define rbtn_right_get(a_type, a_field, a_node) \
89 ((a_node)->a_field.rbn_right)
90 #define rbtn_right_set(a_type, a_field, a_node, a_right) do { \
91 (a_node)->a_field.rbn_right = a_right; \
94 /* Color accessors. */
95 #define rbtn_red_get(a_type, a_field, a_node) \
96 ((a_node)->a_field.rbn_red)
97 #define rbtn_color_set(a_type, a_field, a_node, a_red) do { \
98 (a_node)->a_field.rbn_red = (a_red); \
100 #define rbtn_red_set(a_type, a_field, a_node) do { \
101 (a_node)->a_field.rbn_red = true; \
103 #define rbtn_black_set(a_type, a_field, a_node) do { \
104 (a_node)->a_field.rbn_red = false; \
108 /* Node initializer. */
109 #define rbt_node_new(a_type, a_field, a_rbt, a_node) do { \
110 rbtn_left_set(a_type, a_field, (a_node), &(a_rbt)->rbt_nil); \
111 rbtn_right_set(a_type, a_field, (a_node), &(a_rbt)->rbt_nil); \
112 rbtn_red_set(a_type, a_field, (a_node)); \
115 /* Tree initializer. */
116 #define rb_new(a_type, a_field, a_rbt) do { \
117 (a_rbt)->rbt_root = &(a_rbt)->rbt_nil; \
118 rbt_node_new(a_type, a_field, a_rbt, &(a_rbt)->rbt_nil); \
119 rbtn_black_set(a_type, a_field, &(a_rbt)->rbt_nil); \
122 /* Internal utility macros. */
123 #define rbtn_first(a_type, a_field, a_rbt, a_root, r_node) do { \
124 (r_node) = (a_root); \
125 if ((r_node) != &(a_rbt)->rbt_nil) { \
127 rbtn_left_get(a_type, a_field, (r_node)) != &(a_rbt)->rbt_nil;\
128 (r_node) = rbtn_left_get(a_type, a_field, (r_node))) { \
133 #define rbtn_last(a_type, a_field, a_rbt, a_root, r_node) do { \
134 (r_node) = (a_root); \
135 if ((r_node) != &(a_rbt)->rbt_nil) { \
136 for (; rbtn_right_get(a_type, a_field, (r_node)) != \
137 &(a_rbt)->rbt_nil; (r_node) = rbtn_right_get(a_type, a_field, \
143 #define rbtn_rotate_left(a_type, a_field, a_node, r_node) do { \
144 (r_node) = rbtn_right_get(a_type, a_field, (a_node)); \
145 rbtn_right_set(a_type, a_field, (a_node), \
146 rbtn_left_get(a_type, a_field, (r_node))); \
147 rbtn_left_set(a_type, a_field, (r_node), (a_node)); \
150 #define rbtn_rotate_right(a_type, a_field, a_node, r_node) do { \
151 (r_node) = rbtn_left_get(a_type, a_field, (a_node)); \
152 rbtn_left_set(a_type, a_field, (a_node), \
153 rbtn_right_get(a_type, a_field, (r_node))); \
154 rbtn_right_set(a_type, a_field, (r_node), (a_node)); \
158 * The rb_proto() macro generates function prototypes that correspond to the
159 * functions generated by an equivalently parameterized call to rb_gen().
162 #define rb_proto(a_attr, a_prefix, a_rbt_type, a_type) \
164 a_prefix##new(a_rbt_type *rbtree); \
166 a_prefix##first(a_rbt_type *rbtree); \
168 a_prefix##last(a_rbt_type *rbtree); \
170 a_prefix##next(a_rbt_type *rbtree, a_type *node); \
172 a_prefix##prev(a_rbt_type *rbtree, a_type *node); \
174 a_prefix##search(a_rbt_type *rbtree, a_type *key); \
176 a_prefix##nsearch(a_rbt_type *rbtree, a_type *key); \
178 a_prefix##psearch(a_rbt_type *rbtree, a_type *key); \
180 a_prefix##insert(a_rbt_type *rbtree, a_type *node); \
182 a_prefix##remove(a_rbt_type *rbtree, a_type *node); \
184 a_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)( \
185 a_rbt_type *, a_type *, void *), void *arg); \
187 a_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start, \
188 a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg);
191 * The rb_gen() macro generates a type-specific red-black tree implementation,
192 * based on the above cpp macros.
196 * a_attr : Function attribute for generated functions (ex: static).
197 * a_prefix : Prefix for generated functions (ex: ex_).
198 * a_rb_type : Type for red-black tree data structure (ex: ex_t).
199 * a_type : Type for red-black tree node data structure (ex: ex_node_t).
200 * a_field : Name of red-black tree node linkage (ex: ex_link).
201 * a_cmp : Node comparison function name, with the following prototype:
202 * int (a_cmp *)(a_type *a_node, a_type *a_other);
205 * Interpretation of comparision function return values:
206 * -1 : a_node < a_other
207 * 0 : a_node == a_other
208 * 1 : a_node > a_other
209 * In all cases, the a_node or a_key macro argument is the first
210 * argument to the comparison function, which makes it possible
211 * to write comparison functions that treat the first argument
214 * Assuming the following setup:
216 * typedef struct ex_node_s ex_node_t;
218 * rb_node(ex_node_t) ex_link;
220 * typedef rb_tree(ex_node_t) ex_t;
221 * rb_gen(static, ex_, ex_t, ex_node_t, ex_link, ex_cmp)
223 * The following API is generated:
226 * ex_new(ex_t *extree);
227 * Description: Initialize a red-black tree structure.
229 * extree: Pointer to an uninitialized red-black tree object.
232 * ex_first(ex_t *extree);
234 * ex_last(ex_t *extree);
235 * Description: Get the first/last node in extree.
237 * extree: Pointer to an initialized red-black tree object.
238 * Ret: First/last node in extree, or NULL if extree is empty.
241 * ex_next(ex_t *extree, ex_node_t *node);
243 * ex_prev(ex_t *extree, ex_node_t *node);
244 * Description: Get node's successor/predecessor.
246 * extree: Pointer to an initialized red-black tree object.
247 * node : A node in extree.
248 * Ret: node's successor/predecessor in extree, or NULL if node is
252 * ex_search(ex_t *extree, ex_node_t *key);
253 * Description: Search for node that matches key.
255 * extree: Pointer to an initialized red-black tree object.
257 * Ret: Node in extree that matches key, or NULL if no match.
260 * ex_nsearch(ex_t *extree, ex_node_t *key);
262 * ex_psearch(ex_t *extree, ex_node_t *key);
263 * Description: Search for node that matches key. If no match is found,
264 * return what would be key's successor/predecessor, were
267 * extree: Pointer to an initialized red-black tree object.
269 * Ret: Node in extree that matches key, or if no match, hypothetical
270 * node's successor/predecessor (NULL if no successor/predecessor).
273 * ex_insert(ex_t *extree, ex_node_t *node);
274 * Description: Insert node into extree.
276 * extree: Pointer to an initialized red-black tree object.
277 * node : Node to be inserted into extree.
280 * ex_remove(ex_t *extree, ex_node_t *node);
281 * Description: Remove node from extree.
283 * extree: Pointer to an initialized red-black tree object.
284 * node : Node in extree to be removed.
287 * ex_iter(ex_t *extree, ex_node_t *start, ex_node_t *(*cb)(ex_t *,
288 * ex_node_t *, void *), void *arg);
290 * ex_reverse_iter(ex_t *extree, ex_node_t *start, ex_node *(*cb)(ex_t *,
291 * ex_node_t *, void *), void *arg);
292 * Description: Iterate forward/backward over extree, starting at node.
293 * If extree is modified, iteration must be immediately
294 * terminated by the callback function that causes the
297 * extree: Pointer to an initialized red-black tree object.
298 * start : Node at which to start iteration, or NULL to start at
300 * cb : Callback function, which is called for each node during
301 * iteration. Under normal circumstances the callback function
302 * should return NULL, which causes iteration to continue. If a
303 * callback function returns non-NULL, iteration is immediately
304 * terminated and the non-NULL return value is returned by the
305 * iterator. This is useful for re-starting iteration after
307 * arg : Opaque pointer passed to cb().
308 * Ret: NULL if iteration completed, or the non-NULL callback return value
309 * that caused termination of the iteration.
311 #define rb_gen(a_attr, a_prefix, a_rbt_type, a_type, a_field, a_cmp) \
313 a_prefix##new(a_rbt_type *rbtree) { \
314 rb_new(a_type, a_field, rbtree); \
317 a_prefix##first(a_rbt_type *rbtree) { \
319 rbtn_first(a_type, a_field, rbtree, rbtree->rbt_root, ret); \
320 if (ret == &rbtree->rbt_nil) { \
326 a_prefix##last(a_rbt_type *rbtree) { \
328 rbtn_last(a_type, a_field, rbtree, rbtree->rbt_root, ret); \
329 if (ret == &rbtree->rbt_nil) { \
335 a_prefix##next(a_rbt_type *rbtree, a_type *node) { \
337 if (rbtn_right_get(a_type, a_field, node) != &rbtree->rbt_nil) { \
338 rbtn_first(a_type, a_field, rbtree, rbtn_right_get(a_type, \
339 a_field, node), ret); \
341 a_type *tnode = rbtree->rbt_root; \
342 assert(tnode != &rbtree->rbt_nil); \
343 ret = &rbtree->rbt_nil; \
345 int cmp = (a_cmp)(node, tnode); \
348 tnode = rbtn_left_get(a_type, a_field, tnode); \
349 } else if (cmp > 0) { \
350 tnode = rbtn_right_get(a_type, a_field, tnode); \
354 assert(tnode != &rbtree->rbt_nil); \
357 if (ret == &rbtree->rbt_nil) { \
363 a_prefix##prev(a_rbt_type *rbtree, a_type *node) { \
365 if (rbtn_left_get(a_type, a_field, node) != &rbtree->rbt_nil) { \
366 rbtn_last(a_type, a_field, rbtree, rbtn_left_get(a_type, \
367 a_field, node), ret); \
369 a_type *tnode = rbtree->rbt_root; \
370 assert(tnode != &rbtree->rbt_nil); \
371 ret = &rbtree->rbt_nil; \
373 int cmp = (a_cmp)(node, tnode); \
375 tnode = rbtn_left_get(a_type, a_field, tnode); \
376 } else if (cmp > 0) { \
378 tnode = rbtn_right_get(a_type, a_field, tnode); \
382 assert(tnode != &rbtree->rbt_nil); \
385 if (ret == &rbtree->rbt_nil) { \
391 a_prefix##search(a_rbt_type *rbtree, a_type *key) { \
394 ret = rbtree->rbt_root; \
395 while (ret != &rbtree->rbt_nil \
396 && (cmp = (a_cmp)(key, ret)) != 0) { \
398 ret = rbtn_left_get(a_type, a_field, ret); \
400 ret = rbtn_right_get(a_type, a_field, ret); \
403 if (ret == &rbtree->rbt_nil) { \
409 a_prefix##nsearch(a_rbt_type *rbtree, a_type *key) { \
411 a_type *tnode = rbtree->rbt_root; \
412 ret = &rbtree->rbt_nil; \
413 while (tnode != &rbtree->rbt_nil) { \
414 int cmp = (a_cmp)(key, tnode); \
417 tnode = rbtn_left_get(a_type, a_field, tnode); \
418 } else if (cmp > 0) { \
419 tnode = rbtn_right_get(a_type, a_field, tnode); \
425 if (ret == &rbtree->rbt_nil) { \
431 a_prefix##psearch(a_rbt_type *rbtree, a_type *key) { \
433 a_type *tnode = rbtree->rbt_root; \
434 ret = &rbtree->rbt_nil; \
435 while (tnode != &rbtree->rbt_nil) { \
436 int cmp = (a_cmp)(key, tnode); \
438 tnode = rbtn_left_get(a_type, a_field, tnode); \
439 } else if (cmp > 0) { \
441 tnode = rbtn_right_get(a_type, a_field, tnode); \
447 if (ret == &rbtree->rbt_nil) { \
453 a_prefix##insert(a_rbt_type *rbtree, a_type *node) { \
457 } path[sizeof(void *) << 4], *pathp; \
458 rbt_node_new(a_type, a_field, rbtree, node); \
460 path->node = rbtree->rbt_root; \
461 for (pathp = path; pathp->node != &rbtree->rbt_nil; pathp++) { \
462 int cmp = pathp->cmp = a_cmp(node, pathp->node); \
465 pathp[1].node = rbtn_left_get(a_type, a_field, \
468 pathp[1].node = rbtn_right_get(a_type, a_field, \
472 pathp->node = node; \
474 for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) { \
475 a_type *cnode = pathp->node; \
476 if (pathp->cmp < 0) { \
477 a_type *left = pathp[1].node; \
478 rbtn_left_set(a_type, a_field, cnode, left); \
479 if (rbtn_red_get(a_type, a_field, left)) { \
480 a_type *leftleft = rbtn_left_get(a_type, a_field, left);\
481 if (rbtn_red_get(a_type, a_field, leftleft)) { \
482 /* Fix up 4-node. */ \
484 rbtn_black_set(a_type, a_field, leftleft); \
485 rbtn_rotate_right(a_type, a_field, cnode, tnode); \
492 a_type *right = pathp[1].node; \
493 rbtn_right_set(a_type, a_field, cnode, right); \
494 if (rbtn_red_get(a_type, a_field, right)) { \
495 a_type *left = rbtn_left_get(a_type, a_field, cnode); \
496 if (rbtn_red_get(a_type, a_field, left)) { \
497 /* Split 4-node. */ \
498 rbtn_black_set(a_type, a_field, left); \
499 rbtn_black_set(a_type, a_field, right); \
500 rbtn_red_set(a_type, a_field, cnode); \
504 bool tred = rbtn_red_get(a_type, a_field, cnode); \
505 rbtn_rotate_left(a_type, a_field, cnode, tnode); \
506 rbtn_color_set(a_type, a_field, tnode, tred); \
507 rbtn_red_set(a_type, a_field, cnode); \
514 pathp->node = cnode; \
516 /* Set root, and make it black. */ \
517 rbtree->rbt_root = path->node; \
518 rbtn_black_set(a_type, a_field, rbtree->rbt_root); \
521 a_prefix##remove(a_rbt_type *rbtree, a_type *node) { \
525 } *pathp, *nodep, path[sizeof(void *) << 4]; \
527 nodep = NULL; /* Silence compiler warning. */ \
528 path->node = rbtree->rbt_root; \
529 for (pathp = path; pathp->node != &rbtree->rbt_nil; pathp++) { \
530 int cmp = pathp->cmp = a_cmp(node, pathp->node); \
532 pathp[1].node = rbtn_left_get(a_type, a_field, \
535 pathp[1].node = rbtn_right_get(a_type, a_field, \
538 /* Find node's successor, in preparation for swap. */ \
541 for (pathp++; pathp->node != &rbtree->rbt_nil; \
544 pathp[1].node = rbtn_left_get(a_type, a_field, \
551 assert(nodep->node == node); \
553 if (pathp->node != node) { \
554 /* Swap node with its successor. */ \
555 bool tred = rbtn_red_get(a_type, a_field, pathp->node); \
556 rbtn_color_set(a_type, a_field, pathp->node, \
557 rbtn_red_get(a_type, a_field, node)); \
558 rbtn_left_set(a_type, a_field, pathp->node, \
559 rbtn_left_get(a_type, a_field, node)); \
560 /* If node's successor is its right child, the following code */\
561 /* will do the wrong thing for the right child pointer. */\
562 /* However, it doesn't matter, because the pointer will be */\
563 /* properly set when the successor is pruned. */\
564 rbtn_right_set(a_type, a_field, pathp->node, \
565 rbtn_right_get(a_type, a_field, node)); \
566 rbtn_color_set(a_type, a_field, node, tred); \
567 /* The pruned leaf node's child pointers are never accessed */\
568 /* again, so don't bother setting them to nil. */\
569 nodep->node = pathp->node; \
570 pathp->node = node; \
571 if (nodep == path) { \
572 rbtree->rbt_root = nodep->node; \
574 if (nodep[-1].cmp < 0) { \
575 rbtn_left_set(a_type, a_field, nodep[-1].node, \
578 rbtn_right_set(a_type, a_field, nodep[-1].node, \
583 a_type *left = rbtn_left_get(a_type, a_field, node); \
584 if (left != &rbtree->rbt_nil) { \
585 /* node has no successor, but it has a left child. */\
586 /* Splice node out, without losing the left child. */\
587 assert(rbtn_red_get(a_type, a_field, node) == false); \
588 assert(rbtn_red_get(a_type, a_field, left)); \
589 rbtn_black_set(a_type, a_field, left); \
590 if (pathp == path) { \
591 rbtree->rbt_root = left; \
593 if (pathp[-1].cmp < 0) { \
594 rbtn_left_set(a_type, a_field, pathp[-1].node, \
597 rbtn_right_set(a_type, a_field, pathp[-1].node, \
602 } else if (pathp == path) { \
603 /* The tree only contained one node. */ \
604 rbtree->rbt_root = &rbtree->rbt_nil; \
608 if (rbtn_red_get(a_type, a_field, pathp->node)) { \
609 /* Prune red node, which requires no fixup. */ \
610 assert(pathp[-1].cmp < 0); \
611 rbtn_left_set(a_type, a_field, pathp[-1].node, \
615 /* The node to be pruned is black, so unwind until balance is */\
617 pathp->node = &rbtree->rbt_nil; \
618 for (pathp--; (uintptr_t)pathp >= (uintptr_t)path; pathp--) { \
619 assert(pathp->cmp != 0); \
620 if (pathp->cmp < 0) { \
621 rbtn_left_set(a_type, a_field, pathp->node, \
623 assert(rbtn_red_get(a_type, a_field, pathp[1].node) \
625 if (rbtn_red_get(a_type, a_field, pathp->node)) { \
626 a_type *right = rbtn_right_get(a_type, a_field, \
628 a_type *rightleft = rbtn_left_get(a_type, a_field, \
631 if (rbtn_red_get(a_type, a_field, rightleft)) { \
632 /* In the following diagrams, ||, //, and \\ */\
633 /* indicate the path to the removed node. */\
642 rbtn_black_set(a_type, a_field, pathp->node); \
643 rbtn_rotate_right(a_type, a_field, right, tnode); \
644 rbtn_right_set(a_type, a_field, pathp->node, tnode);\
645 rbtn_rotate_left(a_type, a_field, pathp->node, \
655 rbtn_rotate_left(a_type, a_field, pathp->node, \
658 /* Balance restored, but rotation modified subtree */\
660 assert((uintptr_t)pathp > (uintptr_t)path); \
661 if (pathp[-1].cmp < 0) { \
662 rbtn_left_set(a_type, a_field, pathp[-1].node, \
665 rbtn_right_set(a_type, a_field, pathp[-1].node, \
670 a_type *right = rbtn_right_get(a_type, a_field, \
672 a_type *rightleft = rbtn_left_get(a_type, a_field, \
674 if (rbtn_red_get(a_type, a_field, rightleft)) { \
682 rbtn_black_set(a_type, a_field, rightleft); \
683 rbtn_rotate_right(a_type, a_field, right, tnode); \
684 rbtn_right_set(a_type, a_field, pathp->node, tnode);\
685 rbtn_rotate_left(a_type, a_field, pathp->node, \
687 /* Balance restored, but rotation modified */\
688 /* subree root, which may actually be the tree */\
690 if (pathp == path) { \
692 rbtree->rbt_root = tnode; \
694 if (pathp[-1].cmp < 0) { \
695 rbtn_left_set(a_type, a_field, \
696 pathp[-1].node, tnode); \
698 rbtn_right_set(a_type, a_field, \
699 pathp[-1].node, tnode); \
711 rbtn_red_set(a_type, a_field, pathp->node); \
712 rbtn_rotate_left(a_type, a_field, pathp->node, \
714 pathp->node = tnode; \
719 rbtn_right_set(a_type, a_field, pathp->node, \
721 left = rbtn_left_get(a_type, a_field, pathp->node); \
722 if (rbtn_red_get(a_type, a_field, left)) { \
724 a_type *leftright = rbtn_right_get(a_type, a_field, \
726 a_type *leftrightleft = rbtn_left_get(a_type, a_field, \
728 if (rbtn_red_get(a_type, a_field, leftrightleft)) { \
738 rbtn_black_set(a_type, a_field, leftrightleft); \
739 rbtn_rotate_right(a_type, a_field, pathp->node, \
741 rbtn_rotate_right(a_type, a_field, pathp->node, \
743 rbtn_right_set(a_type, a_field, unode, tnode); \
744 rbtn_rotate_left(a_type, a_field, unode, tnode); \
754 assert(leftright != &rbtree->rbt_nil); \
755 rbtn_red_set(a_type, a_field, leftright); \
756 rbtn_rotate_right(a_type, a_field, pathp->node, \
758 rbtn_black_set(a_type, a_field, tnode); \
760 /* Balance restored, but rotation modified subtree */\
761 /* root, which may actually be the tree root. */\
762 if (pathp == path) { \
764 rbtree->rbt_root = tnode; \
766 if (pathp[-1].cmp < 0) { \
767 rbtn_left_set(a_type, a_field, pathp[-1].node, \
770 rbtn_right_set(a_type, a_field, pathp[-1].node, \
775 } else if (rbtn_red_get(a_type, a_field, pathp->node)) { \
776 a_type *leftleft = rbtn_left_get(a_type, a_field, left);\
777 if (rbtn_red_get(a_type, a_field, leftleft)) { \
785 rbtn_black_set(a_type, a_field, pathp->node); \
786 rbtn_red_set(a_type, a_field, left); \
787 rbtn_black_set(a_type, a_field, leftleft); \
788 rbtn_rotate_right(a_type, a_field, pathp->node, \
790 /* Balance restored, but rotation modified */\
792 assert((uintptr_t)pathp > (uintptr_t)path); \
793 if (pathp[-1].cmp < 0) { \
794 rbtn_left_set(a_type, a_field, pathp[-1].node, \
797 rbtn_right_set(a_type, a_field, pathp[-1].node, \
808 rbtn_red_set(a_type, a_field, left); \
809 rbtn_black_set(a_type, a_field, pathp->node); \
810 /* Balance restored. */ \
814 a_type *leftleft = rbtn_left_get(a_type, a_field, left);\
815 if (rbtn_red_get(a_type, a_field, leftleft)) { \
823 rbtn_black_set(a_type, a_field, leftleft); \
824 rbtn_rotate_right(a_type, a_field, pathp->node, \
826 /* Balance restored, but rotation modified */\
827 /* subtree root, which may actually be the tree */\
829 if (pathp == path) { \
831 rbtree->rbt_root = tnode; \
833 if (pathp[-1].cmp < 0) { \
834 rbtn_left_set(a_type, a_field, \
835 pathp[-1].node, tnode); \
837 rbtn_right_set(a_type, a_field, \
838 pathp[-1].node, tnode); \
849 rbtn_red_set(a_type, a_field, left); \
855 rbtree->rbt_root = path->node; \
856 assert(rbtn_red_get(a_type, a_field, rbtree->rbt_root) == false); \
859 a_prefix##iter_recurse(a_rbt_type *rbtree, a_type *node, \
860 a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) { \
861 if (node == &rbtree->rbt_nil) { \
862 return (&rbtree->rbt_nil); \
865 if ((ret = a_prefix##iter_recurse(rbtree, rbtn_left_get(a_type, \
866 a_field, node), cb, arg)) != &rbtree->rbt_nil \
867 || (ret = cb(rbtree, node, arg)) != NULL) { \
870 return (a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type, \
871 a_field, node), cb, arg)); \
875 a_prefix##iter_start(a_rbt_type *rbtree, a_type *start, a_type *node, \
876 a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) { \
877 int cmp = a_cmp(start, node); \
880 if ((ret = a_prefix##iter_start(rbtree, start, \
881 rbtn_left_get(a_type, a_field, node), cb, arg)) != \
882 &rbtree->rbt_nil || (ret = cb(rbtree, node, arg)) != NULL) { \
885 return (a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type, \
886 a_field, node), cb, arg)); \
887 } else if (cmp > 0) { \
888 return (a_prefix##iter_start(rbtree, start, \
889 rbtn_right_get(a_type, a_field, node), cb, arg)); \
892 if ((ret = cb(rbtree, node, arg)) != NULL) { \
895 return (a_prefix##iter_recurse(rbtree, rbtn_right_get(a_type, \
896 a_field, node), cb, arg)); \
900 a_prefix##iter(a_rbt_type *rbtree, a_type *start, a_type *(*cb)( \
901 a_rbt_type *, a_type *, void *), void *arg) { \
903 if (start != NULL) { \
904 ret = a_prefix##iter_start(rbtree, start, rbtree->rbt_root, \
907 ret = a_prefix##iter_recurse(rbtree, rbtree->rbt_root, cb, arg);\
909 if (ret == &rbtree->rbt_nil) { \
915 a_prefix##reverse_iter_recurse(a_rbt_type *rbtree, a_type *node, \
916 a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) { \
917 if (node == &rbtree->rbt_nil) { \
918 return (&rbtree->rbt_nil); \
921 if ((ret = a_prefix##reverse_iter_recurse(rbtree, \
922 rbtn_right_get(a_type, a_field, node), cb, arg)) != \
923 &rbtree->rbt_nil || (ret = cb(rbtree, node, arg)) != NULL) { \
926 return (a_prefix##reverse_iter_recurse(rbtree, \
927 rbtn_left_get(a_type, a_field, node), cb, arg)); \
931 a_prefix##reverse_iter_start(a_rbt_type *rbtree, a_type *start, \
932 a_type *node, a_type *(*cb)(a_rbt_type *, a_type *, void *), \
934 int cmp = a_cmp(start, node); \
937 if ((ret = a_prefix##reverse_iter_start(rbtree, start, \
938 rbtn_right_get(a_type, a_field, node), cb, arg)) != \
939 &rbtree->rbt_nil || (ret = cb(rbtree, node, arg)) != NULL) { \
942 return (a_prefix##reverse_iter_recurse(rbtree, \
943 rbtn_left_get(a_type, a_field, node), cb, arg)); \
944 } else if (cmp < 0) { \
945 return (a_prefix##reverse_iter_start(rbtree, start, \
946 rbtn_left_get(a_type, a_field, node), cb, arg)); \
949 if ((ret = cb(rbtree, node, arg)) != NULL) { \
952 return (a_prefix##reverse_iter_recurse(rbtree, \
953 rbtn_left_get(a_type, a_field, node), cb, arg)); \
957 a_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start, \
958 a_type *(*cb)(a_rbt_type *, a_type *, void *), void *arg) { \
960 if (start != NULL) { \
961 ret = a_prefix##reverse_iter_start(rbtree, start, \
962 rbtree->rbt_root, cb, arg); \
964 ret = a_prefix##reverse_iter_recurse(rbtree, rbtree->rbt_root, \
967 if (ret == &rbtree->rbt_nil) { \