diff --git a/m-algo.h b/m-algo.h index 7a430931..f283b906 100644 --- a/m-algo.h +++ b/m-algo.h @@ -167,6 +167,20 @@ return count; \ } \ \ + static inline size_t \ + M_C(name, _count_if) (container_t l, bool (*func)(type_t const data)) \ + { \ + it_t it; \ + size_t count = 0; \ + for (M_GET_IT_FIRST cont_oplist (it, l); \ + !M_GET_IT_END_P cont_oplist (it) ; \ + M_GET_IT_NEXT cont_oplist (it)) { \ + if (func (*M_GET_IT_CREF cont_oplist (it))) \ + count++ ; \ + } \ + return count; \ + } \ + \ static inline void \ M_C(name, _map) (container_t l, void (*f)(type_t const) ) \ { \ diff --git a/tests/test-malgo.c b/tests/test-malgo.c index 105fa821..41e43add 100644 --- a/tests/test-malgo.c +++ b/tests/test-malgo.c @@ -168,6 +168,9 @@ static void test_array(void) assert( algo_array_count(l, -1) == 0); assert( algo_array_sort_p(l) == false); + assert( algo_array_count_if(l, func_test_42) == 1); + assert( algo_array_count_if(l, func_test_101) == 0); + array_int_it_t it; algo_array_find_last(it, l, 17); assert (!array_int_end_p (it));