diff options
author | Philipp Janda <siffiejoe@gmx.net> | 2017-08-27 15:50:20 +0200 |
---|---|---|
committer | Philipp Janda <siffiejoe@gmx.net> | 2017-08-27 15:50:20 +0200 |
commit | 818e129043c73930aca0d223db62837ee921bb2a (patch) | |
tree | 1978e1ff1d971dc6d925920588bfb6a7859953a9 /ltablib.c | |
parent | aeb072066f799c87527c81b19919dfd3b55bc373 (diff) | |
download | lua-compat-5.3-818e129043c73930aca0d223db62837ee921bb2a.tar.gz lua-compat-5.3-818e129043c73930aca0d223db62837ee921bb2a.tar.bz2 lua-compat-5.3-818e129043c73930aca0d223db62837ee921bb2a.zip |
Update backports to Lua 5.3.4.
Diffstat (limited to 'ltablib.c')
-rw-r--r-- | ltablib.c | 41 |
1 files changed, 21 insertions, 20 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltablib.c,v 1.90 2015/11/25 12:48:57 roberto Exp $ | 2 | ** $Id: ltablib.c,v 1.93 2016/02/25 19:41:54 roberto Exp $ |
3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -53,7 +53,7 @@ static void checktab (lua_State *L, int arg, int what) { | |||
53 | lua_pop(L, n); /* pop metatable and tested metamethods */ | 53 | lua_pop(L, n); /* pop metatable and tested metamethods */ |
54 | } | 54 | } |
55 | else | 55 | else |
56 | luaL_argerror(L, arg, "table expected"); /* force an error */ | 56 | luaL_checktype(L, arg, LUA_TTABLE); /* force an error */ |
57 | } | 57 | } |
58 | } | 58 | } |
59 | 59 | ||
@@ -139,7 +139,7 @@ static int tmove (lua_State *L) { | |||
139 | n = e - f + 1; /* number of elements to move */ | 139 | n = e - f + 1; /* number of elements to move */ |
140 | luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, | 140 | luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, |
141 | "destination wrap around"); | 141 | "destination wrap around"); |
142 | if (t > e || t <= f || tt != 1) { | 142 | if (t > e || t <= f || (tt != 1 && !lua_compare(L, 1, tt, LUA_OPEQ))) { |
143 | for (i = 0; i < n; i++) { | 143 | for (i = 0; i < n; i++) { |
144 | lua_geti(L, 1, f + i); | 144 | lua_geti(L, 1, f + i); |
145 | lua_seti(L, tt, t + i); | 145 | lua_seti(L, tt, t + i); |
@@ -152,7 +152,7 @@ static int tmove (lua_State *L) { | |||
152 | } | 152 | } |
153 | } | 153 | } |
154 | } | 154 | } |
155 | lua_pushvalue(L, tt); /* return "to table" */ | 155 | lua_pushvalue(L, tt); /* return destination table */ |
156 | return 1; | 156 | return 1; |
157 | } | 157 | } |
158 | 158 | ||
@@ -172,7 +172,7 @@ static int tconcat (lua_State *L) { | |||
172 | size_t lsep; | 172 | size_t lsep; |
173 | const char *sep = luaL_optlstring(L, 2, "", &lsep); | 173 | const char *sep = luaL_optlstring(L, 2, "", &lsep); |
174 | lua_Integer i = luaL_optinteger(L, 3, 1); | 174 | lua_Integer i = luaL_optinteger(L, 3, 1); |
175 | last = luaL_opt(L, luaL_checkinteger, 4, last); | 175 | last = luaL_optinteger(L, 4, last); |
176 | luaL_buffinit(L, &b); | 176 | luaL_buffinit(L, &b); |
177 | for (; i < last; i++) { | 177 | for (; i < last; i++) { |
178 | addfield(L, &b, i); | 178 | addfield(L, &b, i); |
@@ -232,6 +232,10 @@ static int unpack (lua_State *L) { | |||
232 | */ | 232 | */ |
233 | 233 | ||
234 | 234 | ||
235 | /* type for array indices */ | ||
236 | typedef unsigned int IdxT; | ||
237 | |||
238 | |||
235 | /* | 239 | /* |
236 | ** Produce a "random" 'unsigned int' to randomize pivot choice. This | 240 | ** Produce a "random" 'unsigned int' to randomize pivot choice. This |
237 | ** macro is used only when 'sort' detects a big imbalance in the result | 241 | ** macro is used only when 'sort' detects a big imbalance in the result |
@@ -270,7 +274,7 @@ static unsigned int l_randomizePivot (void) { | |||
270 | #define RANLIMIT 100u | 274 | #define RANLIMIT 100u |
271 | 275 | ||
272 | 276 | ||
273 | static void set2 (lua_State *L, unsigned int i, unsigned int j) { | 277 | static void set2 (lua_State *L, IdxT i, IdxT j) { |
274 | lua_seti(L, 1, i); | 278 | lua_seti(L, 1, i); |
275 | lua_seti(L, 1, j); | 279 | lua_seti(L, 1, j); |
276 | } | 280 | } |
@@ -303,10 +307,9 @@ static int sort_comp (lua_State *L, int a, int b) { | |||
303 | ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] | 307 | ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] |
304 | ** returns 'i'. | 308 | ** returns 'i'. |
305 | */ | 309 | */ |
306 | static unsigned int partition (lua_State *L, unsigned int lo, | 310 | static IdxT partition (lua_State *L, IdxT lo, IdxT up) { |
307 | unsigned int up) { | 311 | IdxT i = lo; /* will be incremented before first use */ |
308 | unsigned int i = lo; /* will be incremented before first use */ | 312 | IdxT j = up - 1; /* will be decremented before first use */ |
309 | unsigned int j = up - 1; /* will be decremented before first use */ | ||
310 | /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ | 313 | /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ |
311 | for (;;) { | 314 | for (;;) { |
312 | /* next loop: repeat ++i while a[i] < P */ | 315 | /* next loop: repeat ++i while a[i] < P */ |
@@ -340,10 +343,9 @@ static unsigned int partition (lua_State *L, unsigned int lo, | |||
340 | ** Choose an element in the middle (2nd-3th quarters) of [lo,up] | 343 | ** Choose an element in the middle (2nd-3th quarters) of [lo,up] |
341 | ** "randomized" by 'rnd' | 344 | ** "randomized" by 'rnd' |
342 | */ | 345 | */ |
343 | static unsigned int choosePivot (unsigned int lo, unsigned int up, | 346 | static IdxT choosePivot (IdxT lo, IdxT up, unsigned int rnd) { |
344 | unsigned int rnd) { | 347 | IdxT r4 = (up - lo) / 4; /* range/4 */ |
345 | unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ | 348 | IdxT p = rnd % (r4 * 2) + (lo + r4); |
346 | unsigned int p = rnd % (r4 * 2) + (lo + r4); | ||
347 | lua_assert(lo + r4 <= p && p <= up - r4); | 349 | lua_assert(lo + r4 <= p && p <= up - r4); |
348 | return p; | 350 | return p; |
349 | } | 351 | } |
@@ -352,11 +354,11 @@ static unsigned int choosePivot (unsigned int lo, unsigned int up, | |||
352 | /* | 354 | /* |
353 | ** QuickSort algorithm (recursive function) | 355 | ** QuickSort algorithm (recursive function) |
354 | */ | 356 | */ |
355 | static void auxsort (lua_State *L, unsigned int lo, unsigned int up, | 357 | static void auxsort (lua_State *L, IdxT lo, IdxT up, |
356 | unsigned int rnd) { | 358 | unsigned int rnd) { |
357 | while (lo < up) { /* loop for tail recursion */ | 359 | while (lo < up) { /* loop for tail recursion */ |
358 | unsigned int p; /* Pivot index */ | 360 | IdxT p; /* Pivot index */ |
359 | unsigned int n; /* to be used later */ | 361 | IdxT n; /* to be used later */ |
360 | /* sort elements 'lo', 'p', and 'up' */ | 362 | /* sort elements 'lo', 'p', and 'up' */ |
361 | lua_geti(L, 1, lo); | 363 | lua_geti(L, 1, lo); |
362 | lua_geti(L, 1, up); | 364 | lua_geti(L, 1, up); |
@@ -400,7 +402,7 @@ static void auxsort (lua_State *L, unsigned int lo, unsigned int up, | |||
400 | n = up - p; /* size of smaller interval */ | 402 | n = up - p; /* size of smaller interval */ |
401 | up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ | 403 | up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ |
402 | } | 404 | } |
403 | if ((up - lo) / 128u > n) /* partition too imbalanced? */ | 405 | if ((up - lo) / 128 > n) /* partition too imbalanced? */ |
404 | rnd = l_randomizePivot(); /* try a new randomization */ | 406 | rnd = l_randomizePivot(); /* try a new randomization */ |
405 | } /* tail call auxsort(L, lo, up, rnd) */ | 407 | } /* tail call auxsort(L, lo, up, rnd) */ |
406 | } | 408 | } |
@@ -410,11 +412,10 @@ static int sort (lua_State *L) { | |||
410 | lua_Integer n = aux_getn(L, 1, TAB_RW); | 412 | lua_Integer n = aux_getn(L, 1, TAB_RW); |
411 | if (n > 1) { /* non-trivial interval? */ | 413 | if (n > 1) { /* non-trivial interval? */ |
412 | luaL_argcheck(L, n < INT_MAX, 1, "array too big"); | 414 | luaL_argcheck(L, n < INT_MAX, 1, "array too big"); |
413 | luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ | ||
414 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ | 415 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ |
415 | luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ | 416 | luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ |
416 | lua_settop(L, 2); /* make sure there are two arguments */ | 417 | lua_settop(L, 2); /* make sure there are two arguments */ |
417 | auxsort(L, 1, (unsigned int)n, 0u); | 418 | auxsort(L, 1, (IdxT)n, 0); |
418 | } | 419 | } |
419 | return 0; | 420 | return 0; |
420 | } | 421 | } |