Path: blob/main/sys/contrib/openzfs/module/lua/ltablib.c
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: ltablib.c,v 1.65.1.2 2014/05/07 16:32:55 roberto Exp $3** Library for Table Manipulation4** See Copyright Notice in lua.h5*/678#define ltablib_c9#define LUA_LIB1011#include <sys/lua/lua.h>1213#include <sys/lua/lauxlib.h>14#include <sys/lua/lualib.h>151617#define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))18192021#if defined(LUA_COMPAT_MAXN)22static int maxn (lua_State *L) {23lua_Number max = 0;24luaL_checktype(L, 1, LUA_TTABLE);25lua_pushnil(L); /* first key */26while (lua_next(L, 1)) {27lua_pop(L, 1); /* remove value */28if (lua_type(L, -1) == LUA_TNUMBER) {29lua_Number v = lua_tonumber(L, -1);30if (v > max) max = v;31}32}33lua_pushnumber(L, max);34return 1;35}36#endif373839static int tinsert (lua_State *L) {40int e = aux_getn(L, 1) + 1; /* first empty element */41int pos; /* where to insert new element */42switch (lua_gettop(L)) {43case 2: { /* called with only 2 arguments */44pos = e; /* insert new element at the end */45break;46}47case 3: {48int i;49pos = luaL_checkint(L, 2); /* 2nd argument is the position */50luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");51for (i = e; i > pos; i--) { /* move up elements */52lua_rawgeti(L, 1, i-1);53lua_rawseti(L, 1, i); /* t[i] = t[i-1] */54}55break;56}57default: {58return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));59}60}61lua_rawseti(L, 1, pos); /* t[pos] = v */62return 0;63}646566static int tremove (lua_State *L) {67int size = aux_getn(L, 1);68int pos = luaL_optint(L, 2, size);69if (pos != size) /* validate 'pos' if given */70luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");71lua_rawgeti(L, 1, pos); /* result = t[pos] */72for ( ; pos < size; pos++) {73lua_rawgeti(L, 1, pos+1);74lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */75}76lua_pushnil(L);77lua_rawseti(L, 1, pos); /* t[pos] = nil */78return 1;79}808182static void addfield (lua_State *L, luaL_Buffer *b, int i) {83lua_rawgeti(L, 1, i);84if (!lua_isstring(L, -1))85luaL_error(L, "invalid value (%s) at index %d in table for "86LUA_QL("concat"), luaL_typename(L, -1), i);87luaL_addvalue(b);88}899091static int tconcat (lua_State *L) {92luaL_Buffer b;93size_t lsep;94int i, last;95const char *sep = luaL_optlstring(L, 2, "", &lsep);96luaL_checktype(L, 1, LUA_TTABLE);97i = luaL_optint(L, 3, 1);98last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));99luaL_buffinit(L, &b);100for (; i < last; i++) {101addfield(L, &b, i);102luaL_addlstring(&b, sep, lsep);103}104if (i == last) /* add last value (if interval was not empty) */105addfield(L, &b, i);106luaL_pushresult(&b);107return 1;108}109110111/*112** {======================================================113** Pack/unpack114** =======================================================115*/116117static int pack (lua_State *L) {118int n = lua_gettop(L); /* number of elements to pack */119lua_createtable(L, n, 1); /* create result table */120lua_pushinteger(L, n);121lua_setfield(L, -2, "n"); /* t.n = number of elements */122if (n > 0) { /* at least one element? */123int i;124lua_pushvalue(L, 1);125lua_rawseti(L, -2, 1); /* insert first element */126lua_replace(L, 1); /* move table into index 1 */127for (i = n; i >= 2; i--) /* assign other elements */128lua_rawseti(L, 1, i);129}130return 1; /* return table */131}132133134static int unpack (lua_State *L) {135int i, e;136unsigned int n;137luaL_checktype(L, 1, LUA_TTABLE);138i = luaL_optint(L, 2, 1);139e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));140if (i > e) return 0; /* empty range */141n = (unsigned int)e - (unsigned int)i; /* number of elements minus 1 */142if (n > (INT_MAX - 10) || !lua_checkstack(L, ++n))143return luaL_error(L, "too many results to unpack");144lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */145while (i++ < e) /* push arg[i + 1...e] */146lua_rawgeti(L, 1, i);147return n;148}149150/* }====================================================== */151152153154/*155** {======================================================156** Quicksort157** (based on `Algorithms in MODULA-3', Robert Sedgewick;158** Addison-Wesley, 1993.)159** =======================================================160*/161162163static void set2 (lua_State *L, int i, int j) {164lua_rawseti(L, 1, i);165lua_rawseti(L, 1, j);166}167168static int sort_comp (lua_State *L, int a, int b) {169if (!lua_isnil(L, 2)) { /* function? */170int res;171lua_pushvalue(L, 2);172lua_pushvalue(L, a-1); /* -1 to compensate function */173lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */174lua_call(L, 2, 1);175res = lua_toboolean(L, -1);176lua_pop(L, 1);177return res;178}179else /* a < b? */180return lua_compare(L, a, b, LUA_OPLT);181}182183static void auxsort (lua_State *L, int l, int u) {184while (l < u) { /* for tail recursion */185int i, j;186/* sort elements a[l], a[(l+u)/2] and a[u] */187lua_rawgeti(L, 1, l);188lua_rawgeti(L, 1, u);189if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */190set2(L, l, u); /* swap a[l] - a[u] */191else192lua_pop(L, 2);193if (u-l == 1) break; /* only 2 elements */194i = (l+u)/2;195lua_rawgeti(L, 1, i);196lua_rawgeti(L, 1, l);197if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */198set2(L, i, l);199else {200lua_pop(L, 1); /* remove a[l] */201lua_rawgeti(L, 1, u);202if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */203set2(L, i, u);204else205lua_pop(L, 2);206}207if (u-l == 2) break; /* only 3 elements */208lua_rawgeti(L, 1, i); /* Pivot */209lua_pushvalue(L, -1);210lua_rawgeti(L, 1, u-1);211set2(L, i, u-1);212/* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */213i = l; j = u-1;214for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */215/* repeat ++i until a[i] >= P */216while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {217if (i>=u) luaL_error(L, "invalid order function for sorting");218lua_pop(L, 1); /* remove a[i] */219}220/* repeat --j until a[j] <= P */221while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {222if (j<=l) luaL_error(L, "invalid order function for sorting");223lua_pop(L, 1); /* remove a[j] */224}225if (j<i) {226lua_pop(L, 3); /* pop pivot, a[i], a[j] */227break;228}229set2(L, i, j);230}231lua_rawgeti(L, 1, u-1);232lua_rawgeti(L, 1, i);233set2(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] */236if (i-l < u-i) {237j=l; i=i-1; l=i+2;238}239else {240j=i+1; i=u; u=j-2;241}242auxsort(L, j, i); /* call recursively the smaller one */243} /* repeat the routine for the larger one */244}245246static int tsort (lua_State *L) {247int n = aux_getn(L, 1);248luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */249if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */250luaL_checktype(L, 2, LUA_TFUNCTION);251lua_settop(L, 2); /* make sure there is two arguments */252auxsort(L, 1, n);253return 0;254}255256/* }====================================================== */257258259static const luaL_Reg tab_funcs[] = {260{"concat", tconcat},261#if defined(LUA_COMPAT_MAXN)262{"maxn", maxn},263#endif264{"insert", tinsert},265{"pack", pack},266{"unpack", unpack},267{"remove", tremove},268{"sort", tsort},269{NULL, NULL}270};271272273LUAMOD_API int luaopen_table (lua_State *L) {274luaL_newlib(L, tab_funcs);275#if defined(LUA_COMPAT_UNPACK)276/* _G.unpack = table.unpack */277lua_getfield(L, -1, "unpack");278lua_setglobal(L, "unpack");279#endif280return 1;281}282283#if defined(_KERNEL)284285EXPORT_SYMBOL(luaopen_table);286287#endif288289290