Diff

util/array.lua @ 12403:42b2713ab818

util.array: Take advantage of table.move()
author Matthew Wild <mwild1@gmail.com>
date Fri, 18 Mar 2022 15:22:00 +0000
parent 11787:3ae6fa901a8b
child 12975:d10957394a3c
line wrap: on
line diff
--- a/util/array.lua	Fri Mar 18 15:21:25 2022 +0000
+++ b/util/array.lua	Fri Mar 18 15:22:00 2022 +0000
@@ -8,6 +8,7 @@
 
 local t_insert, t_sort, t_remove, t_concat
     = table.insert, table.sort, table.remove, table.concat;
+local t_move = require "util.table".move;
 
 local setmetatable = setmetatable;
 local getmetatable = getmetatable;
@@ -137,13 +138,11 @@
 		return outa;
 	end
 
-	for idx = 1, 1+j-i do
-		outa[idx] = ina[i+(idx-1)];
-	end
+
+	t_move(ina, i, j, 1, outa);
 	if ina == outa then
-		for idx = 2+j-i, #outa do
-			outa[idx] = nil;
-		end
+		-- Clear (nil) remainder of range
+		t_move(ina, #outa+1, #outa*2, 2+j-i, ina);
 	end
 	return outa;
 end
@@ -209,10 +208,7 @@
 end
 
 function array_methods:append(ina)
-	local len, len2 = #self, #ina;
-	for i = 1, len2 do
-		self[len+i] = ina[i];
-	end
+	t_move(ina, 1, #ina, #self+1, self);
 	return self;
 end