From eeff90f79cdb3ee635b0f2fa270033a91c4824ec Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylo-work@users.noreply.github.com> Date: Mon, 16 Mar 2020 18:32:50 +0100 Subject: Add files via upload --- clib/cdefs.h | 13 +++++++------ clib/cmap.h | 10 ++++------ clib/cstring.h | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/clib/cdefs.h b/clib/cdefs.h index 8d084827..598dc863 100644 --- a/clib/cdefs.h +++ b/clib/cdefs.h @@ -50,14 +50,15 @@ static inline void c_defaultDestroy(void* value) {} #define c_foreach(it, ctag, con) \ for (ctag##_iter_t it = ctag##_begin(con); it.item != ctag##_end(con).item; it = ctag##_next(it)) - -static inline uint32_t c_murmurHash(const void *data, size_t len) { // One-at-a-time 32bit - const unsigned char *key = (const unsigned char *) data; - uint32_t h = 0xC613FC15; // ‭0x749E3E6989DF617‬; 64bit + +// One-byte-at-a-time hash based on Murmur's mix +static inline uint32_t c_defaultHash(const void *data, size_t len) { + const uint8_t *key = (const uint8_t *) data; + uint32_t h = 0xc613fc15; while (len--) { h ^= *key++; - h *= 0x5bd1e995; // 0x5bd1e9955bd1e995; 64bit - h ^= h >> 15; // 47; 64bit + h *= 0x5bd1e995; + h ^= h >> 15; } return h; } diff --git a/clib/cmap.h b/clib/cmap.h index f1bc7a60..2b23a37c 100644 --- a/clib/cmap.h +++ b/clib/cmap.h @@ -53,7 +53,7 @@ typedef struct CMapEntry_##tag CMapEntry_##tag declare_CMap_4(tag, Key, Value, c_defaultDestroy) #define declare_CMap_4(tag, Key, Value, valueDestroy) \ - declare_CMap_7(tag, Key, Value, valueDestroy, memcmp, c_murmurHash, c_defaultDestroy) + declare_CMap_7(tag, Key, Value, valueDestroy, memcmp, c_defaultHash, c_defaultDestroy) #define declare_CMap_7(tag, Key, Value, valueDestroy, keyCompare, keyHash, keyDestroy) \ declare_CMap_10(tag, Key, Value, valueDestroy, keyCompare, keyHash, keyDestroy, Key, c_defaultGetRaw, c_defaultInitRaw) @@ -177,15 +177,13 @@ static inline bool cmap_##tag##_erase(CMap_##tag* self, KeyRaw rawKey) { \ CMapEntry_##tag* slot = self->_vec.data; \ if (! slot[i].used) \ return false; \ - /* https://attractivechaos.wordpress.com/2019/12/28/deletion-from-hash-tables-without-tombstones/ */ \ - do { \ - if (++j == cap) j = 0; \ + do { /* https://attractivechaos.wordpress.com/2019/12/28/deletion-from-hash-tables-without-tombstones/ */ \ + if (++j == cap) j = 0; /* j %= cap; is slow */ \ if (! slot[j].used) \ break; \ KeyRaw r = keyGetRaw(slot[j].key); \ k = c_reduce(keyHashRaw(&r, sizeof(KeyRaw)), cap); \ - /* Check if k is outside [i, j) range */ \ - if ((j < i) ^ (k <= i) ^ (k > j)) \ + if ((j < i) ^ (k <= i) ^ (k > j)) /* Check if k is outside [i, j) range */ \ slot[i] = slot[j], i = j; \ } while (true); \ cmapentry_##tag##_destroy(&slot[i]); \ diff --git a/clib/cstring.h b/clib/cstring.h index 2e5de366..08e6c4c6 100644 --- a/clib/cstring.h +++ b/clib/cstring.h @@ -237,7 +237,7 @@ static inline char* cstring_splitNext(const char* delimiters) { // CVector / CMap API functions: #define cstring_getRaw(x) ((x).str) -static inline uint32_t cstring_hashRaw(const char** str, size_t sz_ignored) { return c_murmurHash(*str, strlen(*str)); } +static inline uint32_t cstring_hashRaw(const char** str, size_t sz_ignored) { return c_defaultHash(*str, strlen(*str)); } static inline int cstring_compareRaw(CString* self, const char** str, size_t sz_ignored) { return strcmp(self->str, *str); } -- cgit v1.2.3