Software /
code /
prosody
Changeset
904:0205dcd0854a
util.set: New util library
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 22 Mar 2009 12:13:39 +0000 |
parents | 903:6737d005a84a |
children | 905:6169597d5574 |
files | util/set.lua |
diffstat | 1 files changed, 75 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util/set.lua Sun Mar 22 12:13:39 2009 +0000 @@ -0,0 +1,75 @@ + +module "set" + +function new(list) + local items = {}; + local set = { items = items }; + + function set:add(set, item) + items[item] = true; + end + + function set:contains(set, item) + return items[item] + end + + function set:items(set) + return items; + end + + function set:remove(set, item) + items[item] = nil; + end + + function set:add_list(set, list) + for _, item in ipairs(list) do + items[item] = true; + end + end + + function set:include(set, otherset) + for item in pairs(otherset) do + items[item] = true; + end + end + + function set:exclude(set, otherset) + for item in pairs(otherset) do + items[item] = nil; + end + end + + return set; +end + +function union(set1, set2) + local set = new(); + local items = set.items; + + for item in pairs(set1.items) do + items[item] = true; + end + + for item in pairs(set2.items) do + items[item] = true; + end + + return set; +end + +function difference(set1, set2) + local set = new(); + local items = set.items; + + for item in pairs(set1.items) do + items[item] = true; + end + + for item in pairs(set2.items) do + items[item] = nil; + end + + return set; +end + +return _M;