]>
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.
30 struct typed_rb_entry
{
31 struct typed_rb_entry
*rbt_parent
;
32 struct typed_rb_entry
*rbt_left
;
33 struct typed_rb_entry
*rbt_right
;
34 unsigned int rbt_color
;
37 struct typed_rb_root
{
38 struct typed_rb_entry
*rbt_root
;
42 struct typed_rb_entry
*typed_rb_insert(struct typed_rb_root
*rbt
,
43 struct typed_rb_entry
*rbe
,
45 const struct typed_rb_entry
*a
,
46 const struct typed_rb_entry
*b
));
47 struct typed_rb_entry
*typed_rb_remove(struct typed_rb_root
*rbt
,
48 struct typed_rb_entry
*rbe
);
49 const struct typed_rb_entry
*typed_rb_find(const struct typed_rb_root
*rbt
,
50 const struct typed_rb_entry
*rbe
,
52 const struct typed_rb_entry
*a
,
53 const struct typed_rb_entry
*b
));
54 const struct typed_rb_entry
*typed_rb_find_gteq(const struct typed_rb_root
*rbt
,
55 const struct typed_rb_entry
*rbe
,
57 const struct typed_rb_entry
*a
,
58 const struct typed_rb_entry
*b
));
59 const struct typed_rb_entry
*typed_rb_find_lt(const struct typed_rb_root
*rbt
,
60 const struct typed_rb_entry
*rbe
,
62 const struct typed_rb_entry
*a
,
63 const struct typed_rb_entry
*b
));
64 struct typed_rb_entry
*typed_rb_min(const struct typed_rb_root
*rbt
);
65 struct typed_rb_entry
*typed_rb_next(const struct typed_rb_entry
*rbe
);
66 bool typed_rb_member(const struct typed_rb_root
*rbt
,
67 const struct typed_rb_entry
*rbe
);
69 #define _PREDECL_RBTREE(prefix) \
70 struct prefix ## _head { struct typed_rb_root rr; }; \
71 struct prefix ## _item { struct typed_rb_entry re; }; \
72 MACRO_REQUIRE_SEMICOLON() /* end */
74 #define INIT_RBTREE_UNIQ(var) { }
75 #define INIT_RBTREE_NONUNIQ(var) { }
77 #define _DECLARE_RBTREE(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
79 macro_inline void prefix ## _init(struct prefix##_head *h) \
81 memset(h, 0, sizeof(*h)); \
83 macro_inline void prefix ## _fini(struct prefix##_head *h) \
85 memset(h, 0, sizeof(*h)); \
87 macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
89 struct typed_rb_entry *re; \
90 re = typed_rb_insert(&h->rr, &item->field.re, cmpfn_uq); \
91 return container_of_null(re, type, field.re); \
93 macro_inline const type *prefix ## _const_find_gteq( \
94 const struct prefix##_head *h, const type *item) \
96 const struct typed_rb_entry *re; \
97 re = typed_rb_find_gteq(&h->rr, &item->field.re, cmpfn_nuq); \
98 return container_of_null(re, type, field.re); \
100 macro_inline const type *prefix ## _const_find_lt( \
101 const struct prefix##_head *h, const type *item) \
103 const struct typed_rb_entry *re; \
104 re = typed_rb_find_lt(&h->rr, &item->field.re, cmpfn_nuq); \
105 return container_of_null(re, type, field.re); \
107 TYPESAFE_FIND_CMP(prefix, type) \
108 macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
110 struct typed_rb_entry *re; \
111 re = typed_rb_remove(&h->rr, &item->field.re); \
112 return container_of_null(re, type, field.re); \
114 macro_inline type *prefix ## _pop(struct prefix##_head *h) \
116 struct typed_rb_entry *re; \
117 re = typed_rb_min(&h->rr); \
120 typed_rb_remove(&h->rr, re); \
121 return container_of(re, type, field.re); \
123 TYPESAFE_SWAP_ALL_SIMPLE(prefix) \
124 macro_pure const type *prefix ## _const_first(const struct prefix##_head *h) \
126 const struct typed_rb_entry *re; \
127 re = typed_rb_min(&h->rr); \
128 return container_of_null(re, type, field.re); \
130 macro_pure const type *prefix ## _const_next(const struct prefix##_head *h, \
133 const struct typed_rb_entry *re; \
134 re = typed_rb_next(&item->field.re); \
135 return container_of_null(re, type, field.re); \
137 TYPESAFE_FIRST_NEXT(prefix, type) \
138 macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
140 struct typed_rb_entry *re; \
141 re = item ? typed_rb_next(&item->field.re) : NULL; \
142 return container_of_null(re, type, field.re); \
144 macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
146 return h->rr.count; \
148 macro_pure bool prefix ## _member(const struct prefix##_head *h, \
151 return typed_rb_member(&h->rr, &item->field.re); \
153 MACRO_REQUIRE_SEMICOLON() /* end */
155 #define PREDECL_RBTREE_UNIQ(prefix) \
156 _PREDECL_RBTREE(prefix)
157 #define DECLARE_RBTREE_UNIQ(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 const type *prefix ## _const_find(const struct prefix##_head *h, \
168 const struct typed_rb_entry *re; \
169 re = typed_rb_find(&h->rr, &item->field.re, &prefix ## __cmp); \
170 return container_of_null(re, type, field.re); \
172 TYPESAFE_FIND(prefix, type) \
174 _DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp); \
175 MACRO_REQUIRE_SEMICOLON() /* end */
177 #define PREDECL_RBTREE_NONUNIQ(prefix) \
178 _PREDECL_RBTREE(prefix)
179 #define DECLARE_RBTREE_NONUNIQ(prefix, type, field, cmpfn) \
181 macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
182 const struct typed_rb_entry *b) \
184 return cmpfn(container_of(a, type, field.re), \
185 container_of(b, type, field.re)); \
187 macro_inline int prefix ## __cmp_uq(const struct typed_rb_entry *a, \
188 const struct typed_rb_entry *b) \
190 int cmpval = cmpfn(container_of(a, type, field.re), \
191 container_of(b, type, field.re)); \
201 _DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp_uq); \
202 MACRO_REQUIRE_SEMICOLON() /* end */
208 #endif /* _FRR_TYPERB_H */