From ef62b340e0a6b7b18931000dcbb19c4703bfe0e8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 24 May 2000 10:54:49 -0300 Subject: code cleaner for 16 bits. --- ltable.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index 3af51b62..a7d4f36a 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.41 2000/05/08 19:32:53 roberto Exp roberto $ +** $Id: ltable.c,v 1.42 2000/05/11 18:57:19 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -122,10 +122,12 @@ int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { } -static void setnodevector (lua_State *L, Hash *t, int size) { +static void setnodevector (lua_State *L, Hash *t, lint32 size) { int i; + if (size > MAX_INT) + lua_error(L, "table overflow"); t->node = luaM_newvector(L, size, Node); - for (i=0; inode[i].key) = ttype(&t->node[i].val) = TAG_NIL; t->node[i].next = NULL; } @@ -153,7 +155,7 @@ void luaH_free (lua_State *L, Hash *t) { } -static int newsize (const Hash *t) { +static int numuse (const Hash *t) { Node *v = t->node; int size = t->size; int realuse = 0; @@ -162,16 +164,24 @@ static int newsize (const Hash *t) { if (ttype(&v[i].val) != TAG_NIL) realuse++; } - return luaO_power2(realuse+realuse/4+1); + return realuse; } static void rehash (lua_State *L, Hash *t) { int oldsize = t->size; Node *nold = t->node; + int newsize = numuse(t); int i; + LUA_ASSERT(L, newsize<=oldsize, "wrong count"); + if (newsize >= oldsize-oldsize/4) /* using more than 3/4? */ + setnodevector(L, t, (lint32)oldsize*2); + else if (newsize <= oldsize/4 && /* less than 1/4? */ + oldsize > MINPOWER2) + setnodevector(L, t, oldsize/2); + else + setnodevector(L, t, oldsize); L->nblocks -= gcsize(L, oldsize); - setnodevector(L, t, newsize(t)); /* create new array of nodes */ for (i=0; ival) != TAG_NIL) @@ -249,3 +259,4 @@ void luaH_setint (lua_State *L, Hash *t, int key, const TObject *val) { const TObject *luaH_getglobal (lua_State *L, const char *name) { return luaH_getstr(L->gt, luaS_new(L, name)); } + -- cgit v1.2.3-55-g6feb