From d0ce3effae99478bd593011795b4184d4fddaed4 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 10 Jun 2022 16:04:22 +0200 Subject: Small simplifications in cmap.h --- examples/mapmap.c | 22 +++++++++------------- include/stc/cmap.h | 6 +++--- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/examples/mapmap.c b/examples/mapmap.c index 2ea8fa61..7cdc2ba0 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -1,11 +1,15 @@ -// unordered_map>: +// create a structure like: std::map>: + #include + +// People: std::map #define i_type People #define i_key_str #define i_val_str #define i_keydrop(p) (printf("kdrop: %s\n", cstr_str(p)), cstr_drop(p)) // override #include +// Departments: std::map #define i_type Departments #define i_key_str #define i_val_bind People @@ -16,14 +20,6 @@ // #define i_valdrop People_drop #include -#define i_type Stack -#define i_val_bind People_value -// Shorthand for: -// #define i_val People_value (pair of cstr) -// #define i_cmp People_value_cmp -// #define i_valclone People_value_clone -// #define i_valdrop People_value_drop -#include void add(Departments* deps, const char* name, const char* email, const char* dep) { @@ -63,10 +59,10 @@ int main(void) printf("%s: %s - %s\n", cstr_str(&i.ref->first), cstr_str(_.name), cstr_str(_.email)); puts(""); - printf("found: %d\n", contains(&map, "Nick Denton")); - printf("found: %d\n", contains(&map, "Patrick Dust")); - printf("found: %d\n", contains(&map, "Dennis Kay")); - printf("found: %d\n", contains(&map, "Serena Bath")); + printf("found Nick Denton: %d\n", contains(&map, "Nick Denton")); + printf("found Patrick Dust: %d\n", contains(&map, "Patrick Dust")); + printf("found Dennis Kay: %d\n", contains(&map, "Dennis Kay")); + printf("found Serena Bath: %d\n", contains(&map, "Serena Bath")); puts("Done"); } } diff --git a/include/stc/cmap.h b/include/stc/cmap.h index e193a8d0..9725de70 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -106,7 +106,7 @@ STC_INLINE bool _cx_memb(_empty)(_cx_self m) { return m.size == 0; } STC_INLINE size_t _cx_memb(_size)(_cx_self m) { return m.size; } STC_INLINE size_t _cx_memb(_bucket_count)(_cx_self map) { return map.bucket_count; } STC_INLINE size_t _cx_memb(_capacity)(_cx_self map) - { return map.bucket_count ? (size_t)((map.bucket_count - 2)*map.max_load_factor) : 0u; } + { return (size_t)(map.bucket_count*map.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]; } @@ -334,8 +334,8 @@ _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr) { STC_DEF _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_rawkey rkey) { bool nomem = false; - if (self->size + 1 >= (i_size)(self->bucket_count*self->max_load_factor)) - nomem = !_cx_memb(_reserve)(self, ((size_t)self->size*3 >> 1) + 4); + if (self->size >= (i_size)(self->bucket_count*self->max_load_factor)) + nomem = !_cx_memb(_reserve)(self, self->size*3/2); chash_bucket_t b = _cx_memb(_bucket_)(self, &rkey); _cx_result res = {&self->table[b.idx], !self->_hashx[b.idx], nomem}; if (res.inserted) { -- cgit v1.2.3