qse/ase/stx/hash.c

132 lines
3.0 KiB
C
Raw Normal View History

2005-05-08 15:54:44 +00:00
/*
2005-06-08 16:14:52 +00:00
* $Id: hash.c,v 1.20 2005-06-08 16:11:18 bacon Exp $
2005-05-08 15:54:44 +00:00
*/
#include <xp/stx/hash.h>
2005-05-10 06:08:57 +00:00
#include <xp/stx/object.h>
2005-05-15 18:37:00 +00:00
#include <xp/stx/misc.h>
2005-05-08 15:54:44 +00:00
2005-06-08 16:00:51 +00:00
xp_word_t xp_stx_new_pairlink (
xp_stx_t* stx, xp_word_t key, xp_word_t value)
2005-05-08 15:54:44 +00:00
{
2005-06-08 16:00:51 +00:00
xp_word_t x;
2005-05-22 15:03:20 +00:00
xp_stx_pairlink_t* obj;
2005-05-08 15:54:44 +00:00
2005-05-22 04:34:22 +00:00
x = xp_stx_alloc_word_object (stx, XP_STX_PAIRLINK_SIZE);
2005-05-22 15:03:20 +00:00
obj = (xp_stx_pairlink_t*)XP_STX_WORD_OBJECT(stx, x);
/*
2005-05-18 04:01:51 +00:00
XP_STX_CLASS(stx,x) = stx->class_pairlink;
2005-05-29 16:51:16 +00:00
XP_STX_WORDAT(stx,x,XP_STX_PAIRLINK_LINK) = stx->nil;
XP_STX_WORDAT(stx,x,XP_STX_PAIRLINK_KEY) = key;
XP_STX_WORDAT(stx,x,XP_STX_PAIRLINK_VALUE) = value;
2005-05-22 15:03:20 +00:00
*/
obj->header.class = stx->class_pairlink;
obj->link = stx->nil;
obj->key = key;
obj->value = value;
2005-05-08 15:54:44 +00:00
return x;
}
2005-05-10 06:02:19 +00:00
/* returns the entire link */
2005-06-08 16:00:51 +00:00
xp_word_t xp_stx_hash_lookup (
xp_stx_t* stx, xp_word_t table,
xp_word_t hash, xp_word_t key)
2005-05-08 15:54:44 +00:00
{
2005-06-08 16:00:51 +00:00
xp_word_t link;
2005-05-22 15:03:20 +00:00
xp_stx_pairlink_t* obj;
2005-05-08 15:54:44 +00:00
2005-06-08 16:00:51 +00:00
xp_assert (XP_STX_TYPE(stx,table) == XP_STX_WORD_INDEXED);
2005-05-08 15:54:44 +00:00
2005-05-10 06:08:57 +00:00
hash = hash % XP_STX_SIZE(stx,table);
2005-05-29 16:51:16 +00:00
link = XP_STX_WORDAT(stx,table,hash);
2005-05-09 15:55:04 +00:00
2005-05-10 06:02:19 +00:00
while (link != stx->nil) {
2005-05-22 15:03:20 +00:00
/*
2005-05-29 16:51:16 +00:00
if (XP_STX_WORDAT(stx,link,XP_STX_PAIRLINK_KEY) == key) return link;
link = XP_STX_WORDAT(stx,link,XP_STX_PAIRLINK_LINK);
2005-05-22 15:03:20 +00:00
*/
obj = (xp_stx_pairlink_t*)XP_STX_WORD_OBJECT(stx,link);
if (obj->key == key) return link;
link = obj->link;
}
return stx->nil; /* not found */
}
2005-06-08 16:00:51 +00:00
xp_word_t xp_stx_hash_lookup_symbol (
xp_stx_t* stx, xp_word_t table, const xp_char_t* name)
2005-05-22 15:03:20 +00:00
{
2005-06-08 16:00:51 +00:00
xp_word_t link, hash;
2005-05-22 15:03:20 +00:00
xp_stx_pairlink_t* obj;
xp_stx_char_object_t* tmp;
2005-06-08 16:00:51 +00:00
xp_assert (XP_STX_TYPE(stx,table) == XP_STX_WORD_INDEXED);
2005-05-22 15:03:20 +00:00
hash = xp_stx_strhash(name) % XP_STX_SIZE(stx,table);
2005-05-29 16:51:16 +00:00
link = XP_STX_WORDAT(stx,table,hash);
2005-05-22 15:03:20 +00:00
while (link != stx->nil) {
obj = (xp_stx_pairlink_t*)XP_STX_WORD_OBJECT(stx,link);
2005-06-08 16:00:51 +00:00
tmp = XP_CHAR_OBJECT(stx,obj->key);
2005-05-22 15:03:20 +00:00
if (tmp->header.class == stx->class_symbol &&
2005-06-08 16:14:52 +00:00
xp_strcmp (tmp->data, name) == 0) return link;
2005-05-22 15:03:20 +00:00
link = obj->link;
2005-05-15 18:37:00 +00:00
}
return stx->nil; /* not found */
}
2005-05-10 06:02:19 +00:00
void xp_stx_hash_insert (
2005-06-08 16:00:51 +00:00
xp_stx_t* stx, xp_word_t table,
xp_word_t hash, xp_word_t key, xp_word_t value)
2005-05-10 06:02:19 +00:00
{
2005-06-08 16:00:51 +00:00
xp_word_t link, next;
2005-05-08 15:54:44 +00:00
2005-06-08 16:00:51 +00:00
xp_assert (XP_STX_TYPE(stx,table) == XP_STX_WORD_INDEXED);
2005-05-10 06:02:19 +00:00
2005-05-10 06:08:57 +00:00
hash = hash % XP_STX_SIZE(stx,table);
2005-05-29 16:51:16 +00:00
link = XP_STX_WORDAT(stx,table,hash);
2005-05-10 06:02:19 +00:00
if (link == stx->nil) {
2005-05-29 16:51:16 +00:00
XP_STX_WORDAT(stx,table,hash) =
2005-05-18 04:01:51 +00:00
xp_stx_new_pairlink (stx, key, value);
2005-05-10 06:02:19 +00:00
}
else {
for (;;) {
2005-05-29 16:51:16 +00:00
if (XP_STX_WORDAT(stx,link,1) == key) {
XP_STX_WORDAT(stx,link,XP_STX_PAIRLINK_VALUE) = value;
2005-05-10 06:02:19 +00:00
break;
}
2005-05-29 16:51:16 +00:00
next = XP_STX_WORDAT(stx,link,XP_STX_PAIRLINK_LINK);
2005-05-10 06:02:19 +00:00
if (next == stx->nil) {
2005-05-29 16:51:16 +00:00
XP_STX_WORDAT(stx,link,XP_STX_PAIRLINK_LINK) =
2005-05-18 04:01:51 +00:00
xp_stx_new_pairlink (stx, key, value);
2005-05-10 06:02:19 +00:00
break;
}
2005-05-08 15:54:44 +00:00
2005-05-10 06:02:19 +00:00
link = next;
}
2005-05-08 15:54:44 +00:00
}
}
2005-05-12 15:25:06 +00:00
void xp_stx_hash_traverse (
2005-06-08 16:00:51 +00:00
xp_stx_t* stx, xp_word_t table,
void (*func) (xp_stx_t*,xp_word_t))
2005-05-12 15:25:06 +00:00
{
2005-06-08 16:00:51 +00:00
xp_word_t link;
xp_word_t size = XP_STX_SIZE(stx,table);
2005-05-12 15:25:06 +00:00
while (size-- > 0) {
2005-05-29 16:51:16 +00:00
link = XP_STX_WORDAT(stx,table,size);
2005-05-12 15:25:06 +00:00
while (link != stx->nil) {
func (stx,link);
2005-05-29 16:51:16 +00:00
link = XP_STX_WORDAT(stx,link,XP_STX_PAIRLINK_LINK);
2005-05-12 15:25:06 +00:00
}
}
}
2005-05-15 18:37:00 +00:00