summaryrefslogtreecommitdiffhomepage
path: root/include/stc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-03-07 22:26:36 +0100
committerTyge Løvset <[email protected]>2023-03-07 22:26:36 +0100
commitecc0b2108cffeb725e3b8e2574b6fb7927dfd96e (patch)
tree221a40b1e41f64353e2b3143277a250ef2ae2ce5 /include/stc
parenta203314647b5c37c7e40230551457f006ff36cd5 (diff)
downloadSTC-modified-ecc0b2108cffeb725e3b8e2574b6fb7927dfd96e.tar.gz
STC-modified-ecc0b2108cffeb725e3b8e2574b6fb7927dfd96e.zip
Improved/simplified c_forfilter (): last optional parameter gone. Now c_flt_take() and c_flt_takewhile() breaks the loop always.
c11/fmt.h : renamed fmt_freebuffer(buf) => fmt_destroy(buf).
Diffstat (limited to 'include/stc')
-rw-r--r--include/stc/algo/crange.h2
-rw-r--r--include/stc/algo/filter.h50
-rw-r--r--include/stc/cspan.h2
3 files changed, 32 insertions, 22 deletions
diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h
index 1b840516..4fc957b6 100644
--- a/include/stc/algo/crange.h
+++ b/include/stc/algo/crange.h
@@ -36,7 +36,7 @@ int main()
int a = 100, b = INT32_MAX;
c_forfilter (i, crange, crange_obj(a, b, 8)
, i.index > 10
- , c_flt_take(i, 3))
+ && c_flt_take(i, 3))
printf(" %lld", *i.ref);
puts("");
}
diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h
index 6f8e1654..48a36d9b 100644
--- a/include/stc/algo/filter.h
+++ b/include/stc/algo/filter.h
@@ -36,9 +36,9 @@ int main()
puts("");
c_forfilter (i, cstack_int, stk
- , c_flt_skipwhile(i, *i.ref < 3)
- && (*i.ref & 1) == 0 // even only
- , c_flt_take(i, 2)) // break after 2
+ , c_flt_skipwhile(i, *i.ref < 3)
+ && (*i.ref & 1) == 0 // even only
+ && c_flt_take(i, 2)) // break after 2
printf(" %d", *i.ref);
puts("");
}
@@ -53,27 +53,37 @@ int main()
#define c_NFILTERS 32
#endif
-#define c_flt_take(i, n) (c_flt_count(i) <= (n))
+#define c_flt_take(i, n) _flt_take(&(i).b, n)
#define c_flt_skip(i, n) (c_flt_count(i) > (n))
-#define c_flt_skipwhile(i, pred) ((i).s2[(i).s2top++] |= !(pred))
-#define c_flt_takewhile(i, pred) !c_flt_skipwhile(i, pred)
-#define c_flt_last(i) (i).s1[(i).s1top-1]
-#define c_flt_count(i) ++(i).s1[(i).s1top++]
+#define c_flt_skipwhile(i, pred) ((i).b.s2[(i).b.s2top++] |= !(pred))
+#define c_flt_takewhile(i, pred) _flt_takewhile(&(i).b, pred)
+#define c_flt_last(i) (i).b.s1[(i).b.s1top-1]
+#define c_flt_count(i) ++(i).b.s1[(i).b.s1top++]
-#define c_forfilter(...) c_MACRO_OVERLOAD(c_forfilter, __VA_ARGS__)
+#define c_forfilter(i, C, cnt, filter) \
+ for (struct {struct _flt_base b; C##_iter it; C##_value *ref;} \
+ i = {.it=C##_begin(&cnt), .ref=i.it.ref} ; !i.b.done & (i.ref != NULL) ; \
+ C##_next(&i.it), i.ref = i.it.ref, i.b.s1top=0, i.b.s2top=0) \
+ if (!(filter)) ; else
-#define c_forfilter_4(i, C, cnt, filter) \
- c_forfilter_B(i, C, C##_begin(&cnt), filter)
+// -----
-#define c_forfilter_5(i, C, cnt, filter, cond) \
- c_forfilter_B(i, C, C##_begin(&cnt), filter) if (!(cond)) break; else
+struct _flt_base {
+ uint32_t s1[c_NFILTERS];
+ bool s2[c_NFILTERS], done;
+ uint8_t s1top, s2top;
+};
-#define c_forfilter_B(i, C, start, filter) \
- for (struct {C##_iter it; C##_value *ref; \
- uint32_t s1[c_NFILTERS], index; \
- bool s2[c_NFILTERS]; uint8_t s1top, s2top;} \
- i = {.it=start, .ref=i.it.ref}; i.it.ref \
- ; C##_next(&i.it), i.ref = i.it.ref, ++i.index, i.s1top=0, i.s2top=0) \
- if (!(filter)) ; else
+static inline bool _flt_take(struct _flt_base* b, uint32_t n) {
+ uint32_t k = ++b->s1[b->s1top++];
+ b->done |= (k >= n);
+ return k <= n;
+}
+
+static inline bool _flt_takewhile(struct _flt_base* b, bool pred) {
+ bool skip = (b->s2[b->s2top++] |= !pred);
+ b->done |= skip;
+ return !skip;
+}
#endif
diff --git a/include/stc/cspan.h b/include/stc/cspan.h
index c6df51af..00313540 100644
--- a/include/stc/cspan.h
+++ b/include/stc/cspan.h
@@ -50,7 +50,7 @@ int demo2() {
c_forfilter (i, Intspan, span,
, c_flt_skipwhile(i, *i.ref < 25)
&& (*i.ref & 1) == 0 // even only
- , c_flt_take(i, 2)) // break after 2
+ && c_flt_take(i, 2)) // break after 2
printf(" %d", *i.ref);
puts("");
}