]>
git.proxmox.com Git - mirror_frr.git/blob - lib/typerb.h
2 * The following Red-Black tree implementation is based off code with
5 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 struct typed_rb_entry
{
26 struct typed_rb_entry
*rbt_parent
;
27 struct typed_rb_entry
*rbt_left
;
28 struct typed_rb_entry
*rbt_right
;
29 unsigned int rbt_color
;
32 struct typed_rb_root
{
33 struct typed_rb_entry
*rbt_root
;
37 struct typed_rb_entry
*typed_rb_insert(struct typed_rb_root
*,
38 struct typed_rb_entry
*rbe
,
40 const struct typed_rb_entry
*a
,
41 const struct typed_rb_entry
*b
));
42 void typed_rb_remove(struct typed_rb_root
*, struct typed_rb_entry
*rbe
);
43 struct typed_rb_entry
*typed_rb_find(struct typed_rb_root
*,
44 const struct typed_rb_entry
*rbe
,
46 const struct typed_rb_entry
*a
,
47 const struct typed_rb_entry
*b
));
48 struct typed_rb_entry
*typed_rb_find_gteq(struct typed_rb_root
*,
49 const struct typed_rb_entry
*rbe
,
51 const struct typed_rb_entry
*a
,
52 const struct typed_rb_entry
*b
));
53 struct typed_rb_entry
*typed_rb_find_lt(struct typed_rb_root
*,
54 const struct typed_rb_entry
*rbe
,
56 const struct typed_rb_entry
*a
,
57 const struct typed_rb_entry
*b
));
58 struct typed_rb_entry
*typed_rb_min(struct typed_rb_root
*);
59 struct typed_rb_entry
*typed_rb_next(struct typed_rb_entry
*);
61 #define _PREDECL_RBTREE(prefix) \
62 struct prefix ## _head { struct typed_rb_root rr; }; \
63 struct prefix ## _item { struct typed_rb_entry re; };
65 #define INIT_RBTREE_UNIQ(var) { }
66 #define INIT_RBTREE_NONUNIQ(var) { }
68 #define _DECLARE_RBTREE(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
70 macro_inline void prefix ## _init(struct prefix##_head *h) \
72 memset(h, 0, sizeof(*h)); \
74 macro_inline void prefix ## _fini(struct prefix##_head *h) \
76 memset(h, 0, sizeof(*h)); \
78 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
80 struct typed_rb_entry *re; \
81 re = typed_rb_insert(&h->rr, &item->field.re, cmpfn_uq); \
82 return container_of_null(re, type, field.re); \
84 macro_inline type *prefix ## _find_gteq(struct prefix##_head *h, \
87 struct typed_rb_entry *re; \
88 re = typed_rb_find_gteq(&h->rr, &item->field.re, cmpfn_nuq); \
89 return container_of_null(re, type, field.re); \
91 macro_inline type *prefix ## _find_lt(struct prefix##_head *h, \
94 struct typed_rb_entry *re; \
95 re = typed_rb_find_lt(&h->rr, &item->field.re, cmpfn_nuq); \
96 return container_of_null(re, type, field.re); \
98 macro_inline void prefix ## _del(struct prefix##_head *h, type *item) \
100 typed_rb_remove(&h->rr, &item->field.re); \
102 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
104 struct typed_rb_entry *re; \
105 re = typed_rb_min(&h->rr); \
108 typed_rb_remove(&h->rr, re); \
109 return container_of(re, type, field.re); \
111 macro_pure type *prefix ## _first(struct prefix##_head *h) \
113 struct typed_rb_entry *re; \
114 re = typed_rb_min(&h->rr); \
115 return container_of_null(re, type, field.re); \
117 macro_pure type *prefix ## _next(struct prefix##_head *h, type *item) \
119 struct typed_rb_entry *re; \
120 re = typed_rb_next(&item->field.re); \
121 return container_of_null(re, type, field.re); \
123 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
125 struct typed_rb_entry *re; \
126 re = item ? typed_rb_next(&item->field.re) : NULL; \
127 return container_of_null(re, type, field.re); \
129 macro_pure size_t prefix ## _count(struct prefix##_head *h) \
131 return h->rr.count; \
135 #define PREDECL_RBTREE_UNIQ(prefix) \
136 _PREDECL_RBTREE(prefix)
137 #define DECLARE_RBTREE_UNIQ(prefix, type, field, cmpfn) \
139 macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
140 const struct typed_rb_entry *b) \
142 return cmpfn(container_of(a, type, field.re), \
143 container_of(b, type, field.re)); \
145 macro_inline type *prefix ## _find(struct prefix##_head *h, const type *item) \
147 struct typed_rb_entry *re; \
148 re = typed_rb_find(&h->rr, &item->field.re, &prefix ## __cmp); \
149 return container_of_null(re, type, field.re); \
152 _DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp) \
155 #define PREDECL_RBTREE_NONUNIQ(prefix) \
156 _PREDECL_RBTREE(prefix)
157 #define DECLARE_RBTREE_NONUNIQ(prefix, type, field, cmpfn) \
159 macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
160 const struct typed_rb_entry *b) \
162 return cmpfn(container_of(a, type, field.re), \
163 container_of(b, type, field.re)); \
165 macro_inline int prefix ## __cmp_uq(const struct typed_rb_entry *a, \
166 const struct typed_rb_entry *b) \
168 int cmpval = cmpfn(container_of(a, type, field.re), \
169 container_of(b, type, field.re)); \
179 _DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp_uq) \
182 #endif /* _FRR_TYPERB_H */