MCPcopy Create free account
hub / github.com/DFHack/dfhack / findindex

Function findindex

depends/lua/src/ltable.c:159–183  ·  view source on GitHub ↗

** returns the index of a 'key' for table traversals. First goes all ** elements in the array part, then elements in the hash part. The ** beginning of a traversal is signaled by 0. */

Source from the content-addressed store, hash-verified

157** beginning of a traversal is signaled by 0.
158*/
159static unsigned int findindex (lua_State *L, Table *t, StkId key) {
160 unsigned int i;
161 if (ttisnil(key)) return 0; /* first iteration */
162 i = arrayindex(key);
163 if (i != 0 && i <= t->sizearray) /* is 'key' inside array part? */
164 return i; /* yes; that's the index */
165 else {
166 int nx;
167 Node *n = mainposition(t, key);
168 for (;;) { /* check whether 'key' is somewhere in the chain */
169 /* key may be dead already, but it is ok to use it in 'next' */
170 if (luaV_rawequalobj(gkey(n), key) ||
171 (ttisdeadkey(gkey(n)) && iscollectable(key) &&
172 deadvalue(gkey(n)) == gcvalue(key))) {
173 i = cast_int(n - gnode(t, 0)); /* key index in hash table */
174 /* hash elements are numbered after array ones */
175 return (i + 1) + t->sizearray;
176 }
177 nx = gnext(n);
178 if (nx == 0)
179 luaG_runerror(L, "invalid key to 'next'"); /* key not found */
180 else n += nx;
181 }
182 }
183}
184
185
186int luaH_next (lua_State *L, Table *t, StkId key) {

Callers 1

luaH_nextFunction · 0.85

Calls 3

arrayindexFunction · 0.85
mainpositionFunction · 0.85
luaG_runerrorFunction · 0.85

Tested by

no test coverage detected