Redo merge with Waqas' PBKDF2 optimizations.
[prosody.git] / util / array.lua
index ae3a0b5bdf178f247356e093eef15c512a56f64b..686f55b1ef2f0b855039dc3cf89630b4388a708e 100644 (file)
@@ -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, ...)