- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.lua
More file actions
Latest commit
325 lines (285 loc) · 7.63 KB
/
Copy pathbase.lua
File metadata and controls
325 lines (285 loc) · 7.63 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
-- only for use with tablua files
---@classTabluaBaseFunctions
---@fieldinsertfunction
---@fieldremovefunction
localBase= {}
setmetatable(Base, { __index=table })
Base.__extVersion="0.0.2"
localfunctionassert(condition, message, stack)
ifnotconditionthen
error(message, stack)
end
end
localfunctionassertTable(t)
assert(type(t) =="table",
"Parameter type needs to be of type 'table'. Passed type: " ..type(t), 4)
end
-------------------------------------------------
---Create a new tablua base
---@paramttable?
---@returntable|TabluaBaseFunctions
functionBase.new(t)
returnsetmetatable(tor {}, { __index=Base })
end
---Swaps two elements in the table.
---@paramatable
---@paramfirstinteger
---@paramsecondinteger
functionBase.swap(a, first, second)
assertTable(a)
localcache=a[first]
a[first] =a[second]
a[second] =cache
end
---This function is a simple clone. Only meant for single layer tables and arrays. <br>
---If passed a table with a nested table, original references to the subsequent table could interfere. <br>
---If you are unsure if you should use this function, or table.clone, use table.clone. <br>
---@paramatable|TabluaBaseFunctions
---@returntable
functionBase.shallowClone(a)
assertTable(a)
localoutput= {}
fork, vinpairs(a) do
output[k] =v
end
returnBase.new(output)
end
---This function will create copy of the passed table, and will clone any nested tables.
---@paramatable|TabluaBaseFunctions
---@returnTabluaBaseFunctions
functionBase.clone(a)
iftype(a) ~="table" thenreturnaend
localoutput= {}
fork, vinpairs(a) do
iftype(v) =="table" then
output[k] =Base.clone(v)
else
output[k] =v
end
end
returnBase.new(output)
end
---This function will join multiple tables together.
---Tables with string keys will set a[key] to b[key] value.
---@paramatable|TabluaBaseFunctions
---@param ... table|TabluaBaseFunctions
---@returnTabluaBaseFunctions
functionBase.join(a, ...)
assertTable(a)
localoutput=Base.clone(a)
fori=1, select("#", ...) do
localt=select(i, ...)
assertTable(t)
fork, vinpairs(t) do
iftype(k) =="number" then
Base.insert(output, v)
else
output[k] =v
end
end
end
returnBase.new(output)
end
---Returns a new array from a from numerical index start to finish.
---@paramaany
---@paramstartany
---@paramfinishany
---@returntable|TabluaBaseFunctions
functionBase.slice(a, start, finish)
assertTable(a)
ifnotfinishthenfinish=#aend
localout= {}
fori=start, finishdo
ifnota[i] thenbreakend
Base.insert(out, a[i])
end
returnBase.new(out)
end
---This function will remove a range of elements from a table.
---@paramaTabluaBaseFunctions
---@paramindexinteger Index to start removing elements exclusive. Item at index will remain
---@paramhowmanyinteger Amount of elements to remove
---@param ... anyElements to insert at index
---@returnTabluaBaseFunctions
functionBase.splice(a, index, howmany, ...)
-- inspired by
-- developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
assertTable(a)
ifnothowmanythenhowmany=0end
localout= {}
fori=1, indexdo
Base.insert(out, a[i])
end
for_, vinipairs({ ... }) do
Base.insert(out, v)
end
fori=index+howmany+1, #ado
Base.insert(out, a[i])
end
returnBase.new(out)
end
---Returns the last ordered item in the table. Holes in array will not be considered.
---@paramatable|TabluaBaseFunctions
---@returnTabluaBaseFunctions #The last ordered item in the table
functionBase.last(a)
returna[#a]
end
---Returns a random item from the table.
---@paramatable|TabluaBaseFunctions
---@returnTabluaBaseFunctions #The random item
---@returninteger #The index of the random item
functionBase.choice(a)
assertTable(a)
localchoice=math.random(#a)
returna[choice], choice
end
---Returns count number of random items from the table.
---@paramatable|TabluaBaseFunctions
---@paramcountinteger total number of random items to return
---@returnTabluaBaseFunctions
functionBase.choices(a, count)
assertTable(a)
localout= {}
localcache=Base.clone(a)
for_=1, countdo
localchoice, key=Base.choice(cache)
Base.insert(out, choice)
Base.remove(cache, key)
end
returnBase.new(out)
end
---This function will search a table for a value and return the index of the value.<br>
---If the value is not found, it will return nil.
---@paramatable|TabluaBaseFunctions
---@paramvalueany
---@returnTabluaBaseFunctions?
functionBase.find(a, value)
assertTable(a)
fork, vinpairs(a) do
ifv==valuethen
returnk
end
end
returnnil
end
---This function will search a table for a value and return the index of the value.<br>
---If the value is not found, it will return nil.<br>
---Important! The table must be sorted before using this function.
---@paramatable|TabluaBaseFunctions
---@paramvalueany
---@returninteger?
functionBase.binarySearch(a, value)
assertTable(a)
locallow=1
localhigh=#a
whilelow<=highdo
localmid=math.floor((low+high) /2)
ifa[mid] ==valuethen
returnmid
elseifa[mid] <valuethen
low=mid+1
else
high=mid-1
end
end
returnnil
end
---This will reduce a into it's unique values
---@paramatable|TabluaBaseFunctions
---@returnTabluaBaseFunctions
functionBase.unique(a)
localunique= {}
localuniqueSet= {}
for_, vinpairs(a) do
ifnotuniqueSet[v] then
uniqueSet[v] =true
Base.insert(unique, v)
end
end
returnBase.new(unique)
end
---This will go through an array and remove all that match [remove].
---@paramatable|TabluaBaseFunctions
---@paramremoveany The value to remove
---@returnTabluaBaseFunctions
---@nodiscard
functionBase.gCondense(a, remove)
localout= {}
for_, vinpairs(a) do
ifv~=removethen
Base.insert(out, v)
end
end
returnBase.new(out)
end
---This will go through an array and remove all that match [remove].<br>
---Condenses the table in place without creating a new address
---@paramatable|TabluaBaseFunctions
---@paramremoveany The value to remove
functionBase.condense(a, remove)
localnewTab= {}
fork, vinpairs(a) do
ifv~=removethen
Base.insert(newTab, v)
end
a[k] =nil
end
fork, vinpairs(newTab) do
a[k] =v
end
end
---Returns true if every element in this array satisfies the testing function.
---@paramatable|TabluaBaseFunctions
---@paramtestfunction The test function. ex. `function(v) return v > 4 end`
---@returnboolean
functionBase.every(a, test)
for_, vinpairs(a) do
ifnottest(v) then
returnfalse
end
end
returntrue
end
---Returns a new array containing all elements that param test returns true on
---@paramatable|TabluaBaseFunctions
---@paramtestfunction The test function. ex. `function(k, v) return v > 4 end`
---@returnTabluaBaseFunctions
functionBase.filter(a, test)
localout=Base.new()
fork, vinpairs(a) do
iftest(k, v) then
Base.insert(out, v)
end
end
returnout
end
---Reverses the order of a
---@paramatable|TabluaBaseFunctions
functionBase.invert(a)
fori=1, math.floor(#a/2) do
Base.swap(a, i, #a- (i-1))
end
end
---Directly shuffles table `a` using Fisher-Yates Shuffle algorithm
---@paramatable|TabluaBaseFunctions
functionBase.shuffle(a)
locallength=#a
whilelength>1do
locali=math.random(1, length)
Base.swap(a, length, i)
length=length-1
end
end
---This is an iterator that iterates backwards through the array
---@paramatable|TabluaBaseFunctions
---@returnfunction The iterator
functionBase.reverse(a)
locali=#a+1
returnfunction()
i=i-1
ifi>0then
returni, a[i]
end
end
end
returnBase