Changeset

917:f12f88b3d4a1

util.set: Rename private items container, optimise set.difference() and add set.intersection()
author Matthew Wild <mwild1@gmail.com>
date Mon, 23 Mar 2009 01:49:22 +0000
parents 916:f0743928ef7e
children 918:967edf874df7
files util/set.lua
diffstat 1 files changed, 18 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/util/set.lua	Mon Mar 23 00:31:29 2009 +0000
+++ b/util/set.lua	Mon Mar 23 01:49:22 2009 +0000
@@ -5,7 +5,7 @@
 
 function new(list)
 	local items = {};
-	local set = { items = items };
+	local set = { _items = items };
 	
 	function set:add(item)
 		items[item] = true;
@@ -50,13 +50,13 @@
 
 function union(set1, set2)
 	local set = new();
-	local items = set.items;
+	local items = set._items;
 	
-	for item in pairs(set1.items) do
+	for item in pairs(set1._items) do
 		items[item] = true;
 	end
 
-	for item in pairs(set2.items) do
+	for item in pairs(set2._items) do
 		items[item] = true;
 	end
 	
@@ -65,14 +65,23 @@
 
 function difference(set1, set2)
 	local set = new();
-	local items = set.items;
+	local items = set._items;
 	
-	for item in pairs(set1.items) do
-		items[item] = true;
+	for item in pairs(set1._items) do
+		items[item] = (not set2._items[item]) or nil;
 	end
 
-	for item in pairs(set2.items) do
-		items[item] = nil;
+	return set;
+end
+
+function intersection(set1, set2)
+	local set = new();
+	local items = set._items;
+	
+	set1, set2 = set1._items, set2._items;
+	
+	for item in pairs(set1) do
+		items[item] = (not not set2[item]) or nil;
 	end
 	
 	return set;