]>
git.proxmox.com Git - mirror_frr.git/blob - lib/openbsd-tree.h
1 /* $OpenBSD: tree.h,v 1.14 2015/05/25 03:07:49 deraadt Exp $ */
3 * Copyright 2002 Niels Provos <provos@citi.umich.edu>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
31 * This file defines data structures for different types of trees:
32 * splay trees and red-black trees.
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.
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.
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);
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.
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).
57 #define SPLAY_HEAD(name, type) \
59 struct type *sph_root; /* root of the tree */ \
62 #define SPLAY_INITIALIZER(root) \
67 #define SPLAY_INIT(root) \
69 (root)->sph_root = NULL; \
72 #define SPLAY_ENTRY(type) \
74 struct type *spe_left; /* left element */ \
75 struct type *spe_right; /* right element */ \
78 #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
79 #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
80 #define SPLAY_ROOT(head) (head)->sph_root
81 #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
83 /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
84 #define SPLAY_ROTATE_RIGHT(head, tmp, field) \
86 SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
87 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
88 (head)->sph_root = tmp; \
91 #define SPLAY_ROTATE_LEFT(head, tmp, field) \
93 SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
94 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
95 (head)->sph_root = tmp; \
98 #define SPLAY_LINKLEFT(head, tmp, field) \
100 SPLAY_LEFT(tmp, field) = (head)->sph_root; \
101 tmp = (head)->sph_root; \
102 (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
105 #define SPLAY_LINKRIGHT(head, tmp, field) \
107 SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
108 tmp = (head)->sph_root; \
109 (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
112 #define SPLAY_ASSEMBLE(head, node, left, right, field) \
114 SPLAY_RIGHT(left, field) = \
115 SPLAY_LEFT((head)->sph_root, field); \
116 SPLAY_LEFT(right, field) = \
117 SPLAY_RIGHT((head)->sph_root, field); \
118 SPLAY_LEFT((head)->sph_root, field) = \
119 SPLAY_RIGHT(node, field); \
120 SPLAY_RIGHT((head)->sph_root, field) = \
121 SPLAY_LEFT(node, field); \
124 /* Generates prototypes and inline functions */
126 #define SPLAY_PROTOTYPE(name, type, field, cmp) \
127 void name##_SPLAY(struct name *, struct type *); \
128 void name##_SPLAY_MINMAX(struct name *, int); \
129 struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
130 struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
132 /* Finds the node with the same key as elm */ \
133 static __inline struct type *name##_SPLAY_FIND(struct name *head, \
136 if (SPLAY_EMPTY(head)) \
138 name##_SPLAY(head, elm); \
139 if ((cmp)(elm, (head)->sph_root) == 0) \
140 return (head->sph_root); \
144 static __inline struct type *name##_SPLAY_NEXT(struct name *head, \
147 name##_SPLAY(head, elm); \
148 if (SPLAY_RIGHT(elm, field) != NULL) { \
149 elm = SPLAY_RIGHT(elm, field); \
150 while (SPLAY_LEFT(elm, field) != NULL) { \
151 elm = SPLAY_LEFT(elm, field); \
158 static __inline struct type *name##_SPLAY_MIN_MAX(struct name *head, \
161 name##_SPLAY_MINMAX(head, val); \
162 return (SPLAY_ROOT(head)); \
165 /* Main splay operation.
166 * Moves node close to the key of elm to top
168 #define SPLAY_GENERATE(name, type, field, cmp) \
169 struct type *name##_SPLAY_INSERT(struct name *head, struct type *elm) \
171 if (SPLAY_EMPTY(head)) { \
172 SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = \
176 name##_SPLAY(head, elm); \
177 __comp = (cmp)(elm, (head)->sph_root); \
179 SPLAY_LEFT(elm, field) = \
180 SPLAY_LEFT((head)->sph_root, field); \
181 SPLAY_RIGHT(elm, field) = (head)->sph_root; \
182 SPLAY_LEFT((head)->sph_root, field) = NULL; \
183 } else if (__comp > 0) { \
184 SPLAY_RIGHT(elm, field) = \
185 SPLAY_RIGHT((head)->sph_root, field); \
186 SPLAY_LEFT(elm, field) = (head)->sph_root; \
187 SPLAY_RIGHT((head)->sph_root, field) = NULL; \
189 return ((head)->sph_root); \
191 (head)->sph_root = (elm); \
195 struct type *name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
197 struct type *__tmp; \
198 if (SPLAY_EMPTY(head)) \
200 name##_SPLAY(head, elm); \
201 if ((cmp)(elm, (head)->sph_root) == 0) { \
202 if (SPLAY_LEFT((head)->sph_root, field) == NULL) { \
204 SPLAY_RIGHT((head)->sph_root, field); \
206 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
208 SPLAY_LEFT((head)->sph_root, field); \
209 name##_SPLAY(head, elm); \
210 SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
217 void name##_SPLAY(struct name *head, struct type *elm) \
219 struct type __node, *__left, *__right, *__tmp; \
222 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = \
224 __left = __right = &__node; \
226 while ((__comp = (cmp)(elm, (head)->sph_root))) { \
228 __tmp = SPLAY_LEFT((head)->sph_root, field); \
231 if ((cmp)(elm, __tmp) < 0) { \
232 SPLAY_ROTATE_RIGHT(head, __tmp, \
234 if (SPLAY_LEFT((head)->sph_root, \
239 SPLAY_LINKLEFT(head, __right, field); \
240 } else if (__comp > 0) { \
241 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
244 if ((cmp)(elm, __tmp) > 0) { \
245 SPLAY_ROTATE_LEFT(head, __tmp, field); \
246 if (SPLAY_RIGHT((head)->sph_root, \
251 SPLAY_LINKRIGHT(head, __left, field); \
254 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
257 /* Splay with either the minimum or the maximum element \
258 * Used to find minimum or maximum element in tree. \
260 void name##_SPLAY_MINMAX(struct name *head, int __comp) \
262 struct type __node, *__left, *__right, *__tmp; \
264 SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = \
266 __left = __right = &__node; \
270 __tmp = SPLAY_LEFT((head)->sph_root, field); \
274 SPLAY_ROTATE_RIGHT(head, __tmp, \
276 if (SPLAY_LEFT((head)->sph_root, \
281 SPLAY_LINKLEFT(head, __right, field); \
282 } else if (__comp > 0) { \
283 __tmp = SPLAY_RIGHT((head)->sph_root, field); \
287 SPLAY_ROTATE_LEFT(head, __tmp, field); \
288 if (SPLAY_RIGHT((head)->sph_root, \
293 SPLAY_LINKRIGHT(head, __left, field); \
296 SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
299 #define SPLAY_NEGINF -1
302 #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
303 #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
304 #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
305 #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
306 #define SPLAY_MIN(name, x) \
307 (SPLAY_EMPTY(x) ? NULL : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
308 #define SPLAY_MAX(name, x) \
309 (SPLAY_EMPTY(x) ? NULL : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
311 #define SPLAY_FOREACH(x, name, head) \
312 for ((x) = SPLAY_MIN(name, head); (x) != NULL; \
313 (x) = SPLAY_NEXT(name, head, x))
316 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
318 * Permission to use, copy, modify, and distribute this software for any
319 * purpose with or without fee is hereby granted, provided that the above
320 * copyright notice and this permission notice appear in all copies.
322 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
323 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
324 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
325 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
326 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
327 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
328 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
335 int (*t_compare
)(const void *, const void *);
336 void (*t_augment
)(void *);
337 unsigned int t_offset
; /* offset of rb_entry in type */
341 struct rb_entry
*rbt_root
;
345 struct rb_entry
*rbt_parent
;
346 struct rb_entry
*rbt_left
;
347 struct rb_entry
*rbt_right
;
348 unsigned int rbt_color
;
351 #define RB_HEAD(_name, _type) \
353 struct rbt_tree rbh_root; \
356 #define RB_ENTRY(_type) struct rb_entry
358 static inline void _rb_init(struct rbt_tree
*rbt
)
360 rbt
->rbt_root
= NULL
;
363 static inline int _rb_empty(struct rbt_tree
*rbt
)
365 return (rbt
->rbt_root
== NULL
);
368 void *_rb_insert(const struct rb_type
*, struct rbt_tree
*, void *);
369 void *_rb_remove(const struct rb_type
*, struct rbt_tree
*, void *);
370 void *_rb_find(const struct rb_type
*, struct rbt_tree
*, const void *);
371 void *_rb_nfind(const struct rb_type
*, struct rbt_tree
*, const void *);
372 void *_rb_root(const struct rb_type
*, struct rbt_tree
*);
373 void *_rb_min(const struct rb_type
*, struct rbt_tree
*);
374 void *_rb_max(const struct rb_type
*, struct rbt_tree
*);
375 void *_rb_next(const struct rb_type
*, void *);
376 void *_rb_prev(const struct rb_type
*, void *);
377 void *_rb_left(const struct rb_type
*, void *);
378 void *_rb_right(const struct rb_type
*, void *);
379 void *_rb_parent(const struct rb_type
*, void *);
380 void _rb_set_left(const struct rb_type
*, void *, void *);
381 void _rb_set_right(const struct rb_type
*, void *, void *);
382 void _rb_set_parent(const struct rb_type
*, void *, void *);
383 void _rb_poison(const struct rb_type
*, void *, unsigned long);
384 int _rb_check(const struct rb_type
*, void *, unsigned long);
386 #define RB_INITIALIZER(_head) { { NULL } }
388 #define RB_PROTOTYPE(_name, _type, _field, _cmp) \
389 extern const struct rb_type *const _name##_RB_TYPE; \
391 __attribute__((__unused__)) static inline void _name##_RB_INIT( \
392 struct _name *head) \
394 _rb_init(&head->rbh_root); \
397 __attribute__((__unused__)) static inline struct _type \
398 *_name##_RB_INSERT(struct _name *head, struct _type *elm) \
400 return _rb_insert(_name##_RB_TYPE, &head->rbh_root, elm); \
403 __attribute__((__unused__)) static inline struct _type \
404 *_name##_RB_REMOVE(struct _name *head, struct _type *elm) \
406 return _rb_remove(_name##_RB_TYPE, &head->rbh_root, elm); \
409 __attribute__((__unused__)) static inline struct _type \
410 *_name##_RB_FIND(struct _name *head, const struct _type *key) \
412 return _rb_find(_name##_RB_TYPE, &head->rbh_root, key); \
415 __attribute__((__unused__)) static inline struct _type \
416 *_name##_RB_NFIND(struct _name *head, const struct _type *key) \
418 return _rb_nfind(_name##_RB_TYPE, &head->rbh_root, key); \
421 __attribute__((__unused__)) static inline struct _type \
422 *_name##_RB_ROOT(struct _name *head) \
424 return _rb_root(_name##_RB_TYPE, &head->rbh_root); \
427 __attribute__((__unused__)) static inline int _name##_RB_EMPTY( \
428 struct _name *head) \
430 return _rb_empty(&head->rbh_root); \
433 __attribute__((__unused__)) static inline struct _type \
434 *_name##_RB_MIN(struct _name *head) \
436 return _rb_min(_name##_RB_TYPE, &head->rbh_root); \
439 __attribute__((__unused__)) static inline struct _type \
440 *_name##_RB_MAX(struct _name *head) \
442 return _rb_max(_name##_RB_TYPE, &head->rbh_root); \
445 __attribute__((__unused__)) static inline struct _type \
446 *_name##_RB_NEXT(struct _type *elm) \
448 return _rb_next(_name##_RB_TYPE, elm); \
451 __attribute__((__unused__)) static inline struct _type \
452 *_name##_RB_PREV(struct _type *elm) \
454 return _rb_prev(_name##_RB_TYPE, elm); \
457 __attribute__((__unused__)) static inline struct _type \
458 *_name##_RB_LEFT(struct _type *elm) \
460 return _rb_left(_name##_RB_TYPE, elm); \
463 __attribute__((__unused__)) static inline struct _type \
464 *_name##_RB_RIGHT(struct _type *elm) \
466 return _rb_right(_name##_RB_TYPE, elm); \
469 __attribute__((__unused__)) static inline struct _type \
470 *_name##_RB_PARENT(struct _type *elm) \
472 return _rb_parent(_name##_RB_TYPE, elm); \
475 __attribute__((__unused__)) static inline void _name##_RB_SET_LEFT( \
476 struct _type *elm, struct _type *left) \
478 return _rb_set_left(_name##_RB_TYPE, elm, left); \
481 __attribute__((__unused__)) static inline void _name##_RB_SET_RIGHT( \
482 struct _type *elm, struct _type *right) \
484 return _rb_set_right(_name##_RB_TYPE, elm, right); \
487 __attribute__((__unused__)) static inline void _name##_RB_SET_PARENT( \
488 struct _type *elm, struct _type *parent) \
490 return _rb_set_parent(_name##_RB_TYPE, elm, parent); \
493 __attribute__((__unused__)) static inline void _name##_RB_POISON( \
494 struct _type *elm, unsigned long poison) \
496 return _rb_poison(_name##_RB_TYPE, elm, poison); \
499 __attribute__((__unused__)) static inline int _name##_RB_CHECK( \
500 struct _type *elm, unsigned long poison) \
502 return _rb_check(_name##_RB_TYPE, elm, poison); \
505 #define RB_GENERATE_INTERNAL(_name, _type, _field, _cmp, _aug) \
506 static int _name##_RB_COMPARE(const void *lptr, const void *rptr) \
508 const struct _type *l = lptr, *r = rptr; \
511 static const struct rb_type _name##_RB_INFO = { \
512 _name##_RB_COMPARE, _aug, offsetof(struct _type, _field), \
514 const struct rb_type *const _name##_RB_TYPE = &_name##_RB_INFO;
516 #define RB_GENERATE_AUGMENT(_name, _type, _field, _cmp, _aug) \
517 static void _name##_RB_AUGMENT(void *ptr) \
519 struct _type *p = ptr; \
522 RB_GENERATE_INTERNAL(_name, _type, _field, _cmp, _name##_RB_AUGMENT)
524 #define RB_GENERATE(_name, _type, _field, _cmp) \
525 RB_GENERATE_INTERNAL(_name, _type, _field, _cmp, NULL)
527 #define RB_INIT(_name, _head) _name##_RB_INIT(_head)
528 #define RB_INSERT(_name, _head, _elm) _name##_RB_INSERT(_head, _elm)
529 #define RB_REMOVE(_name, _head, _elm) _name##_RB_REMOVE(_head, _elm)
530 #define RB_FIND(_name, _head, _key) _name##_RB_FIND(_head, _key)
531 #define RB_NFIND(_name, _head, _key) _name##_RB_NFIND(_head, _key)
532 #define RB_ROOT(_name, _head) _name##_RB_ROOT(_head)
533 #define RB_EMPTY(_name, _head) _name##_RB_EMPTY(_head)
534 #define RB_MIN(_name, _head) _name##_RB_MIN(_head)
535 #define RB_MAX(_name, _head) _name##_RB_MAX(_head)
536 #define RB_NEXT(_name, _elm) _name##_RB_NEXT(_elm)
537 #define RB_PREV(_name, _elm) _name##_RB_PREV(_elm)
538 #define RB_LEFT(_name, _elm) _name##_RB_LEFT(_elm)
539 #define RB_RIGHT(_name, _elm) _name##_RB_RIGHT(_elm)
540 #define RB_PARENT(_name, _elm) _name##_RB_PARENT(_elm)
541 #define RB_SET_LEFT(_name, _elm, _l) _name##_RB_SET_LEFT(_elm, _l)
542 #define RB_SET_RIGHT(_name, _elm, _r) _name##_RB_SET_RIGHT(_elm, _r)
543 #define RB_SET_PARENT(_name, _elm, _p) _name##_RB_SET_PARENT(_elm, _p)
544 #define RB_POISON(_name, _elm, _p) _name##_RB_POISON(_elm, _p)
545 #define RB_CHECK(_name, _elm, _p) _name##_RB_CHECK(_elm, _p)
547 #define RB_FOREACH(_e, _name, _head) \
548 for ((_e) = RB_MIN(_name, (_head)); (_e) != NULL; \
549 (_e) = RB_NEXT(_name, (_e)))
551 #define RB_FOREACH_SAFE(_e, _name, _head, _n) \
552 for ((_e) = RB_MIN(_name, (_head)); \
553 (_e) != NULL && ((_n) = RB_NEXT(_name, (_e)), 1); (_e) = (_n))
555 #define RB_FOREACH_REVERSE(_e, _name, _head) \
556 for ((_e) = RB_MAX(_name, (_head)); (_e) != NULL; \
557 (_e) = RB_PREV(_name, (_e)))
559 #define RB_FOREACH_REVERSE_SAFE(_e, _name, _head, _n) \
560 for ((_e) = RB_MAX(_name, (_head)); \
561 (_e) != NULL && ((_n) = RB_PREV(_name, (_e)), 1); (_e) = (_n))
563 #endif /* _SYS_TREE_H_ */