2002-04-10 00:19:06 +04:00
|
|
|
/*
|
2018-08-23 20:26:12 +03:00
|
|
|
** $Id: ltablib.c $
|
2002-04-10 00:19:06 +04:00
|
|
|
** Library for Table Manipulation
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#define ltablib_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
2002-04-10 00:19:06 +04:00
|
|
|
|
2014-04-01 22:50:34 +04:00
|
|
|
#include <limits.h>
|
2002-04-10 00:19:06 +04:00
|
|
|
#include <stddef.h>
|
2015-11-24 19:54:32 +03:00
|
|
|
#include <string.h>
|
2002-04-10 00:19:06 +04:00
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lualib.h"
|
2024-06-20 20:46:06 +03:00
|
|
|
#include "llimits.h"
|
2002-04-10 00:19:06 +04:00
|
|
|
|
|
|
|
|
2014-07-16 16:44:52 +04:00
|
|
|
/*
|
2015-09-09 18:42:30 +03:00
|
|
|
** Operations that an object must define to mimic a table
|
|
|
|
** (some functions only need some of them)
|
2014-07-16 16:44:52 +04:00
|
|
|
*/
|
2015-11-20 15:30:20 +03:00
|
|
|
#define TAB_R 1 /* read */
|
|
|
|
#define TAB_W 2 /* write */
|
|
|
|
#define TAB_L 4 /* length */
|
|
|
|
#define TAB_RW (TAB_R | TAB_W) /* read/write */
|
2015-09-09 18:42:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
#define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
|
|
|
|
|
|
|
|
|
|
|
|
static int checkfield (lua_State *L, const char *key, int n) {
|
|
|
|
lua_pushstring(L, key);
|
|
|
|
return (lua_rawget(L, -n) != LUA_TNIL);
|
|
|
|
}
|
2014-07-16 16:44:52 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2015-09-09 18:42:30 +03:00
|
|
|
** Check that 'arg' either is a table or can behave like one (that is,
|
|
|
|
** has a metatable with the required metamethods)
|
2014-07-16 16:44:52 +04:00
|
|
|
*/
|
2015-09-09 18:42:30 +03:00
|
|
|
static void checktab (lua_State *L, int arg, int what) {
|
|
|
|
if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
|
|
|
|
int n = 1; /* number of elements to pop */
|
|
|
|
if (lua_getmetatable(L, arg) && /* must have metatable */
|
|
|
|
(!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
|
|
|
|
(!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
|
|
|
|
(!(what & TAB_L) || checkfield(L, "__len", ++n))) {
|
|
|
|
lua_pop(L, n); /* pop metatable and tested metamethods */
|
2015-11-20 15:30:20 +03:00
|
|
|
}
|
2015-09-09 18:42:30 +03:00
|
|
|
else
|
2016-02-08 15:55:19 +03:00
|
|
|
luaL_checktype(L, arg, LUA_TTABLE); /* force an error */
|
2014-08-21 23:13:55 +04:00
|
|
|
}
|
2014-07-16 16:44:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-18 21:16:26 +03:00
|
|
|
static int tcreate (lua_State *L) {
|
2024-02-07 19:39:54 +03:00
|
|
|
lua_Unsigned sizeseq = (lua_Unsigned)luaL_checkinteger(L, 1);
|
|
|
|
lua_Unsigned sizerest = (lua_Unsigned)luaL_optinteger(L, 2, 0);
|
|
|
|
luaL_argcheck(L, sizeseq <= UINT_MAX, 1, "out of range");
|
|
|
|
luaL_argcheck(L, sizerest <= UINT_MAX, 2, "out of range");
|
2024-03-13 15:20:34 +03:00
|
|
|
lua_createtable(L, (unsigned)sizeseq, (unsigned)sizerest);
|
2024-01-18 21:16:26 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-07 21:31:16 +03:00
|
|
|
static int tinsert (lua_State *L) {
|
2014-04-04 20:38:11 +04:00
|
|
|
lua_Integer pos; /* where to insert new element */
|
2021-09-22 19:10:39 +03:00
|
|
|
lua_Integer e = aux_getn(L, 1, TAB_RW);
|
|
|
|
e = luaL_intop(+, e, 1); /* first empty element */
|
2005-10-23 21:38:15 +04:00
|
|
|
switch (lua_gettop(L)) {
|
|
|
|
case 2: { /* called with only 2 arguments */
|
|
|
|
pos = e; /* insert new element at the end */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
2014-04-04 20:38:11 +04:00
|
|
|
lua_Integer i;
|
|
|
|
pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
|
2019-03-27 20:56:10 +03:00
|
|
|
/* check whether 'pos' is in [1, e] */
|
|
|
|
luaL_argcheck(L, (lua_Unsigned)pos - 1u < (lua_Unsigned)e, 2,
|
|
|
|
"position out of bounds");
|
2005-10-23 21:38:15 +04:00
|
|
|
for (i = e; i > pos; i--) { /* move up elements */
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_geti(L, 1, i - 1);
|
|
|
|
lua_seti(L, 1, i); /* t[i] = t[i - 1] */
|
2005-10-23 21:38:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2014-10-17 20:28:21 +04:00
|
|
|
return luaL_error(L, "wrong number of arguments to 'insert'");
|
2005-03-16 19:58:41 +03:00
|
|
|
}
|
2002-04-10 00:19:06 +04:00
|
|
|
}
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_seti(L, 1, pos); /* t[pos] = v */
|
2002-04-10 00:19:06 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-07 21:31:16 +03:00
|
|
|
static int tremove (lua_State *L) {
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_Integer size = aux_getn(L, 1, TAB_RW);
|
2014-04-04 20:38:11 +04:00
|
|
|
lua_Integer pos = luaL_optinteger(L, 2, size);
|
2013-02-06 22:29:03 +04:00
|
|
|
if (pos != size) /* validate 'pos' if given */
|
2019-03-27 20:56:10 +03:00
|
|
|
/* check whether 'pos' is in [1, size + 1] */
|
2022-09-07 18:21:46 +03:00
|
|
|
luaL_argcheck(L, (lua_Unsigned)pos - 1u <= (lua_Unsigned)size, 2,
|
2019-03-27 20:56:10 +03:00
|
|
|
"position out of bounds");
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_geti(L, 1, pos); /* result = t[pos] */
|
2013-02-06 22:29:03 +04:00
|
|
|
for ( ; pos < size; pos++) {
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_geti(L, 1, pos + 1);
|
|
|
|
lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
|
2002-04-10 00:19:06 +04:00
|
|
|
}
|
2018-04-04 17:23:41 +03:00
|
|
|
lua_pushnil(L);
|
|
|
|
lua_seti(L, 1, pos); /* remove entry t[pos] */
|
2002-04-10 00:19:06 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-17 18:53:50 +03:00
|
|
|
/*
|
|
|
|
** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
|
|
|
|
** possible, copy in increasing order, which is better for rehashing.
|
|
|
|
** "possible" means destination after original range, or smaller
|
|
|
|
** than origin, or copying to another table.
|
|
|
|
*/
|
2014-08-21 23:13:55 +04:00
|
|
|
static int tmove (lua_State *L) {
|
2014-07-25 22:46:00 +04:00
|
|
|
lua_Integer f = luaL_checkinteger(L, 2);
|
|
|
|
lua_Integer e = luaL_checkinteger(L, 3);
|
2014-08-21 23:13:55 +04:00
|
|
|
lua_Integer t = luaL_checkinteger(L, 4);
|
|
|
|
int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
|
2015-09-09 18:42:30 +03:00
|
|
|
checktab(L, 1, TAB_R);
|
|
|
|
checktab(L, tt, TAB_W);
|
2014-07-25 22:46:00 +04:00
|
|
|
if (e >= f) { /* otherwise, nothing to move */
|
|
|
|
lua_Integer n, i;
|
2015-01-13 19:27:29 +03:00
|
|
|
luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
|
|
|
|
"too many elements to move");
|
2014-07-25 22:46:00 +04:00
|
|
|
n = e - f + 1; /* number of elements to move */
|
2015-01-13 19:27:29 +03:00
|
|
|
luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
|
|
|
|
"destination wrap around");
|
2016-02-25 22:41:54 +03:00
|
|
|
if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
|
2015-09-17 18:53:50 +03:00
|
|
|
for (i = 0; i < n; i++) {
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_geti(L, 1, f + i);
|
|
|
|
lua_seti(L, tt, t + i);
|
2014-07-25 22:46:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2015-09-17 18:53:50 +03:00
|
|
|
for (i = n - 1; i >= 0; i--) {
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_geti(L, 1, f + i);
|
|
|
|
lua_seti(L, tt, t + i);
|
2014-07-25 22:46:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-25 22:41:54 +03:00
|
|
|
lua_pushvalue(L, tt); /* return destination table */
|
2014-07-25 22:46:00 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-09 18:42:30 +03:00
|
|
|
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) {
|
|
|
|
lua_geti(L, 1, i);
|
2021-02-24 17:14:44 +03:00
|
|
|
if (l_unlikely(!lua_isstring(L, -1)))
|
2021-02-15 19:31:45 +03:00
|
|
|
luaL_error(L, "invalid value (%s) at index %I in table for 'concat'",
|
2021-03-29 17:47:12 +03:00
|
|
|
luaL_typename(L, -1), (LUAI_UACINT)i);
|
2011-09-30 16:45:45 +04:00
|
|
|
luaL_addvalue(b);
|
2008-02-14 19:03:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-11 22:48:02 +04:00
|
|
|
static int tconcat (lua_State *L) {
|
2002-07-01 23:25:28 +04:00
|
|
|
luaL_Buffer b;
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_Integer last = aux_getn(L, 1, TAB_R);
|
2015-11-23 14:09:27 +03:00
|
|
|
size_t lsep;
|
2002-11-14 18:41:38 +03:00
|
|
|
const char *sep = luaL_optlstring(L, 2, "", &lsep);
|
2015-11-23 14:09:27 +03:00
|
|
|
lua_Integer i = luaL_optinteger(L, 3, 1);
|
2016-02-08 15:55:19 +03:00
|
|
|
last = luaL_optinteger(L, 4, last);
|
2002-07-01 23:25:28 +04:00
|
|
|
luaL_buffinit(L, &b);
|
2008-02-14 19:03:27 +03:00
|
|
|
for (; i < last; i++) {
|
2015-09-09 18:42:30 +03:00
|
|
|
addfield(L, &b, i);
|
2008-02-14 19:03:27 +03:00
|
|
|
luaL_addlstring(&b, sep, lsep);
|
2002-07-01 23:25:28 +04:00
|
|
|
}
|
2008-02-14 19:03:27 +03:00
|
|
|
if (i == last) /* add last value (if interval was not empty) */
|
2015-09-09 18:42:30 +03:00
|
|
|
addfield(L, &b, i);
|
2002-07-01 23:25:28 +04:00
|
|
|
luaL_pushresult(&b);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-07 18:50:27 +03:00
|
|
|
/*
|
|
|
|
** {======================================================
|
2009-12-28 19:30:31 +03:00
|
|
|
** Pack/unpack
|
2009-12-07 18:50:27 +03:00
|
|
|
** =======================================================
|
|
|
|
*/
|
|
|
|
|
2018-03-16 17:18:18 +03:00
|
|
|
static int tpack (lua_State *L) {
|
2014-05-16 22:53:25 +04:00
|
|
|
int i;
|
2011-07-02 20:01:44 +04:00
|
|
|
int n = lua_gettop(L); /* number of elements to pack */
|
2024-07-27 19:32:59 +03:00
|
|
|
lua_createtable(L, cast_uint(n), 1); /* create result table */
|
2014-05-16 22:53:25 +04:00
|
|
|
lua_insert(L, 1); /* put it at index 1 */
|
|
|
|
for (i = n; i >= 1; i--) /* assign elements */
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_seti(L, 1, i);
|
2011-07-02 20:01:44 +04:00
|
|
|
lua_pushinteger(L, n);
|
2014-05-16 22:53:25 +04:00
|
|
|
lua_setfield(L, 1, "n"); /* t.n = number of elements */
|
2011-11-28 21:26:30 +04:00
|
|
|
return 1; /* return table */
|
2009-12-07 18:50:27 +03:00
|
|
|
}
|
|
|
|
|
2009-12-28 19:30:31 +03:00
|
|
|
|
2018-03-16 17:18:18 +03:00
|
|
|
static int tunpack (lua_State *L) {
|
2014-04-01 22:50:34 +04:00
|
|
|
lua_Unsigned n;
|
2015-11-23 14:09:27 +03:00
|
|
|
lua_Integer i = luaL_optinteger(L, 2, 1);
|
|
|
|
lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1));
|
2009-12-28 19:30:31 +03:00
|
|
|
if (i > e) return 0; /* empty range */
|
2024-07-27 19:32:59 +03:00
|
|
|
n = l_castS2U(e) - l_castS2U(i); /* number of elements minus 1 */
|
2021-02-24 17:14:44 +03:00
|
|
|
if (l_unlikely(n >= (unsigned int)INT_MAX ||
|
|
|
|
!lua_checkstack(L, (int)(++n))))
|
2009-12-28 19:30:31 +03:00
|
|
|
return luaL_error(L, "too many results to unpack");
|
2015-07-04 19:31:42 +03:00
|
|
|
for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_geti(L, 1, i);
|
2015-07-04 19:31:42 +03:00
|
|
|
}
|
2015-09-09 18:42:30 +03:00
|
|
|
lua_geti(L, 1, e); /* push last element */
|
2014-07-29 20:01:00 +04:00
|
|
|
return (int)n;
|
2009-12-28 19:30:31 +03:00
|
|
|
}
|
|
|
|
|
2009-12-07 18:50:27 +03:00
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2002-04-10 00:19:06 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** {======================================================
|
|
|
|
** Quicksort
|
2014-10-25 15:50:46 +04:00
|
|
|
** (based on 'Algorithms in MODULA-3', Robert Sedgewick;
|
2002-04-10 00:19:06 +04:00
|
|
|
** Addison-Wesley, 1993.)
|
2009-12-07 18:50:27 +03:00
|
|
|
** =======================================================
|
2002-04-10 00:19:06 +04:00
|
|
|
*/
|
|
|
|
|
2015-11-25 15:48:57 +03:00
|
|
|
|
2024-07-27 21:13:21 +03:00
|
|
|
/*
|
|
|
|
** Type for array indices. These indices are always limited by INT_MAX,
|
|
|
|
** so it is safe to cast them to lua_Integer even for Lua 32 bits.
|
|
|
|
*/
|
2015-12-14 14:57:38 +03:00
|
|
|
typedef unsigned int IdxT;
|
|
|
|
|
|
|
|
|
2024-07-27 21:13:21 +03:00
|
|
|
/* Versions of lua_seti/lua_geti specialized for IdxT */
|
|
|
|
#define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx))
|
|
|
|
#define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx))
|
|
|
|
|
|
|
|
|
2015-11-25 15:48:57 +03:00
|
|
|
/*
|
|
|
|
** Produce a "random" 'unsigned int' to randomize pivot choice. This
|
|
|
|
** macro is used only when 'sort' detects a big imbalance in the result
|
|
|
|
** of a partition. (If you don't want/need this "randomness", ~0 is a
|
|
|
|
** good choice.)
|
|
|
|
*/
|
2023-03-20 22:13:17 +03:00
|
|
|
#if !defined(l_randomizePivot)
|
|
|
|
#define l_randomizePivot(L) luaL_makeseed(L)
|
2015-11-25 15:48:57 +03:00
|
|
|
#endif /* } */
|
|
|
|
|
|
|
|
|
|
|
|
/* arrays larger than 'RANLIMIT' may use randomized pivots */
|
|
|
|
#define RANLIMIT 100u
|
|
|
|
|
|
|
|
|
2015-12-14 14:57:38 +03:00
|
|
|
static void set2 (lua_State *L, IdxT i, IdxT j) {
|
2024-07-27 21:13:21 +03:00
|
|
|
seti(L, 1, i);
|
|
|
|
seti(L, 1, j);
|
2002-04-10 00:19:06 +04:00
|
|
|
}
|
|
|
|
|
2015-11-25 15:48:57 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Return true iff value at stack index 'a' is less than the value at
|
|
|
|
** index 'b' (according to the order of the sort).
|
|
|
|
*/
|
2002-04-10 00:19:06 +04:00
|
|
|
static int sort_comp (lua_State *L, int a, int b) {
|
2015-11-25 15:48:57 +03:00
|
|
|
if (lua_isnil(L, 2)) /* no function? */
|
|
|
|
return lua_compare(L, a, b, LUA_OPLT); /* a < b */
|
|
|
|
else { /* function */
|
2002-04-10 00:19:06 +04:00
|
|
|
int res;
|
2015-11-25 15:48:57 +03:00
|
|
|
lua_pushvalue(L, 2); /* push function */
|
2002-04-10 00:19:06 +04:00
|
|
|
lua_pushvalue(L, a-1); /* -1 to compensate function */
|
2014-10-25 15:50:46 +04:00
|
|
|
lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
|
2015-11-25 15:48:57 +03:00
|
|
|
lua_call(L, 2, 1); /* call function */
|
|
|
|
res = lua_toboolean(L, -1); /* get result */
|
|
|
|
lua_pop(L, 1); /* pop result */
|
2002-04-10 00:19:06 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 19:07:14 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Does the partition: Pivot P is at the top of the stack.
|
|
|
|
** precondition: a[lo] <= P == a[up-1] <= a[up],
|
|
|
|
** so it only needs to do the partition from lo + 1 to up - 2.
|
|
|
|
** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up]
|
|
|
|
** returns 'i'.
|
|
|
|
*/
|
2015-12-14 14:57:38 +03:00
|
|
|
static IdxT partition (lua_State *L, IdxT lo, IdxT up) {
|
|
|
|
IdxT i = lo; /* will be incremented before first use */
|
|
|
|
IdxT j = up - 1; /* will be decremented before first use */
|
2015-11-06 19:07:14 +03:00
|
|
|
/* loop invariant: a[lo .. i] <= P <= a[j .. up] */
|
|
|
|
for (;;) {
|
|
|
|
/* next loop: repeat ++i while a[i] < P */
|
2024-07-27 21:13:21 +03:00
|
|
|
while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) {
|
2021-02-24 17:14:44 +03:00
|
|
|
if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */
|
2015-11-06 19:07:14 +03:00
|
|
|
luaL_error(L, "invalid order function for sorting");
|
|
|
|
lua_pop(L, 1); /* remove a[i] */
|
|
|
|
}
|
|
|
|
/* after the loop, a[i] >= P and a[lo .. i - 1] < P */
|
|
|
|
/* next loop: repeat --j while P < a[j] */
|
2024-07-27 21:13:21 +03:00
|
|
|
while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) {
|
2021-02-24 17:14:44 +03:00
|
|
|
if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */
|
2015-11-06 19:07:14 +03:00
|
|
|
luaL_error(L, "invalid order function for sorting");
|
|
|
|
lua_pop(L, 1); /* remove a[j] */
|
|
|
|
}
|
|
|
|
/* after the loop, a[j] <= P and a[j + 1 .. up] >= P */
|
|
|
|
if (j < i) { /* no elements out of place? */
|
|
|
|
/* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */
|
|
|
|
lua_pop(L, 1); /* pop a[j] */
|
|
|
|
/* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */
|
|
|
|
set2(L, up - 1, i);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/* otherwise, swap a[i] - a[j] to restore invariant and repeat */
|
|
|
|
set2(L, i, j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-12 21:07:25 +03:00
|
|
|
/*
|
2015-11-25 15:48:57 +03:00
|
|
|
** Choose an element in the middle (2nd-3th quarters) of [lo,up]
|
|
|
|
** "randomized" by 'rnd'
|
2015-11-12 21:07:25 +03:00
|
|
|
*/
|
2015-12-14 14:57:38 +03:00
|
|
|
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) {
|
|
|
|
IdxT r4 = (up - lo) / 4; /* range/4 */
|
2023-03-23 22:01:16 +03:00
|
|
|
IdxT p = (rnd ^ lo ^ up) % (r4 * 2) + (lo + r4);
|
2015-11-12 21:07:25 +03:00
|
|
|
lua_assert(lo + r4 <= p && p <= up - r4);
|
2015-11-23 14:17:31 +03:00
|
|
|
return p;
|
2015-11-12 21:07:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-25 15:48:57 +03:00
|
|
|
/*
|
2019-07-05 21:03:15 +03:00
|
|
|
** Quicksort algorithm (recursive function)
|
2015-11-25 15:48:57 +03:00
|
|
|
*/
|
2024-03-22 20:06:11 +03:00
|
|
|
static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) {
|
2015-11-06 19:07:14 +03:00
|
|
|
while (lo < up) { /* loop for tail recursion */
|
2015-12-14 14:57:38 +03:00
|
|
|
IdxT p; /* Pivot index */
|
|
|
|
IdxT n; /* to be used later */
|
2015-11-20 15:30:20 +03:00
|
|
|
/* sort elements 'lo', 'p', and 'up' */
|
2024-07-27 21:13:21 +03:00
|
|
|
geti(L, 1, lo);
|
|
|
|
geti(L, 1, up);
|
2015-11-06 19:07:14 +03:00
|
|
|
if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
|
|
|
|
set2(L, lo, up); /* swap a[lo] - a[up] */
|
2002-04-10 00:19:06 +04:00
|
|
|
else
|
2015-11-06 19:07:14 +03:00
|
|
|
lua_pop(L, 2); /* remove both values */
|
|
|
|
if (up - lo == 1) /* only 2 elements? */
|
|
|
|
return; /* already sorted */
|
2015-11-25 15:48:57 +03:00
|
|
|
if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
|
2015-11-12 21:07:25 +03:00
|
|
|
p = (lo + up)/2; /* middle element is a good pivot */
|
|
|
|
else /* for larger intervals, it is worth a random pivot */
|
2015-11-25 15:48:57 +03:00
|
|
|
p = choosePivot(lo, up, rnd);
|
2024-07-27 21:13:21 +03:00
|
|
|
geti(L, 1, p);
|
|
|
|
geti(L, 1, lo);
|
2015-11-06 19:07:14 +03:00
|
|
|
if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
|
|
|
|
set2(L, p, lo); /* swap a[p] - a[lo] */
|
2002-04-10 00:19:06 +04:00
|
|
|
else {
|
2015-11-06 19:07:14 +03:00
|
|
|
lua_pop(L, 1); /* remove a[lo] */
|
2024-07-27 21:13:21 +03:00
|
|
|
geti(L, 1, up);
|
2015-11-06 19:07:14 +03:00
|
|
|
if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
|
|
|
|
set2(L, p, up); /* swap a[up] - a[p] */
|
2002-04-10 00:19:06 +04:00
|
|
|
else
|
|
|
|
lua_pop(L, 2);
|
|
|
|
}
|
2015-11-06 19:07:14 +03:00
|
|
|
if (up - lo == 2) /* only 3 elements? */
|
|
|
|
return; /* already sorted */
|
2024-07-27 21:13:21 +03:00
|
|
|
geti(L, 1, p); /* get middle element (Pivot) */
|
2015-11-06 19:07:14 +03:00
|
|
|
lua_pushvalue(L, -1); /* push Pivot */
|
2024-07-27 21:13:21 +03:00
|
|
|
geti(L, 1, up - 1); /* push a[up - 1] */
|
2015-11-06 19:07:14 +03:00
|
|
|
set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
|
|
|
|
p = partition(L, lo, up);
|
|
|
|
/* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
|
|
|
|
if (p - lo < up - p) { /* lower interval is smaller? */
|
2015-11-25 15:48:57 +03:00
|
|
|
auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
|
|
|
|
n = p - lo; /* size of smaller interval */
|
2015-11-06 19:07:14 +03:00
|
|
|
lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
|
2002-04-10 00:19:06 +04:00
|
|
|
}
|
|
|
|
else {
|
2015-11-25 15:48:57 +03:00
|
|
|
auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
|
|
|
|
n = up - p; /* size of smaller interval */
|
2015-11-06 19:07:14 +03:00
|
|
|
up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
|
2002-04-10 00:19:06 +04:00
|
|
|
}
|
2015-12-14 14:57:38 +03:00
|
|
|
if ((up - lo) / 128 > n) /* partition too imbalanced? */
|
2023-03-20 22:13:17 +03:00
|
|
|
rnd = l_randomizePivot(L); /* try a new randomization */
|
2015-11-25 15:48:57 +03:00
|
|
|
} /* tail call auxsort(L, lo, up, rnd) */
|
2002-04-10 00:19:06 +04:00
|
|
|
}
|
|
|
|
|
2015-11-12 21:07:25 +03:00
|
|
|
|
2004-12-07 21:31:16 +03:00
|
|
|
static int sort (lua_State *L) {
|
2015-11-23 14:17:31 +03:00
|
|
|
lua_Integer n = aux_getn(L, 1, TAB_RW);
|
|
|
|
if (n > 1) { /* non-trivial interval? */
|
|
|
|
luaL_argcheck(L, n < INT_MAX, 1, "array too big");
|
|
|
|
if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
|
|
|
|
luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */
|
|
|
|
lua_settop(L, 2); /* make sure there are two arguments */
|
2015-12-14 14:57:38 +03:00
|
|
|
auxsort(L, 1, (IdxT)n, 0);
|
2015-11-23 14:17:31 +03:00
|
|
|
}
|
2002-04-10 00:19:06 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }====================================================== */
|
|
|
|
|
|
|
|
|
2005-08-26 21:36:32 +04:00
|
|
|
static const luaL_Reg tab_funcs[] = {
|
2005-07-11 22:48:02 +04:00
|
|
|
{"concat", tconcat},
|
2024-01-18 21:16:26 +03:00
|
|
|
{"create", tcreate},
|
2004-12-07 21:31:16 +03:00
|
|
|
{"insert", tinsert},
|
2018-03-16 17:18:18 +03:00
|
|
|
{"pack", tpack},
|
|
|
|
{"unpack", tunpack},
|
2004-12-07 21:31:16 +03:00
|
|
|
{"remove", tremove},
|
2014-08-21 23:13:55 +04:00
|
|
|
{"move", tmove},
|
2005-07-12 18:32:08 +04:00
|
|
|
{"sort", sort},
|
2002-04-10 00:19:06 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-11-24 15:05:44 +03:00
|
|
|
LUAMOD_API int luaopen_table (lua_State *L) {
|
2010-07-02 15:38:13 +04:00
|
|
|
luaL_newlib(L, tab_funcs);
|
2002-12-20 13:26:33 +03:00
|
|
|
return 1;
|
2002-04-10 00:19:06 +04:00
|
|
|
}
|
|
|
|
|