diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-07-27 15:13:21 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-07-27 15:13:21 -0300 |
commit | f2206b2abe848f65956fa48da338c2bfac599e4a (patch) | |
tree | 94c1a856ec06846ad7485648ccafb429b5ca1b9b /ltablib.c | |
parent | 0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b (diff) | |
download | lua-f2206b2abe848f65956fa48da338c2bfac599e4a.tar.gz lua-f2206b2abe848f65956fa48da338c2bfac599e4a.tar.bz2 lua-f2206b2abe848f65956fa48da338c2bfac599e4a.zip |
'-Wconversion' extended to all options of Lua numbers
Diffstat (limited to 'ltablib.c')
-rw-r--r-- | ltablib.c | 32 |
1 files changed, 20 insertions, 12 deletions
@@ -231,10 +231,18 @@ static int tunpack (lua_State *L) { | |||
231 | */ | 231 | */ |
232 | 232 | ||
233 | 233 | ||
234 | /* type for array indices */ | 234 | /* |
235 | ** Type for array indices. These indices are always limited by INT_MAX, | ||
236 | ** so it is safe to cast them to lua_Integer even for Lua 32 bits. | ||
237 | */ | ||
235 | typedef unsigned int IdxT; | 238 | typedef unsigned int IdxT; |
236 | 239 | ||
237 | 240 | ||
241 | /* Versions of lua_seti/lua_geti specialized for IdxT */ | ||
242 | #define geti(L,idt,idx) lua_geti(L, idt, l_castU2S(idx)) | ||
243 | #define seti(L,idt,idx) lua_seti(L, idt, l_castU2S(idx)) | ||
244 | |||
245 | |||
238 | /* | 246 | /* |
239 | ** Produce a "random" 'unsigned int' to randomize pivot choice. This | 247 | ** Produce a "random" 'unsigned int' to randomize pivot choice. This |
240 | ** macro is used only when 'sort' detects a big imbalance in the result | 248 | ** macro is used only when 'sort' detects a big imbalance in the result |
@@ -251,8 +259,8 @@ typedef unsigned int IdxT; | |||
251 | 259 | ||
252 | 260 | ||
253 | static void set2 (lua_State *L, IdxT i, IdxT j) { | 261 | static void set2 (lua_State *L, IdxT i, IdxT j) { |
254 | lua_seti(L, 1, i); | 262 | seti(L, 1, i); |
255 | lua_seti(L, 1, j); | 263 | seti(L, 1, j); |
256 | } | 264 | } |
257 | 265 | ||
258 | 266 | ||
@@ -289,14 +297,14 @@ static IdxT partition (lua_State *L, IdxT lo, IdxT up) { | |||
289 | /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ | 297 | /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ |
290 | for (;;) { | 298 | for (;;) { |
291 | /* next loop: repeat ++i while a[i] < P */ | 299 | /* next loop: repeat ++i while a[i] < P */ |
292 | while ((void)lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { | 300 | while ((void)geti(L, 1, ++i), sort_comp(L, -1, -2)) { |
293 | if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ | 301 | if (l_unlikely(i == up - 1)) /* a[i] < P but a[up - 1] == P ?? */ |
294 | luaL_error(L, "invalid order function for sorting"); | 302 | luaL_error(L, "invalid order function for sorting"); |
295 | lua_pop(L, 1); /* remove a[i] */ | 303 | lua_pop(L, 1); /* remove a[i] */ |
296 | } | 304 | } |
297 | /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ | 305 | /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ |
298 | /* next loop: repeat --j while P < a[j] */ | 306 | /* next loop: repeat --j while P < a[j] */ |
299 | while ((void)lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { | 307 | while ((void)geti(L, 1, --j), sort_comp(L, -3, -1)) { |
300 | if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ | 308 | if (l_unlikely(j < i)) /* j < i but a[j] > P ?? */ |
301 | luaL_error(L, "invalid order function for sorting"); | 309 | luaL_error(L, "invalid order function for sorting"); |
302 | lua_pop(L, 1); /* remove a[j] */ | 310 | lua_pop(L, 1); /* remove a[j] */ |
@@ -335,8 +343,8 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { | |||
335 | IdxT p; /* Pivot index */ | 343 | IdxT p; /* Pivot index */ |
336 | IdxT n; /* to be used later */ | 344 | IdxT n; /* to be used later */ |
337 | /* sort elements 'lo', 'p', and 'up' */ | 345 | /* sort elements 'lo', 'p', and 'up' */ |
338 | lua_geti(L, 1, lo); | 346 | geti(L, 1, lo); |
339 | lua_geti(L, 1, up); | 347 | geti(L, 1, up); |
340 | if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ | 348 | if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ |
341 | set2(L, lo, up); /* swap a[lo] - a[up] */ | 349 | set2(L, lo, up); /* swap a[lo] - a[up] */ |
342 | else | 350 | else |
@@ -347,13 +355,13 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { | |||
347 | p = (lo + up)/2; /* middle element is a good pivot */ | 355 | p = (lo + up)/2; /* middle element is a good pivot */ |
348 | else /* for larger intervals, it is worth a random pivot */ | 356 | else /* for larger intervals, it is worth a random pivot */ |
349 | p = choosePivot(lo, up, rnd); | 357 | p = choosePivot(lo, up, rnd); |
350 | lua_geti(L, 1, p); | 358 | geti(L, 1, p); |
351 | lua_geti(L, 1, lo); | 359 | geti(L, 1, lo); |
352 | if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ | 360 | if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ |
353 | set2(L, p, lo); /* swap a[p] - a[lo] */ | 361 | set2(L, p, lo); /* swap a[p] - a[lo] */ |
354 | else { | 362 | else { |
355 | lua_pop(L, 1); /* remove a[lo] */ | 363 | lua_pop(L, 1); /* remove a[lo] */ |
356 | lua_geti(L, 1, up); | 364 | geti(L, 1, up); |
357 | if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ | 365 | if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ |
358 | set2(L, p, up); /* swap a[up] - a[p] */ | 366 | set2(L, p, up); /* swap a[up] - a[p] */ |
359 | else | 367 | else |
@@ -361,9 +369,9 @@ static void auxsort (lua_State *L, IdxT lo, IdxT up, unsigned rnd) { | |||
361 | } | 369 | } |
362 | if (up - lo == 2) /* only 3 elements? */ | 370 | if (up - lo == 2) /* only 3 elements? */ |
363 | return; /* already sorted */ | 371 | return; /* already sorted */ |
364 | lua_geti(L, 1, p); /* get middle element (Pivot) */ | 372 | geti(L, 1, p); /* get middle element (Pivot) */ |
365 | lua_pushvalue(L, -1); /* push Pivot */ | 373 | lua_pushvalue(L, -1); /* push Pivot */ |
366 | lua_geti(L, 1, up - 1); /* push a[up - 1] */ | 374 | geti(L, 1, up - 1); /* push a[up - 1] */ |
367 | set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ | 375 | set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ |
368 | p = partition(L, lo, up); | 376 | p = partition(L, lo, up); |
369 | /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ | 377 | /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ |