From 1a9020f895754f564e480ba9ed564c637f26060e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 12 Sep 2021 09:26:03 +0200 Subject: Some improvements in examples. --- examples/complex.c | 2 +- examples/csmap_find.c | 1 - examples/multimap.c | 29 +++++++++++++++-------------- examples/read.c | 29 ++++++++++++++--------------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/examples/complex.c b/examples/complex.c index 5cbaaa40..e7789815 100644 --- a/examples/complex.c +++ b/examples/complex.c @@ -44,7 +44,7 @@ int main() { printf("stk size: %zu\n", cstack_f_size(stk)); - // Put in some data in 2D array + // Put in some data in stack array stk.data[x] = 3.1415927f; clist_arr_push_back(&tableList, stk); cmap_lst_insert(&listMap, tableKey, tableList); diff --git a/examples/csmap_find.c b/examples/csmap_find.c index 79a0f993..66768016 100644 --- a/examples/csmap_find.c +++ b/examples/csmap_find.c @@ -1,7 +1,6 @@ // 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 #define i_tag istr #define i_key int diff --git a/examples/multimap.c b/examples/multimap.c index eeb7a501..0f5ce538 100644 --- a/examples/multimap.c +++ b/examples/multimap.c @@ -45,11 +45,11 @@ void OlympicLocation_del(OlympicLocation* self) { // Create a clist, can be sorted by year. #define i_tag OL #define i_val OlympicLocation -#define i_cmp OlympicLocation_compare #define i_valdel OlympicLocation_del +#define i_cmp OlympicLocation_compare #include -// Create a csmap where key is country +// Create a csmap where key is country name #define i_tag OL #define i_key_str #define i_val clist_OL @@ -59,27 +59,28 @@ void OlympicLocation_del(OlympicLocation* self) { int main() { // Define the multimap with destructor defered to when block is completed. - c_forvar (csmap_OL multimap = csmap_OL_init(), csmap_OL_del(&multimap)) + c_forauto (csmap_OL, multimap) { - clist_OL empty = clist_OL_init(); + const clist_OL empty = clist_OL_init(); for (int i = 0; i < c_arraylen(ol_data); ++i) { - struct OlympicsData* it = &ol_data[i]; - OlympicLocation loc = {.year = it->year, - .city = cstr_from(it->city), - .date = cstr_from(it->date)}; + struct OlympicsData* d = &ol_data[i]; + OlympicLocation loc = {.year = d->year, + .city = cstr_from(d->city), + .date = cstr_from(d->date)}; // Insert an empty list for each new country, and append the entry to the list. - // If list already exist in map, it returns it from the insert function. - clist_OL_push_back(&csmap_OL_insert(&multimap, cstr_from(it->country), empty).ref->second, loc); + // If country already exist in map, its list is returned from the insert function. + clist_OL* list = &csmap_OL_insert(&multimap, cstr_from(d->country), empty).ref->second; + clist_OL_push_back(list, loc); } - - // Iterate the multimap: + // Sort locations by year for each country. c_foreach (country, csmap_OL, multimap) - { - // Sort locations by year for each country. clist_OL_sort(&country.ref->second); + // Print the multimap: + c_foreach (country, csmap_OL, multimap) + { // Loop the locations for a country sorted by year c_foreach (loc, clist_OL, country.ref->second) printf("%s: %d, %s, %s\n", country.ref->first.str, loc.ref->year, diff --git a/examples/read.c b/examples/read.c index bb0e139f..2e8320eb 100644 --- a/examples/read.c +++ b/examples/read.c @@ -1,27 +1,26 @@ -#include #include +#include #define i_val_str #include -cvec_str read_file(const char* name) { +cvec_str read_file(const char* name) +{ cvec_str vec = cvec_str_init(); - for (FILE* f = fopen(name, "r"); f; fclose(f), f=NULL) { - cstr line = cstr_init(); - while (cstr_getline(&line, f)) - cvec_str_emplace_back(&vec, line.str); - cstr_del(&line); - } + c_forvar (FILE* f = fopen(name, "r"), fclose(f)) + c_forauto (cstr, line) + while (cstr_getline(&line, f)) + cvec_str_emplace_back(&vec, line.str); return vec; } -int main() { - cvec_str vec = read_file("read.c"); - if (errno) printf("errno: %d\n", errno); - +int main() +{ int n = 0; - c_foreach (i, cvec_str, vec) - printf("%5d: %s\n", ++n, i.ref->str); + c_forvar (cvec_str vec = read_file(__FILE__), cvec_str_del(&vec)) + c_foreach (i, cvec_str, vec) + printf("%5d: %s\n", ++n, i.ref->str); - cvec_str_del(&vec); + if (errno) + printf("error: read_file(" __FILE__ "). errno: %d\n", errno); } \ No newline at end of file -- cgit v1.2.3