aboutsummaryrefslogtreecommitdiffstats
path: root/util/array.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2009-10-05 17:56:31 +0100
committerMatthew Wild <mwild1@gmail.com>2009-10-05 17:56:31 +0100
commit04d29dacbcbbe9e44321cf88ea3098342cfbe293 (patch)
treec30d95614ad4a07de27411af45b44e7b4a5bd4ed /util/array.lua
parent0bb9aa879725848464b56a956a4384ebcf166866 (diff)
downloadprosody-04d29dacbcbbe9e44321cf88ea3098342cfbe293.tar.gz
prosody-04d29dacbcbbe9e44321cf88ea3098342cfbe293.zip
util.array: Fix for array:filter() (in-place filtering)
Diffstat (limited to 'util/array.lua')
-rw-r--r--util/array.lua15
1 files changed, 13 insertions, 2 deletions
diff --git a/util/array.lua b/util/array.lua
index ae3a0b5b..bbf26f32 100644
--- a/util/array.lua
+++ b/util/array.lua
@@ -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