diff options
| author | Tyge Løvset <[email protected]> | 2023-03-07 22:26:36 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-03-07 22:26:36 +0100 |
| commit | ecc0b2108cffeb725e3b8e2574b6fb7927dfd96e (patch) | |
| tree | 221a40b1e41f64353e2b3143277a250ef2ae2ce5 /misc/examples | |
| parent | a203314647b5c37c7e40230551457f006ff36cd5 (diff) | |
| download | STC-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 'misc/examples')
| -rw-r--r-- | misc/examples/forfilter.c | 12 | ||||
| -rw-r--r-- | misc/examples/forloops.c | 2 | ||||
| -rwxr-xr-x | misc/examples/make.sh | 2 | ||||
| -rw-r--r-- | misc/examples/prime.c | 2 | ||||
| -rw-r--r-- | misc/examples/triples.c | 34 |
5 files changed, 27 insertions, 25 deletions
diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index 5e1cf15e..2be975a6 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -36,9 +36,9 @@ void demo1(void) , c_flt_skipwhile(i, *i.ref != 80) && c_flt_skip(i, 1) && c_flt_skipwhile(i, *i.ref != 80) - && flt_isEven(i) - && flt_skipValue(i, 80) - , c_flt_take(i, 5) // short-circuit + && flt_isEven(i) + && flt_skipValue(i, 80) + && c_flt_take(i, 5) // short-circuit ){ sum += res = flt_square(i); printf(" %d", res); @@ -68,7 +68,7 @@ void demo2(void) c_forfilter (x, crange, R , c_flt_skipwhile(x, *x.ref != 11) && *x.ref % 2 != 0 - , c_flt_take(x, 5)) + && c_flt_take(x, 5)) IVec_push(&vector, (int)(*x.ref * *x.ref)); c_foreach (x, IVec, vector) printf(" %d", *x.ref); @@ -131,8 +131,8 @@ void demo5(void) , c_flt_skip(i,15) && c_flt_skipwhile(i, flt_mid_decade(i)) && c_flt_skip(i,30) - && flt_even(i) - , c_flt_take(i,10)) + && flt_even(i) + && c_flt_take(i,5)) printf(" %lld", *i.ref); puts(""); } diff --git a/misc/examples/forloops.c b/misc/examples/forloops.c index 66c587c4..707e8285 100644 --- a/misc/examples/forloops.c +++ b/misc/examples/forloops.c @@ -73,7 +73,7 @@ int main() && c_flt_skipwhile(i, isOdd(i))
&& isOdd(i)
&& c_flt_skip(i, 2)
- , c_flt_take(i, 1))
+ && c_flt_take(i, 2))
printf(" %d", *i.ref);
puts("");
// 189
diff --git a/misc/examples/make.sh b/misc/examples/make.sh index 5ac0b653..0297e5a1 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -6,7 +6,7 @@ if [ "$(uname)" = 'Linux' ]; then oflag='-o ' fi -cc=gcc; cflags="-s -O2 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings" +cc=gcc; cflags="-s -O3 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings" #cc=gcc; cflags="-g -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" #cc=tcc; cflags="-Wall -std=c99" #cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -Wno-unused-function -Wsign-compare -Wwrite-strings" diff --git a/misc/examples/prime.c b/misc/examples/prime.c index e705dcb7..59ee336c 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -45,7 +45,7 @@ int main(void) crange R = crange_make(n - 1, 0, -2); c_forfilter (i, crange, R , cbits_test(&primes, *i.ref>>1) - , c_flt_take(i, 50)) { + && c_flt_take(i, 50)) { printf("%lld ", *i.ref); if (c_flt_last(i) % 10 == 0) puts(""); } diff --git a/misc/examples/triples.c b/misc/examples/triples.c index 8a46d653..4783d603 100644 --- a/misc/examples/triples.c +++ b/misc/examples/triples.c @@ -4,11 +4,11 @@ #include <stdio.h> void triples_vanilla(int n) { - for (int i = 1, z = 1;; ++z) { - for (int x = 1; x < z; ++x) { - for (int y = x; y < z; ++y) { - if (x*x + y*y == z*z) { - printf("{%d, %d, %d},\n", x, y, z); + for (int i = 1, c = 1;; ++c) { + for (int a = 1; a < c; ++a) { + for (int b = a; b < c; ++b) { + if (a*a + b*b == c*c) { + printf("{%d, %d, %d},\n", a, b, c); if (++i > n) goto done; } } @@ -19,17 +19,17 @@ void triples_vanilla(int n) { struct triples { int n; - int x, y, z; + int a, b, c; int cco_state; }; -bool triples_coro(struct triples* t) { - cco_begin(t); - for (t->z = 1;; ++t->z) { - for (t->x = 1; t->x < t->z; ++t->x) { - for (t->y = t->x; t->y < t->z; ++t->y) { - if (t->x*t->x + t->y*t->y == t->z*t->z) { - if (t->n-- == 0) cco_return; +bool triples_next(struct triples* I) { + cco_begin(I); + for (I->c = 1;; ++I->c) { + for (I->a = 1; I->a < I->c; ++I->a) { + for (I->b = I->a; I->b < I->c; ++I->b) { + if (I->a*I->a + I->b*I->b == I->c*I->c) { + if (I->n-- == 0) cco_return; cco_yield(true); } } @@ -46,7 +46,9 @@ int main() triples_vanilla(6); puts("\nCoroutine triples:"); - struct triples t = {6}; - while (triples_coro(&t)) - printf("{%d, %d, %d},\n", t.x, t.y, t.z); + struct triples t = {INT32_MAX}; + while (triples_next(&t)) { + if (t.c < 100) printf("{%d, %d, %d},\n", t.a, t.b, t.c); + else cco_stop(&t); + } } |
