Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
ltablib.c File Reference
#include "lprefix.h"
#include <limits.h>
#include <stddef.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include <time.h>

Go to the source code of this file.

Macros

#define ltablib_c
 
#define LUA_LIB
 
#define TAB_R   1 /* read */
 
#define TAB_W   2 /* write */
 
#define TAB_L   4 /* length */
 
#define TAB_RW   (TAB_R | TAB_W) /* read/write */
 
#define aux_getn(L, n, w)   (checktab(L, n, (w) | TAB_L), luaL_len(L, n))
 
#define sof(e)   (sizeof(e) / sizeof(unsigned int))
 
#define RANLIMIT   100u
 

Typedefs

typedef unsigned int IdxT
 

Functions

static int checkfield (lua_State *L, const char *key, int n)
 
static void checktab (lua_State *L, int arg, int what)
 
static int tinsert (lua_State *L)
 
static int tremove (lua_State *L)
 
static int tmove (lua_State *L)
 
static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i)
 
static int tconcat (lua_State *L)
 
static int pack (lua_State *L)
 
static int unpack (lua_State *L)
 
static unsigned int l_randomizePivot (void)
 
static void set2 (lua_State *L, IdxT i, IdxT j)
 
static int sort_comp (lua_State *L, int a, int b)
 
static IdxT partition (lua_State *L, IdxT lo, IdxT up)
 
static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd)
 
static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned int rnd)
 
static int sort (lua_State *L)
 
LUAMOD_API int luaopen_table (lua_State *L)
 

Variables

static const luaL_Reg tab_funcs []
 

Macro Definition Documentation

◆ aux_getn

#define aux_getn ( L,
n,
w )   (checktab(L, n, (w) | TAB_L), luaL_len(L, n))

Definition at line 33 of file lua-5.3.6/src/ltablib.c.

Referenced by sort(), tconcat(), tinsert(), and tremove().

◆ ltablib_c

#define ltablib_c

Definition at line 7 of file lua-5.3.6/src/ltablib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.3.6/src/ltablib.c.

◆ RANLIMIT

#define RANLIMIT   100u

Definition at line 274 of file lua-5.3.6/src/ltablib.c.

Referenced by auxsort().

◆ sof

#define sof ( e)    (sizeof(e) / sizeof(unsigned int))

Definition at line 250 of file lua-5.3.6/src/ltablib.c.

Referenced by l_randomizePivot().

◆ TAB_L

#define TAB_L   4 /* length */

Definition at line 29 of file lua-5.3.6/src/ltablib.c.

Referenced by checktab().

◆ TAB_R

#define TAB_R   1 /* read */

Definition at line 27 of file lua-5.3.6/src/ltablib.c.

Referenced by checktab(), tconcat(), and tmove().

◆ TAB_RW

#define TAB_RW   (TAB_R | TAB_W) /* read/write */

Definition at line 30 of file lua-5.3.6/src/ltablib.c.

Referenced by sort(), tinsert(), and tremove().

◆ TAB_W

#define TAB_W   2 /* write */

Definition at line 28 of file lua-5.3.6/src/ltablib.c.

Referenced by checktab(), and tmove().

Typedef Documentation

◆ IdxT

typedef unsigned int IdxT

Definition at line 236 of file lua-5.3.6/src/ltablib.c.

Function Documentation

◆ addfield()

static void addfield ( lua_State * L,
luaL_Buffer * b,
lua_Integer i )
static

Definition at line 160 of file lua-5.3.6/src/ltablib.c.

160 {
161 lua_geti(L, 1, i);
162 if (!lua_isstring(L, -1))
163 luaL_error(L, "invalid value (%s) at index %d in table for 'concat'",
164 luaL_typename(L, -1), i);
165 luaL_addvalue(b);
166}
LUA_API int lua_isstring(lua_State *L, int idx)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define luaL_typename(L, i)
LUA_API int lua_geti(lua_State *L, int idx, lua_Integer n)

References lua_geti(), lua_isstring(), luaL_addvalue(), luaL_error(), and luaL_typename.

Referenced by tconcat().

◆ auxsort()

static void auxsort ( lua_State * L,
IdxT lo,
IdxT up,
unsigned int rnd )
static

Definition at line 357 of file lua-5.3.6/src/ltablib.c.

358 {
359 while (lo < up) { /* loop for tail recursion */
360 IdxT p; /* Pivot index */
361 IdxT n; /* to be used later */
362 /* sort elements 'lo', 'p', and 'up' */
363 lua_geti(L, 1, lo);
364 lua_geti(L, 1, up);
365 if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */
366 set2(L, lo, up); /* swap a[lo] - a[up] */
367 else
368 lua_pop(L, 2); /* remove both values */
369 if (up - lo == 1) /* only 2 elements? */
370 return; /* already sorted */
371 if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */
372 p = (lo + up)/2; /* middle element is a good pivot */
373 else /* for larger intervals, it is worth a random pivot */
374 p = choosePivot(lo, up, rnd);
375 lua_geti(L, 1, p);
376 lua_geti(L, 1, lo);
377 if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */
378 set2(L, p, lo); /* swap a[p] - a[lo] */
379 else {
380 lua_pop(L, 1); /* remove a[lo] */
381 lua_geti(L, 1, up);
382 if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */
383 set2(L, p, up); /* swap a[up] - a[p] */
384 else
385 lua_pop(L, 2);
386 }
387 if (up - lo == 2) /* only 3 elements? */
388 return; /* already sorted */
389 lua_geti(L, 1, p); /* get middle element (Pivot) */
390 lua_pushvalue(L, -1); /* push Pivot */
391 lua_geti(L, 1, up - 1); /* push a[up - 1] */
392 set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */
393 p = partition(L, lo, up);
394 /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */
395 if (p - lo < up - p) { /* lower interval is smaller? */
396 auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */
397 n = p - lo; /* size of smaller interval */
398 lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */
399 }
400 else {
401 auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */
402 n = up - p; /* size of smaller interval */
403 up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */
404 }
405 if ((up - lo) / 128 > n) /* partition too imbalanced? */
406 rnd = l_randomizePivot(); /* try a new randomization */
407 } /* tail call auxsort(L, lo, up, rnd) */
408}
LUA_API void lua_pushvalue(lua_State *L, int idx)
#define lua_pop(L, n)
static void auxsort(lua_State *L, IdxT lo, IdxT up, unsigned int rnd)
static IdxT partition(lua_State *L, IdxT lo, IdxT up)
unsigned int IdxT
static void set2(lua_State *L, IdxT i, IdxT j)
static unsigned int l_randomizePivot(void)
static IdxT choosePivot(IdxT lo, IdxT up, unsigned int rnd)
static int sort_comp(lua_State *L, int a, int b)
#define RANLIMIT

References auxsort(), choosePivot(), l_randomizePivot(), lua_geti(), lua_pop, lua_pushvalue(), partition(), RANLIMIT, set2(), and sort_comp().

Referenced by auxsort(), and sort().

◆ checkfield()

static int checkfield ( lua_State * L,
const char * key,
int n )
static

Definition at line 36 of file lua-5.3.6/src/ltablib.c.

36 {
37 lua_pushstring(L, key);
38 return (lua_rawget(L, -n) != LUA_TNIL);
39}
LUA_API void lua_pushstring(lua_State *L, const char *s)
LUA_API void lua_rawget(lua_State *L, int idx)
#define LUA_TNIL

References lua_pushstring(), lua_rawget(), and LUA_TNIL.

Referenced by checktab().

◆ checktab()

static void checktab ( lua_State * L,
int arg,
int what )
static

Definition at line 46 of file lua-5.3.6/src/ltablib.c.

46 {
47 if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */
48 int n = 1; /* number of elements to pop */
49 if (lua_getmetatable(L, arg) && /* must have metatable */
50 (!(what & TAB_R) || checkfield(L, "__index", ++n)) &&
51 (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) &&
52 (!(what & TAB_L) || checkfield(L, "__len", ++n))) {
53 lua_pop(L, n); /* pop metatable and tested metamethods */
54 }
55 else
56 luaL_checktype(L, arg, LUA_TTABLE); /* force an error */
57 }
58}
CURL_EXTERN int void * arg
Definition curl.h:2622
LUA_API int lua_getmetatable(lua_State *L, int objindex)
LUA_API int lua_type(lua_State *L, int idx)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
#define LUA_TTABLE
#define TAB_L
static int checkfield(lua_State *L, const char *key, int n)
#define TAB_R
#define TAB_W

References arg, checkfield(), lua_getmetatable(), lua_pop, LUA_TTABLE, lua_type(), luaL_checktype(), TAB_L, TAB_R, and TAB_W.

Referenced by tmove().

◆ choosePivot()

static IdxT choosePivot ( IdxT lo,
IdxT up,
unsigned int rnd )
static

Definition at line 346 of file lua-5.3.6/src/ltablib.c.

346 {
347 IdxT r4 = (up - lo) / 4; /* range/4 */
348 IdxT p = rnd % (r4 * 2) + (lo + r4);
349 lua_assert(lo + r4 <= p && p <= up - r4);
350 return p;
351}
#define lua_assert(c)

References lua_assert.

Referenced by auxsort().

◆ l_randomizePivot()

static unsigned int l_randomizePivot ( void )
static

Definition at line 258 of file lua-5.3.6/src/ltablib.c.

258 {
259 clock_t c = clock();
260 time_t t = time(NULL);
261 unsigned int buff[sof(c) + sof(t)];
262 unsigned int i, rnd = 0;
263 memcpy(buff, &c, sof(c) * sizeof(unsigned int));
264 memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
265 for (i = 0; i < sof(buff); i++)
266 rnd += buff[i];
267 return rnd;
268}
#define NULL
Definition gmacros.h:924
#define sof(e)

References NULL, and sof.

Referenced by auxsort().

◆ luaopen_table()

LUAMOD_API int luaopen_table ( lua_State * L)

Definition at line 441 of file lua-5.3.6/src/ltablib.c.

441 {
443#if defined(LUA_COMPAT_UNPACK)
444 /* _G.unpack = table.unpack */
445 lua_getfield(L, -1, "unpack");
446 lua_setglobal(L, "unpack");
447#endif
448 return 1;
449}
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
#define lua_setglobal(L, s)
#define luaL_newlib(L, l)
static const luaL_Reg tab_funcs[]

References lua_getfield(), lua_setglobal, luaL_newlib, and tab_funcs.

◆ pack()

static int pack ( lua_State * L)
static

Definition at line 194 of file lua-5.3.6/src/ltablib.c.

194 {
195 int i;
196 int n = lua_gettop(L); /* number of elements to pack */
197 lua_createtable(L, n, 1); /* create result table */
198 lua_insert(L, 1); /* put it at index 1 */
199 for (i = n; i >= 1; i--) /* assign elements */
200 lua_seti(L, 1, i);
201 lua_pushinteger(L, n);
202 lua_setfield(L, 1, "n"); /* t.n = number of elements */
203 return 1; /* return table */
204}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API int lua_gettop(lua_State *L)
LUA_API void lua_seti(lua_State *L, int idx, lua_Integer n)
#define lua_insert(L, idx)

References lua_createtable(), lua_gettop(), lua_insert, lua_pushinteger(), lua_setfield(), and lua_seti().

◆ partition()

static IdxT partition ( lua_State * L,
IdxT lo,
IdxT up )
static

Definition at line 310 of file lua-5.3.6/src/ltablib.c.

310 {
311 IdxT i = lo; /* will be incremented before first use */
312 IdxT j = up - 1; /* will be decremented before first use */
313 /* loop invariant: a[lo .. i] <= P <= a[j .. up] */
314 for (;;) {
315 /* next loop: repeat ++i while a[i] < P */
316 while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) {
317 if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */
318 luaL_error(L, "invalid order function for sorting");
319 lua_pop(L, 1); /* remove a[i] */
320 }
321 /* after the loop, a[i] >= P and a[lo .. i - 1] < P */
322 /* next loop: repeat --j while P < a[j] */
323 while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) {
324 if (j < i) /* j < i but a[j] > P ?? */
325 luaL_error(L, "invalid order function for sorting");
326 lua_pop(L, 1); /* remove a[j] */
327 }
328 /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */
329 if (j < i) { /* no elements out of place? */
330 /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */
331 lua_pop(L, 1); /* pop a[j] */
332 /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */
333 set2(L, up - 1, i);
334 return i;
335 }
336 /* otherwise, swap a[i] - a[j] to restore invariant and repeat */
337 set2(L, i, j);
338 }
339}

References lua_geti(), lua_pop, luaL_error(), set2(), and sort_comp().

Referenced by auxsort().

◆ set2()

static void set2 ( lua_State * L,
IdxT i,
IdxT j )
static

Definition at line 277 of file lua-5.3.6/src/ltablib.c.

277 {
278 lua_seti(L, 1, i);
279 lua_seti(L, 1, j);
280}

References lua_seti().

Referenced by auxsort(), and partition().

◆ sort()

static int sort ( lua_State * L)
static

Definition at line 411 of file lua-5.3.6/src/ltablib.c.

411 {
412 lua_Integer n = aux_getn(L, 1, TAB_RW);
413 if (n > 1) { /* non-trivial interval? */
414 luaL_argcheck(L, n < INT_MAX, 1, "array too big");
415 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */
416 luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */
417 lua_settop(L, 2); /* make sure there are two arguments */
418 auxsort(L, 1, (IdxT)n, 0);
419 }
420 return 0;
421}
LUA_API void lua_settop(lua_State *L, int idx)
#define luaL_argcheck(L, cond, numarg, extramsg)
LUA_INTEGER lua_Integer
#define LUA_TFUNCTION
#define lua_isnoneornil(L, n)
#define aux_getn(L, n, w)
#define TAB_RW

References aux_getn, auxsort(), lua_isnoneornil, lua_settop(), LUA_TFUNCTION, luaL_argcheck, luaL_checktype(), and TAB_RW.

◆ sort_comp()

static int sort_comp ( lua_State * L,
int a,
int b )
static

Definition at line 287 of file lua-5.3.6/src/ltablib.c.

287 {
288 if (lua_isnil(L, 2)) /* no function? */
289 return lua_compare(L, a, b, LUA_OPLT); /* a < b */
290 else { /* function */
291 int res;
292 lua_pushvalue(L, 2); /* push function */
293 lua_pushvalue(L, a-1); /* -1 to compensate function */
294 lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */
295 lua_call(L, 2, 1); /* call function */
296 res = lua_toboolean(L, -1); /* get result */
297 lua_pop(L, 1); /* pop result */
298 return res;
299 }
300}
LUA_API int lua_toboolean(lua_State *L, int idx)
#define lua_isnil(L, n)
LUA_API int lua_compare(lua_State *L, int index1, int index2, int op)
#define LUA_OPLT
#define lua_call(L, n, r)

References lua_call, lua_compare(), lua_isnil, LUA_OPLT, lua_pop, lua_pushvalue(), and lua_toboolean().

Referenced by auxsort(), and partition().

◆ tconcat()

static int tconcat ( lua_State * L)
static

Definition at line 169 of file lua-5.3.6/src/ltablib.c.

169 {
170 luaL_Buffer b;
171 lua_Integer last = aux_getn(L, 1, TAB_R);
172 size_t lsep;
173 const char *sep = luaL_optlstring(L, 2, "", &lsep);
174 lua_Integer i = luaL_optinteger(L, 3, 1);
175 last = luaL_optinteger(L, 4, last);
176 luaL_buffinit(L, &b);
177 for (; i < last; i++) {
178 addfield(L, &b, i);
179 luaL_addlstring(&b, sep, lsep);
180 }
181 if (i == last) /* add last value (if interval was not empty) */
182 addfield(L, &b, i);
183 luaL_pushresult(&b);
184 return 1;
185}
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
LUALIB_API const char * luaL_optlstring(lua_State *L, int narg, const char *def, size_t *len)
static void addfield(lua_State *L, luaL_Buffer *b, lua_Integer i)

References addfield(), aux_getn, luaL_addlstring(), luaL_buffinit(), luaL_optinteger(), luaL_optlstring(), luaL_pushresult(), and TAB_R.

◆ tinsert()

static int tinsert ( lua_State * L)
static

Definition at line 79 of file lua-5.3.6/src/ltablib.c.

79 {
80 lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */
81 lua_Integer pos; /* where to insert new element */
82 switch (lua_gettop(L)) {
83 case 2: { /* called with only 2 arguments */
84 pos = e; /* insert new element at the end */
85 break;
86 }
87 case 3: {
89 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */
90 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds");
91 for (i = e; i > pos; i--) { /* move up elements */
92 lua_geti(L, 1, i - 1);
93 lua_seti(L, 1, i); /* t[i] = t[i - 1] */
94 }
95 break;
96 }
97 default: {
98 return luaL_error(L, "wrong number of arguments to 'insert'");
99 }
100 }
101 lua_seti(L, 1, pos); /* t[pos] = v */
102 return 0;
103}
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)

References aux_getn, lua_geti(), lua_gettop(), lua_seti(), luaL_argcheck, luaL_checkinteger(), luaL_error(), and TAB_RW.

◆ tmove()

static int tmove ( lua_State * L)
static

Definition at line 128 of file lua-5.3.6/src/ltablib.c.

128 {
132 int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */
133 checktab(L, 1, TAB_R);
134 checktab(L, tt, TAB_W);
135 if (e >= f) { /* otherwise, nothing to move */
136 lua_Integer n, i;
137 luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3,
138 "too many elements to move");
139 n = e - f + 1; /* number of elements to move */
140 luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
141 "destination wrap around");
142 if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) {
143 for (i = 0; i < n; i++) {
144 lua_geti(L, 1, f + i);
145 lua_seti(L, tt, t + i);
146 }
147 }
148 else {
149 for (i = n - 1; i >= 0; i--) {
150 lua_geti(L, 1, f + i);
151 lua_seti(L, tt, t + i);
152 }
153 }
154 }
155 lua_pushvalue(L, tt); /* return destination table */
156 return 1;
157}
#define LUA_OPEQ
static void checktab(lua_State *L, int arg, int what)

References checktab(), lua_compare(), lua_geti(), lua_isnoneornil, LUA_OPEQ, lua_pushvalue(), lua_seti(), luaL_argcheck, luaL_checkinteger(), TAB_R, and TAB_W.

◆ tremove()

static int tremove ( lua_State * L)
static

Definition at line 106 of file lua-5.3.6/src/ltablib.c.

106 {
107 lua_Integer size = aux_getn(L, 1, TAB_RW);
108 lua_Integer pos = luaL_optinteger(L, 2, size);
109 if (pos != size) /* validate 'pos' if given */
110 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");
111 lua_geti(L, 1, pos); /* result = t[pos] */
112 for ( ; pos < size; pos++) {
113 lua_geti(L, 1, pos + 1);
114 lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */
115 }
116 lua_pushnil(L);
117 lua_seti(L, 1, pos); /* t[pos] = nil */
118 return 1;
119}
LUA_API void lua_pushnil(lua_State *L)

References aux_getn, lua_geti(), lua_pushnil(), lua_seti(), luaL_argcheck, luaL_optinteger(), and TAB_RW.

◆ unpack()

static int unpack ( lua_State * L)
static

Definition at line 207 of file lua-5.3.6/src/ltablib.c.

207 {
208 lua_Unsigned n;
209 lua_Integer i = luaL_optinteger(L, 2, 1);
211 if (i > e) return 0; /* empty range */
212 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */
213 if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n)))
214 return luaL_error(L, "too many results to unpack");
215 for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */
216 lua_geti(L, 1, i);
217 }
218 lua_geti(L, 1, e); /* push last element */
219 return (int)n;
220}
LUA_API int lua_checkstack(lua_State *L, int size)
#define luaL_opt(L, f, n, d)
LUALIB_API int luaL_len(lua_State *L, int idx)
LUA_UNSIGNED lua_Unsigned

References lua_checkstack(), lua_geti(), luaL_checkinteger(), luaL_error(), luaL_len(), luaL_opt, and luaL_optinteger().

Variable Documentation

◆ tab_funcs

const luaL_Reg tab_funcs[]
static
Initial value:
= {
{"concat", tconcat},
{"insert", tinsert},
{"pack", pack},
{"unpack", unpack},
{"remove", tremove},
{"move", tmove},
{"sort", sort},
}
static int tinsert(lua_State *L)
static int tmove(lua_State *L)
static int pack(lua_State *L)
static int unpack(lua_State *L)
static int tremove(lua_State *L)
static int tconcat(lua_State *L)
static int sort(lua_State *L)

Definition at line 426 of file lua-5.3.6/src/ltablib.c.

426 {
427 {"concat", tconcat},
428#if defined(LUA_COMPAT_MAXN)
429 {"maxn", maxn},
430#endif
431 {"insert", tinsert},
432 {"pack", pack},
433 {"unpack", unpack},
434 {"remove", tremove},
435 {"move", tmove},
436 {"sort", sort},
437 {NULL, NULL}
438};
static int maxn(lua_State *L)

Referenced by luaopen_table().