-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlate.lua
More file actions
234 lines (194 loc) · 4.71 KB
/
Plate.lua
File metadata and controls
234 lines (194 loc) · 4.71 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
local Plate = {}
function Plate.DeepCopy(t) --Recursively copies a table.
if type(t) ~= "table" then
error("Argument is not a table")
end
local out = {}
for k, v in pairs(t) do
if type(v) == "table" then
out[k] = Plate.DeepCopy(v)
else
out[k] = v
end
end
return out
end
function Plate.RemoveByValue(t, value) --removes the first matching value from a table.
if type(t) ~= "table" then
error("First argument is not a table")
end
for i = 1, #t do
if t[i] == value then
return table.remove(t, i)
end
end
return nil
end
function Plate.At(t, idx) --Returns the value at a given index like t[i] but it has bounds checking.
if type(t) ~= "table" then
error("First argument is not a table")
end
if type(idx) ~= "number" or idx % 1 ~= 0 then
error("Index is not an integer")
end
if idx < 1 or idx > #t then
error("Index out of bounds")
end
return t[idx]
end
function Plate.Count(t) --Counts how many key value pairs exist in a table
if type(t) ~= "table" then
error("Argument is not a table")
end
local count = 0
for i, v in pairs(t) do
count = count + 1
end
return count
end
function Plate.Reverse(t) --Returns an array containing the elements of t in reverse order.
if type(t) ~= "table" then
error("Argument is not a table")
end
local out = {}
for i = #t, 1, -1 do
out[#out + 1] = t[i]
end
return out
end
function Plate.Slice(t, StartIdx, EndIdx) --Returns an array containing elements from StartIdx to EndIdx
if type(t) ~= "table" then
error("First argument is not a table")
end
if type(StartIdx) ~= "number" or StartIdx % 1 ~= 0 then
error("Start index is not an integer")
end
if type(EndIdx) ~= "number" or EndIdx % 1 ~= 0 then
error("End index is not an integer")
end
if StartIdx < 1 or StartIdx > #t then
error("Start index out of bounds")
end
if EndIdx < 1 or EndIdx > #t then
error("End index out of bounds")
end
if StartIdx > EndIdx then
warn("Start index greater than end index")
end
local out = {}
for i = StartIdx, EndIdx do
out[#out + 1] = t[i]
end
return out
end
function Plate.Append(t1, t2) --Appends all array elements from t2 onto the end of t1.
if type(t1) ~= "table" then
error("First argument is not a table")
end
if type(t2) ~= "table" then
error("Second argument is not a table")
end
for i, v in ipairs(t2) do
t1[#t1 + 1] = v
end
end
function Plate.Merge(t1, t2) --Copies every key value pair from t2 into t1.
if type(t1) ~= "table" then
error("First argument is not a table")
end
if type(t2) ~= "table" then
error("Second argument is not a table")
end
for i, v in pairs(t2) do
t1[i] = v
end
return t1
end
-- Set like table stuff
-- Both t1 and t2 are expected to already be sorted.
-- Values must be comparable.
-- Duplicate values are not removed automatically.
function Plate.Union(t1, t2) --Returns the union of two sorted tables.
if type(t1) ~= "table" then
error("First argument is not a table")
end
if type(t2) ~= "table" then
error("Second argument is not a table")
end
local out = {}
local i1 = 1
local i2 = 1
while i1 <= #t1 and i2 <= #t2 do
if t1[i1] < t2[i2] then
out[#out + 1] = t1[i1]
i1 = i1 + 1
elseif t1[i1] > t2[i2] then
out[#out + 1] = t2[i2]
i2 = i2 + 1
else
out[#out + 1] = t1[i1]
i1 = i1 + 1
i2 = i2 + 1
end
end
while i1 <= #t1 do
out[#out + 1] = t1[i1]
i1 = i1 + 1
end
while i2 <= #t2 do
out[#out + 1] = t2[i2]
i2 = i2 + 1
end
return out
end
function Plate.Intersect(t1, t2) --Returns the intersection of two sorted tables.
if type(t1) ~= "table" then
error("First argument is not a table")
end
if type(t2) ~= "table" then
error("Second argument is not a table")
end
local out = {}
local i1 = 1
local i2 = 1
while i1 <= #t1 and i2 <= #t2 do
if t1[i1] < t2[i2] then
i1 = i1 + 1
elseif t1[i1] > t2[i2] then
i2 = i2 + 1
else
out[#out + 1] = t1[i1]
i1 = i1 + 1
i2 = i2 + 1
end
end
return out
end
function Plate.Difference(t1, t2) --Returns the set difference of two sorted tables, values that are in t1 but not in t2.
if type(t1) ~= "table" then
error("First argument is not a table")
end
if type(t2) ~= "table" then
error("Second argument is not a table")
end
local out = {}
local i1 = 1
local i2 = 1
while i1 <= #t1 and i2 <= #t2 do
if t1[i1] < t2[i2] then
out[#out + 1] = t1[i1]
i1 = i1 + 1
elseif t1[i1] > t2[i2] then
i2 = i2 + 1
else
i1 = i1 + 1
i2 = i2 + 1
end
end
while i1 <= #t1 do
out[#out + 1] = t1[i1]
i1 = i1 + 1
end
return out
end
return Plate