updated qse_stx_hashobj()

This commit is contained in:
hyung-hwan 2011-06-08 10:47:20 +00:00
parent adcf59e44d
commit 143bf5aa2b
3 changed files with 50 additions and 28 deletions

View File

@ -47,7 +47,7 @@ static qse_word_t new_assoc (
static qse_word_t find_slot ( static qse_word_t find_slot (
qse_stx_t* stx, qse_word_t dic, qse_word_t key) qse_stx_t* stx, qse_word_t dic, qse_word_t key)
{ {
qse_word_t size, hash, index, assoc, symbol; qse_word_t size, hash, index, assoc, sym;
qse_stx_dic_t* ptr; qse_stx_dic_t* ptr;
/* ensure that dic is a system dictionary */ /* ensure that dic is a system dictionary */
@ -59,6 +59,7 @@ static qse_word_t find_slot (
/* ensure that the key is a symbol */ /* ensure that the key is a symbol */
QSE_ASSERT (REFISIDX(stx,key)); QSE_ASSERT (REFISIDX(stx,key));
QSE_ASSERT (OBJCLASS(stx,key) == stx->ref.class_symbol); QSE_ASSERT (OBJCLASS(stx,key) == stx->ref.class_symbol);
QSE_ASSERT (OBJTYPE(stx,key) == CHAROBJ);
size = OBJSIZE (stx, dic); size = OBJSIZE (stx, dic);
hash = qse_stx_hashobj (stx, key); hash = qse_stx_hashobj (stx, key);
@ -73,18 +74,19 @@ static qse_word_t find_slot (
assoc = ptr->slot[index]; assoc = ptr->slot[index];
if (assoc == stx->ref.nil) break; if (assoc == stx->ref.nil) break;
symbol = WORDAT (stx, assoc, QSE_STX_ASSOC_KEY); sym = WORDAT (stx, assoc, QSE_STX_ASSOC_KEY);
QSE_ASSERT (REFISIDX(stx,symbol)); QSE_ASSERT (REFISIDX(stx,sym));
QSE_ASSERT (OBJCLASS(stx,symbol) == stx->ref.class_symbol); QSE_ASSERT (OBJCLASS(stx,sym) == stx->ref.class_symbol);
QSE_ASSERT (OBJTYPE(stx,sym) == CHAROBJ);
/* NOTE: /* NOTE:
* shallow comparison is enough for identity check * shallow comparison is enough for identity check
* because a symbol can just be a key of a system dicionary * because a symbol can just be a key of a system dicionary
*/ */
if (qse_strxncmp( if (qse_strxncmp(
QSE_STX_DATA(stx,key), OBJSIZE(stx,key), &CHARAT(stx,key,0), OBJSIZE(stx,key),
QSE_STX_DATA(stx,symbol), OBJSIZE(stx,symbol)) == 0) break; &CHARAT(stx,key,0), OBJSIZE(stx,sym)) == 0) break;
/* consider tally here too */ /* consider tally here too */
index = index % (size - 1); index = index % (size - 1);
@ -93,7 +95,7 @@ static qse_word_t find_slot (
return index; return index;
} }
static void grow_dic (qse_stx_t* stx, qse_word_t dic) static qse_word_t grow_dic (qse_stx_t* stx, qse_word_t dic)
{ {
qse_word_t new, size, index, assoc; qse_word_t new, size, index, assoc;
@ -103,12 +105,14 @@ static void grow_dic (qse_stx_t* stx, qse_word_t dic)
* during the bootstrapping. * during the bootstrapping.
*/ */
QSE_ASSERT (stx->ref.class_system_dictionary != stx->ref.nil); QSE_ASSERT (stx->ref.class_system_dictionary != stx->ref.nil);
QSE_ASSERT (qse_stx_classof(stx,dic) == stx->ref.class_system_dicionary); QSE_ASSERT (REFISIDX(stx,dic));
QSE_ASSERT (OBJCLASS(stx,dic) == stx->ref.class_system_dictionary);
size = OBJSIZE(stx,dic); size = OBJSIZE(stx,dic);
new = qse_stx_instantiate (stx, new = qse_stx_instantiate (stx,
OBJCLASS(stx,dic), QSE_NULL, QSE_NULL, (size - 1) * 2); OBJCLASS(stx,dic), QSE_NULL, QSE_NULL, (size - 1) * 2);
WORDAT(stx,new,0) = QSE_STX_TO_SMALLINT(0); if (new == stx->ref.nil) return stx->ref.nil;
WORDAT(stx,new,QSE_STX_DIC_TALLY) = INTTOREF (stx, 0);
for (index = 1; index < size; index++) for (index = 1; index < size; index++)
{ {
@ -130,6 +134,8 @@ static void grow_dic (qse_stx_t* stx, qse_word_t dic)
qse_stx_object_t*, qse_stx_object_t*,
qse_uint_t qse_uint_t
); );
return dic;
} }
qse_word_t qse_stx_lookupdic ( qse_word_t qse_stx_lookupdic (

View File

@ -3,7 +3,6 @@
*/ */
#include "stx.h" #include "stx.h"
#include "mem.h"
qse_word_t qse_stx_allocwordobj ( qse_word_t qse_stx_allocwordobj (
qse_stx_t* stx, const qse_word_t* data, qse_word_t nflds, qse_stx_t* stx, const qse_word_t* data, qse_word_t nflds,
@ -197,24 +196,43 @@ qse_word_t qse_stx_hashobj (qse_stx_t* stx, qse_word_t ref)
if (REFISINT(stx, ref)) if (REFISINT(stx, ref))
{ {
qse_word_t tmp = REFTOINT(stx, ref); qse_word_t tmp = REFTOINT(stx, ref);
hv = qse_stx_hash (&tmp, QSE_SIZEOF(tmp)); hv = qse_stx_hashbytes (stx, &tmp, QSE_SIZEOF(tmp));
} }
else if (QSE_STX_ISCHAROBJECT(stx,ref)) else
{ {
/* the additional null is not taken into account */ switch (OBJTYPE(stx,ref))
hv = qse_stx_hash (QSE_STX_DATA(stx,ref), {
QSE_STX_SIZE(stx,ref) * QSE_SIZEOF(qse_char_t)); case BYTEOBJ:
} hv = qse_stx_hashbytes (
else if (QSE_STX_ISBYTEOBJECT(stx,ref)) stx,
{ &BYTEAT(stx,ref,0),
hv = qse_stx_hash ( OBJSIZE(stx,ref)
QSE_STX_DATA(stx,ref), QSE_STX_SIZE(stx,ref)); );
} break;
else
{ case CHAROBJ:
QSE_ASSERT (QSE_STX_ISWORDOBJECT(stx,ref)); /* the additional null is not taken into account */
hv = qse_stx_hash (QSE_STX_DATA(stx,ref), hv = qse_stx_hashbytes (
QSE_STX_SIZE(stx,ref) * QSE_SIZEOF(qse_word_t)); stx,
&CHARAT(stx,ref,0),
OBJSIZE(stx,ref) * QSE_SIZEOF(qse_char_t)
);
break;
case WORDOBJ:
hv = qse_stx_hashbytes (
stx,
&WORDAT(stx,ref,0),
OBJSIZE(stx,ref) * QSE_SIZEOF(qse_word_t)
);
break;
default:
QSE_ASSERT (
!"This should never happen"
);
break;
}
} }
return hv; return hv;

View File

@ -186,8 +186,6 @@ struct qse_stx_t
#define QSE_STX_CHARAT(stx,ref,pos) \ #define QSE_STX_CHARAT(stx,ref,pos) \
(((qse_stx_charobjptr_t)QSE_STX_PTRBYREF(stx,ref))->fld[pos]) (((qse_stx_charobjptr_t)QSE_STX_PTRBYREF(stx,ref))->fld[pos])
/* REDEFINITION DROPPING PREFIX FOR INTERNAL USE */ /* REDEFINITION DROPPING PREFIX FOR INTERNAL USE */
#define REFISINT(stx,x) QSE_STX_REFISINT(stx,x) #define REFISINT(stx,x) QSE_STX_REFISINT(stx,x)
#define INTTOREF(stx,x) QSE_STX_INTTOREF(stx,x) #define INTTOREF(stx,x) QSE_STX_INTTOREF(stx,x)