mirror of
https://github.com/bjc/prosody.git
synced 2025-04-04 13:47:41 +03:00
util.array: Fix for array:filter() (in-place filtering)
This commit is contained in:
parent
0bb9aa8797
commit
04d29dacbc
1 changed files with 13 additions and 2 deletions
|
@ -33,11 +33,22 @@ function array_base.map(outa, ina, func)
|
|||
end
|
||||
|
||||
function array_base.filter(outa, ina, func)
|
||||
for k,v in ipairs(ina) do
|
||||
local inplace, start_length = ina == outa, #ina;
|
||||
local write = 1;
|
||||
for read=1,start_length do
|
||||
local v = ina[read];
|
||||
if func(v) then
|
||||
outa:push(v);
|
||||
outa[write] = v;
|
||||
write = write + 1;
|
||||
end
|
||||
end
|
||||
|
||||
if inplace and write < start_length then
|
||||
for i=write,start_length do
|
||||
outa[i] = nil;
|
||||
end
|
||||
end
|
||||
|
||||
return outa;
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue