From a1d6c85b72027c9cd09d6bf0b1e0f7c3942e4aee Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 21 Sep 2022 11:44:24 +0200 Subject: Added c_forfilter() macro(s) - easy filtering of elements. --- benchmarks/shootout_hashmaps.cpp | 1 - include/stc/ccommon.h | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/benchmarks/shootout_hashmaps.cpp b/benchmarks/shootout_hashmaps.cpp index ec3852f1..334eff64 100644 --- a/benchmarks/shootout_hashmaps.cpp +++ b/benchmarks/shootout_hashmaps.cpp @@ -35,7 +35,6 @@ KHASH_MAP_INIT_INT64(ii, IValue) #define i_key IKey #define i_val IValue #define i_size uint32_t // optional, enables 2x expand -#define i_hash(x) (*x * 0xc6a4a7935bd1e99d) // optional #define i_tag ii #include diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 0c724d55..db273296 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -162,6 +162,17 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, for (C##_iter it = start, *_endref = (C##_iter*)(finish).ref \ ; it.ref != (C##_value*)_endref; C##_next(&it)) +#define c_forpred(i, C, cnt, pred) \ + for (struct {C##_iter it; const C##_value *ref; size_t idx, count;} \ + i = {.it=C##_begin(&cnt), .ref=i.it.ref, .idx=0, .count=1} \ + ; i.ref && (pred); C##_next(&i.it), i.ref = i.it.ref, ++i.idx) + +#define c_forfilter(...) c_MACRO_OVERLOAD(c_forfilter, __VA_ARGS__) +#define c_forfilter4(it, C, cnt, filter) \ + c_forpred(it, C, cnt, true) if (!(filter)) ; else +#define c_forfilter5(it, C, cnt, filter, pred) \ + c_forpred(it, C, cnt, pred) if (!((filter) && ++it.count)) ; else + #define c_forpair(key, val, C, cnt) /* structured binding */ \ for (struct {C##_iter _it; const C##_key* key; C##_mapped* val;} _ = {C##_begin(&cnt)} \ ; _._it.ref && (_.key = &_._it.ref->first, _.val = &_._it.ref->second) \ -- cgit v1.2.3