From 5b78847871217562dcddbb0bd34076a892eb49e1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 6 Sep 2020 23:32:30 +0200 Subject: Renamed push_****(), _insert() to _emplace(). added insert_or_assign(), etc. --- examples/advanced.c | 2 +- examples/benchmark.c | 19 ++++++------------- examples/birthday.c | 4 ++-- examples/demos.c | 27 +++++++++++++-------------- examples/ex_gaussian.c | 2 +- examples/geek1.c | 4 ++-- examples/geek3.c | 8 ++++---- examples/geek4.c | 12 ++++++------ examples/geek7.c | 2 +- examples/inits.c | 8 ++++---- examples/mapmap.c | 14 +++++++------- examples/phonebook.c | 2 +- examples/stack.c | 3 ++- examples/words.c | 4 ++-- 14 files changed, 52 insertions(+), 59 deletions(-) (limited to 'examples') diff --git a/examples/advanced.c b/examples/advanced.c index 90f800e0..29ba55aa 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -72,7 +72,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)->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/benchmark.c b/examples/benchmark.c index 1c94397d..15d659ff 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -37,8 +37,7 @@ crand_rng64_t rng; #define CMAP_SETUP(tt, Key, Value) cmap_##tt map = cmap_ini \ ; cmap_##tt##_set_load_factors(&map, max_load_factor, 0.0) #define CMAP_PUT(tt, key, val) cmap_##tt##_put(&map, key, val)->value -#define CMAP_INSERT(tt, key, val) cmap_##tt##_insert(&map, key, val) -#define CMAP_EMPLACE(tt, key, val) cmap_try_emplace(tt, &map, key, val) +#define CMAP_EMPLACE(tt, key, val) cmap_##tt##_emplace(&map, key, val) #define CMAP_ERASE(tt, key) cmap_##tt##_erase(&map, key) #define CMAP_FIND(tt, key) (cmap_##tt##_find(map, key) != NULL) #define CMAP_FOR(tt, i) c_foreach (i, cmap_##tt, map) @@ -50,7 +49,7 @@ crand_rng64_t rng; #define KMAP_SETUP(tt, Key, Value) khash_t(ii)* map = kh_init(ii); khiter_t ki; int ret #define KMAP_PUT(tt, key, val) (*(ki = kh_put(ii, map, key, &ret), map->vals[ki] = val, &map->vals[ki])) -#define KMAP_INSERT(tt, key, val) (ki = kh_put(ii, map, key, &ret), ret ? (map->vals[ki] = val) : val) +#define KMAP_EMPLACE(tt, key, val) (ki = kh_put(ii, map, key, &ret), ret ? (map->vals[ki] = val) : val) #define KMAP_ERASE(tt, key) ((ki = kh_get(ii, map, key)) != kh_end(map) ? kh_del(ii, map, ki), 1 : 0) #define KMAP_FIND(tt, key) (kh_get(ii, map, key) != kh_end(map)) #define KMAP_SIZE(tt) kh_size(map) @@ -60,7 +59,6 @@ crand_rng64_t rng; #define UMAP_SETUP(tt, Key, Value) std::unordered_map map; map.max_load_factor(max_load_factor) #define UMAP_PUT(tt, key, val) (map[key] = val) -#define UMAP_INSERT(tt, key, val) map.insert(std::make_pair(key, val)) #define UMAP_EMPLACE(tt, key, val) map.emplace(key, val) #define UMAP_FIND(tt, key) (map.find(key) != map.end()) #define UMAP_ERASE(tt, key) map.erase(key) @@ -73,7 +71,6 @@ crand_rng64_t rng; #define BMAP_SETUP(tt, Key, Value) ska::bytell_hash_map map; map.max_load_factor(max_load_factor) #define BMAP_PUT(tt, key, val) UMAP_PUT(tt, key, val) -#define BMAP_INSERT(tt, key, val) UMAP_INSERT(tt, key, val) #define BMAP_EMPLACE(tt, key, val) UMAP_EMPLACE(tt, key, val) #define BMAP_FIND(tt, key) UMAP_FIND(tt, key) #define BMAP_ERASE(tt, key) UMAP_ERASE(tt, key) @@ -86,7 +83,6 @@ crand_rng64_t rng; #define FMAP_SETUP(tt, Key, Value) ska::flat_hash_map map; map.max_load_factor(max_load_factor) #define FMAP_PUT(tt, key, val) UMAP_PUT(tt, key, val) -#define FMAP_INSERT(tt, key, val) UMAP_INSERT(tt, key, val) #define FMAP_EMPLACE(tt, key, val) UMAP_EMPLACE(tt, key, val) #define FMAP_FIND(tt, key) UMAP_FIND(tt, key) #define FMAP_ERASE(tt, key) UMAP_ERASE(tt, key) @@ -99,7 +95,6 @@ crand_rng64_t rng; #define HMAP_SETUP(tt, Key, Value) tsl::hopscotch_map map; map.max_load_factor(max_load_factor) #define HMAP_PUT(tt, key, val) UMAP_PUT(tt, key, val) -#define HMAP_INSERT(tt, key, val) UMAP_INSERT(tt, key, val) #define HMAP_EMPLACE(tt, key, val) UMAP_EMPLACE(tt, key, val) #define HMAP_FIND(tt, key) UMAP_FIND(tt, key) #define HMAP_ERASE(tt, key) UMAP_ERASE(tt, key) @@ -112,7 +107,6 @@ crand_rng64_t rng; #define RMAP_SETUP(tt, Key, Value) robin_hood::unordered_map map #define RMAP_PUT(tt, key, val) UMAP_PUT(tt, key, val) -#define RMAP_INSERT(tt, key, val) UMAP_INSERT(tt, key, val) #define RMAP_EMPLACE(tt, key, val) UMAP_EMPLACE(tt, key, val) #define RMAP_FIND(tt, key) UMAP_FIND(tt, key) #define RMAP_ERASE(tt, key) UMAP_ERASE(tt, key) @@ -125,7 +119,6 @@ crand_rng64_t rng; #define SMAP_SETUP(tt, Key, Value) spp::sparse_hash_map map; map.max_load_factor(max_load_factor) #define SMAP_PUT(tt, key, val) UMAP_PUT(tt, key, val) -#define SMAP_INSERT(tt, key, val) UMAP_INSERT(tt, key, val) #define SMAP_EMPLACE(tt, key, val) UMAP_EMPLACE(tt, key, val) #define SMAP_FIND(tt, key) UMAP_FIND(tt, key) #define SMAP_ERASE(tt, key) UMAP_ERASE(tt, key) @@ -225,14 +218,14 @@ int main(int argc, char* argv[]) seed = time(NULL); printf("\nRandom keys are in range [0, 2^%d), seed = %zu:\n", rr, seed); printf("\nUnordered maps: %d repeats of Insert random key + try to remove a random key:\n", N1); - RUN_TEST(1) + //RUN_TEST(1) printf("\nUnordered maps: Insert %d index keys, then remove them in same order:\n", N2); - RUN_TEST(2) + //RUN_TEST(2) printf("\nUnordered maps: Insert %d random keys, then remove them in same order:\n", N3); - RUN_TEST(3) + //RUN_TEST(3) - printf("\nUnordered maps: Iterate %d random keys, then remove them in same order:\n", N4); + printf("\nUnordered maps: Iterate %d random keys:\n", N4); RUN_TEST(4) } diff --git a/examples/birthday.c b/examples/birthday.c index be2114da..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)->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)->value; + ++cmap_x_emplace(&map, crand_uniform_i32(&dist), 0).item->value; } float diff = (float) (clock() - now) / CLOCKS_PER_SEC; diff --git a/examples/demos.c b/examples/demos.c index 474e539e..c0c5de18 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -61,9 +61,9 @@ void vectordemo2() { printf("\nVECTORDEMO2\n"); cvec_str names = cvec_ini; - cvec_str_push_back(&names, "Mary"); - cvec_str_push_back(&names, "Joe"); - cvec_str_push_back(&names, "Chris"); + cvec_str_emplace_back(&names, "Mary"); + cvec_str_emplace_back(&names, "Joe"); + cvec_str_emplace_back(&names, "Chris"); cstr_assign(&names.data[1], "Jane"); // replace Joe printf("names[1]: %s\n", names.data[1].str); @@ -151,26 +151,25 @@ void mapdemo2() } -declare_cmap_strkey(ss, cstr_t, cstr_destroy); +declare_cmap_str(); void mapdemo3() { printf("\nMAPDEMO3\n"); - cmap_ss table = cmap_ini; - cmap_ss_put(&table, "Map", cstr_make("test")); - cmap_ss_put(&table, "Make", cstr_make("my")); - cmap_ss_put(&table, "Sunny", cstr_make("day")); - cmap_ss_entry_t *e = cmap_ss_find(&table, "Make"); - c_foreach (i, cmap_ss, table) + cmap_str table = cmap_ini; + cmap_str_put(&table, "Map", "test"); + cmap_str_put(&table, "Make", "my"); + cmap_str_put(&table, "Sunny", "day"); + cmap_str_entry_t *e = cmap_str_find(&table, "Make"); + c_foreach (i, cmap_str, table) printf("entry: %s: %s\n", i.item->key.str, i.item->value.str); printf("size %zu: remove: Make: %s\n", cmap_size(table), e->value.str); - cmap_ss_erase(&table, "Make"); - //cmap_ss_eraseEntry(&table, e); + cmap_str_erase(&table, "Make"); printf("size %zu\n", cmap_size(table)); - c_foreach (i, cmap_ss, table) + c_foreach (i, cmap_str, table) printf("entry: %s: %s\n", i.item->key.str, i.item->value.str); - cmap_ss_destroy(&table); // frees key and value CStrs, and hash table (CVec). + cmap_str_destroy(&table); // frees key and value CStrs, and hash table (CVec). } diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c index a4682a2b..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)->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 8759e143..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)->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); + *hs = cmap_ii_emplace(&hash, second, 0).item; // Check for minimal occurrence if (hf->value < hs->value) { // Take the minimal diff --git a/examples/geek3.c b/examples/geek3.c index 044d7b16..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)->value += 272; - cmap_si_put(&mymap, "Jupiter", cmap_si_insert(&mymap, "Saturn", 0)->value + 9638); - cmap_si_insert(&mymap, "Sun", 0)->value += 1000; - cmap_si_insert(&mymap, "Sun", 0)->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/geek4.c b/examples/geek4.c index 06c93de4..9f4df539 100644 --- a/examples/geek4.c +++ b/examples/geek4.c @@ -106,13 +106,13 @@ int commonWords(cvec_str S) // make it false for (int k = 0; k < cvec_size(ans); k++) { if (ans.data[k].value != false - && cmap_sb_insert(&has, ans.data[k].key.str, false)->value == false) { + && cmap_sb_emplace(&has, ans.data[k].key.str, false).item->value == false) { ans.data[k].value = false; } // This line is used to consider only distinct words else if (ans.data[k].value != false - && cmap_sb_insert(&has, ans.data[k].key.str, false)->value == true) { + && cmap_sb_emplace(&has, ans.data[k].key.str, false).item->value == true) { cmap_sb_put(&has, ans.data[k].key.str, false); } } @@ -137,10 +137,10 @@ int commonWords(cvec_str S) int main() { cvec_str S = cvec_ini; - cvec_str_push_back(&S, "there is a cow"); - cvec_str_push_back(&S, "cow is our mother"); - cvec_str_push_back(&S, "cow gives us milk and milk is sweet"); - cvec_str_push_back(&S, "there is a boy who loves cow"); + cvec_str_emplace_back(&S, "there is a cow"); + cvec_str_emplace_back(&S, "cow is our mother"); + cvec_str_emplace_back(&S, "cow gives us milk and milk is sweet"); + cvec_str_emplace_back(&S, "there is a boy who loves cow"); printf("%d\n", commonWords(S)); cvec_str_destroy(&S); diff --git a/examples/geek7.c b/examples/geek7.c index 4be984ee..379dac9f 100644 --- a/examples/geek7.c +++ b/examples/geek7.c @@ -41,7 +41,7 @@ void findElementsAfterDel(int arr[], int m, int del[], for (int i = 0; i < n; ++i) { // Increment the count of del[i] - cmap_ii_insert(&mp, del[i], 0)->value++; + cmap_ii_emplace(&mp, del[i], 0).item->value++; } cpqueue_i heap = cpqueue_i_init(); diff --git a/examples/inits.c b/examples/inits.c index d7b7a37b..16882e7a 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -68,10 +68,10 @@ int main(void) { {"Spain", 10}, {"France", 10}, )); - cmap_cnt_insert(&countries, "Greenland", 0)->value += 20; - cmap_cnt_insert(&countries, "Sweden", 0)->value += 20; - cmap_cnt_insert(&countries, "Norway", 0)->value += 20; - cmap_cnt_insert(&countries, "Finland", 0)->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 a0c1b173..6929dc69 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)->value, "name", "Joe"); - cmap_str_put(&cmap_cfg_insert(&config, "user", init)->value, "groups", "proj1,proj3"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init)->value, "proj1", "Energy"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init)->value, "proj2", "Windy"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init)->value, "proj3", "Oil"); - cmap_str_put(&cmap_cfg_insert(&config, "admin", init)->value, "employees", "2302"); + cmap_str_insert_or_assign(&cmap_cfg_emplace(&config, "user", init).item->value, "name", "Joe"); + cmap_str_insert_or_assign(&cmap_cfg_emplace(&config, "user", init).item->value, "groups", "proj1,proj3"); + cmap_str_insert_or_assign(&cmap_cfg_emplace(&config, "group", init).item->value, "proj1", "Energy"); + cmap_str_insert_or_assign(&cmap_cfg_emplace(&config, "group", init).item->value, "proj2", "Windy"); + cmap_str_insert_or_assign(&cmap_cfg_emplace(&config, "group", init).item->value, "proj3", "Oil"); + cmap_str_insert_or_assign(&cmap_cfg_emplace(&config, "admin", init).item->value, "employees", "2302"); - cmap_str_put(&cmap_cfg_insert(&config, "group", init)->value, "proj2", "Wind"); // Update + cmap_str_insert_or_assign(&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/phonebook.c b/examples/phonebook.c index d2ed227f..50a16467 100644 --- a/examples/phonebook.c +++ b/examples/phonebook.c @@ -62,7 +62,7 @@ int main(int argc, char **argv) printf("\nPhone book after erasing Tariq and Elliott:\n"); print_phone_book(phone_book); - cmap_str_put(&phone_book, "Zak Byers", "(555) 396-188"); + cmap_str_insert_or_assign(&phone_book, "Zak Byers", "(555) 396-188"); printf("\nPhone book after update phone of Zak Byers:\n"); print_phone_book(phone_book); diff --git a/examples/stack.c b/examples/stack.c index de30c5b4..dedf1eb5 100644 --- a/examples/stack.c +++ b/examples/stack.c @@ -4,8 +4,9 @@ #include declare_cvec(i, int); +declare_cvec(c, char); declare_cstack(i, cvec_i); -declare_cstack(c, cstr); +declare_cstack(c, cvec_c); int main() { cstack_i stack = cstack_i_init(); diff --git a/examples/words.c b/examples/words.c index 275ffee7..92f98c85 100644 --- a/examples/words.c +++ b/examples/words.c @@ -16,7 +16,7 @@ int main1() "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" )); - clist_str_push_back_v(&lwords, cstr_from("%f", 123897.0 / 23.0)); + clist_str_push_back(&lwords, cstr_from("%f", 123897.0 / 23.0)); c_foreach (w, clist_str, lwords) printf("%s\n", w.item->value.str); puts(""); @@ -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)->value; + ++cmap_si_emplace(&word_map, w.item->str, 0).item->value; c_foreach (pair, cmap_si, word_map) { printf("%d occurrences of word '%s'\n", -- cgit v1.2.3