double hashing + tables do not grow if there are empty slots

This commit is contained in:
Roberto Ierusalimschy
1997-05-08 17:43:30 -03:00
parent 12d9731a49
commit 9747f3c87a

185
hash.c
View File

@@ -3,7 +3,7 @@
** hash manager for lua ** hash manager for lua
*/ */
char *rcs_hash="$Id: hash.c,v 2.40 1997/04/04 15:35:37 roberto Exp roberto $"; char *rcs_hash="$Id: hash.c,v 2.41 1997/04/06 14:08:08 roberto Exp roberto $";
#include "luamem.h" #include "luamem.h"
@@ -33,7 +33,7 @@ static Hash *listhead = NULL;
/* hash dimensions values */ /* hash dimensions values */
static Long dimensions[] = static Long dimensions[] =
{3L, 5L, 7L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L, {5L, 11L, 23L, 47L, 97L, 197L, 397L, 797L, 1597L, 3203L, 6421L,
12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L, 12853L, 25717L, 51437L, 102811L, 205619L, 411233L, 822433L,
1644817L, 3289613L, 6579211L, 13158023L, MAX_INT}; 1644817L, 3289613L, 6579211L, 13158023L, MAX_INT};
@@ -49,27 +49,6 @@ int luaI_redimension (int nhash)
return 0; /* to avoid warnings */ return 0; /* to avoid warnings */
} }
static int hashindex (Hash *t, TObject *ref) /* hash function */
{
long int h;
switch (ttype(ref)) {
case LUA_T_NUMBER:
h = (long int)nvalue(ref); break;
case LUA_T_STRING: case LUA_T_USERDATA:
h = tsvalue(ref)->hash; break;
case LUA_T_FUNCTION:
h = (IntPoint)ref->value.tf; break;
case LUA_T_CFUNCTION:
h = (IntPoint)fvalue(ref); break;
case LUA_T_ARRAY:
h = (IntPoint)avalue(ref); break;
default:
lua_error ("unexpected type to index table");
h = 0; /* UNREACHEABLE */
}
if (h < 0) h = -h;
return h%nhash(t); /* make it a valid index */
}
int lua_equalObj (TObject *t1, TObject *t2) int lua_equalObj (TObject *t1, TObject *t2)
{ {
@@ -88,16 +67,44 @@ int lua_equalObj (TObject *t1, TObject *t2)
} }
} }
static int present (Hash *t, TObject *ref)
{ static long int hashindex (TObject *ref)
int h = hashindex(t, ref); {
while (ttype(ref(node(t, h))) != LUA_T_NIL) long int h;
{ switch (ttype(ref)) {
if (lua_equalObj(ref, ref(node(t, h)))) case LUA_T_NUMBER:
return h; h = (long int)nvalue(ref); break;
h = (h+1) % nhash(t); case LUA_T_STRING: case LUA_T_USERDATA:
} h = tsvalue(ref)->hash; break;
return h; case LUA_T_FUNCTION:
h = (IntPoint)ref->value.tf; break;
case LUA_T_CFUNCTION:
h = (IntPoint)fvalue(ref); break;
case LUA_T_ARRAY:
h = (IntPoint)avalue(ref); break;
default:
lua_error ("unexpected type to index table");
h = 0; /* UNREACHEABLE */
}
if (h < 0) h = -h;
return h;
}
static int present (Hash *t, TObject *key)
{
long int h = hashindex(key);
int tsize = nhash(t);
int h1 = h%tsize;
TObject *rf = ref(node(t, h1));
if (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf)) {
int h2 = h%(tsize-2) + 1;
do {
h1 = (h1+h2)%tsize;
rf = ref(node(t, h1));
} while (ttype(rf) != LUA_T_NIL && !lua_equalObj(key, rf));
}
return h1;
} }
@@ -221,22 +228,35 @@ Hash *lua_createarray (int nhash)
/* /*
** Re-hash ** Rehash:
** Check if table has deleted slots. It it has, it does not need to
** grow, since rehash will reuse them.
*/ */
static int emptyslots (Hash *t)
{
int i;
for (i=nhash(t)-1; i>=0; i--) {
Node *n = node(t, i);
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) == LUA_T_NIL)
return 1;
}
return 0;
}
static void rehash (Hash *t) static void rehash (Hash *t)
{ {
int i; int nold = nhash(t);
int nold = nhash(t); Node *vold = nodevector(t);
Node *vold = nodevector(t); int i;
nhash(t) = luaI_redimension(nhash(t)); if (!emptyslots(t))
nodevector(t) = hashnodecreate(nhash(t)); nhash(t) = luaI_redimension(nhash(t));
for (i=0; i<nold; i++) nodevector(t) = hashnodecreate(nhash(t));
{ for (i=0; i<nold; i++) {
Node *n = vold+i; Node *n = vold+i;
if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL) if (ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL)
*node(t, present(t, ref(n))) = *n; /* copy old node to new hahs */ *node(t, present(t, ref(n))) = *n; /* copy old node to new hash */
} }
luaI_free(vold); luaI_free(vold);
} }
/* /*
@@ -257,23 +277,17 @@ TObject *lua_hashget (Hash *t, TObject *ref)
*/ */
TObject *lua_hashdefine (Hash *t, TObject *ref) TObject *lua_hashdefine (Hash *t, TObject *ref)
{ {
int h; Node *n = node(t, present(t, ref));
Node *n; if (ttype(ref(n)) == LUA_T_NIL) {
h = present(t, ref); nuse(t)++;
n = node(t, h); if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) {
if (ttype(ref(n)) == LUA_T_NIL) rehash(t);
{ n = node(t, present(t, ref));
nuse(t)++; }
if ((float)nuse(t) > (float)nhash(t)*REHASH_LIMIT) *ref(n) = *ref;
{ ttype(val(n)) = LUA_T_NIL;
rehash(t);
h = present(t, ref);
n = node(t, h);
} }
*ref(n) = *ref; return (val(n));
ttype(val(n)) = LUA_T_NIL;
}
return (val(n));
} }
@@ -285,33 +299,30 @@ TObject *lua_hashdefine (Hash *t, TObject *ref)
*/ */
static void hashnext (Hash *t, int i) static void hashnext (Hash *t, int i)
{ {
if (i >= nhash(t)) Node *n;
return; int tsize = nhash(t);
while (ttype(ref(node(t,i))) == LUA_T_NIL || if (i >= tsize)
ttype(val(node(t,i))) == LUA_T_NIL) return;
{ n = node(t, i);
if (++i >= nhash(t)) while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) {
return; if (++i >= tsize)
} return;
luaI_pushobject(ref(node(t,i))); n = node(t, i);
luaI_pushobject(val(node(t,i))); }
luaI_pushobject(ref(node(t,i)));
luaI_pushobject(val(node(t,i)));
} }
void lua_next (void) void lua_next (void)
{ {
Hash *t; Hash *t;
lua_Object o = lua_getparam(1); lua_Object o = lua_getparam(1);
lua_Object r = lua_getparam(2); lua_Object r = lua_getparam(2);
luaL_arg_check(lua_istable(o), 1, "table expected"); luaL_arg_check(lua_istable(o), 1, "table expected");
luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected"); luaL_arg_check(r != LUA_NOOBJECT, 2, "value expected");
t = avalue(luaI_Address(o)); t = avalue(luaI_Address(o));
if (lua_isnil(r)) if (lua_isnil(r))
{ hashnext(t, 0);
hashnext(t, 0); else
} hashnext(t, present(t, luaI_Address(r))+1);
else
{
int h = present (t, luaI_Address(r));
hashnext(t, h+1);
}
} }