diff options
| author | Tyge Løvset <[email protected]> | 2023-07-24 08:48:41 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-07-24 08:48:41 +0200 |
| commit | 374b3c27831cd4e09461867ed231669777b96951 (patch) | |
| tree | 88011006f6d536cdb1ad1eca8073392ca80687cc /misc/examples | |
| parent | 177418232a2d8a8b0df1667d3e4bd15dc37db59f (diff) | |
| parent | 650b053f443f9132dadb6d1ca924c0b36849739f (diff) | |
| download | STC-modified-374b3c27831cd4e09461867ed231669777b96951.tar.gz STC-modified-374b3c27831cd4e09461867ed231669777b96951.zip | |
Merge pull request #65 from stclib/dev43
Dev43
Diffstat (limited to 'misc/examples')
| -rw-r--r-- | misc/examples/algorithms/forfilter.c (renamed from misc/examples/forfilter.c) | 7 | ||||
| -rw-r--r-- | misc/examples/algorithms/forloops.c (renamed from misc/examples/forloops.c) | 134 | ||||
| -rw-r--r-- | misc/examples/algorithms/random.c (renamed from misc/examples/random.c) | 20 | ||||
| -rw-r--r-- | misc/examples/algorithms/shape.c (renamed from misc/examples/shape.c) | 0 | ||||
| -rw-r--r-- | misc/examples/algorithms/shape.cpp (renamed from misc/examples/shape.cpp) | 0 | ||||
| -rw-r--r-- | misc/examples/bitsets/bits.c (renamed from misc/examples/bits.c) | 0 | ||||
| -rw-r--r-- | misc/examples/bitsets/bits2.c (renamed from misc/examples/bits2.c) | 0 | ||||
| -rw-r--r-- | misc/examples/bitsets/prime.c (renamed from misc/examples/prime.c) | 13 | ||||
| -rw-r--r-- | misc/examples/coroutines/cointerleave.c (renamed from misc/examples/cointerleave.c) | 4 | ||||
| -rw-r--r-- | misc/examples/coroutines/coread.c (renamed from misc/examples/coread.c) | 4 | ||||
| -rw-r--r-- | misc/examples/coroutines/coroutines.c (renamed from misc/examples/coroutines.c) | 10 | ||||
| -rw-r--r-- | misc/examples/coroutines/cotasks1.c | 100 | ||||
| -rw-r--r-- | misc/examples/coroutines/cotasks2.c | 98 | ||||
| -rw-r--r-- | misc/examples/coroutines/dining_philosophers.c (renamed from misc/examples/dining_philosophers.c) | 2 | ||||
| -rw-r--r-- | misc/examples/coroutines/filetask.c | 80 | ||||
| -rw-r--r-- | misc/examples/coroutines/generator.c (renamed from misc/examples/generator.c) | 2 | ||||
| -rw-r--r-- | misc/examples/coroutines/scheduler.c (renamed from misc/examples/scheduler.c) | 2 | ||||
| -rw-r--r-- | misc/examples/coroutines/triples.c (renamed from misc/examples/triples.c) | 53 | ||||
| -rw-r--r-- | misc/examples/hashmaps/birthday.c (renamed from misc/examples/birthday.c) | 0 | ||||
| -rw-r--r-- | misc/examples/hashmaps/books.c (renamed from misc/examples/books.c) | 0 | ||||
| -rw-r--r-- | misc/examples/hashmaps/hashmap.c (renamed from misc/examples/hashmap.c) | 0 | ||||
| -rw-r--r-- | misc/examples/hashmaps/new_map.c (renamed from misc/examples/new_map.c) | 0 | ||||
| -rw-r--r-- | misc/examples/hashmaps/phonebook.c (renamed from misc/examples/phonebook.c) | 0 | ||||
| -rw-r--r-- | misc/examples/hashmaps/unordered_set.c (renamed from misc/examples/unordered_set.c) | 0 | ||||
| -rw-r--r-- | misc/examples/hashmaps/vikings.c (renamed from misc/examples/vikings.c) | 0 | ||||
| -rw-r--r-- | misc/examples/linkedlists/intrusive.c (renamed from misc/examples/intrusive.c) | 2 | ||||
| -rw-r--r-- | misc/examples/linkedlists/list.c (renamed from misc/examples/list.c) | 8 | ||||
| -rw-r--r-- | misc/examples/linkedlists/list_erase.c (renamed from misc/examples/list_erase.c) | 0 | ||||
| -rw-r--r-- | misc/examples/linkedlists/list_splice.c (renamed from misc/examples/list_splice.c) | 0 | ||||
| -rw-r--r-- | misc/examples/linkedlists/new_list.c (renamed from misc/examples/new_list.c) | 2 | ||||
| -rwxr-xr-x | misc/examples/make.sh | 18 | ||||
| -rw-r--r-- | misc/examples/mixed/astar.c (renamed from misc/examples/astar.c) | 0 | ||||
| -rw-r--r-- | misc/examples/mixed/complex.c (renamed from misc/examples/complex.c) | 0 | ||||
| -rw-r--r-- | misc/examples/mixed/convert.c (renamed from misc/examples/convert.c) | 0 | ||||
| -rw-r--r-- | misc/examples/mixed/demos.c (renamed from misc/examples/demos.c) | 2 | ||||
| -rw-r--r-- | misc/examples/mixed/inits.c (renamed from misc/examples/inits.c) | 0 | ||||
| -rw-r--r-- | misc/examples/mixed/read.c (renamed from misc/examples/read.c) | 4 | ||||
| -rw-r--r-- | misc/examples/priorityqueues/functor.c (renamed from misc/examples/functor.c) | 0 | ||||
| -rw-r--r-- | misc/examples/priorityqueues/new_pque.c (renamed from misc/examples/new_pque.c) | 0 | ||||
| -rw-r--r-- | misc/examples/priorityqueues/priority.c (renamed from misc/examples/priority.c) | 6 | ||||
| -rw-r--r-- | misc/examples/queues/new_queue.c (renamed from misc/examples/new_queue.c) | 6 | ||||
| -rw-r--r-- | misc/examples/queues/queue.c (renamed from misc/examples/queue.c) | 10 | ||||
| -rw-r--r-- | misc/examples/rawptr_elements.c | 59 | ||||
| -rw-r--r-- | misc/examples/regularexpressions/regex1.c (renamed from misc/examples/regex1.c) | 0 | ||||
| -rw-r--r-- | misc/examples/regularexpressions/regex2.c (renamed from misc/examples/regex2.c) | 0 | ||||
| -rw-r--r-- | misc/examples/regularexpressions/regex_match.c (renamed from misc/examples/regex_match.c) | 0 | ||||
| -rw-r--r-- | misc/examples/regularexpressions/regex_replace.c (renamed from misc/examples/regex_replace.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sidebyside.cpp | 57 | ||||
| -rw-r--r-- | misc/examples/smartpointers/arc_containers.c (renamed from misc/examples/arc_containers.c) | 0 | ||||
| -rw-r--r-- | misc/examples/smartpointers/arc_demo.c (renamed from misc/examples/arc_demo.c) | 2 | ||||
| -rw-r--r-- | misc/examples/smartpointers/arcvec_erase.c (renamed from misc/examples/arcvec_erase.c) | 2 | ||||
| -rw-r--r-- | misc/examples/smartpointers/box.c (renamed from misc/examples/box.c) | 0 | ||||
| -rw-r--r-- | misc/examples/smartpointers/box2.c (renamed from misc/examples/box2.c) | 0 | ||||
| -rw-r--r-- | misc/examples/smartpointers/map_box.c | 34 | ||||
| -rw-r--r-- | misc/examples/smartpointers/map_ptr.c | 34 | ||||
| -rw-r--r-- | misc/examples/smartpointers/music_arc.c (renamed from misc/examples/music_arc.c) | 12 | ||||
| -rw-r--r-- | misc/examples/smartpointers/new_sptr.c (renamed from misc/examples/new_sptr.c) | 2 | ||||
| -rw-r--r-- | misc/examples/smartpointers/person_arc.c (renamed from misc/examples/person_arc.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/csmap_erase.c (renamed from misc/examples/csmap_erase.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/csmap_find.c (renamed from misc/examples/csmap_find.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/csmap_insert.c (renamed from misc/examples/csmap_insert.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/csset_erase.c (renamed from misc/examples/csset_erase.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/gauss2.c (renamed from misc/examples/gauss2.c) | 4 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/listmap.c (renamed from misc/examples/mmap.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/mapmap.c (renamed from misc/examples/mapmap.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/multimap.c (renamed from misc/examples/multimap.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/new_smap.c (renamed from misc/examples/new_smap.c) | 0 | ||||
| -rw-r--r-- | misc/examples/sortedmaps/sorted_map.c (renamed from misc/examples/sorted_map.c) | 0 | ||||
| -rw-r--r-- | misc/examples/spans/mdspan.c | 51 | ||||
| -rw-r--r-- | misc/examples/spans/multidim.c (renamed from misc/examples/multidim.c) | 0 | ||||
| -rw-r--r-- | misc/examples/spans/printspan.c (renamed from misc/examples/printspan.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/cstr_match.c (renamed from misc/examples/cstr_match.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/replace.c (renamed from misc/examples/replace.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/splitstr.c (renamed from misc/examples/splitstr.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/sso_map.c (renamed from misc/examples/sso_map.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/sso_substr.c (renamed from misc/examples/sso_substr.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/sview_split.c (renamed from misc/examples/sview_split.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/utf8replace_c.c (renamed from misc/examples/utf8replace_c.c) | 0 | ||||
| -rw-r--r-- | misc/examples/strings/utf8replace_rs.rs (renamed from misc/examples/utf8replace_rs.rs) | 0 | ||||
| -rw-r--r-- | misc/examples/vectors/lower_bound.c (renamed from misc/examples/lower_bound.c) | 2 | ||||
| -rw-r--r-- | misc/examples/vectors/new_vec.c (renamed from misc/examples/new_vec.c) | 0 | ||||
| -rw-r--r-- | misc/examples/vectors/stack.c (renamed from misc/examples/stack.c) | 0 |
82 files changed, 562 insertions, 284 deletions
diff --git a/misc/examples/forfilter.c b/misc/examples/algorithms/forfilter.c index f3c008b3..c1426045 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/algorithms/forfilter.c @@ -3,8 +3,7 @@ #include <stc/cstr.h> #define i_implement #include <stc/csview.h> -#include <stc/algo/filter.h> -#include <stc/algo/crange.h> +#include <stc/algorithm.h> #define i_type IVec #define i_key int @@ -55,7 +54,7 @@ fn main() { void demo2(void) { IVec vector = {0}; - crange r = crange_make(INT64_MAX); + crange r = crange_init(INT64_MAX); c_forfilter (x, crange, r, c_flt_skipwhile(x, *x.ref != 11) && (*x.ref % 2) != 0 && @@ -125,7 +124,7 @@ void demo5(void) { #define flt_even(i) ((*i.ref & 1) == 0) #define flt_mid_decade(i) ((*i.ref % 10) != 0) - crange R = crange_make(1963, INT32_MAX); + crange R = crange_init(1963, INT32_MAX); c_forfilter (i, crange, R, c_flt_skip(i,15) && diff --git a/misc/examples/forloops.c b/misc/examples/algorithms/forloops.c index 47cced8f..a83d4a53 100644 --- a/misc/examples/forloops.c +++ b/misc/examples/algorithms/forloops.c @@ -1,69 +1,65 @@ -#include <stdio.h>
-#include <stc/algo/filter.h>
-
-#define i_type IVec
-#define i_key int
-#include <stc/cstack.h>
-
-#define i_type IMap
-#define i_key int
-#define i_val int
-#include <stc/cmap.h>
-
-
-int main(void)
-{
- puts("c_forrange:");
- c_forrange (30) printf(" xx");
- puts("");
-
- c_forrange (i, 30) printf(" %lld", i);
- puts("");
-
- c_forrange (i, 30, 60) printf(" %lld", i);
- puts("");
-
- c_forrange (i, 30, 90, 2) printf(" %lld", i);
-
- puts("\n\nc_forlist:");
- c_forlist (i, int, {12, 23, 453, 65, 676})
- printf(" %d", *i.ref);
- puts("");
-
- c_forlist (i, const char*, {"12", "23", "453", "65", "676"})
- printf(" %s", *i.ref);
- puts("");
-
- IVec vec = c_init(IVec, {12, 23, 453, 65, 113, 215, 676, 34, 67, 20, 27, 66, 189, 45, 280, 199});
- IMap map = c_init(IMap, {{12, 23}, {453, 65}, {676, 123}, {34, 67}});
-
- puts("\n\nc_foreach:");
- c_foreach (i, IVec, vec)
- printf(" %d", *i.ref);
-
- puts("\n\nc_foreach_r: reverse");
- c_foreach_rv (i, IVec, vec)
- printf(" %d", *i.ref);
-
- puts("\n\nc_foreach in map:");
- c_foreach (i, IMap, map)
- printf(" (%d %d)", i.ref->first, i.ref->second);
-
- puts("\n\nc_forpair:");
- c_forpair (key, val, IMap, map)
- printf(" (%d %d)", *_.key, *_.val);
-
- #define isOdd(i) (*i.ref & 1)
-
- puts("\n\nc_forfilter:");
- c_forfilter (i, IVec, vec,
- isOdd(i) &&
- c_flt_skip(i, 4) &&
- c_flt_take(i, 4)
- ){
- printf(" %d", *i.ref);
- }
-
- IVec_drop(&vec);
- IMap_drop(&map);
-}
+#include <stdio.h> +#include <stc/algorithm.h> + +#define i_type IVec +#define i_key int +#include <stc/cstack.h> + +#define i_type IMap +#define i_key int +#define i_val int +#include <stc/cmap.h> + + +int main(void) +{ + puts("c_forrange:"); + c_forrange (30) printf(" xx"); + puts(""); + + c_forrange (i, 30) printf(" %lld", i); + puts(""); + + c_forrange (i, 30, 60) printf(" %lld", i); + puts(""); + + c_forrange (i, 30, 90, 2) printf(" %lld", i); + + puts("\n\nc_forlist:"); + c_forlist (i, int, {12, 23, 453, 65, 676}) + printf(" %d", *i.ref); + puts(""); + + c_forlist (i, const char*, {"12", "23", "453", "65", "676"}) + printf(" %s", *i.ref); + puts(""); + + IVec vec = c_init(IVec, {12, 23, 453, 65, 113, 215, 676, 34, 67, 20, 27, 66, 189, 45, 280, 199}); + IMap map = c_init(IMap, {{12, 23}, {453, 65}, {676, 123}, {34, 67}}); + + puts("\n\nc_foreach:"); + c_foreach (i, IVec, vec) + printf(" %d", *i.ref); + + puts("\n\nc_foreach in map:"); + c_foreach (i, IMap, map) + printf(" (%d %d)", i.ref->first, i.ref->second); + + puts("\n\nc_forpair:"); + c_forpair (key, val, IMap, map) + printf(" (%d %d)", *_.key, *_.val); + + #define isOdd(i) (*i.ref & 1) + + puts("\n\nc_forfilter:"); + c_forfilter (i, IVec, vec, + isOdd(i) && + c_flt_skip(i, 4) && + c_flt_take(i, 4) + ){ + printf(" %d", *i.ref); + } + + IVec_drop(&vec); + IMap_drop(&map); +} diff --git a/misc/examples/random.c b/misc/examples/algorithms/random.c index a36b2389..9522c16d 100644 --- a/misc/examples/random.c +++ b/misc/examples/algorithms/random.c @@ -4,11 +4,11 @@ int main(void) { - const int N = 10000000; + const long long N = 10000000; const uint64_t seed = (uint64_t)time(NULL), range = 1000000; crand_t rng = crand_init(seed); - int64_t sum; + long long sum; clock_t diff, before; printf("Compare speed of full and unbiased ranged random numbers...\n"); @@ -18,19 +18,19 @@ int main(void) sum += (uint32_t)crand_u64(&rng); } diff = clock() - before; - printf("full range\t\t: %f secs, %d, avg: %f\n", - (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + printf("full range\t\t: %f secs, %lld, avg: %f\n", + (double)diff/(double)CLOCKS_PER_SEC, N, (double)(sum/N)); - crand_unif_t dist1 = crand_unif_init(0, range); + crand_uniform_t dist1 = crand_uniform_init(0, range); rng = crand_init(seed); sum = 0; before = clock(); c_forrange (N) { - sum += crand_unif(&rng, &dist1); // unbiased + sum += crand_uniform(&rng, &dist1); // unbiased } diff = clock() - before; - printf("unbiased 0-%" PRIu64 "\t: %f secs, %d, avg: %f\n", - range, (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + printf("unbiased 0-%lld\t: %f secs, %lld, avg: %f\n", + (long long)range, (double)diff/CLOCKS_PER_SEC, N, (double)(sum/N)); sum = 0; rng = crand_init(seed); @@ -39,7 +39,7 @@ int main(void) sum += (int64_t)(crand_u64(&rng) % (range + 1)); // biased } diff = clock() - before; - printf("biased 0-%" PRIu64 " \t: %f secs, %d, avg: %f\n", - range, (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + printf("biased 0-%" PRIu64 " \t: %f secs, %lld, avg: %f\n", + (long long)range, (double)diff/CLOCKS_PER_SEC, N, (double)(sum/N)); } diff --git a/misc/examples/shape.c b/misc/examples/algorithms/shape.c index bd4bdd5a..bd4bdd5a 100644 --- a/misc/examples/shape.c +++ b/misc/examples/algorithms/shape.c diff --git a/misc/examples/shape.cpp b/misc/examples/algorithms/shape.cpp index ea1f53d2..ea1f53d2 100644 --- a/misc/examples/shape.cpp +++ b/misc/examples/algorithms/shape.cpp diff --git a/misc/examples/bits.c b/misc/examples/bitsets/bits.c index e0a11346..e0a11346 100644 --- a/misc/examples/bits.c +++ b/misc/examples/bitsets/bits.c diff --git a/misc/examples/bits2.c b/misc/examples/bitsets/bits2.c index de2f16f4..de2f16f4 100644 --- a/misc/examples/bits2.c +++ b/misc/examples/bitsets/bits2.c diff --git a/misc/examples/prime.c b/misc/examples/bitsets/prime.c index e7b3d993..7e5a2b3f 100644 --- a/misc/examples/prime.c +++ b/misc/examples/bitsets/prime.c @@ -2,12 +2,10 @@ #include <math.h> #include <time.h> #include <stc/cbits.h> -#include <stc/algo/filter.h> -#include <stc/algo/crange.h> +#include <stc/algorithm.h> typedef long long llong; - cbits sieveOfEratosthenes(llong n) { cbits bits = cbits_with_size(n/2 + 1, true); @@ -28,17 +26,16 @@ cbits sieveOfEratosthenes(llong n) int main(void) { - llong n = 100000000; printf("Computing prime numbers up to %lld\n", n); - clock_t t1 = clock(); + clock_t t = clock(); cbits primes = sieveOfEratosthenes(n + 1); llong np = cbits_count(&primes); - clock_t t2 = clock(); + t = clock() - t; - printf("Number of primes: %lld, time: %f\n\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC); + printf("Number of primes: %lld, time: %f\n\n", np, (double)t/CLOCKS_PER_SEC); puts("Show all the primes in the range [2, 1000):"); printf("2"); @@ -47,7 +44,7 @@ int main(void) puts("\n"); puts("Show the last 50 primes using a temporary crange generator:"); - crange range = crange_make(n - 1, 0, -2); + crange range = crange_init(n - 1, 0, -2); c_forfilter (i, crange, range, cbits_test(&primes, *i.ref/2) && diff --git a/misc/examples/cointerleave.c b/misc/examples/coroutines/cointerleave.c index 599ceaab..f3710ba3 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/coroutines/cointerleave.c @@ -1,6 +1,6 @@ // https://www.youtube.com/watch?v=8sEe-4tig_A #include <stdio.h> -#include <stc/calgo.h> +#include <stc/coroutine.h> #define i_type IVec #define i_key int #include <stc/cvec.h> @@ -49,7 +49,7 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - cco_block_on(interleaved(&g)) { + cco_call_blocking(interleaved(&g)) { printf("%d ", g.value); } puts(""); diff --git a/misc/examples/coread.c b/misc/examples/coroutines/coread.c index a13f6be5..ebaaf19d 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coroutines/coread.c @@ -1,6 +1,6 @@ #define i_implement #include <stc/cstr.h> -#include <stc/algo/coroutine.h> +#include <stc/coroutine.h> #include <errno.h> // Read file line by line using coroutines: @@ -33,7 +33,7 @@ int main(void) { struct file_read g = {__FILE__}; int n = 0; - cco_block_on(file_read(&g)) + cco_call_blocking(file_read(&g)) { printf("%3d %s\n", ++n, cstr_str(&g.line)); //if (n == 10) cco_stop(&g); diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines/coroutines.c index b8dfaa13..489c3ed6 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines/coroutines.c @@ -1,4 +1,4 @@ -#include <stc/algo/coroutine.h> +#include <stc/coroutine.h> #include <stdio.h> #include <stdint.h> @@ -84,13 +84,13 @@ struct combined { int combined(struct combined* g) { cco_routine(g) { - cco_await_on(prime(&g->prm)); - cco_await_on(fibonacci(&g->fib)); + cco_call_await(prime(&g->prm)); + cco_call_await(fibonacci(&g->fib)); // Reuse the g->prm context and extend the count: g->prm.count = 8, g->prm.result += 2; cco_reset(&g->prm); - cco_await_on(prime(&g->prm)); + cco_call_await(prime(&g->prm)); cco_cleanup: puts("final combined"); @@ -103,7 +103,7 @@ int main(void) struct combined c = {.prm={.count=8}, .fib={14}}; int res; - cco_block_on(combined(&c), &res) { + cco_call_blocking(combined(&c), &res) { if (res == CCO_YIELD) printf("Prime(%d)=%lld, Fib(%d)=%lld\n", c.prm.idx, c.prm.result, diff --git a/misc/examples/coroutines/cotasks1.c b/misc/examples/coroutines/cotasks1.c new file mode 100644 index 00000000..e4afbe2b --- /dev/null +++ b/misc/examples/coroutines/cotasks1.c @@ -0,0 +1,100 @@ +// https://mariusbancila.ro/blog/2020/06/22/a-cpp20-coroutine-example/ + +#include <time.h> +#include <stdio.h> +#define i_static +#include <stc/cstr.h> +#include <stc/coroutine.h> + +struct next_value { + int val; + int cco_state; + cco_timer tm; +}; + +int next_value(struct next_value* co) +{ + cco_routine (co) { + while (true) { + cco_timer_await(&co->tm, 1 + rand() % 2); + co->val = rand(); + cco_yield(); + } + } + return 0; +} + +void print_time() +{ + time_t now = time(NULL); + char mbstr[64]; + strftime(mbstr, sizeof(mbstr), "[%H:%M:%S]", localtime(&now)); + printf("%s ", mbstr); +} + +// PRODUCER + +struct produce_items { + struct next_value next; + cstr str; + int cco_state; +}; + +int produce_items(struct produce_items* p) +{ + cco_routine (p) { + p->str = cstr_null; + while (true) + { + cco_await(next_value(&p->next) != CCO_AWAIT); + 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"); + } + return 0; +} + +// CONSUMER + +struct consume_items { + int n, i; + int cco_state; +}; + +int consume_items(struct consume_items* c, struct produce_items* p) +{ + cco_routine (c) { + for (c->i = 1; c->i <= c->n; ++c->i) + { + printf("consume #%d\n", c->i); + cco_await(produce_items(p) != CCO_AWAIT); + print_time(); + printf("consumed %s\n", cstr_str(&p->str)); + } + cco_cleanup: + puts("done consume"); + } + return 0; +} + +int main(void) +{ + struct produce_items produce = {0}; + struct consume_items consume = {.n=5}; + int count = 0; + + cco_call_blocking(consume_items(&consume, &produce)) + { + ++count; + //cco_sleep(0.001); + //if (consume.i == 3) cco_stop(&consume); + } + cco_stop(&produce); + produce_items(&produce); + printf("count: %d\n", count); +}
\ No newline at end of file diff --git a/misc/examples/coroutines/cotasks2.c b/misc/examples/coroutines/cotasks2.c new file mode 100644 index 00000000..4fdf98d9 --- /dev/null +++ b/misc/examples/coroutines/cotasks2.c @@ -0,0 +1,98 @@ +// https://mariusbancila.ro/blog/2020/06/22/a-cpp20-coroutine-example/ + +#include <time.h> +#include <stdio.h> +#define i_static +#include <stc/cstr.h> +#include <stc/coroutine.h> + +cco_task_struct (next_value, + int val; + cco_timer tm; +); + +int next_value(struct next_value* co, cco_runtime* rt) +{ + cco_routine (co) { + while (true) { + cco_timer_await(&co->tm, 1 + rand() % 2); + co->val = rand(); + cco_yield(); + } + } + return 0; +} + +void print_time() +{ + time_t now = time(NULL); + char mbstr[64]; + strftime(mbstr, sizeof(mbstr), "[%H:%M:%S]", localtime(&now)); + printf("%s ", mbstr); +} + +// PRODUCER + +cco_task_struct (produce_items, + struct next_value next; + cstr str; +); + +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 (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"); + } + return 0; +} + +// CONSUMER + +cco_task_struct (consume_items, + int n, i; + struct produce_items produce; +); + +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); + cco_task_await(&c->produce, rt, CCO_YIELD); + 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"); + } + return 0; +} + +int main(void) +{ + struct consume_items consume = { + .cco_func = consume_items, + .n = 5, + }; + cco_task_blocking(&consume); +} diff --git a/misc/examples/dining_philosophers.c b/misc/examples/coroutines/dining_philosophers.c index a5063a42..abe09204 100644 --- a/misc/examples/dining_philosophers.c +++ b/misc/examples/coroutines/dining_philosophers.c @@ -2,7 +2,7 @@ #include <stdio.h> #include <time.h> #include <stc/crand.h> -#include <stc/algo/coroutine.h> +#include <stc/coroutine.h> // Define the number of philosophers and forks enum { diff --git a/misc/examples/coroutines/filetask.c b/misc/examples/coroutines/filetask.c new file mode 100644 index 00000000..bfce7810 --- /dev/null +++ b/misc/examples/coroutines/filetask.c @@ -0,0 +1,80 @@ +// https://github.com/lewissbaker/cppcoro#taskt + +#include <time.h> +#include <stdio.h> +#define i_static +#include <stc/cstr.h> +#include <stc/coroutine.h> + +cco_task_struct(file_read, + const char* path; + cstr line; + FILE* fp; + cco_timer tm; +); + +int file_read(struct file_read* co, cco_runtime* rt) +{ + cco_routine (co) { + co->fp = fopen(co->path, "r"); + co->line = cstr_null; + + while (true) { + // emulate async io: await 10ms per line + cco_timer_await(&co->tm, 0.003); + + if (!cstr_getline(&co->line, co->fp)) + break; + cco_yield(); + } + + cco_cleanup: + fclose(co->fp); + cstr_drop(&co->line); + puts("done file_read"); + } + return 0; +} + +cco_task_struct(count_line, + cstr path; + struct file_read reader; + int lineCount; +); + +int count_line(struct count_line* co, cco_runtime* rt) +{ + cco_routine (co) { + co->reader.cco_func = file_read; + co->reader.path = cstr_str(&co->path); + while (true) + { + // await for next CCO_YIELD (or CCO_DONE) in file_read() + cco_task_await(&co->reader, rt, CCO_YIELD); + if (rt->result == CCO_DONE) break; + co->lineCount += 1; + cco_yield(); + } + + cco_cleanup: + cstr_drop(&co->path); + puts("done count_line"); + } + return 0; +} + +int main(void) +{ + // Creates a new task + struct count_line countTask = { + .cco_func = count_line, + .path = cstr_from(__FILE__), + }; + + // Execute coroutine as top-level blocking + int loop = 0; + cco_task_blocking(&countTask) { ++loop; } + + printf("line count = %d\n", countTask.lineCount); + printf("exec count = %d\n", loop); +} diff --git a/misc/examples/generator.c b/misc/examples/coroutines/generator.c index a15f9ba5..f9e59fea 100644 --- a/misc/examples/generator.c +++ b/misc/examples/coroutines/generator.c @@ -1,7 +1,7 @@ // https://quuxplusone.github.io/blog/2019/03/06/pythagorean-triples/ -#include <stc/algo/coroutine.h> #include <stdio.h> +#include <stc/coroutine.h> typedef struct { int size; diff --git a/misc/examples/scheduler.c b/misc/examples/coroutines/scheduler.c index 38defd0f..78461277 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/coroutines/scheduler.c @@ -1,6 +1,6 @@ // https://www.youtube.com/watch?v=8sEe-4tig_A #include <stdio.h> -#include <stc/calgo.h> +#include <stc/coroutine.h> struct Task { int (*fn)(struct Task*); diff --git a/misc/examples/triples.c b/misc/examples/coroutines/triples.c index 9f2fcc1e..9fd771ce 100644 --- a/misc/examples/triples.c +++ b/misc/examples/coroutines/triples.c @@ -1,24 +1,16 @@ // https://quuxplusone.github.io/blog/2019/03/06/pythagorean-triples/ -#include <stc/algo/coroutine.h> #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/birthday.c b/misc/examples/hashmaps/birthday.c index 4742cb45..4742cb45 100644 --- a/misc/examples/birthday.c +++ b/misc/examples/hashmaps/birthday.c diff --git a/misc/examples/books.c b/misc/examples/hashmaps/books.c index 1fd57f27..1fd57f27 100644 --- a/misc/examples/books.c +++ b/misc/examples/hashmaps/books.c diff --git a/misc/examples/hashmap.c b/misc/examples/hashmaps/hashmap.c index cf11b7f7..cf11b7f7 100644 --- a/misc/examples/hashmap.c +++ b/misc/examples/hashmaps/hashmap.c diff --git a/misc/examples/new_map.c b/misc/examples/hashmaps/new_map.c index de990040..de990040 100644 --- a/misc/examples/new_map.c +++ b/misc/examples/hashmaps/new_map.c diff --git a/misc/examples/phonebook.c b/misc/examples/hashmaps/phonebook.c index faf7566e..faf7566e 100644 --- a/misc/examples/phonebook.c +++ b/misc/examples/hashmaps/phonebook.c diff --git a/misc/examples/unordered_set.c b/misc/examples/hashmaps/unordered_set.c index dd899d78..dd899d78 100644 --- a/misc/examples/unordered_set.c +++ b/misc/examples/hashmaps/unordered_set.c diff --git a/misc/examples/vikings.c b/misc/examples/hashmaps/vikings.c index d6125854..d6125854 100644 --- a/misc/examples/vikings.c +++ b/misc/examples/hashmaps/vikings.c diff --git a/misc/examples/intrusive.c b/misc/examples/linkedlists/intrusive.c index 80c1f63b..c7402d09 100644 --- a/misc/examples/intrusive.c +++ b/misc/examples/linkedlists/intrusive.c @@ -4,7 +4,7 @@ #define i_type List #define i_key int -#define i_native_cmp +#define i_cmp_native #include <stc/clist.h> void printList(List list) { diff --git a/misc/examples/list.c b/misc/examples/linkedlists/list.c index fa33305a..518cc09b 100644 --- a/misc/examples/list.c +++ b/misc/examples/linkedlists/list.c @@ -1,21 +1,21 @@ #include <stdio.h> #include <time.h> -#include <stc/algo/filter.h> +#include <stc/algorithm.h> #include <stc/crand.h> #define i_type DList #define i_key double -#define i_native_cmp +#define i_cmp_native #include <stc/clist.h> int main(void) { const int n = 3000000; DList list = {0}; - crand_t rng = crand_init(1234567); + csrand(1234567); int m = 0; c_forrange (n) - DList_push_back(&list, crand_f64(&rng)*n + 100), ++m; + DList_push_back(&list, crandf()*n + 100), ++m; double sum = 0.0; printf("sumarize %d:\n", m); diff --git a/misc/examples/list_erase.c b/misc/examples/linkedlists/list_erase.c index 211c5a5d..211c5a5d 100644 --- a/misc/examples/list_erase.c +++ b/misc/examples/linkedlists/list_erase.c diff --git a/misc/examples/list_splice.c b/misc/examples/linkedlists/list_splice.c index f1fd6e1f..f1fd6e1f 100644 --- a/misc/examples/list_splice.c +++ b/misc/examples/linkedlists/list_splice.c diff --git a/misc/examples/new_list.c b/misc/examples/linkedlists/new_list.c index 9676e7b4..2112bf1f 100644 --- a/misc/examples/new_list.c +++ b/misc/examples/linkedlists/new_list.c @@ -27,7 +27,7 @@ int point_cmp(const Point* a, const Point* b) { #include <stc/clist.h> #define i_key float -#define i_native_cmp // use < and == operators for comparison +#define i_cmp_native // use < and == operators for comparison #include <stc/clist.h> void MyStruct_drop(MyStruct* s); diff --git a/misc/examples/make.sh b/misc/examples/make.sh index cf224950..b362f275 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -37,18 +37,20 @@ else fi if [ $run = 0 ] ; then - for i in *.c ; do - echo $comp -I../../include $i $clibs $oflag$(basename $i .c).exe - $comp -I../../include $i $clibs $oflag$(basename $i .c).exe + for i in */*.c ; do + out=$(basename $i .c).exe + #out=$(dirname $i)/$(basename $i .c).exe + echo $comp -I../../include $i $clibs $oflag$out + $comp -I../../include $i $clibs $oflag$out done else - for i in *.c ; do + for i in */*.c ; do echo $comp -I../../include $i $clibs $comp -I../../include $i $clibs - if [ -f $(basename -s .c $i).exe ]; then ./$(basename -s .c $i).exe; fi - if [ -f ./a.exe ]; then ./a.exe; fi - if [ -f ./a.out ]; then ./a.out; fi + out=$(basename $i .c).exe + #out=$(dirname $i)/$(basename $i .c).exe + if [ -f $out ]; then ./$out; fi done fi -rm -f a.out *.o *.obj # *.exe +#rm -f a.out *.o *.obj # *.exe diff --git a/misc/examples/astar.c b/misc/examples/mixed/astar.c index 590b7952..590b7952 100644 --- a/misc/examples/astar.c +++ b/misc/examples/mixed/astar.c diff --git a/misc/examples/complex.c b/misc/examples/mixed/complex.c index 4eb1574b..4eb1574b 100644 --- a/misc/examples/complex.c +++ b/misc/examples/mixed/complex.c diff --git a/misc/examples/convert.c b/misc/examples/mixed/convert.c index fa64560e..fa64560e 100644 --- a/misc/examples/convert.c +++ b/misc/examples/mixed/convert.c diff --git a/misc/examples/demos.c b/misc/examples/mixed/demos.c index ecc89f2e..1a604d9f 100644 --- a/misc/examples/demos.c +++ b/misc/examples/mixed/demos.c @@ -74,7 +74,7 @@ void vectordemo2(void) #define i_key int #define i_tag ix -#define i_native_cmp +#define i_cmp_native #include <stc/clist.h> void listdemo1(void) diff --git a/misc/examples/inits.c b/misc/examples/mixed/inits.c index 53a49f1f..53a49f1f 100644 --- a/misc/examples/inits.c +++ b/misc/examples/mixed/inits.c diff --git a/misc/examples/read.c b/misc/examples/mixed/read.c index b12f7409..de04fd31 100644 --- a/misc/examples/read.c +++ b/misc/examples/mixed/read.c @@ -7,9 +7,9 @@ cvec_str read_file(const char* name) { - cvec_str vec = cvec_str_init(); + cvec_str vec = {0}; c_with (FILE* f = fopen(name, "r"), fclose(f)) - c_with (cstr line = cstr_null, cstr_drop(&line)) + c_with (cstr line = {0}, cstr_drop(&line)) while (cstr_getline(&line, f)) cvec_str_push(&vec, cstr_clone(line)); return vec; diff --git a/misc/examples/functor.c b/misc/examples/priorityqueues/functor.c index e3bde1dd..e3bde1dd 100644 --- a/misc/examples/functor.c +++ b/misc/examples/priorityqueues/functor.c diff --git a/misc/examples/new_pque.c b/misc/examples/priorityqueues/new_pque.c index 16823bb6..16823bb6 100644 --- a/misc/examples/new_pque.c +++ b/misc/examples/priorityqueues/new_pque.c diff --git a/misc/examples/priority.c b/misc/examples/priorityqueues/priority.c index bf2e188a..18684e73 100644 --- a/misc/examples/priority.c +++ b/misc/examples/priorityqueues/priority.c @@ -11,21 +11,21 @@ int main(void) { intptr_t N = 10000000; crand_t rng = crand_init((uint64_t)time(NULL)); - crand_unif_t dist = crand_unif_init(0, N * 10); + crand_uniform_t dist = crand_uniform_init(0, N * 10); cpque_i heap = {0}; // Push ten million random numbers to priority queue printf("Push %" c_ZI " numbers\n", N); c_forrange (N) - cpque_i_push(&heap, crand_unif(&rng, &dist)); + cpque_i_push(&heap, crand_uniform(&rng, &dist)); // push some negative numbers too. c_forlist (i, int, {-231, -32, -873, -4, -343}) cpque_i_push(&heap, *i.ref); c_forrange (N) - cpque_i_push(&heap, crand_unif(&rng, &dist)); + cpque_i_push(&heap, crand_uniform(&rng, &dist)); puts("Extract the hundred smallest."); c_forrange (100) { diff --git a/misc/examples/new_queue.c b/misc/examples/queues/new_queue.c index f3592df6..3904c50c 100644 --- a/misc/examples/new_queue.c +++ b/misc/examples/queues/new_queue.c @@ -23,19 +23,19 @@ int point_cmp(const Point* a, const Point* b) { int main(void) { int n = 50000000; crand_t rng = crand_init((uint64_t)time(NULL)); - crand_unif_t dist = crand_unif_init(0, n); + crand_uniform_t dist = crand_uniform_init(0, n); IQ Q = {0}; // Push 50'000'000 random numbers onto the queue. c_forrange (n) - IQ_push(&Q, (int)crand_unif(&rng, &dist)); + IQ_push(&Q, (int)crand_uniform(&rng, &dist)); // Push or pop on the queue 50 million times printf("befor: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q)); c_forrange (n) { - int r = (int)crand_unif(&rng, &dist); + int r = (int)crand_uniform(&rng, &dist); if (r & 3) IQ_push(&Q, r); else diff --git a/misc/examples/queue.c b/misc/examples/queues/queue.c index 867a6c7b..5b1f7606 100644 --- a/misc/examples/queue.c +++ b/misc/examples/queues/queue.c @@ -5,22 +5,22 @@ #define i_tag i #include <stc/cqueue.h> -int main() { +int main(void) { int n = 1000000; - crand_unif_t dist; + crand_uniform_t dist; crand_t rng = crand_init(1234); - dist = crand_unif_init(0, n); + dist = crand_uniform_init(0, n); cqueue_i queue = {0}; // Push ten million random numbers onto the queue. c_forrange (n) - cqueue_i_push(&queue, (int)crand_unif(&rng, &dist)); + cqueue_i_push(&queue, (int)crand_uniform(&rng, &dist)); // Push or pop on the queue ten million times printf("%d\n", n); c_forrange (n) { // forrange uses initial n only. - int r = (int)crand_unif(&rng, &dist); + int r = (int)crand_uniform(&rng, &dist); if (r & 1) ++n, cqueue_i_push(&queue, r); else diff --git a/misc/examples/rawptr_elements.c b/misc/examples/rawptr_elements.c deleted file mode 100644 index 694ce12e..00000000 --- a/misc/examples/rawptr_elements.c +++ /dev/null @@ -1,59 +0,0 @@ -#include <stc/ccommon.h> -#include <stdio.h> -#define i_implement -#include <stc/cstr.h> - -// Create cmap of cstr => long* -#define i_type SIPtrMap -#define i_key_str -#define i_val long* -#define i_valraw long -#define i_valfrom(raw) c_new(long, raw) -#define i_valto(x) **x -#define i_valclone(x) c_new(long, *x) -#define i_valdrop(x) c_free(*x) -#include <stc/cmap.h> - -// Alternatively, using cbox: -#define i_type IBox -#define i_key long -#include <stc/cbox.h> // unique_ptr<long> alike. - -// cmap of cstr => IBox -#define i_type SIBoxMap -#define i_key_str -#define i_valboxed IBox // i_valboxed: use properties from IBox automatically -#include <stc/cmap.h> - -int main(void) -{ - // These have the same behaviour, except IBox has a get member: - SIPtrMap map1 = {0}; - SIBoxMap map2 = {0}; - - printf("\nMap cstr => long*:\n"); - SIPtrMap_insert(&map1, cstr_from("Test1"), c_new(long, 1)); - SIPtrMap_insert(&map1, cstr_from("Test2"), c_new(long, 2)); - - // Emplace implicitly creates cstr from const char* and an owned long* from long! - SIPtrMap_emplace(&map1, "Test3", 3); - SIPtrMap_emplace(&map1, "Test4", 4); - - c_forpair (name, number, SIPtrMap, map1) - printf("%s: %ld\n", cstr_str(_.name), **_.number); - - puts("\nMap cstr => IBox:"); - SIBoxMap_insert(&map2, cstr_from("Test1"), IBox_make(1)); - SIBoxMap_insert(&map2, cstr_from("Test2"), IBox_make(2)); - - // Emplace implicitly creates cstr from const char* and IBox from long! - SIBoxMap_emplace(&map2, "Test3", 3); - SIBoxMap_emplace(&map2, "Test4", 4); - - c_forpair (name, number, SIBoxMap, map2) - printf("%s: %ld\n", cstr_str(_.name), *_.number->get); - puts(""); - - SIPtrMap_drop(&map1); - SIBoxMap_drop(&map2); -} diff --git a/misc/examples/regex1.c b/misc/examples/regularexpressions/regex1.c index d8032358..d8032358 100644 --- a/misc/examples/regex1.c +++ b/misc/examples/regularexpressions/regex1.c diff --git a/misc/examples/regex2.c b/misc/examples/regularexpressions/regex2.c index a798b1a1..a798b1a1 100644 --- a/misc/examples/regex2.c +++ b/misc/examples/regularexpressions/regex2.c diff --git a/misc/examples/regex_match.c b/misc/examples/regularexpressions/regex_match.c index 11426d2d..11426d2d 100644 --- a/misc/examples/regex_match.c +++ b/misc/examples/regularexpressions/regex_match.c diff --git a/misc/examples/regex_replace.c b/misc/examples/regularexpressions/regex_replace.c index f1ea2711..f1ea2711 100644 --- a/misc/examples/regex_replace.c +++ b/misc/examples/regularexpressions/regex_replace.c diff --git a/misc/examples/sidebyside.cpp b/misc/examples/sidebyside.cpp deleted file mode 100644 index 9414b691..00000000 --- a/misc/examples/sidebyside.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include <iostream> -#include <map> -#include <string> -#include <stc/cstr.h> - -#define i_type IIMap -#define i_key int -#define i_val int -#include <stc/csmap.h> - -#define i_type SIMap -#define i_key_str -#define i_val int -#include <stc/cmap.h> - -int main(void) { - { - std::map<int, int> hist; - hist.emplace(12, 100).first->second += 1; - hist.emplace(13, 100).first->second += 1; - hist.emplace(12, 100).first->second += 1; - - for (auto i: hist) - std::cout << i.first << ", " << i.second << std::endl; - std::cout << std::endl; - } - { - IIMap hist = {0}; - IIMap_insert(&hist, 12, 100).ref->second += 1; - IIMap_insert(&hist, 13, 100).ref->second += 1; - IIMap_insert(&hist, 12, 100).ref->second += 1; - - c_foreach (i, IIMap, hist) - printf("%d, %d\n", i.ref->first, i.ref->second); - puts(""); - IIMap_drop(&hist); - } - // =================================================== - { - std::map<std::string, int> food = - {{"burger", 5}, {"pizza", 12}, {"steak", 15}}; - - for (auto i: food) - std::cout << i.first << ", " << i.second << std::endl; - std::cout << std::endl; - } - { - SIMap food = {0}; - c_forlist (i, SIMap_raw, {{"burger", 5}, {"pizza", 12}, {"steak", 15}}) - SIMap_emplace(&food, i.ref->first, i.ref->second); - - c_foreach (i, SIMap, food) - printf("%s, %d\n", cstr_str(&i.ref->first), i.ref->second); - puts(""); - SIMap_drop(&food); - } -} diff --git a/misc/examples/arc_containers.c b/misc/examples/smartpointers/arc_containers.c index 2fb04c56..2fb04c56 100644 --- a/misc/examples/arc_containers.c +++ b/misc/examples/smartpointers/arc_containers.c diff --git a/misc/examples/arc_demo.c b/misc/examples/smartpointers/arc_demo.c index 87d64e67..929a48a1 100644 --- a/misc/examples/arc_demo.c +++ b/misc/examples/smartpointers/arc_demo.c @@ -11,7 +11,7 @@ void int_drop(int* x) { #define i_type Arc // set type name to be defined (instead of 'carc_int') #define i_key int #define i_keydrop int_drop // optional, just to display the elements destroyed -#define i_native_cmp // use int comparison (x < y, x == y). +#define i_cmp_native // use int comparison (x < y, x == y). #include <stc/carc.h> // Arc #define i_keyboxed Arc // note: use i_keyboxed instead of i_key for carc/cbox elements diff --git a/misc/examples/arcvec_erase.c b/misc/examples/smartpointers/arcvec_erase.c index addef8b7..ba54c1c7 100644 --- a/misc/examples/arcvec_erase.c +++ b/misc/examples/smartpointers/arcvec_erase.c @@ -5,7 +5,7 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } #define i_type Arc #define i_key int #define i_keydrop show_drop -#define i_native_cmp // enable sort/search for int type +#define i_cmp_native // enable sort/search for int type #include <stc/carc.h> // Shared pointer to int #define i_type Vec diff --git a/misc/examples/box.c b/misc/examples/smartpointers/box.c index 94d126c0..94d126c0 100644 --- a/misc/examples/box.c +++ b/misc/examples/smartpointers/box.c diff --git a/misc/examples/box2.c b/misc/examples/smartpointers/box2.c index eaab1c47..eaab1c47 100644 --- a/misc/examples/box2.c +++ b/misc/examples/smartpointers/box2.c diff --git a/misc/examples/smartpointers/map_box.c b/misc/examples/smartpointers/map_box.c new file mode 100644 index 00000000..f651b302 --- /dev/null +++ b/misc/examples/smartpointers/map_box.c @@ -0,0 +1,34 @@ +#include <stc/ccommon.h> +#include <stdio.h> +#define i_implement +#include <stc/cstr.h> + +#define i_type IBox +#define i_key long +#include <stc/cbox.h> // unique_ptr<long> alike. + +// cmap of cstr => IBox +#define i_type Boxmap +#define i_key_str +#define i_valboxed IBox // i_valboxed: use properties from IBox automatically +#include <stc/cmap.h> + + +int main(void) +{ + Boxmap map = {0}; + + puts("Map cstr => IBox:"); + Boxmap_insert(&map, cstr_from("Test1"), IBox_make(1)); + Boxmap_insert(&map, cstr_from("Test2"), IBox_make(2)); + + // Simpler: emplace() implicitly creates cstr from const char* and IBox from long! + Boxmap_emplace(&map, "Test3", 3); + Boxmap_emplace(&map, "Test4", 4); + + c_forpair (name, number, Boxmap, map) + printf("%s: %ld\n", cstr_str(_.name), *_.number->get); + puts(""); + + Boxmap_drop(&map); +} diff --git a/misc/examples/smartpointers/map_ptr.c b/misc/examples/smartpointers/map_ptr.c new file mode 100644 index 00000000..453322c5 --- /dev/null +++ b/misc/examples/smartpointers/map_ptr.c @@ -0,0 +1,34 @@ +#include <stc/ccommon.h> +#include <stdio.h> +#define i_implement +#include <stc/cstr.h> + +// cmap of cstr => long* +#define i_type Ptrmap +#define i_key_str +#define i_val long* +#define i_valraw long +#define i_valfrom(raw) c_new(long, raw) +#define i_valto(x) **x +#define i_valclone(x) c_new(long, *x) +#define i_valdrop(x) c_free(*x) +#include <stc/cmap.h> + +int main(void) +{ + Ptrmap map = {0}; + + puts("Map cstr => long*:"); + Ptrmap_insert(&map, cstr_from("Test1"), c_new(long, 1)); + Ptrmap_insert(&map, cstr_from("Test2"), c_new(long, 2)); + + // Simple: emplace() implicitly creates cstr from const char* and an owned long* from long! + Ptrmap_emplace(&map, "Test3", 3); + Ptrmap_emplace(&map, "Test4", 4); + + c_forpair (name, number, Ptrmap, map) + printf("%s: %ld\n", cstr_str(_.name), **_.number); + puts(""); + + Ptrmap_drop(&map); +} diff --git a/misc/examples/music_arc.c b/misc/examples/smartpointers/music_arc.c index 16111b0b..13d368c3 100644 --- a/misc/examples/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}) { diff --git a/misc/examples/new_sptr.c b/misc/examples/smartpointers/new_sptr.c index 7fef5d1f..3c6fa16c 100644 --- a/misc/examples/new_sptr.c +++ b/misc/examples/smartpointers/new_sptr.c @@ -15,7 +15,7 @@ uint64_t Person_hash(const Person* p); #define i_type IPtr #define i_key int #define i_keydrop(x) printf("drop: %d\n", *x) -#define i_native_cmp +#define i_cmp_native #include <stc/carc.h> #define i_type IPStack diff --git a/misc/examples/person_arc.c b/misc/examples/smartpointers/person_arc.c index 38c883a7..38c883a7 100644 --- a/misc/examples/person_arc.c +++ b/misc/examples/smartpointers/person_arc.c diff --git a/misc/examples/csmap_erase.c b/misc/examples/sortedmaps/csmap_erase.c index 8d4eeae3..8d4eeae3 100644 --- a/misc/examples/csmap_erase.c +++ b/misc/examples/sortedmaps/csmap_erase.c diff --git a/misc/examples/csmap_find.c b/misc/examples/sortedmaps/csmap_find.c index c392338d..c392338d 100644 --- a/misc/examples/csmap_find.c +++ b/misc/examples/sortedmaps/csmap_find.c diff --git a/misc/examples/csmap_insert.c b/misc/examples/sortedmaps/csmap_insert.c index c9f02891..c9f02891 100644 --- a/misc/examples/csmap_insert.c +++ b/misc/examples/sortedmaps/csmap_insert.c diff --git a/misc/examples/csset_erase.c b/misc/examples/sortedmaps/csset_erase.c index 9c7f5e1a..9c7f5e1a 100644 --- a/misc/examples/csset_erase.c +++ b/misc/examples/sortedmaps/csset_erase.c diff --git a/misc/examples/gauss2.c b/misc/examples/sortedmaps/gauss2.c index 1ab8ade5..02ce4bc5 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/sortedmaps/gauss2.c @@ -21,14 +21,14 @@ int main(void) printf("Mean %f, StdDev %f\n", Mean, StdDev); // Setup random engine with normal distribution. - crand_norm_t dist = crand_norm_init(Mean, StdDev); + crand_normal_t dist = crand_normal_init(Mean, StdDev); // Create and init histogram map with defered destruct csmap_int hist = {0}; cstr bar = {0}; c_forrange (N) { - int index = (int)round(crand_norm(&rng, &dist)); + int index = (int)round(crand_normal(&rng, &dist)); csmap_int_insert(&hist, index, 0).ref->second += 1; } diff --git a/misc/examples/mmap.c b/misc/examples/sortedmaps/listmap.c index 04a605a7..04a605a7 100644 --- a/misc/examples/mmap.c +++ b/misc/examples/sortedmaps/listmap.c diff --git a/misc/examples/mapmap.c b/misc/examples/sortedmaps/mapmap.c index d3065659..d3065659 100644 --- a/misc/examples/mapmap.c +++ b/misc/examples/sortedmaps/mapmap.c diff --git a/misc/examples/multimap.c b/misc/examples/sortedmaps/multimap.c index 1068a5dc..1068a5dc 100644 --- a/misc/examples/multimap.c +++ b/misc/examples/sortedmaps/multimap.c diff --git a/misc/examples/new_smap.c b/misc/examples/sortedmaps/new_smap.c index ee946c9a..ee946c9a 100644 --- a/misc/examples/new_smap.c +++ b/misc/examples/sortedmaps/new_smap.c diff --git a/misc/examples/sorted_map.c b/misc/examples/sortedmaps/sorted_map.c index 89381554..89381554 100644 --- a/misc/examples/sorted_map.c +++ b/misc/examples/sortedmaps/sorted_map.c diff --git a/misc/examples/spans/mdspan.c b/misc/examples/spans/mdspan.c new file mode 100644 index 00000000..db601850 --- /dev/null +++ b/misc/examples/spans/mdspan.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <stc/cspan.h> +#include <stdlib.h> + +using_cspan3(DSpan, double); + +int main(void) { + const int nx=5, ny=4, nz=3; + double* data = c_new_n(double, nx*ny*nz); + + printf("\nMultidim span ms[5, 4, 3], fortran ordered"); + DSpan3 ms = cspan_md_order('F', data, nx, ny, nz); // Fortran, not 'C' + + int idx = 0; + for (int i = 0; i < ms.shape[0]; ++i) + for (int j = 0; j < ms.shape[1]; ++j) + for (int k = 0; k < ms.shape[2]; ++k) + *cspan_at(&ms, i, j, k) = ++idx; + + cspan_transpose(&ms); + + printf(", transposed:\n\n"); + for (int i = 0; i < ms.shape[0]; ++i) { + for (int j = 0; j < ms.shape[1]; ++j) { + for (int k = 0; k < ms.shape[2]; ++k) + printf(" %3g", *cspan_at(&ms, i, j, k)); + puts(""); + } + puts(""); + } + + DSpan2 sub; + + puts("Slicing:"); + printf("\nms[0, :, :] "); + sub = cspan_slice(DSpan2, &ms, {0}, {c_ALL}, {c_ALL}); + c_foreach (i, DSpan2, sub) printf(" %g", *i.ref); + puts(""); + + printf("\nms[:, 0, :] "); + sub = cspan_slice(DSpan2, &ms, {c_ALL}, {0}, {c_ALL}); + c_foreach (i, DSpan2, sub) printf(" %g", *i.ref); + puts(""); + + sub = cspan_slice(DSpan2, &ms, {c_ALL}, {c_ALL}, {0}); + printf("\nms[:, :, 0] "); + c_foreach (i, DSpan2, sub) printf(" %g", *i.ref); + puts(""); + + free(data); +} diff --git a/misc/examples/multidim.c b/misc/examples/spans/multidim.c index 798a1126..798a1126 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/spans/multidim.c diff --git a/misc/examples/printspan.c b/misc/examples/spans/printspan.c index cd3c5f4f..cd3c5f4f 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/spans/printspan.c diff --git a/misc/examples/cstr_match.c b/misc/examples/strings/cstr_match.c index be03e981..be03e981 100644 --- a/misc/examples/cstr_match.c +++ b/misc/examples/strings/cstr_match.c diff --git a/misc/examples/replace.c b/misc/examples/strings/replace.c index 59a56bf7..59a56bf7 100644 --- a/misc/examples/replace.c +++ b/misc/examples/strings/replace.c diff --git a/misc/examples/splitstr.c b/misc/examples/strings/splitstr.c index ef7ed174..ef7ed174 100644 --- a/misc/examples/splitstr.c +++ b/misc/examples/strings/splitstr.c diff --git a/misc/examples/sso_map.c b/misc/examples/strings/sso_map.c index 4f84b651..4f84b651 100644 --- a/misc/examples/sso_map.c +++ b/misc/examples/strings/sso_map.c diff --git a/misc/examples/sso_substr.c b/misc/examples/strings/sso_substr.c index 687658df..687658df 100644 --- a/misc/examples/sso_substr.c +++ b/misc/examples/strings/sso_substr.c diff --git a/misc/examples/sview_split.c b/misc/examples/strings/sview_split.c index ac275da0..ac275da0 100644 --- a/misc/examples/sview_split.c +++ b/misc/examples/strings/sview_split.c diff --git a/misc/examples/utf8replace_c.c b/misc/examples/strings/utf8replace_c.c index 1d54486f..1d54486f 100644 --- a/misc/examples/utf8replace_c.c +++ b/misc/examples/strings/utf8replace_c.c diff --git a/misc/examples/utf8replace_rs.rs b/misc/examples/strings/utf8replace_rs.rs index 8b163b4e..8b163b4e 100644 --- a/misc/examples/utf8replace_rs.rs +++ b/misc/examples/strings/utf8replace_rs.rs diff --git a/misc/examples/lower_bound.c b/misc/examples/vectors/lower_bound.c index e5d816e9..bea828f2 100644 --- a/misc/examples/lower_bound.c +++ b/misc/examples/vectors/lower_bound.c @@ -1,7 +1,7 @@ #include <stdio.h> #define i_key int -#define i_native_cmp +#define i_cmp_native #include <stc/cvec.h> #define i_key int diff --git a/misc/examples/new_vec.c b/misc/examples/vectors/new_vec.c index 88efd55a..88efd55a 100644 --- a/misc/examples/new_vec.c +++ b/misc/examples/vectors/new_vec.c diff --git a/misc/examples/stack.c b/misc/examples/vectors/stack.c index 6297fb6f..6297fb6f 100644 --- a/misc/examples/stack.c +++ b/misc/examples/vectors/stack.c |
