2 ** $Id: ltablib.c,v 1.65.1.2 2014/05/07 16:32:55 roberto Exp $
3 ** Library for Table Manipulation
4 ** See Copyright Notice in lua.h
20 #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
24 #if defined(LUA_COMPAT_MAXN)
25 static int maxn (lua_State
*L
) {
27 luaL_checktype(L
, 1, LUA_TTABLE
);
28 lua_pushnil(L
); /* first key */
29 while (lua_next(L
, 1)) {
30 lua_pop(L
, 1); /* remove value */
31 if (lua_type(L
, -1) == LUA_TNUMBER
) {
32 lua_Number v
= lua_tonumber(L
, -1);
36 lua_pushnumber(L
, max
);
42 static int tinsert (lua_State
*L
) {
43 int e
= aux_getn(L
, 1) + 1; /* first empty element */
44 int pos
; /* where to insert new element */
45 switch (lua_gettop(L
)) {
46 case 2: { /* called with only 2 arguments */
47 pos
= e
; /* insert new element at the end */
52 pos
= luaL_checkint(L
, 2); /* 2nd argument is the position */
53 luaL_argcheck(L
, 1 <= pos
&& pos
<= e
, 2, "position out of bounds");
54 for (i
= e
; i
> pos
; i
--) { /* move up elements */
55 lua_rawgeti(L
, 1, i
-1);
56 lua_rawseti(L
, 1, i
); /* t[i] = t[i-1] */
61 return luaL_error(L
, "wrong number of arguments to " LUA_QL("insert"));
64 lua_rawseti(L
, 1, pos
); /* t[pos] = v */
69 static int tremove (lua_State
*L
) {
70 int size
= aux_getn(L
, 1);
71 int pos
= luaL_optint(L
, 2, size
);
72 if (pos
!= size
) /* validate 'pos' if given */
73 luaL_argcheck(L
, 1 <= pos
&& pos
<= size
+ 1, 1, "position out of bounds");
74 lua_rawgeti(L
, 1, pos
); /* result = t[pos] */
75 for ( ; pos
< size
; pos
++) {
76 lua_rawgeti(L
, 1, pos
+1);
77 lua_rawseti(L
, 1, pos
); /* t[pos] = t[pos+1] */
80 lua_rawseti(L
, 1, pos
); /* t[pos] = nil */
85 static void addfield (lua_State
*L
, luaL_Buffer
*b
, int i
) {
87 if (!lua_isstring(L
, -1))
88 luaL_error(L
, "invalid value (%s) at index %d in table for "
89 LUA_QL("concat"), luaL_typename(L
, -1), i
);
94 static int tconcat (lua_State
*L
) {
98 const char *sep
= luaL_optlstring(L
, 2, "", &lsep
);
99 luaL_checktype(L
, 1, LUA_TTABLE
);
100 i
= luaL_optint(L
, 3, 1);
101 last
= luaL_opt(L
, luaL_checkint
, 4, luaL_len(L
, 1));
102 luaL_buffinit(L
, &b
);
103 for (; i
< last
; i
++) {
105 luaL_addlstring(&b
, sep
, lsep
);
107 if (i
== last
) /* add last value (if interval was not empty) */
115 ** {======================================================
117 ** =======================================================
120 static int pack (lua_State
*L
) {
121 int n
= lua_gettop(L
); /* number of elements to pack */
122 lua_createtable(L
, n
, 1); /* create result table */
123 lua_pushinteger(L
, n
);
124 lua_setfield(L
, -2, "n"); /* t.n = number of elements */
125 if (n
> 0) { /* at least one element? */
128 lua_rawseti(L
, -2, 1); /* insert first element */
129 lua_replace(L
, 1); /* move table into index 1 */
130 for (i
= n
; i
>= 2; i
--) /* assign other elements */
131 lua_rawseti(L
, 1, i
);
133 return 1; /* return table */
137 static int unpack (lua_State
*L
) {
140 luaL_checktype(L
, 1, LUA_TTABLE
);
141 i
= luaL_optint(L
, 2, 1);
142 e
= luaL_opt(L
, luaL_checkint
, 3, luaL_len(L
, 1));
143 if (i
> e
) return 0; /* empty range */
144 n
= (unsigned int)e
- (unsigned int)i
; /* number of elements minus 1 */
145 if (n
> (INT_MAX
- 10) || !lua_checkstack(L
, ++n
))
146 return luaL_error(L
, "too many results to unpack");
147 lua_rawgeti(L
, 1, i
); /* push arg[i] (avoiding overflow problems) */
148 while (i
++ < e
) /* push arg[i + 1...e] */
149 lua_rawgeti(L
, 1, i
);
153 /* }====================================================== */
158 ** {======================================================
160 ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
161 ** Addison-Wesley, 1993.)
162 ** =======================================================
166 static void set2 (lua_State
*L
, int i
, int j
) {
167 lua_rawseti(L
, 1, i
);
168 lua_rawseti(L
, 1, j
);
171 static int sort_comp (lua_State
*L
, int a
, int b
) {
172 if (!lua_isnil(L
, 2)) { /* function? */
175 lua_pushvalue(L
, a
-1); /* -1 to compensate function */
176 lua_pushvalue(L
, b
-2); /* -2 to compensate function and `a' */
178 res
= lua_toboolean(L
, -1);
183 return lua_compare(L
, a
, b
, LUA_OPLT
);
186 static void auxsort (lua_State
*L
, int l
, int u
) {
187 while (l
< u
) { /* for tail recursion */
189 /* sort elements a[l], a[(l+u)/2] and a[u] */
190 lua_rawgeti(L
, 1, l
);
191 lua_rawgeti(L
, 1, u
);
192 if (sort_comp(L
, -1, -2)) /* a[u] < a[l]? */
193 set2(L
, l
, u
); /* swap a[l] - a[u] */
196 if (u
-l
== 1) break; /* only 2 elements */
198 lua_rawgeti(L
, 1, i
);
199 lua_rawgeti(L
, 1, l
);
200 if (sort_comp(L
, -2, -1)) /* a[i]<a[l]? */
203 lua_pop(L
, 1); /* remove a[l] */
204 lua_rawgeti(L
, 1, u
);
205 if (sort_comp(L
, -1, -2)) /* a[u]<a[i]? */
210 if (u
-l
== 2) break; /* only 3 elements */
211 lua_rawgeti(L
, 1, i
); /* Pivot */
212 lua_pushvalue(L
, -1);
213 lua_rawgeti(L
, 1, u
-1);
215 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
217 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
218 /* repeat ++i until a[i] >= P */
219 while (lua_rawgeti(L
, 1, ++i
), sort_comp(L
, -1, -2)) {
220 if (i
>=u
) luaL_error(L
, "invalid order function for sorting");
221 lua_pop(L
, 1); /* remove a[i] */
223 /* repeat --j until a[j] <= P */
224 while (lua_rawgeti(L
, 1, --j
), sort_comp(L
, -3, -1)) {
225 if (j
<=l
) luaL_error(L
, "invalid order function for sorting");
226 lua_pop(L
, 1); /* remove a[j] */
229 lua_pop(L
, 3); /* pop pivot, a[i], a[j] */
234 lua_rawgeti(L
, 1, u
-1);
235 lua_rawgeti(L
, 1, i
);
236 set2(L
, u
-1, i
); /* swap pivot (a[u-1]) with a[i] */
237 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
238 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
245 auxsort(L
, j
, i
); /* call recursively the smaller one */
246 } /* repeat the routine for the larger one */
249 static int sort (lua_State
*L
) {
250 int n
= aux_getn(L
, 1);
251 luaL_checkstack(L
, 40, ""); /* assume array is smaller than 2^40 */
252 if (!lua_isnoneornil(L
, 2)) /* is there a 2nd argument? */
253 luaL_checktype(L
, 2, LUA_TFUNCTION
);
254 lua_settop(L
, 2); /* make sure there is two arguments */
259 /* }====================================================== */
262 static const luaL_Reg tab_funcs
[] = {
264 #if defined(LUA_COMPAT_MAXN)
276 LUAMOD_API
int luaopen_table (lua_State
*L
) {
277 luaL_newlib(L
, tab_funcs
);
278 #if defined(LUA_COMPAT_UNPACK)
279 /* _G.unpack = table.unpack */
280 lua_getfield(L
, -1, "unpack");
281 lua_setglobal(L
, "unpack");