-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_utils.lua
More file actions
292 lines (233 loc) · 4.94 KB
/
table_utils.lua
File metadata and controls
292 lines (233 loc) · 4.94 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
local M = {}
---@param self table
---@param value any
---@return boolean
function M.contains(self, value)
if M.is_empty(self) then
return false
end
for _, _value in pairs(self) do
if _value == value then
return true
end
end
return false
end
---@param self table
---@return boolean
function M.is_empty(self)
return #self == 0
end
---@param value any
---@param self table
---@return integer?
function M.get_index(self, value)
local index = 1
for _, _value in pairs(self) do
if _value == value then
return index
end
index = index + 1
end
return nil
end
---@param self table
---@param other_table table
---@return boolean
function M.is_equal(self, other_table)
if #self ~= #other_table then
return false
end
for key, value in pairs(self) do
if other_table[key] ~= value then
return false
end
end
return true
end
---@deprecated Use table.concat() instead
---@param self table
---@param other_table table
---@param element_condition? fun(any):boolean
---@return table
function M.concat_tables(self, other_table, element_condition)
local always_true_fun = function()
return true
end
element_condition = element_condition or always_true_fun
local final_result = {}
for _, value in pairs(self) do
if element_condition(value) then
table.insert(final_result, value)
end
end
for _, value in pairs(other_table) do
if element_condition(value) then
table.insert(final_result, value)
end
end
return final_result
end
---@param lhs table
---@param rhs table
---@return table
function M.concat(lhs, rhs)
local result = lhs
for k, v in pairs(rhs) do
result[k] = v
end
return result
end
---@generic T
---@param self table
---@param callback fun(element: T):boolean
---@return table
function M.filter(self, callback)
local result = {}
for _, value in pairs(self) do
if callback(value) then
table.insert(result, value)
end
end
return result
end
---@generic T, U
---@param self table
---@param callback fun(item: T): U
---@return table
function M.map(self, callback)
local result = {}
for _, value in pairs(self) do
table.insert(result, callback(value))
end
return result
end
---@generic K, V, U
---@param self table
---@param callback fun(key: K, value: V): U
---@return table
function M.mapkv(self, callback)
local t = {}
for key, value in pairs(self) do
table.insert(t, callback(key, value))
end
return t
end
---@generic T
---@param size integer
---@param defaultValue T
---@return table<T>
function M.alloc(size, defaultValue)
local t = {}
for i = 1, size do
t[i] = defaultValue
end
return t
end
---@param self table
---@return string
function M.toString(self)
local json = require("cluautils.json")
return json.encode(self, {
pretty = true,
indent = " ",
})
end
---@param start number
---@param stop number
---@param step number
---@return table<number>
function M.makeRange(start, stop, step)
local t = {}
for i = start, stop, step do
table.insert(t, i)
end
return t
end
---@param t table<T>
---@param element T
---@return integer?
function M.binarySearch(t, element)
if #t == 0 then
return nil
end
local lhs = 1
local rhs = #t
if element <= t[lhs] or element >= t[rhs] then
return nil
end
while lhs <= rhs do
local mid = math.floor(lhs + (rhs - lhs) / 2)
if element == t[mid] then
return mid
end
if element >= t[mid] then
lhs = mid + 1
else
rhs = mid - 1
end
end
return nil
end
---@param t table<T>
---@param compare (fun(T, T): boolean)?
function M.mergeSort(t, compare)
local c = compare or function(a, b)
return a < b
end
---@param l integer
---@param r integer
---@return table<T>
local function sub(l, r)
local res = {}
for i = l, r do
table.insert(res, t[i])
end
return res
end
---@param l integer
---@param m integer
---@param r integer
local function merge(l, m, r)
local L = sub(l, m)
local R = sub(m + 1, r)
local n1 = m - l + 1
local n2 = r - m
local i = 1
local j = 1
local k = 1
while i <= n1 and j <= n2 do
if c(L[i], R[j]) then
t[k] = L[i]
i = i + 1
else
t[k] = R[j]
j = j + 1
end
k = k + 1
end
while i <= n1 do
t[k] = L[i]
i = i + 1
k = k + 1
end
while j <= n2 do
t[k] = R[j]
j = j + 1
k = k + 1
end
end
---@param l integer
---@param r integer
local function sort(l, r)
if l >= r then
return
end
local m = math.floor(l + (r - l) / 2)
sort(l, m)
sort(m + 1, r)
merge(l, m, r)
end
sort(1, #t)
end
return M