From 0b7b008208c19bffc803b0b222af94223ac58551 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 14 Sep 2020 14:19:41 +0200 Subject: Renamed result_t type members to first, second (from item, inserted) for conformance with std::pair --- examples/advanced.c | 2 +- examples/benchmark.c | 2 +- examples/birthday.c | 4 ++-- examples/ex_gaussian.c | 2 +- examples/geek1.c | 12 ++++++------ examples/geek3.c | 8 ++++---- examples/geek4.c | 4 ++-- examples/geek7.c | 2 +- examples/inits.c | 8 ++++---- examples/mapmap.c | 14 +++++++------- examples/words.c | 2 +- stc/cmap.h | 20 ++++++++++---------- 12 files changed, 40 insertions(+), 40 deletions(-) diff --git a/examples/advanced.c b/examples/advanced.c index d3c1ca21..fe46f5da 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_emplace(&vikings, einar, 0).item->value += 5; // again + cmap_vk_emplace(&vikings, einar, 0).first->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 99e06fee..3824e02f 100644 --- a/examples/benchmark.c +++ b/examples/benchmark.c @@ -36,7 +36,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).item->value +#define CMAP_PUT(tt, key, val) cmap_##tt##_put(&map, key, val).first->value #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) diff --git a/examples/birthday.c b/examples/birthday.c index 8b6abd77..59a8aee5 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_emplace(&m, k, 0).item->value; + int v = ++cmap_ic_emplace(&m, k, 0).first->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(0, M); for (size_t i = 0; i < N; ++i) { - ++cmap_x_emplace(&map, crand_uniform_i32(&rng, &dist), 0).item->value; + ++cmap_x_emplace(&map, crand_uniform_i32(&rng, &dist), 0).first->value; } float diff = (float) (clock() - now) / CLOCKS_PER_SEC; diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c index 8751ffa0..66abb889 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 = (int) round( crand_normal_f64(&rng, &dist) ); - cmap_i_emplace(&mhist, index, 0).item->value += 1; + cmap_i_emplace(&mhist, index, 0).first->value += 1; } // Transfer map to vec and sort it by map keys. diff --git a/examples/geek1.c b/examples/geek1.c index 194b92da..983539b8 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_emplace(&hash, a[i] % k, 0).item->value++; + cmap_ii_emplace(&hash, a[i] % k, 0).first->value++; } int count = 0; @@ -35,11 +35,11 @@ int findMaximumPairs(int a[], int n, int k) count += it.item->value / 2; cmap_ii_put(&hash, it.item->key, it.item->key & 1); } else { - int first = it.item->key; - int second = k - it.item->key; + int one = it.item->key; + int two = k - it.item->key; - cmap_ii_entry_t *hf = cmap_ii_find(&hash, first), - *hs = cmap_ii_emplace(&hash, second, 0).item; + cmap_ii_entry_t *hf = cmap_ii_find(&hash, one), + *hs = cmap_ii_emplace(&hash, two, 0).first; // Check for minimal occurrence if (hf->value < hs->value) { // Take the minimal @@ -59,7 +59,7 @@ int findMaximumPairs(int a[], int n, int k) } else { // Check if numbers are same - if (first == second) { + if (one == two) { // If same then number of pairs will be half count += it.item->value / 2; diff --git a/examples/geek3.c b/examples/geek3.c index be3c8c10..9c959f0e 100644 --- a/examples/geek3.c +++ b/examples/geek3.c @@ -15,10 +15,10 @@ int main () // ... cmap_si_put(&mymap, "Mars", 3396); - 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; + cmap_si_emplace(&mymap, "Saturn", 0).first->value += 272; + cmap_si_put(&mymap, "Jupiter", cmap_si_emplace(&mymap, "Saturn", 0).first->value + 9638); + cmap_si_emplace(&mymap, "Sun", 0).first->value += 1000; + cmap_si_emplace(&mymap, "Sun", 0).first->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 9f4df539..ec62efbc 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_emplace(&has, ans.data[k].key.str, false).item->value == false) { + && cmap_sb_emplace(&has, ans.data[k].key.str, false).first->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_emplace(&has, ans.data[k].key.str, false).item->value == true) { + && cmap_sb_emplace(&has, ans.data[k].key.str, false).first->value == true) { cmap_sb_put(&has, ans.data[k].key.str, false); } } diff --git a/examples/geek7.c b/examples/geek7.c index 379dac9f..eb6b61cb 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_emplace(&mp, del[i], 0).item->value++; + cmap_ii_emplace(&mp, del[i], 0).first->value++; } cpqueue_i heap = cpqueue_i_init(); diff --git a/examples/inits.c b/examples/inits.c index 5a0ae04b..7134c3ff 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -71,10 +71,10 @@ int main(void) { {"Spain", 10}, {"France", 10}, }); - 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; + cmap_cnt_emplace(&countries, "Greenland", 0).first->value += 20; + cmap_cnt_emplace(&countries, "Sweden", 0).first->value += 20; + cmap_cnt_emplace(&countries, "Norway", 0).first->value += 20; + cmap_cnt_emplace(&countries, "Finland", 0).first->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 f717a4bc..f3df9609 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_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_emplace(&config, "user", init).first->value, "name", "Joe"); + cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->value, "groups", "proj1,proj3"); + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->value, "proj1", "Energy"); + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->value, "proj2", "Windy"); + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->value, "proj3", "Oil"); + cmap_str_put(&cmap_cfg_emplace(&config, "admin", init).first->value, "employees", "2302"); - cmap_str_put(&cmap_cfg_emplace(&config, "group", init).item->value, "proj2", "Wind"); // Update + cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->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 09dd43dc..5fc80c62 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_emplace(&word_map, w.item->str, 0).item->value += 1; + cmap_si_emplace(&word_map, w.item->str, 0).first->value += 1; c_foreach (pair, cmap_si, word_map) { printf("%d occurrences of word '%s'\n", diff --git a/stc/cmap.h b/stc/cmap.h index 7b62efeb..d049cb2b 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -63,7 +63,7 @@ int main(void) { #define c_try_emplace(self, ctype, key, val) do { \ ctype##_result_t __r = ctype##_insert_key_(self, key); \ - if (__r.inserted) __r.item->value = val; \ + if (__r.second) __r.first->value = val; \ } while (0) #define c_insert(self, ctype, ...) do { \ const ctype##_input_t __arr[] = __VA_ARGS__; \ @@ -178,8 +178,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; ctype##_##X##_input_t; \ \ typedef struct { \ - c_UNION(ctype##_##X##_value_t *item, *first); \ - c_UNION(bool inserted, second); \ + ctype##_##X##_value_t *first; \ + bool second; /* inserted */ \ } ctype##_##X##_result_t; \ \ typedef struct ctype##_##X { \ @@ -232,7 +232,7 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; STC_INLINE ctype##_##X##_result_t \ ctype##_##X##_emplace(ctype##_##X* self, CMAP_ARGS_##ctype(RawKey rawKey, RawVal rawVal)) { \ ctype##_##X##_result_t res = ctype##_##X##_insert_key_(self, rawKey); \ - CMAP_ONLY_##ctype( if (res.inserted) res.item->value = valueFromRaw(rawVal); ) \ + CMAP_ONLY_##ctype( if (res.second) res.first->value = valueFromRaw(rawVal); ) \ return res; \ } \ STC_INLINE ctype##_##X##_result_t \ @@ -245,8 +245,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; STC_INLINE ctype##_##X##_result_t \ ctype##_##X##_insert_or_assign(ctype##_##X* self, RawKey rawKey, RawVal rawVal) { \ ctype##_##X##_result_t res = ctype##_##X##_insert_key_(self, rawKey); \ - if (!res.inserted) valueDestroy(&res.item->value); \ - res.item->value = valueFromRaw(rawVal); return res; \ + if (!res.second) valueDestroy(&res.first->value); \ + res.first->value = valueFromRaw(rawVal); return res; \ } \ STC_API ctype##_##X##_mapped_t* \ ctype##_##X##_at(const ctype##_##X* self, RawKey rawKey) { \ @@ -261,8 +261,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; STC_INLINE ctype##_##X##_result_t \ ctype##_##X##_putv(ctype##_##X* self, RawKey rawKey, Mapped mapped) { \ ctype##_##X##_result_t res = ctype##_##X##_insert_key_(self, rawKey); \ - if (!res.inserted) valueDestroy(&res.item->value); \ - res.item->value = mapped; return res; \ + if (!res.second) valueDestroy(&res.first->value); \ + res.first->value = mapped; return res; \ }) \ \ STC_INLINE ctype##_##X##_iter_t \ @@ -375,8 +375,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; ctype##_##X##_reserve_expand(self); \ ctype##_bucket_t b = ctype##_##X##_bucket(self, &rawKey); \ ctype##_##X##_result_t res = {&self->table[b.idx], !self->_hashx[b.idx]}; \ - if (res.inserted) { \ - res.item->key = keyFromRaw(rawKey); \ + if (res.second) { \ + res.first->key = keyFromRaw(rawKey); \ self->_hashx[b.idx] = (uint8_t) b.hx; \ ++self->size; \ } \ -- cgit v1.2.3