From 1aea9f44924814e50a4bcc090e22a7533eb5f5f3 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 1 Mar 2019 09:27:03 +0000 Subject: [PATCH] some use of sdbm hash --- qse/lib/awk/val.c | 7 +++++++ qse/lib/cmn/htb.c | 21 +++++++++++---------- qse/lib/cmn/htl.c | 6 +++--- qse/lib/cmn/oht.c | 20 +++++++++++++------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/qse/lib/awk/val.c b/qse/lib/awk/val.c index 8db2a86c..022c9b2a 100644 --- a/qse/lib/awk/val.c +++ b/qse/lib/awk/val.c @@ -1698,9 +1698,16 @@ int qse_awk_rtx_strtonum ( static qse_awk_uint_t hash (qse_uint8_t* ptr, qse_size_t len) { + /* qse_awk_uint_t h = 5381; while (len > 0) h = ((h << 5) + h) + ptr[--len]; return h; + */ + + /* SDBM hash */ + qse_awk_uint_t h = 0; + while (len > 0) h = (h << 6) + (h << 16) - h + ptr[--len]; + return h; } qse_awk_int_t qse_awk_rtx_hashval (qse_awk_rtx_t* rtx, qse_awk_val_t* v) diff --git a/qse/lib/cmn/htb.c b/qse/lib/cmn/htb.c index 3cf122bf..4fb3e86a 100644 --- a/qse/lib/cmn/htb.c +++ b/qse/lib/cmn/htb.c @@ -672,7 +672,6 @@ void qse_htb_clear (htb_t* htb) } } - void qse_htb_walk (htb_t* htb, walker_t walker, void* ctx) { size_t i; @@ -734,29 +733,31 @@ pair_t* qse_htb_getnextpair (htb_t* htb, pair_t* pair, size_t* buckno) return QSE_NULL; } -size_t qse_htb_dflhash ( - const htb_t* htb, const void* kptr, size_t klen) +size_t qse_htb_dflhash (const htb_t* htb, const void* kptr, size_t klen) { + const byte_t* p = (const byte_t*)kptr; + const byte_t* bound = p + klen; +#if 0 /*size_t h = 2166136261;*/ /*size_t h = 0;*/ size_t h = 5381; - const byte_t* p = (const byte_t*)kptr; - const byte_t* bound = p + klen; while (p < bound) { /*h = (h * 16777619) ^ *p++;*/ /*h = h * 31 + *p++;*/ h = ((h << 5) + h) + *p++; - } + } +#else + /* SDBM hash */ + size_t h = 0; + while (p < bound) h = (h << 6) + (h << 16) - h + *p++; +#endif return h ; } -int qse_htb_dflcomp ( - const htb_t* htb, - const void* kptr1, size_t klen1, - const void* kptr2, size_t klen2) +int qse_htb_dflcomp (const htb_t* htb, const void* kptr1, size_t klen1, const void* kptr2, size_t klen2) { if (klen1 == klen2) return QSE_MEMCMP (kptr1, kptr2, KTOB(htb,klen1)); /* it just returns 1 to indicate that they are different. */ diff --git a/qse/lib/cmn/htl.c b/qse/lib/cmn/htl.c index a823503f..d915d894 100644 --- a/qse/lib/cmn/htl.c +++ b/qse/lib/cmn/htl.c @@ -843,7 +843,7 @@ QSE_INLINE qse_uint32_t qse_genhash32_update (const void* data, qse_size_t size, qse_uint32_t qse_genhash32 (const void *data, qse_size_t size) { - return qse_genhash32_update (data, size, FNV_MAGIC_INIT); + return qse_genhash32_update(data, size, FNV_MAGIC_INIT); } /* @@ -855,7 +855,7 @@ qse_uint32_t qse_mbshash32 (const qse_mchar_t* p) while (*p) { - hash ^= (qse_uint32_t) (*p++); + hash ^= (qse_uint32_t)(*p++); hash *= FNV_MAGIC_PRIME; } @@ -873,7 +873,7 @@ qse_uint32_t qse_wcshash32 (const qse_wchar_t* p) hash ^= (qse_uint32_t)(*p); hash *= FNV_MAGIC_PRIME; #else - hash = qse_genhash_update (*p, QSE_SIZEOF(*p), hash); + hash = qse_genhash_update(*p, QSE_SIZEOF(*p), hash); #endif p++; } diff --git a/qse/lib/cmn/oht.c b/qse/lib/cmn/oht.c index e0bd8830..ff9b6494 100644 --- a/qse/lib/cmn/oht.c +++ b/qse/lib/cmn/oht.c @@ -4,24 +4,30 @@ #define DATA_PTR(oht,index) \ ((void*)(((qse_byte_t*)(oht)->data) + ((index) * (oht)->scale))) -static QSE_INLINE_ALWAYS qse_size_t default_hasher ( - qse_oht_t* oht, const void* data) +static QSE_INLINE_ALWAYS qse_size_t default_hasher (qse_oht_t* oht, const void* data) { - qse_size_t h = 5381; const qse_byte_t* p = (const qse_byte_t*)data; const qse_byte_t* bound = p + oht->scale; + +#if 0 + // DJB2 hash + qse_size_t h = 5381; while (p < bound) h = ((h << 5) + h) + *p++; +#else + // SDBM hash + qse_size_t h = 0; + while (p < bound) h = (h << 6) + (h << 16) - h + *p++; +#endif + return h ; } -static QSE_INLINE_ALWAYS int default_comper ( - qse_oht_t* oht, const void* data1, const void* data2) +static QSE_INLINE_ALWAYS int default_comper (qse_oht_t* oht, const void* data1, const void* data2) { return QSE_MEMCMP(data1, data2, oht->scale); } -static QSE_INLINE_ALWAYS void default_copier ( - qse_oht_t* oht, void* dst, const void* src) +static QSE_INLINE_ALWAYS void default_copier (qse_oht_t* oht, void* dst, const void* src) { QSE_MEMCPY (dst, src, oht->scale); }