From c542aac0b935a65203244c130cdfa700113f0bc8 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Jun 2000 17:07:53 -0300 Subject: collect dead indices in tables --- ltable.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index a7d4f36a..9929146d 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.42 2000/05/11 18:57:19 roberto Exp roberto $ +** $Id: ltable.c,v 1.43 2000/05/24 13:54:49 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -121,6 +121,28 @@ int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { (int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node)); } +/* +** try to remove a key without value from a table. To avoid problems with +** hash, change `key' for a number with the same hash. +*/ +void luaH_remove (Hash *t, TObject *key) { + /* do not remove numbers */ + if (ttype(key) != TAG_NUMBER) { + /* try to find a number `n' with the same hash as `key' */ + Node *mp = luaH_mainposition(t, key); + int n = mp - &t->node[0]; + /* make sure `n' is not in `t' */ + while (luaH_getnum(t, n) != &luaO_nilobject) { + if (t->size >= MAX_INT-n) + return; /* give up; (to avoid overflow) */ + n += t->size; + } + ttype(key) = TAG_NUMBER; + nvalue(key) = n; + LUA_ASSERT(L, luaH_mainposition(t, key) == mp, "cannot change hash"); + } +} + static void setnodevector (lua_State *L, Hash *t, lint32 size) { int i; -- cgit v1.2.3-55-g6feb