summaryrefslogtreecommitdiffstats
path: root/lua/ltablib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lua/ltablib.c')
-rw-r--r--lua/ltablib.c130
1 files changed, 63 insertions, 67 deletions
diff --git a/lua/ltablib.c b/lua/ltablib.c
index b6d9cb4..a52add0 100644
--- a/lua/ltablib.c
+++ b/lua/ltablib.c
@@ -1,5 +1,5 @@
/*
-** $Id: ltablib.c,v 1.38.1.3 2008/02/14 16:46:58 roberto Exp $
+** $Id: ltablib.c,v 1.63 2011/11/28 17:26:30 roberto Exp $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@@ -16,43 +16,11 @@
#include "lualib.h"
-#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n))
-
-
-static int foreachi (lua_State *L) {
- int i;
- int n = aux_getn(L, 1);
- luaL_checktype(L, 2, LUA_TFUNCTION);
- for (i=1; i <= n; 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 foreach (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- luaL_checktype(L, 2, LUA_TFUNCTION);
- lua_pushnil(L); /* first key */
- while (lua_next(L, 1)) {
- 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 */
- }
- return 0;
-}
+#define aux_getn(L,n) \
+ (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
+#if defined(LUA_COMPAT_MAXN)
static int maxn (lua_State *L) {
lua_Number max = 0;
luaL_checktype(L, 1, LUA_TTABLE);
@@ -67,24 +35,7 @@ static int maxn (lua_State *L) {
lua_pushnumber(L, max);
return 1;
}
-
-
-static int getn (lua_State *L) {
- lua_pushinteger(L, aux_getn(L, 1));
- return 1;
-}
-
-
-static int setn (lua_State *L) {
- luaL_checktype(L, 1, LUA_TTABLE);
-#ifndef luaL_setn
- luaL_setn(L, 1, luaL_checkint(L, 2));
-#else
- luaL_error(L, LUA_QL("setn") " is obsolete");
#endif
- lua_pushvalue(L, 1);
- return 1;
-}
static int tinsert (lua_State *L) {
@@ -109,7 +60,6 @@ static int tinsert (lua_State *L) {
return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
}
}
- luaL_setn(L, 1, e); /* new size */
lua_rawseti(L, 1, pos); /* t[pos] = v */
return 0;
}
@@ -119,8 +69,7 @@ static int tremove (lua_State *L) {
int e = aux_getn(L, 1);
int pos = luaL_optint(L, 2, e);
if (!(1 <= pos && pos <= e)) /* position is outside bounds? */
- return 0; /* nothing to remove */
- luaL_setn(L, 1, e - 1); /* t.n = n-1 */
+ return 0; /* nothing to remove */
lua_rawgeti(L, 1, pos); /* result = t[pos] */
for ( ;pos<e; pos++) {
lua_rawgeti(L, 1, pos+1);
@@ -137,7 +86,7 @@ static void addfield (lua_State *L, luaL_Buffer *b, int i) {
if (!lua_isstring(L, -1))
luaL_error(L, "invalid value (%s) at index %d in table for "
LUA_QL("concat"), luaL_typename(L, -1), i);
- luaL_addvalue(b);
+ luaL_addvalue(b);
}
@@ -148,7 +97,7 @@ static int tconcat (lua_State *L) {
const char *sep = luaL_optlstring(L, 2, "", &lsep);
luaL_checktype(L, 1, LUA_TTABLE);
i = luaL_optint(L, 3, 1);
- last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
+ last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));
luaL_buffinit(L, &b);
for (; i < last; i++) {
addfield(L, &b, i);
@@ -161,12 +110,54 @@ static int tconcat (lua_State *L) {
}
+/*
+** {======================================================
+** Pack/unpack
+** =======================================================
+*/
+
+static int pack (lua_State *L) {
+ int n = lua_gettop(L); /* number of elements to pack */
+ lua_createtable(L, n, 1); /* create result table */
+ lua_pushinteger(L, n);
+ lua_setfield(L, -2, "n"); /* t.n = number of elements */
+ if (n > 0) { /* at least one element? */
+ int i;
+ lua_pushvalue(L, 1);
+ lua_rawseti(L, -2, 1); /* insert first element */
+ lua_replace(L, 1); /* move table into index 1 */
+ for (i = n; i >= 2; i--) /* assign other elements */
+ lua_rawseti(L, 1, i);
+ }
+ return 1; /* return table */
+}
+
+
+static int unpack (lua_State *L) {
+ int i, e, n;
+ luaL_checktype(L, 1, LUA_TTABLE);
+ i = luaL_optint(L, 2, 1);
+ e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
+ if (i > e) return 0; /* empty range */
+ n = e - i + 1; /* number of elements */
+ if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
+ return luaL_error(L, "too many results to unpack");
+ lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
+ while (i++ < e) /* push arg[i + 1...e] */
+ lua_rawgeti(L, 1, i);
+ return n;
+}
+
+/* }====================================================== */
+
+
/*
** {======================================================
** Quicksort
** (based on `Algorithms in MODULA-3', Robert Sedgewick;
** Addison-Wesley, 1993.)
+** =======================================================
*/
@@ -187,7 +178,7 @@ static int sort_comp (lua_State *L, int a, int b) {
return res;
}
else /* a < b? */
- return lua_lessthan(L, a, b);
+ return lua_compare(L, a, b, LUA_OPLT);
}
static void auxsort (lua_State *L, int l, int u) {
@@ -224,12 +215,12 @@ static void auxsort (lua_State *L, int l, int u) {
for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
/* repeat ++i until a[i] >= P */
while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
- if (i>u) luaL_error(L, "invalid order function for sorting");
+ 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<l) luaL_error(L, "invalid order function for sorting");
+ if (j<=l) luaL_error(L, "invalid order function for sorting");
lua_pop(L, 1); /* remove a[j] */
}
if (j<i) {
@@ -268,20 +259,25 @@ static int sort (lua_State *L) {
static const luaL_Reg tab_funcs[] = {
{"concat", tconcat},
- {"foreach", foreach},
- {"foreachi", foreachi},
- {"getn", getn},
+#if defined(LUA_COMPAT_MAXN)
{"maxn", maxn},
+#endif
{"insert", tinsert},
+ {"pack", pack},
+ {"unpack", unpack},
{"remove", tremove},
- {"setn", setn},
{"sort", sort},
{NULL, NULL}
};
-LUALIB_API int luaopen_table (lua_State *L) {
- luaL_register(L, LUA_TABLIBNAME, tab_funcs);
+LUAMOD_API int luaopen_table (lua_State *L) {
+ luaL_newlib(L, tab_funcs);
+#if defined(LUA_COMPAT_UNPACK)
+ /* _G.unpack = table.unpack */
+ lua_getfield(L, -1, "unpack");
+ lua_setglobal(L, "unpack");
+#endif
return 1;
}