From a1c3ca1e5b34715cde8ea1809b688d5fef9ad99f Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 30 Jul 2020 18:48:23 +0200 Subject: Small tweak. --- examples/inits.c | 5 +++++ examples/mapmap.c | 37 ++++++++++++++++++++++--------------- stc/cmap.h | 10 +++++----- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/examples/inits.c b/examples/inits.c index 0d3f8bd8..91973190 100644 --- a/examples/inits.c +++ b/examples/inits.c @@ -62,6 +62,11 @@ int main(void) { {"Norway", 100}, {"Denmark", 50}, {"Iceland", 10}, + {"Belgium", 10}, + {"Italy", 10}, + {"Germany", 10}, + {"Spain", 10}, + {"France", 10}, )); cmap_cnt_insert(&countries, "Sweden", 0)->value += 20; diff --git a/examples/mapmap.c b/examples/mapmap.c index 82f35cd3..14ccd9a9 100644 --- a/examples/mapmap.c +++ b/examples/mapmap.c @@ -1,26 +1,33 @@ #include #include +#include -static void test_destr(int* x) { - printf("destroy int: %d\n", *x); +static void my_str_destr(cstr_t* x) { + printf("destroy: %s\n", x->str); + cstr_destroy(x); } -declare_cmap(ii, int, int, test_destr); -declare_cmap(im, int, cmap_ii, cmap_ii_destroy); +declare_cmap_str(ss, cstr_t, my_str_destr); +declare_cmap_str(cfg, cmap_ss, cmap_ss_destroy); int main(void) { - cmap_im m = cmap_init; - cmap_ii init = cmap_init; - cmap_ii_put(&cmap_im_insert(&m, 100, init)->value, 2000, 200); - cmap_ii_put(&cmap_im_insert(&m, 100, init)->value, 2001, 201); - cmap_ii_put(&cmap_im_insert(&m, 100, init)->value, 2000, 400); // update - cmap_ii_put(&cmap_im_insert(&m, 110, init)->value, 2000, 500); - cmap_ii_put(&cmap_im_insert(&m, 120, init)->value, 3000, 600); + cmap_cfg config = cmap_init; + cmap_ss init = cmap_init; + cmap_ss_put(&cmap_cfg_insert(&config, "user", init)->value, "name", cstr_make("Joe")); + cmap_ss_put(&cmap_cfg_insert(&config, "user", init)->value, "groups", cstr_make("proj1,proj3")); + cmap_ss_put(&cmap_cfg_insert(&config, "group", init)->value, "proj1", cstr_make("Energy")); + cmap_ss_put(&cmap_cfg_insert(&config, "group", init)->value, "proj2", cstr_make("Windy")); + cmap_ss_put(&cmap_cfg_insert(&config, "group", init)->value, "proj3", cstr_make("Oil")); + cmap_ss_put(&cmap_cfg_insert(&config, "admin", init)->value, "employees", cstr_make("2302")); - c_foreach (i, cmap_im, m) - c_foreach (j, cmap_ii, i.item->value) - printf("%d: %d - %d\n", i.item->key, j.item->key, j.item->value); + cmap_ss_put(&cmap_cfg_insert(&config, "group", init)->value, "proj2", cstr_make("Wind")); // Update + puts(""); - cmap_im_destroy(&m); + c_foreach (i, cmap_cfg, config) + c_foreach (j, cmap_ss, i.item->value) + printf("%s: %s - %s (%u)\n", i.item->key.str, j.item->key.str, j.item->value.str, i.item->value.bucket_count); + + puts(""); + cmap_cfg_destroy(&config); } \ No newline at end of file diff --git a/stc/cmap.h b/stc/cmap.h index 7a58506e..80101a43 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -260,14 +260,14 @@ ctype##_##tag##_find(const ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey) return self->_hashx[idx] ? &self->table[idx] : NULL; \ } \ \ -static inline void ctype##_##tag##_reserveExpand_(ctype##_##tag* self) { \ - if (self->size + 1 >= self->bucket_count * self->max_load_factor) \ - ctype##_##tag##_reserve(self, 7 + self->size * 3 / 2); \ +static inline void ctype##_##tag##_reserve_expand(ctype##_##tag* self) { \ + if (self->size >= self->bucket_count * self->max_load_factor) \ + ctype##_##tag##_reserve(self, 4 + self->size * 3 / 2); \ } \ \ STC_API ctype##entry_##tag* \ ctype##_##tag##_put(ctype##_##tag* self, OPT_2_##ctype(ctype##_##tag##_rawkey_t rawKey, Value value)) { \ - ctype##_##tag##_reserveExpand_(self); \ + ctype##_##tag##_reserve_expand(self); \ uint32_t hx; \ size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \ ctype##entry_##tag* e = &self->table[idx]; \ @@ -285,7 +285,7 @@ ctype##_##tag##_put(ctype##_##tag* self, OPT_2_##ctype(ctype##_##tag##_rawkey_t OPT_1_##ctype( \ STC_API ctype##entry_##tag* \ ctype##_##tag##_insert(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey, Value initValue) { \ - ctype##_##tag##_reserveExpand_(self); \ + ctype##_##tag##_reserve_expand(self); \ uint32_t hx; \ size_t idx = ctype##_##tag##_bucket(self, &rawKey, &hx); \ ctype##entry_##tag* e = &self->table[idx]; \ -- cgit v1.2.3