From 7abea5ab04bffebbedfad7bd8d9c5c26170d19af Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 6 Jan 2023 11:40:41 +0100 Subject: Removed swap() function from all containers. Use safe c_SWAP() macro instead. --- docs/ccommon_api.md | 14 +++++++++++--- docs/cdeq_api.md | 1 - docs/cmap_api.md | 1 - docs/cset_api.md | 1 - docs/csmap_api.md | 1 - docs/csset_api.md | 1 - docs/cvec_api.md | 1 - include/stc/ccommon.h | 5 +++-- include/stc/cdeq.h | 1 - include/stc/cmap.h | 3 +-- include/stc/csmap.h | 1 - include/stc/cvec.h | 1 - misc/archived/csmap.h | 1 - misc/examples/lower_bound.c | 8 ++++---- 14 files changed, 19 insertions(+), 21 deletions(-) diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index bb9efac4..8c194a97 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -347,8 +347,16 @@ uint64_t crawstr_hash(const crawstr* x); ``` ### c_MALLOC, c_CALLOC, c_REALLOC, c_FREE -Memory allocator for the entire library. Macros can be overloaded by the user. +Memory allocator for the entire library. Macros can be overridden by the user. ### c_SWAP, c_ARRAYLEN -- **c_SWAP(type, x, y)**: Simple macro for swapping internals of two objects. -- **c_ARRAYLEN(array)**: Return number of elements in an array, e.g. `int array[] = {1, 2, 3, 4};` +- **c_SWAP(T, xp, yp)**: Safe macro for swapping internals of two objects of same type. +- **c_ARRAYLEN(array)**: Return number of elements in an array. +```c +cmap_int map1 = {0}, map2 = {0}; +... +c_SWAP(cmap_int, &map1, &map2); + +int array[] = {1, 2, 3, 4}; +size_t n = c_ARRAYLEN(array); +``` diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index e39a9d5d..584d382f 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -35,7 +35,6 @@ void cdeq_X_copy(cdeq_X* self, const cdeq_X* other); cdeq_X_iter cdeq_X_copy_range(cdeq_X* self, i_val* pos, const i_val* p1, const i_val* p2); bool cdeq_X_reserve(cdeq_X* self, size_t cap); void cdeq_X_shrink_to_fit(cdeq_X* self); -void cdeq_X_swap(cdeq_X* a, cdeq_X* b); void cdeq_X_drop(cdeq_X* self); // destructor bool cdeq_X_empty(const cdeq_X* self); diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 10bfb040..2c690d13 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -56,7 +56,6 @@ void cmap_X_copy(cmap_X* self, const cmap_X* other); float cmap_X_max_load_factor(const cmap_X* self); // default: 0.85f 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(const cmap_X* self); diff --git a/docs/cset_api.md b/docs/cset_api.md index aff8506a..e1d08a87 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -38,7 +38,6 @@ void cset_X_copy(cset_X* self, const cset_X* other); float cset_X_max_load_factor(const cset_X* self); // default: 0.85 bool cset_X_reserve(cset_X* self, size_t size); void cset_X_shrink_to_fit(cset_X* self); -void cset_X_swap(cset_X* a, cset_X* b); void cset_X_drop(cset_X* self); // destructor size_t cset_X_size(const cset_X* self); // num. of allocated buckets diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 20346735..687a6cab 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -49,7 +49,6 @@ csmap_X csmap_X_clone(csmap_x map); void csmap_X_clear(csmap_X* self); void csmap_X_copy(csmap_X* self, const csmap_X* other); -void csmap_X_swap(csmap_X* a, csmap_X* b); void csmap_X_drop(csmap_X* self); // destructor size_t csmap_X_size(const csmap_X* self); diff --git a/docs/csset_api.md b/docs/csset_api.md index e242b8e4..6276f486 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -35,7 +35,6 @@ csset_X csset_X_clone(csset_x set); void csset_X_clear(csset_X* self); void csset_X_copy(csset_X* self, const csset_X* other); -void csset_X_swap(csset_X* a, csset_X* b); void csset_X_drop(csset_X* self); // destructor size_t csset_X_size(const csset_X* self); diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 23722275..92629c8b 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -42,7 +42,6 @@ bool cvec_X_reserve(cvec_X* self, size_t cap); bool cvec_X_resize(cvec_X* self, size_t size, i_val null); cvec_X_iter cvec_X_insert_uninit(cvec_X* self, i_val* pos, size_t n); // return pos iter void cvec_X_shrink_to_fit(cvec_X* self); -void cvec_X_swap(cvec_X* a, cvec_X* b); void cvec_X_drop(cvec_X* self); // destructor bool cvec_X_empty(const cvec_X* self); diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 220b0037..6bc5a2a5 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -85,8 +85,9 @@ #define c_FREE(p) free(p) #endif -#define c_DELETE(T, ptr) do { T *_c_p = ptr; T##_drop(_c_p); c_FREE(_c_p); } while (0) -#define c_SWAP(T, x, y) do { T _c_t = x; x = y; y = _c_t; } while (0) +#define c_DELETE(T, ptr) do { T *_tp = ptr; T##_drop(_tp); c_FREE(_tp); } while (0) +#define c_SWAP(T, xp, yp) do { T *_xp = xp, *_yp = yp, \ + _tv = *_xp; *_xp = *_yp; *_yp = _tv; } while (0) #define c_ARRAYLEN(a) (sizeof (a)/sizeof *(a)) // x and y are i_keyraw* type, defaults to i_key*: diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 47d336f1..1fe52548 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -90,7 +90,6 @@ STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) { return self->_le STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* pval) { return i_keyto(pval); } -STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_SWAP(_cx_self, *a, *b); } 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 + self->_len - 1; } diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 20375051..df3ce6fc 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -115,7 +115,6 @@ STC_INLINE size_t _cx_memb(_size)(const _cx_self* map) { return map->size; STC_INLINE size_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* map) { return (size_t)((float)map->bucket_count * (i_max_load_factor)); } -STC_INLINE void _cx_memb(_swap)(_cx_self *map1, _cx_self *map2) {c_SWAP(_cx_self, *map1, *map2); } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } @@ -416,7 +415,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { m.table[b.idx] = *e; m._hashx[b.idx] = (uint8_t)b.hx; } - c_SWAP(_cx_self, *self, m); + c_SWAP(_cx_self, self, &m); } c_FREE(m._hashx); c_FREE(m.table); diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 0af55b2b..e5df0fe2 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -120,7 +120,6 @@ STC_API void _cx_memb(_next)(_cx_iter* it); STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return cx->size == 0; } STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; } STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; } -STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_SWAP(_cx_self, *a, *b); } STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { _cx_iter it; _cx_memb(_find_it)(self, rkey, &it); return it; } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 64876b9b..fbacd305 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -128,7 +128,6 @@ STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) { return self->_le STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* val) { return i_keyto(val); } -STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_SWAP(_cx_self, *a, *b); } 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 + self->_len - 1; } diff --git a/misc/archived/csmap.h b/misc/archived/csmap.h index 8b182e70..5c9fba6b 100644 --- a/misc/archived/csmap.h +++ b/misc/archived/csmap.h @@ -113,7 +113,6 @@ STC_API void _cx_memb(_next)(_cx_iter* it); STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.size == 0; } STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return cx.size; } -STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_SWAP(_cx_self, *a, *b); } STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_keyraw rkey) { _cx_iter it; _cx_memb(_find_it)(self, rkey, &it); return it; } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) diff --git a/misc/examples/lower_bound.c b/misc/examples/lower_bound.c index dde9d93a..8d048e7c 100644 --- a/misc/examples/lower_bound.c +++ b/misc/examples/lower_bound.c @@ -25,12 +25,12 @@ int main() key = 10; cvec_int_iter it1 = cvec_int_lower_bound(&vec, key); - if (res) + if (it1.ref) printf("Sorted Vec %3d: lower_bound: %d\n", key, *it1.ref); // 30 key = 600; cvec_int_iter it2 = cvec_int_binary_search(&vec, key); - if (res) + if (it2.ref) printf("Sorted Vec %d: bin. search: %d\n", key, *it2.ref); // 600 c_FOREACH (i, cvec_int, it1, it2) @@ -54,12 +54,12 @@ int main() key = 10; csset_int_iter it1 = csset_int_lower_bound(&set, key); - if (res) + if (it1.ref) printf("Sorted Set %3d: lower bound: %d\n", key, *it1.ref); // 30 key = 600; csset_int_iter it2 = csset_int_find(&set, key); - if (res) + if (it2.ref) printf("Sorted Set %d: find : %d\n", key, *it2.ref); // 600 c_FOREACH (i, csset_int, it1, it2) -- cgit v1.2.3