-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsets.lua
More file actions
67 lines (53 loc) · 1.04 KB
/
sets.lua
File metadata and controls
67 lines (53 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local Set = {}
local mt = {}
function Set.new(l)
local set = {}
setmetatable(set, mt)
for _, v in ipairs(l) do
set[v] = true
end
return set
end
function Set.union(a, b)
local res = Set.new{}
for k in pairs(a) do
res[k] = true
end
for k in pairs(b) do
res[k] = true
end
return res
end
function Set.intersection( a, b )
local res = Set.new{}
for k in pairs(a) do
res[k] = b[k]
end
return res
end
function Set.tostring( set )
local l = {}
for e in pairs(set) do
l[#l + 1] = tostring(e)
end
return "{" .. table.concat(l, ", ") .. "}"
end
mt.__add = Set.union
mt.__mul = Set.intersection
mt.__le = function ( a, b )
for k in pairs(a) do
if not b[k] then
return false
end
end
return true
end
mt.__lt = function ( a, b )
return a <= b and not (b <= a)
end
mt.__eq = function ( a, b )
return a <= b and b <= a
end
mt.__tostring = Set.tostring
mt.__metatable = "not your business"
return Set