summaryrefslogtreecommitdiffhomepage
path: root/misc/examples/multimap.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc/examples/multimap.c')
-rw-r--r--misc/examples/multimap.c53
1 files changed, 26 insertions, 27 deletions
diff --git a/misc/examples/multimap.c b/misc/examples/multimap.c
index 1d5d259d..d8981a81 100644
--- a/misc/examples/multimap.c
+++ b/misc/examples/multimap.c
@@ -41,7 +41,6 @@ void OlympicLoc_drop(OlympicLoc* self);
// Create a clist<OlympicLoc>, can be sorted by year.
#define i_valclass OlympicLoc // binds _cmp, _clone and _drop.
#define i_tag OL
-#define i_extern // define _clist_mergesort()
#include <stc/clist.h>
// Create a csmap<cstr, clist_OL> where key is country name
@@ -69,34 +68,34 @@ void OlympicLoc_drop(OlympicLoc* self) {
int main()
{
// Define the multimap with destructor defered to when block is completed.
- c_auto (csmap_OL, multimap)
+ csmap_OL multimap = {0};
+ const clist_OL empty = clist_OL_init();
+
+ for (size_t i = 0; i < c_arraylen(ol_data); ++i)
{
- const clist_OL empty = clist_OL_init();
+ struct OlympicsData* d = &ol_data[i];
+ OlympicLoc 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 country already exist in map, its list is returned from the insert function.
+ clist_OL* list = &csmap_OL_emplace(&multimap, d->country, empty).ref->second;
+ clist_OL_push_back(list, loc);
+ }
- for (size_t i = 0; i < c_ARRAYLEN(ol_data); ++i)
- {
- struct OlympicsData* d = &ol_data[i];
- OlympicLoc 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 country already exist in map, its list is returned from the insert function.
- clist_OL* list = &csmap_OL_emplace(&multimap, d->country, empty).ref->second;
- clist_OL_push_back(list, loc);
- }
- // Sort locations by year for each country.
- c_foreach (country, csmap_OL, multimap)
- clist_OL_sort(&country.ref->second);
+ // Sort locations by year for each country.
+ c_foreach (country, csmap_OL, multimap)
+ 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", cstr_str(&country.ref->first),
- loc.ref->year,
- cstr_str(&loc.ref->city),
- cstr_str(&loc.ref->date));
- }
+ // 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", cstr_str(&country.ref->first),
+ loc.ref->year,
+ cstr_str(&loc.ref->city),
+ cstr_str(&loc.ref->date));
}
+ csmap_OL_drop(&multimap);
}