beta-0.89.2
[luatex.git] / source / libs / lua52 / lua52-src / src / ltablib.c
blob99764d26d100dac97420f27a4edeaecead89c36f
1 /*
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
5 */
8 #include <limits.h>
9 #include <stddef.h>
11 #define ltablib_c
12 #define LUA_LIB
14 #include "lua.h"
16 #include "lauxlib.h"
17 #include "lualib.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) {
26 lua_Number max = 0;
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);
33 if (v > max) max = v;
36 lua_pushnumber(L, max);
37 return 1;
39 #endif
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 */
48 break;
50 case 3: {
51 int i;
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] */
58 break;
60 default: {
61 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
64 lua_rawseti(L, 1, pos); /* t[pos] = v */
65 return 0;
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] */
79 lua_pushnil(L);
80 lua_rawseti(L, 1, pos); /* t[pos] = nil */
81 return 1;
85 static void addfield (lua_State *L, luaL_Buffer *b, int i) {
86 lua_rawgeti(L, 1, 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);
90 luaL_addvalue(b);
94 static int tconcat (lua_State *L) {
95 luaL_Buffer b;
96 size_t lsep;
97 int i, last;
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++) {
104 addfield(L, &b, i);
105 luaL_addlstring(&b, sep, lsep);
107 if (i == last) /* add last value (if interval was not empty) */
108 addfield(L, &b, i);
109 luaL_pushresult(&b);
110 return 1;
115 ** {======================================================
116 ** Pack/unpack
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? */
126 int i;
127 lua_pushvalue(L, 1);
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) {
138 int i, e;
139 unsigned int n;
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);
150 return n;
153 /* }====================================================== */
158 ** {======================================================
159 ** Quicksort
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? */
173 int res;
174 lua_pushvalue(L, 2);
175 lua_pushvalue(L, a-1); /* -1 to compensate function */
176 lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */
177 lua_call(L, 2, 1);
178 res = lua_toboolean(L, -1);
179 lua_pop(L, 1);
180 return res;
182 else /* a < b? */
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 */
188 int i, j;
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] */
194 else
195 lua_pop(L, 2);
196 if (u-l == 1) break; /* only 2 elements */
197 i = (l+u)/2;
198 lua_rawgeti(L, 1, i);
199 lua_rawgeti(L, 1, l);
200 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */
201 set2(L, i, l);
202 else {
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]? */
206 set2(L, i, u);
207 else
208 lua_pop(L, 2);
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);
214 set2(L, i, u-1);
215 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
216 i = l; j = u-1;
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] */
228 if (j<i) {
229 lua_pop(L, 3); /* pop pivot, a[i], a[j] */
230 break;
232 set2(L, i, 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] */
239 if (i-l < u-i) {
240 j=l; i=i-1; l=i+2;
242 else {
243 j=i+1; i=u; u=j-2;
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 */
255 auxsort(L, 1, n);
256 return 0;
259 /* }====================================================== */
262 static const luaL_Reg tab_funcs[] = {
263 {"concat", tconcat},
264 #if defined(LUA_COMPAT_MAXN)
265 {"maxn", maxn},
266 #endif
267 {"insert", tinsert},
268 {"pack", pack},
269 {"unpack", unpack},
270 {"remove", tremove},
271 {"sort", sort},
272 {NULL, NULL}
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");
282 #endif
283 return 1;