From 10c2736823c2bbfd613846a5cc88a66bd7971f1b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 11 Sep 2020 23:18:57 +0200 Subject: Added at() to cmap. Added c_assert(), c_static_assert() in cdefs.h --- examples/demos.c | 5 ++--- stc/cdefs.h | 2 ++ stc/cmap.h | 8 ++++++++ stc/cvec.h | 9 +++++---- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/examples/demos.c b/examples/demos.c index 8ff72840..0fa6f5e2 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -123,8 +123,7 @@ void mapdemo1() cmap_ii nums = cmap_ini; cmap_ii_put(&nums, 8, 64); cmap_ii_put(&nums, 11, 121); - - printf("get 8: %d\n", cmap_ii_find(&nums, 8)->value); + printf("get 8: %d\n", *cmap_ii_at(&nums, 8)); cmap_ii_destroy(&nums); } @@ -140,7 +139,7 @@ void mapdemo2() cmap_si_put(&nums, "Groovy", 200); // overwrite previous // iterate the map: - for (cmap_si_iter_t i = cmap_si_begin(&nums), ie = cmap_si_end(&nums); i.item != ie.item; cmap_si_next(&i)) + for (cmap_si_iter_t i = cmap_si_begin(&nums); i.item != cmap_si_end(&nums).item; cmap_si_next(&i)) printf("long: %s: %d\n", i.item->key.str, i.item->value); // or rather use the short form: diff --git a/stc/cdefs.h b/stc/cdefs.h index 82b411aa..2682fd0d 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -57,6 +57,8 @@ #define c_new(T) ((T *) malloc(sizeof(T))) #define c_new_n(T, n) ((T *) malloc(sizeof(T) * (n))) +#define c_assert(cond, msg) assert((msg, cond)) +#define c_static_assert(cond, msg) typedef char static_assert_##msg[(cond) ? 1 : -1] #define c_max_alloca (512) #define c_swap(T, x, y) do { T __t = x; x = y; y = __t; } while (0) diff --git a/stc/cmap.h b/stc/cmap.h index 4d01ed8e..fdc7f7bf 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -218,6 +218,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; \ STC_API ctype##_##X##_result_t \ ctype##_##X##_insert_key_(ctype##_##X* self, ctype##_##X##_rawkey_t rawKey); \ + STC_API ctype##_bucket_t \ + ctype##_##X##_bucket(const ctype##_##X* self, const ctype##_##X##_rawkey_t* rawKeyPtr); \ \ STC_INLINE ctype##_##X##_result_t \ ctype##_##X##_emplace(ctype##_##X* self, CMAP_ARGS_##ctype(ctype##_##X##_rawkey_t rawKey, RawValue rawValue)) { \ @@ -236,6 +238,12 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; if (!res.inserted) valueDestroy(&res.item->value); \ res.item->value = valueFromRaw(rawValue); return res; \ } \ + STC_API ctype##_##X##_value_t* \ + ctype##_##X##_at(const ctype##_##X* self, ctype##_##X##_rawkey_t rawKey) { \ + ctype##_bucket_t b = ctype##_##X##_bucket(self, &rawKey); \ + c_assert(self->_hashx[b.idx], "cmap_at()"); \ + return &self->table[b.idx].value; \ + } \ STC_INLINE ctype##_##X##_result_t \ ctype##_##X##_putv(ctype##_##X* self, ctype##_##X##_rawkey_t rawKey, Value value) { \ ctype##_##X##_result_t res = ctype##_##X##_insert_key_(self, rawKey); \ diff --git a/stc/cvec.h b/stc/cvec.h index 27082369..ae1da05e 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -71,6 +71,8 @@ cvec_##X##_reserve(cvec_##X* self, size_t cap); \ STC_API void \ cvec_##X##_resize(cvec_##X* self, size_t size, Value fill_val); \ + STC_INLINE void \ + cvec_##X##_swap(cvec_##X* a, cvec_##X* b) {c_swap(Value*, a->data, b->data);} \ \ STC_INLINE cvec_##X \ cvec_##X##_with_size(size_t size, Value null_val) { \ @@ -154,10 +156,9 @@ STC_INLINE Value* \ cvec_##X##_back(cvec_##X* self) {return self->data + _cvec_size(self) - 1;} \ STC_INLINE Value* \ - cvec_##X##_at(cvec_##X* self, size_t i) {return self->data + i;} \ - STC_INLINE void \ - cvec_##X##_swap(cvec_##X* a, cvec_##X* b) { \ - c_swap(Value*, a->data, b->data); \ + cvec_##X##_at(cvec_##X* self, size_t i) { \ + c_assert(i < cvec_size(*self), "cvec_at()"); \ + return self->data + i; \ } \ \ STC_API int \ -- cgit v1.2.3