From: Attractive Chaos Date: Wed, 13 Dec 2023 01:18:41 +0000 (-0500) Subject: use FNV1a for strings X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=3d0b46a91c4a7071aa617cf86d78908d24d9f3d4;p=klib.git use FNV1a for strings --- diff --git a/khashl.h b/khashl.h index 2d0f413..3d41364 100644 --- a/khashl.h +++ b/khashl.h @@ -342,9 +342,21 @@ static kh_inline khint_t kh_hash_uint64(khint64_t key) { return (khint_t)key; } -static kh_inline khint_t kh_hash_str(const char *s) { - khint_t h = (khint_t)*s; - if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s; +#define KH_FNV_SEED 11 + +static kh_inline khint_t kh_hash_str(const char *s) { /* FNV1a */ + khint_t h = KH_FNV_SEED ^ 2166136261U; + const unsigned char *t = (const unsigned char*)s; + for (; *t; ++t) + h ^= *t, h *= 16777619; + return h; +} + +static kh_inline khint_t kh_hash_bytes(int len, const unsigned char *s) { + khint_t h = KH_FNV_SEED ^ 2166136261U; + int i; + for (i = 0; i < len; ++i) + h ^= s[i], h *= 16777619; return h; }