aboutsummaryrefslogtreecommitdiff
path: root/ltablib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-09-17 12:53:50 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2015-09-17 12:53:50 -0300
commit0f1f51be4b1e2fb561b2ec6dce7fe292d5b3764d (patch)
tree038a610031621c764565079ef2274153ccd2a560 /ltablib.c
parentee5edb6b680589805c8fc348f6a7566c2bd8735d (diff)
downloadlua-0f1f51be4b1e2fb561b2ec6dce7fe292d5b3764d.tar.gz
lua-0f1f51be4b1e2fb561b2ec6dce7fe292d5b3764d.tar.bz2
lua-0f1f51be4b1e2fb561b2ec6dce7fe292d5b3764d.zip
'table.move' tries to copy elements in increasing order
whenever possible
Diffstat (limited to 'ltablib.c')
-rw-r--r--ltablib.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/ltablib.c b/ltablib.c
index a63b5351..10a4a3d2 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltablib.c,v 1.81 2015/07/04 16:31:42 roberto Exp roberto $ 2** $Id: ltablib.c,v 1.82 2015/09/09 15:42:30 roberto Exp roberto $
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*/
@@ -118,6 +118,12 @@ static int tremove (lua_State *L) {
118} 118}
119 119
120 120
121/*
122** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever
123** possible, copy in increasing order, which is better for rehashing.
124** "possible" means destination after original range, or smaller
125** than origin, or copying to another table.
126*/
121static int tmove (lua_State *L) { 127static int tmove (lua_State *L) {
122 lua_Integer f = luaL_checkinteger(L, 2); 128 lua_Integer f = luaL_checkinteger(L, 2);
123 lua_Integer e = luaL_checkinteger(L, 3); 129 lua_Integer e = luaL_checkinteger(L, 3);
@@ -132,14 +138,14 @@ static int tmove (lua_State *L) {
132 n = e - f + 1; /* number of elements to move */ 138 n = e - f + 1; /* number of elements to move */
133 luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, 139 luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
134 "destination wrap around"); 140 "destination wrap around");
135 if (t > f) { 141 if (t > e || t <= f || tt != 1) {
136 for (i = n - 1; i >= 0; i--) { 142 for (i = 0; i < n; i++) {
137 lua_geti(L, 1, f + i); 143 lua_geti(L, 1, f + i);
138 lua_seti(L, tt, t + i); 144 lua_seti(L, tt, t + i);
139 } 145 }
140 } 146 }
141 else { 147 else {
142 for (i = 0; i < n; i++) { 148 for (i = n - 1; i >= 0; i--) {
143 lua_geti(L, 1, f + i); 149 lua_geti(L, 1, f + i);
144 lua_seti(L, tt, t + i); 150 lua_seti(L, tt, t + i);
145 } 151 }