- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.lua
More file actions
Latest commit
142 lines (121 loc) · 2.43 KB
/
Copy pathset.lua
File metadata and controls
142 lines (121 loc) · 2.43 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
---@classSet
localSet= {}
Set.__extVersion="0.1.0"
Set.__index=Set
localsetsize= {} -- use in key of the set to store it's size.
---@returnSet
functionSet.new(keys)
localself=setmetatable({[setsize] =0}, Set)
self:padd(keys)
returnself
end
functionSet:add(key)
assert(key~=setsize, "Attempt to use protected key.")
ifkey==nilorself[key] thenreturnend
self[setsize] =self[setsize] +1
self[key] =true
end
functionSet:padd(keys)
ifkeys==nilthenreturnend
for_, vinipairs(keys) doself:add(v) end
end
functionSet:remove(key)
assert(key~=setsize, "Attempt to use protected key.")
ifkey==nilornotself[key] thenreturnend
self[setsize] =self[setsize] -1
self[key] =nil
end
functionSet:toggle(key)
assert(key~=setsize, "Attempt to use protected key.")
localsz, to=1, true---@typenumber,boolean|nil
ifself[key] then
sz, to=-1, nil
end
self[key] =to
self[setsize] =self[setsize] +sz
end
functionSet:list()
localout= {}
forkinpairs(self) do
ifk~=setsizethen
table.insert(out, k)
end
end
returnout
end
functionSet:clone()
localout= {}
fork,vinpairs(self) do
out[k] =v
end
returnsetmetatable(out, Set)
end
functionSet:iter()
localkey
returnfunction()
key=next(self, key)
ifkey==setsizethenkey=next(self, key) end
returnkey
end
end
functionSet:union(other)
forkinpairs(other) do
self[k] =true
end
end
functionSet:difference(other)
localclone=self:clone()
forkinother:iter() do
ifclone[k] then
clone:remove(k)
else
clone:add(k)
end
end
returnclone
end
functionSet:intersection(other)
localout=Set.new()
forkinself:iter() do
ifother[k] then
out:add(k)
end
end
returnout
end
functionSet:joint(b)
forkinself:iter() do
ifb[k] thenreturntrueend
end
returnfalse
end
functionSet:isSubset(b)
forkinself:iter() do
ifnotb[k] thenreturnfalseend
end
returntrue
end
functionSet:isSuperset(b)
forkinb:iter() do
ifnotself[k] thenreturnfalseend
end
returntrue
end
functionSet:__le(b) returnself:isSubset(b) end
functionSet:__ge(b) returnself:isSubset(b) end
functionSet:__sub(other)
returnself:difference(other)
end
functionSet:__len()
returnself[setsize]
end
functionSet:__eq(other)
ifother[setsize] and#other~=#selfthenreturnfalseend
forkinself:iter() do
ifnotother[k] then
returnfalse
end
end
returntrue
end
returnSet