-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtable_util.lua
More file actions
588 lines (533 loc) · 11.2 KB
/
table_util.lua
File metadata and controls
588 lines (533 loc) · 11.2 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
local table_util = {}
table_util.new = require("table.new")
table_util.clear = require("table.clear")
---@generic T: table
---@param t T
---@param keys any[]
---@return T
function table_util.sub(t, keys)
---@type {[any]: any}
local _t = {}
for _, k in ipairs(keys) do
_t[k] = t[k]
end
return _t
end
---@param a {[any]: any}
---@param b {[any]: any}
---@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 {[any]: any}
---@param b {[any]: any}
---@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 ta table
---@param tb table
---@param keys string[]
---@param t_eq (fun(a: table, b: table): boolean)?
---@return boolean
function table_util.subequal(ta, tb, keys, t_eq)
for _, k in ipairs(keys) do
---@type any, any
local a, b = ta[k], tb[k]
if t_eq and type(a) == "table" and type(b) == "table" then
if not t_eq(a, b) then
return false
end
elseif a ~= b then
return false
end
end
return true
end
assert(not table_util.subequal({a = 1, b = 3, 0}, {a = 1, b = 2}, {"a", "b"}))
assert(table_util.subequal({a = 1, b = 2, 0}, {a = 1, b = 2}, {"a", "b"}))
assert(not table_util.subequal({a = {1, 2}, 0}, {a = {1, 2}}, {"a"}))
assert(not table_util.subequal({a = {1, 2}, 0}, {a = {1, 3}}, {"a"}, table_util.deepequal))
assert(table_util.subequal({a = {1, 2}, 0}, {a = {1, 2}}, {"a"}, table_util.deepequal))
---@generic T: table
---@param src T?
---@param dst T?
---@return T
function table_util.copy(src, dst)
if not src then
return {}
end
---@type {[any]: any}
dst = dst or {}
---@cast src {[any]: any}
for k, v in pairs(src) do
dst[k] = v
end
return dst
end
---@generic T: table
---@param t T
---@return T
function table_util.deepcopy(t)
if type(t) ~= "table" then
return t
end
---@type {[any]: any}
local out = {}
---@cast t {[any]: any}
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
---@generic T
---@param new T[]
---@param old T[]
---@param new_f (fun(v: T): T)?
---@param old_f (fun(v: T): T)?
---@return T[]
---@return T[]
---@return T[]
function table_util.array_update(new, old, new_f, old_f)
---@cast new any[]
---@cast old any[]
---@type {[any]: true}
local _new = {}
for _, v in ipairs(new) do
if new_f then v = new_f(v) end
_new[v] = true
end
---@type {[any]: true}
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
---@generic T
---@param new T[]
---@param old T[]
---@return T[]
---@return T[]
function table_util.array_update2(new, old)
---@cast new any[]
---@cast old any[]
---@type {[any]: true}
local _new = {}
for _, v in ipairs(new) do
_new[v] = true
end
---@type {[any]: true}
local _old = {}
for _, v in ipairs(old) do
_old[v] = true
end
---@type any[]
new = {}
for v in pairs(_new) do
if not _old[v] then
table.insert(new, v)
end
end
---@type any[]
old = {}
for v in pairs(_old) do
if not _new[v] then
table.insert(old, v)
end
end
return new, old
end
---@param t table
---@param key (string|[string, function])[]|string?
---@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
---@type any
subvalue = subvalue[subkey]
end
return subvalue
end
end
---@generic T
---@param t T[]
---@param i integer?
---@param j integer?
---@return T? ...
---@nodiscard
function table_util.unpack(t, i, j)
i = i or 1
j = j or t.n or #t
if i > j then
return
end
return t[i], unpack(t, i + 1, j)
end
assert(table_util.equal({table_util.unpack({1, 2, 3})}, {1, 2, 3}))
assert(table_util.equal({table_util.unpack({1, 2, 3}, 2, 2)}, {2}))
---@generic T
---@param ... T?
---@return {n: integer, [integer]: T}
function table_util.pack(...)
return {n = select("#", ...), ...}
end
---@generic T: function
---@param f T
---@param index number?
---@return T
function table_util.cache(f, index)
---@type {[any]: any[]}
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
---@generic T
---@param t T[]
---@param v T
---@param f (fun(v: T): T)?
---@return number?
function table_util.indexof(t, v, f)
---@cast t any[]
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
---@generic T
---@param t T[]
---@param f fun(v: T): boolean
---@return T?
function table_util.find(t, f)
for i, v in ipairs(t) do
if f(v) then
return v
end
end
end
---@generic K, V
---@param t {[K]: V}
---@param v V
---@param f (fun(v: V): V)?
---@return K?
function table_util.keyof(t, v, f)
---@cast t {[any]: any}
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)
---@cast t {[any]: any}
---@type {[any]: any}
local _t = {}
for k, v in pairs(t) do
assert(not _t[v], "duplicate value '" .. tostring(v) .. "'")
_t[v] = k
end
return _t
end
---@generic T
---@param t T[]
---@param append T[]
---@return T[]
function table_util.append(t, append)
---@cast t {[any]: any}
---@cast append {[any]: any}
for i, v in ipairs(append) do
table.insert(t, v)
end
return t
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
---@class table_util.LinkedNode
---@field prev table_util.LinkedNode?
---@field next table_util.LinkedNode?
---@generic T
---@param a T
---@param _prev T?
---@param _next T?
function table_util.insert_linked(a, _prev, _next)
---@cast a table_util.LinkedNode
---@cast _prev table_util.LinkedNode
---@cast _next table_util.LinkedNode
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)
---@cast a table_util.LinkedNode
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[]
---@return T
function table_util.to_linked(t)
---@cast t table_util.LinkedNode[]
for i = 1, #t do
t[i].prev = t[i - 1] ---@diagnostic disable-line: no-unknown
t[i].next = t[i + 1] ---@diagnostic disable-line: no-unknown
end
return t[1]
end
---@generic T
---@param head T
---@param unlink boolean?
---@return T[]
function table_util.to_array(head, unlink)
---@cast head table_util.LinkedNode
---@type any[]
local t = {}
local i = 0
while head do
i = i + 1
t[i] = head
local _next = head.next
if unlink then
head.prev = nil
head.next = 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
---@type any
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")
---@param graph {[any]: any[]}
---@return {[any]: any[]}
function table_util.invert_graph(graph)
---@type {[any]: any[]}
local _graph = {}
for vert, verts in pairs(graph) do
for _, _vert in ipairs(verts) do
_graph[_vert] = _graph[_vert] or {}
table.insert(_graph[_vert], vert)
end
end
return _graph
end
---@param t any[]
---@param size integer
---@return any[][]
function table_util.slices(t, size)
---@type any[][]
local slices = {}
local count = math.ceil(#t / size)
for i = 1, count do
slices[i] = table.move(t, size * (i - 1) + 1, size * i, 1, {})
end
return slices
end
assert(table_util.deepequal(
table_util.slices({1, 2, 3, 4, 5, 6}, 2),
{{1, 2}, {3, 4}, {5, 6}}
))
assert(table_util.deepequal(
table_util.slices({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 3),
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10}}
))
---@generic T
---@param t {[T]: any}
---@return T[]
function table_util.keys(t)
---@cast t {[any]: any}
local keys = {}
for k in pairs(t) do
table.insert(keys, k)
end
return keys
end
---@param t table
---@param f type|fun(v: any): boolean
---@return boolean
function table_util.is_array_of(t, f)
if type(t) ~= "table" then
return false
end
if type(f) == "string" then
local _f = f
f = function(v) return type(v) == _f end
end
---@cast t {[any]: [any]}
local max_key = 0
local count = 0
for k, v in pairs(t) do
if type(k) ~= "number" or k ~= math.floor(k) or k <= 0 or not f(v) then
return false
end
count = count + 1
max_key = math.max(max_key, k)
end
if count ~= max_key then
return false
end
return true
end
assert(table_util.is_array_of({{1}}, function(v) return not not v[1] end))
assert(table_util.is_array_of({"q"}, "string"))
assert(table_util.is_array_of({1, 2, 3}, "number"))
assert(not table_util.is_array_of({[0] = 1, 2, 3}, "number"))
assert(not table_util.is_array_of({t = 1, 2, 3}, "number"))
assert(not table_util.is_array_of({1, nil, 3}, "number"))
---@param n integer
---@param m integer?
---@return integer[]
function table_util.range(n, m)
---@type integer[]
local t = {}
if not m then
n, m = 1, n
end
for i = n, m do
t[i - n + 1] = i
end
return t
end
assert(table_util.equal(table_util.range(3), {1, 2, 3}))
assert(table_util.equal(table_util.range(2, 4), {2, 3, 4}))
---@generic T
---@param ... T
---@return fun(a: T, b: T): boolean
function table_util.sortby(...)
local t = {...}
return function(a, b)
for i = 1, #t do
local ti = t[i]
if a[ti] ~= b[ti] then
return a[ti] < b[ti]
end
end
end
end
return table_util