summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authortylov <[email protected]>2023-07-30 22:46:33 +0200
committertylov <[email protected]>2023-07-30 22:46:33 +0200
commite6003c0ecfd7e76803bac7d98feca814997e8a86 (patch)
treeb63ce2c6886af2a1e396f98566902e562e830bd8 /misc/examples
parentb7efd45f0b59511be7202ce0641283e8a8ff6028 (diff)
downloadSTC-modified-e6003c0ecfd7e76803bac7d98feca814997e8a86.tar.gz
STC-modified-e6003c0ecfd7e76803bac7d98feca814997e8a86.zip
Added cco_generator(Name, ...) macro to simplify defining the iterator struct. Updated example generator.c
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/coroutines/generator.c19
1 files changed, 9 insertions, 10 deletions
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);