This repository was archived by the owner on Mar 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathset.lua
More file actions
68 lines (57 loc) · 1.34 KB
/
Copy pathset.lua
File metadata and controls
68 lines (57 loc) · 1.34 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
68
local set = {}
function set:new(elements)
local res = {}
setmetatable(res, self)
self.__index = self
if elements then res:add(elements) end
return res
end
function set:has(elements)
for _, e in pairs(elements) do
if not self[e] then return false end
end
return true
end
function set:add(elements)
for _, e in pairs(elements) do
if not self:has { e } then self[e] = true end
end
end
function set:len()
return table_size(self)
end
function set:is_empty()
return next(self) == nil
end
function set:elements()
local res = {}
for k, _ in pairs(self) do
table.insert(res, k)
end
return res
end
function set:intersection(others)
local res = set:new {}
for k in pairs(self) do
for _, s in pairs(others) do
if not s[k] then goto continue end
end
res[k] = true
::continue::
end
return res
end
function set:union(others)
for _, s in pairs(others) do
self:add(s:elements())
end
return self
end
function set:equal(others)
return self:len() == (self:intersection(others)):len()
end
assert(set:new { "a" }:has { "a" })
assert(not set:new { "a" }:has { "b" })
assert(set:new { "a", "b" }:has { "a", "b" })
assert(set:new { "a", "b" }:intersection { set:new { "b", "c" } }:equal { set:new { "b" } })
return set