MultiTable: Remove all empty sub-tables when elements are removed

Fri, 05 Dec 2008 05:24:10 +0500

author
Waqas Hussain <waqas20@gmail.com>
date
Fri, 05 Dec 2008 05:24:10 +0500
changeset 554
9a695724681d
parent 552
6db1f41e475a
child 555
1c29c0b22022

MultiTable: Remove all empty sub-tables when elements are removed

util/multitable.lua file | annotate | diff | comparison | revisions
--- a/util/multitable.lua	Fri Dec 05 04:56:04 2008 +0500
+++ b/util/multitable.lua	Fri Dec 05 05:24:10 2008 +0500
@@ -22,6 +22,7 @@
 local select = select;
 local t_insert = table.insert;
 local pairs = pairs;
+local next = next;
 
 module "multitable"
 
@@ -55,10 +56,19 @@
 	end
 	local k = select(n, ...);
 	if k then
-		r(t[k], n+1, _end, ...);
+		v = t[k];
+		if v then
+			r(v, n+1, _end, ...);
+			if not next(v) then
+				t[k] = nil;
+			end
+		end
 	else
 		for _,b in pairs(t) do
 			r(b, n+1, _end, ...);
+			if not next(b) then
+				t[_] = nil;
+			end
 		end
 	end
 end

mercurial