diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-31 11:08:27 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-31 11:08:27 -0300 |
commit | 100bfec39a3de3029a97e645e7fe33877d7bbc2f (patch) | |
tree | 01c2c9bb63f71d3fb186b8f5be0b7577978f8a19 /ltable.c | |
parent | a290b84c670bbf0770d76429e7282c555a80058e (diff) | |
download | lua-100bfec39a3de3029a97e645e7fe33877d7bbc2f.tar.gz lua-100bfec39a3de3029a97e645e7fe33877d7bbc2f.tar.bz2 lua-100bfec39a3de3029a97e645e7fe33877d7bbc2f.zip |
new implementation for `next'
Diffstat (limited to 'ltable.c')
-rw-r--r-- | ltable.c | 24 |
1 files changed, 19 insertions, 5 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.52 2000/08/07 20:21:34 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.53 2000/08/09 19:16:57 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -114,12 +114,26 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { | |||
114 | } | 114 | } |
115 | 115 | ||
116 | 116 | ||
117 | int luaH_pos (lua_State *L, const Hash *t, const TObject *key) { | 117 | Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) { |
118 | const TObject *v = luaH_get(L, t, key); | 118 | int i; |
119 | return (v == &luaO_nilobject) ? -1 : /* key not found */ | 119 | if (ttype(key) == TAG_NIL) |
120 | (int)(((const char *)v - (const char *)(&t->node[0].val))/sizeof(Node)); | 120 | i = 0; /* first iteration */ |
121 | else { | ||
122 | const TObject *v = luaH_get(L, t, key); | ||
123 | if (v == &luaO_nilobject) | ||
124 | lua_error(L, "invalid key for `next'"); | ||
125 | i = (int)(((const char *)v - | ||
126 | (const char *)(&t->node[0].val)) / sizeof(Node)) + 1; | ||
127 | } | ||
128 | for (; i<t->size; i++) { | ||
129 | Node *n = node(t, i); | ||
130 | if (ttype(val(n)) != TAG_NIL) | ||
131 | return n; | ||
132 | } | ||
133 | return NULL; /* no more elements */ | ||
121 | } | 134 | } |
122 | 135 | ||
136 | |||
123 | /* | 137 | /* |
124 | ** try to remove a key without value from a table. To avoid problems with | 138 | ** try to remove a key without value from a table. To avoid problems with |
125 | ** hash, change `key' for a number with the same hash. | 139 | ** hash, change `key' for a number with the same hash. |