From 1b45e967b4dcb234551c2a731147b111584f4145 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 25 Jan 1999 10:30:11 -0200 Subject: [PATCH] table entries with ref=null always have val=null too. --- lbuiltin.c | 4 ++-- ltable.c | 43 +++++++++++++++---------------------------- ltable.h | 6 ++++-- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/lbuiltin.c b/lbuiltin.c index 8526ae27..f67059ca 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.45 1999/01/04 17:34:49 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.46 1999/01/22 18:46:11 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -348,7 +348,7 @@ static void luaB_foreach (void) { luaD_checkstack(3); /* for f, ref, and val */ for (i=0; inhash; i++) { Node *nd = &(a->node[i]); - if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) { + if (ttype(val(nd)) != LUA_T_NIL) { *(L->stack.top++) = *f; *(L->stack.top++) = *ref(nd); *(L->stack.top++) = *val(nd); diff --git a/ltable.c b/ltable.c index 205ab574..3363be88 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.17 1999/01/04 12:54:33 roberto Exp $ +** $Id: ltable.c,v 1.18 1999/01/22 18:47:23 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -53,20 +53,20 @@ static long int hashindex (TObject *ref) { } -static Node *present (Hash *t, TObject *key) { +Node *luaH_present (Hash *t, TObject *ref) { int tsize = nhash(t); - long int h = hashindex(key); + long int h = hashindex(ref); int h1 = h%tsize; Node *n = node(t, h1); - /* keep looking until an entry with "ref" equal to key or nil */ - if ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n)) + /* keep looking until an entry with "ref" equal to ref or nil */ + if ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n)) : ttype(ref(n)) != LUA_T_NIL)) { int h2 = h%(tsize-2) + 1; do { h1 += h2; if (h1 >= tsize) h1 -= tsize; n = node(t, h1); - } while ((ttype(ref(n)) == ttype(key) ? !luaO_equalval(key, ref(n)) + } while ((ttype(ref(n)) == ttype(ref) ? !luaO_equalval(ref, ref(n)) : ttype(ref(n)) != LUA_T_NIL)); } return n; @@ -88,7 +88,7 @@ static Node *hashnodecreate (int nhash) { Node *v = luaM_newvector(nhash, Node); int i; for (i=0; i (long)nhash(t)*2L) { rehash(t); - n = present(t, ref); + n = luaH_present(t, ref); } nuse(t)++; *ref(n) = *ref; - ttype(val(n)) = LUA_T_NIL; } return (val(n)); } @@ -175,7 +163,7 @@ static Node *hashnext (Hash *t, int i) { if (i >= tsize) return NULL; n = node(t, i); - while (ttype(ref(n)) == LUA_T_NIL || ttype(val(n)) == LUA_T_NIL) { + while (ttype(val(n)) == LUA_T_NIL) { if (++i >= tsize) return NULL; n = node(t, i); @@ -187,9 +175,8 @@ Node *luaH_next (Hash *t, TObject *r) { if (ttype(r) == LUA_T_NIL) return hashnext(t, 0); else { - Node *n = present(t, r); - luaL_arg_check(ttype(ref(n))!=LUA_T_NIL && ttype(val(n))!=LUA_T_NIL, - 2, "key not found"); + Node *n = luaH_present(t, r); + luaL_arg_check(ttype(val(n)) != LUA_T_NIL, 2, "key not found"); return hashnext(t, (n-(t->node))+1); } } diff --git a/ltable.h b/ltable.h index 87a01f83..e03f9d5c 100644 --- a/ltable.h +++ b/ltable.h @@ -1,5 +1,5 @@ /* -** $Id: ltable.h,v 1.7 1998/12/30 13:14:46 roberto Exp $ +** $Id: ltable.h,v 1.8 1999/01/04 12:54:33 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -15,9 +15,11 @@ #define val(n) (&(n)->val) #define nhash(t) ((t)->nhash) +#define luaH_get(t,ref) (val(luaH_present((t), (ref)))) + Hash *luaH_new (int nhash); void luaH_free (Hash *frees); -TObject *luaH_get (Hash *t, TObject *ref); +Node *luaH_present (Hash *t, TObject *ref); TObject *luaH_set (Hash *t, TObject *ref); Node *luaH_next (Hash *t, TObject *r); void luaH_setint (Hash *t, int ref, TObject *val);