]> git.proxmox.com Git - mirror_frr.git/blame - lib/typerb.h
Merge pull request #8008 from chiragshah6/yang_nb5
[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);
80911bc2
DL
65
66#define _PREDECL_RBTREE(prefix) \
67struct prefix ## _head { struct typed_rb_root rr; }; \
68struct prefix ## _item { struct typed_rb_entry re; };
69
70#define INIT_RBTREE_UNIQ(var) { }
71#define INIT_RBTREE_NONUNIQ(var) { }
72
73#define _DECLARE_RBTREE(prefix, type, field, cmpfn_nuq, cmpfn_uq) \
74 \
75macro_inline void prefix ## _init(struct prefix##_head *h) \
76{ \
77 memset(h, 0, sizeof(*h)); \
78} \
79macro_inline void prefix ## _fini(struct prefix##_head *h) \
80{ \
81 memset(h, 0, sizeof(*h)); \
82} \
83macro_inline type *prefix ## _add(struct prefix##_head *h, type *item) \
84{ \
85 struct typed_rb_entry *re; \
86 re = typed_rb_insert(&h->rr, &item->field.re, cmpfn_uq); \
87 return container_of_null(re, type, field.re); \
88} \
daf3441d
DL
89macro_inline const type *prefix ## _const_find_gteq( \
90 const struct prefix##_head *h, const type *item) \
80911bc2 91{ \
daf3441d 92 const struct typed_rb_entry *re; \
80911bc2
DL
93 re = typed_rb_find_gteq(&h->rr, &item->field.re, cmpfn_nuq); \
94 return container_of_null(re, type, field.re); \
95} \
daf3441d
DL
96macro_inline const type *prefix ## _const_find_lt( \
97 const struct prefix##_head *h, const type *item) \
80911bc2 98{ \
daf3441d 99 const struct typed_rb_entry *re; \
80911bc2
DL
100 re = typed_rb_find_lt(&h->rr, &item->field.re, cmpfn_nuq); \
101 return container_of_null(re, type, field.re); \
102} \
daf3441d 103TYPESAFE_FIND_CMP(prefix, type) \
da098d78 104macro_inline type *prefix ## _del(struct prefix##_head *h, type *item) \
80911bc2 105{ \
da098d78
SW
106 struct typed_rb_entry *re; \
107 re = typed_rb_remove(&h->rr, &item->field.re); \
108 return container_of_null(re, type, field.re); \
80911bc2
DL
109} \
110macro_inline type *prefix ## _pop(struct prefix##_head *h) \
111{ \
112 struct typed_rb_entry *re; \
113 re = typed_rb_min(&h->rr); \
114 if (!re) \
115 return NULL; \
116 typed_rb_remove(&h->rr, re); \
117 return container_of(re, type, field.re); \
118} \
daf3441d 119macro_pure const type *prefix ## _const_first(const struct prefix##_head *h) \
80911bc2 120{ \
daf3441d 121 const struct typed_rb_entry *re; \
80911bc2
DL
122 re = typed_rb_min(&h->rr); \
123 return container_of_null(re, type, field.re); \
124} \
daf3441d
DL
125macro_pure const type *prefix ## _const_next(const struct prefix##_head *h, \
126 const type *item) \
80911bc2 127{ \
daf3441d 128 const struct typed_rb_entry *re; \
80911bc2
DL
129 re = typed_rb_next(&item->field.re); \
130 return container_of_null(re, type, field.re); \
131} \
daf3441d 132TYPESAFE_FIRST_NEXT(prefix, type) \
80911bc2
DL
133macro_pure type *prefix ## _next_safe(struct prefix##_head *h, type *item) \
134{ \
135 struct typed_rb_entry *re; \
136 re = item ? typed_rb_next(&item->field.re) : NULL; \
137 return container_of_null(re, type, field.re); \
138} \
0ca16c58 139macro_pure size_t prefix ## _count(const struct prefix##_head *h) \
80911bc2
DL
140{ \
141 return h->rr.count; \
142} \
143/* ... */
144
145#define PREDECL_RBTREE_UNIQ(prefix) \
146 _PREDECL_RBTREE(prefix)
147#define DECLARE_RBTREE_UNIQ(prefix, type, field, cmpfn) \
148 \
149macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
150 const struct typed_rb_entry *b) \
151{ \
152 return cmpfn(container_of(a, type, field.re), \
153 container_of(b, type, field.re)); \
154} \
daf3441d
DL
155macro_inline const type *prefix ## _const_find(const struct prefix##_head *h, \
156 const type *item) \
80911bc2 157{ \
daf3441d 158 const struct typed_rb_entry *re; \
80911bc2
DL
159 re = typed_rb_find(&h->rr, &item->field.re, &prefix ## __cmp); \
160 return container_of_null(re, type, field.re); \
161} \
daf3441d 162TYPESAFE_FIND(prefix, type) \
80911bc2
DL
163 \
164_DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp) \
165/* ... */
166
167#define PREDECL_RBTREE_NONUNIQ(prefix) \
168 _PREDECL_RBTREE(prefix)
169#define DECLARE_RBTREE_NONUNIQ(prefix, type, field, cmpfn) \
170 \
171macro_inline int prefix ## __cmp(const struct typed_rb_entry *a, \
172 const struct typed_rb_entry *b) \
173{ \
174 return cmpfn(container_of(a, type, field.re), \
175 container_of(b, type, field.re)); \
176} \
177macro_inline int prefix ## __cmp_uq(const struct typed_rb_entry *a, \
178 const struct typed_rb_entry *b) \
179{ \
180 int cmpval = cmpfn(container_of(a, type, field.re), \
181 container_of(b, type, field.re)); \
182 if (cmpval) \
183 return cmpval; \
184 if (a < b) \
185 return -1; \
186 if (a > b) \
187 return 1; \
188 return 0; \
189} \
190 \
191_DECLARE_RBTREE(prefix, type, field, prefix ## __cmp, prefix ## __cmp_uq) \
192/* ... */
193
eea3c899
RW
194#ifdef __cplusplus
195}
196#endif
197
80911bc2 198#endif /* _FRR_TYPERB_H */