qse/ase/stx/hash.c

74 lines
1.5 KiB
C
Raw Normal View History

2005-05-08 15:54:44 +00:00
/*
2005-05-10 08:21:10 +00:00
* $Id: hash.c,v 1.5 2005-05-10 08:21:10 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>
#include <xp/bas/assert.h>
2005-05-08 15:54:44 +00:00
2005-05-10 06:02:19 +00:00
xp_stx_word_t xp_stx_new_link (
xp_stx_t* stx, xp_stx_word_t key, xp_stx_word_t value)
2005-05-08 15:54:44 +00:00
{
xp_stx_word_t x;
2005-05-10 06:08:57 +00:00
x = xp_stx_alloc_object (stx, 3);
XP_STX_CLASS(stx,x) = stx->link_class;
2005-05-08 15:54:44 +00:00
XP_STX_AT(stx,x,0) = key;
XP_STX_AT(stx,x,1) = value;
/* XP_STX_AT(stx,x,2) = stx->nil; */
return x;
}
2005-05-10 06:02:19 +00:00
/* returns the entire link */
xp_stx_word_t xp_stx_hash_lookup (
xp_stx_t* stx, xp_stx_word_t table,
xp_stx_word_t hash, xp_stx_word_t key)
2005-05-08 15:54:44 +00:00
{
2005-05-10 08:21:10 +00:00
xp_stx_word_t link;
2005-05-08 15:54:44 +00:00
2005-05-10 06:02:19 +00:00
xp_assert (XP_STX_TYPE(stx,table) == XP_STX_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-10 08:21:10 +00:00
link = XP_STX_AT(stx,table,hash);
2005-05-09 15:55:04 +00:00
2005-05-10 06:02:19 +00:00
while (link != stx->nil) {
if (XP_STX_AT(stx,link,0) == key) return link;
link = XP_STX_AT(stx,link,2);
2005-05-09 15:55:04 +00:00
}
2005-05-08 15:54:44 +00:00
2005-05-10 06:02:19 +00:00
return stx->nil; /* not found */
}
2005-05-09 15:55:04 +00:00
2005-05-10 06:02:19 +00:00
void xp_stx_hash_insert (
xp_stx_t* stx, xp_stx_word_t table,
xp_stx_word_t hash, xp_stx_word_t key, xp_stx_word_t value)
{
2005-05-10 08:21:10 +00:00
xp_stx_word_t link, next;
2005-05-08 15:54:44 +00:00
2005-05-10 06:02:19 +00:00
xp_assert (XP_STX_TYPE(stx,table) == XP_STX_INDEXED);
2005-05-10 06:08:57 +00:00
hash = hash % XP_STX_SIZE(stx,table);
2005-05-10 08:21:10 +00:00
link = XP_STX_AT(stx,table,hash);
2005-05-10 06:02:19 +00:00
if (link == stx->nil) {
2005-05-10 08:21:10 +00:00
XP_STX_AT(stx,table,hash) = xp_stx_new_link (stx, key, value);
2005-05-10 06:02:19 +00:00
}
else {
for (;;) {
if (XP_STX_AT(stx,link,0) == key) {
XP_STX_AT(stx,link,1) = value;
break;
}
next = XP_STX_AT(stx,link,2);
if (next == stx->nil) {
2005-05-10 06:08:57 +00:00
XP_STX_AT(stx,link,2) = xp_stx_new_link (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
}
}