From 40ee228da9ae156f6129a7fd87845e1272d2bf92 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Wed, 29 Sep 2021 19:59:27 +0200 Subject: [PATCH] lib: add unsorted typesafe_anywhere() *_anywhere(item) returns whether an item is on _any_ container. Only available for unsorted containers for now. Signed-off-by: David Lamparter --- lib/typesafe.h | 9 +++++++++ tests/lib/test_typelist.h | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/typesafe.h b/lib/typesafe.h index b03934b964..44c42ffbca 100644 --- a/lib/typesafe.h +++ b/lib/typesafe.h @@ -258,6 +258,10 @@ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \ { \ return h->sh.count; \ } \ +macro_pure bool prefix ## _anywhere(const type *item) \ +{ \ + return item->field.si.next != NULL; \ +} \ macro_pure bool prefix ## _member(const struct prefix##_head *h, \ const type *item) \ { \ @@ -405,6 +409,11 @@ macro_pure size_t prefix ## _count(const struct prefix##_head *h) \ { \ return h->dh.count; \ } \ +macro_pure bool prefix ## _anywhere(const type *item) \ +{ \ + const struct dlist_item *ditem = &item->field.di; \ + return ditem->next && ditem->prev; \ +} \ macro_pure bool prefix ## _member(const struct prefix##_head *h, \ const type *item) \ { \ diff --git a/tests/lib/test_typelist.h b/tests/lib/test_typelist.h index a12a995bf6..8261616ed2 100644 --- a/tests/lib/test_typelist.h +++ b/tests/lib/test_typelist.h @@ -40,6 +40,7 @@ #define list_find_lt concat(TYPE, _find_lt) #define list_find_gteq concat(TYPE, _find_gteq) #define list_member concat(TYPE, _member) +#define list_anywhere concat(TYPE, _anywhere) #define list_del concat(TYPE, _del) #define list_pop concat(TYPE, _pop) #define list_swap_all concat(TYPE, _swap_all) @@ -502,10 +503,14 @@ static void concat(test_, TYPE)(void) ts_hash("del-prng", "86d568a95eb429dab3162976c5a5f3f75aabc835932cd682aa280b6923549564"); #if !IS_ATOMIC(REALTYPE) - for (i = 0; i < NITEM; i++) + for (i = 0; i < NITEM; i++) { assertf(list_member(&head, &itm[i]) == itm[i].scratchpad, "%zu should:%d is:%d", i, itm[i].scratchpad, list_member(&head, &itm[i])); + assertf(list_anywhere(&itm[i]) == itm[i].scratchpad, + "%zu should:%d is:%d", i, itm[i].scratchpad, + list_anywhere(&itm[i])); + } ts_hash("member", "86d568a95eb429dab3162976c5a5f3f75aabc835932cd682aa280b6923549564"); #endif @@ -519,10 +524,14 @@ static void concat(test_, TYPE)(void) ts_hash("pop#1", "42b8950c880535b2d2e0c980f9845f7841ecf675c0fb9801aec4170d2036349d"); #if !IS_ATOMIC(REALTYPE) - for (i = 0; i < NITEM; i++) + for (i = 0; i < NITEM; i++) { assertf(list_member(&head, &itm[i]) == itm[i].scratchpad, "%zu should:%d is:%d", i, itm[i].scratchpad, list_member(&head, &itm[i])); + assertf(list_anywhere(&itm[i]) == itm[i].scratchpad, + "%zu should:%d is:%d", i, itm[i].scratchpad, + list_anywhere(&itm[i])); + } ts_hash("member", "42b8950c880535b2d2e0c980f9845f7841ecf675c0fb9801aec4170d2036349d"); #endif @@ -746,6 +755,7 @@ static void concat(test_, TYPE)(void) #undef list_find_lt #undef list_find_gteq #undef list_member +#undef list_anywhere #undef list_del #undef list_pop #undef list_swap_all -- 2.39.5