From 100bfec39a3de3029a97e645e7fe33877d7bbc2f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 31 Aug 2000 11:08:27 -0300 Subject: new implementation for `next' --- ltable.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index 604129c5..3d2c4765 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.52 2000/08/07 20:21:34 roberto Exp roberto $ +** $Id: ltable.c,v 1.53 2000/08/09 19:16:57 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -114,12 +114,26 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { } -int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { - const TObject *v = luaH_get(L, t, key); - return (v == &luaO_nilobject) ? -1 : /* key not found */ - (int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node)); +Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) { + int i; + if (ttype(key) == TAG_NIL) + i = 0; /* first iteration */ + else { + const TObject *v = luaH_get(L, t, key); + if (v == &luaO_nilobject) + lua_error(L, "invalid key for `next'"); + i = (int)(((const char *)v - + (const char *)(&t->node[0].val)) / sizeof(Node)) + 1; + } + for (; isize; i++) { + Node *n = node(t, i); + if (ttype(val(n)) != TAG_NIL) + return n; + } + return NULL; /* no more elements */ } + /* ** try to remove a key without value from a table. To avoid problems with ** hash, change `key' for a number with the same hash. -- cgit v1.2.3-55-g6feb