aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2009-03-22 12:13:39 +0000
committerMatthew Wild <mwild1@gmail.com>2009-03-22 12:13:39 +0000
commit7f87706ee40a05db4e8572313d68ca8911f1415a (patch)
treeec02ea16f9c1ef3d8df72e929eea577a1a080258
parent51fd2073f805a4ca9dfd7de68c0a722c33094780 (diff)
downloadprosody-7f87706ee40a05db4e8572313d68ca8911f1415a.tar.gz
prosody-7f87706ee40a05db4e8572313d68ca8911f1415a.zip
util.set: New util library
-rw-r--r--util/set.lua75
1 files changed, 75 insertions, 0 deletions
diff --git a/util/set.lua b/util/set.lua
new file mode 100644
index 00000000..08361d03
--- /dev/null
+++ b/util/set.lua
@@ -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;