qse/ase/stx/symbol.c

103 lines
2.0 KiB
C
Raw Normal View History

2005-05-17 16:18:56 +00:00
/*
2007-04-22 13:55:18 +00:00
* $Id: symbol.c,v 1.23 2007-04-22 13:55:18 bacon Exp $
2005-05-17 16:18:56 +00:00
*/
2007-03-22 11:21:59 +00:00
#include <ase/stx/symbol.h>
#include <ase/stx/object.h>
#include <ase/stx/misc.h>
2005-05-17 16:18:56 +00:00
2007-03-22 11:21:59 +00:00
static void __grow_symtab (ase_stx_t* stx)
2005-05-17 16:18:56 +00:00
{
2007-03-22 11:21:59 +00:00
ase_word_t capa, ncapa, i, j;
ase_word_t* nspace;
2005-05-17 16:18:56 +00:00
2005-08-11 09:57:54 +00:00
capa = stx->symtab.capacity;
ncapa = capa << 1;
2005-05-17 16:18:56 +00:00
2007-03-22 11:21:59 +00:00
nspace = (ase_word_t*)ase_malloc(ase_sizeof(ase_word_t) * ncapa);
2007-04-22 13:55:18 +00:00
if (nspace == ASE_NULL)
{
2005-08-11 09:57:54 +00:00
/* TODO: handle memory error */
}
2005-05-17 16:18:56 +00:00
2007-04-22 13:55:18 +00:00
for (i = 0; i < capa; i++)
{
2007-03-22 11:21:59 +00:00
ase_word_t x = stx->symtab.datum[i];
2005-08-11 09:57:54 +00:00
if (x == stx->nil) continue;
2005-05-23 14:43:03 +00:00
2007-03-22 11:21:59 +00:00
j = ase_stx_strxhash (
ASE_STX_DATA(stx,x), ASE_STX_SIZE(stx,x)) % ncapa;
2005-05-23 14:43:03 +00:00
2007-04-22 13:55:18 +00:00
while (1)
{
if (nspace[j] == stx->nil)
{
2005-08-11 09:57:54 +00:00
nspace[j] = x;
2005-05-23 14:43:03 +00:00
break;
}
2005-08-11 09:57:54 +00:00
j = (j % ncapa) + 1;
}
2005-05-23 14:43:03 +00:00
}
2005-08-11 09:57:54 +00:00
stx->symtab.capacity = ncapa;
2007-03-22 11:21:59 +00:00
ase_free (stx->symtab.datum);
2005-08-11 09:57:54 +00:00
stx->symtab.datum = nspace;
2005-05-17 16:18:56 +00:00
}
2007-03-22 11:21:59 +00:00
ase_word_t ase_stx_new_symbol (ase_stx_t* stx, const ase_char_t* name)
2005-05-18 04:01:51 +00:00
{
2007-03-22 11:21:59 +00:00
return ase_stx_new_symbolx (stx, name, ase_strlen(name));
2005-05-18 04:01:51 +00:00
}
2005-08-03 16:00:01 +00:00
2007-03-22 11:21:59 +00:00
ase_word_t ase_stx_new_symbolx (
ase_stx_t* stx, const ase_char_t* name, ase_word_t len)
2005-08-03 16:00:01 +00:00
{
2007-03-22 11:21:59 +00:00
ase_word_t capa, hash, index, size, x;
2005-08-03 16:00:01 +00:00
capa = stx->symtab.capacity;
size = stx->symtab.size;
2007-04-22 13:55:18 +00:00
if (capa <= size + 1)
{
2005-08-03 16:00:01 +00:00
__grow_symtab (stx);
2005-08-11 09:57:54 +00:00
capa = stx->symtab.capacity;
2005-08-03 16:00:01 +00:00
}
2007-03-22 11:21:59 +00:00
hash = ase_stx_strxhash(name,len);
2005-08-03 16:00:01 +00:00
index = hash % stx->symtab.capacity;
2007-04-22 13:55:18 +00:00
while (1)
{
2005-08-11 09:57:54 +00:00
x = stx->symtab.datum[index];
2007-04-22 13:55:18 +00:00
if (x == stx->nil)
{
2005-08-11 09:57:54 +00:00
/* insert a new item into an empty slot */
2007-03-22 11:21:59 +00:00
x = ase_stx_alloc_char_objectx (stx, name, len);
ASE_STX_CLASS(stx,x) = stx->class_symbol;
2005-08-11 09:57:54 +00:00
stx->symtab.datum[index] = x;
stx->symtab.size++;
break;
}
2005-08-03 16:00:01 +00:00
2007-03-22 11:21:59 +00:00
if (ase_strxncmp(name, len,
ASE_STX_DATA(stx,x), ASE_STX_SIZE(stx,x)) == 0) break;
2005-08-03 16:00:01 +00:00
2005-08-11 09:57:54 +00:00
index = (index % stx->symtab.capacity) + 1;
2005-08-03 16:00:01 +00:00
}
2005-08-11 09:57:54 +00:00
return x;
2005-08-03 16:00:01 +00:00
}
2005-08-11 09:57:54 +00:00
2007-03-22 11:21:59 +00:00
void ase_stx_traverse_symbol_table (
ase_stx_t* stx, void (*func) (ase_stx_t*,ase_word_t,void*), void* data)
2005-08-11 09:57:54 +00:00
{
2007-03-22 11:21:59 +00:00
ase_word_t index, x;
2005-08-11 09:57:54 +00:00
2007-04-22 13:55:18 +00:00
for (index = 0; index < stx->symtab.capacity; index++)
{
2005-08-11 09:57:54 +00:00
x = stx->symtab.datum[index];
if (x != stx->nil) func (stx, x, data);
}
}