-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.lua
More file actions
63 lines (51 loc) · 1.42 KB
/
set.lua
File metadata and controls
63 lines (51 loc) · 1.42 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
local currentPath = (...):match('(.-)[^%./]+$')
--- @class IUILib
local iui = require(currentPath .. "iui")
--- A type that stores an unordered collection of unique values.
--- @class IUISet<T>
--- @field private storage table<T, true>
--- @field new fun(): IUISet<T> Returns a new set.
--- @field put fun(self: self, v: T) Inserts `v`, if not already present.
--- @field putAll fun(self: self, s: IUISet<T>) Inserts all members of `s`.
--- @field remove fun(self: self, v: T) Removes `v`, if present.
--- @field removeAll fun(self: self) Removes all values.
--- @field has fun(self: self, v: T): boolean Returns whether the set has `v`.
--- @field getCount fun(self: self): number Returns the number of values, in O(n) time.
local Set = {}
Set.__index = Set
function Set.new()
local out = {
storage = {}
}
setmetatable(out, Set)
return out
end
function Set:put(v)
self.storage[v] = true
end
function Set:putAll(s)
local storage = self.storage
for k, _ in pairs(s.storage) do
storage[k] = true
end
end
function Set:remove(v)
self.storage[v] = nil
end
function Set:removeAll()
local storage = self.storage
for k, _ in pairs(storage) do
storage[k] = nil
end
end
function Set:has(v)
return self.storage[v] == true
end
function Set:getCount()
local count = 0
for _ in pairs(self.storage) do
count = count + 1
end
return count
end
iui.set = Set