From 9bb9a815ae49a23ffa7499ab779cbc8c79f57d56 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 4 Jan 2022 21:03:43 +0100 Subject: Fixed csview hash func. --- examples/new_vec.c | 39 ++++++++++++++++++--------------------- include/stc/ccommon.h | 2 +- include/stc/csview.h | 2 +- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/examples/new_vec.c b/examples/new_vec.c index b0382121..d1fe8728 100644 --- a/examples/new_vec.c +++ b/examples/new_vec.c @@ -35,25 +35,22 @@ int point_cmp(const Point* a, const Point* b) { int main() { - cvec_i32 vec = cvec_i32_init(); - cvec_i32_push_back(&vec, 123); - cvec_i32_drop(&vec); - - cvec_float fvec = cvec_float_init(); - cvec_float_push_back(&fvec, 123.3); - cvec_float_drop(&fvec); - - cvec_pnt pvec = cvec_pnt_init(); - cvec_pnt_push_back(&pvec, (Point){42, 14}); - cvec_pnt_push_back(&pvec, (Point){32, 94}); - cvec_pnt_push_back(&pvec, (Point){62, 81}); - cvec_pnt_sort(&pvec); - c_foreach (i, cvec_pnt, pvec) - printf(" (%d %d)", i.ref->x, i.ref->y); - puts(""); - cvec_pnt_drop(&pvec); - - cvec_str svec = cvec_str_init(); - cvec_str_emplace_back(&svec, "Hello, friend"); - cvec_str_drop(&svec); + c_auto (cvec_i32, vec) + c_auto (cvec_float, fvec) + c_auto (cvec_pnt, pvec) + c_auto (cvec_str, svec) + { + cvec_i32_push_back(&vec, 123); + cvec_float_push_back(&fvec, 123.3); + + cvec_pnt_push_back(&pvec, (Point){42, 14}); + cvec_pnt_push_back(&pvec, (Point){32, 94}); + cvec_pnt_push_back(&pvec, (Point){62, 81}); + cvec_pnt_sort(&pvec); + c_foreach (i, cvec_pnt, pvec) + printf(" (%d %d)", i.ref->x, i.ref->y); + puts(""); + + cvec_str_emplace_back(&svec, "Hello, friend"); + } } diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index dd2401a9..60e12433 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -121,8 +121,8 @@ STC_INLINE uint64_t c_strhash(const char *s) { if (h) while ((c = *s++)) h = (h << 10) - h + c; return _c_ROTL(h, 26) ^ h; } -// len >= 1 STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { + if (!len) return 1; const uint8_t *x = (const uint8_t*) key; uint64_t h = *x++; while (--len) h = (h << 10) - h + *x++; diff --git a/include/stc/csview.h b/include/stc/csview.h index 0995e698..d6492649 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -118,7 +118,7 @@ STC_INLINE int csview_cmp(const csview* x, const csview* y) { const int c = memcmp(x->str, y->str, m); return c ? c : x->size - y->size; } -#define csview_hash(xp, dummy) c_strhash((xp)->str) +#define csview_hash(xp, dummy) c_default_hash((xp)->str, (xp)->size) #define csview_eq(xp, yp) (!csview_cmp(xp, yp)) /* -------------------------- IMPLEMENTATION ------------------------- */ -- cgit v1.2.3