lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 0f1f51be4b1e2fb561b2ec6dce7fe292d5b3764d
parent ee5edb6b680589805c8fc348f6a7566c2bd8735d
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Thu, 17 Sep 2015 12:53:24 -0300

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

Diffstat:
Mltablib.c | 14++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/ltablib.c b/ltablib.c @@ -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); }