/* ** $Id: ltablib.c,v 1.25 2004/05/10 18:06:14 roberto Exp roberto $ ** Library for Table Manipulation ** See Copyright Notice in lua.h */ #include #define ltablib_c #define LUA_LIB #include "lua.h" #include "lauxlib.h" #include "lualib.h" #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) static int luaB_foreachi (lua_State *L) { int i; int n = aux_getn(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); for (i=LUA_FIRSTINDEX; i < n+LUA_FIRSTINDEX; i++) { lua_pushvalue(L, 2); /* function */ lua_pushinteger(L, i); /* 1st argument */ lua_rawgeti(L, 1, i); /* 2nd argument */ lua_call(L, 2, 1); if (!lua_isnil(L, -1)) return 1; lua_pop(L, 1); /* remove nil result */ } return 0; } static int luaB_foreach (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_checktype(L, 2, LUA_TFUNCTION); lua_pushnil(L); /* first key */ for (;;) { if (lua_next(L, 1) == 0) return 0; lua_pushvalue(L, 2); /* function */ lua_pushvalue(L, -3); /* key */ lua_pushvalue(L, -3); /* value */ lua_call(L, 2, 1); if (!lua_isnil(L, -1)) return 1; lua_pop(L, 2); /* remove value and result */ } } static int luaB_getn (lua_State *L) { lua_pushinteger(L, aux_getn(L, 1)); return 1; } static int luaB_setn (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_setn(L, 1, luaL_checkint(L, 2)); lua_pushvalue(L, 1); return 1; } static int luaB_tinsert (lua_State *L) { int v = lua_gettop(L); /* number of arguments */ int e = aux_getn(L, 1) + LUA_FIRSTINDEX; /* first empty element */ int pos; /* where to insert new element */ if (v == 2) /* called with only 2 arguments */ pos = e; /* insert new element at the end */ else { pos = luaL_checkint(L, 2); /* 2nd argument is the position */ if (pos > e) e = pos; /* `grow' array if necessary */ v = 3; /* function may be called with more than 3 args */ } luaL_setn(L, 1, e - LUA_FIRSTINDEX + 1); /* new size */ while (--e >= pos) { /* move up elements */ lua_rawgeti(L, 1, e); lua_rawseti(L, 1, e+1); /* t[e+1] = t[e] */ } lua_pushvalue(L, v); lua_rawseti(L, 1, pos); /* t[pos] = v */ return 0; } static int luaB_tremove (lua_State *L) { int e = aux_getn(L, 1) + LUA_FIRSTINDEX - 1; int pos = luaL_optint(L, 2, e); if (e < LUA_FIRSTINDEX) return 0; /* table is `empty' */ luaL_setn(L, 1, e - LUA_FIRSTINDEX); /* t.n = n-1 */ lua_rawgeti(L, 1, pos); /* result = t[pos] */ for ( ;pos= P */ while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { if (i>u) luaL_error(L, "invalid order function for sorting"); lua_pop(L, 1); /* remove a[i] */ } /* repeat --j until a[j] <= P */ while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { if (j