From 8f57f3d331de4cb4aa7d06862c2de3424eb1ba5b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 22 Apr 2022 12:20:31 +0200 Subject: Readded push()/emplace() to all containers missing them. Made _hash function required for i_key_bind, _eq is derived from _cmp. --- docs/cdeq_api.md | 2 ++ docs/cmap_api.md | 18 ++++++++++-------- docs/cset_api.md | 1 + docs/csmap_api.md | 3 +-- docs/csset_api.md | 1 + examples/box.c | 4 ++++ examples/city.c | 7 +++++-- examples/person_arc.c | 4 ++++ examples/rawptr_elements.c | 4 ++-- examples/vikings.c | 7 ++++--- include/stc/cdeq.h | 15 +++++++++++---- include/stc/clist.h | 4 ++++ include/stc/cmap.h | 11 +++++------ include/stc/cqueue.h | 4 ---- include/stc/csmap.h | 12 +++++------- include/stc/template.h | 5 +---- 16 files changed, 60 insertions(+), 42 deletions(-) diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 3cd4fece..aa528aa2 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -51,7 +51,9 @@ cdeq_X_value* cdeq_X_emplace_front(cdeq_X* self, i_valraw raw); void cdeq_X_pop_front(cdeq_X* self); cdeq_X_value* cdeq_X_push_back(cdeq_X* self, i_val value); +cdeq_X_value* cdeq_X_push(cdeq_X* self, i_val value); // alias for push_back() cdeq_X_value* cdeq_X_emplace_back(cdeq_X* self, i_valraw raw); +cdeq_X_value* cdeq_X_emplace(cdeq_X* self, i_valraw raw); // alias for emplace_back() void cdeq_X_pop_back(cdeq_X* self); cdeq_X_iter cdeq_X_insert(cdeq_X* self, size_t idx, i_val value); // move value diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 69257779..645b0bd0 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -67,11 +67,10 @@ cmap_X_iter cmap_X_find(const cmap_X* self, i_keyraw rkey); 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_push(cmap_X* self, i_key key, i_val mapped); // alias for insert 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 -cmap_X_result cmap_X_put_raw(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // alias for emplace_or_assign 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 @@ -276,8 +275,9 @@ typedef struct { #define Viking_init() ((Viking){cstr_null, cstr_null}) -static inline bool Viking_eq(const Viking* a, const Viking* b) { - return cstr_equals_s(a->name, b->name) && cstr_equals_s(a->country, b->country); +static inline int Viking_cmp(const Viking* a, const Viking* b) { + int c = cstr_cmp(&a->name, &b->name); + return c ? c : cstr_cmp(&a->country, &b->country); } static inline uint32_t Viking_hash(const Viking* a, int ignored) { @@ -298,7 +298,7 @@ static inline void Viking_drop(Viking* vk) { #define i_key_bind Viking #define i_val int // i_key_bind auto-binds: -// #define i_eq Viking_eq +// #define i_cmp Viking_cmp // #define i_hash Viking_hash // #define i_keyfrom Viking_clone // #define i_keydrop Viking_drop @@ -361,8 +361,10 @@ static inline uint64_t RViking_hash(const RViking* raw, size_t ignore) { uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); return hash; } -static inline bool RViking_eq(const RViking* rx, const RViking* ry) { - return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; + +static inline int RViking_cmp(const RViking* rx, const RViking* ry) { + int c = strcmp(rx->name, ry->name); + return c ? c : strcmp(rx->country, ry->country); } static inline Viking Viking_from(RViking raw) { @@ -379,7 +381,7 @@ static inline RViking Viking_toraw(const Viking* vk) { #define i_keyraw RViking // i_key_bind macro will make these functions auto-bind: // #define i_hash RViking_hash -// #define i_eq RViking_eq +// #define i_cmp RViking_cmp // #define i_keyfrom Viking_from // uses _from because i_keyraw is defined // #define i_keyto Viking_toraw // #define i_keydrop Viking_drop diff --git a/docs/cset_api.md b/docs/cset_api.md index e429c5ae..d9b412da 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -46,6 +46,7 @@ cset_X_value* cset_X_get_mut(cset_X* self, i_keyraw rkey); cset_X_iter cset_X_find(const cset_X* self, i_keyraw rkey); cset_X_result cset_X_insert(cset_X* self, i_key key); +cset_X_result cset_X_push(cset_X* self, i_key key); // alias for insert. cset_X_result cset_X_emplace(cset_X* self, i_keyraw rkey); size_t cset_X_erase(cset_X* self, i_keyraw rkey); // return 0 or 1 diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 012a7d58..e73e6562 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -63,11 +63,10 @@ csmap_X_value* csmap_X_back(const csmap_X* self); csmap_X_result csmap_X_insert(csmap_X* self, i_key key, i_val mapped); // no change if key in map csmap_X_result csmap_X_insert_or_assign(csmap_X* self, i_key key, i_val mapped); // always update mapped -csmap_X_result csmap_X_put(csmap_X* self, i_key key, i_val mapped); // alias for insert_or_assign() +csmap_X_result csmap_X_push(csmap_X* self, i_key key, i_val mapped); // alias for insert() csmap_X_result csmap_X_emplace(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map csmap_X_result csmap_X_emplace_or_assign(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped -csmap_X_result csmap_X_put_raw(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // alias for emplace_or_assign size_t csmap_X_erase(csmap_X* self, i_keyraw rkey); csmap_X_iter csmap_X_erase_at(csmap_X* self, csmap_X_iter it); // returns iter after it diff --git a/docs/csset_api.md b/docs/csset_api.md index 0ce8e811..57d23ee6 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -42,6 +42,7 @@ csset_X_value* csset_X_find_it(const csset_X* self, i_keyraw rkey, csset_X csset_X_iter csset_X_lower_bound(const csset_X* self, i_keyraw rkey); // find closest entry >= rkey csset_X_result csset_X_insert(csset_X* self, i_key key); +csset_X_result csset_X_push(csset_X* self, i_key key); // alias for insert() csset_X_result csset_X_emplace(csset_X* self, i_keyraw rkey); size_t csset_X_erase(csset_X* self, i_keyraw rkey); diff --git a/examples/box.c b/examples/box.c index d2d98218..4a43b149 100644 --- a/examples/box.c +++ b/examples/box.c @@ -7,6 +7,10 @@ Person Person_new(const char* name, const char* last) { return (Person){.name = cstr_from(name), .last = cstr_from(last)}; } +uint64_t Person_hash(const Person* a, size_t n) { + return cstr_hash(&a->name, 0) ^ cstr_hash(&a->last, 0); +} + int Person_cmp(const Person* a, const Person* b) { int c = cstr_cmp(&a->name, &b->name); return c ? c : cstr_cmp(&a->last, &b->last); diff --git a/examples/city.c b/examples/city.c index 16c9b6f1..0e1cbe96 100644 --- a/examples/city.c +++ b/examples/city.c @@ -1,7 +1,6 @@ #include -typedef struct -{ +typedef struct { cstr name; cstr country; float lat, lon; @@ -13,6 +12,10 @@ static inline int City_cmp(const City* a, const City* b) { return c ? c : cstr_cmp(&a->country, &b->country); } +static inline uint64_t City_hash(const City* a, size_t n) { + return cstr_hash(&a->name, 0) ^ cstr_hash(&a->country, 0); +} + static inline City City_clone(City c) { c.name = cstr_clone(c.name); c.country = cstr_clone(c.country); diff --git a/examples/person_arc.c b/examples/person_arc.c index 2fb51be5..9d245340 100644 --- a/examples/person_arc.c +++ b/examples/person_arc.c @@ -12,6 +12,10 @@ int Person_cmp(const Person* a, const Person* b) { return c ? c : cstr_cmp(&a->last, &b->last); } +uint64_t Person_hash(const Person* a, size_t n) { + return cstr_hash(&a->name, 0) ^ cstr_hash(&a->last, 0); +} + Person Person_clone(Person p) { p.name = cstr_clone(p.name); p.last = cstr_clone(p.last); diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c index b0878941..20231528 100644 --- a/examples/rawptr_elements.c +++ b/examples/rawptr_elements.c @@ -8,8 +8,8 @@ struct { double x, y; } typedef Point; #define i_key Point* #define i_keydrop(x) c_free(*(x)) #define i_keyfrom(x) c_new(Point, *(x)) -#define i_hash(x, n) c_default_hash(*(x), sizeof *(x)) -#define i_eq(x, y) c_memcmp_eq(*(x), *(y)) +#define i_hash(x, n) c_default_hash(*(x), sizeof **(x)) +#define i_cmp(x, y) memcmp(*(x), *(y), sizeof **(x)) // not good! #define i_tag pnt #include diff --git a/examples/vikings.c b/examples/vikings.c index b093ff9b..ff2fe8ab 100644 --- a/examples/vikings.c +++ b/examples/vikings.c @@ -22,8 +22,9 @@ uint64_t RViking_hash(const RViking* raw, size_t ignore) { uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); return hash; } -static inline bool RViking_eq(const RViking* rx, const RViking* ry) { - return strcmp(rx->name, ry->name) == 0 && strcmp(rx->country, ry->country) == 0; +static inline int RViking_cmp(const RViking* rx, const RViking* ry) { + int c = strcmp(rx->name, ry->name); + return c ? c : strcmp(rx->country, ry->country); } static inline Viking Viking_from(RViking raw) { // note: parameter is by value @@ -40,7 +41,7 @@ static inline RViking Viking_toraw(const Viking* vk) { #define i_val int // i_key_bind auto-binds these functions: // i_hash => Viking_hash -// i_eq => Viking_eq +// i_cmp => Viking_cmp // i_keyfrom => Viking_from // not _clone because i_keyraw is defined // i_keyto => Viking_toraw // i_keydrop => Viking_drop diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 7d6c0254..2c0df8c3 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -46,7 +46,7 @@ STC_API _cx_self _cx_memb(_with_capacity)(const size_t n); STC_API bool _cx_memb(_reserve)(_cx_self* self, const size_t n); STC_API void _cx_memb(_clear)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); -STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value); +STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value); STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self); #if !defined _i_queue #if !defined _i_no_clone @@ -71,8 +71,8 @@ STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self cx); #if !defined _i_no_emplace -STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_keyraw raw) - { return _cx_memb(_push_back)(self, i_keyfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_keyraw raw) + { return _cx_memb(_push)(self, i_keyfrom(raw)); } #endif STC_INLINE i_key _cx_memb(_value_clone)(i_key val) { return i_keyclone(val); } @@ -115,6 +115,9 @@ STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, const size_t idx) { assert(idx < cdeq_rep_(self)->size); return self->data + idx; } +STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) { + return _cx_memb(_push)(self, value); +} STC_INLINE _cx_iter _cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) { return _cx_memb(_insert_range_p)(self, self->data + idx, &value, &value + 1); @@ -151,6 +154,10 @@ STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_keyraw raw) { return _cx_memb(_push_front)(self, i_keyfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_keyraw raw) { + return _cx_memb(_push)(self, i_keyfrom(raw)); +} + STC_INLINE _cx_iter _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) { return _cx_memb(_emplace_range_p)(self, self->data + idx, arr, arr + n); @@ -287,7 +294,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t n) { } STC_DEF _cx_value* -_cx_memb(_push_back)(_cx_self* self, i_key value) { +_cx_memb(_push)(_cx_self* self, i_key value) { struct cdeq_rep* r = cdeq_rep_(self); if (_cdeq_nfront(self) + r->size == r->cap) { _cx_memb(_expand_right_half_)(self, r->size, 1); diff --git a/include/stc/clist.h b/include/stc/clist.h index 8247b04a..33aa6feb 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -122,6 +122,8 @@ STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_keyraw raw) { return _cx_memb(_push_front)(self, i_keyfrom(raw)); } STC_INLINE _cx_iter _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_keyraw raw) { return _cx_memb(_insert_at)(self, it, i_keyfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_keyraw raw) + { return _cx_memb(_push_back)(self, i_keyfrom(raw)); } #endif // !_i_no_emplace #endif // !_i_no_clone @@ -131,6 +133,8 @@ STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.last == NULL; STC_INLINE size_t _cx_memb(_count)(_cx_self cx) { return _clist_count((const clist_VOID*) &cx); } STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); } +STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, i_key value) + { return _cx_memb(_push_back)(self, value); } STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) { _cx_memb(_erase_after_)(self, self->last); } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return &self->last->next->value; } diff --git a/include/stc/cmap.h b/include/stc/cmap.h index c5172f3b..16041094 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -118,14 +118,8 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) 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_raw)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) - { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); } // alias #endif - STC_INLINE _cx_result - _cx_memb(_put)(_cx_self* self, i_key _key, i_val _mapped) - { return _cx_memb(_insert_or_assign)(self, _key, _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); @@ -183,6 +177,11 @@ _cx_memb(_insert)(_cx_self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) { return _res; } +STC_INLINE _cx_result +_cx_memb(_push)(_cx_self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) { + return _cx_memb(_insert)(self, _key _i_MAP_ONLY(, _mapped)); +} + STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_keyraw rkey) { _cx_size idx; diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index b07582e0..920f8eac 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -57,13 +57,9 @@ int main() { #define _i_prefix cqueue_ #endif #define _i_queue -#define _push_back _push -#define _emplace_back _emplace #define _pop_front _pop #include "cdeq.h" -#undef _push_back -#undef _emplace_back #undef _pop_front #undef _i_queue diff --git a/include/stc/csmap.h b/include/stc/csmap.h index eba9a591..700aa4c2 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -159,15 +159,8 @@ _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_raw)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) - { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); } // alias #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) - { return _cx_memb(_insert_or_assign)(self, _key, _mapped); } STC_INLINE const _cx_mapped* _cx_memb(_at)(const _cx_self* self, i_keyraw rkey) @@ -177,6 +170,11 @@ _cx_memb(_value_drop)(_cx_value* val) { { _cx_iter it; return &_cx_memb(_find_it)(self, rkey, &it)->second; } #endif // !_i_isset +STC_INLINE _cx_result +_cx_memb(_push)(_cx_self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) { + return _cx_memb(_insert)(self, _key _i_MAP_ONLY(, _mapped)); +} + STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_keyraw rkey) { _cx_iter it; diff --git a/include/stc/template.h b/include/stc/template.h index f614cd58..d47b06ec 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -123,10 +123,7 @@ #ifndef i_cmp #define i_cmp c_paste(i_keyraw, _cmp) #endif - #if !defined i_eq && (defined _i_ishash || c_option(c_eq)) - #define i_eq c_paste(i_keyraw, _eq) - #endif - #if !defined i_hash && (defined _i_ishash || c_option(c_hash)) + #if !defined i_hash #define i_hash c_paste(i_keyraw, _hash) #endif #endif -- cgit v1.2.3