placed basic hash macros in qse/hash.h

This commit is contained in:
2019-03-05 08:21:01 +00:00
parent 1aea9f4492
commit bda9213f86
7 changed files with 131 additions and 84 deletions

View File

@ -26,6 +26,7 @@
#include "awk-prv.h"
#include <qse/cmn/mbwc.h>
#include <qse/hash.h>
#ifdef DEBUG_VAL
#include <qse/cmn/sio.h>
@ -1696,17 +1697,10 @@ int qse_awk_rtx_strtonum (
return 0; /* int */
}
static qse_awk_uint_t hash (qse_uint8_t* ptr, qse_size_t len)
static QSE_INLINE 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];
qse_awk_uint_t h;
QSE_HASH_BYTES (h, ptr, len);
return h;
}
@ -1725,18 +1719,18 @@ qse_awk_int_t qse_awk_rtx_hashval (qse_awk_rtx_t* rtx, qse_awk_val_t* v)
{
qse_awk_int_t tmp = QSE_AWK_RTX_GETINTFROMVAL (rtx, v);
/*hv = ((qse_awk_val_int_t*)v)->val;*/
hv = (qse_awk_int_t)hash ((qse_uint8_t*)&tmp, QSE_SIZEOF(tmp));
hv = (qse_awk_int_t)hash((qse_uint8_t*)&tmp, QSE_SIZEOF(tmp));
break;
}
case QSE_AWK_VAL_FLT:
hv = (qse_awk_int_t)hash (
hv = (qse_awk_int_t)hash(
(qse_uint8_t*)&((qse_awk_val_flt_t*)v)->val,
QSE_SIZEOF(((qse_awk_val_flt_t*)v)->val));
break;
case QSE_AWK_VAL_STR:
hv = (qse_awk_int_t)hash (
hv = (qse_awk_int_t)hash(
(qse_uint8_t*)((qse_awk_val_str_t*)v)->val.ptr,
((qse_awk_val_str_t*)v)->val.len * QSE_SIZEOF(qse_char_t));
break;

View File

@ -25,6 +25,7 @@
*/
#include <qse/cmn/htb.h>
#include <qse/hash.h>
#include "mem-prv.h"
@ -735,25 +736,8 @@ pair_t* qse_htb_getnextpair (htb_t* htb, pair_t* pair, size_t* buckno)
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;
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
size_t h;
QSE_HASH_BYTES (h, kptr, klen);
return h ;
}

View File

@ -281,10 +281,10 @@ qse_htl_t* qse_htl_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, int keysize)
{
qse_htl_t* htl;
htl = QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_htl_t) + xtnsize);
htl = QSE_MMGR_ALLOC(mmgr, QSE_SIZEOF(qse_htl_t) + xtnsize);
if (htl == QSE_NULL) return QSE_NULL;
if (qse_htl_init (htl, mmgr, keysize) <= -1)
if (qse_htl_init(htl, mmgr, keysize) <= -1)
{
QSE_MMGR_FREE (mmgr, htl);
return QSE_NULL;

View File

@ -1,4 +1,5 @@
#include <qse/cmn/oht.h>
#include <qse/hash.h>
#include "mem-prv.h"
#define DATA_PTR(oht,index) \
@ -6,19 +7,8 @@
static QSE_INLINE_ALWAYS qse_size_t default_hasher (qse_oht_t* oht, const void* data)
{
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
qse_size_t h;
QSE_HASH_BYTES (h, data, oht->scale);
return h ;
}

View File

@ -254,7 +254,6 @@ static qse_uint32_t dict_attr_value_hash (qse_htl_t* htl, const void* data)
{
const qse_raddic_attr_t* v = (const qse_raddic_attr_t*)data;
qse_uint32_t hv;
hv = qse_genhash32(&v->vendor, QSE_SIZEOF(v->vendor));
return qse_genhash32_update(&v->attr, QSE_SIZEOF(v->attr), hv);
}