Software /
code /
prosody
Annotate
util/set.lua @ 1452:0ae8787e4e7d
Merged with trunk
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Thu, 02 Jul 2009 00:47:21 +0500 |
parent | 1030:a82268d507fc |
child | 1522:569d58d21612 |
rev | line source |
---|---|
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
1 local ipairs, pairs, setmetatable, next, tostring = |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
2 ipairs, pairs, setmetatable, next, tostring; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
3 local t_concat = table.concat; |
904 | 4 |
5 module "set" | |
6 | |
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
7 local set_mt = {}; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
8 function set_mt.__call(set, _, k) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
9 return next(set._items, k); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
10 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
11 function set_mt.__add(set1, set2) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
12 return _M.union(set1, set2); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
13 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
14 function set_mt.__sub(set1, set2) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
15 return _M.difference(set1, set2); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
16 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
17 function set_mt.__div(set, func) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
18 local new_set, new_items = _M.new(); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
19 local items, new_items = set._items, new_set._items; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
20 for item in pairs(items) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
21 if func(item) then |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
22 new_items[item] = true; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
23 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
24 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
25 return new_set; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
26 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
27 function set_mt.__eq(set1, set2) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
28 local set1, set2 = set1._items, set2._items; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
29 for item in pairs(set1) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
30 if not set2[item] then |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
31 return false; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
32 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
33 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
34 |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
35 for item in pairs(set2) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
36 if not set1[item] then |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
37 return false; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
38 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
39 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
40 |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
41 return true; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
42 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
43 function set_mt.__tostring(set) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
44 local s, items = { }, set._items; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
45 for item in pairs(items) do |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
46 s[#s+1] = tostring(item); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
47 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
48 return t_concat(s, ", "); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
49 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
50 |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
51 local items_mt = {}; |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
52 function items_mt.__call(items, _, k) |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
53 return next(items, k); |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
54 end |
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
55 |
904 | 56 function new(list) |
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
57 local items = setmetatable({}, items_mt); |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
58 local set = { _items = items }; |
904 | 59 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
60 function set:add(item) |
904 | 61 items[item] = true; |
62 end | |
63 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
64 function set:contains(item) |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
65 return items[item]; |
904 | 66 end |
67 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
68 function set:items() |
904 | 69 return items; |
70 end | |
71 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
72 function set:remove(item) |
904 | 73 items[item] = nil; |
74 end | |
75 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
76 function set:add_list(list) |
904 | 77 for _, item in ipairs(list) do |
78 items[item] = true; | |
79 end | |
80 end | |
81 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
82 function set:include(otherset) |
904 | 83 for item in pairs(otherset) do |
84 items[item] = true; | |
85 end | |
86 end | |
87 | |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
88 function set:exclude(otherset) |
904 | 89 for item in pairs(otherset) do |
90 items[item] = nil; | |
91 end | |
92 end | |
93 | |
1029
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
94 function set:empty() |
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
95 return not next(items); |
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
96 end |
4ead03974759
util.set: Add set:empty() to discover if the set is the empty set
Matthew Wild <mwild1@gmail.com>
parents:
1028
diff
changeset
|
97 |
905
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
98 if list then |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
99 set:add_list(list); |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
100 end |
6169597d5574
util.set: Fix to make constructor work, and functions defined correctly
Matthew Wild <mwild1@gmail.com>
parents:
904
diff
changeset
|
101 |
1028
594a07e753a0
util.set: Add metatable to sets to allow +, -, /, ==, tostring and to double as iterators
Matthew Wild <mwild1@gmail.com>
parents:
917
diff
changeset
|
102 return setmetatable(set, set_mt); |
904 | 103 end |
104 | |
105 function union(set1, set2) | |
106 local set = new(); | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
107 local items = set._items; |
904 | 108 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
109 for item in pairs(set1._items) do |
904 | 110 items[item] = true; |
111 end | |
112 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
113 for item in pairs(set2._items) do |
904 | 114 items[item] = true; |
115 end | |
116 | |
117 return set; | |
118 end | |
119 | |
120 function difference(set1, set2) | |
121 local set = new(); | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
122 local items = set._items; |
904 | 123 |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
124 for item in pairs(set1._items) do |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
125 items[item] = (not set2._items[item]) or nil; |
904 | 126 end |
127 | |
917
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
128 return set; |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
129 end |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
130 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
131 function intersection(set1, set2) |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
132 local set = new(); |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
133 local items = set._items; |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
134 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
135 set1, set2 = set1._items, set2._items; |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
136 |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
137 for item in pairs(set1) do |
f12f88b3d4a1
util.set: Rename private items container, optimise set.difference() and add set.intersection()
Matthew Wild <mwild1@gmail.com>
parents:
905
diff
changeset
|
138 items[item] = (not not set2[item]) or nil; |
904 | 139 end |
140 | |
141 return set; | |
142 end | |
143 | |
1030
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
144 function xor(set1, set2) |
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
145 return union(set1, set2) - intersection(set1, set2); |
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
146 end |
a82268d507fc
util.set: Add set.xor() to get a set consisting of items not in both sets
Matthew Wild <mwild1@gmail.com>
parents:
1029
diff
changeset
|
147 |
904 | 148 return _M; |