diff options
| -rw-r--r-- | include/stc/coroutine.h | 14 | ||||
| -rw-r--r-- | misc/examples/coroutines/generator.c | 19 |
2 files changed, 22 insertions, 11 deletions
diff --git a/include/stc/coroutine.h b/include/stc/coroutine.h index 9f55dddf..7917878a 100644 --- a/include/stc/coroutine.h +++ b/include/stc/coroutine.h @@ -132,7 +132,19 @@ typedef enum { (void)((co)->cco_state = 0) /* - * Tasks (optional) + * Generators + */ + +#define cco_generator(Name, ...) \ + typedef Name Name##_value; \ + typedef struct { \ + Name##_value* ref; \ + int cco_state; \ + __VA_ARGS__ \ + } Name##_iter + +/* + * Tasks */ struct cco_runtime; diff --git a/misc/examples/coroutines/generator.c b/misc/examples/coroutines/generator.c index f9e59fea..c092b92d 100644 --- a/misc/examples/coroutines/generator.c +++ b/misc/examples/coroutines/generator.c @@ -6,19 +6,17 @@ typedef struct { int size; int a, b, c; -} Triple, Triple_value; +} Triple; -typedef struct { - Triple_value* ref; +cco_generator(Triple, int count; - int cco_state; -} Triple_iter; +); int Triple_next(Triple_iter* it) { - Triple_value* g = it->ref; + Triple* g = it->ref; // note: before cco_routine cco_routine(it) { - for (g->c = 5; g->size; ++g->c) { + for (g->c = 5;; ++g->c) { for (g->a = 1; g->a < g->c; ++g->a) { for (g->b = g->a; g->b < g->c; ++g->b) { if (g->a*g->a + g->b*g->b == g->c*g->c) { @@ -31,12 +29,13 @@ int Triple_next(Triple_iter* it) { } cco_cleanup: it->ref = NULL; + puts("done"); } return 0; } Triple_iter Triple_begin(Triple* g) { - Triple_iter it = {.ref=g}; + Triple_iter it = {.ref=g}; Triple_next(&it); return it; } @@ -44,8 +43,8 @@ Triple_iter Triple_begin(Triple* g) { int main(void) { - puts("Pythagorean triples with c < 100:"); - Triple triple = {.size=30}; // max number of triples + puts("Pythagorean triples; stops at 100 triples or c >= 100:"); + Triple triple = {.size=100}; c_foreach (i, Triple, triple) { if (i.ref->c < 100) printf("%u: (%d, %d, %d)\n", i.count, i.ref->a, i.ref->b, i.ref->c); |
