From 03587c1f8d46240c181711c9919d883562244327 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 18 Jan 2021 12:08:17 +0100 Subject: Minor ex updates. --- examples/csmap_ex.c | 28 ++++++++++++++-------------- examples/ex_gauss1.c | 5 ++--- examples/ex_gauss2.c | 2 +- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/examples/csmap_ex.c b/examples/csmap_ex.c index a8e24f98..1fd14a09 100644 --- a/examples/csmap_ex.c +++ b/examples/csmap_ex.c @@ -13,46 +13,46 @@ int main(int argc, char **argv) csmap_i map = csmap_i_init(); time_t seed = time(NULL); - size_t n = 500000; + size_t n = 1000000; uint64_t mask = (1ull << 20) - 1; csmap_i_iter_t it; stc64_srandom(seed); - for (size_t i = 0; i < n; ++i) { + c_forrange (i, n) { uint64_t val = stc64_random() & mask; csmap_i_emplace(&map, val, i); if (!csmap_i_find(&map, val, &it)) { printf("Not found: %zu, %zu: ", i, val); } } + printf("size %zu\n\n", csmap_i_size(map)); stc64_srandom(seed); - for (unsigned int i = 0; i < n - 20; ++i) { + c_forrange (n - 20) csmap_i_erase(&map, stc64_random() & mask); - } size_t val = 500000; csmap_i_emplace(&map, val, 5); int k = 0; c_foreach (i, csmap_i, map) - printf("%2d %d: %d\n", ++k, i.ref->first, i.ref->second); + printf("%2d %d: %zu\n", ++k, i.ref->first, i.ref->second); csmap_i_find(&map, val, &it); printf("\nmin/max: %d -- %d: found: %d. size: %zu\n", csmap_i_front(&map)->first, - csmap_i_back(&map)->first, - it.ref->first, - csmap_i_size(map)); + csmap_i_back(&map)->first, + it.ref->first, + csmap_i_size(map)); c_foreach (i, csmap_i, it, csmap_i_end(&map)) printf("-- %d: %zu\n", i.ref->first, i.ref->second); csmap_i_del(&map); puts(""); - c_init (csset_str, names, { - "Hello", "Try this", "Awesome", "Works well", "Greetings" - }); - c_foreach (i, csset_str, names) - printf("name: %s\n", i.ref->str); + + c_init (csset_str, shouts, {"Hello", "Try this", "Awesome", "Works well", "Greetings"}); + + c_foreach (i, csset_str, shouts) + printf("shout: %s\n", i.ref->str); - csset_str_del(&names); + csset_str_del(&shouts); } diff --git a/examples/ex_gauss1.c b/examples/ex_gauss1.c index b9228aa5..4139210c 100644 --- a/examples/ex_gauss1.c +++ b/examples/ex_gauss1.c @@ -13,7 +13,6 @@ using_cmap(i, int, size_t); static int compare(cmap_i_value_t *a, cmap_i_value_t *b) { return c_default_compare(&a->first, &b->first); } -// Vector: typetag 'e' for (map) entry using_cvec(e, cmap_i_value_t, compare); int main() @@ -30,9 +29,9 @@ int main() // Create histogram map cmap_i mhist = cmap_i_init(); - for (size_t i = 0; i < N; ++i) { + c_forrange (N) { int index = (int) round( stc64_normalf(&rng, &dist) ); - cmap_i_emplace(&mhist, index, 0).first->second += 1; + ++ cmap_i_emplace(&mhist, index, 0).first->second; } // Transfer map to vec and sort it by map keys. diff --git a/examples/ex_gauss2.c b/examples/ex_gauss2.c index 7352d6a8..cff0cef3 100644 --- a/examples/ex_gauss2.c +++ b/examples/ex_gauss2.c @@ -22,7 +22,7 @@ int main() // Create histogram map csmap_i mhist = csmap_i_init(); - for (size_t i = 0; i < N; ++i) { + c_forrange (N) { int index = (int) round( stc64_normalf(&rng, &dist) ); ++ csmap_i_emplace(&mhist, index, 0).first->second; } -- cgit v1.2.3