]> git.proxmox.com Git - mirror_frr.git/blob - lib/openbsd-tree.h
Merge branch 'stable/3.0' into tmp-3.0-master-merge
[mirror_frr.git] / lib / openbsd-tree.h
1 /* $OpenBSD: tree.h,v 1.14 2015/05/25 03:07:49 deraadt Exp $ */
2 /*
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef _SYS_TREE_H_
28 #define _SYS_TREE_H_
29
30 /*
31 * This file defines data structures for different types of trees:
32 * splay trees and red-black trees.
33 *
34 * A splay tree is a self-organizing data structure. Every operation
35 * on the tree causes a splay to happen. The splay moves the requested
36 * node to the root of the tree and partly rebalances it.
37 *
38 * This has the benefit that request locality causes faster lookups as
39 * the requested nodes move to the top of the tree. On the other hand,
40 * every lookup causes memory writes.
41 *
42 * The Balance Theorem bounds the total access time for m operations
43 * and n inserts on an initially empty tree as O((m + n)lg n). The
44 * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
45 *
46 * A red-black tree is a binary search tree with the node color as an
47 * extra attribute. It fulfills a set of conditions:
48 * - every search path from the root to a leaf consists of the
49 * same number of black nodes,
50 * - each red node (except for the root) has a black parent,
51 * - each leaf node is black.
52 *
53 * Every operation on a red-black tree is bounded as O(lg n).
54 * The maximum height of a red-black tree is 2lg (n+1).
55 */
56
57 #define SPLAY_HEAD(name, type) \
58 struct name { \
59 struct type *sph_root; /* root of the tree */ \
60 }
61
62 #define SPLAY_INITIALIZER(root) \
63 { NULL }
64
65 #define SPLAY_INIT(root) do { \
66 (root)->sph_root = NULL; \
67 } while (0)
68
69 #define SPLAY_ENTRY(type) \
70 struct { \
71 struct type *spe_left; /* left element */ \
72 struct type *spe_right; /* right element */ \
73 }
74
75 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
76 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
77 #define SPLAY_ROOT(head) (head)->sph_root
78 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
79
80 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
81 #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
82 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
83 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
84 (head)->sph_root = tmp; \
85 } while (0)
86
87 #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
88 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
89 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
90 (head)->sph_root = tmp; \
91 } while (0)
92
93 #define SPLAY_LINKLEFT(head, tmp, field) do { \
94 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
95 tmp = (head)->sph_root; \
96 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
97 } while (0)
98
99 #define SPLAY_LINKRIGHT(head, tmp, field) do { \
100 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
101 tmp = (head)->sph_root; \
102 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
103 } while (0)
104
105 #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
106 SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
107 SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
108 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
109 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
110 } while (0)
111
112 /* Generates prototypes and inline functions */
113
114 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
115 void name##_SPLAY(struct name *, struct type *); \
116 void name##_SPLAY_MINMAX(struct name *, int); \
117 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
118 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
119 \
120 /* Finds the node with the same key as elm */ \
121 static __inline struct type * \
122 name##_SPLAY_FIND(struct name *head, struct type *elm) \
123 { \
124 if (SPLAY_EMPTY(head)) \
125 return(NULL); \
126 name##_SPLAY(head, elm); \
127 if ((cmp)(elm, (head)->sph_root) == 0) \
128 return (head->sph_root); \
129 return (NULL); \
130 } \
131 \
132 static __inline struct type * \
133 name##_SPLAY_NEXT(struct name *head, struct type *elm) \
134 { \
135 name##_SPLAY(head, elm); \
136 if (SPLAY_RIGHT(elm, field) != NULL) { \
137 elm = SPLAY_RIGHT(elm, field); \
138 while (SPLAY_LEFT(elm, field) != NULL) { \
139 elm = SPLAY_LEFT(elm, field); \
140 } \
141 } else \
142 elm = NULL; \
143 return (elm); \
144 } \
145 \
146 static __inline struct type * \
147 name##_SPLAY_MIN_MAX(struct name *head, int val) \
148 { \
149 name##_SPLAY_MINMAX(head, val); \
150 return (SPLAY_ROOT(head)); \
151 }
152
153 /* Main splay operation.
154 * Moves node close to the key of elm to top
155 */
156 #define SPLAY_GENERATE(name, type, field, cmp) \
157 struct type * \
158 name##_SPLAY_INSERT(struct name *head, struct type *elm) \
159 { \
160 if (SPLAY_EMPTY(head)) { \
161 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
162 } else { \
163 int __comp; \
164 name##_SPLAY(head, elm); \
165 __comp = (cmp)(elm, (head)->sph_root); \
166 if(__comp < 0) { \
167 SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
168 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
169 SPLAY_LEFT((head)->sph_root, field) = NULL; \
170 } else if (__comp > 0) { \
171 SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
172 SPLAY_LEFT(elm, field) = (head)->sph_root; \
173 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
174 } else \
175 return ((head)->sph_root); \
176 } \
177 (head)->sph_root = (elm); \
178 return (NULL); \
179 } \
180 \
181 struct type * \
182 name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
183 { \
184 struct type *__tmp; \
185 if (SPLAY_EMPTY(head)) \
186 return (NULL); \
187 name##_SPLAY(head, elm); \
188 if ((cmp)(elm, (head)->sph_root) == 0) { \
189 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
190 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
191 } else { \
192 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
193 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
194 name##_SPLAY(head, elm); \
195 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
196 } \
197 return (elm); \
198 } \
199 return (NULL); \
200 } \
201 \
202 void \
203 name##_SPLAY(struct name *head, struct type *elm) \
204 { \
205 struct type __node, *__left, *__right, *__tmp; \
206 int __comp; \
207 \
208 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
209 __left = __right = &__node; \
210 \
211 while ((__comp = (cmp)(elm, (head)->sph_root))) { \
212 if (__comp < 0) { \
213 __tmp = SPLAY_LEFT((head)->sph_root, field); \
214 if (__tmp == NULL) \
215 break; \
216 if ((cmp)(elm, __tmp) < 0){ \
217 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
218 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
219 break; \
220 } \
221 SPLAY_LINKLEFT(head, __right, field); \
222 } else if (__comp > 0) { \
223 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
224 if (__tmp == NULL) \
225 break; \
226 if ((cmp)(elm, __tmp) > 0){ \
227 SPLAY_ROTATE_LEFT(head, __tmp, field); \
228 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
229 break; \
230 } \
231 SPLAY_LINKRIGHT(head, __left, field); \
232 } \
233 } \
234 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
235 } \
236 \
237 /* Splay with either the minimum or the maximum element \
238 * Used to find minimum or maximum element in tree. \
239 */ \
240 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
241 { \
242 struct type __node, *__left, *__right, *__tmp; \
243 \
244 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL;\
245 __left = __right = &__node; \
246 \
247 while (1) { \
248 if (__comp < 0) { \
249 __tmp = SPLAY_LEFT((head)->sph_root, field); \
250 if (__tmp == NULL) \
251 break; \
252 if (__comp < 0){ \
253 SPLAY_ROTATE_RIGHT(head, __tmp, field); \
254 if (SPLAY_LEFT((head)->sph_root, field) == NULL)\
255 break; \
256 } \
257 SPLAY_LINKLEFT(head, __right, field); \
258 } else if (__comp > 0) { \
259 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
260 if (__tmp == NULL) \
261 break; \
262 if (__comp > 0) { \
263 SPLAY_ROTATE_LEFT(head, __tmp, field); \
264 if (SPLAY_RIGHT((head)->sph_root, field) == NULL)\
265 break; \
266 } \
267 SPLAY_LINKRIGHT(head, __left, field); \
268 } \
269 } \
270 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
271 }
272
273 #define SPLAY_NEGINF -1
274 #define SPLAY_INF 1
275
276 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
277 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
278 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
279 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
280 #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? NULL \
281 : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
282 #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? NULL \
283 : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
284
285 #define SPLAY_FOREACH(x, name, head) \
286 for ((x) = SPLAY_MIN(name, head); \
287 (x) != NULL; \
288 (x) = SPLAY_NEXT(name, head, x))
289
290 /*
291 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
292 *
293 * Permission to use, copy, modify, and distribute this software for any
294 * purpose with or without fee is hereby granted, provided that the above
295 * copyright notice and this permission notice appear in all copies.
296 *
297 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
298 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
299 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
300 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
301 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
302 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
303 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
304 */
305
306 #define RB_BLACK 0
307 #define RB_RED 1
308
309 struct rb_type {
310 int (*t_compare)(const void *, const void *);
311 void (*t_augment)(void *);
312 unsigned int t_offset; /* offset of rb_entry in type */
313 };
314
315 struct rbt_tree {
316 struct rb_entry *rbt_root;
317 };
318
319 struct rb_entry {
320 struct rb_entry *rbt_parent;
321 struct rb_entry *rbt_left;
322 struct rb_entry *rbt_right;
323 unsigned int rbt_color;
324 };
325
326 #define RB_HEAD(_name, _type) \
327 struct _name { \
328 struct rbt_tree rbh_root; \
329 }
330
331 #define RB_ENTRY(_type) struct rb_entry
332
333 static inline void
334 _rb_init(struct rbt_tree *rbt)
335 {
336 rbt->rbt_root = NULL;
337 }
338
339 static inline int
340 _rb_empty(struct rbt_tree *rbt)
341 {
342 return (rbt->rbt_root == NULL);
343 }
344
345 void *_rb_insert(const struct rb_type *, struct rbt_tree *, void *);
346 void *_rb_remove(const struct rb_type *, struct rbt_tree *, void *);
347 void *_rb_find(const struct rb_type *, struct rbt_tree *, const void *);
348 void *_rb_nfind(const struct rb_type *, struct rbt_tree *, const void *);
349 void *_rb_root(const struct rb_type *, struct rbt_tree *);
350 void *_rb_min(const struct rb_type *, struct rbt_tree *);
351 void *_rb_max(const struct rb_type *, struct rbt_tree *);
352 void *_rb_next(const struct rb_type *, void *);
353 void *_rb_prev(const struct rb_type *, void *);
354 void *_rb_left(const struct rb_type *, void *);
355 void *_rb_right(const struct rb_type *, void *);
356 void *_rb_parent(const struct rb_type *, void *);
357 void _rb_set_left(const struct rb_type *, void *, void *);
358 void _rb_set_right(const struct rb_type *, void *, void *);
359 void _rb_set_parent(const struct rb_type *, void *, void *);
360 void _rb_poison(const struct rb_type *, void *, unsigned long);
361 int _rb_check(const struct rb_type *, void *, unsigned long);
362
363 #define RB_INITIALIZER(_head) { { NULL } }
364
365 #define RB_PROTOTYPE(_name, _type, _field, _cmp) \
366 extern const struct rb_type *const _name##_RB_TYPE; \
367 \
368 __attribute__((__unused__)) static inline void \
369 _name##_RB_INIT(struct _name *head) \
370 { \
371 _rb_init(&head->rbh_root); \
372 } \
373 \
374 __attribute__((__unused__)) static inline struct _type * \
375 _name##_RB_INSERT(struct _name *head, struct _type *elm) \
376 { \
377 return _rb_insert(_name##_RB_TYPE, &head->rbh_root, elm); \
378 } \
379 \
380 __attribute__((__unused__)) static inline struct _type * \
381 _name##_RB_REMOVE(struct _name *head, struct _type *elm) \
382 { \
383 return _rb_remove(_name##_RB_TYPE, &head->rbh_root, elm); \
384 } \
385 \
386 __attribute__((__unused__)) static inline struct _type * \
387 _name##_RB_FIND(struct _name *head, const struct _type *key) \
388 { \
389 return _rb_find(_name##_RB_TYPE, &head->rbh_root, key); \
390 } \
391 \
392 __attribute__((__unused__)) static inline struct _type * \
393 _name##_RB_NFIND(struct _name *head, const struct _type *key) \
394 { \
395 return _rb_nfind(_name##_RB_TYPE, &head->rbh_root, key); \
396 } \
397 \
398 __attribute__((__unused__)) static inline struct _type * \
399 _name##_RB_ROOT(struct _name *head) \
400 { \
401 return _rb_root(_name##_RB_TYPE, &head->rbh_root); \
402 } \
403 \
404 __attribute__((__unused__)) static inline int \
405 _name##_RB_EMPTY(struct _name *head) \
406 { \
407 return _rb_empty(&head->rbh_root); \
408 } \
409 \
410 __attribute__((__unused__)) static inline struct _type * \
411 _name##_RB_MIN(struct _name *head) \
412 { \
413 return _rb_min(_name##_RB_TYPE, &head->rbh_root); \
414 } \
415 \
416 __attribute__((__unused__)) static inline struct _type * \
417 _name##_RB_MAX(struct _name *head) \
418 { \
419 return _rb_max(_name##_RB_TYPE, &head->rbh_root); \
420 } \
421 \
422 __attribute__((__unused__)) static inline struct _type * \
423 _name##_RB_NEXT(struct _type *elm) \
424 { \
425 return _rb_next(_name##_RB_TYPE, elm); \
426 } \
427 \
428 __attribute__((__unused__)) static inline struct _type * \
429 _name##_RB_PREV(struct _type *elm) \
430 { \
431 return _rb_prev(_name##_RB_TYPE, elm); \
432 } \
433 \
434 __attribute__((__unused__)) static inline struct _type * \
435 _name##_RB_LEFT(struct _type *elm) \
436 { \
437 return _rb_left(_name##_RB_TYPE, elm); \
438 } \
439 \
440 __attribute__((__unused__)) static inline struct _type * \
441 _name##_RB_RIGHT(struct _type *elm) \
442 { \
443 return _rb_right(_name##_RB_TYPE, elm); \
444 } \
445 \
446 __attribute__((__unused__)) static inline struct _type * \
447 _name##_RB_PARENT(struct _type *elm) \
448 { \
449 return _rb_parent(_name##_RB_TYPE, elm); \
450 } \
451 \
452 __attribute__((__unused__)) static inline void \
453 _name##_RB_SET_LEFT(struct _type *elm, struct _type *left) \
454 { \
455 return _rb_set_left(_name##_RB_TYPE, elm, left); \
456 } \
457 \
458 __attribute__((__unused__)) static inline void \
459 _name##_RB_SET_RIGHT(struct _type *elm, struct _type *right) \
460 { \
461 return _rb_set_right(_name##_RB_TYPE, elm, right); \
462 } \
463 \
464 __attribute__((__unused__)) static inline void \
465 _name##_RB_SET_PARENT(struct _type *elm, struct _type *parent) \
466 { \
467 return _rb_set_parent(_name##_RB_TYPE, elm, parent); \
468 } \
469 \
470 __attribute__((__unused__)) static inline void \
471 _name##_RB_POISON(struct _type *elm, unsigned long poison) \
472 { \
473 return _rb_poison(_name##_RB_TYPE, elm, poison); \
474 } \
475 \
476 __attribute__((__unused__)) static inline int \
477 _name##_RB_CHECK(struct _type *elm, unsigned long poison) \
478 { \
479 return _rb_check(_name##_RB_TYPE, elm, poison); \
480 }
481
482 #define RB_GENERATE_INTERNAL(_name, _type, _field, _cmp, _aug) \
483 static int \
484 _name##_RB_COMPARE(const void *lptr, const void *rptr) \
485 { \
486 const struct _type *l = lptr, *r = rptr; \
487 return _cmp(l, r); \
488 } \
489 static const struct rb_type _name##_RB_INFO = { \
490 _name##_RB_COMPARE, \
491 _aug, \
492 offsetof(struct _type, _field), \
493 }; \
494 const struct rb_type *const _name##_RB_TYPE = &_name##_RB_INFO;
495
496 #define RB_GENERATE_AUGMENT(_name, _type, _field, _cmp, _aug) \
497 static void \
498 _name##_RB_AUGMENT(void *ptr) \
499 { \
500 struct _type *p = ptr; \
501 return _aug(p); \
502 } \
503 RB_GENERATE_INTERNAL(_name, _type, _field, _cmp, _name##_RB_AUGMENT)
504
505 #define RB_GENERATE(_name, _type, _field, _cmp) \
506 RB_GENERATE_INTERNAL(_name, _type, _field, _cmp, NULL)
507
508 #define RB_INIT(_name, _head) _name##_RB_INIT(_head)
509 #define RB_INSERT(_name, _head, _elm) _name##_RB_INSERT(_head, _elm)
510 #define RB_REMOVE(_name, _head, _elm) _name##_RB_REMOVE(_head, _elm)
511 #define RB_FIND(_name, _head, _key) _name##_RB_FIND(_head, _key)
512 #define RB_NFIND(_name, _head, _key) _name##_RB_NFIND(_head, _key)
513 #define RB_ROOT(_name, _head) _name##_RB_ROOT(_head)
514 #define RB_EMPTY(_name, _head) _name##_RB_EMPTY(_head)
515 #define RB_MIN(_name, _head) _name##_RB_MIN(_head)
516 #define RB_MAX(_name, _head) _name##_RB_MAX(_head)
517 #define RB_NEXT(_name, _elm) _name##_RB_NEXT(_elm)
518 #define RB_PREV(_name, _elm) _name##_RB_PREV(_elm)
519 #define RB_LEFT(_name, _elm) _name##_RB_LEFT(_elm)
520 #define RB_RIGHT(_name, _elm) _name##_RB_RIGHT(_elm)
521 #define RB_PARENT(_name, _elm) _name##_RB_PARENT(_elm)
522 #define RB_SET_LEFT(_name, _elm, _l) _name##_RB_SET_LEFT(_elm, _l)
523 #define RB_SET_RIGHT(_name, _elm, _r) _name##_RB_SET_RIGHT(_elm, _r)
524 #define RB_SET_PARENT(_name, _elm, _p) _name##_RB_SET_PARENT(_elm, _p)
525 #define RB_POISON(_name, _elm, _p) _name##_RB_POISON(_elm, _p)
526 #define RB_CHECK(_name, _elm, _p) _name##_RB_CHECK(_elm, _p)
527
528 #define RB_FOREACH(_e, _name, _head) \
529 for ((_e) = RB_MIN(_name, (_head)); \
530 (_e) != NULL; \
531 (_e) = RB_NEXT(_name, (_e)))
532
533 #define RB_FOREACH_SAFE(_e, _name, _head, _n) \
534 for ((_e) = RB_MIN(_name, (_head)); \
535 (_e) != NULL && ((_n) = RB_NEXT(_name, (_e)), 1); \
536 (_e) = (_n))
537
538 #define RB_FOREACH_REVERSE(_e, _name, _head) \
539 for ((_e) = RB_MAX(_name, (_head)); \
540 (_e) != NULL; \
541 (_e) = RB_PREV(_name, (_e)))
542
543 #define RB_FOREACH_REVERSE_SAFE(_e, _name, _head, _n) \
544 for ((_e) = RB_MAX(_name, (_head)); \
545 (_e) != NULL && ((_n) = RB_PREV(_name, (_e)), 1); \
546 (_e) = (_n))
547
548 #endif /* _SYS_TREE_H_ */