Software /
code /
prosody
Comparison
util/set.lua @ 6777:5de6b93d0190
util.*: Remove use of module() function, make all module functions local and return them in a table at the end
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 21 Feb 2015 10:36:37 +0100 |
parent | 6675:cb5b56ddabfd |
child | 8522:073e517a1487 |
comparison
equal
deleted
inserted
replaced
6774:3965662ae091 | 6777:5de6b93d0190 |
---|---|
8 | 8 |
9 local ipairs, pairs, setmetatable, next, tostring = | 9 local ipairs, pairs, setmetatable, next, tostring = |
10 ipairs, pairs, setmetatable, next, tostring; | 10 ipairs, pairs, setmetatable, next, tostring; |
11 local t_concat = table.concat; | 11 local t_concat = table.concat; |
12 | 12 |
13 module "set" | 13 local _ENV = nil; |
14 | 14 |
15 local set_mt = {}; | 15 local set_mt = {}; |
16 function set_mt.__call(set, _, k) | 16 function set_mt.__call(set, _, k) |
17 return next(set._items, k); | 17 return next(set._items, k); |
18 end | |
19 function set_mt.__add(set1, set2) | |
20 return _M.union(set1, set2); | |
21 end | |
22 function set_mt.__sub(set1, set2) | |
23 return _M.difference(set1, set2); | |
24 end | |
25 function set_mt.__div(set, func) | |
26 local new_set = _M.new(); | |
27 local items, new_items = set._items, new_set._items; | |
28 for item in pairs(items) do | |
29 local new_item = func(item); | |
30 if new_item ~= nil then | |
31 new_items[new_item] = true; | |
32 end | |
33 end | |
34 return new_set; | |
35 end | |
36 function set_mt.__eq(set1, set2) | |
37 set1, set2 = set1._items, set2._items; | |
38 for item in pairs(set1) do | |
39 if not set2[item] then | |
40 return false; | |
41 end | |
42 end | |
43 | |
44 for item in pairs(set2) do | |
45 if not set1[item] then | |
46 return false; | |
47 end | |
48 end | |
49 | |
50 return true; | |
51 end | |
52 function set_mt.__tostring(set) | |
53 local s, items = { }, set._items; | |
54 for item in pairs(items) do | |
55 s[#s+1] = tostring(item); | |
56 end | |
57 return t_concat(s, ", "); | |
58 end | 18 end |
59 | 19 |
60 local items_mt = {}; | 20 local items_mt = {}; |
61 function items_mt.__call(items, _, k) | 21 function items_mt.__call(items, _, k) |
62 return next(items, k); | 22 return next(items, k); |
63 end | 23 end |
64 | 24 |
65 function new(list) | 25 local function new(list) |
66 local items = setmetatable({}, items_mt); | 26 local items = setmetatable({}, items_mt); |
67 local set = { _items = items }; | 27 local set = { _items = items }; |
68 | 28 |
69 -- We access the set through an upvalue in these methods, so ignore 'self' being unused | 29 -- We access the set through an upvalue in these methods, so ignore 'self' being unused |
70 --luacheck: ignore 212/self | 30 --luacheck: ignore 212/self |
114 end | 74 end |
115 | 75 |
116 return setmetatable(set, set_mt); | 76 return setmetatable(set, set_mt); |
117 end | 77 end |
118 | 78 |
119 function union(set1, set2) | 79 local function union(set1, set2) |
120 local set = new(); | 80 local set = new(); |
121 local items = set._items; | 81 local items = set._items; |
122 | 82 |
123 for item in pairs(set1._items) do | 83 for item in pairs(set1._items) do |
124 items[item] = true; | 84 items[item] = true; |
129 end | 89 end |
130 | 90 |
131 return set; | 91 return set; |
132 end | 92 end |
133 | 93 |
134 function difference(set1, set2) | 94 local function difference(set1, set2) |
135 local set = new(); | 95 local set = new(); |
136 local items = set._items; | 96 local items = set._items; |
137 | 97 |
138 for item in pairs(set1._items) do | 98 for item in pairs(set1._items) do |
139 items[item] = (not set2._items[item]) or nil; | 99 items[item] = (not set2._items[item]) or nil; |
140 end | 100 end |
141 | 101 |
142 return set; | 102 return set; |
143 end | 103 end |
144 | 104 |
145 function intersection(set1, set2) | 105 local function intersection(set1, set2) |
146 local set = new(); | 106 local set = new(); |
147 local items = set._items; | 107 local items = set._items; |
148 | 108 |
149 set1, set2 = set1._items, set2._items; | 109 set1, set2 = set1._items, set2._items; |
150 | 110 |
153 end | 113 end |
154 | 114 |
155 return set; | 115 return set; |
156 end | 116 end |
157 | 117 |
158 function xor(set1, set2) | 118 local function xor(set1, set2) |
159 return union(set1, set2) - intersection(set1, set2); | 119 return union(set1, set2) - intersection(set1, set2); |
160 end | 120 end |
161 | 121 |
162 return _M; | 122 function set_mt.__add(set1, set2) |
123 return union(set1, set2); | |
124 end | |
125 function set_mt.__sub(set1, set2) | |
126 return difference(set1, set2); | |
127 end | |
128 function set_mt.__div(set, func) | |
129 local new_set = new(); | |
130 local items, new_items = set._items, new_set._items; | |
131 for item in pairs(items) do | |
132 local new_item = func(item); | |
133 if new_item ~= nil then | |
134 new_items[new_item] = true; | |
135 end | |
136 end | |
137 return new_set; | |
138 end | |
139 function set_mt.__eq(set1, set2) | |
140 set1, set2 = set1._items, set2._items; | |
141 for item in pairs(set1) do | |
142 if not set2[item] then | |
143 return false; | |
144 end | |
145 end | |
146 | |
147 for item in pairs(set2) do | |
148 if not set1[item] then | |
149 return false; | |
150 end | |
151 end | |
152 | |
153 return true; | |
154 end | |
155 function set_mt.__tostring(set) | |
156 local s, items = { }, set._items; | |
157 for item in pairs(items) do | |
158 s[#s+1] = tostring(item); | |
159 end | |
160 return t_concat(s, ", "); | |
161 end | |
162 | |
163 return { | |
164 new = new; | |
165 union = union; | |
166 difference = difference; | |
167 intersection = intersection; | |
168 xor = xor; | |
169 }; |