From 26152f06399506bf5bca823f42ed3089ce404aa8 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 8 Sep 2021 22:27:45 +0200 Subject: Updated rest of examples, except cqueue.c --- examples/csmap_find.c | 15 ++++++++++----- examples/list_erase.c | 7 ++++--- examples/mapmap.c | 40 ++++++++++++++++++++++++++-------------- examples/phonebook.c | 16 +++++++++------- examples/read.c | 4 ++-- examples/splitstr.c | 4 ++-- examples/stc_astar.c | 30 +++++++++++++++++++++--------- examples/words.c | 41 ++++++++++++++++------------------------- 8 files changed, 90 insertions(+), 67 deletions(-) diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 46ae01fa..79a0f993 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -1,12 +1,17 @@ // This implements the c++ std::map::find example at: // https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17 -#include -#include #include #include -using_csmap_strval(istr, int); -using_cvec(istr, csmap_istr_rawvalue_t, c_no_compare); +#define i_tag istr +#define i_key int +#define i_val_str +#include + +#define i_tag istr +#define i_val csmap_istr_rawvalue_t +#define i_cmp c_no_compare +#include void print_elem(csmap_istr_rawvalue_t p) { printf("(%d, %s) ", p.first, p.second); @@ -56,7 +61,7 @@ int main() puts("Inserting the following vector data into m1:"); print_collection_cvec_istr(v); - + c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second); puts("The modified map m1 is (key, value):"); diff --git a/examples/list_erase.c b/examples/list_erase.c index 523ee0b8..6d619b9d 100644 --- a/examples/list_erase.c +++ b/examples/list_erase.c @@ -1,8 +1,9 @@ // erasing from clist -#include #include -using_clist(i, int); +#define i_tag i +#define i_val int +#include int main () { @@ -11,7 +12,7 @@ int main () c_emplace(clist_i, L, {10, 20, 30, 40, 50}); // 10 20 30 40 50 clist_i_iter_t it = clist_i_begin(&L); // ^ - clist_i_next(&it); + clist_i_next(&it); it = clist_i_erase_at(&L, it); // 10 30 40 50 // ^ clist_i_iter_t end = clist_i_end(&L); // diff --git a/examples/mapmap.c b/examples/mapmap.c index e582ab64..b7dd8afb 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -1,24 +1,36 @@ #include -#include #include -// unordered_map>: +// unordered_map>: + +#define i_key_str +#define i_val_str +#include -using_cmap_str(); -using_cmap_strkey(cfg, cmap_str, cmap_str_del, c_no_clone); +#define i_tag cfg +#define i_key_str +#define i_val cmap_str +#define i_valdel cmap_str_del +#include -int main(void) { - c_forvar (cmap_cfg cfg = cmap_cfg_init(), cmap_cfg_del(&cfg)) { +int main(void) +{ + c_forauto (cmap_cfg, cfg) + { cmap_str init = cmap_str_init(); - cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "name", "Joe"); - cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("user"), init).ref->second, "groups", "proj1,proj3"); - cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj1", "Energy"); - cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj2", "Windy"); - cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj3", "Oil"); - cmap_str_emplace(&cmap_cfg_insert(&cfg, cstr_from("admin"), init).ref->second, "employees", "2302"); - - cmap_str_emplace_or_assign(&cmap_cfg_insert(&cfg, cstr_from("group"), init).ref->second, "proj2", "Wind"); // Update + cmap_cfg_insert(&cfg, cstr_from("user"), init); + cmap_cfg_insert(&cfg, cstr_from("group"), init); + cmap_cfg_insert(&cfg, cstr_from("admin"), init); + + cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "name", "Joe"); + cmap_str_emplace(cmap_cfg_at(&cfg, "user"), "groups", "proj1,proj3"); + cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj1", "Energy"); + cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj2", "Windy"); + cmap_str_emplace(cmap_cfg_at(&cfg, "group"), "proj3", "Oil"); + cmap_str_emplace(cmap_cfg_at(&cfg, "admin"), "employees", "2302"); + + cmap_str_emplace_or_assign(cmap_cfg_at(&cfg, "group"), "proj2", "Wind"); // Update c_foreach (i, cmap_cfg, cfg) c_foreach (j, cmap_str, i.ref->second) diff --git a/examples/phonebook.c b/examples/phonebook.c index a69e701b..a4e2e0f2 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -22,12 +22,14 @@ // Program to emulates the phone book. #include -#include -#include #include -using_cmap_str(); -using_cset_str(); +#define i_key_str +#define i_val_str +#include + +#define i_key_str +#include void print_phone_book(cmap_str phone_book) { @@ -39,14 +41,14 @@ int main(int argc, char **argv) { c_static_assert(3 == 3, "hello"); - c_forvar (cset_str names = cset_str_init(), cset_str_del(&names)) { + c_forauto (cset_str, names) { c_emplace (cset_str, names, {"Hello", "Cool", "True"}); c_foreach (i, cset_str, names) printf("%s ", i.ref->str); puts(""); } bool erased; - c_forvar (cmap_str phone_book = cmap_str_init(), cmap_str_del(&phone_book)) { + c_forauto (cmap_str, phone_book) { c_emplace (cmap_str, phone_book, { {"Lilia Friedman", "(892) 670-4739"}, {"Tariq Beltran", "(489) 600-7575"}, @@ -63,7 +65,7 @@ int main(int argc, char **argv) printf("\nPhone book after adding Zak Byers:\n"); print_phone_book(phone_book); - if (cmap_str_find(&phone_book, "Tariq Beltran").ref != NULL) + if (cmap_str_contains(&phone_book, "Tariq Beltran")) printf("\nTariq Beltran is in phone book\n"); erased = cmap_str_erase(&phone_book, "Tariq Beltran"); diff --git a/examples/read.c b/examples/read.c index 2324d1df..bb0e139f 100644 --- a/examples/read.c +++ b/examples/read.c @@ -1,8 +1,8 @@ #include #include -#include -using_cvec_str(); +#define i_val_str +#include cvec_str read_file(const char* name) { cvec_str vec = cvec_str_init(); diff --git a/examples/splitstr.c b/examples/splitstr.c index 54bbe92d..42dd4dbd 100644 --- a/examples/splitstr.c +++ b/examples/splitstr.c @@ -1,5 +1,4 @@ #include -#include void print_split(csview str, csview sep) { @@ -12,7 +11,8 @@ void print_split(csview str, csview sep) } } -using_cvec_str(); +#define i_val_str +#include cvec_str string_split(csview str, csview sep) { diff --git a/examples/stc_astar.c b/examples/stc_astar.c index 6122aad4..4f5075a1 100644 --- a/examples/stc_astar.c +++ b/examples/stc_astar.c @@ -5,10 +5,6 @@ // https://www.redblobgames.com/pathfinding/a-star/introduction.html #include -#include -#include -#include - #include typedef struct { @@ -66,11 +62,27 @@ typedef struct { MazePoint b; } MazeStep; -using_cdeq(pnt, MazePoint, c_no_compare); -using_cpque(pnt, cdeq_pnt, mpnt_compare_priority); -using_csmap(step, MazePoint, MazePoint, mpnt_key_compare); -using_csmap(cost, MazePoint, int, mpnt_key_compare); +#define i_tag pnt +#define i_val MazePoint +#define i_cmp c_no_compare +#include +#define i_tag pnt +#define i_val MazePoint +#define i_cmp mpnt_compare_priority +#include + +#define i_tag step +#define i_key MazePoint +#define i_val MazePoint +#define i_cmp mpnt_key_compare +#include + +#define i_tag cost +#define i_key MazePoint +#define i_val int +#define i_cmp mpnt_key_compare +#include cdeq_pnt astar(cstr maze, int width) @@ -79,7 +91,7 @@ astar(cstr maze, int width) MazePoint goal = mpnt_from(maze, "!", width); cdeq_pnt path = cdeq_pnt_init(); // returned - + cpque_pnt frontier = cpque_pnt_init(); csmap_step came_from = csmap_step_init(); csmap_cost cost_so_far = csmap_cost_init(); diff --git a/examples/words.c b/examples/words.c index afc1f989..442f04f0 100644 --- a/examples/words.c +++ b/examples/words.c @@ -1,40 +1,31 @@ #include #include -#include -#include -#include -using_cvec_str(); -using_clist_str(); -using_cmap_strkey(si, int); +#define i_val_str +#include +#define i_tag strn +#define i_key_str +#define i_val int +#include int main1() { - c_var (clist_str, lwords, { - "this", "sentence", "is", "not", "a", "sentence", - "this", "sentence", "is", "a", "hoax" - }); - c_fordefer (clist_str_del(&lwords)) + c_forauto (cvec_str, words) + c_forauto (cmap_strn, word_map) { - clist_str_push_back(&lwords, cstr_from_fmt("%.15f", sqrt(2))); - c_foreach (w, clist_str, lwords) - printf("%s\n", w.ref->str); - puts(""); - - c_var (cvec_str, words, { + c_emplace (cvec_str, words, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); - c_fordefer (cvec_str_del(&words)) - { - cmap_si word_map = cmap_si_init(); - c_foreach (w, cvec_str, words) - cmap_si_emplace(&word_map, w.ref->str, 0).ref->second += 1; - c_foreach (i, cmap_si, word_map) { - printf("%d occurrences of word '%s'\n", i.ref->second, i.ref->first.str); - } + c_foreach (w, cvec_str, words) { + cmap_strn_emplace(&word_map, w.ref->str, 0).ref->second += 1; + } + + c_foreach (i, cmap_strn, word_map) { + printf("%d occurrences of word '%s'\n", + i.ref->second, i.ref->first.str); } } return 0; -- cgit v1.2.3