]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add unsorted typesafe_anywhere()
authorDavid Lamparter <equinox@opensourcerouting.org>
Wed, 29 Sep 2021 17:59:27 +0000 (19:59 +0200)
committerDavid Lamparter <equinox@opensourcerouting.org>
Tue, 19 Oct 2021 12:55:39 +0000 (14:55 +0200)
*_anywhere(item) returns whether an item is on _any_ container.  Only
available for unsorted containers for now.

Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
lib/typesafe.h
tests/lib/test_typelist.h

index b03934b9649484e18ae6cce9ceec3aaefaf358f4..44c42ffbca96ba5bcb27933e1dae1175123b5eda 100644 (file)
@@ -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)                            \
 {                                                                              \
index a12a995bf6f33559280c538c7d09244c3d28c7ed..8261616ed215af6d4292b71ed5a64a07aa45053b 100644 (file)
@@ -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