'table.move' tries to copy elements in increasing order

whenever possible
This commit is contained in:
Roberto Ierusalimschy 2015-09-17 12:53:50 -03:00
parent ee5edb6b68
commit 0f1f51be4b
1 changed files with 10 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: ltablib.c,v 1.81 2015/07/04 16:31:42 roberto Exp roberto $
** $Id: ltablib.c,v 1.82 2015/09/09 15:42:30 roberto Exp roberto $
** Library for Table Manipulation
** See Copyright Notice in lua.h
*/
@ -118,6 +118,12 @@ static int tremove (lua_State *L) {
}
/*
** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
** possible, copy in increasing order, which is better for rehashing.
** "possible" means destination after original range, or smaller
** than origin, or copying to another table.
*/
static int tmove (lua_State *L) {
lua_Integer f = luaL_checkinteger(L, 2);
lua_Integer e = luaL_checkinteger(L, 3);
@ -132,14 +138,14 @@ static int tmove (lua_State *L) {
n = e - f + 1; /* number of elements to move */
luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
"destination wrap around");
if (t > f) {
for (i = n - 1; i >= 0; i--) {
if (t > e || t <= f || tt != 1) {
for (i = 0; i < n; i++) {
lua_geti(L, 1, f + i);
lua_seti(L, tt, t + i);
}
}
else {
for (i = 0; i < n; i++) {
for (i = n - 1; i >= 0; i--) {
lua_geti(L, 1, f + i);
lua_seti(L, tt, t + i);
}