diff options
| author | Tyge Løvset <[email protected]> | 2021-11-08 18:18:42 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-11-08 18:18:42 +0100 |
| commit | 02dfa9735234fd2676ce260e0ba4bc26e3e8b12f (patch) | |
| tree | f27764119b88eed1cd90a418d5cd9b83be4e5af9 /include | |
| parent | c8278d5437ac51ce1d63327fee6296035d80fe20 (diff) | |
| download | STC-modified-02dfa9735234fd2676ce260e0ba4bc26e3e8b12f.tar.gz STC-modified-02dfa9735234fd2676ce260e0ba4bc26e3e8b12f.zip | |
Changed default_hash() and c_strhash()
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/ccommon.h | 18 | ||||
| -rw-r--r-- | include/stc/cmap.h | 7 |
2 files changed, 13 insertions, 12 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 9359414c..f0e4cb2a 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -114,12 +114,20 @@ #define c_default_del(ptr) ((void) (ptr))
#define _c_rotl(x, k) (x << (k) | x >> (8*sizeof(x) - (k)))
-STC_INLINE uint64_t c_strhash(const char *str) {
- int c; uint64_t h = 0xb5ad4eceda1ce2a9;
- while ((c = *str++)) h = (_c_rotl(h, 4) ^ (h << 13)) + c;
- return h ^ (h >> 15);
+
+STC_INLINE uint64_t c_strhash(const char *s) {
+ int c; uint64_t h = (uint64_t)*s++ << 26;
+ if (h) while ((c = *s++)) h = ((h << 5) - h) + c;
+ return h;
+}
+// len >= 2 and multiple of 16:
+STC_INLINE uint64_t c_default_hash(const void* key, size_t len) {
+ const uint16_t *x = (const uint16_t*) key;
+ uint64_t h = *x++; h += h << 11;
+ while ((len -= 2)) h ^= (h << 9) + *x++;
+ return h;
}
-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 883fec03..2d0a81f7 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -221,13 +221,6 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp)
#ifndef CMAP_H_INCLUDED
-STC_INLINE uint64_t c_default_hash(const void *key, size_t len) {
- const char* str = (const char*)key, *e = str + len;
- uint64_t h = 0xb5ad4eceda1ce2a9;
- while (str != e) h = (_c_rotl(h, 4) ^ (h << 13)) + *str++;
- return h ^ (h >> 15);
-}
-
//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)
|
