1*088332b5SXin Li /*
2*088332b5SXin Li ** $Id: ltablib.c $
3*088332b5SXin Li ** Library for Table Manipulation
4*088332b5SXin Li ** See Copyright Notice in lua.h
5*088332b5SXin Li */
6*088332b5SXin Li
7*088332b5SXin Li #define ltablib_c
8*088332b5SXin Li #define LUA_LIB
9*088332b5SXin Li
10*088332b5SXin Li #include "lprefix.h"
11*088332b5SXin Li
12*088332b5SXin Li
13*088332b5SXin Li #include <limits.h>
14*088332b5SXin Li #include <stddef.h>
15*088332b5SXin Li #include <string.h>
16*088332b5SXin Li
17*088332b5SXin Li #include "lua.h"
18*088332b5SXin Li
19*088332b5SXin Li #include "lauxlib.h"
20*088332b5SXin Li #include "lualib.h"
21*088332b5SXin Li
22*088332b5SXin Li
23*088332b5SXin Li /*
24*088332b5SXin Li ** Operations that an object must define to mimic a table
25*088332b5SXin Li ** (some functions only need some of them)
26*088332b5SXin Li */
27*088332b5SXin Li #define TAB_R 1 /* read */
28*088332b5SXin Li #define TAB_W 2 /* write */
29*088332b5SXin Li #define TAB_L 4 /* length */
30*088332b5SXin Li #define TAB_RW (TAB_R | TAB_W) /* read/write */
31*088332b5SXin Li
32*088332b5SXin Li
33*088332b5SXin Li #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
34*088332b5SXin Li
35*088332b5SXin Li
checkfield(lua_State * L,const char * key,int n)36*088332b5SXin Li static int checkfield (lua_State *L, const char *key, int n) {
37*088332b5SXin Li lua_pushstring(L, key);
38*088332b5SXin Li return (lua_rawget(L, -n) != LUA_TNIL);
39*088332b5SXin Li }
40*088332b5SXin Li
41*088332b5SXin Li
42*088332b5SXin Li /*
43*088332b5SXin Li ** Check that 'arg' either is a table or can behave like one (that is,
44*088332b5SXin Li ** has a metatable with the required metamethods)
45*088332b5SXin Li */
checktab(lua_State * L,int arg,int what)46*088332b5SXin Li static void checktab (lua_State *L, int arg, int what) {
47*088332b5SXin Li if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
48*088332b5SXin Li int n = 1; /* number of elements to pop */
49*088332b5SXin Li if (lua_getmetatable(L, arg) && /* must have metatable */
50*088332b5SXin Li (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
51*088332b5SXin Li (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
52*088332b5SXin Li (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
53*088332b5SXin Li lua_pop(L, n); /* pop metatable and tested metamethods */
54*088332b5SXin Li }
55*088332b5SXin Li else
56*088332b5SXin Li luaL_checktype(L, arg, LUA_TTABLE); /* force an error */
57*088332b5SXin Li }
58*088332b5SXin Li }
59*088332b5SXin Li
60*088332b5SXin Li
tinsert(lua_State * L)61*088332b5SXin Li static int tinsert (lua_State *L) {
62*088332b5SXin Li lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
63*088332b5SXin Li lua_Integer pos; /* where to insert new element */
64*088332b5SXin Li switch (lua_gettop(L)) {
65*088332b5SXin Li case 2: { /* called with only 2 arguments */
66*088332b5SXin Li pos = e; /* insert new element at the end */
67*088332b5SXin Li break;
68*088332b5SXin Li }
69*088332b5SXin Li case 3: {
70*088332b5SXin Li lua_Integer i;
71*088332b5SXin Li pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
72*088332b5SXin Li /* check whether 'pos' is in [1, e] */
73*088332b5SXin Li luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
74*088332b5SXin Li "position out of bounds");
75*088332b5SXin Li for (i = e; i > pos; i--) { /* move up elements */
76*088332b5SXin Li lua_geti(L, 1, i - 1);
77*088332b5SXin Li lua_seti(L, 1, i); /* t[i] = t[i - 1] */
78*088332b5SXin Li }
79*088332b5SXin Li break;
80*088332b5SXin Li }
81*088332b5SXin Li default: {
82*088332b5SXin Li return luaL_error(L, "wrong number of arguments to 'insert'");
83*088332b5SXin Li }
84*088332b5SXin Li }
85*088332b5SXin Li lua_seti(L, 1, pos); /* t[pos] = v */
86*088332b5SXin Li return 0;
87*088332b5SXin Li }
88*088332b5SXin Li
89*088332b5SXin Li
tremove(lua_State * L)90*088332b5SXin Li static int tremove (lua_State *L) {
91*088332b5SXin Li lua_Integer size = aux_getn(L, 1, TAB_RW);
92*088332b5SXin Li lua_Integer pos = luaL_optinteger(L, 2, size);
93*088332b5SXin Li if (pos != size) /* validate 'pos' if given */
94*088332b5SXin Li /* check whether 'pos' is in [1, size + 1] */
95*088332b5SXin Li luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 1,
96*088332b5SXin Li "position out of bounds");
97*088332b5SXin Li lua_geti(L, 1, pos); /* result = t[pos] */
98*088332b5SXin Li for ( ; pos < size; pos++) {
99*088332b5SXin Li lua_geti(L, 1, pos + 1);
100*088332b5SXin Li lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
101*088332b5SXin Li }
102*088332b5SXin Li lua_pushnil(L);
103*088332b5SXin Li lua_seti(L, 1, pos); /* remove entry t[pos] */
104*088332b5SXin Li return 1;
105*088332b5SXin Li }
106*088332b5SXin Li
107*088332b5SXin Li
108*088332b5SXin Li /*
109*088332b5SXin Li ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
110*088332b5SXin Li ** possible, copy in increasing order, which is better for rehashing.
111*088332b5SXin Li ** "possible" means destination after original range, or smaller
112*088332b5SXin Li ** than origin, or copying to another table.
113*088332b5SXin Li */
tmove(lua_State * L)114*088332b5SXin Li static int tmove (lua_State *L) {
115*088332b5SXin Li lua_Integer f = luaL_checkinteger(L, 2);
116*088332b5SXin Li lua_Integer e = luaL_checkinteger(L, 3);
117*088332b5SXin Li lua_Integer t = luaL_checkinteger(L, 4);
118*088332b5SXin Li int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
119*088332b5SXin Li checktab(L, 1, TAB_R);
120*088332b5SXin Li checktab(L, tt, TAB_W);
121*088332b5SXin Li if (e >= f) { /* otherwise, nothing to move */
122*088332b5SXin Li lua_Integer n, i;
123*088332b5SXin Li luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
124*088332b5SXin Li "too many elements to move");
125*088332b5SXin Li n = e - f + 1; /* number of elements to move */
126*088332b5SXin Li luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
127*088332b5SXin Li "destination wrap around");
128*088332b5SXin Li if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
129*088332b5SXin Li for (i = 0; i < n; i++) {
130*088332b5SXin Li lua_geti(L, 1, f + i);
131*088332b5SXin Li lua_seti(L, tt, t + i);
132*088332b5SXin Li }
133*088332b5SXin Li }
134*088332b5SXin Li else {
135*088332b5SXin Li for (i = n - 1; i >= 0; i--) {
136*088332b5SXin Li lua_geti(L, 1, f + i);
137*088332b5SXin Li lua_seti(L, tt, t + i);
138*088332b5SXin Li }
139*088332b5SXin Li }
140*088332b5SXin Li }
141*088332b5SXin Li lua_pushvalue(L, tt); /* return destination table */
142*088332b5SXin Li return 1;
143*088332b5SXin Li }
144*088332b5SXin Li
145*088332b5SXin Li
addfield(lua_State * L,luaL_Buffer * b,lua_Integer i)146*088332b5SXin Li static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
147*088332b5SXin Li lua_geti(L, 1, i);
148*088332b5SXin Li if (!lua_isstring(L, -1))
149*088332b5SXin Li luaL_error(L, "invalid value (%s) at index %d in table for 'concat'",
150*088332b5SXin Li luaL_typename(L, -1), i);
151*088332b5SXin Li luaL_addvalue(b);
152*088332b5SXin Li }
153*088332b5SXin Li
154*088332b5SXin Li
tconcat(lua_State * L)155*088332b5SXin Li static int tconcat (lua_State *L) {
156*088332b5SXin Li luaL_Buffer b;
157*088332b5SXin Li lua_Integer last = aux_getn(L, 1, TAB_R);
158*088332b5SXin Li size_t lsep;
159*088332b5SXin Li const char *sep = luaL_optlstring(L, 2, "", &lsep);
160*088332b5SXin Li lua_Integer i = luaL_optinteger(L, 3, 1);
161*088332b5SXin Li last = luaL_optinteger(L, 4, last);
162*088332b5SXin Li luaL_buffinit(L, &b);
163*088332b5SXin Li for (; i < last; i++) {
164*088332b5SXin Li addfield(L, &b, i);
165*088332b5SXin Li luaL_addlstring(&b, sep, lsep);
166*088332b5SXin Li }
167*088332b5SXin Li if (i == last) /* add last value (if interval was not empty) */
168*088332b5SXin Li addfield(L, &b, i);
169*088332b5SXin Li luaL_pushresult(&b);
170*088332b5SXin Li return 1;
171*088332b5SXin Li }
172*088332b5SXin Li
173*088332b5SXin Li
174*088332b5SXin Li /*
175*088332b5SXin Li ** {======================================================
176*088332b5SXin Li ** Pack/unpack
177*088332b5SXin Li ** =======================================================
178*088332b5SXin Li */
179*088332b5SXin Li
tpack(lua_State * L)180*088332b5SXin Li static int tpack (lua_State *L) {
181*088332b5SXin Li int i;
182*088332b5SXin Li int n = lua_gettop(L); /* number of elements to pack */
183*088332b5SXin Li lua_createtable(L, n, 1); /* create result table */
184*088332b5SXin Li lua_insert(L, 1); /* put it at index 1 */
185*088332b5SXin Li for (i = n; i >= 1; i--) /* assign elements */
186*088332b5SXin Li lua_seti(L, 1, i);
187*088332b5SXin Li lua_pushinteger(L, n);
188*088332b5SXin Li lua_setfield(L, 1, "n"); /* t.n = number of elements */
189*088332b5SXin Li return 1; /* return table */
190*088332b5SXin Li }
191*088332b5SXin Li
192*088332b5SXin Li
tunpack(lua_State * L)193*088332b5SXin Li static int tunpack (lua_State *L) {
194*088332b5SXin Li lua_Unsigned n;
195*088332b5SXin Li lua_Integer i = luaL_optinteger(L, 2, 1);
196*088332b5SXin Li lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
197*088332b5SXin Li if (i > e) return 0; /* empty range */
198*088332b5SXin Li n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
199*088332b5SXin Li if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
200*088332b5SXin Li return luaL_error(L, "too many results to unpack");
201*088332b5SXin Li for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
202*088332b5SXin Li lua_geti(L, 1, i);
203*088332b5SXin Li }
204*088332b5SXin Li lua_geti(L, 1, e); /* push last element */
205*088332b5SXin Li return (int)n;
206*088332b5SXin Li }
207*088332b5SXin Li
208*088332b5SXin Li /* }====================================================== */
209*088332b5SXin Li
210*088332b5SXin Li
211*088332b5SXin Li
212*088332b5SXin Li /*
213*088332b5SXin Li ** {======================================================
214*088332b5SXin Li ** Quicksort
215*088332b5SXin Li ** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
216*088332b5SXin Li ** Addison-Wesley, 1993.)
217*088332b5SXin Li ** =======================================================
218*088332b5SXin Li */
219*088332b5SXin Li
220*088332b5SXin Li
221*088332b5SXin Li /* type for array indices */
222*088332b5SXin Li typedef unsigned int IdxT;
223*088332b5SXin Li
224*088332b5SXin Li
225*088332b5SXin Li /*
226*088332b5SXin Li ** Produce a "random" 'unsigned int' to randomize pivot choice. This
227*088332b5SXin Li ** macro is used only when 'sort' detects a big imbalance in the result
228*088332b5SXin Li ** of a partition. (If you don't want/need this "randomness", ~0 is a
229*088332b5SXin Li ** good choice.)
230*088332b5SXin Li */
231*088332b5SXin Li #if !defined(l_randomizePivot) /* { */
232*088332b5SXin Li
233*088332b5SXin Li #include <time.h>
234*088332b5SXin Li
235*088332b5SXin Li /* size of 'e' measured in number of 'unsigned int's */
236*088332b5SXin Li #define sof(e) (sizeof(e) / sizeof(unsigned int))
237*088332b5SXin Li
238*088332b5SXin Li /*
239*088332b5SXin Li ** Use 'time' and 'clock' as sources of "randomness". Because we don't
240*088332b5SXin Li ** know the types 'clock_t' and 'time_t', we cannot cast them to
241*088332b5SXin Li ** anything without risking overflows. A safe way to use their values
242*088332b5SXin Li ** is to copy them to an array of a known type and use the array values.
243*088332b5SXin Li */
l_randomizePivot(void)244*088332b5SXin Li static unsigned int l_randomizePivot (void) {
245*088332b5SXin Li clock_t c = clock();
246*088332b5SXin Li time_t t = time(NULL);
247*088332b5SXin Li unsigned int buff[sof(c) + sof(t)];
248*088332b5SXin Li unsigned int i, rnd = 0;
249*088332b5SXin Li memcpy(buff, &c, sof(c) * sizeof(unsigned int));
250*088332b5SXin Li memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
251*088332b5SXin Li for (i = 0; i < sof(buff); i++)
252*088332b5SXin Li rnd += buff[i];
253*088332b5SXin Li return rnd;
254*088332b5SXin Li }
255*088332b5SXin Li
256*088332b5SXin Li #endif /* } */
257*088332b5SXin Li
258*088332b5SXin Li
259*088332b5SXin Li /* arrays larger than 'RANLIMIT' may use randomized pivots */
260*088332b5SXin Li #define RANLIMIT 100u
261*088332b5SXin Li
262*088332b5SXin Li
set2(lua_State * L,IdxT i,IdxT j)263*088332b5SXin Li static void set2 (lua_State *L, IdxT i, IdxT j) {
264*088332b5SXin Li lua_seti(L, 1, i);
265*088332b5SXin Li lua_seti(L, 1, j);
266*088332b5SXin Li }
267*088332b5SXin Li
268*088332b5SXin Li
269*088332b5SXin Li /*
270*088332b5SXin Li ** Return true iff value at stack index 'a' is less than the value at
271*088332b5SXin Li ** index 'b' (according to the order of the sort).
272*088332b5SXin Li */
sort_comp(lua_State * L,int a,int b)273*088332b5SXin Li static int sort_comp (lua_State *L, int a, int b) {
274*088332b5SXin Li if (lua_isnil(L, 2)) /* no function? */
275*088332b5SXin Li return lua_compare(L, a, b, LUA_OPLT); /* a < b */
276*088332b5SXin Li else { /* function */
277*088332b5SXin Li int res;
278*088332b5SXin Li lua_pushvalue(L, 2); /* push function */
279*088332b5SXin Li lua_pushvalue(L, a-1); /* -1 to compensate function */
280*088332b5SXin Li lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
281*088332b5SXin Li lua_call(L, 2, 1); /* call function */
282*088332b5SXin Li res = lua_toboolean(L, -1); /* get result */
283*088332b5SXin Li lua_pop(L, 1); /* pop result */
284*088332b5SXin Li return res;
285*088332b5SXin Li }
286*088332b5SXin Li }
287*088332b5SXin Li
288*088332b5SXin Li
289*088332b5SXin Li /*
290*088332b5SXin Li ** Does the partition: Pivot P is at the top of the stack.
291*088332b5SXin Li ** precondition: a[lo] <= P == a[up-1] <= a[up],
292*088332b5SXin Li ** so it only needs to do the partition from lo + 1 to up - 2.
293*088332b5SXin Li ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
294*088332b5SXin Li ** returns 'i'.
295*088332b5SXin Li */
partition(lua_State * L,IdxT lo,IdxT up)296*088332b5SXin Li static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
297*088332b5SXin Li IdxT i = lo; /* will be incremented before first use */
298*088332b5SXin Li IdxT j = up - 1; /* will be decremented before first use */
299*088332b5SXin Li /* loop invariant: a[lo .. i] <= P <= a[j .. up] */
300*088332b5SXin Li for (;;) {
301*088332b5SXin Li /* next loop: repeat ++i while a[i] < P */
302*088332b5SXin Li while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
303*088332b5SXin Li if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
304*088332b5SXin Li luaL_error(L, "invalid order function for sorting");
305*088332b5SXin Li lua_pop(L, 1); /* remove a[i] */
306*088332b5SXin Li }
307*088332b5SXin Li /* after the loop, a[i] >= P and a[lo .. i - 1] < P */
308*088332b5SXin Li /* next loop: repeat --j while P < a[j] */
309*088332b5SXin Li while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
310*088332b5SXin Li if (j < i) /* j < i but a[j] > P ?? */
311*088332b5SXin Li luaL_error(L, "invalid order function for sorting");
312*088332b5SXin Li lua_pop(L, 1); /* remove a[j] */
313*088332b5SXin Li }
314*088332b5SXin Li /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */
315*088332b5SXin Li if (j < i) { /* no elements out of place? */
316*088332b5SXin Li /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */
317*088332b5SXin Li lua_pop(L, 1); /* pop a[j] */
318*088332b5SXin Li /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */
319*088332b5SXin Li set2(L, up - 1, i);
320*088332b5SXin Li return i;
321*088332b5SXin Li }
322*088332b5SXin Li /* otherwise, swap a[i] - a[j] to restore invariant and repeat */
323*088332b5SXin Li set2(L, i, j);
324*088332b5SXin Li }
325*088332b5SXin Li }
326*088332b5SXin Li
327*088332b5SXin Li
328*088332b5SXin Li /*
329*088332b5SXin Li ** Choose an element in the middle (2nd-3th quarters) of [lo,up]
330*088332b5SXin Li ** "randomized" by 'rnd'
331*088332b5SXin Li */
choosePivot(IdxT lo,IdxT up,unsigned int rnd)332*088332b5SXin Li static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
333*088332b5SXin Li IdxT r4 = (up - lo) / 4; /* range/4 */
334*088332b5SXin Li IdxT p = rnd % (r4 * 2) + (lo + r4);
335*088332b5SXin Li lua_assert(lo + r4 <= p && p <= up - r4);
336*088332b5SXin Li return p;
337*088332b5SXin Li }
338*088332b5SXin Li
339*088332b5SXin Li
340*088332b5SXin Li /*
341*088332b5SXin Li ** Quicksort algorithm (recursive function)
342*088332b5SXin Li */
auxsort(lua_State * L,IdxT lo,IdxT up,unsigned int rnd)343*088332b5SXin Li static void auxsort (lua_State *L, IdxT lo, IdxT up,
344*088332b5SXin Li unsigned int rnd) {
345*088332b5SXin Li while (lo < up) { /* loop for tail recursion */
346*088332b5SXin Li IdxT p; /* Pivot index */
347*088332b5SXin Li IdxT n; /* to be used later */
348*088332b5SXin Li /* sort elements 'lo', 'p', and 'up' */
349*088332b5SXin Li lua_geti(L, 1, lo);
350*088332b5SXin Li lua_geti(L, 1, up);
351*088332b5SXin Li if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
352*088332b5SXin Li set2(L, lo, up); /* swap a[lo] - a[up] */
353*088332b5SXin Li else
354*088332b5SXin Li lua_pop(L, 2); /* remove both values */
355*088332b5SXin Li if (up - lo == 1) /* only 2 elements? */
356*088332b5SXin Li return; /* already sorted */
357*088332b5SXin Li if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
358*088332b5SXin Li p = (lo + up)/2; /* middle element is a good pivot */
359*088332b5SXin Li else /* for larger intervals, it is worth a random pivot */
360*088332b5SXin Li p = choosePivot(lo, up, rnd);
361*088332b5SXin Li lua_geti(L, 1, p);
362*088332b5SXin Li lua_geti(L, 1, lo);
363*088332b5SXin Li if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
364*088332b5SXin Li set2(L, p, lo); /* swap a[p] - a[lo] */
365*088332b5SXin Li else {
366*088332b5SXin Li lua_pop(L, 1); /* remove a[lo] */
367*088332b5SXin Li lua_geti(L, 1, up);
368*088332b5SXin Li if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
369*088332b5SXin Li set2(L, p, up); /* swap a[up] - a[p] */
370*088332b5SXin Li else
371*088332b5SXin Li lua_pop(L, 2);
372*088332b5SXin Li }
373*088332b5SXin Li if (up - lo == 2) /* only 3 elements? */
374*088332b5SXin Li return; /* already sorted */
375*088332b5SXin Li lua_geti(L, 1, p); /* get middle element (Pivot) */
376*088332b5SXin Li lua_pushvalue(L, -1); /* push Pivot */
377*088332b5SXin Li lua_geti(L, 1, up - 1); /* push a[up - 1] */
378*088332b5SXin Li set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
379*088332b5SXin Li p = partition(L, lo, up);
380*088332b5SXin Li /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
381*088332b5SXin Li if (p - lo < up - p) { /* lower interval is smaller? */
382*088332b5SXin Li auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
383*088332b5SXin Li n = p - lo; /* size of smaller interval */
384*088332b5SXin Li lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
385*088332b5SXin Li }
386*088332b5SXin Li else {
387*088332b5SXin Li auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
388*088332b5SXin Li n = up - p; /* size of smaller interval */
389*088332b5SXin Li up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
390*088332b5SXin Li }
391*088332b5SXin Li if ((up - lo) / 128 > n) /* partition too imbalanced? */
392*088332b5SXin Li rnd = l_randomizePivot(); /* try a new randomization */
393*088332b5SXin Li } /* tail call auxsort(L, lo, up, rnd) */
394*088332b5SXin Li }
395*088332b5SXin Li
396*088332b5SXin Li
sort(lua_State * L)397*088332b5SXin Li static int sort (lua_State *L) {
398*088332b5SXin Li lua_Integer n = aux_getn(L, 1, TAB_RW);
399*088332b5SXin Li if (n > 1) { /* non-trivial interval? */
400*088332b5SXin Li luaL_argcheck(L, n < INT_MAX, 1, "array too big");
401*088332b5SXin Li if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
402*088332b5SXin Li luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */
403*088332b5SXin Li lua_settop(L, 2); /* make sure there are two arguments */
404*088332b5SXin Li auxsort(L, 1, (IdxT)n, 0);
405*088332b5SXin Li }
406*088332b5SXin Li return 0;
407*088332b5SXin Li }
408*088332b5SXin Li
409*088332b5SXin Li /* }====================================================== */
410*088332b5SXin Li
411*088332b5SXin Li
412*088332b5SXin Li static const luaL_Reg tab_funcs[] = {
413*088332b5SXin Li {"concat", tconcat},
414*088332b5SXin Li {"insert", tinsert},
415*088332b5SXin Li {"pack", tpack},
416*088332b5SXin Li {"unpack", tunpack},
417*088332b5SXin Li {"remove", tremove},
418*088332b5SXin Li {"move", tmove},
419*088332b5SXin Li {"sort", sort},
420*088332b5SXin Li {NULL, NULL}
421*088332b5SXin Li };
422*088332b5SXin Li
423*088332b5SXin Li
luaopen_table(lua_State * L)424*088332b5SXin Li LUAMOD_API int luaopen_table (lua_State *L) {
425*088332b5SXin Li luaL_newlib(L, tab_funcs);
426*088332b5SXin Li return 1;
427*088332b5SXin Li }
428*088332b5SXin Li
429