summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/generator.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-05-04 11:51:56 +0200
committerTyge Løvset <[email protected]>2023-05-04 16:19:00 +0200
commitb03148caa1d6fc660e6e7c5986dd6fd38779bedc (patch)
tree6e4c7972437f6e8e6fddf39b0f48a2b0ace05af8 /misc/examples/generator.c
parent6b23e35287f26dad63abd755c5f365b443e025a3 (diff)
downloadSTC-modified-b03148caa1d6fc660e6e7c5986dd6fd38779bedc.tar.gz
STC-modified-b03148caa1d6fc660e6e7c5986dd6fd38779bedc.zip
Updates in coroutines.h: No longer *required* with cco_final:, but only when no cleanup is needed.
Diffstat (limited to 'misc/examples/generator.c')
-rw-r--r--misc/examples/generator.c32
1 files changed, 16 insertions, 16 deletions
diff --git a/misc/examples/generator.c b/misc/examples/generator.c
index 2bccc489..41dffafb 100644
--- a/misc/examples/generator.c
+++ b/misc/examples/generator.c
@@ -4,37 +4,38 @@
#include <stdio.h>
typedef struct {
- int n;
+ int size;
int a, b, c;
-} Triple_value, Triple;
+} Triple, Triple_value;
typedef struct {
Triple_value* ref;
+ int count;
int cco_state;
} Triple_iter;
-bool Triple_next(Triple_iter* it) {
+void Triple_next(Triple_iter* it) {
Triple_value* t = it->ref;
cco_begin(it);
- for (t->c = 1;; ++t->c) {
+ for (t->c = 5; t->size; ++t->c) {
for (t->a = 1; t->a < t->c; ++t->a) {
for (t->b = t->a; t->b < t->c; ++t->b) {
if (t->a*t->a + t->b*t->b == t->c*t->c) {
- if (t->n-- == 0) cco_return;
- cco_yield(true);
+ if (it->count++ == t->size)
+ cco_return;
+ cco_yield();
}
}
}
}
- cco_final:
- it->ref = NULL;
- cco_end(false);
+ cco_final:
+ it->ref = NULL;
+ cco_end();
}
Triple_iter Triple_begin(Triple* t) {
- Triple_iter it = {t};
- if (t->n > 0) Triple_next(&it);
- else it.ref = NULL;
+ Triple_iter it = {.ref=t};
+ Triple_next(&it);
return it;
}
@@ -42,11 +43,10 @@ Triple_iter Triple_begin(Triple* t) {
int main()
{
puts("Pythagorean triples with c < 100:");
- Triple t = {INT32_MAX};
- c_foreach (i, Triple, t)
- {
+ Triple triple = {.size=30}; // max number of triples
+ c_foreach (i, Triple, triple) {
if (i.ref->c < 100)
- printf("%u: (%d, %d, %d)\n", INT32_MAX - i.ref->n + 1, i.ref->a, i.ref->b, i.ref->c);
+ printf("%u: (%d, %d, %d)\n", i.count, i.ref->a, i.ref->b, i.ref->c);
else
cco_stop(&i);
}