diff options
| author | Tyge Løvset <[email protected]> | 2023-02-28 08:40:40 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-28 08:40:40 +0100 |
| commit | 0fb86f00db4f1d3600ec1b91d0a4e2ef8f778164 (patch) | |
| tree | b2b65809bda272f73fa06147df549e6f889886b5 /misc/examples | |
| parent | d2228c3dc993e47c8d2df951230cf43a93299f5f (diff) | |
| download | STC-modified-0fb86f00db4f1d3600ec1b91d0a4e2ef8f778164.tar.gz STC-modified-0fb86f00db4f1d3600ec1b91d0a4e2ef8f778164.zip | |
Fixed coroutine.h and examples. cco_return; has no arguments.
Diffstat (limited to 'misc/examples')
| -rw-r--r-- | misc/examples/coroutines.c | 43 | ||||
| -rw-r--r-- | misc/examples/triples.c | 13 |
2 files changed, 33 insertions, 23 deletions
diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 0588c2ea..867d02a8 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -13,26 +13,29 @@ bool is_prime(int64_t i) { struct prime { int count, idx; - int64_t num, result; + int64_t result, pos; int cco_state; }; bool prime(struct prime* U) { cco_begin(U); - if (U->num < 2) U->num = 2; - if (U->num == 2) { + if (U->result < 2) U->result = 2; + if (U->result == 2) { + if (U->count-- == 0) cco_return; ++U->idx; - U->result = U->num; cco_yield(true); } - U->num += !(U->num & 1); - for (; U->idx < U->count; U->num += 2) - if (is_prime(U->num)) { + U->result += !(U->result & 1); + for (U->pos = U->result; U->count > 0; U->pos += 2) { + if (is_prime(U->pos)) { + --U->count; ++U->idx; - U->result = U->num; + U->result = U->pos; cco_yield(true); } + } cco_final: + printf("final prm\n"); cco_end(false); } @@ -45,21 +48,23 @@ struct fibonacci { int cco_state; }; -int64_t fibonacci(struct fibonacci* F) { - assert (F->count < 94); +bool fibonacci(struct fibonacci* F) { + assert(F->count < 94); cco_begin(F); - F->idx = 0; + F->idx = 1; F->result = 0; F->b = 1; - for (F->idx = 1; F->idx < F->count; F->idx++) { - cco_yield(F->result); + for (;; F->idx++) { + if (F->count-- == 0) cco_return; + cco_yield(true); int64_t sum = F->result + F->b; // NB! locals only lasts until next cco_yield! F->result = F->b; F->b = sum; } cco_final: - cco_end(-1); + printf("final fib\n"); + cco_end(false); } // Combine @@ -85,11 +90,13 @@ bool combine(struct combine* C) { int main(void) { struct combine comb = {.prm={.count=10}, .fib={14}}; - while (combine(&comb)) - if (cco_done(&comb.prm)) - cco_reset(&comb.prm); - else + if (true) + while (combine(&comb)) printf("Prime(%d)=%lld, Fib(%d)=%lld\n", comb.prm.idx, (long long)comb.prm.result, comb.fib.idx, (long long)comb.fib.result); + else + while (prime(&comb.prm)) + printf("Prime(%d)=%lld\n", + comb.prm.idx, (long long)comb.prm.result); } diff --git a/misc/examples/triples.c b/misc/examples/triples.c index e85558a3..05e6fca2 100644 --- a/misc/examples/triples.c +++ b/misc/examples/triples.c @@ -18,22 +18,27 @@ void triples_vanilla(int n) { } struct tricoro { + int n; int cco_state; int x, y, z; }; +#include <stdlib.h> + bool triples_coro(struct tricoro* 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; cco_yield(true); } } } } cco_final: + puts("final"); cco_end(false); } @@ -41,12 +46,10 @@ bool triples_coro(struct tricoro* t) { int main() { puts("Vanilla triples:"); - triples_vanilla(20); + triples_vanilla(6); puts("\nCoroutine triples:"); - struct tricoro t = {0}; - int i = 0; - while (i++ < 20 && triples_coro(&t)) + struct tricoro t = {6}; + while (triples_coro(&t)) printf("{%d, %d, %d},\n", t.x, t.y, t.z); - triples_coro(cco_stop(&t)); }
\ No newline at end of file |
