From 2da0a2c2d5fa154ea0151156b4e35a1ac8d50bc4 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 22 Feb 2021 18:53:09 +0100 Subject: Small changes. Added back csmap/cmap put() but now with Key,Mapped args (not RawKey,RawMapped). --- docs/cmap_api.md | 1 + docs/csmap_api.md | 1 + examples/inits.c | 22 +++++----------------- stc/cmap.h | 21 ++++++++++++--------- stc/csmap.h | 21 ++++++++++++--------- 5 files changed, 31 insertions(+), 35 deletions(-) diff --git a/docs/cmap_api.md b/docs/cmap_api.md index ec5d3dd1..0c1a2b97 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -64,6 +64,7 @@ bool cmap_X_contains(const cmap_X* self, RawKey rkey); cmap_X_result_t cmap_X_insert(cmap_X* self, Key key, Mapped mapped); // no change if key in map cmap_X_result_t cmap_X_insert_or_assign(cmap_X* self, Key key, Mapped mapped); // always update mapped +cmap_X_result_t cmap_X_put(cmap_X* self, RawKey rkey, RawMapped rmapped); // alias for insert_or_assign cmap_X_result_t cmap_X_emplace(cmap_X* self, RawKey rkey, RawMapped rmapped); // no change if rkey in map cmap_X_result_t cmap_X_emplace_or_assign(cmap_X* self, RawKey rkey, RawMapped rmapped); // always update rmapped void cmap_X_emplace_n(cmap_X* self, const cmap_X_rawvalue_t arr[], size_t size); diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 93341d39..48059077 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -57,6 +57,7 @@ bool csmap_X_contains(const csmap_X* self, RawKey rkey); csmap_X_result_t csmap_X_insert(csmap_X* self, Key key, Mapped mapped); // no change if key in map csmap_X_result_t csmap_X_insert_or_assign(csmap_X* self, Key key, Mapped mapped); // always update mapped +csmap_X_result_t csmap_X_put(csmap_X* self, RawKey rkey, RawMapped rmapped); // alias for insert_or_assign csmap_X_result_t csmap_X_emplace(csmap_X* self, RawKey rkey, RawMapped rmapped); // no change if rkey in map csmap_X_result_t csmap_X_emplace_or_assign(csmap_X* self, RawKey rkey, RawMapped rmapped); // always update rmapped void csmap_X_emplace_n(csmap_X* self, const csmap_X_rawvalue_t arr[], size_t size); diff --git a/examples/inits.c b/examples/inits.c index edcb4336..0c716fd1 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -5,8 +5,8 @@ #include #include -using_cmap(id, int, cstr, c_default_equals, c_default_hash, cstr_del, c_no_clone); // Map of int -> cstr -using_cmap_strkey(cnt, int); +using_cmap_strval(id, int); // Map of int => cstr +using_cmap_strkey(cnt, int); // Map of cstr => int typedef struct {int x, y;} ipair_t; inline static int ipair_compare(const ipair_t* a, const ipair_t* b) { @@ -46,7 +46,7 @@ int main(void) int year = 2020; cmap_id idnames = cmap_id_init(); - cmap_id_insert(&idnames, 100, cstr_from("Hello")); + cmap_id_emplace(&idnames, 100, "Hello"); cmap_id_insert(&idnames, 110, cstr_from("World")); cmap_id_insert(&idnames, 120, cstr_from_fmt("Howdy, -%d-", year)); @@ -80,13 +80,7 @@ int main(void) // CVEC PAIR - cvec_ip pairs1 = cvec_ip_init(); - c_emplace_items(&pairs1, cvec_ip, { - {5, 6}, - {3, 4}, - {1, 2}, - {7, 8}, - }); + c_init (cvec_ip, pairs1, { {5, 6}, {3, 4}, {1, 2}, {7, 8} }); cvec_ip_sort(&pairs1); c_foreach (i, cvec_ip, pairs1) @@ -96,13 +90,7 @@ int main(void) // CLIST PAIR - clist_ip pairs2 = clist_ip_init(); - c_emplace_items(&pairs2, clist_ip, { - {5, 6}, - {3, 4}, - {1, 2}, - {7, 8}, - }); + c_init (clist_ip, pairs2, { {5, 6}, {3, 4}, {1, 2}, {7, 8} }); clist_ip_sort(&pairs2); c_foreach (i, clist_ip, pairs2) diff --git a/stc/cmap.h b/stc/cmap.h index 203f0263..db15eeaa 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -261,14 +261,6 @@ typedef struct {size_t idx; uint32_t hx;} chash_bucket_t; } \ return res; \ } \ - STC_INLINE C##_##X##_result_t \ - C##_##X##_emplace_or_assign(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \ - C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ - if (res.second) *KEY_REF_##C(res.first) = keyFromRaw(rkey); \ - MAP_ONLY_##C( else mappedDel(&res.first->second); \ - res.first->second = mappedFromRaw(rmapped); ) \ - return res; \ - } \ STC_INLINE void \ C##_##X##_emplace_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \ for (size_t i=0; ifirst = key; else {keyDel(&key); mappedDel(&res.first->second);} \ + if (res.second) res.first->first = key; \ + else {keyDel(&key); mappedDel(&res.first->second);} \ res.first->second = mapped; return res; \ } \ + STC_INLINE C##_##X##_result_t \ + C##_##X##_put(C##_##X* self, Key k, Mapped m) \ + {return C##_##X##_insert_or_assign(self, k, m);} \ + STC_INLINE C##_##X##_result_t \ + C##_##X##_emplace_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \ + C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ + if (res.second) res.first->first = keyFromRaw(rkey); \ + else mappedDel(&res.first->second); \ + res.first->second = mappedFromRaw(rmapped); return res; \ + } \ STC_INLINE C##_##X##_mapped_t* \ C##_##X##_at(const C##_##X* self, RawKey rkey) { \ chash_bucket_t b = C##_##X##_bucket_(self, &rkey); \ diff --git a/stc/csmap.h b/stc/csmap.h index 7b6b04bd..c261801d 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -253,14 +253,6 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; } \ return res; \ } \ - STC_INLINE C##_##X##_result_t \ - C##_##X##_emplace_or_assign(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \ - C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ - if (res.second) *KEY_REF_##C(res.first) = keyFromRaw(rkey); \ - MAP_ONLY_##C( else mappedDel(&res.first->second); \ - res.first->second = mappedFromRaw(rmapped); ) \ - return res; \ - } \ STC_INLINE void \ C##_##X##_emplace_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \ for (size_t i=0; ifirst = key; else {keyDel(&key); mappedDel(&res.first->second);} \ + if (res.second) res.first->first = key; \ + else {keyDel(&key); mappedDel(&res.first->second);} \ res.first->second = mapped; return res; \ } \ + STC_INLINE C##_##X##_result_t \ + C##_##X##_put(C##_##X* self, Key k, Mapped m) \ + {return C##_##X##_insert_or_assign(self, k, m);} \ + STC_INLINE C##_##X##_result_t \ + C##_##X##_emplace_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \ + C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ + if (res.second) res.first->first = keyFromRaw(rkey); \ + else mappedDel(&res.first->second); \ + res.first->second = mappedFromRaw(rmapped); return res; \ + } \ STC_INLINE C##_##X##_mapped_t* \ C##_##X##_at(const C##_##X* self, RawKey rkey) { \ C##_##X##_iter_t it; \ -- cgit v1.2.3