summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-21 11:44:24 +0200
committerTyge Løvset <[email protected]>2022-09-21 11:44:24 +0200
commita1d6c85b72027c9cd09d6bf0b1e0f7c3942e4aee (patch)
tree973005252d207952bf4fabd46222dcfe381299fe
parent6d7873be4a9031ceffef3182ca8cdfa80197c8b0 (diff)
downloadSTC-modified-a1d6c85b72027c9cd09d6bf0b1e0f7c3942e4aee.tar.gz
STC-modified-a1d6c85b72027c9cd09d6bf0b1e0f7c3942e4aee.zip
Added c_forfilter() macro(s) - easy filtering of elements.
-rw-r--r--benchmarks/shootout_hashmaps.cpp1
-rw-r--r--include/stc/ccommon.h11
2 files changed, 11 insertions, 1 deletions
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 <stc/cmap.h>
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) \