mirror of https://github.com/rusefi/lua.git
new function 'table.maxn'
This commit is contained in:
parent
361a9adba7
commit
0fae476ed4
24
ltablib.c
24
ltablib.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: ltablib.c,v 1.34 2005/08/15 14:12:32 roberto Exp roberto $
|
** $Id: ltablib.c,v 1.35 2005/08/26 17:36:32 roberto Exp roberto $
|
||||||
** Library for Table Manipulation
|
** Library for Table Manipulation
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -40,9 +40,7 @@ static int foreach (lua_State *L) {
|
||||||
luaL_checktype(L, 1, LUA_TTABLE);
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
luaL_checktype(L, 2, LUA_TFUNCTION);
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
||||||
lua_pushnil(L); /* first key */
|
lua_pushnil(L); /* first key */
|
||||||
for (;;) {
|
while (lua_next(L, 1)) {
|
||||||
if (lua_next(L, 1) == 0)
|
|
||||||
return 0;
|
|
||||||
lua_pushvalue(L, 2); /* function */
|
lua_pushvalue(L, 2); /* function */
|
||||||
lua_pushvalue(L, -3); /* key */
|
lua_pushvalue(L, -3); /* key */
|
||||||
lua_pushvalue(L, -3); /* value */
|
lua_pushvalue(L, -3); /* value */
|
||||||
|
@ -51,6 +49,23 @@ static int foreach (lua_State *L) {
|
||||||
return 1;
|
return 1;
|
||||||
lua_pop(L, 2); /* remove value and result */
|
lua_pop(L, 2); /* remove value and result */
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int maxn (lua_State *L) {
|
||||||
|
lua_Number max = 0;
|
||||||
|
luaL_checktype(L, 1, LUA_TTABLE);
|
||||||
|
lua_pushnil(L); /* first key */
|
||||||
|
while (lua_next(L, 1)) {
|
||||||
|
lua_pop(L, 1); /* remove value */
|
||||||
|
if (lua_type(L, -1) == LUA_TNUMBER) {
|
||||||
|
lua_Number v = lua_tonumber(L, -1);
|
||||||
|
if (v > max) max = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushnumber(L, max);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -241,6 +256,7 @@ static const luaL_Reg tab_funcs[] = {
|
||||||
{"foreach", foreach},
|
{"foreach", foreach},
|
||||||
{"foreachi", foreachi},
|
{"foreachi", foreachi},
|
||||||
{"getn", getn},
|
{"getn", getn},
|
||||||
|
{"maxn", maxn},
|
||||||
{"insert", tinsert},
|
{"insert", tinsert},
|
||||||
{"remove", tremove},
|
{"remove", tremove},
|
||||||
{"setn", setn},
|
{"setn", setn},
|
||||||
|
|
Loading…
Reference in New Issue