diff options
| author | Tyge Løvset <[email protected]> | 2023-02-27 16:37:13 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-27 16:37:13 +0100 |
| commit | 400e5bd8ad2b2daef411e5530b6fda6158672829 (patch) | |
| tree | 0473b8c629dc158dc632db2815a6c7f08adc5ce0 /misc | |
| parent | d37820ddc211aec2876950cd4f2236cc9b92c9eb (diff) | |
| download | STC-modified-400e5bd8ad2b2daef411e5530b6fda6158672829.tar.gz STC-modified-400e5bd8ad2b2daef411e5530b6fda6158672829.zip | |
Enhanced c_with and c_scope macros. Improved coroutine.h and example.
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/examples/coroutines.c | 73 |
1 files changed, 49 insertions, 24 deletions
diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 89b428ee..0588c2ea 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -3,41 +3,59 @@ #include <stdint.h> // Demonstrate to call another coroutine from a coroutine: -// First create a 2D iterator, then call fibonacci sequence: +// First create prime generator, then call fibonacci sequence: -struct iterate { - int max_x, max_y; +bool is_prime(int64_t i) { + for (int64_t j=2; j*j <= i; ++j) + if (i % j == 0) return false; + return true; +} + +struct prime { + int count, idx; + int64_t num, result; int cco_state; - int x, y; }; -bool iterate(struct iterate* I) { - cco_begin(I); - for (I->x = 0; I->x < I->max_x; I->x++) - for (I->y = 0; I->y < I->max_y; I->y++) +bool prime(struct prime* U) { + cco_begin(U); + if (U->num < 2) U->num = 2; + if (U->num == 2) { + ++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->idx; + U->result = U->num; cco_yield(true); + } cco_final: cco_end(false); } + // Use coroutine to create a fibonacci sequence generator: struct fibonacci { - int n; + int count, idx; + int64_t result, b; int cco_state; - int64_t a, b, idx; }; int64_t fibonacci(struct fibonacci* F) { - assert (F->n < 95); + assert (F->count < 94); cco_begin(F); - F->a = 0; + F->idx = 0; + F->result = 0; F->b = 1; - for (F->idx = 0; F->idx < F->n; F->idx++) { - cco_yield(F->a); - int64_t sum = F->a + F->b; // NB! locals only lasts until next cco_yield! - F->a = F->b; + for (F->idx = 1; F->idx < F->count; F->idx++) { + cco_yield(F->result); + int64_t sum = F->result + F->b; // NB! locals only lasts until next cco_yield! + F->result = F->b; F->b = sum; } cco_final: @@ -47,24 +65,31 @@ int64_t fibonacci(struct fibonacci* F) { // Combine struct combine { - struct iterate it; + struct prime prm; struct fibonacci fib; int cco_state; }; bool combine(struct combine* C) { cco_begin(C); - cco_yield_coroutine(&C->it, iterate(&C->it), true); - cco_yield_coroutine(&C->fib, fibonacci(&C->fib), true); - // May reuse the C->it context; state has been reset to 0. - C->it.max_x = 2; C->it.max_y = 2; - cco_yield_coroutine(&C->it, iterate(&C->it), true); + cco_coroutine(prime(&C->prm), &C->prm, true); + cco_coroutine(fibonacci(&C->fib), &C->fib, true); + + // Reuse the C->prm context and extend the count: + C->prm.count = 20; + cco_coroutine(prime(&C->prm), &C->prm, true); + cco_final: puts("final"); cco_end(false); } int main(void) { - struct combine comb = {.it={3, 3}, .fib={14}}; + struct combine comb = {.prm={.count=10}, .fib={14}}; while (combine(&comb)) - printf("Iter=(%d, %d). Fib=%lld\n", comb.it.x, comb.it.y, (long long)comb.fib.a); + if (cco_done(&comb.prm)) + cco_reset(&comb.prm); + else + printf("Prime(%d)=%lld, Fib(%d)=%lld\n", + comb.prm.idx, (long long)comb.prm.result, + comb.fib.idx, (long long)comb.fib.result); } |
