]> git.proxmox.com Git - mirror_frr.git/blame - lib/typerb.h
lib: typesafe *_member()
[mirror_frr.git] / lib / typerb.h
CommitLineData
80911bc2
DL
1/*
2 * The following Red-Black tree implementation is based off code with
3 * original copyright:
4 *
5 * Copyright (c) 2016 David Gwynne <dlg@openbsd.org>
6 *
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.
10 *
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.
18 */
19
20#ifndef _FRR_TYPERB_H
21#define _FRR_TYPERB_H
22
23#include "typesafe.h"
24
eea3c899
RW
25#ifdef __cplusplus
26extern "C" {
27#endif
28
80911bc2
DL
29struct typed_rb_entry {
30 struct typed_rb_entry *rbt_parent;
31 struct typed_rb_entry *rbt_left;
32 struct typed_rb_entry *rbt_right;
33 unsigned int rbt_color;
34};
35
36struct typed_rb_root {
37 struct typed_rb_entry *rbt_root;
38 size_t count;
39};
40
571a045b 41struct typed_rb_entry *typed_rb_insert(struct typed_rb_root *rbt,
80911bc2
DL
42 struct typed_rb_entry *rbe,
43 int (*cmpfn)(
44 const struct typed_rb_entry *a,
45 const struct typed_rb_entry *b));
571a045b 46struct typed_rb_entry *typed_rb_remove(struct typed_rb_root *rbt,
da098d78 47 struct typed_rb_entry *rbe);
daf3441d 48const struct typed_rb_entry *typed_rb_find(const struct typed_rb_root *rbt,
80911bc2
DL
49 const struct typed_rb_entry *rbe,
50 int (*cmpfn)(
51 const struct typed_rb_entry *a,
52 const struct typed_rb_entry *b));
daf3441d 53const struct typed_rb_entry *typed_rb_find_gteq(const struct typed_rb_root *rbt,
80911bc2
DL
54 const struct typed_rb_entry *rbe,
55 int (*cmpfn)(
56 const struct typed_rb_entry *a,
57 const struct typed_rb_entry *b));
daf3441d 58const struct typed_rb_entry *typed_rb_find_lt(const struct typed_rb_root *rbt,
80911bc2
DL
59 const struct typed_rb_entry *rbe,
60 int (*cmpfn)(
61 const struct typed_rb_entry *a,
62 const struct typed_rb_entry *b));
daf3441d
DL
63struct typed_rb_entry *typed_rb_min(const struct typed_rb_root *rbt);
64struct typed_rb_entry *typed_rb_next(const struct typed_rb_entry *rbe);
f45897e4
DL
65bool typed_rb_member(const struct typed_rb_root *rbt,
66 const struct typed_rb_entry *rbe);
80911bc2
DL
67
68#define _PREDECL_RBTREE(prefix) \
69struct prefix ## _head { struct typed_rb_root rr; }; \
960b9a53
DL
70struct prefix ## _item { struct typed_rb_entry re; }; \
71MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2
DL
72
73#define INIT_RBTREE_UNIQ(var) { }
74#define INIT_RBTREE_NONUNIQ(var) { }
75
76#define _DECLARE_RBTREE(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
77 \
78macro_inline void prefix ## _init(struct prefix##_head *h) \
79{ \
80 memset(h, 0, sizeof(*h)); \
81} \
82macro_inline void prefix ## _fini(struct prefix##_head *h) \
83{ \
84 memset(h, 0, sizeof(*h)); \
85} \
86macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
87{ \
88 struct typed_rb_entry *re; \
89 re = typed_rb_insert(&h->rr, &item->field.re, cmpfn_uq); \
90 return container_of_null(re, type, field.re); \
91} \
daf3441d
DL
92macro_inline const type *prefix ## _const_find_gteq( \
93 const struct prefix##_head *h, const type *item) \
80911bc2 94{ \
daf3441d 95 const struct typed_rb_entry *re; \
80911bc2
DL
96 re = typed_rb_find_gteq(&h->rr, &item->field.re, cmpfn_nuq); \
97 return container_of_null(re, type, field.re); \
98} \
daf3441d
DL
99macro_inline const type *prefix ## _const_find_lt( \
100 const struct prefix##_head *h, const type *item) \
80911bc2 101{ \
daf3441d 102 const struct typed_rb_entry *re; \
80911bc2
DL
103 re = typed_rb_find_lt(&h->rr, &item->field.re, cmpfn_nuq); \
104 return container_of_null(re, type, field.re); \
105} \
daf3441d 106TYPESAFE_FIND_CMP(prefix, type) \
da098d78 107macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
80911bc2 108{ \
da098d78
SW
109 struct typed_rb_entry *re; \
110 re = typed_rb_remove(&h->rr, &item->field.re); \
111 return container_of_null(re, type, field.re); \
80911bc2
DL
112} \
113macro_inline type *prefix ## _pop(struct prefix##_head *h) \
114{ \
115 struct typed_rb_entry *re; \
116 re = typed_rb_min(&h->rr); \
117 if (!re) \
118 return NULL; \
119 typed_rb_remove(&h->rr, re); \
120 return container_of(re, type, field.re); \
121} \
507e0e5d 122TYPESAFE_SWAP_ALL_SIMPLE(prefix) \
daf3441d 123macro_pure const type *prefix ## _const_first(const struct prefix##_head *h) \
80911bc2 124{ \
daf3441d 125 const struct typed_rb_entry *re; \
80911bc2
DL
126 re = typed_rb_min(&h->rr); \
127 return container_of_null(re, type, field.re); \
128} \
daf3441d
DL
129macro_pure const type *prefix ## _const_next(const struct prefix##_head *h, \
130 const type *item) \
80911bc2 131{ \
daf3441d 132 const struct typed_rb_entry *re; \
80911bc2
DL
133 re = typed_rb_next(&item->field.re); \
134 return container_of_null(re, type, field.re); \
135} \
daf3441d 136TYPESAFE_FIRST_NEXT(prefix, type) \
80911bc2
DL
137macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
138{ \
139 struct typed_rb_entry *re; \
140 re = item ? typed_rb_next(&item->field.re) : NULL; \
141 return container_of_null(re, type, field.re); \
142} \
0ca16c58 143macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
80911bc2
DL
144{ \
145 return h->rr.count; \
146} \
f45897e4
DL
147macro_pure bool prefix ## _member(const struct prefix##_head *h, \
148 const type *item) \
149{ \
150 return typed_rb_member(&h->rr, &item->field.re); \
151} \
960b9a53 152MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2
DL
153
154#define PREDECL_RBTREE_UNIQ(prefix) \
155 _PREDECL_RBTREE(prefix)
156#define DECLARE_RBTREE_UNIQ(prefix, type, field, cmpfn) \
157 \
158macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
159 const struct typed_rb_entry *b) \
160{ \
161 return cmpfn(container_of(a, type, field.re), \
162 container_of(b, type, field.re)); \
163} \
daf3441d
DL
164macro_inline const type *prefix ## _const_find(const struct prefix##_head *h, \
165 const type *item) \
80911bc2 166{ \
daf3441d 167 const struct typed_rb_entry *re; \
80911bc2
DL
168 re = typed_rb_find(&h->rr, &item->field.re, &prefix ## __cmp); \
169 return container_of_null(re, type, field.re); \
170} \
daf3441d 171TYPESAFE_FIND(prefix, type) \
80911bc2 172 \
960b9a53
DL
173_DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp); \
174MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2
DL
175
176#define PREDECL_RBTREE_NONUNIQ(prefix) \
177 _PREDECL_RBTREE(prefix)
178#define DECLARE_RBTREE_NONUNIQ(prefix, type, field, cmpfn) \
179 \
180macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
181 const struct typed_rb_entry *b) \
182{ \
183 return cmpfn(container_of(a, type, field.re), \
184 container_of(b, type, field.re)); \
185} \
186macro_inline int prefix ## __cmp_uq(const struct typed_rb_entry *a, \
187 const struct typed_rb_entry *b) \
188{ \
189 int cmpval = cmpfn(container_of(a, type, field.re), \
190 container_of(b, type, field.re)); \
191 if (cmpval) \
192 return cmpval; \
193 if (a < b) \
194 return -1; \
195 if (a > b) \
196 return 1; \
197 return 0; \
198} \
199 \
960b9a53
DL
200_DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp_uq); \
201MACRO_REQUIRE_SEMICOLON() /* end */
80911bc2 202
eea3c899
RW
203#ifdef __cplusplus
204}
205#endif
206
80911bc2 207#endif /* _FRR_TYPERB_H */