From 8dd8368bca262dbab39741fbbb61c16478772e4e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 10 Sep 2020 20:48:44 +0200 Subject: Use cmap_emplace() rather than cmap_insert(). insert() is reduced to an alias of emplace() because it makes no sense using the std::unordered_map API. --- examples/advanced.c | 2 +- examples/birthday.c | 4 ++-- examples/ex_gaussian.c | 2 +- examples/geek1.c | 4 ++-- examples/geek2.c | 8 ++++---- examples/geek3.c | 8 ++++---- examples/inits.c | 8 ++++---- examples/mapmap.c | 14 +++++++------- examples/words.c | 2 +- 9 files changed, 26 insertions(+), 26 deletions(-) (limited to 'examples') diff --git a/examples/advanced.c b/examples/advanced.c index aa82e38e..0aa1600e 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -71,7 +71,7 @@ int main() VikingVw einar = {"Einar", "Norway"}; cmap_vk_entry_t *e = cmap_vk_find(&vikings, einar); e->value += 5; // update - cmap_vk_insert(&vikings, einar, 0).item->value += 5; // again + cmap_vk_emplace(&vikings, einar, 0).item->value += 5; // again c_foreach (k, cmap_vk, vikings) { printf("%s of %s has %d hp\n", k.item->key.name.str, k.item->key.country.str, k.item->value); diff --git a/examples/birthday.c b/examples/birthday.c index 5f5174d6..bf807e67 100644 --- a/examples/birthday.c +++ b/examples/birthday.c @@ -21,7 +21,7 @@ void repeats(void) clock_t now = clock(); for (size_t i = 0; i < N; ++i) { uint64_t k = crand_i64(&rng) & mask; - int v = ++cmap_ic_insert(&m, k, 0).item->value; + int v = ++cmap_ic_emplace(&m, k, 0).item->value; if (v > 1) printf("%zu: %llx - %d\n", i, k, v); } float diff = (float) (clock() - now) / CLOCKS_PER_SEC; @@ -40,7 +40,7 @@ void distribution(void) clock_t now = clock(); crand_uniform_i32_t dist = crand_uniform_i32_init(rng, 0, M); for (size_t i = 0; i < N; ++i) { - ++cmap_x_insert(&map, crand_uniform_i32(&dist), 0).item->value; + ++cmap_x_emplace(&map, crand_uniform_i32(&dist), 0).item->value; } float diff = (float) (clock() - now) / CLOCKS_PER_SEC; diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c index dd3a61ac..d2da77d9 100644 --- a/examples/ex_gaussian.c +++ b/examples/ex_gaussian.c @@ -32,7 +32,7 @@ int main() cmap_i mhist = cmap_ini; for (size_t i = 0; i < N; ++i) { int index = round( crand_normal_f64(&dist) ); - cmap_i_insert(&mhist, index, 0).item->value += 1; + cmap_i_emplace(&mhist, index, 0).item->value += 1; } // Transfer map to vec and sort it by map keys. diff --git a/examples/geek1.c b/examples/geek1.c index fdc6d84c..194b92da 100644 --- a/examples/geek1.c +++ b/examples/geek1.c @@ -21,7 +21,7 @@ int findMaximumPairs(int a[], int n, int k) // Hash-table cmap_ii hash = cmap_ini; for (int i = 0; i < n; i++) { - cmap_ii_insert(&hash, a[i] % k, 0).item->value++; + cmap_ii_emplace(&hash, a[i] % k, 0).item->value++; } int count = 0; @@ -39,7 +39,7 @@ int findMaximumPairs(int a[], int n, int k) int second = k - it.item->key; cmap_ii_entry_t *hf = cmap_ii_find(&hash, first), - *hs = cmap_ii_insert(&hash, second, 0).item; + *hs = cmap_ii_emplace(&hash, second, 0).item; // Check for minimal occurrence if (hf->value < hs->value) { // Take the minimal diff --git a/examples/geek2.c b/examples/geek2.c index 01b5c367..b65f097d 100644 --- a/examples/geek2.c +++ b/examples/geek2.c @@ -27,19 +27,19 @@ int main() {"The Adventures of Sherlock Holmes", "Eye lyked it alot."}, )); /* - cmap_str_insert(&book_reviews, + cmap_str_emplace(&book_reviews, "Adventures of Huckleberry Finn", "My favorite book." ); - cmap_str_insert(&book_reviews, + cmap_str_emplace(&book_reviews, "Grimms' Fairy Tales", "Masterpiece." ); - cmap_str_insert(&book_reviews, + cmap_str_emplace(&book_reviews, "Pride and Prejudice", "Very enjoyable." ); - cmap_str_insert(&book_reviews, + cmap_str_emplace(&book_reviews, "The Adventures of Sherlock Holmes", "Eye lyked it alot." ); diff --git a/examples/geek3.c b/examples/geek3.c index 29614fc7..be3c8c10 100644 --- a/examples/geek3.c +++ b/examples/geek3.c @@ -15,10 +15,10 @@ int main () // ... cmap_si_put(&mymap, "Mars", 3396); - cmap_si_insert(&mymap, "Saturn", 0).item->value += 272; - cmap_si_put(&mymap, "Jupiter", cmap_si_insert(&mymap, "Saturn", 0).item->value + 9638); - cmap_si_insert(&mymap, "Sun", 0).item->value += 1000; - cmap_si_insert(&mymap, "Sun", 0).item->value += 1000; + cmap_si_emplace(&mymap, "Saturn", 0).item->value += 272; + cmap_si_put(&mymap, "Jupiter", cmap_si_emplace(&mymap, "Saturn", 0).item->value + 9638); + cmap_si_emplace(&mymap, "Sun", 0).item->value += 1000; + cmap_si_emplace(&mymap, "Sun", 0).item->value += 1000; c_foreach (x, cmap_si, mymap) { printf("%s: %d\n", x.item->key.str, x.item->value); diff --git a/examples/inits.c b/examples/inits.c index 55f35bdd..ebb2e0e1 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -71,10 +71,10 @@ int main(void) { {"Spain", 10}, {"France", 10}, )); - cmap_cnt_insert(&countries, "Greenland", 0).item->value += 20; - cmap_cnt_insert(&countries, "Sweden", 0).item->value += 20; - cmap_cnt_insert(&countries, "Norway", 0).item->value += 20; - cmap_cnt_insert(&countries, "Finland", 0).item->value += 20; + cmap_cnt_emplace(&countries, "Greenland", 0).item->value += 20; + cmap_cnt_emplace(&countries, "Sweden", 0).item->value += 20; + cmap_cnt_emplace(&countries, "Norway", 0).item->value += 20; + cmap_cnt_emplace(&countries, "Finland", 0).item->value += 20; c_foreach (i, cmap_cnt, countries) printf("%s: %d\n", i.item->key.str, i.item->value); diff --git a/examples/mapmap.c b/examples/mapmap.c index e1e78bba..f717a4bc 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -9,14 +9,14 @@ declare_cmap_strkey(cfg, cmap_str, cmap_str_destroy); int main(void) { cmap_cfg config = cmap_ini; cmap_str init = cmap_ini; - cmap_str_put(&cmap_cfg_insert(&config, "user", init).item->value, "name", "Joe"); - cmap_str_put(&cmap_cfg_insert(&config, "user", init).item->value, "groups", "proj1,proj3"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init).item->value, "proj1", "Energy"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init).item->value, "proj2", "Windy"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init).item->value, "proj3", "Oil"); - cmap_str_put(&cmap_cfg_insert(&config, "admin", init).item->value, "employees", "2302"); + cmap_str_put(&cmap_cfg_emplace(&config, "user", init).item->value, "name", "Joe"); + cmap_str_put(&cmap_cfg_emplace(&config, "user", init).item->value, "groups", "proj1,proj3"); + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).item->value, "proj1", "Energy"); + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).item->value, "proj2", "Windy"); + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).item->value, "proj3", "Oil"); + cmap_str_put(&cmap_cfg_emplace(&config, "admin", init).item->value, "employees", "2302"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init).item->value, "proj2", "Wind"); // Update + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).item->value, "proj2", "Wind"); // Update c_foreach (i, cmap_cfg, config) c_foreach (j, cmap_str, i.item->value) diff --git a/examples/words.c b/examples/words.c index af637b8d..9e0fc0cc 100644 --- a/examples/words.c +++ b/examples/words.c @@ -29,7 +29,7 @@ int main1() cmap_si word_map = cmap_ini; c_foreach (w, cvec_str, words) - cmap_si_insert(&word_map, w.item->str, 0).item->value += 1; + cmap_si_emplace(&word_map, w.item->str, 0).item->value += 1; c_foreach (pair, cmap_si, word_map) { printf("%d occurrences of word '%s'\n", -- cgit v1.2.3