Comparison

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
comparison
equal deleted inserted replaced
12402:8deb401a21f6 12403:42b2713ab818
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local t_insert, t_sort, t_remove, t_concat 9 local t_insert, t_sort, t_remove, t_concat
10 = table.insert, table.sort, table.remove, table.concat; 10 = table.insert, table.sort, table.remove, table.concat;
11 local t_move = require "util.table".move;
11 12
12 local setmetatable = setmetatable; 13 local setmetatable = setmetatable;
13 local getmetatable = getmetatable; 14 local getmetatable = getmetatable;
14 local math_random = math.random; 15 local math_random = math.random;
15 local math_floor = math.floor; 16 local math_floor = math.floor;
135 outa[idx] = nil; 136 outa[idx] = nil;
136 end 137 end
137 return outa; 138 return outa;
138 end 139 end
139 140
140 for idx = 1, 1+j-i do 141
141 outa[idx] = ina[i+(idx-1)]; 142 t_move(ina, i, j, 1, outa);
142 end
143 if ina == outa then 143 if ina == outa then
144 for idx = 2+j-i, #outa do 144 -- Clear (nil) remainder of range
145 outa[idx] = nil; 145 t_move(ina, #outa+1, #outa*2, 2+j-i, ina);
146 end
147 end 146 end
148 return outa; 147 return outa;
149 end 148 end
150 149
151 function array_base.sort(outa, ina, ...) 150 function array_base.sort(outa, ina, ...)
207 end 206 end
208 return self; 207 return self;
209 end 208 end
210 209
211 function array_methods:append(ina) 210 function array_methods:append(ina)
212 local len, len2 = #self, #ina; 211 t_move(ina, 1, #ina, #self+1, self);
213 for i = 1, len2 do
214 self[len+i] = ina[i];
215 end
216 return self; 212 return self;
217 end 213 end
218 214
219 function array_methods:push(x) 215 function array_methods:push(x)
220 t_insert(self, x); 216 t_insert(self, x);