added Apex>>hash

added Symbol>>asString
revised String>>&
added some methods to Dictionary
This commit is contained in:
hyunghwan.chung
2017-01-05 10:16:04 +00:00
parent c8f97832c5
commit af9c144a1c
12 changed files with 446 additions and 139 deletions

View File

@ -76,7 +76,7 @@ static stix_oop_oop_t expand_bucket (stix_t* stix, stix_oop_oop_t oldbuc)
key = (stix_oop_char_t)ass->key;
STIX_ASSERT (stix, STIX_CLASSOF(stix,key) == (stix_oop_t)stix->_symbol);
index = stix_hashchars(key->slot, STIX_OBJ_GET_SIZE(key)) % newsz;
index = stix_hashoochars(key->slot, STIX_OBJ_GET_SIZE(key)) % newsz;
while (newbuc->slot[index] != stix->_nil) index = (index + 1) % newsz;
newbuc->slot[index] = (stix_oop_t)ass;
}
@ -98,7 +98,7 @@ static stix_oop_association_t find_or_upsert (stix_t* stix, stix_oop_set_t dic,
STIX_ASSERT (stix, STIX_CLASSOF(stix,dic->tally) == stix->_small_integer);
STIX_ASSERT (stix, STIX_CLASSOF(stix,dic->bucket) == stix->_array);
index = stix_hashchars(key->slot, STIX_OBJ_GET_SIZE(key)) % STIX_OBJ_GET_SIZE(dic->bucket);
index = stix_hashoochars(key->slot, STIX_OBJ_GET_SIZE(key)) % STIX_OBJ_GET_SIZE(dic->bucket);
/* find */
while (dic->bucket->slot[index] != stix->_nil)
@ -164,7 +164,7 @@ static stix_oop_association_t find_or_upsert (stix_t* stix, stix_oop_set_t dic,
dic->bucket = bucket;
/* recalculate the index for the expanded bucket */
index = stix_hashchars(key->slot, STIX_OBJ_GET_SIZE(key)) % STIX_OBJ_GET_SIZE(dic->bucket);
index = stix_hashoochars(key->slot, STIX_OBJ_GET_SIZE(key)) % STIX_OBJ_GET_SIZE(dic->bucket);
while (dic->bucket->slot[index] != stix->_nil)
index = (index + 1) % STIX_OBJ_GET_SIZE(dic->bucket);
@ -203,7 +203,7 @@ static stix_oop_association_t lookup (stix_t* stix, stix_oop_set_t dic, const st
STIX_ASSERT (stix, STIX_CLASSOF(stix,dic->tally) == stix->_small_integer);
STIX_ASSERT (stix, STIX_CLASSOF(stix,dic->bucket) == stix->_array);
index = stix_hashchars(name->ptr, name->len) % STIX_OBJ_GET_SIZE(dic->bucket);
index = stix_hashoochars(name->ptr, name->len) % STIX_OBJ_GET_SIZE(dic->bucket);
while (dic->bucket->slot[index] != stix->_nil)
{

View File

@ -1613,6 +1613,87 @@ static int pf_basic_at_put (stix_t* stix, stix_ooi_t nargs)
return 1;
}
static int pf_hash (stix_t* stix, stix_ooi_t nargs)
{
stix_oop_t rcv;
stix_oow_t hv;
STIX_ASSERT (stix, nargs == 0);
rcv = STIX_STACK_GETRCV(stix, nargs);
switch (STIX_OOP_GET_TAG(rcv))
{
case STIX_OOP_TAG_SMINT:
hv = STIX_OOP_TO_CHAR(rcv);
break;
case STIX_OOP_TAG_CHAR:
hv = STIX_OOP_TO_CHAR(rcv);
break;
case STIX_OOP_TAG_ERROR:
hv = STIX_OOP_TO_ERROR(rcv);
break;
default:
{
int type;
STIX_ASSERT (stix, STIX_OOP_IS_POINTER(rcv));
type = STIX_OBJ_GET_FLAGS_TYPE(rcv);
switch (type)
{
case STIX_OBJ_TYPE_BYTE:
hv = stix_hashbytes(((stix_oop_byte_t)rcv)->slot, STIX_OBJ_GET_SIZE(rcv));
break;
case STIX_OBJ_TYPE_CHAR:
hv = stix_hashoochars (((stix_oop_char_t)rcv)->slot, STIX_OBJ_GET_SIZE(rcv));
break;
case STIX_OBJ_TYPE_HALFWORD:
hv = stix_hashhalfwords(((stix_oop_halfword_t)rcv)->slot, STIX_OBJ_GET_SIZE(rcv));
break;
case STIX_OBJ_TYPE_WORD:
hv = stix_hashwords(((stix_oop_word_t)rcv)->slot, STIX_OBJ_GET_SIZE(rcv));
break;
default:
/* STIX_OBJ_TYPE_OOP, ... */
STIX_DEBUG1 (stix, "<pf_hash> Cannot hash an object of type %d\n", type);
return 0;
}
break;
}
}
hv %= STIX_SMOOI_MAX;
STIX_STACK_SETRET (stix, nargs, STIX_SMOOI_TO_OOP(hv));
return 1;
}
static int pf_exceptionize_error (stix_t* stix, stix_ooi_t nargs)
{
stix_oop_t rcv;
STIX_ASSERT (stix, nargs == 1);
rcv = STIX_STACK_GETRCV(stix, nargs);
if (!STIX_OOP_IS_POINTER(rcv))
{
/* the receiver is a special numeric object, not a normal pointer.
* excceptionization is not supported for small integers, characters, and errors.
* first of all, methods of these classes must not return errors */
return 0;
}
// TODO: .......
// STIX_OBJ_SET_FLAGS_EXTRA (rcv, xxx);
STIX_STACK_SETRETTORCV (stix, nargs);
return 1;
}
static int pf_context_goto (stix_t* stix, stix_ooi_t nargs)
{
stix_oop_t rcv;
@ -2496,7 +2577,6 @@ static int pf_error_as_string (stix_t* stix, stix_ooi_t nargs)
return 1;
}
static int pf_ffi_open (stix_t* stix, stix_ooi_t nargs)
{
stix_oop_t rcv, arg;
@ -2809,6 +2889,9 @@ static pf_t pftab[] =
{ 1, 1, pf_basic_at, "_basic_at" },
{ 2, 2, pf_basic_at_put, "_basic_at_put" },
{ 0, 0, pf_hash, "_hash" },
{ 1, 1, pf_exceptionize_error, "_exceptionize_error" },
{ 1, 1, pf_context_goto, "_context_goto" },
{ 0, MAX_NARGS, pf_block_value, "_block_value" },
@ -2855,7 +2938,6 @@ static pf_t pftab[] =
{ 0, 0, pf_error_as_integer, "_error_as_integer" },
{ 0, 0, pf_error_as_string, "_error_as_string" },
{ 1, 1, pf_ffi_open, "_ffi_open" },
{ 1, 1, pf_ffi_close, "_ffi_close" },
{ 2, 2, pf_ffi_getsym, "_ffi_getsym" },

View File

@ -370,7 +370,7 @@ static void compact_symbol_table (stix_t* stix, stix_oop_t _nil)
STIX_ASSERT (stix, STIX_CLASSOF(stix,symbol) == stix->_symbol);
z = stix_hashchars(symbol->slot, STIX_OBJ_GET_SIZE(symbol)) % bucket_size;
z = stix_hashoochars(symbol->slot, STIX_OBJ_GET_SIZE(symbol)) % bucket_size;
/* move an element if necessary */
if ((y > x && (z <= x || z > y)) ||

View File

@ -33,48 +33,50 @@
extern "C" {
#endif
#if defined(STIX_OOCH_IS_UCH)
# define stix_hashchars(ptr,len) stix_hashuchars(ptr,len)
# define stix_equaloochars(str1,str2,len) stix_equaluchars(str1,str2,len)
# define stix_compoocbcstr(str1,str2) stix_compucbcstr(str1,str2)
# define stix_compoocharsbcstr(str1,len1,str2) stix_compucharsbcstr(str1,len1,str2)
# define stix_compoocstr(str1,str2) stix_compucstr(str1,str2)
# define stix_copyoochars(dst,src,len) stix_copyuchars(dst,src,len)
# define stix_copybctooochars(dst,src,len) stix_copybtouchars(dst,src,len)
# define stix_copyoocstr(dst,len,src) stix_copyucstr(dst,len,src)
# define stix_findoochar(ptr,len,c) stix_finduchar(ptr,len,c)
# define stix_rfindoochar(ptr,len,c) stix_rfinduchar(ptr,len,c)
# define stix_countoocstr(str) stix_countucstr(str)
#else
# define stix_hashchars(ptr,len) stix_hashbchars(ptr,len)
# define stix_equaloochars(str1,str2,len) stix_equalbchars(str1,str2,len)
# define stix_compoocbcstr(str1,str2) stix_compbcstr(str1,str2)
# define stix_compoocharsbcstr(str1,len1,str2) stix_compbcharsbcstr(str1,len1,str2)
# define stix_compoocstr(str1,str2) stix_compbcstr(str1,str2)
# define stix_copyoochars(dst,src,len) stix_copybchars(dst,src,len)
# define stix_copybctooochars(dst,src,len) stix_copybchars(dst,src,len)
# define stix_copyoocstr(dst,len,src) stix_copybcstr(dst,len,src)
# define stix_findoochar(ptr,len,c) stix_findbchar(ptr,len,c)
# define stix_rfindoochar(ptr,len,c) stix_rfindbchar(ptr,len,c)
# define stix_countoocstr(str) stix_countbcstr(str)
#endif
/* ========================================================================= */
/* utl.c */
/* ========================================================================= */
STIX_EXPORT stix_oow_t stix_hashbytes (
const stix_oob_t* ptr,
stix_oow_t len
);
STIX_EXPORT stix_oow_t stix_hashuchars (
const stix_uch_t* ptr,
#if defined(STIX_HAVE_INLINE)
static STIX_INLINE stix_oow_t stix_hashbchars (const stix_bch_t* ptr, stix_oow_t len)
{
return stix_hashbytes((const stix_oob_t*)ptr,len * STIX_SIZEOF(stix_bch_t));
}
static STIX_INLINE stix_oow_t stix_hashuchars (const stix_uch_t* ptr, stix_oow_t len)
{
return stix_hashbytes((const stix_oob_t*)ptr,len * STIX_SIZEOF(stix_uch_t));
}
static STIX_INLINE stix_oow_t stix_hashwords (const stix_oow_t* ptr, stix_oow_t len)
{
return stix_hashbytes((const stix_oob_t*)ptr, len * STIX_SIZEOF(stix_oow_t));
}
static STIX_INLINE stix_oow_t stix_hashhalfwords (const stix_oohw_t* ptr, stix_oow_t len)
{
return stix_hashbytes((const stix_oob_t*)ptr, len * STIX_SIZEOF(stix_oohw_t));
}
#else
# define stix_hashbchars(ptr,len) stix_hashbytes((const stix_oob_t*)ptr,len * STIX_SIZEOF(stix_bch_t))
# define stix_hashuchars(ptr,len) stix_hashbytes((const stix_oob_t*)ptr,len * STIX_SIZEOF(stix_uch_t))
# define stix_hashwords(ptr,len) stix_hashbytes((const stix_oob_t*)ptr, len * STIX_SIZEOF(stix_oow_t))
# define stix_hashhalfwords(ptr,len) stix_hashbytes((const stix_oob_t*)ptr, len * STIX_SIZEOF(stix_oohw_t))
#endif
#if defined(STIX_OOCH_IS_UCH)
# define stix_hashoochars(ptr,len) stix_hashuchars(ptr,len)
#else
# define stix_hashoochars(ptr,len) stix_hashbchars(ptr,len)
#endif
STIX_EXPORT stix_oow_t stix_hashwords (
const stix_oow_t* ptr,
stix_oow_t len
);
#define stix_hashbchars(ptr,len) stix_hashbytes(ptr,len)
/**
* The stix_equaluchars() function determines equality of two strings
* of the same length \a len.
@ -181,6 +183,33 @@ STIX_EXPORT stix_oow_t stix_countbcstr (
const stix_bch_t* str
);
#if defined(STIX_OOCH_IS_UCH)
# define stix_equaloochars(str1,str2,len) stix_equaluchars(str1,str2,len)
# define stix_compoocbcstr(str1,str2) stix_compucbcstr(str1,str2)
# define stix_compoocharsbcstr(str1,len1,str2) stix_compucharsbcstr(str1,len1,str2)
# define stix_compoocstr(str1,str2) stix_compucstr(str1,str2)
# define stix_copyoochars(dst,src,len) stix_copyuchars(dst,src,len)
# define stix_copybctooochars(dst,src,len) stix_copybtouchars(dst,src,len)
# define stix_copyoocstr(dst,len,src) stix_copyucstr(dst,len,src)
# define stix_findoochar(ptr,len,c) stix_finduchar(ptr,len,c)
# define stix_rfindoochar(ptr,len,c) stix_rfinduchar(ptr,len,c)
# define stix_countoocstr(str) stix_countucstr(str)
#else
# define stix_equaloochars(str1,str2,len) stix_equalbchars(str1,str2,len)
# define stix_compoocbcstr(str1,str2) stix_compbcstr(str1,str2)
# define stix_compoocharsbcstr(str1,len1,str2) stix_compbcharsbcstr(str1,len1,str2)
# define stix_compoocstr(str1,str2) stix_compbcstr(str1,str2)
# define stix_copyoochars(dst,src,len) stix_copybchars(dst,src,len)
# define stix_copybctooochars(dst,src,len) stix_copybchars(dst,src,len)
# define stix_copyoocstr(dst,len,src) stix_copybcstr(dst,len,src)
# define stix_findoochar(ptr,len,c) stix_findbchar(ptr,len,c)
# define stix_rfindoochar(ptr,len,c) stix_rfindbchar(ptr,len,c)
# define stix_countoocstr(str) stix_countbcstr(str)
#endif
STIX_EXPORT int stix_copyoocstrtosbuf (
stix_t* stix,
const stix_ooch_t* str,
@ -279,9 +308,8 @@ STIX_EXPORT int stix_convutf8toucstr (
stix_oow_t* ucslen
);
/* ========================================================================= */
/* utf8.c */
/* ========================================================================= */
STIX_EXPORT stix_oow_t stix_uctoutf8 (
stix_uch_t uc,
stix_bch_t* utf8,
@ -294,6 +322,7 @@ STIX_EXPORT stix_oow_t stix_utf8touc (
stix_uch_t* uc
);
#if defined(__cplusplus)
}
#endif

View File

@ -526,7 +526,10 @@ struct stix_method_t
* 8 - do primitive[index]
* 9 - do named primitive[index]
* 10 - exception handler
* 11 - ensure block
*/
/* NOTE: changing preamble code bit structure requires changes to CompiledMethod>>preambleCode */
#define STIX_METHOD_MAKE_PREAMBLE(code,index,flags) ((((stix_ooi_t)index) << 8) | ((stix_ooi_t)code << 2) | flags)
#define STIX_METHOD_GET_PREAMBLE_CODE(preamble) ((((stix_ooi_t)preamble) & 0xFF) >> 2)
#define STIX_METHOD_GET_PREAMBLE_INDEX(preamble) (((stix_ooi_t)preamble) >> 8)
@ -543,8 +546,9 @@ struct stix_method_t
#define STIX_METHOD_PREAMBLE_RETURN_INSTVAR 7
#define STIX_METHOD_PREAMBLE_PRIMITIVE 8
#define STIX_METHOD_PREAMBLE_NAMED_PRIMITIVE 9 /* index is an index to the symbol table */
#define STIX_METHOD_PREAMBLE_EXCEPTION 10
#define STIX_METHOD_PREAMBLE_ENSURE 11
#define STIX_METHOD_PREAMBLE_EXCEPTION 10 /* NOTE changing this requires changes in Except.st */
#define STIX_METHOD_PREAMBLE_ENSURE 11 /* NOTE changing this requires changes in Except.st */
/* the index is an 16-bit unsigned integer. */
#define STIX_METHOD_PREAMBLE_INDEX_MIN 0x0000
@ -554,6 +558,9 @@ struct stix_method_t
/* preamble flags */
#define STIX_METHOD_PREAMBLE_FLAG_VARIADIC (1 << 0)
/* NOTE: if you change the number of instance variables for stix_context_t,
* you need to change the defintion of BlockContext and MethodContext.
* plus, you need to update various exception handling code in MethodContext */
#define STIX_CONTEXT_NAMED_INSTVARS 8
typedef struct stix_context_t stix_context_t;
typedef struct stix_context_t* stix_oop_context_t;

View File

@ -73,7 +73,7 @@ static stix_oop_oop_t expand_bucket (stix_t* stix, stix_oop_oop_t oldbuc)
STIX_ASSERT (stix, STIX_CLASSOF(stix,symbol) == stix->_symbol);
/*STIX_ASSERT (stix, sym->size > 0);*/
index = stix_hashchars(symbol->slot, STIX_OBJ_GET_SIZE(symbol)) % newsz;
index = stix_hashoochars(symbol->slot, STIX_OBJ_GET_SIZE(symbol)) % newsz;
while (newbuc->slot[index] != stix->_nil) index = (index + 1) % newsz;
newbuc->slot[index] = (stix_oop_t)symbol;
}
@ -97,7 +97,7 @@ static stix_oop_t find_or_make_symbol (stix_t* stix, const stix_ooch_t* ptr, sti
}
STIX_ASSERT (stix, STIX_CLASSOF(stix,stix->symtab->bucket) == stix->_array);
index = stix_hashchars(ptr, len) % STIX_OBJ_GET_SIZE(stix->symtab->bucket);
index = stix_hashoochars(ptr, len) % STIX_OBJ_GET_SIZE(stix->symtab->bucket);
/* find a matching symbol in the open-addressed symbol table */
while (stix->symtab->bucket->slot[index] != stix->_nil)
@ -153,7 +153,7 @@ static stix_oop_t find_or_make_symbol (stix_t* stix, const stix_ooch_t* ptr, sti
stix->symtab->bucket = bucket;
/* recalculate the index for the expanded bucket */
index = stix_hashchars(ptr, len) % STIX_OBJ_GET_SIZE(stix->symtab->bucket);
index = stix_hashoochars(ptr, len) % STIX_OBJ_GET_SIZE(stix->symtab->bucket);
while (stix->symtab->bucket->slot[index] != stix->_nil)
index = (index + 1) % STIX_OBJ_GET_SIZE(stix->symtab->bucket);

View File

@ -48,11 +48,6 @@ stix_oow_t stix_hashbytes (const stix_oob_t* ptr, stix_oow_t len)
return h;
}
stix_oow_t stix_hashuchars (const stix_uch_t* ptr, stix_oow_t len)
{
return stix_hashbytes ((const stix_oob_t *)ptr, len * STIX_SIZEOF(*ptr));
}
int stix_equaluchars (const stix_uch_t* str1, const stix_uch_t* str2, stix_oow_t len)
{
stix_oow_t i;