-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.lua
99 lines (85 loc) · 2.09 KB
/
set.lua
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
--
-- Copyright (c) 2024 Min Idzelis
--
-- This file is part of LR-Immich.
--
-- Foobar is free software: you can redistribute it and/or modify it under the terms
-- of the GNU General Public License as published by the Free Software Foundation,
-- either version 3 of the License, or (at your option) any later version.
--
-- LR-Immich is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-- without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-- See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with Foobar.
-- If not, see <https://www.gnu.org/licenses/>.
local Set = {}
function Set:new(list)
local set = {}
setmetatable(set, {
__index = Set,
__tostring = Set.tostring
})
set.items = {}
set.size = 0
set:addAll(list)
return set
end
function Set:intersection(a, b)
local result = Set:new();
a:each(function(value)
if (b:has(value)) then
result:add(value)
end
end)
return result
end
function Set:add(value)
if not self.items[value] then
self.items[value] = true
self.size = self.size + 1
end
end
function Set:addAll(list)
if type(list) == 'table' then
for _, value in ipairs(list) do
self:add(value);
end
end
end
function Set:remove(value)
if self:has(value) then
self.items[value] = nil
self.size = self.size - 1
end
end
function Set:removeAll(list)
for _, value in ipairs(list) do
self:remove(value)
end
end
function Set:has(value)
return self.items[value] ~= nil
end
function Set:each(callback)
for key in pairs(self.items) do
callback(key)
end
end
function Set:list()
local _list = {}
for key in pairs(self.items) do
table.insert(_list, key)
end
return _list
end
function Set:tostring()
local s = "{ "
local sep = ""
for e in pairs(self.items) do
s = s .. sep .. e
sep = ", "
end
return s .. " }"
end
return Set