From 36d2e0960ad33ca6d194ca41a17f0834526173d0 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 16 Oct 2021 00:01:40 +0200 Subject: Changed c_hash_default() function. Was bad on long strings. Updated mapmap.c example. --- include/stc/ccommon.h | 2 +- include/stc/cmap.h | 31 ++++++++++--------------------- include/stc/cstr.h | 2 +- 3 files changed, 12 insertions(+), 23 deletions(-) (limited to 'include') diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index e3ccfffd..5480d9f2 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -111,7 +111,7 @@ #define c_default_del(ptr) ((void) (ptr)) -STC_API uint64_t c_default_hash(const void *key, size_t len); +STC_INLINE uint64_t c_default_hash(const void *key, size_t len); #define c_default_hash32(data, len_is_4) \ ((*(const uint32_t*)data * 0xc6a4a7935bd1e99d) >> 15) #define c_default_hash64(data, len_is_8) \ diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 3a860f39..1dc1fd90 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -213,34 +213,23 @@ cx_memb(_erase_at)(Self* self, cx_iter_t it) { /* -------------------------- IMPLEMENTATION ------------------------- */ -#if !defined(STC_HEADER) && !defined(CMAP_H_INCLUDED) || defined(i_imp) && i_imp == 2 - -STC_DEF uint64_t c_default_hash(const void *key, size_t len) { - const uint64_t m = 0xb5ad4eceda1ce2a9; - uint64_t k, h = m + len; - const uint8_t *p = (const uint8_t *)key, *end = p + (len & ~7ull); - for (; p != end; p += 8) { memcpy(&k, p, 8); h ^= m*k; } - switch (len & 7) { - case 7: h ^= (uint64_t) p[6] << 48; /* @fallthrough@ */ - case 6: h ^= (uint64_t) p[5] << 40; /* @fallthrough@ */ - case 5: h ^= (uint64_t) p[4] << 32; /* @fallthrough@ */ - case 4: h ^= (uint64_t) p[3] << 24; /* @fallthrough@ */ - case 3: h ^= (uint64_t) p[2] << 16; /* @fallthrough@ */ - case 2: h ^= (uint64_t) p[1] << 8; /* @fallthrough@ */ - case 1: h ^= (uint64_t) p[0]; h *= m; - } - return h ^ (h >> 15); -} -#endif // NON-TEMPLATED - #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) #ifndef CMAP_H_INCLUDED +#define _c_rotl(x, k) (x << (k) | x >> (8*sizeof(x) - (k))) +STC_INLINE uint64_t c_default_hash(const void *key, size_t len) { + const char* str = (const char*)key; + uint64_t h = 0xb5ad4eceda1ce2a9; + for (size_t i = 0; i < len; ++i) + h ^= (_c_rotl(h, 4) ^ (h << 13)) + str[i]; + return h; +} + //STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) // { uint64_t lo, hi; c_umul128(x, n, &lo, &hi); return hi; } #define fastrange_uint32_t(x, n) (uint32_t)((uint32_t)(x)*(uint64_t)(n) >> 32) #define chash_index_(h, entryPtr) ((entryPtr) - (h).table) -#endif +#endif // CMAP_H_INCLUDED STC_DEF Self cx_memb(_with_capacity)(size_t cap) { diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 23830d75..98b95e3d 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -68,11 +68,11 @@ STC_API char* c_strnstrn(const char* s, const char* needle, size_t sle STC_API int c_strncasecmp(const char* s1, const char* s2, size_t nmax); STC_INLINE cstr cstr_init() { return cstr_null; } +#define cstr_str(self) (self)->str #define cstr_lit(literal) \ cstr_from_n(literal, sizeof c_make(strlit_t){literal} - 1) STC_INLINE cstr cstr_from(const char* str) { return cstr_from_n(str, strlen(str)); } -STC_INLINE const char* cstr_str(const cstr* self) { return self->str; } STC_INLINE char* cstr_data(cstr* self) { return self->str; } STC_INLINE size_t cstr_size(cstr s) { return _cstr_rep(&s)->size; } STC_INLINE size_t cstr_length(cstr s) { return _cstr_rep(&s)->size; } -- cgit v1.2.3