From 765e46dba87a3fa7070ef7fb70d9be76e1d7a47b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 6 Feb 2021 23:11:03 +0100 Subject: Updated csmap.h to match api of cmap. Updated docs. --- docs/cmap_api.md | 22 ++++++++--------- docs/cset_api.md | 17 +++++++------ docs/csmap_api.md | 20 +++++++--------- docs/csset_api.md | 11 ++++----- examples/csmap_ex.c | 2 +- stc/csmap.h | 69 ++++++++++++++++++++++++++++------------------------- 6 files changed, 70 insertions(+), 71 deletions(-) diff --git a/docs/cmap_api.md b/docs/cmap_api.md index c57dc38f..a9bc9b3a 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -61,27 +61,25 @@ void cmap_X_swap(cmap_X* a, cmap_X* b); void cmap_X_del(cmap_X* self); -bool cmap_X_empty(cmap_X m); -size_t cmap_X_size(cmap_X m); -size_t cmap_X_bucket_count(cmap_X m); // num. of allocated buckets -size_t cmap_X_capacity(cmap_X m); // buckets * max_load_factor +bool cmap_X_empty(cmap_X map); +size_t cmap_X_size(cmap_X map); +size_t cmap_X_capacity(cmap_X map); // buckets * max_load_factor +size_t cmap_X_bucket_count(cmap_X map); // num. of allocated buckets -void cmap_X_push_n(cmap_X* self, const cmap_X_rawvalue_t arr[], size_t size); +cmap_X_iter_t cmap_X_find(const cmap_X* self, RawKey rkey); +bool cmap_X_contains(const cmap_X* self, RawKey rkey); +void cmap_X_push_n(cmap_X* self, const cmap_X_rawvalue_t arr[], size_t size); +cmap_X_result_t cmap_X_put(cmap_X* self, RawKey rkey, RawMapped rmapped); cmap_X_result_t cmap_X_emplace(cmap_X* self, RawKey rkey, RawMapped rmapped); // no change if rkey in map -cmap_X_result_t cmap_X_insert(cmap_X* self, cmap_X_rawvalue_t rval); // same, but takes rawvalue param -cmap_X_result_t cmap_X_insert_or_assign(cmap_X* self, RawKey rkey, RawMapped rmapped); -cmap_X_result_t cmap_X_put(cmap_X* self, RawKey rkey, RawMapped rmapped); // same as insert_or_assign() -cmap_X_result_t cmap_X_put_mapped(cmap_X* self, RawKey rkey, Mapped mapped); // same, but takes Mapped param +cmap_X_result_t cmap_X_insert(cmap_X* self, cmap_X_value_t val); // like emplace. +cmap_X_result_t cmap_X_insert_or_assign(cmap_X* self, Key key, Mapped mapped); // like put cmap_X_mapped_t* cmap_X_at(const cmap_X* self, RawKey rkey); // rkey must be in map size_t cmap_X_erase(cmap_X* self, RawKey rkey); void cmap_X_erase_entry(cmap_X* self, cmap_X_value_t* entry); cmap_X_iter_t cmap_X_erase_at(cmap_X* self, cmap_X_iter_t pos); -cmap_X_iter_t cmap_X_find(const cmap_X* self, RawKey rkey); -bool cmap_X_contains(const cmap_X* self, RawKey rkey); - cmap_X_iter_t cmap_X_begin(cmap_X* self); cmap_X_iter_t cmap_X_end(cmap_X* self); void cmap_X_next(cmap_X_iter_t* it); diff --git a/docs/cset_api.md b/docs/cset_api.md index 949171e9..03a0e765 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -43,23 +43,22 @@ void cset_X_swap(cset_X* a, cset_X* b); void cset_X_del(cset_X* self); -bool cset_X_empty(cset_X m); -size_t cset_X_size(cset_X m); -size_t cset_X_bucket_count(cset_X m); -size_t cset_X_capacity(cset_X m); +bool cset_X_empty(cset_X set); +size_t cset_X_size(cset_X set); +size_t cset_X_bucket_count(cset_X set); +size_t cset_X_capacity(cset_X set); -void cset_X_push_n(cset_X* self, const RawKey arr[], size_t size); +cset_X_iter_t cset_X_find(const cset_X* self, RawKey rkey); +bool cset_X_contains(const cset_X* self, RawKey rkey); +void cset_X_push_n(cset_X* self, const RawKey arr[], size_t size); cset_X_result_t cset_X_emplace(cset_X* self, RawKey rkey); -cset_X_result_t cset_X_insert(cset_X* self, RawKey rkey); +cset_X_result_t cset_X_insert(cset_X* self, Key key); size_t cset_X_erase(cset_X* self, RawKey rkey); void cset_X_erase_entry(cset_X* self, cset_X_key_t* key); cset_X_iter_t cset_X_erase_at(cset_X* self, cset_X_iter_t pos); -cset_X_iter_t cset_X_find(const cset_X* self, RawKey rkey); -bool cset_X_contains(const cset_X* self, RawKey rkey); - cset_X_iter_t cset_X_begin(cset_X* self); cset_X_iter_t cset_X_end(cset_X* self); void cset_X_next(cset_X_iter_t* it); diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 8400f044..45ce8fee 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -53,25 +53,23 @@ void csmap_X_clear(csmap_X* self); void csmap_X_swap(csmap_X* a, csmap_X* b); void csmap_X_del(csmap_X* self); -bool csmap_X_empty(csmap_X m); -size_t csmap_X_size(csmap_X m); +bool csmap_X_empty(csmap_X map); +size_t csmap_X_size(csmap_X map); -void csmap_X_push_n(csmap_X* self, const csmap_X_rawvalue_t arr[], size_t size); +csmap_X_iter_t csmap_X_find(const csmap_X* self, RawKey rkey); +csmap_X_value_t* csmap_X_find_it(const csmap_X* self, RawKey rkey, csmap_X_iter_t* out); // return NULL if not found +bool csmap_X_contains(const csmap_X* self, RawKey rkey); +void csmap_X_push_n(csmap_X* self, const csmap_X_rawvalue_t arr[], size_t size); +csmap_X_result_t csmap_X_put(csmap_X* self, RawKey rkey, RawMapped rmapped); // like std::map::operator[] csmap_X_result_t csmap_X_emplace(csmap_X* self, RawKey rkey, RawMapped rmapped); // no change if rkey in map -csmap_X_result_t csmap_X_insert(csmap_X* self, csmap_X_rawvalue_t rval); // same, just different param -csmap_X_result_t csmap_X_insert_or_assign(csmap_X* self, RawKey rkey, RawMapped rmapped); -csmap_X_result_t csmap_X_put(csmap_X* self, RawKey rkey, RawMapped rmapped); // same as insert_or_assign() -csmap_X_result_t csmap_X_put_mapped(csmap_X* self, RawKey rkey, Mapped mapped); // same, different param +csmap_X_result_t csmap_X_insert(csmap_X* self, csmap_X_value_t val); // like emplace +csmap_X_result_t csmap_X_insert_or_assign(csmap_X* self, Key key, Mapped mapped); // like put csmap_X_mapped_t* csmap_X_at(const csmap_X* self, RawKey rkey); // rkey must be in map. size_t csmap_X_erase(csmap_X* self, RawKey rkey); csmap_X_iter_t csmap_X_erase_at(csmap_X* self, csmap_X_iter_t pos); -csmap_X_iter_t csmap_X_find(const csmap_X* self, RawKey rkey); -csmap_X_value_t* csmap_X_find_it(const csmap_X* self, RawKey rkey, csmap_X_iter_t* out); // return NULL if not found -bool csmap_X_contains(const csmap_X* self, RawKey rkey); - csmap_X_iter_t csmap_X_begin(csmap_X* self); csmap_X_iter_t csmap_X_end(csmap_X* self); void csmap_X_next(csmap_X_iter_t* it); diff --git a/docs/csset_api.md b/docs/csset_api.md index af276532..33eecd1b 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -41,18 +41,17 @@ void csset_X_del(csset_X* self); bool csset_X_empty(csset_X m); size_t csset_X_size(csset_X m); -void csset_X_push_n(csset_X* self, const RawKey arr[], size_t size); +csset_X_iter_t csset_X_find(const csset_X* self, RawKey rkey); +csset_X_value_t* csset_X_find_it(const csset_X* self, RawKey rkey, csset_X_iter_t* out); +bool csset_X_contains(const csset_X* self, RawKey rkey); +void csset_X_push_n(csset_X* self, const RawKey arr[], size_t size); csset_X_result_t csset_X_emplace(csset_X* self, RawKey rkey); -csset_X_result_t csset_X_insert(csset_X* self, RawKey rkey); +csset_X_result_t csset_X_insert(csset_X* self, Key key); size_t csset_X_erase(csset_X* self, RawKey rkey); csset_X_iter_t csset_X_erase_at(csset_X* self, csset_X_iter_t pos); -csset_X_iter_t csset_X_find(const csset_X* self, RawKey rkey); -csset_X_value_t* csset_X_find_it(const csset_X* self, RawKey rkey, csset_X_iter_t* out); -bool csset_X_contains(const csset_X* self, RawKey rkey); - csset_X_iter_t csset_X_begin(csset_X* self); csset_X_iter_t csset_X_end(csset_X* self); void csset_X_next(csset_X_iter_t* it); diff --git a/examples/csmap_ex.c b/examples/csmap_ex.c index 7b3692ed..f26c0fc0 100644 --- a/examples/csmap_ex.c +++ b/examples/csmap_ex.c @@ -13,7 +13,7 @@ int main(int argc, char **argv) csmap_i map = csmap_i_init(); time_t seed = time(NULL); - size_t n = 10000000; + size_t n = 1000000; uint64_t mask = (1ull << 28) - 1; csmap_i_iter_t it; diff --git a/stc/csmap.h b/stc/csmap.h index fb2d4b2b..b765d1c1 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -225,46 +225,52 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* data[]; }; } \ \ STC_API C##_##X##_result_t \ - C##_##X##_insert_key(C##_##X* self, RawKey rkey); \ + C##_##X##_insert_entry_(C##_##X* self, RawKey rkey); \ +\ \ STC_INLINE C##_##X##_result_t \ - C##_##X##_emplace(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped) ) { \ - C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \ - MAP_ONLY_##C( if (res.second) res.first->second = mappedFromRaw(rmapped); ) \ + C##_##X##_emplace(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \ + C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ + if (res.second) { \ + *KEY_REF_##C(res.first) = keyFromRaw(rkey); \ + MAP_ONLY_##C(res.first->second = mappedFromRaw(rmapped);) \ + } \ return res; \ } \ STC_INLINE C##_##X##_result_t \ - C##_##X##_insert(C##_##X* self, C##_##X##_rawvalue_t raw) { \ - return SET_ONLY_##C( C##_##X##_insert_key(self, raw) ) \ - MAP_ONLY_##C( C##_##X##_emplace(self, raw.first, raw.second) ); \ + C##_##X##_put(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \ + C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ + if (res.second) *KEY_REF_##C(res.first) = keyFromRaw(rkey); \ + MAP_ONLY_##C( else mappedDel(&res.first->second); \ + res.first->second = mappedFromRaw(rmapped); ) \ + return res; \ } \ STC_INLINE void \ C##_##X##_push_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \ - for (size_t i=0; isecond); \ - res.first->second = mappedFromRaw(rmapped); return res; \ - } \ - STC_INLINE C##_##X##_result_t \ - C##_##X##_insert_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \ - return C##_##X##_put(self, rkey, rmapped); \ - } \ STC_INLINE C##_##X##_result_t \ - C##_##X##_put_mapped(C##_##X* self, RawKey rkey, Mapped mapped) { \ - C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \ - if (!res.second) mappedDel(&res.first->second); \ - res.first->second = mapped; return res; \ + C##_##X##_insert(C##_##X* self, C##_##X##_value_t val) { \ + C##_##X##_result_t res = C##_##X##_insert_entry_(self, keyToRaw(KEY_REF_##C(&val))); \ + if (res.second) *res.first = val; else C##_##X##_value_del(&val); \ + return res; \ } \ - STC_INLINE C##_##X##_mapped_t* \ - C##_##X##_at(const C##_##X* self, RawKey rkey) { \ - C##_##X##_iter_t it; \ - return &C##_##X##_find_it(self, rkey, &it)->second; \ - }) \ +\ + MAP_ONLY_##C( \ + STC_INLINE C##_##X##_result_t \ + C##_##X##_insert_or_assign(C##_##X* self, Key key, Mapped mapped) { \ + C##_##X##_result_t res = C##_##X##_insert_entry_(self, keyToRaw(&key)); \ + if (res.second) res.first->first = key; else keyDel(&key); \ + mappedDel(&res.first->second); res.first->second = mapped; \ + return res; \ + } \ + STC_INLINE C##_##X##_mapped_t* \ + C##_##X##_at(const C##_##X* self, RawKey rkey) { \ + C##_##X##_iter_t it; \ + return &C##_##X##_find_it(self, rkey, &it)->second; \ + }) \ \ STC_API C##_##X##_value_t* C##_##X##_front(C##_##X* self); \ STC_API C##_##X##_value_t* C##_##X##_back(C##_##X* self); \ @@ -413,7 +419,7 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; } \ \ static inline C##_##X##_size_t \ - C##_##X##_insert_key_i_(C##_##X* self, C##_##X##_size_t tn, const C##_##X##_rawkey_t* rkey, C##_##X##_result_t* res) { \ + C##_##X##insert_entry_i_(C##_##X* self, C##_##X##_size_t tn, const C##_##X##_rawkey_t* rkey, C##_##X##_result_t* res) { \ C##_##X##_size_t up[64], it = tn; \ C##_##X##_node_t* d = self->data; \ int c, top = 0, dir = 0; \ @@ -425,7 +431,6 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; it = d[it].link[dir]; \ } \ it = C##_##X##_node_new_(self, 1); d = self->data; \ - *KEY_REF_##C(&d[it].value) = keyFromRaw(*rkey); \ res->first = &d[it].value, res->second = true; \ if (top == 0) return it; \ d[up[top - 1]].link[dir] = it; \ @@ -439,9 +444,9 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; } \ \ STC_DEF C##_##X##_result_t \ - C##_##X##_insert_key(C##_##X* self, RawKey rkey) { \ + C##_##X##_insert_entry_(C##_##X* self, RawKey rkey) { \ C##_##X##_result_t res = {NULL, false}; \ - C##_##X##_size_t tn = C##_##X##_insert_key_i_(self, (C##_##X##_size_t) _csmap_rep(self)->root, &rkey, &res); \ + C##_##X##_size_t tn = C##_##X##insert_entry_i_(self, (C##_##X##_size_t) _csmap_rep(self)->root, &rkey, &res); \ _csmap_rep(self)->root = tn; \ _csmap_rep(self)->size += res.second; \ return res; \ -- cgit v1.2.3