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
11 #include <sys/lua/lua.h>
13 #include <sys/lua/lauxlib.h>
14 #include <sys/lua/lualib.h>
17 #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
21 #if defined(LUA_COMPAT_MAXN)
22 static int maxn (lua_State
*L
) {
24 luaL_checktype(L
, 1, LUA_TTABLE
);
25 lua_pushnil(L
); /* first key */
26 while (lua_next(L
, 1)) {
27 lua_pop(L
, 1); /* remove value */
28 if (lua_type(L
, -1) == LUA_TNUMBER
) {
29 lua_Number v
= lua_tonumber(L
, -1);
33 lua_pushnumber(L
, max
);
39 static int tinsert (lua_State
*L
) {
40 int e
= aux_getn(L
, 1) + 1; /* first empty element */
41 int pos
; /* where to insert new element */
42 switch (lua_gettop(L
)) {
43 case 2: { /* called with only 2 arguments */
44 pos
= e
; /* insert new element at the end */
49 pos
= luaL_checkint(L
, 2); /* 2nd argument is the position */
50 luaL_argcheck(L
, 1 <= pos
&& pos
<= e
, 2, "position out of bounds");
51 for (i
= e
; i
> pos
; i
--) { /* move up elements */
52 lua_rawgeti(L
, 1, i
-1);
53 lua_rawseti(L
, 1, i
); /* t[i] = t[i-1] */
58 return luaL_error(L
, "wrong number of arguments to " LUA_QL("insert"));
61 lua_rawseti(L
, 1, pos
); /* t[pos] = v */
66 static int tremove (lua_State
*L
) {
67 int size
= aux_getn(L
, 1);
68 int pos
= luaL_optint(L
, 2, size
);
69 if (pos
!= size
) /* validate 'pos' if given */
70 luaL_argcheck(L
, 1 <= pos
&& pos
<= size
+ 1, 1, "position out of bounds");
71 lua_rawgeti(L
, 1, pos
); /* result = t[pos] */
72 for ( ; pos
< size
; pos
++) {
73 lua_rawgeti(L
, 1, pos
+1);
74 lua_rawseti(L
, 1, pos
); /* t[pos] = t[pos+1] */
77 lua_rawseti(L
, 1, pos
); /* t[pos] = nil */
82 static void addfield (lua_State
*L
, luaL_Buffer
*b
, int i
) {
84 if (!lua_isstring(L
, -1))
85 luaL_error(L
, "invalid value (%s) at index %d in table for "
86 LUA_QL("concat"), luaL_typename(L
, -1), i
);
91 static int tconcat (lua_State
*L
) {
95 const char *sep
= luaL_optlstring(L
, 2, "", &lsep
);
96 luaL_checktype(L
, 1, LUA_TTABLE
);
97 i
= luaL_optint(L
, 3, 1);
98 last
= luaL_opt(L
, luaL_checkint
, 4, luaL_len(L
, 1));
100 for (; i
< last
; i
++) {
102 luaL_addlstring(&b
, sep
, lsep
);
104 if (i
== last
) /* add last value (if interval was not empty) */
112 ** {======================================================
114 ** =======================================================
117 static int pack (lua_State
*L
) {
118 int n
= lua_gettop(L
); /* number of elements to pack */
119 lua_createtable(L
, n
, 1); /* create result table */
120 lua_pushinteger(L
, n
);
121 lua_setfield(L
, -2, "n"); /* t.n = number of elements */
122 if (n
> 0) { /* at least one element? */
125 lua_rawseti(L
, -2, 1); /* insert first element */
126 lua_replace(L
, 1); /* move table into index 1 */
127 for (i
= n
; i
>= 2; i
--) /* assign other elements */
128 lua_rawseti(L
, 1, i
);
130 return 1; /* return table */
134 static int unpack (lua_State
*L
) {
137 luaL_checktype(L
, 1, LUA_TTABLE
);
138 i
= luaL_optint(L
, 2, 1);
139 e
= luaL_opt(L
, luaL_checkint
, 3, luaL_len(L
, 1));
140 if (i
> e
) return 0; /* empty range */
141 n
= (unsigned int)e
- (unsigned int)i
; /* number of elements minus 1 */
142 if (n
> (INT_MAX
- 10) || !lua_checkstack(L
, ++n
))
143 return luaL_error(L
, "too many results to unpack");
144 lua_rawgeti(L
, 1, i
); /* push arg[i] (avoiding overflow problems) */
145 while (i
++ < e
) /* push arg[i + 1...e] */
146 lua_rawgeti(L
, 1, i
);
150 /* }====================================================== */
155 ** {======================================================
157 ** (based on `Algorithms in MODULA-3', Robert Sedgewick;
158 ** Addison-Wesley, 1993.)
159 ** =======================================================
163 static void set2 (lua_State
*L
, int i
, int j
) {
164 lua_rawseti(L
, 1, i
);
165 lua_rawseti(L
, 1, j
);
168 static int sort_comp (lua_State
*L
, int a
, int b
) {
169 if (!lua_isnil(L
, 2)) { /* function? */
172 lua_pushvalue(L
, a
-1); /* -1 to compensate function */
173 lua_pushvalue(L
, b
-2); /* -2 to compensate function and `a' */
175 res
= lua_toboolean(L
, -1);
180 return lua_compare(L
, a
, b
, LUA_OPLT
);
183 static void auxsort (lua_State
*L
, int l
, int u
) {
184 while (l
< u
) { /* for tail recursion */
186 /* sort elements a[l], a[(l+u)/2] and a[u] */
187 lua_rawgeti(L
, 1, l
);
188 lua_rawgeti(L
, 1, u
);
189 if (sort_comp(L
, -1, -2)) /* a[u] < a[l]? */
190 set2(L
, l
, u
); /* swap a[l] - a[u] */
193 if (u
-l
== 1) break; /* only 2 elements */
195 lua_rawgeti(L
, 1, i
);
196 lua_rawgeti(L
, 1, l
);
197 if (sort_comp(L
, -2, -1)) /* a[i]<a[l]? */
200 lua_pop(L
, 1); /* remove a[l] */
201 lua_rawgeti(L
, 1, u
);
202 if (sort_comp(L
, -1, -2)) /* a[u]<a[i]? */
207 if (u
-l
== 2) break; /* only 3 elements */
208 lua_rawgeti(L
, 1, i
); /* Pivot */
209 lua_pushvalue(L
, -1);
210 lua_rawgeti(L
, 1, u
-1);
212 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
214 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */
215 /* repeat ++i until a[i] >= P */
216 while (lua_rawgeti(L
, 1, ++i
), sort_comp(L
, -1, -2)) {
217 if (i
>=u
) luaL_error(L
, "invalid order function for sorting");
218 lua_pop(L
, 1); /* remove a[i] */
220 /* repeat --j until a[j] <= P */
221 while (lua_rawgeti(L
, 1, --j
), sort_comp(L
, -3, -1)) {
222 if (j
<=l
) luaL_error(L
, "invalid order function for sorting");
223 lua_pop(L
, 1); /* remove a[j] */
226 lua_pop(L
, 3); /* pop pivot, a[i], a[j] */
231 lua_rawgeti(L
, 1, u
-1);
232 lua_rawgeti(L
, 1, i
);
233 set2(L
, u
-1, i
); /* swap pivot (a[u-1]) with a[i] */
234 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
235 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
242 auxsort(L
, j
, i
); /* call recursively the smaller one */
243 } /* repeat the routine for the larger one */
246 static int tsort (lua_State
*L
) {
247 int n
= aux_getn(L
, 1);
248 luaL_checkstack(L
, 40, ""); /* assume array is smaller than 2^40 */
249 if (!lua_isnoneornil(L
, 2)) /* is there a 2nd argument? */
250 luaL_checktype(L
, 2, LUA_TFUNCTION
);
251 lua_settop(L
, 2); /* make sure there is two arguments */
256 /* }====================================================== */
259 static const luaL_Reg tab_funcs
[] = {
261 #if defined(LUA_COMPAT_MAXN)
273 LUAMOD_API
int luaopen_table (lua_State
*L
) {
274 luaL_newlib(L
, tab_funcs
);
275 #if defined(LUA_COMPAT_UNPACK)
276 /* _G.unpack = table.unpack */
277 lua_getfield(L
, -1, "unpack");
278 lua_setglobal(L
, "unpack");
285 EXPORT_SYMBOL(luaopen_table
);