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