1994-07-20 01:24:17 +04:00
|
|
|
/*
|
|
|
|
** tree.c
|
|
|
|
** TecCGraf - PUC-Rio
|
|
|
|
*/
|
|
|
|
|
1996-03-14 18:56:26 +03:00
|
|
|
char *rcs_tree="$Id: tree.c,v 1.19 1996/02/22 20:34:33 roberto Exp $";
|
1994-07-20 01:24:17 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
1994-11-16 20:39:16 +03:00
|
|
|
#include "mem.h"
|
1994-07-20 01:24:17 +04:00
|
|
|
#include "lua.h"
|
|
|
|
#include "tree.h"
|
1996-02-14 16:35:51 +03:00
|
|
|
#include "lex.h"
|
1996-02-12 21:32:40 +03:00
|
|
|
#include "hash.h"
|
1994-10-18 20:36:11 +03:00
|
|
|
#include "table.h"
|
1994-07-20 01:24:17 +04:00
|
|
|
|
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
#define NUM_HASHS 64
|
1994-07-20 01:24:17 +04:00
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
typedef struct {
|
|
|
|
int size;
|
|
|
|
int nuse; /* number of elements (including EMPTYs) */
|
|
|
|
TaggedString **hash;
|
|
|
|
} stringtable;
|
1994-11-16 21:09:11 +03:00
|
|
|
|
1996-02-14 16:35:51 +03:00
|
|
|
static int initialized = 0;
|
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
static stringtable string_root[NUM_HASHS];
|
1994-11-16 21:09:11 +03:00
|
|
|
|
1996-02-14 22:11:09 +03:00
|
|
|
static TaggedString EMPTY = {NOT_USED, NOT_USED, 0, 2, {0}};
|
1994-07-20 01:24:17 +04:00
|
|
|
|
1996-02-14 16:35:51 +03:00
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
static unsigned long hash (char *str)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
1996-02-12 21:32:40 +03:00
|
|
|
unsigned long h = 0;
|
|
|
|
while (*str)
|
|
|
|
h = ((h<<5)-h)^(unsigned char)*(str++);
|
|
|
|
return h;
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
|
1996-02-14 16:35:51 +03:00
|
|
|
static void initialize (void)
|
|
|
|
{
|
|
|
|
initialized = 1;
|
|
|
|
luaI_addReserved();
|
|
|
|
luaI_initsymbol();
|
|
|
|
luaI_initconstant();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
static void grow (stringtable *tb)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
1996-02-12 21:32:40 +03:00
|
|
|
int newsize = luaI_redimension(tb->size);
|
|
|
|
TaggedString **newhash = newvector(newsize, TaggedString *);
|
|
|
|
int i;
|
|
|
|
for (i=0; i<newsize; i++)
|
|
|
|
newhash[i] = NULL;
|
1996-02-22 23:34:33 +03:00
|
|
|
/* rehash */
|
|
|
|
tb->nuse = 0;
|
|
|
|
for (i=0; i<tb->size; i++)
|
|
|
|
if (tb->hash[i] != NULL && tb->hash[i] != &EMPTY)
|
|
|
|
{
|
|
|
|
int h = tb->hash[i]->hash%newsize;
|
|
|
|
while (newhash[h])
|
|
|
|
h = (h+1)%newsize;
|
|
|
|
newhash[h] = tb->hash[i];
|
|
|
|
tb->nuse++;
|
|
|
|
}
|
|
|
|
luaI_free(tb->hash);
|
1996-02-12 21:32:40 +03:00
|
|
|
tb->size = newsize;
|
|
|
|
tb->hash = newhash;
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
static TaggedString *insert (char *str, stringtable *tb)
|
|
|
|
{
|
|
|
|
TaggedString *ts;
|
|
|
|
unsigned long h = hash(str);
|
|
|
|
int i;
|
|
|
|
int j = -1;
|
|
|
|
if ((Long)tb->nuse*3 >= (Long)tb->size*2)
|
1996-02-14 16:35:51 +03:00
|
|
|
{
|
|
|
|
if (!initialized)
|
|
|
|
initialize();
|
1996-02-12 21:32:40 +03:00
|
|
|
grow(tb);
|
1996-02-14 16:35:51 +03:00
|
|
|
}
|
1996-02-12 21:32:40 +03:00
|
|
|
i = h%tb->size;
|
|
|
|
while (tb->hash[i])
|
|
|
|
{
|
|
|
|
if (tb->hash[i] == &EMPTY)
|
|
|
|
j = i;
|
1996-02-14 22:11:09 +03:00
|
|
|
else if (strcmp(str, tb->hash[i]->str) == 0)
|
1996-02-12 21:32:40 +03:00
|
|
|
return tb->hash[i];
|
|
|
|
i = (i+1)%tb->size;
|
|
|
|
}
|
|
|
|
/* not found */
|
1996-03-14 18:56:26 +03:00
|
|
|
lua_pack();
|
1996-02-12 21:32:40 +03:00
|
|
|
if (j != -1) /* is there an EMPTY space? */
|
|
|
|
i = j;
|
|
|
|
else
|
|
|
|
tb->nuse++;
|
|
|
|
ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(str));
|
|
|
|
strcpy(ts->str, str);
|
|
|
|
ts->marked = 0;
|
|
|
|
ts->hash = h;
|
|
|
|
ts->varindex = ts->constindex = NOT_USED;
|
|
|
|
return ts;
|
|
|
|
}
|
1994-07-20 01:24:17 +04:00
|
|
|
|
1996-02-12 21:32:40 +03:00
|
|
|
TaggedString *lua_createstring (char *str)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
1996-02-12 21:32:40 +03:00
|
|
|
return insert(str, &string_root[(unsigned)str[0]%NUM_HASHS]);
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Garbage collection function.
|
1994-11-16 21:09:11 +03:00
|
|
|
** This function traverse the string list freeing unindexed strings
|
1994-07-20 01:24:17 +04:00
|
|
|
*/
|
1995-01-12 17:19:04 +03:00
|
|
|
Long lua_strcollector (void)
|
1994-07-20 01:24:17 +04:00
|
|
|
{
|
1995-01-12 17:19:04 +03:00
|
|
|
Long counter = 0;
|
1996-02-12 21:32:40 +03:00
|
|
|
int i;
|
|
|
|
for (i=0; i<NUM_HASHS; i++)
|
1994-11-16 21:09:11 +03:00
|
|
|
{
|
1996-02-12 21:32:40 +03:00
|
|
|
stringtable *tb = &string_root[i];
|
|
|
|
int j;
|
|
|
|
for (j=0; j<tb->size; j++)
|
1994-11-16 21:09:11 +03:00
|
|
|
{
|
1996-02-12 21:32:40 +03:00
|
|
|
TaggedString *t = tb->hash[j];
|
1996-02-14 22:11:09 +03:00
|
|
|
if (t != NULL && t->marked <= 1)
|
1996-02-12 21:32:40 +03:00
|
|
|
{
|
|
|
|
if (t->marked)
|
|
|
|
t->marked = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
luaI_free(t);
|
|
|
|
tb->hash[j] = &EMPTY;
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
}
|
1994-11-16 21:09:11 +03:00
|
|
|
}
|
|
|
|
}
|
1994-11-17 16:58:57 +03:00
|
|
|
return counter;
|
1994-07-20 01:24:17 +04:00
|
|
|
}
|
|
|
|
|