diff options
author | Matthew Wild <mwild1@gmail.com> | 2009-03-23 01:49:22 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2009-03-23 01:49:22 +0000 |
commit | db2a808e1412fa6f25b2a16a84cc5e250ca9da03 (patch) | |
tree | 1235e282e1e8a38e9bb82bc97521873fe3606bd6 /util/set.lua | |
parent | 000fa4bb34a65ae928c18be727d27b00facb5a35 (diff) | |
download | prosody-db2a808e1412fa6f25b2a16a84cc5e250ca9da03.tar.gz prosody-db2a808e1412fa6f25b2a16a84cc5e250ca9da03.zip |
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Diffstat (limited to 'util/set.lua')
-rw-r--r-- | util/set.lua | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/util/set.lua b/util/set.lua index 67f3dcaa..f550a721 100644 --- a/util/set.lua +++ b/util/set.lua @@ -5,7 +5,7 @@ module "set" function new(list) local items = {}; - local set = { items = items }; + local set = { _items = items }; function set:add(item) items[item] = true; @@ -50,13 +50,13 @@ end 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 @@ end 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; |