diff options
| author | David Lamparter <equinox@diac24.net> | 2021-03-27 22:05:07 +0100 | 
|---|---|---|
| committer | David Lamparter <equinox@opensourcerouting.org> | 2021-10-19 14:55:39 +0200 | 
| commit | f45897e45c57db80b21e31d44723733a17bfac54 (patch) | |
| tree | c5a8bcdf49194dfe78fb2ffc8800997e7bf211f2 /lib/typesafe.c | |
| parent | 4a1b3289c7a8abc9858bfd1ac2ced35e8d20dd68 (diff) | |
lib: typesafe *_member()
This provides a "is this item on this list" check, which may or may not
be faster than using *_find() for the same purpose.  (If the container
has no faster way of doing it, it falls back to using *_find().)
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/typesafe.c')
| -rw-r--r-- | lib/typesafe.c | 38 | 
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/typesafe.c b/lib/typesafe.c index 76705fad0d..f886c5bdf6 100644 --- a/lib/typesafe.c +++ b/lib/typesafe.c @@ -29,6 +29,44 @@ DEFINE_MTYPE_STATIC(LIB, TYPEDHASH_BUCKET, "Typed-hash bucket");  DEFINE_MTYPE_STATIC(LIB, SKIPLIST_OFLOW, "Skiplist overflow");  DEFINE_MTYPE_STATIC(LIB, HEAP_ARRAY, "Typed-heap array"); +bool typesafe_list_member(const struct slist_head *head, +			  const struct slist_item *item) +{ +	struct slist_item *fromhead = head->first; +	struct slist_item **fromnext = (struct slist_item **)&item->next; + +	while (fromhead) { +		if (fromhead == item || fromnext == head->last_next) +			return true; + +		fromhead = fromhead->next; +		if (!*fromnext) +			break; +		fromnext = &(*fromnext)->next; +	} + +	return false; +} + +bool typesafe_dlist_member(const struct dlist_head *head, +			   const struct dlist_item *item) +{ +	const struct dlist_item *fromhead = head->hitem.next; +	const struct dlist_item *fromitem = item->next; + +	if (!item->prev || !item->next) +		return false; + +	while (fromhead != &head->hitem && fromitem != item) { +		if (fromitem == &head->hitem || fromhead == item) +			return true; +		fromhead = fromhead->next; +		fromitem = fromitem->next; +	} + +	return false; +} +  #if 0  static void hash_consistency_check(struct thash_head *head)  {  | 
