xref: /netbsd-src/external/mit/lua/dist/src/ltablib.c (revision bdda0531de537df87feb2bf576711ab1be9b3675)
1*bdda0531Snikita /*	$NetBSD: ltablib.c,v 1.11 2023/06/08 21:12:08 nikita Exp $	*/
2dbec5304Smbalmer 
3dbec5304Smbalmer /*
4f0dad708Snikita ** Id: ltablib.c
5dbec5304Smbalmer ** Library for Table Manipulation
6dbec5304Smbalmer ** See Copyright Notice in lua.h
7dbec5304Smbalmer */
8dbec5304Smbalmer 
973008250Slneto #define ltablib_c
1073008250Slneto #define LUA_LIB
1173008250Slneto 
1273008250Slneto #include "lprefix.h"
1373008250Slneto 
14dbec5304Smbalmer 
154ab4902eSlneto #ifndef _KERNEL
164ab4902eSlneto #include <limits.h>
17dbec5304Smbalmer #include <stddef.h>
182d6cb6c2Slneto #include <string.h>
192d6cb6c2Slneto #endif /* _KERNEL */
20dbec5304Smbalmer 
21dbec5304Smbalmer #include "lua.h"
22dbec5304Smbalmer 
23dbec5304Smbalmer #include "lauxlib.h"
24dbec5304Smbalmer #include "lualib.h"
25dbec5304Smbalmer 
26dbec5304Smbalmer 
2773008250Slneto /*
282d6cb6c2Slneto ** Operations that an object must define to mimic a table
292d6cb6c2Slneto ** (some functions only need some of them)
3073008250Slneto */
312d6cb6c2Slneto #define TAB_R	1			/* read */
322d6cb6c2Slneto #define TAB_W	2			/* write */
332d6cb6c2Slneto #define TAB_L	4			/* length */
342d6cb6c2Slneto #define TAB_RW	(TAB_R | TAB_W)		/* read/write */
352d6cb6c2Slneto 
362d6cb6c2Slneto 
372d6cb6c2Slneto #define aux_getn(L,n,w)	(checktab(L, n, (w) | TAB_L), luaL_len(L, n))
382d6cb6c2Slneto 
392d6cb6c2Slneto 
checkfield(lua_State * L,const char * key,int n)402d6cb6c2Slneto static int checkfield (lua_State *L, const char *key, int n) {
412d6cb6c2Slneto   lua_pushstring(L, key);
422d6cb6c2Slneto   return (lua_rawget(L, -n) != LUA_TNIL);
432d6cb6c2Slneto }
4473008250Slneto 
4573008250Slneto 
4673008250Slneto /*
472d6cb6c2Slneto ** Check that 'arg' either is a table or can behave like one (that is,
482d6cb6c2Slneto ** has a metatable with the required metamethods)
4973008250Slneto */
checktab(lua_State * L,int arg,int what)502d6cb6c2Slneto static void checktab (lua_State *L, int arg, int what) {
512d6cb6c2Slneto   if (lua_type(L, arg) != LUA_TTABLE) {  /* is it not a table? */
522d6cb6c2Slneto     int n = 1;  /* number of elements to pop */
532d6cb6c2Slneto     if (lua_getmetatable(L, arg) &&  /* must have metatable */
542d6cb6c2Slneto         (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
552d6cb6c2Slneto         (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
562d6cb6c2Slneto         (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
572d6cb6c2Slneto       lua_pop(L, n);  /* pop metatable and tested metamethods */
5873008250Slneto     }
592d6cb6c2Slneto     else
60c7f896d7Ssalazar       luaL_checktype(L, arg, LUA_TTABLE);  /* force an error */
6173008250Slneto   }
6273008250Slneto }
6373008250Slneto 
6473008250Slneto 
tinsert(lua_State * L)65dbec5304Smbalmer static int tinsert (lua_State *L) {
664ab4902eSlneto   lua_Integer pos;  /* where to insert new element */
67f0dad708Snikita   lua_Integer e = aux_getn(L, 1, TAB_RW);
68f0dad708Snikita   e = luaL_intop(+, e, 1);  /* first empty element */
69dbec5304Smbalmer   switch (lua_gettop(L)) {
70dbec5304Smbalmer     case 2: {  /* called with only 2 arguments */
71dbec5304Smbalmer       pos = e;  /* insert new element at the end */
72dbec5304Smbalmer       break;
73dbec5304Smbalmer     }
74dbec5304Smbalmer     case 3: {
754ab4902eSlneto       lua_Integer i;
764ab4902eSlneto       pos = luaL_checkinteger(L, 2);  /* 2nd argument is the position */
77f0dad708Snikita       /* check whether 'pos' is in [1, e] */
78f0dad708Snikita       luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
79f0dad708Snikita                        "position out of bounds");
80dbec5304Smbalmer       for (i = e; i > pos; i--) {  /* move up elements */
812d6cb6c2Slneto         lua_geti(L, 1, i - 1);
822d6cb6c2Slneto         lua_seti(L, 1, i);  /* t[i] = t[i - 1] */
83dbec5304Smbalmer       }
84dbec5304Smbalmer       break;
85dbec5304Smbalmer     }
86dbec5304Smbalmer     default: {
8773008250Slneto       return luaL_error(L, "wrong number of arguments to 'insert'");
88dbec5304Smbalmer     }
89dbec5304Smbalmer   }
902d6cb6c2Slneto   lua_seti(L, 1, pos);  /* t[pos] = v */
91dbec5304Smbalmer   return 0;
92dbec5304Smbalmer }
93dbec5304Smbalmer 
94dbec5304Smbalmer 
tremove(lua_State * L)95dbec5304Smbalmer static int tremove (lua_State *L) {
962d6cb6c2Slneto   lua_Integer size = aux_getn(L, 1, TAB_RW);
974ab4902eSlneto   lua_Integer pos = luaL_optinteger(L, 2, size);
984ab4902eSlneto   if (pos != size)  /* validate 'pos' if given */
99f0dad708Snikita     /* check whether 'pos' is in [1, size + 1] */
100*bdda0531Snikita     luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2,
101f0dad708Snikita                      "position out of bounds");
1022d6cb6c2Slneto   lua_geti(L, 1, pos);  /* result = t[pos] */
1034ab4902eSlneto   for ( ; pos < size; pos++) {
1042d6cb6c2Slneto     lua_geti(L, 1, pos + 1);
1052d6cb6c2Slneto     lua_seti(L, 1, pos);  /* t[pos] = t[pos + 1] */
106dbec5304Smbalmer   }
107dbec5304Smbalmer   lua_pushnil(L);
108f0dad708Snikita   lua_seti(L, 1, pos);  /* remove entry t[pos] */
109dbec5304Smbalmer   return 1;
110dbec5304Smbalmer }
111dbec5304Smbalmer 
112dbec5304Smbalmer 
1132d6cb6c2Slneto /*
1142d6cb6c2Slneto ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
1152d6cb6c2Slneto ** possible, copy in increasing order, which is better for rehashing.
1162d6cb6c2Slneto ** "possible" means destination after original range, or smaller
1172d6cb6c2Slneto ** than origin, or copying to another table.
1182d6cb6c2Slneto */
tmove(lua_State * L)11973008250Slneto static int tmove (lua_State *L) {
12073008250Slneto   lua_Integer f = luaL_checkinteger(L, 2);
12173008250Slneto   lua_Integer e = luaL_checkinteger(L, 3);
12273008250Slneto   lua_Integer t = luaL_checkinteger(L, 4);
12373008250Slneto   int tt = !lua_isnoneornil(L, 5) ? 5 : 1;  /* destination table */
1242d6cb6c2Slneto   checktab(L, 1, TAB_R);
1252d6cb6c2Slneto   checktab(L, tt, TAB_W);
12673008250Slneto   if (e >= f) {  /* otherwise, nothing to move */
12773008250Slneto     lua_Integer n, i;
128bee09862Smbalmer     luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
129bee09862Smbalmer                   "too many elements to move");
13073008250Slneto     n = e - f + 1;  /* number of elements to move */
131bee09862Smbalmer     luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
132bee09862Smbalmer                   "destination wrap around");
133c7f896d7Ssalazar     if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
1342d6cb6c2Slneto       for (i = 0; i < n; i++) {
1352d6cb6c2Slneto         lua_geti(L, 1, f + i);
1362d6cb6c2Slneto         lua_seti(L, tt, t + i);
13773008250Slneto       }
13873008250Slneto     }
13973008250Slneto     else {
1402d6cb6c2Slneto       for (i = n - 1; i >= 0; i--) {
1412d6cb6c2Slneto         lua_geti(L, 1, f + i);
1422d6cb6c2Slneto         lua_seti(L, tt, t + i);
14373008250Slneto       }
14473008250Slneto     }
14573008250Slneto   }
146c7f896d7Ssalazar   lua_pushvalue(L, tt);  /* return destination table */
14773008250Slneto   return 1;
14873008250Slneto }
14973008250Slneto 
15073008250Slneto 
addfield(lua_State * L,luaL_Buffer * b,lua_Integer i)1512d6cb6c2Slneto static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
1522d6cb6c2Slneto   lua_geti(L, 1, i);
153f0dad708Snikita   if (l_unlikely(!lua_isstring(L, -1)))
154f0dad708Snikita     luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
155f0dad708Snikita                   luaL_typename(L, -1), (LUAI_UACINT)i);
156dbec5304Smbalmer   luaL_addvalue(b);
157dbec5304Smbalmer }
158dbec5304Smbalmer 
159dbec5304Smbalmer 
tconcat(lua_State * L)160dbec5304Smbalmer static int tconcat (lua_State *L) {
161dbec5304Smbalmer   luaL_Buffer b;
1622d6cb6c2Slneto   lua_Integer last = aux_getn(L, 1, TAB_R);
163dbec5304Smbalmer   size_t lsep;
164dbec5304Smbalmer   const char *sep = luaL_optlstring(L, 2, "", &lsep);
1652d6cb6c2Slneto   lua_Integer i = luaL_optinteger(L, 3, 1);
166c7f896d7Ssalazar   last = luaL_optinteger(L, 4, last);
167dbec5304Smbalmer   luaL_buffinit(L, &b);
168dbec5304Smbalmer   for (; i < last; i++) {
1692d6cb6c2Slneto     addfield(L, &b, i);
170dbec5304Smbalmer     luaL_addlstring(&b, sep, lsep);
171dbec5304Smbalmer   }
172dbec5304Smbalmer   if (i == last)  /* add last value (if interval was not empty) */
1732d6cb6c2Slneto     addfield(L, &b, i);
174dbec5304Smbalmer   luaL_pushresult(&b);
175dbec5304Smbalmer   return 1;
176dbec5304Smbalmer }
177dbec5304Smbalmer 
178dbec5304Smbalmer 
1794ab4902eSlneto /*
1804ab4902eSlneto ** {======================================================
1814ab4902eSlneto ** Pack/unpack
1824ab4902eSlneto ** =======================================================
1834ab4902eSlneto */
1844ab4902eSlneto 
tpack(lua_State * L)185f0dad708Snikita static int tpack (lua_State *L) {
1864ab4902eSlneto   int i;
1874ab4902eSlneto   int n = lua_gettop(L);  /* number of elements to pack */
1884ab4902eSlneto   lua_createtable(L, n, 1);  /* create result table */
1894ab4902eSlneto   lua_insert(L, 1);  /* put it at index 1 */
1904ab4902eSlneto   for (i = n; i >= 1; i--)  /* assign elements */
1912d6cb6c2Slneto     lua_seti(L, 1, i);
1924ab4902eSlneto   lua_pushinteger(L, n);
1934ab4902eSlneto   lua_setfield(L, 1, "n");  /* t.n = number of elements */
1944ab4902eSlneto   return 1;  /* return table */
1954ab4902eSlneto }
1964ab4902eSlneto 
1974ab4902eSlneto 
tunpack(lua_State * L)198f0dad708Snikita static int tunpack (lua_State *L) {
1994ab4902eSlneto   lua_Unsigned n;
2002d6cb6c2Slneto   lua_Integer i = luaL_optinteger(L, 2, 1);
2012d6cb6c2Slneto   lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
2024ab4902eSlneto   if (i > e) return 0;  /* empty range */
2034ab4902eSlneto   n = (lua_Unsigned)e - i;  /* number of elements minus 1 (avoid overflows) */
204f0dad708Snikita   if (l_unlikely(n >= (unsigned int)INT_MAX  ||
205f0dad708Snikita                  !lua_checkstack(L, (int)(++n))))
2064ab4902eSlneto     return luaL_error(L, "too many results to unpack");
2072d6cb6c2Slneto   for (; i < e; i++) {  /* push arg[i..e - 1] (to avoid overflows) */
2082d6cb6c2Slneto     lua_geti(L, 1, i);
2092d6cb6c2Slneto   }
2102d6cb6c2Slneto   lua_geti(L, 1, e);  /* push last element */
21173008250Slneto   return (int)n;
2124ab4902eSlneto }
2134ab4902eSlneto 
2144ab4902eSlneto /* }====================================================== */
2154ab4902eSlneto 
2164ab4902eSlneto 
217dbec5304Smbalmer 
218dbec5304Smbalmer /*
219dbec5304Smbalmer ** {======================================================
220dbec5304Smbalmer ** Quicksort
22173008250Slneto ** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
222dbec5304Smbalmer **  Addison-Wesley, 1993.)
2234ab4902eSlneto ** =======================================================
224dbec5304Smbalmer */
225dbec5304Smbalmer 
226dbec5304Smbalmer 
227c7f896d7Ssalazar /* type for array indices */
228c7f896d7Ssalazar typedef unsigned int IdxT;
229c7f896d7Ssalazar 
230c7f896d7Ssalazar 
2312d6cb6c2Slneto /*
2322d6cb6c2Slneto ** Produce a "random" 'unsigned int' to randomize pivot choice. This
2332d6cb6c2Slneto ** macro is used only when 'sort' detects a big imbalance in the result
2342d6cb6c2Slneto ** of a partition. (If you don't want/need this "randomness", ~0 is a
2352d6cb6c2Slneto ** good choice.)
2362d6cb6c2Slneto */
2372d6cb6c2Slneto #if !defined(l_randomizePivot)		/* { */
2382d6cb6c2Slneto 
2392d6cb6c2Slneto #include <time.h>
2402d6cb6c2Slneto 
2412d6cb6c2Slneto /* size of 'e' measured in number of 'unsigned int's */
2422d6cb6c2Slneto #define sof(e)		(sizeof(e) / sizeof(unsigned int))
2432d6cb6c2Slneto 
2442d6cb6c2Slneto /*
2452d6cb6c2Slneto ** Use 'time' and 'clock' as sources of "randomness". Because we don't
2462d6cb6c2Slneto ** know the types 'clock_t' and 'time_t', we cannot cast them to
2472d6cb6c2Slneto ** anything without risking overflows. A safe way to use their values
2482d6cb6c2Slneto ** is to copy them to an array of a known type and use the array values.
2492d6cb6c2Slneto */
l_randomizePivot(void)2502d6cb6c2Slneto static unsigned int l_randomizePivot (void) {
2512d6cb6c2Slneto   clock_t c = clock();
2522d6cb6c2Slneto   time_t t = time(NULL);
2532d6cb6c2Slneto   unsigned int buff[sof(c) + sof(t)];
2542d6cb6c2Slneto   unsigned int i, rnd = 0;
2552d6cb6c2Slneto   memcpy(buff, &c, sof(c) * sizeof(unsigned int));
2562d6cb6c2Slneto   memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
2572d6cb6c2Slneto   for (i = 0; i < sof(buff); i++)
2582d6cb6c2Slneto     rnd += buff[i];
2592d6cb6c2Slneto   return rnd;
260dbec5304Smbalmer }
261dbec5304Smbalmer 
2622d6cb6c2Slneto #endif					/* } */
2632d6cb6c2Slneto 
2642d6cb6c2Slneto 
2652d6cb6c2Slneto /* arrays larger than 'RANLIMIT' may use randomized pivots */
2662d6cb6c2Slneto #define RANLIMIT	100u
2672d6cb6c2Slneto 
2682d6cb6c2Slneto 
set2(lua_State * L,IdxT i,IdxT j)269c7f896d7Ssalazar static void set2 (lua_State *L, IdxT i, IdxT j) {
2702d6cb6c2Slneto   lua_seti(L, 1, i);
2712d6cb6c2Slneto   lua_seti(L, 1, j);
2722d6cb6c2Slneto }
2732d6cb6c2Slneto 
2742d6cb6c2Slneto 
2752d6cb6c2Slneto /*
2762d6cb6c2Slneto ** Return true iff value at stack index 'a' is less than the value at
2772d6cb6c2Slneto ** index 'b' (according to the order of the sort).
2782d6cb6c2Slneto */
sort_comp(lua_State * L,int a,int b)279dbec5304Smbalmer static int sort_comp (lua_State *L, int a, int b) {
2802d6cb6c2Slneto   if (lua_isnil(L, 2))  /* no function? */
2812d6cb6c2Slneto     return lua_compare(L, a, b, LUA_OPLT);  /* a < b */
2822d6cb6c2Slneto   else {  /* function */
283dbec5304Smbalmer     int res;
2842d6cb6c2Slneto     lua_pushvalue(L, 2);    /* push function */
285dbec5304Smbalmer     lua_pushvalue(L, a-1);  /* -1 to compensate function */
28673008250Slneto     lua_pushvalue(L, b-2);  /* -2 to compensate function and 'a' */
2872d6cb6c2Slneto     lua_call(L, 2, 1);      /* call function */
2882d6cb6c2Slneto     res = lua_toboolean(L, -1);  /* get result */
2892d6cb6c2Slneto     lua_pop(L, 1);          /* pop result */
290dbec5304Smbalmer     return res;
291dbec5304Smbalmer   }
292dbec5304Smbalmer }
293dbec5304Smbalmer 
2942d6cb6c2Slneto 
2952d6cb6c2Slneto /*
2962d6cb6c2Slneto ** Does the partition: Pivot P is at the top of the stack.
2972d6cb6c2Slneto ** precondition: a[lo] <= P == a[up-1] <= a[up],
2982d6cb6c2Slneto ** so it only needs to do the partition from lo + 1 to up - 2.
2992d6cb6c2Slneto ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
3002d6cb6c2Slneto ** returns 'i'.
3012d6cb6c2Slneto */
partition(lua_State * L,IdxT lo,IdxT up)302c7f896d7Ssalazar static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
303c7f896d7Ssalazar   IdxT i = lo;  /* will be incremented before first use */
304c7f896d7Ssalazar   IdxT j = up - 1;  /* will be decremented before first use */
3052d6cb6c2Slneto   /* loop invariant: a[lo .. i] <= P <= a[j .. up] */
3062d6cb6c2Slneto   for (;;) {
3072d6cb6c2Slneto     /* next loop: repeat ++i while a[i] < P */
308f0dad708Snikita     while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
309f0dad708Snikita       if (l_unlikely(i == up - 1))  /* a[i] < P  but a[up - 1] == P  ?? */
3102d6cb6c2Slneto         luaL_error(L, "invalid order function for sorting");
311dbec5304Smbalmer       lua_pop(L, 1);  /* remove a[i] */
312dbec5304Smbalmer     }
3132d6cb6c2Slneto     /* after the loop, a[i] >= P and a[lo .. i - 1] < P */
3142d6cb6c2Slneto     /* next loop: repeat --j while P < a[j] */
315f0dad708Snikita     while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
316f0dad708Snikita       if (l_unlikely(j < i))  /* j < i  but  a[j] > P ?? */
3172d6cb6c2Slneto         luaL_error(L, "invalid order function for sorting");
318dbec5304Smbalmer       lua_pop(L, 1);  /* remove a[j] */
319dbec5304Smbalmer     }
3202d6cb6c2Slneto     /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */
3212d6cb6c2Slneto     if (j < i) {  /* no elements out of place? */
3222d6cb6c2Slneto       /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */
3232d6cb6c2Slneto       lua_pop(L, 1);  /* pop a[j] */
3242d6cb6c2Slneto       /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */
3252d6cb6c2Slneto       set2(L, up - 1, i);
3262d6cb6c2Slneto       return i;
327dbec5304Smbalmer     }
3282d6cb6c2Slneto     /* otherwise, swap a[i] - a[j] to restore invariant and repeat */
3292d6cb6c2Slneto     set2(L, i, j);
330dbec5304Smbalmer   }
331dbec5304Smbalmer }
332dbec5304Smbalmer 
3332d6cb6c2Slneto 
3342d6cb6c2Slneto /*
3352d6cb6c2Slneto ** Choose an element in the middle (2nd-3th quarters) of [lo,up]
3362d6cb6c2Slneto ** "randomized" by 'rnd'
3372d6cb6c2Slneto */
choosePivot(IdxT lo,IdxT up,unsigned int rnd)338c7f896d7Ssalazar static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
339c7f896d7Ssalazar   IdxT r4 = (up - lo) / 4;  /* range/4 */
340c7f896d7Ssalazar   IdxT p = rnd % (r4 * 2) + (lo + r4);
3412d6cb6c2Slneto   lua_assert(lo + r4 <= p && p <= up - r4);
3422d6cb6c2Slneto   return p;
3432d6cb6c2Slneto }
3442d6cb6c2Slneto 
3452d6cb6c2Slneto 
3462d6cb6c2Slneto /*
347f0dad708Snikita ** Quicksort algorithm (recursive function)
3482d6cb6c2Slneto */
auxsort(lua_State * L,IdxT lo,IdxT up,unsigned int rnd)349c7f896d7Ssalazar static void auxsort (lua_State *L, IdxT lo, IdxT up,
3502d6cb6c2Slneto                                    unsigned int rnd) {
3512d6cb6c2Slneto   while (lo < up) {  /* loop for tail recursion */
352c7f896d7Ssalazar     IdxT p;  /* Pivot index */
353c7f896d7Ssalazar     IdxT n;  /* to be used later */
3542d6cb6c2Slneto     /* sort elements 'lo', 'p', and 'up' */
3552d6cb6c2Slneto     lua_geti(L, 1, lo);
3562d6cb6c2Slneto     lua_geti(L, 1, up);
3572d6cb6c2Slneto     if (sort_comp(L, -1, -2))  /* a[up] < a[lo]? */
3582d6cb6c2Slneto       set2(L, lo, up);  /* swap a[lo] - a[up] */
3592d6cb6c2Slneto     else
3602d6cb6c2Slneto       lua_pop(L, 2);  /* remove both values */
3612d6cb6c2Slneto     if (up - lo == 1)  /* only 2 elements? */
3622d6cb6c2Slneto       return;  /* already sorted */
3632d6cb6c2Slneto     if (up - lo < RANLIMIT || rnd == 0)  /* small interval or no randomize? */
3642d6cb6c2Slneto       p = (lo + up)/2;  /* middle element is a good pivot */
3652d6cb6c2Slneto     else  /* for larger intervals, it is worth a random pivot */
3662d6cb6c2Slneto       p = choosePivot(lo, up, rnd);
3672d6cb6c2Slneto     lua_geti(L, 1, p);
3682d6cb6c2Slneto     lua_geti(L, 1, lo);
3692d6cb6c2Slneto     if (sort_comp(L, -2, -1))  /* a[p] < a[lo]? */
3702d6cb6c2Slneto       set2(L, p, lo);  /* swap a[p] - a[lo] */
3712d6cb6c2Slneto     else {
3722d6cb6c2Slneto       lua_pop(L, 1);  /* remove a[lo] */
3732d6cb6c2Slneto       lua_geti(L, 1, up);
3742d6cb6c2Slneto       if (sort_comp(L, -1, -2))  /* a[up] < a[p]? */
3752d6cb6c2Slneto         set2(L, p, up);  /* swap a[up] - a[p] */
3762d6cb6c2Slneto       else
3772d6cb6c2Slneto         lua_pop(L, 2);
3782d6cb6c2Slneto     }
3792d6cb6c2Slneto     if (up - lo == 2)  /* only 3 elements? */
3802d6cb6c2Slneto       return;  /* already sorted */
3812d6cb6c2Slneto     lua_geti(L, 1, p);  /* get middle element (Pivot) */
3822d6cb6c2Slneto     lua_pushvalue(L, -1);  /* push Pivot */
3832d6cb6c2Slneto     lua_geti(L, 1, up - 1);  /* push a[up - 1] */
3842d6cb6c2Slneto     set2(L, p, up - 1);  /* swap Pivot (a[p]) with a[up - 1] */
3852d6cb6c2Slneto     p = partition(L, lo, up);
3862d6cb6c2Slneto     /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
3872d6cb6c2Slneto     if (p - lo < up - p) {  /* lower interval is smaller? */
3882d6cb6c2Slneto       auxsort(L, lo, p - 1, rnd);  /* call recursively for lower interval */
3892d6cb6c2Slneto       n = p - lo;  /* size of smaller interval */
3902d6cb6c2Slneto       lo = p + 1;  /* tail call for [p + 1 .. up] (upper interval) */
3912d6cb6c2Slneto     }
3922d6cb6c2Slneto     else {
3932d6cb6c2Slneto       auxsort(L, p + 1, up, rnd);  /* call recursively for upper interval */
3942d6cb6c2Slneto       n = up - p;  /* size of smaller interval */
3952d6cb6c2Slneto       up = p - 1;  /* tail call for [lo .. p - 1]  (lower interval) */
3962d6cb6c2Slneto     }
397c7f896d7Ssalazar     if ((up - lo) / 128 > n) /* partition too imbalanced? */
3982d6cb6c2Slneto       rnd = l_randomizePivot();  /* try a new randomization */
3992d6cb6c2Slneto   }  /* tail call auxsort(L, lo, up, rnd) */
4002d6cb6c2Slneto }
4012d6cb6c2Slneto 
4022d6cb6c2Slneto 
sort(lua_State * L)403dbec5304Smbalmer static int sort (lua_State *L) {
4042d6cb6c2Slneto   lua_Integer n = aux_getn(L, 1, TAB_RW);
4052d6cb6c2Slneto   if (n > 1) {  /* non-trivial interval? */
4062d6cb6c2Slneto     luaL_argcheck(L, n < INT_MAX, 1, "array too big");
407dbec5304Smbalmer     if (!lua_isnoneornil(L, 2))  /* is there a 2nd argument? */
4082d6cb6c2Slneto       luaL_checktype(L, 2, LUA_TFUNCTION);  /* must be a function */
40973008250Slneto     lua_settop(L, 2);  /* make sure there are two arguments */
410c7f896d7Ssalazar     auxsort(L, 1, (IdxT)n, 0);
4112d6cb6c2Slneto   }
412dbec5304Smbalmer   return 0;
413dbec5304Smbalmer }
414dbec5304Smbalmer 
415dbec5304Smbalmer /* }====================================================== */
416dbec5304Smbalmer 
417dbec5304Smbalmer 
418dbec5304Smbalmer static const luaL_Reg tab_funcs[] = {
419dbec5304Smbalmer   {"concat", tconcat},
420dbec5304Smbalmer   {"insert", tinsert},
421f0dad708Snikita   {"pack", tpack},
422f0dad708Snikita   {"unpack", tunpack},
423dbec5304Smbalmer   {"remove", tremove},
42473008250Slneto   {"move", tmove},
425dbec5304Smbalmer   {"sort", sort},
426dbec5304Smbalmer   {NULL, NULL}
427dbec5304Smbalmer };
428dbec5304Smbalmer 
429dbec5304Smbalmer 
luaopen_table(lua_State * L)4304ab4902eSlneto LUAMOD_API int luaopen_table (lua_State *L) {
4314ab4902eSlneto   luaL_newlib(L, tab_funcs);
432dbec5304Smbalmer   return 1;
433dbec5304Smbalmer }
434dbec5304Smbalmer 
435