Przeglądaj źródła

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

Roberto Ierusalimschy 10 lat temu
rodzic
commit
0f1f51be4b
1 zmienionych plików z 10 dodań i 4 usunięć
  1. 10 4
      ltablib.c

+ 10 - 4
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
 ** Library for Table Manipulation
 ** See Copyright Notice in lua.h
 ** 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) {
 static int tmove (lua_State *L) {
   lua_Integer f = luaL_checkinteger(L, 2);
   lua_Integer f = luaL_checkinteger(L, 2);
   lua_Integer e = luaL_checkinteger(L, 3);
   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 */
     n = e - f + 1;  /* number of elements to move */
     luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
     luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4,
                   "destination wrap around");
                   "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_geti(L, 1, f + i);
         lua_seti(L, tt, t + i);
         lua_seti(L, tt, t + i);
       }
       }
     }
     }
     else {
     else {
-      for (i = 0; i < n; i++) {
+      for (i = n - 1; i >= 0; i--) {
         lua_geti(L, 1, f + i);
         lua_geti(L, 1, f + i);
         lua_seti(L, tt, t + i);
         lua_seti(L, tt, t + i);
       }
       }