-
Notifications
You must be signed in to change notification settings - Fork 1
/
table_util.lua
339 lines (312 loc) · 5.99 KB
/
table_util.lua
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
local table_util = {}
table_util.new = require("table.new")
table_util.clear = require("table.clear")
---@param a table
---@param b table
---@return boolean
function table_util.equal(a, b)
local size, _size = 0, 0
for k, v in pairs(a) do
size = size + 1
local _v = b[k]
if v == v and v ~= _v then -- nan check
return false
end
end
for _ in pairs(b) do
_size = _size + 1
end
return size == _size
end
---@param a table
---@param b table
---@return boolean
function table_util.deepequal(a, b)
local size, _size = 0, 0
for k, v in pairs(a) do
size = size + 1
local _v = b[k]
if type(v) == "table" and type(_v) == "table" then
if not table_util.equal(v, _v) and not table_util.deepequal(v, _v) then
return false
end
elseif v == v and v ~= _v then -- nan check
return false
end
end
for _ in pairs(b) do
_size = _size + 1
end
return size == _size
end
assert(table_util.deepequal({{}}, {{}}))
---@param src table?
---@param dst table?
---@return table?
function table_util.copy(src, dst)
if not src then
return
end
dst = dst or {}
for k, v in pairs(src) do
dst[k] = v
end
return dst
end
---@param t table
---@return table
function table_util.deepcopy(t)
if type(t) ~= "table" then
return t
end
local out = {}
for k, v in pairs(t) do
if type(v) == "table" then
out[k] = table_util.deepcopy(v)
else
out[k] = v
end
end
return out
end
---@param new table
---@param old table
---@param new_f function?
---@param old_f function?
---@return table
---@return table
---@return table
function table_util.array_update(new, old, new_f, old_f)
local _new = {}
for _, v in ipairs(new) do
if new_f then v = new_f(v) end
_new[v] = true
end
local _old = {}
for _, v in ipairs(old) do
if old_f then v = old_f(v) end
_old[v] = true
end
new = {}
old = {}
local all = {}
for v in pairs(_new) do
if not _old[v] then
table.insert(new, v)
end
table.insert(all, v)
end
for v in pairs(_old) do
if not _new[v] then
table.insert(old, v)
end
end
return new, old, all
end
---@param t table
---@param key any?
---@return any?
function table_util.inside(t, key)
local subvalue = t
if type(key) == "table" then
for _, subkey in ipairs(key) do
if type(subkey) == "table" then
local k = subkey[1]
local f = subkey[2]
local v = table_util.inside(t, k)
if v and f(t) then
return v
end
elseif type(subkey) == "string" then
local v = table_util.inside(t, subkey)
if v then
return v
end
end
end
return
elseif type(key) == "string" then
for subkey in key:gmatch("[^.]+") do
if type(subvalue) ~= "table" then
return
end
subvalue = subvalue[subkey]
end
return subvalue
end
end
---@param ... any?
---@return table
function table_util.pack(...)
return {n = select("#", ...), ...}
end
---@param f function
---@param index number?
---@return function
function table_util.cache(f, index)
local cache = {}
return function(...)
local k = select(index or 1, ...)
local t = cache[k]
if t then
return unpack(t, 1, t.n)
end
t = table_util.pack(f(...))
cache[k] = t
return unpack(t, 1, t.n)
end
end
---@param t table
---@param v any
---@param f function?
---@return number?
function table_util.indexof(t, v, f)
for i, _v in ipairs(t) do
if not f and _v == v or f and f(_v) == v then
return i
end
end
end
---@param t table
---@param v any
---@param f function?
---@return any?
function table_util.keyof(t, v, f)
for k, _v in pairs(t) do
if not f and _v == v or f and f(_v) == v then
return k
end
end
end
---@generic K
---@generic V
---@param t {[K]: V}
---@return {[V]: K}
function table_util.invert(t)
local _t = {}
for k, v in pairs(t) do
assert(not _t[v], "duplicate value '" .. tostring(v) .. "'")
_t[v] = k
end
return _t
end
---@param t table
---@param append table
function table_util.append(t, append)
for i, v in ipairs(append) do
table.insert(t, v)
end
end
---@param t table
---@return number
function table_util.max_index(t)
local max_i = 0
for i in pairs(t) do
if type(i) == "number" then
max_i = math.max(max_i, i)
end
end
return max_i
end
---@param t {[string]: number}
---@return string?
function table_util.keyofenum(t, v)
if t[v] then
return v
end
local k = table_util.keyof(t, tonumber(v))
if type(k) == "string" then
return k
end
end
---@generic T
---@param a T
---@param _prev T?
---@param _next T?
function table_util.insert_linked(a, _prev, _next)
a.prev, a.next = nil, nil
if _prev then
_prev.next = a
a.prev = _prev
end
if _next then
_next.prev = a
a.next = _next
end
end
---@generic T
---@param a T
---@return T?
---@return T?
function table_util.remove_linked(a)
local prev, next = a.prev, a.next
if prev then prev.next = next end
if next then next.prev = prev end
a.prev, a.next = nil, nil
return prev, next
end
---@generic T
---@param t T[]
---@param pk string?
---@param nk string?
---@return T
function table_util.to_linked(t, pk, nk)
pk = pk or "prev"
nk = nk or "next"
for i = 1, #t do
t[i][pk] = t[i - 1]
t[i][nk] = t[i + 1]
end
return t[1]
end
---@generic T
---@param head T
---@param unlink boolean?
---@param pk string?
---@param nk string?
---@return T[]
function table_util.to_array(head, unlink, pk, nk)
pk = pk or "prev"
nk = nk or "next"
local t = {}
local i = 0
while head do
i = i + 1
t[i] = head
local _next = head[nk]
if unlink then
head[pk] = nil
head[nk] = nil
end
head = _next
end
return t
end
---@generic K, V
---@param t {[K]: V}
---@param k K
---@param f fun(...: any?): V
---@param ... any?
---@return V
function table_util.get_or_create(t, k, f, ...)
local v = t[k]
if v then
return v
end
v = f(...)
t[k] = v
return v
end
---@param ... any
---@return any
function table_util.remove_holes(...)
if select("#", ...) == 0 then
return
end
if select(1, ...) == nil then
return table_util.remove_holes(select(2, ...))
end
return select(1, ...), table_util.remove_holes(select(2, ...))
end
assert(table.concat({table_util.remove_holes(nil, 1, nil, 2, nil)}) == "12")
return table_util