2005-07-18 11:53:01 +00:00
|
|
|
/*
|
2005-08-11 10:18:35 +00:00
|
|
|
* $Id: dict.c,v 1.10 2005-08-11 10:18:35 bacon Exp $
|
2005-07-18 11:53:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xp/stx/dict.h>
|
|
|
|
#include <xp/stx/object.h>
|
|
|
|
#include <xp/stx/misc.h>
|
|
|
|
|
2005-08-11 10:18:35 +00:00
|
|
|
/* NOTE:
|
|
|
|
* The code here implements SystemDictionary whose key is always a symbol.
|
|
|
|
* Dictionary, on the contrary, can accept any object as a key.
|
|
|
|
*/
|
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_word_t __new_association (
|
2005-07-18 11:53:01 +00:00
|
|
|
xp_stx_t* stx, xp_word_t key, xp_word_t value)
|
|
|
|
{
|
|
|
|
xp_word_t x;
|
2005-07-19 15:00:09 +00:00
|
|
|
#ifdef __GNUC__
|
2005-07-18 11:53:01 +00:00
|
|
|
xp_word_t data[2] = { key, value };
|
2005-07-19 15:00:09 +00:00
|
|
|
#else
|
|
|
|
xp_word_t data[2];
|
|
|
|
data[0] = key;
|
|
|
|
data[1] = value;
|
|
|
|
#endif
|
2005-07-18 11:53:01 +00:00
|
|
|
x = xp_stx_alloc_word_object (
|
|
|
|
stx, data, XP_STX_ASSOCIATION_SIZE, XP_NULL, 0);
|
|
|
|
XP_STX_CLASS(stx,x) = stx->class_association;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
static xp_word_t __dict_find_slot (
|
|
|
|
xp_stx_t* stx, xp_word_t dict, xp_word_t key)
|
2005-07-18 11:53:01 +00:00
|
|
|
{
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_word_t size, hash, index, assoc, symbol;
|
|
|
|
xp_stx_word_object_t* dict_obj;
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 15:52:19 +00:00
|
|
|
xp_assert (!XP_STX_IS_SMALLINT(dict) &&
|
|
|
|
XP_STX_IS_WORD_OBJECT(stx, dict));
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_assert (dict == stx->smalltalk ||
|
2005-07-19 15:52:19 +00:00
|
|
|
xp_stx_classof(stx,dict) == stx->class_system_dictionary);
|
|
|
|
xp_assert (xp_stx_classof(stx,key) == stx->class_symbol);
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
size = XP_STX_SIZE(stx,dict);
|
|
|
|
hash = xp_stx_hash_object(stx, key);
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
/* consider tally, the only instance variable of a system dictionary */
|
|
|
|
index = hash % (size - 1) + 1;
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
dict_obj = XP_STX_WORD_OBJECT(stx,dict);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
assoc = dict_obj->data[index];
|
|
|
|
if (assoc == stx->nil) break;
|
|
|
|
|
|
|
|
symbol = XP_STX_WORD_AT(stx,assoc,XP_STX_ASSOCIATION_KEY);
|
2005-07-19 15:52:19 +00:00
|
|
|
xp_assert (xp_stx_classof(stx,symbol) == stx->class_symbol);
|
2005-07-19 12:08:04 +00:00
|
|
|
|
2005-08-11 10:11:26 +00:00
|
|
|
/* NOTE:
|
2005-07-19 12:08:04 +00:00
|
|
|
* shallow comparison is enough for identity check
|
2005-08-11 10:18:35 +00:00
|
|
|
* because a symbol can just be a key of a system dictionary
|
2005-07-19 12:08:04 +00:00
|
|
|
*/
|
2005-07-19 15:52:19 +00:00
|
|
|
if (xp_strxncmp(
|
|
|
|
XP_STX_DATA(stx,key), XP_STX_SIZE(stx,key),
|
|
|
|
XP_STX_DATA(stx,symbol), XP_STX_SIZE(stx,symbol)) == 0) break;
|
2005-07-19 12:08:04 +00:00
|
|
|
|
|
|
|
/* consider tally here too */
|
|
|
|
index = index % (size - 1) + 1;
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
return index;
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
|
|
|
|
2005-08-11 09:57:54 +00:00
|
|
|
static void __grow_dict (xp_stx_t* stx, xp_word_t dict)
|
2005-07-18 11:53:01 +00:00
|
|
|
{
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_word_t new, size, index, assoc;
|
|
|
|
|
2005-08-11 10:11:26 +00:00
|
|
|
/* WARNING:
|
2005-07-19 12:08:04 +00:00
|
|
|
* if this assertion fails, adjust the initial size of the
|
|
|
|
* system dictionary. i don't want this function to be called
|
|
|
|
* during the bootstrapping.
|
|
|
|
*/
|
|
|
|
xp_assert (stx->class_system_dictionary != stx->nil);
|
2005-07-19 15:52:19 +00:00
|
|
|
xp_assert (xp_stx_classof(stx,dict) == stx->class_system_dictionary);
|
2005-07-19 12:08:04 +00:00
|
|
|
|
|
|
|
size = XP_STX_SIZE(stx,dict);
|
|
|
|
new = xp_stx_instantiate (stx,
|
|
|
|
XP_STX_CLASS(stx,dict), XP_NULL, XP_NULL, (size - 1) * 2);
|
|
|
|
XP_STX_WORD_AT(stx,new,0) = XP_STX_TO_SMALLINT(0);
|
|
|
|
|
|
|
|
for (index = 1; index < size; index++) {
|
|
|
|
assoc = XP_STX_WORD_AT(stx,dict,index);
|
|
|
|
if (assoc == stx->nil) continue;
|
|
|
|
|
|
|
|
xp_stx_dict_put (stx, new,
|
|
|
|
XP_STX_WORD_AT(stx,assoc,XP_STX_ASSOCIATION_KEY),
|
|
|
|
XP_STX_WORD_AT(stx,assoc,XP_STX_ASSOCIATION_VALUE));
|
|
|
|
}
|
|
|
|
|
2005-08-11 10:11:26 +00:00
|
|
|
/* TODO: explore if dict can be immediately destroyed. */
|
|
|
|
|
2005-07-30 05:11:39 +00:00
|
|
|
xp_assert (xp_sizeof(xp_stx_object_t*) == xp_sizeof(xp_uint_t));
|
|
|
|
XP_SWAP (XP_STX_OBJECT(stx,dict),
|
|
|
|
XP_STX_OBJECT(stx,new),
|
|
|
|
xp_stx_object_t*, xp_uint_t);
|
2005-07-19 12:08:04 +00:00
|
|
|
}
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_word_t xp_stx_dict_lookup (
|
|
|
|
xp_stx_t* stx, xp_word_t dict, const xp_char_t* key)
|
|
|
|
{
|
|
|
|
xp_word_t size, hash, index, assoc, symbol;
|
|
|
|
xp_stx_word_object_t* dict_obj;
|
|
|
|
|
2005-07-19 15:52:19 +00:00
|
|
|
xp_assert (!XP_STX_IS_SMALLINT(dict) &&
|
|
|
|
XP_STX_IS_WORD_OBJECT(stx, dict));
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_assert (dict == stx->smalltalk ||
|
2005-07-19 15:52:19 +00:00
|
|
|
xp_stx_classof(stx,dict) == stx->class_system_dictionary);
|
2005-07-19 12:08:04 +00:00
|
|
|
|
|
|
|
size = XP_STX_SIZE(stx,dict);
|
|
|
|
hash = xp_stx_hash(key, xp_strlen(key) * xp_sizeof(xp_char_t));
|
|
|
|
|
|
|
|
/* consider tally, the only instance variable of a system dictionary */
|
|
|
|
index = hash % (size - 1) + 1;
|
|
|
|
|
|
|
|
dict_obj = XP_STX_WORD_OBJECT(stx,dict);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
assoc = dict_obj->data[index];
|
|
|
|
if (assoc == stx->nil) break;
|
|
|
|
|
|
|
|
symbol = XP_STX_WORD_AT(stx,assoc,XP_STX_ASSOCIATION_KEY);
|
2005-07-19 15:52:19 +00:00
|
|
|
xp_assert (xp_stx_classof(stx,symbol) == stx->class_symbol);
|
|
|
|
|
|
|
|
if (xp_strxcmp (XP_STX_DATA(stx,symbol),
|
|
|
|
XP_STX_SIZE(stx,symbol), key) == 0) break;
|
2005-07-19 12:08:04 +00:00
|
|
|
|
|
|
|
/* consider tally here too */
|
|
|
|
index = index % (size - 1) + 1;
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
return XP_STX_WORD_AT(stx,dict,index);
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_word_t xp_stx_dict_get (xp_stx_t* stx, xp_word_t dict, xp_word_t key)
|
2005-07-18 11:53:01 +00:00
|
|
|
{
|
2005-07-19 12:08:04 +00:00
|
|
|
return XP_STX_WORD_AT(stx,dict,__dict_find_slot(stx, dict, key));
|
|
|
|
}
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_word_t xp_stx_dict_put (
|
|
|
|
xp_stx_t* stx, xp_word_t dict, xp_word_t key, xp_word_t value)
|
|
|
|
{
|
|
|
|
xp_word_t slot, capa, tally, assoc;
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
/* the dictionary must have at least one slot excluding tally */
|
|
|
|
xp_assert (XP_STX_SIZE(stx,dict) > 1);
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
capa = XP_STX_SIZE(stx,dict) - 1;
|
|
|
|
tally = XP_STX_FROM_SMALLINT(XP_STX_WORD_AT(stx,dict,0));
|
|
|
|
if (capa <= tally + 1) {
|
2005-08-11 09:57:54 +00:00
|
|
|
__grow_dict (stx, dict);
|
2005-07-19 12:08:04 +00:00
|
|
|
/* refresh tally */
|
|
|
|
tally = XP_STX_FROM_SMALLINT(XP_STX_WORD_AT(stx,dict,0));
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
2005-07-19 12:08:04 +00:00
|
|
|
|
|
|
|
slot = __dict_find_slot (stx, dict, key);
|
|
|
|
|
|
|
|
assoc = XP_STX_WORD_AT(stx,dict,slot);
|
|
|
|
if (assoc == stx->nil) {
|
|
|
|
XP_STX_WORD_AT(stx,dict,slot) =
|
|
|
|
__new_association (stx, key, value);
|
|
|
|
XP_STX_WORD_AT(stx,dict,0) = XP_STX_TO_SMALLINT(tally + 1);
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
2005-07-19 12:08:04 +00:00
|
|
|
else XP_STX_WORD_AT(stx,assoc,XP_STX_ASSOCIATION_VALUE) = value;
|
|
|
|
|
|
|
|
return XP_STX_WORD_AT(stx,dict,slot);
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
void xp_stx_dict_traverse (
|
|
|
|
xp_stx_t* stx, xp_word_t dict,
|
2005-07-18 11:53:01 +00:00
|
|
|
void (*func) (xp_stx_t*,xp_word_t,void*), void* data)
|
|
|
|
{
|
2005-07-19 12:08:04 +00:00
|
|
|
xp_word_t index, assoc;
|
|
|
|
xp_word_t size = XP_STX_SIZE(stx,dict);
|
2005-07-18 11:53:01 +00:00
|
|
|
|
2005-07-19 12:08:04 +00:00
|
|
|
for (index = 1; index < size; index++) {
|
|
|
|
assoc = XP_STX_WORD_AT(stx,dict,index);
|
|
|
|
if (assoc == stx->nil) continue;
|
|
|
|
func (stx, assoc, data);
|
2005-07-18 11:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|