summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authortylov <[email protected]>2023-07-23 23:51:02 +0200
committertylov <[email protected]>2023-07-23 23:51:02 +0200
commitdd87a488eeeffa41a06757dda2220da21337a35e (patch)
treef9af2908c4f24775c57e87af51ed5dd08974cc0c
parente8aed0431140fd0d202d302f19f756046858bad7 (diff)
downloadSTC-modified-dd87a488eeeffa41a06757dda2220da21337a35e.tar.gz
STC-modified-dd87a488eeeffa41a06757dda2220da21337a35e.zip
- algo/sort.h: Use plural form of i_key (or i_val) to define default name for sort, like: <i_key>s_sort_n(data, n).
- Updated some examples.
-rw-r--r--include/stc/algo/sort.h2
-rw-r--r--misc/examples/coroutines/cotasks2.c35
-rw-r--r--misc/examples/coroutines/triples.c51
-rw-r--r--misc/examples/smartpointers/music_arc.c12
4 files changed, 49 insertions, 51 deletions
diff --git a/include/stc/algo/sort.h b/include/stc/algo/sort.h
index 06d7395f..86c9b9f1 100644
--- a/include/stc/algo/sort.h
+++ b/include/stc/algo/sort.h
@@ -70,7 +70,7 @@ int main(void) {
#ifndef i_tag
#define i_tag i_key
#endif
- #define i_type c_PASTE(i_tag, array)
+ #define i_type c_PASTE(i_tag, s)
typedef i_key i_type;
#endif
#ifndef i_at
diff --git a/misc/examples/coroutines/cotasks2.c b/misc/examples/coroutines/cotasks2.c
index 24a9f23f..4fdf98d9 100644
--- a/misc/examples/coroutines/cotasks2.c
+++ b/misc/examples/coroutines/cotasks2.c
@@ -42,18 +42,20 @@ int produce_items(struct produce_items* p, cco_runtime* rt)
{
cco_routine (p) {
p->str = cstr_null;
+ p->next.cco_func = next_value;
while (true)
{
- // await for next CCO_YIELD in next_value()
+ // await for next CCO_YIELD (or CCO_DONE) in next_value
cco_task_await(&p->next, rt, CCO_YIELD);
cstr_printf(&p->str, "item %d", p->next.val);
print_time();
printf("produced %s\n", cstr_str(&p->str));
cco_yield();
}
+
cco_cleanup:
- cstr_drop(&p->str);
- puts("done produce");
+ cstr_drop(&p->str);
+ puts("done produce");
}
return 0;
}
@@ -68,6 +70,8 @@ cco_task_struct (consume_items,
int consume_items(struct consume_items* c, cco_runtime* rt)
{
cco_routine (c) {
+ c->produce.cco_func = produce_items;
+
for (c->i = 1; c->i <= c->n; ++c->i)
{
printf("consume #%d\n", c->i);
@@ -75,10 +79,11 @@ int consume_items(struct consume_items* c, cco_runtime* rt)
print_time();
printf("consumed %s\n", cstr_str(&c->produce.str));
}
+
cco_cleanup:
- cco_stop(&c->produce);
- cco_task_resume(&c->produce, rt);
- puts("done consume");
+ cco_stop(&c->produce);
+ cco_task_resume(&c->produce, rt);
+ puts("done consume");
}
return 0;
}
@@ -86,18 +91,8 @@ int consume_items(struct consume_items* c, cco_runtime* rt)
int main(void)
{
struct consume_items consume = {
- .n=5,
- .cco_func=consume_items,
- .produce={.cco_func=produce_items, .next={.cco_func=next_value}},
+ .cco_func = consume_items,
+ .n = 5,
};
- int count = 0;
-
- cco_task_blocking(&consume)
- {
- ++count;
- //cco_sleep(0.001);
- //if (consume.i == 3)
- // cco_stop(&consume);
- }
- printf("count: %d\n", count);
-} \ No newline at end of file
+ cco_task_blocking(&consume);
+}
diff --git a/misc/examples/coroutines/triples.c b/misc/examples/coroutines/triples.c
index fe1ca7c3..9fd771ce 100644
--- a/misc/examples/coroutines/triples.c
+++ b/misc/examples/coroutines/triples.c
@@ -3,22 +3,14 @@
#include <stdio.h>
#include <stc/coroutine.h>
-int gcd(int a, int b) {
- while (b) {
- int t = a % b;
- a = b;
- b = t;
- }
- return a;
-}
-
-void triples_vanilla(int n) {
- for (int c = 5, i = 0; n; ++c) {
+void triples_vanilla(int max_c) {
+ for (int c = 5, i = 0;; ++c) {
for (int a = 1; a < c; ++a) {
for (int b = a + 1; b < c; ++b) {
- if ((int64_t)a*a + (int64_t)b*b == (int64_t)c*c && gcd(a, b) == 1) {
+ if ((int64_t)a*a + (int64_t)b*b == (int64_t)c*c) {
+ if (c > max_c)
+ goto done;
printf("%d: {%d, %d, %d}\n", ++i, a, b, c);
- if (--n == 0) goto done;
}
}
}
@@ -27,19 +19,21 @@ void triples_vanilla(int n) {
}
struct triples {
- int size, count;
+ int max_c;
int a, b, c;
int cco_state;
};
int triples_coro(struct triples* t) {
cco_routine(t) {
- t->count = 0;
- for (t->c = 5; t->size; ++t->c) {
+ for (t->c = 5;; ++t->c) {
for (t->a = 1; t->a < t->c; ++t->a) {
for (t->b = t->a + 1; t->b < t->c; ++t->b) {
- if ((int64_t)t->a*t->a + (int64_t)t->b*t->b == (int64_t)t->c*t->c) {
- if (t->count++ == t->size)
+ if ((int64_t)t->a * t->a +
+ (int64_t)t->b * t->b ==
+ (int64_t)t->c * t->c)
+ {
+ if (t->c > t->max_c)
cco_return;
cco_yield();
}
@@ -52,20 +46,29 @@ int triples_coro(struct triples* t) {
return 0;
}
+int gcd(int a, int b) {
+ while (b) {
+ int t = a % b;
+ a = b;
+ b = t;
+ }
+ return a;
+}
+
int main(void)
{
puts("Vanilla triples:");
- triples_vanilla(5);
+ triples_vanilla(20);
- puts("\nCoroutine triples:");
- struct triples t = {.size=INT32_MAX};
+ puts("\nCoroutine triples with GCD = 1:");
+ struct triples t = {.max_c = 100};
int n = 0;
- while (triples_coro(&t)) {
+ cco_call_blocking(triples_coro(&t)) {
if (gcd(t.a, t.b) > 1)
continue;
- if (t.c < 100)
- printf("%d: {%d, %d, %d}\n", ++n, t.a, t.b, t.c);
+ if (++n <= 20)
+ printf("%d: {%d, %d, %d}\n", n, t.a, t.b, t.c);
else
cco_stop(&t);
}
diff --git a/misc/examples/smartpointers/music_arc.c b/misc/examples/smartpointers/music_arc.c
index 16111b0b..13d368c3 100644
--- a/misc/examples/smartpointers/music_arc.c
+++ b/misc/examples/smartpointers/music_arc.c
@@ -12,7 +12,7 @@ typedef struct
int Song_cmp(const Song* x, const Song* y)
{ return cstr_cmp(&x->title, &y->title); }
-Song Song_make(const char* artist, const char* title)
+Song Song_init(const char* artist, const char* title)
{ return c_LITERAL(Song){cstr_from(artist), cstr_from(title)}; }
void Song_drop(Song* s) {
@@ -34,9 +34,9 @@ void Song_drop(Song* s) {
void example3(void)
{
SongVec vec1 = c_init(SongVec, {
- Song_make("Bob Dylan", "The Times They Are A Changing"),
- Song_make("Aretha Franklin", "Bridge Over Troubled Water"),
- Song_make("Thalia", "Entre El Mar y Una Estrella")
+ Song_init("Bob Dylan", "The Times They Are A Changing"),
+ Song_init("Aretha Franklin", "Bridge Over Troubled Water"),
+ Song_init("Thalia", "Entre El Mar y Una Estrella")
});
SongVec vec2 = {0};
@@ -47,8 +47,8 @@ void example3(void)
// Add a few more to vec2. We can use emplace when creating new entries
// Emplace calls SongArc_from() on the argument to create the Arc:
- SongVec_emplace(&vec2, Song_make("Michael Jackson", "Billie Jean"));
- SongVec_emplace(&vec2, Song_make("Rihanna", "Stay"));
+ SongVec_emplace(&vec2, Song_init("Michael Jackson", "Billie Jean"));
+ SongVec_emplace(&vec2, Song_init("Rihanna", "Stay"));
// We now have two vectors with some shared, some unique entries.
c_forlist (i, SongVec, {vec1, vec2}) {