diff options
| author | David Lamparter <equinox@diac24.net> | 2020-05-04 21:36:03 +0200 |
|---|---|---|
| committer | David Lamparter <equinox@diac24.net> | 2020-05-04 22:13:28 +0200 |
| commit | daf3441d2bc7855a10886ec85ba0999be9e44e59 (patch) | |
| tree | 36e5b0ea841d9a44df549d6b4190581413f44828 /lib/typerb.c | |
| parent | 15e9c561b2a10e90b1370e7a8d43d02ffde9e61a (diff) | |
lib: add const iteration & find to typesafe lists
Based on work originally by Mark Stapp <mjs@voltanet.io>.
Make it possible to iterate the typesafe lists in a const
context, as well as find items from them.
Signed-off-by: Mark Stapp <mjs@voltanet.io>
[above signoff was for the original version before modification]
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/typerb.c')
| -rw-r--r-- | lib/typerb.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/lib/typerb.c b/lib/typerb.c index 7e8cd9d8f7..092faa4cc9 100644 --- a/lib/typerb.c +++ b/lib/typerb.c @@ -377,12 +377,13 @@ struct typed_rb_entry *typed_rb_insert(struct rbt_tree *rbt, } /* Finds the node with the same key as elm */ -struct rb_entry *typed_rb_find(struct rbt_tree *rbt, const struct rb_entry *key, +const struct rb_entry *typed_rb_find(const struct rbt_tree *rbt, + const struct rb_entry *key, int (*cmpfn)( const struct typed_rb_entry *a, const struct typed_rb_entry *b)) { - struct rb_entry *tmp = RBH_ROOT(rbt); + const struct rb_entry *tmp = RBH_ROOT(rbt); int comp; while (tmp != NULL) { @@ -398,13 +399,13 @@ struct rb_entry *typed_rb_find(struct rbt_tree *rbt, const struct rb_entry *key, return NULL; } -struct rb_entry *typed_rb_find_gteq(struct rbt_tree *rbt, +const struct rb_entry *typed_rb_find_gteq(const struct rbt_tree *rbt, const struct rb_entry *key, int (*cmpfn)( const struct typed_rb_entry *a, const struct typed_rb_entry *b)) { - struct rb_entry *tmp = RBH_ROOT(rbt), *best = NULL; + const struct rb_entry *tmp = RBH_ROOT(rbt), *best = NULL; int comp; while (tmp != NULL) { @@ -421,13 +422,13 @@ struct rb_entry *typed_rb_find_gteq(struct rbt_tree *rbt, return best; } -struct rb_entry *typed_rb_find_lt(struct rbt_tree *rbt, +const struct rb_entry *typed_rb_find_lt(const struct rbt_tree *rbt, const struct rb_entry *key, int (*cmpfn)( const struct typed_rb_entry *a, const struct typed_rb_entry *b)) { - struct rb_entry *tmp = RBH_ROOT(rbt), *best = NULL; + const struct rb_entry *tmp = RBH_ROOT(rbt), *best = NULL; int comp; while (tmp != NULL) { @@ -443,8 +444,10 @@ struct rb_entry *typed_rb_find_lt(struct rbt_tree *rbt, return best; } -struct rb_entry *typed_rb_next(struct rb_entry *rbe) +struct rb_entry *typed_rb_next(const struct rb_entry *rbe_const) { + struct rb_entry *rbe = (struct rb_entry *)rbe_const; + if (RBE_RIGHT(rbe) != NULL) { rbe = RBE_RIGHT(rbe); while (RBE_LEFT(rbe) != NULL) @@ -463,7 +466,7 @@ struct rb_entry *typed_rb_next(struct rb_entry *rbe) return rbe; } -struct rb_entry *typed_rb_min(struct rbt_tree *rbt) +struct rb_entry *typed_rb_min(const struct rbt_tree *rbt) { struct rb_entry *rbe = RBH_ROOT(rbt); struct rb_entry *parent = NULL; |
