From 57de677b5b00e0aa75772d6b5fe6347edbe03ffd Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 16 Apr 2022 17:12:14 +0200 Subject: Added at_mut() to some containers. Added put_r() to cmap, csmap as alias for emplace_or_assign(). --- docs/cmap_api.md | 93 ++++++++++++++++++++++++++-------------------------- docs/csmap_api.md | 3 +- include/stc/cmap.h | 7 +++- include/stc/csmap.h | 6 ++++ include/stc/cstack.h | 2 ++ include/stc/cstr.h | 2 +- include/stc/cvec.h | 13 +++++--- 7 files changed, 72 insertions(+), 54 deletions(-) diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 4c945a25..493ee7cc 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -41,59 +41,60 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain ## Methods ```c -cmap_X cmap_X_init(void); -cmap_X cmap_X_with_capacity(size_t cap); -cmap_X cmap_X_clone(cmap_x map); - -void cmap_X_clear(cmap_X* self); -void cmap_X_copy(cmap_X* self, cmap_X other); -void cmap_X_max_load_factor(cmap_X* self, float max_load); // default: 0.85 -bool cmap_X_reserve(cmap_X* self, size_t size); -void cmap_X_shrink_to_fit(cmap_X* self); -void cmap_X_swap(cmap_X* a, cmap_X* b); -void cmap_X_drop(cmap_X* self); // destructor - -size_t cmap_X_size(cmap_X map); -size_t cmap_X_capacity(cmap_X map); // buckets * max_load_factor -bool cmap_X_empty(cmap_X map); -size_t cmap_X_bucket_count(cmap_X map); // num. of allocated buckets - -const cmap_X_mapped* cmap_X_at(const cmap_X* self, i_keyraw rkey); // rkey must be in map. -const cmap_X_value* cmap_X_get(const cmap_X* self, i_keyraw rkey); // const get -cmap_X_value* cmap_X_get_mut(cmap_X* self, i_keyraw rkey); // mutable get -bool cmap_X_contains(const cmap_X* self, i_keyraw rkey); -cmap_X_iter cmap_X_find(const cmap_X* self, i_keyraw rkey); // find element - -cmap_X_result cmap_X_insert(cmap_X* self, i_key key, i_val mapped); // no change if key in map -cmap_X_result cmap_X_insert_or_assign(cmap_X* self, i_key key, i_val mapped); // always update mapped -cmap_X_result cmap_X_put(cmap_X* self, i_key key, i_val mapped); // alias for insert_or_assign - -cmap_X_result cmap_X_emplace(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map -cmap_X_result cmap_X_emplace_or_assign(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped - -size_t cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1 -cmap_X_iter cmap_X_erase_at(cmap_X* self, cmap_X_iter it); // return iter after it -void cmap_X_erase_entry(cmap_X* self, cmap_X_value* entry); - -cmap_X_iter cmap_X_begin(const cmap_X* self); -cmap_X_iter cmap_X_end(const cmap_X* self); -void cmap_X_next(cmap_X_iter* it); - -cmap_X_value cmap_X_value_clone(cmap_X_value val); -cmap_X_raw cmap_X_value_toraw(cmap_X_value* pval); +cmap_X cmap_X_init(void); +cmap_X cmap_X_with_capacity(size_t cap); +cmap_X cmap_X_clone(cmap_x map); + +void cmap_X_clear(cmap_X* self); +void cmap_X_copy(cmap_X* self, cmap_X other); +void cmap_X_max_load_factor(cmap_X* self, float max_load); // default: 0.85 +bool cmap_X_reserve(cmap_X* self, size_t size); +void cmap_X_shrink_to_fit(cmap_X* self); +void cmap_X_swap(cmap_X* a, cmap_X* b); +void cmap_X_drop(cmap_X* self); // destructor + +size_t cmap_X_size(cmap_X map); +size_t cmap_X_capacity(cmap_X map); // buckets * max_load_factor +bool cmap_X_empty(cmap_X map); +size_t cmap_X_bucket_count(cmap_X map); // num. of allocated buckets + +const cmap_X_mapped* cmap_X_at(const cmap_X* self, i_keyraw rkey); // rkey must be in map +cmap_X_mapped* cmap_X_at_mut(cmap_X* self, i_keyraw rkey); // mutable at +const cmap_X_value* cmap_X_get(const cmap_X* self, i_keyraw rkey); // const get +cmap_X_value* cmap_X_get_mut(cmap_X* self, i_keyraw rkey); // mutable get +bool cmap_X_contains(const cmap_X* self, i_keyraw rkey); +cmap_X_iter cmap_X_find(const cmap_X* self, i_keyraw rkey); // find element + +cmap_X_result cmap_X_insert(cmap_X* self, i_key key, i_val mapped); // no change if key in map +cmap_X_result cmap_X_insert_or_assign(cmap_X* self, i_key key, i_val mapped); // always update mapped +cmap_X_result cmap_X_put(cmap_X* self, i_key key, i_val mapped); // alias for insert_or_assign + +cmap_X_result cmap_X_emplace(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map +cmap_X_result cmap_X_emplace_or_assign(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped + +size_t cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1 +cmap_X_iter cmap_X_erase_at(cmap_X* self, cmap_X_iter it); // return iter after it +void cmap_X_erase_entry(cmap_X* self, cmap_X_value* entry); + +cmap_X_iter cmap_X_begin(const cmap_X* self); +cmap_X_iter cmap_X_end(const cmap_X* self); +void cmap_X_next(cmap_X_iter* it); + +cmap_X_value cmap_X_value_clone(cmap_X_value val); +cmap_X_raw cmap_X_value_toraw(cmap_X_value* pval); ``` Helpers: ```c -uint64_t c_strhash(const char *str); // utility function +uint64_t c_strhash(const char *str); // utility function // hash template parameter functions: -uint64_t c_default_hash(const void *data, size_t len); // key is any integral type -uint64_t c_hash32(const void* data, size_t is4); // key is one 32-bit int -uint64_t c_hash64(const void* data, size_t is8); // key is one 64-bit int +uint64_t c_default_hash(const void *data, size_t len); // key is any integral type +uint64_t c_hash32(const void* data, size_t is4); // key is one 32-bit int +uint64_t c_hash64(const void* data, size_t is8); // key is one 64-bit int // equalto template parameter functions: -bool c_default_eq(const i_keyraw* a, const i_keyraw* b); // *a == *b -bool c_memcmp_eq(const i_keyraw* a, const i_keyraw* b); // !memcmp(a, b, sizeof *a) +bool c_default_eq(const i_keyraw* a, const i_keyraw* b); // *a == *b +bool c_memcmp_eq(const i_keyraw* a, const i_keyraw* b); // !memcmp(a, b, sizeof *a) ``` ## Types diff --git a/docs/csmap_api.md b/docs/csmap_api.md index e6f5515c..0b23878b 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -49,7 +49,8 @@ void csmap_X_drop(csmap_X* self); size_t csmap_X_size(csmap_X map); bool csmap_X_empty(csmap_X map); -const csmap_X_mapped* csmap_X_at(const csmap_X* self, i_keyraw rkey); // rkey must be in map. +const csmap_X_mapped* csmap_X_at(const csmap_X* self, i_keyraw rkey); // rkey must be in map +csmap_X_mapped* csmap_X_at_mut(csmap_X* self, i_keyraw rkey); // mutable at const csmap_X_value* csmap_X_get(const csmap_X* self, i_keyraw rkey); // return NULL if not found csmap_X_value* csmap_X_get_mut(csmap_X* self, i_keyraw rkey); // mutable get bool csmap_X_contains(const csmap_X* self, i_keyraw rkey); diff --git a/include/stc/cmap.h b/include/stc/cmap.h index af14cd1e..1a3e7944 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -110,10 +110,12 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } #ifndef _i_isset + STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped); #if !defined _i_no_clone && !defined _i_no_emplace STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped); + STC_INLINE _cx_result _cx_memb(_put_r)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) + { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); } #endif - STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped); STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, i_key _key, i_val _mapped) @@ -125,6 +127,9 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) assert(self->_hashx[b.idx]); return &self->table[b.idx].second; } + STC_INLINE _cx_mapped* + _cx_memb(_at_mut)(_cx_self* self, i_keyraw rkey) + { return (_cx_mapped*)_cx_memb(_at)(self, rkey); } #endif // !_i_isset #if !defined _i_no_clone diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 48dc053a..682f123b 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -158,6 +158,9 @@ _cx_memb(_value_drop)(_cx_value* val) { #ifndef _i_isset #if !defined _i_no_clone && !defined _i_no_emplace STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped); + STC_INLINE _cx_result + _cx_memb(_put_r)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) + { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); } #endif STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped); @@ -168,6 +171,9 @@ _cx_memb(_value_drop)(_cx_value* val) { STC_INLINE const _cx_mapped* _cx_memb(_at)(const _cx_self* self, i_keyraw rkey) { _cx_iter it; return &_cx_memb(_find_it)(self, rkey, &it)->second; } + STC_INLINE _cx_mapped* + _cx_memb(_at_mut)(_cx_self* self, i_keyraw rkey) + { _cx_iter it; return &_cx_memb(_find_it)(self, rkey, &it)->second; } #endif // !_i_isset STC_INLINE _cx_iter diff --git a/include/stc/cstack.h b/include/stc/cstack.h index caa934df..b33319c5 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -98,6 +98,8 @@ STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) { assert(idx < self->size); return self->data + idx; } +STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx) + { assert(idx < self->size); return self->data + idx; } #if !defined _i_no_clone #if !defined _i_no_emplace diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 1e93a0ef..8347778c 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -185,7 +185,7 @@ cstr_reserve(cstr* self, const size_t cap) { cstr_priv* p = _cstr_p(self); const size_t oldcap = p->cap; if (cap > oldcap) { - p = (cstr_priv*) c_realloc((oldcap != 0) & (p != &_cstr_nullrep) ? p : NULL, _cstr_opt_mem(cap)); + p = (cstr_priv*) c_realloc(((oldcap != 0) & (p != &_cstr_nullrep)) ? p : NULL, _cstr_opt_mem(cap)); self->str = p->chr; if (oldcap == 0) self->str[p->size = 0] = '\0'; p->cap = _cstr_opt_cap(cap); diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 6db8e3c7..52cac08a 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -134,11 +134,11 @@ STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_swap(_cx_s STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; } STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self) { return self->data + cvec_rep_(self)->size - 1; } +STC_INLINE void _cx_memb(_pop)(_cx_self* self) + { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdrop(p); } STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value) { return _cx_memb(_push)(self, value); } -STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) - { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdrop(p); } -STC_INLINE void _cx_memb(_pop)(_cx_self* self) { _cx_memb(_pop_back)(self); } +STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { return c_make(_cx_iter){self->data}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) @@ -195,8 +195,11 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) { STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const size_t idx) { - assert(idx < cvec_rep_(self)->size); - return self->data + idx; + assert(idx < cvec_rep_(self)->size); return self->data + idx; +} +STC_INLINE _cx_value* +_cx_memb(_at_mut)(_cx_self* self, const size_t idx) { + assert(idx < cvec_rep_(self)->size); return self->data + idx; } #if !c_option(c_no_cmp) -- cgit v1.2.3