Changeset

1916:7f8928aa0b9d

Merge with 0.5
author Matthew Wild <mwild1@gmail.com>
date Mon, 05 Oct 2009 17:57:59 +0100
parents 1914:e7f6baf39b56 (current diff) 1915:e9d5406caf8c (diff)
children 1923:2d242334508d
files
diffstat 1 files changed, 13 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/util/array.lua	Mon Oct 05 15:16:34 2009 +0100
+++ b/util/array.lua	Mon Oct 05 17:57:59 2009 +0100
@@ -33,11 +33,22 @@
 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