From e38e9164d816bf1ee1f1dcc40f69ac158562fb1a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 20 Dec 2021 19:52:51 +0100 Subject: Some small addition to constness in API. Updated docs. --- docs/cmap_api.md | 4 ++-- docs/csmap_api.md | 2 +- docs/cvec_api.md | 6 ++++-- examples/vikings.c | 14 +++++++------- include/stc/cmap.h | 2 +- include/stc/csmap.h | 2 +- include/stc/cstack.h | 2 +- include/stc/cvec.h | 9 +++++---- 8 files changed, 22 insertions(+), 19 deletions(-) diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 4bb367f8..8531eb82 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -391,8 +391,8 @@ int main() }); Vikings_emplace_or_assign(&vikings, (RViking){"Bjorn", "Sweden"}, 10); - Vikings_value *einar = Vikings_find(&vikings, (RViking){"Einar", "Norway"}).ref; - if (einar) einar->second += 3; // add 3 hp points to Einar + Vikings_value *v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"}); + if (v) v->second += 3; // add 3 hp points to Einar c_forpair (viking, health, Vikings, vikings) { printf("%s of %s has %d hp\n", _.viking.name.str, _.viking.country.str, _.health); diff --git a/docs/csmap_api.md b/docs/csmap_api.md index c4f0ff16..a6e06b54 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -45,7 +45,7 @@ void csmap_X_drop(csmap_X* self); size_t csmap_X_size(csmap_X map); bool csmap_X_empty(csmap_X map); -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. 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/docs/cvec_api.md b/docs/cvec_api.md index b9d4d2d5..29a3efcb 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -56,15 +56,17 @@ cvec_X_iter cvec_X_bsearch_in(cvec_X_iter i1, cvec_X_iter i2, i_valraw r cvec_X_value* cvec_X_front(const cvec_X* self); cvec_X_value* cvec_X_back(const cvec_X* self); -cvec_X_value* cvec_X_push_back(cvec_X* self, i_val value); cvec_X_value* cvec_X_emplace_back(cvec_X* self, i_valraw raw); +cvec_X_value* cvec_X_push_back(cvec_X* self, i_val value); void cvec_X_pop_back(cvec_X* self); +cvec_X_value* cvec_X_emplace(cvec_X* self, i_valraw raw); // alias for emplace_back +cvec_X_value* cvec_X_push(cvec_X* self, i_val value); // alias for push_back +void cvec_X_pop(cvec_X* self); // alias for pop_back cvec_X_iter cvec_X_insert(cvec_X* self, size_t idx, i_val value); // move value cvec_X_iter cvec_X_insert_n(cvec_X* self, size_t idx, const i_val[] arr, size_t n); // move arr values cvec_X_iter cvec_X_insert_at(cvec_X* self, cvec_X_iter it, i_val value); // move value -cvec_X_iter cvec_X_emplace(cvec_X* self, size_t idx, i_valraw raw); cvec_X_iter cvec_X_emplace_n(cvec_X* self, size_t idx, const i_valraw[] arr, size_t n); cvec_X_iter cvec_X_emplace_at(cvec_X* self, cvec_X_iter it, i_valraw raw); cvec_X_iter cvec_X_emplace_range(cvec_X* self, cvec_X_iter it, diff --git a/examples/vikings.c b/examples/vikings.c index c4e86c73..7a953059 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -39,11 +39,11 @@ static inline RViking Viking_toraw(const Viking* vk) { #define i_keyraw RViking #define i_val int // i_key_bind auto-binds these functions: -// #define i_hash Viking_hash -// #define i_eq Viking_eq -// #define i_keyfrom Viking_from // uses _from (not _clone) because i_keyraw is defined -// #define i_keyto Viking_toraw -// #define i_keydrop Viking_drop +// i_hash => Viking_hash +// i_eq => Viking_eq +// i_keyfrom => Viking_from // not _clone because i_keyraw is defined +// i_keyto => Viking_toraw +// i_keydrop => Viking_drop #include int main() @@ -58,8 +58,8 @@ int main() Vikings_emplace_or_assign(&vikings, bjorn, 10); RViking einar = {"Einar", "Norway"}; - Vikings_value *e = Vikings_find(&vikings, einar).ref; - e->second += 3; // add 3 hp points to Einar + Vikings_value* v = Vikings_get_mut(&vikings, einar); + v->second += 3; // add 3 hp points to Einar Vikings_emplace(&vikings, einar, 0).ref->second += 5; // add 5 more to Einar c_forpair (viking, hp, Vikings, vikings) { diff --git a/include/stc/cmap.h b/include/stc/cmap.h index c78c2472..b7529871 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -121,7 +121,7 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) return _cx_memb(_insert_or_assign)(self, key, mapped); } - STC_INLINE _cx_mapped* + STC_INLINE const _cx_mapped* _cx_memb(_at)(const _cx_self* self, i_keyraw rkey) { chash_bucket_t b = _cx_memb(_bucket_)(self, &rkey); assert(self->_hashx[b.idx]); diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 6ba220ab..04dc895d 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -156,7 +156,7 @@ _cx_memb(_value_drop)(_cx_value* val) { _cx_memb(_put)(_cx_self* self, i_key key, i_val mapped) { return _cx_memb(_insert_or_assign)(self, key, mapped); } - STC_INLINE _cx_mapped* + 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; } #endif diff --git a/include/stc/cstack.h b/include/stc/cstack.h index d43afe29..fa74494d 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -91,7 +91,7 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) { *vp = val; return vp; } -STC_INLINE _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) { assert(idx < self->size); return self->data + idx; } #if !c_option(c_no_clone) diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 01143d25..92f6afd6 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -104,16 +104,14 @@ STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); } STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) { return _cx_memb(_push_back)(self, i_valfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_valraw raw) + { return _cx_memb(_push_back)(self, i_valfrom(raw)); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } STC_INLINE _cx_iter -_cx_memb(_emplace)(_cx_self* self, const size_t idx, i_valraw raw) { - return _cx_memb(_emplace_range_p)(self, self->data + idx, &raw, &raw + 1); -} -STC_INLINE _cx_iter _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_rawvalue arr[], const size_t n) { return _cx_memb(_emplace_range_p)(self, self->data + idx, arr, arr + n); } @@ -135,8 +133,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 _cx_value* _cx_memb(_push)(_cx_self* self, i_val value) + { return _cx_memb(_push_back)(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 _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) -- cgit v1.2.3