X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=util%2Farray.lua;h=6c1f04606642bc5cdb700f36e289a0aa655d319e;hb=492c253d150aeb7edb6687eb9bf085be6c33133a;hp=ae3a0b5bdf178f247356e093eef15c512a56f64b;hpb=4ac119e7af89cd0003f32427b69314bec147ecb6;p=prosody.git diff --git a/util/array.lua b/util/array.lua index ae3a0b5b..6c1f0460 100644 --- a/util/array.lua +++ b/util/array.lua @@ -1,13 +1,13 @@ -- Prosody IM --- Copyright (C) 2008-2009 Matthew Wild --- Copyright (C) 2008-2009 Waqas Hussain +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain -- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. -- -local t_insert, t_sort, t_remove, t_concat - = table.insert, table.sort, table.remove, table.concat; +local t_insert, t_sort, t_remove, t_concat + = table.insert, table.sort, table.remove, table.concat; local array = {}; local array_base = {}; @@ -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 @@ -100,7 +111,6 @@ end -- Setup methods from array_base for method, f in pairs(array_base) do - local method = method; -- Yes, this is necessary :) local base_method = f; -- Setup global array method which makes new array array[method] = function (old_a, ...)