Software /
code /
prosody
Comparison
util/set.lua @ 904:0205dcd0854a
util.set: New util library
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 22 Mar 2009 12:13:39 +0000 |
child | 905:6169597d5574 |
comparison
equal
deleted
inserted
replaced
903:6737d005a84a | 904:0205dcd0854a |
---|---|
1 | |
2 module "set" | |
3 | |
4 function new(list) | |
5 local items = {}; | |
6 local set = { items = items }; | |
7 | |
8 function set:add(set, item) | |
9 items[item] = true; | |
10 end | |
11 | |
12 function set:contains(set, item) | |
13 return items[item] | |
14 end | |
15 | |
16 function set:items(set) | |
17 return items; | |
18 end | |
19 | |
20 function set:remove(set, item) | |
21 items[item] = nil; | |
22 end | |
23 | |
24 function set:add_list(set, list) | |
25 for _, item in ipairs(list) do | |
26 items[item] = true; | |
27 end | |
28 end | |
29 | |
30 function set:include(set, otherset) | |
31 for item in pairs(otherset) do | |
32 items[item] = true; | |
33 end | |
34 end | |
35 | |
36 function set:exclude(set, otherset) | |
37 for item in pairs(otherset) do | |
38 items[item] = nil; | |
39 end | |
40 end | |
41 | |
42 return set; | |
43 end | |
44 | |
45 function union(set1, set2) | |
46 local set = new(); | |
47 local items = set.items; | |
48 | |
49 for item in pairs(set1.items) do | |
50 items[item] = true; | |
51 end | |
52 | |
53 for item in pairs(set2.items) do | |
54 items[item] = true; | |
55 end | |
56 | |
57 return set; | |
58 end | |
59 | |
60 function difference(set1, set2) | |
61 local set = new(); | |
62 local items = set.items; | |
63 | |
64 for item in pairs(set1.items) do | |
65 items[item] = true; | |
66 end | |
67 | |
68 for item in pairs(set2.items) do | |
69 items[item] = nil; | |
70 end | |
71 | |
72 return set; | |
73 end | |
74 | |
75 return _M; |