-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtableExtend.lua
1170 lines (1089 loc) · 27.1 KB
/
tableExtend.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
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local rnd,floor=math.random,math.floor
local gsub,match,gmatch=string.gsub,string.match,string.gmatch
local rem=table.remove
local next,type,select=next,type,select
---@class Zenitha.TableExt
local TABLE={}
for k,v in next,table do TABLE[k]=v end
--------------------------------------------------------------
-- Builder
---Create a new filled table
---
---You can also use `TABLE.newSize`, which is alias of `table.new` from luajit:
---https://luajit.org/extensions.html
---@generic V
---@param val V value to fill
---@param count integer how many elements
---@return V[]
---@nodiscard
function TABLE.new(val,count)
local L={}
for i=1,count do
L[i]=val
end
return L
end
---Create a new table with specific size allocated (from luajit)
---
---Fallback to `return {}` if failed to require `table.new`
---@param nArray? integer the size of "list part" of the table
---@param nHash? integer the size of "hash part" of the table
---@return table
---@nodiscard
---@diagnostic disable-next-line
function TABLE.newSize(nArray,nHash) return {} end
pcall(function() TABLE[('newSize')]=require'table.new' end)
---Create a new filled matrix
---@generic V
---@param val V value to fill
---@param height integer
---@param width integer
---@return Mat<V>
---@nodiscard
function TABLE.newMat(val,height,width)
local L={}
for y=1,height do
L[y]={}
for x=1,width do
L[y][x]=val
end
end
return L
end
---Create the subset list of a list, like string.sub
---
---leave `start&stop` as `nil` will simply copy
---@generic T
---@param org T original table
---@param start? integer start pos (default 1)
---@param stop? integer end pos (default #org)
---@return T
---@nodiscard
function TABLE.sub(org,start,stop)
if not start then start=1 end
local L={}
for i=0,(stop or #org)-start do
L[i+1]=org[start+i]
end
return L
end
---Create a copy of [1~#] elements
---@generic V
---@param org V[] original table
---@param depth? integer how many layers will be recreate, default to inf
---@return V[]
---@nodiscard
function TABLE.copy(org,depth)
if not depth then depth=1e99 end
local L={}
for i=1,#org do
if type(org[i])=='table' and depth>0 then
L[i]=TABLE.copy(org[i],depth-1)
else
L[i]=org[i]
end
end
return L
end
---Create a full copy of org, depth = how many layers will be recreate, default to inf
---@generic T
---@param org T original table
---@param depth? integer how many layers will be recreate, default to inf
---@return T
---@nodiscard
function TABLE.copyAll(org,depth)
if not depth then depth=1e99 end
local L={}
for k,v in next,org do
if type(v)=='table' and depth>0 then
L[k]=TABLE.copyAll(v,depth-1)
else
L[k]=v
end
end
return L
end
---Get a new table which keys and values are swapped
---@generic K, V
---@param org {[K]:V}
---@return {[V]:K}
function TABLE.inverse(org)
local T={}
for k,v in next,org do
T[v]=k
end
return T
end
---Get keys of a table as a list
---@generic K
---@param org {[K]:any}
---@return K[]
---@nodiscard
function TABLE.getKeys(org)
local L={}
local n=0
for k in next,org do
n=n+1
L[n]=k
end
return L
end
---Get values of a table as a list
---@generic V
---@param org {[any]:V}
---@return V[]
---@nodiscard
function TABLE.getValues(org)
local L={}
local n=0
for _,v in next,org do
n=n+1
L[n]=v
end
return L
end
---Set all values to k
---@generic T1, T2
---@param org {[any]:T1}
---@param val? T2 default to `true`
---@return {[T1]:T2}
---@nodiscard
function TABLE.getValueSet(org,val)
if val==nil then val=true end
local T={}
for _,v in next,org do
T[v]=val
end
return T
end
---**Create** a table of two lists combined
---For **Appending** a table, use `TABLE.append`
---@generic T1, T2
---@param L1 T1[] list 1
---@param L2 T2[] list 2
---@return (T1 | T2)[]
---@nodiscard
function TABLE.combine(L1,L2)
local L={}
local l0=#L1
for i=1,l0 do L[i]=L1[i] end
for i=1,#L2 do L[l0+i]=L2[i] end
return L
end
---Transpose a matrix
---@generic V
---@param matrix Mat<V>
---@return Mat<V>
function TABLE.transpose(matrix)
if #matrix==0 then return matrix end
local w,h=#matrix[1],#matrix
if w>h then
for y=h+1,w do matrix[y]={} end
for y=2,w do
for x=1,y-1 do
matrix[y][x],matrix[x][y]=matrix[x][y],matrix[y][x]
end
end
else
for y=2,h do
for x=1,y-1 do
matrix[y][x],matrix[x][y]=matrix[x][y],matrix[y][x]
end
end
for y=h+1,w do matrix[y]=nil end
end
return matrix
end
---Create a transposed copy of a matrix
---This one is faster then `TABLE.transpose` but creates new table
---@generic V
---@param matrix Mat<V>
---@return Mat<V>
---@nodiscard
function TABLE.transposeNew(matrix)
local newMat={}
for y=1,#matrix[1] do
newMat[y]={}
for x=1,#matrix do
newMat[y][x]=matrix[x][y]
end
end
return newMat
end
---Create a rotated copy of a matrix
---@generic V
---@param matrix Mat<V>
---@param direction 'R' | 'L' | 'F' | '0' CW, CCW, 180 deg, 0 deg (copy)
---@return Mat<V>
---@nodiscard
function TABLE.rotateNew(matrix,direction)
local iMat={}
if direction=='R' then -- Rotate CW
for y=1,#matrix[1] do
iMat[y]={}
for x=1,#matrix do
iMat[y][x]=matrix[x][#matrix[1]-y+1]
end
end
elseif direction=='L' then -- Rotate CCW
for y=1,#matrix[1] do
iMat[y]={}
for x=1,#matrix do
iMat[y][x]=matrix[#matrix-x+1][y]
end
end
elseif direction=='F' then -- Rotate 180 degree
for y=1,#matrix do
iMat[y]={}
for x=1,#matrix[1] do
iMat[y][x]=matrix[#matrix-y+1][#matrix[1]-x+1]
end
end
elseif direction=='0' then -- No rotation, just copy
for y=1,#matrix do
iMat[y]={}
for x=1,#matrix[1] do
iMat[y][x]=matrix[y][x]
end
end
else
errorf("TABLE.rotateNew: Invalid rotate direction '%s'",direction)
end
return iMat
end
TABLE.rotate=TABLE.rotateNew
--------------------------------------------------------------
-- Set operation
---Check if two table have same elements in [1~#]
---@param a any[]
---@param b any[]
---@return boolean
---@nodiscard
function TABLE.equal(a,b)
if #a~=#b then return false end
if a==b then return true end
for i=1,#a do
if a[i]~=b[i] then return false end
end
return true
end
---Check if two whole table have same elements
---**Warning**: won't check whether two table have same keys of hash part
---@param a table
---@param b table
---@return boolean
---@nodiscard
function TABLE.equalAll(a,b)
if #a~=#b then return false end
if a==b then return true end
for k,v in next,a do
if b[k]~=v then return false end
end
return true
end
---**Append** [1~#] elements of new to the end of org
---For **Creating** a new table, use `TABLE.combine`
---@generic T1, T2
---@param org T1[] original list
---@param new T2[] new list
---@return (T1 | T2)[]
function TABLE.append(org,new)
local l0=#org
for i=1,#new do
org[l0+i]=new[i]
end
return org
end
---Delete items in [1~#] of org which also in [1~#] of sub
---@generic V
---@param org V[]
---@param sub table
---@return V[]
function TABLE.subtract(org,sub)
for i=#org,1,-1 do
for j=#sub,1,-1 do
if org[i]==sub[j] then
rem(org,i)
break
end
end
end
return org
end
---Delete all items in org which also in sub
---@generic T
---@param org T
---@param sub table
---@return T
function TABLE.subtractAll(org,sub)
for _,v in next,sub do
while true do
local p=TABLE.findAll(org,v)
if p then
rem(org,p)
else
break
end
end
end
return org
end
---Update old table with new table (recursive when both table type and below specifiled depth)
---@generic T
---@param new T
---@param old table
---@param depth? integer how many layer will be entered, default to inf
---@return T
function TABLE.update(old,new,depth)
if not depth then depth=1e99 end
for k,v in next,new do
if type(v)=='table' and type(old[k])=='table' and depth>0 then
TABLE.update(old[k],v,depth-1)
else
old[k]=v
end
end
return old
end
---Update old table with new table when same type (recursive)
---@generic T
---@param old T
---@param new table
---@return T
function TABLE.updateType(old,new)
for k,v in next,new do
if type(v)==type(old[k]) then
if type(v)=='table' then
TABLE.updateType(old[k],v)
else
old[k]=v
end
end
end
return old
end
---Update old table with new table when no value (recursive)
---@generic T
---@param old T
---@param new table
---@return T
function TABLE.updateMissing(old,new)
for k,v in next,new do
if type(v)=='table' then
if old[k]==nil then old[k]={} end
TABLE.updateMissing(old[k],v)
elseif old[k]==nil then
old[k]=v
end
end
return old
end
--------------------------------------------------------------
-- Editing
---`table.clear` from luajit extension, clear the whole table but preserve the allocated array/hash sizes
---
---Fallback to `TABLE.clearAll` if failed to require `table.clear`
---
---@param org table
function TABLE.clear(org)
for k in next,org do
org[k]=nil
end
end
pcall(function() TABLE[('clear')]=require'table.clear' end)
---Clear whole table (pure lua implementation)
---
---Recommend to use `TABLE.clear` instead
---@generic T
---@param org T
---@return T
function TABLE.clearAll(org)
for k in next,org do
org[k]=nil
end
return org
end
---Clear [1~#] of a table (pure lua implementation)
---@generic V
---@param org V[]
---@return V[]
function TABLE.clearList(org)
for i=1,#org do
org[i]=nil
end
return org
end
---Clear whole table but keep the tree structure
---@generic T
---@param org T
---@return T
function TABLE.clearRecursive(org)
for k,v in next,org do
if type(v)=='table' then
TABLE.clearRecursive(v)
else
org[k]=nil
end
end
return org
end
---Remove a specific value of [1~#]
---@generic V
---@param org V[]
---@return V[]
function TABLE.delete(org,value)
for i=#org,1,-1 do
if org[i]==value then
rem(org,i)
end
end
return org
end
---Remove a specific value in whole table
---@generic T
---@param org T
---@return T
function TABLE.deleteAll(org,value)
for k,v in next,org do
if v==value then
org[k]=nil
end
end
return org
end
---Remove duplicated value of [1~#]
---@generic V
---@param org V[]
---@return V[]
function TABLE.removeDuplicate(org)
local cache={}
local len=#org
local i=1
while i<=len do
if cache[org[i]] then
rem(org,i)
len=len-1
else
cache[org[i]]=true
i=i+1
end
end
return org
end
---Remove duplicated value in whole table
---@generic T
---@param org T
---@return T
function TABLE.removeDuplicateAll(org)
local cache={}
for k,v in next,org do
if cache[v] then
org[k]=nil
else
cache[v]=true
end
end
return org
end
---Reverse [1~#]
---@generic V
---@param org V[]
---@return V[]
function TABLE.reverse(org)
local l=#org
for i=1,floor(l/2) do
org[i],org[l+1-i]=org[l+1-i],org[i]
end
return org
end
---Get random [1~#] of table
---@generic V
---@param org V[]
---@return V
---@nodiscard
function TABLE.getRandom(org)
local l=#org
if l>0 then
return org[rnd(l)]
else
error("TABLE.popRandom(org): Table is empty")
end
end
---Remove & return random [1~#] of table (not really "pop"!)
---@generic V
---@param org V[]
---@return V
---@nodiscard
function TABLE.popRandom(org)
local l=#org
if l>0 then
local r=rnd(l)
r,org[r]=org[r],org[l]
org[l]=nil
return r
else
error("TABLE.popRandom(org): Table is empty")
end
end
---Shuffle [1~#]
---@generic V
---@param org V[]
---@return V[]
function TABLE.shuffle(org)
for i=#org,2,-1 do
local r=rnd(i)
org[i],org[r]=org[r],org[i]
end
return org
end
---Re-index string value as key
---### Example
---```
---local t={a=print,b='a'}
---TABLE.reIndex(t)
---t.b('Hello Zenitha')
---```
---@generic T
---@param org T
---@return T
function TABLE.reIndex(org)
for k,v in next,org do
if type(v)=='string' then
org[k]=org[v]
end
end
return org
end
---Flatten a nested table to a flat table (no table type value included)
---### Example
---```
---local T={a=1,b={c=2},d={e={f=3}}}
---TABLE.flatten(T)
-----[[ Now T is
---{
--- ['a']=1,
--- ['b/c']=2
--- ['d/e/f']=3
---}
---]]
---```
---@param org table
---@param depth? integer how many layer will be entered and flattened, default to inf
---@return table
function TABLE.flatten(org,depth)
if not depth then depth=1e99 end
while depth>0 do
local tKeyList={}
for k,v in next,org do
if type(v)=='table' then
table.insert(tKeyList,k)
end
end
if #tKeyList==0 then return org end
for i=1,#tKeyList do
local key=tKeyList[i]
for k,v in next,org[key] do
org[key..'/'..k]=v
end
org[key]=nil
end
depth=depth-1
end
return org
end
--------------------------------------------------------------
-- Find & Replace
---Find value in [1~#], like string.find
---@param org any[]
---@param val any
---@param start? integer
---@return integer? key
---@nodiscard
function TABLE.find(org,val,start)
for i=start or 1,#org do if org[i]==val then return i end end
end
---TABLE.find for ordered list only, faster (binary search)
---@param org any[]
---@param val any
---@return integer | nil key
---@nodiscard
function TABLE.findOrdered(org,val)
if val<org[1] or val>org[#org] then return nil end
local i,j=1,#org
while i<=j do
local m=floor((i+j)/2)
if org[m]>val then
j=m-1
elseif org[m]<val then
i=m+1
else
return m
end
end
end
---Find value in whole table
---@generic K, V
---@param org {[K]:V}
---@param val V
---@return K | nil key
---@nodiscard
function TABLE.findAll(org,val)
for k,v in next,org do if v==val then return k end end
return nil
end
---Replace value in [1~#], like string.gsub
---@generic T1, T2
---@param org T1[]
---@param v_old T1
---@param v_new T2
---@param start? integer
---@return (T1 | T2)[]
function TABLE.replace(org,v_old,v_new,start)
for i=start or 1,#org do
if org[i]==v_old then
org[i]=v_new
end
end
return org
end
---Replace value in whole table
---@generic K, V1, V2
---@param org {[K]:V1}
---@param v_old V1
---@param v_new V2
---@return {[K]:V1|V2}
function TABLE.replaceAll(org,v_old,v_new)
for k,v in next,org do
if v==v_old then
org[k]=v_new
end
end
return org
end
---Find the minimum value (and key)
---if you don't need the key and the list is short, use `math.min(unpack(t))` for better performance
---@generic V
---@param org V[]
---@return V | number minVal, integer | nil key `minVal` will be inf when empty
---@nodiscard
function TABLE.min(org)
local min,key=MATH.inf,nil
for i=1,#org do
if org[i]<min then
min,key=org[i],i
end
end
return min,key
end
---Find the minimum value (and key) in whole table
---@generic K, V
---@param org {[K]:V}
---@return V | number minVal, K | nil key `minVal` will be inf when empty
---@nodiscard
function TABLE.minAll(org)
local min,key=MATH.inf,nil
for k,v in next,org do
if v<min then
min,key=v,k
end
end
return min,key
end
---Find the maximum value (and key)
---if you don't need the key and the list is short, use `math.max(unpack(t))` for better performance
---@generic V
---@param org V[]
---@return V | number maxVal, integer | nil key `maxVal` will be -inf when empty
---@nodiscard
function TABLE.max(org)
local max,key=-MATH.inf,nil
for i=1,#org do
if org[i]>max then
max,key=org[i],i
end
end
return max,key
end
---Find the maximum value (and key) in whole table
---@generic K, V
---@param org {[K]:V}
---@return V | number maxVal, K | nil key `maxVal` will be -inf when empty
---@nodiscard
function TABLE.maxAll(org)
local max,key=-MATH.inf,nil
for k,v in next,org do
if v>max then
max,key=v,k
end
end
return max,key
end
--------------------------------------------------------------
-- Dump
do -- function TABLE.dumpDeflate(org,depth)
local function dump(L,t,lim)
local s='{'
local count=1
for k,v in next,L do
-- Key part
local T=type(k)
if T=='number' then
if k==count then
k='' -- List part, no brackets needed
count=count+1
else
k='['..k..']='
end
elseif T=='string' then
if match(k,'^[^a-zA-Z_]') then
k='["'..gsub(k,'"',[[\"]])..'"]='
else
k=k..'='
end
elseif T=='boolean' then
k='['..k..']='
else
k='["*'..tostring(k)..'"]='
end
-- Value part
T=type(v)
if T=='number' or T=='boolean' then
v=tostring(v)
elseif T=='string' then
v='"'..gsub(v,'"',[[\"]])..'"'
elseif T=='table' then
if t>=lim then v=tostring(v) else v=dump(v,t+1,lim) end
else
v='*'..tostring(v)
end
s=s..k..v..','
end
return s..'}'
end
---Dump a simple lua table (no whitespaces)
---@param org table
---@param depth? integer how many layers will be dumped, default to inf
---@return string
---@nodiscard
function TABLE.dumpDeflate(org,depth)
assert(type(org)=='table',"TABLE.dumpDeflate: need table")
return dump(org,1,depth or 1e99)
end
end
do -- function TABLE.dump(org,depth)
local tabs=setmetatable({[0]='','\t'},{
__index=function(self,k)
if k>=260 then error("TABLE.dump(org,depth): Table depth over 260") end
for i=#self+1,k do
self[i]=self[i-1]..'\t'
end
return self[k]
end,
})
local function dump(L,t,lim)
local s='{\n'
local count=1
for k,v in next,L do
-- Key part
local T=type(k)
if T=='number' then
if k==count then
k='' -- List part, no brackets needed
count=count+1
else
k='['..k..']='
end
elseif T=='string' then
if match(k,'^[^a-zA-Z_]') then
k='["'..gsub(k,'"',[[\"]])..'"]='
else
k=k..'='
end
elseif T=='boolean' then
k='['..k..']='
else
k='["*'..tostring(k)..'"]='
end
-- Value part
T=type(v)
if T=='number' or T=='boolean' then
v=tostring(v)
elseif T=='string' then
v='"'..gsub(v,'"',[[\"]])..'"'
elseif T=='table' then
if t>=lim then v=tostring(v) else v=dump(v,t+1,lim) end
else
v='*'..tostring(v)
end
s=s..tabs[t]..k..v..',\n'
end
return s..tabs[t-1]..'}'
end
---Dump a simple lua table
---@param org table
---@param depth? integer how many layers will be dumped, default to inf
---@return string
---@nodiscard
function TABLE.dump(org,depth)
assert(type(org)=='table',"TABLE.dump: need table")
return dump(org,1,depth or 1e99)
end
end
--------------------------------------------------------------
-- Information
---Get element count of table
---@param org table
---@return integer
---@nodiscard
function TABLE.getSize(org)
local size=0
for _ in next,org do size=size+1 end
return size
end
---Count value repeating time in [1~#]
---@param org any[]
---@param val any
---@return integer
---@nodiscard
function TABLE.count(org,val)
local count=0
for i=1,#org do
if org[i]==val then
count=count+1
end
end
return count
end
---Count value repeating time in whole table
---@param org table
---@param val any
---@return integer
---@nodiscard
function TABLE.countAll(org,val)
local count=0
for _,v in next,org do
if v==val then
count=count+1
end
end
return count
end
---Return next value of [1~#] (by value), like _G.next
---Return t[1] if val is nil
---@generic K, V
---@param org {[K]:V}
---@param val V
---@return V | nil nextValue nil when not found
---@nodiscard
function TABLE.next(org,val)
if val==nil then return org[1] end
for i=1,#org do if org[i]==val then return org[i+1] end end
return nil
end
---Return previous value of [1~#] (by value), like TABLE.next but reversed
---Return t[#t] if val is nil
---@generic K, V
---@param org {[K]:V}
---@param val V
---@return V | nil prevValue nil when not found
---@nodiscard
function TABLE.prev(org,val)
if val==nil then return org[#org] end
for i=#org,1,-1 do if org[i]==val then return org[i-1] end end
return nil
end
--------------------------------------------------------------
-- (Utility) Foreach
---Execute func(table[i],i) in [1~#], and optional element removing
---@generic V
---@param org V[]
---@param f fun(v:V, i:integer): boolean return `true` to remove element (do this in reverse mode for better performance)
---@param rev? boolean Reverse the iterating order
---@return V[]
function TABLE.foreach(org,f,rev)
if rev then
for i=#org,1,-1 do
if f(org[i],i) then
rem(org,i)
end
end
else
local remCount=0
for i=1,#org do
if f(org[i-remCount],i) then
rem(org,i-remCount)
remCount=remCount+1
end
end
end
return org
end
---Execute func(table[k],k) in whole table, and optional element removing
---@generic K, V
---@param org {[K]:V}
---@param f fun(v:V, k:K): boolean return `true` to remove element (**Warning**: Won't shrink the list part when removing list element)
---@return {[K]:V}
function TABLE.foreachAll(org,f)
for k,v in next,org do
if f(v,k) then org[k]=nil end
end
return org
end
---Apply a function to value in [1~#]
---@generic V1, V2
---@param org V1[]