some use of sdbm hash

This commit is contained in:
hyung-hwan 2019-03-01 09:27:03 +00:00
parent 6463fbec12
commit 1aea9f4492
4 changed files with 34 additions and 20 deletions

View File

@ -1698,9 +1698,16 @@ int qse_awk_rtx_strtonum (
static qse_awk_uint_t hash (qse_uint8_t* ptr, qse_size_t len) static qse_awk_uint_t hash (qse_uint8_t* ptr, qse_size_t len)
{ {
/*
qse_awk_uint_t h = 5381; qse_awk_uint_t h = 5381;
while (len > 0) h = ((h << 5) + h) + ptr[--len]; while (len > 0) h = ((h << 5) + h) + ptr[--len];
return h; 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) qse_awk_int_t qse_awk_rtx_hashval (qse_awk_rtx_t* rtx, qse_awk_val_t* v)

View File

@ -672,7 +672,6 @@ void qse_htb_clear (htb_t* htb)
} }
} }
void qse_htb_walk (htb_t* htb, walker_t walker, void* ctx) void qse_htb_walk (htb_t* htb, walker_t walker, void* ctx)
{ {
size_t i; size_t i;
@ -734,29 +733,31 @@ pair_t* qse_htb_getnextpair (htb_t* htb, pair_t* pair, size_t* buckno)
return QSE_NULL; return QSE_NULL;
} }
size_t qse_htb_dflhash ( size_t qse_htb_dflhash (const htb_t* htb, const void* kptr, size_t klen)
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 = 2166136261;*/
/*size_t h = 0;*/ /*size_t h = 0;*/
size_t h = 5381; size_t h = 5381;
const byte_t* p = (const byte_t*)kptr;
const byte_t* bound = p + klen;
while (p < bound) while (p < bound)
{ {
/*h = (h * 16777619) ^ *p++;*/ /*h = (h * 16777619) ^ *p++;*/
/*h = h * 31 + *p++;*/ /*h = h * 31 + *p++;*/
h = ((h << 5) + h) + *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 ; return h ;
} }
int qse_htb_dflcomp ( int qse_htb_dflcomp (const htb_t* htb, const void* kptr1, size_t klen1, const void* kptr2, size_t klen2)
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)); if (klen1 == klen2) return QSE_MEMCMP (kptr1, kptr2, KTOB(htb,klen1));
/* it just returns 1 to indicate that they are different. */ /* it just returns 1 to indicate that they are different. */

View File

@ -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) 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) while (*p)
{ {
hash ^= (qse_uint32_t) (*p++); hash ^= (qse_uint32_t)(*p++);
hash *= FNV_MAGIC_PRIME; hash *= FNV_MAGIC_PRIME;
} }
@ -873,7 +873,7 @@ qse_uint32_t qse_wcshash32 (const qse_wchar_t* p)
hash ^= (qse_uint32_t)(*p); hash ^= (qse_uint32_t)(*p);
hash *= FNV_MAGIC_PRIME; hash *= FNV_MAGIC_PRIME;
#else #else
hash = qse_genhash_update (*p, QSE_SIZEOF(*p), hash); hash = qse_genhash_update(*p, QSE_SIZEOF(*p), hash);
#endif #endif
p++; p++;
} }

View File

@ -4,24 +4,30 @@
#define DATA_PTR(oht,index) \ #define DATA_PTR(oht,index) \
((void*)(((qse_byte_t*)(oht)->data) + ((index) * (oht)->scale))) ((void*)(((qse_byte_t*)(oht)->data) + ((index) * (oht)->scale)))
static QSE_INLINE_ALWAYS qse_size_t default_hasher ( static QSE_INLINE_ALWAYS qse_size_t default_hasher (qse_oht_t* oht, const void* data)
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* p = (const qse_byte_t*)data;
const qse_byte_t* bound = p + oht->scale; 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++; 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 ; return h ;
} }
static QSE_INLINE_ALWAYS int default_comper ( static QSE_INLINE_ALWAYS int default_comper (qse_oht_t* oht, const void* data1, const void* data2)
qse_oht_t* oht, const void* data1, const void* data2)
{ {
return QSE_MEMCMP(data1, data2, oht->scale); return QSE_MEMCMP(data1, data2, oht->scale);
} }
static QSE_INLINE_ALWAYS void default_copier ( static QSE_INLINE_ALWAYS void default_copier (qse_oht_t* oht, void* dst, const void* src)
qse_oht_t* oht, void* dst, const void* src)
{ {
QSE_MEMCPY (dst, src, oht->scale); QSE_MEMCPY (dst, src, oht->scale);
} }