-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcommon.lua
1159 lines (1031 loc) · 35.4 KB
/
common.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
-- Copyright (C) 2017 Deyan Dobromirov
-- A common functionalities library
local os = os
local math = math
local type = type
local next = next
local pcall = pcall
local table = table
local pairs = pairs
local select = select
local tonumber = tonumber
local tostring = tostring
local getmetatable = getmetatable
local common = {}
local metaCommon = {}
if not debug.getinfo(3) then
print("This is a module to load with `local common = require('common')`.")
os.exit(1)
end
metaCommon.__time = 0
metaCommon.__clok = 0
metaCommon.__func = {}
metaCommon.__sort = {}
metaCommon.__marg = 1e-10
metaCommon.__prct = {">","|"}
metaCommon.__fmdr = "%s?.lua"
metaCommon.__fmtb = "[%s]:%d {%s} [%d]<%s>[%s](%d)"
metaCommon.__type = {"number", "boolean", "string", "function", "table", "nil", "userdata"}
metaCommon.__syms = "1234567890abcdefghijklmnopqrstuvwxyxABCDEFGHIJKLMNOPQRSTUVWXYZ"
metaCommon.__metatable = "common.lib"
metaCommon.__nlog = {__top = 0}
metaCommon.__rmod = {{"*all" , "Reads the whole file"},
{"*line" , "Reads the next line (default)"},
{"*number", "Reads a number"},
["*all"]=true,["*line"]=true,["*number"]=true}
metaCommon.__func["pi"] = {}
metaCommon.__func["pi"].foo = function (itr, top)
if(top == itr) then return 1 end
local bs, nu = ((2 * itr) + 1), ((itr + 1) ^ 2)
return bs + nu / metaCommon.__func["pi"].foo(itr+1, top)
end
metaCommon.__func["pi"].out = function(itr)
return (4 / metaCommon.__func["pi"].foo(0, itr))
end
metaCommon.__func["exp"] = {}
metaCommon.__func["exp"].foo = function (itr, top)
if(top == itr) then return 1 end; local fac = 1
for I = 1, itr do fac = fac * I end
return (1/fac + metaCommon.__func["exp"].foo(itr+1, top))
end
metaCommon.__func["exp"].out = function(itr)
return metaCommon.__func["exp"].foo(1, itr)
end
metaCommon.__func["phi"] = {}
metaCommon.__func["phi"].foo = function (itr, top)
if(top == itr) then return 1 end
return (1 + (1 / metaCommon.__func["phi"].foo(itr+1, top)))
end
metaCommon.__func["phi"].out = function(itr)
return metaCommon.__func["phi"].foo(0, itr)
end
function common.isNil(nVal)
return (nVal == nil)
end
function common.isNan(nVal)
return (nVal ~= nVal)
end
function common.isInf(nVal)
if(nVal == math.huge) then return true, 1 end
if(nVal == -math.huge) then return true, -1 end
return false
end
function common.isTable(tVal)
return (type(tVal) == metaCommon.__type[5])
end
function common.isDryTable(tVal)
if(not common.isTable(tVal)) then return false end
return (next(tVal) == nil)
end
function common.isString(sVal)
local sTy = metaCommon.__type[3]
return (getmetatable(sTy) == getmetatable(sVal))
end
function common.isDryString(sVal)
if(not common.isString(sVal)) then return false end
return (sVal == "")
end
function common.isNumber(nVal)
if(not tonumber(nVal)) then return false end
if(nil ~= getmetatable(nVal)) then return false end
return (type(nVal) == metaCommon.__type[1])
end
function common.isInteger(nVal)
if(not common.isNumber(nVal)) then return false end
local nW, nF = math.modf(nVal); return (nF == 0)
end
function common.isFunction(fVal)
return (type(fVal) == metaCommon.__type[4])
end
function common.isBool(bVal)
if(bVal == true ) then return true end
if(bVal == false) then return true end
return false
end
function common.isZero(vVal)
return (not ((vVal + vVal) ~= vVal))
end
function common.isType(sT, iD)
return (sT == metaCommon.__type[iD])
end
function common.logSkipAdd(...)
local tArg, tNlg = {...}, metaCommon.__nlog
for key, val in pairs(tArg) do
table.insert(tNlg, tostring(val))
tNlg.__top = tNlg.__top + 1
end
end
function common.logSkipClear(...)
local tNlg = metaCommon.__nlog
if(common.isDryTable(tNlg)) then tNlg.__top = 0
for key, val in pairs(tNlg) do tNlg[key] = nil end
else local tArg = {...}
for key, val in pairs(tArg) do
local sVal = tostring(val); for ind, now in pairs(tNlg) do
if(tostring(now):find(sVal)) then tNlg[ind] = nil end
end
end
end; local nTop = tNlg.__top
while(not tNlg[nTop] and nTop > 0) do nTop = nTop - 1 end
tNlg.__top = nTop; collectgarbage()
end
function common.logString(anyMsg, ...)
local tNlg = metaCommon.__nlog
local nB, nT, sM = 1, tNlg.__top, tostring(anyMsg)
while(nB <= nT) do
local vB, vT = tNlg[nB], tNlg[nT]
if(vB and sM:find(vB)) then return ... end
if(vT and sM:find(vT)) then return ... end
nB, nT = (nB + 1), (nT - 1)
end; io.write(sM); return ...
end
function common.logStatus(anyMsg, ...)
return common.logString(tostring(anyMsg).."\n", ...)
end
function common.logUnpackInfo(tInf)
return tInf.source , tInf.currentline, tInf.what,
tInf.linedefined, tInf.namewhat , tInf.name, tInf.nparams
end
function common.logCallStack(sMsg, ...)
local iLev, sFmt = 1, metaCommon.__fmtb
if(sMsg) then common.logStatus(tostring(sMsg)) end
while(true) do local tInf = debug.getinfo(iLev)
if(not tInf) then break end
if tInf.what ~= "C" then
common.logStatus(sFmt:format(common.logUnpackInfo(tInf)))
end; iLev = iLev + 1
end; return ...
end
function common.logConcat(anyMsg, aDel, ...)
local tPar, tDat = metaCommon.__prct, {...}
local sDel = tostring(aDel or tPar[2])
io.write(tostring(anyMsg)..tPar[1])
for ID = 1, #tDat do
io.write(tostring(tDat[ID] or ""))
if(tDat[ID+1]) then io.write(sDel) end
end; io.write("\n")
end
function common.logCase(bCond, ...)
if(bCond) then common.logConcat(...) end
end
-- http://lua-users.org/wiki/MathLibraryTutorial
function common.randomSetSeed(bL)
local nT = os.time()
if((nT - metaCommon.__time) > 0) then
local nS = tonumber(tostring(nT):reverse():sub(1,6))
if(bL) then common.logStatus("common.randomSetSeed: #"..nS) end
math.randomseed(nS); metaCommon.__seed = nS
metaCommon.__time = nT; return nS
end; return 0
end
function common.randomGetSeed(sS)
return (metaCommon.__seed or 0)
end
function common.randomSetString(sS)
metaCommon.__syms = tostring(sS or "")
end
function common.randomGetNumber(nL, nU, vC)
local iC = math.floor(tonumber(vC) or 0)
for iD = 1, iC do math.random() end
if(nL and nU) then return math.random(nL, nU)
elseif(nL and not nU) then return math.random(nL) end
return math.random()
end
function common.randomGetString(vE, vN)
local iN = math.floor(tonumber(vN) or 0)
local iE = math.floor(tonumber(vE) or 0)
local sS = metaCommon.__syms
local sR, nL = "", sS:len()
for iD = 1, iE do
local rN = common.randomGetNumber(1, nL, iN)
sR = sR..sS:sub(rN, rN)
end; return sR
end
-- Noramalizes spaces in a CSV
function common.stringNormSpaceCSV(sS)
return sS:gsub("(,)(%S)", "%1 %2"):gsub("(%s+)(,)", " %2"):gsub("%s+,", ",")
end
-- Returns true when string includes atleast one letter
function common.stringHasLetter(sS)
return (sS:find("%a") ~= nil)
end
-- Returns true when charactters are not affected by upper
function common.stringIsUpper(sS)
return (sS:upper() == sS)
end
-- Returns true when charactters are not affected by lower
function common.stringIsLower(sS)
return (sS:lower() == sS)
end
function common.stringImplode(tLst, sDel)
local ID, sStr, sDel = 1, "", tostring(sDel or "")
while(tLst and tLst[ID]) do sStr = sStr..tLst[ID]; ID = ID + 1
if(tLst[ID] and not common.isDryString(sDel)) then sStr = sStr..sDel end
end; return sStr
end
function common.stringExplode(sStr, sDel)
local sDel = tostring(sDel or " ")
local tLst, sC, iDx, ID, dL = {""}, "", 1, 1, (sDel:len()-1)
while(sC) do sC = sStr:sub(iDx, iDx + dL)
if(common.isDryString(sC)) then return tLst
elseif(sC == sDel) then ID = ID + 1; tLst[ID], iDx = "", (iDx + dL)
else tLst[ID] = tLst[ID]..sC:sub(1,1) end; iDx = iDx + 1
end; return tLst
end
function common.stringExplodePattern(sStr, sPat)
local sPat, tLst, ID = tostring(sPat or " "), {sStr}, 1
local bM, nB, nE = false, tLst[ID]:find(sPat)
if(common.isDryString(sPat)) then -- Modify pattern
bM, sPat, nB, nE = true, ".", 1, 1 -- Trigger flag
end -- Empty string can be found at every position
while(nB and nE and nE >= nB and nB > 0) do
tLst[ID + 1] = tLst[ID]:sub(nE + 1, -1)
tLst[ID] = tLst[ID]:sub(1, nB - (bM and 0 or 1))
ID = ID + 1; nB, nE = tLst[ID]:find(sPat)
end; if(bM) then table.remove(tLst) end
return tLst
end
function common.stringCenter(sStr, vN, vC, bS)
local nN = common.getClamp(tonumber(vN) or 0, sStr:len())
local sC = tostring(vC or " "):sub(1,1)
nN = ((nN - sStr:len()) / 2); nN = ((nN > 0) and nN or 0)
if(nN > 0) then local nL, nH = math.floor(nN), math.ceil(nN)
if(bS) then return (sC:rep(nL)..sStr..sC:rep(nH))
else return (sC:rep(nH)..sStr..sC:rep(nL)) end
end; return sStr
end
function common.stringTrimL(sStr, sC)
local sC = tostring(sC or "%s")
local sO = sStr:gsub("^"..sC.."*", ""); return sO
end
function common.stringTrimR(sStr, sC)
local sC = tostring(sC or "%s")
local sO = sStr:gsub(sC.."*$", ""); return sO
end
function common.stringTrim(sStr, sC)
local sC = tostring(sC or "%s")
return (sStr:match("^"..sC.."*(.-)"..sC.."*$") or sStr)
end
function common.stringPadR(sS, nL, sC)
return sS..tostring(sC or " "):rep(nL - sS:len())
end
function common.stringPadL(sS, nL, sC)
return tostring(sC or " "):rep(nL - sS:len())..sS
end
function common.stringGetExtension(sSrc)
return sSrc:match("%.([^%.]+)$")
end
function common.stringStripExtension(sSrc)
local nP = sSrc:match(".+()%.%w+$")
if(nP) then return sSrc:sub(1, nP-1) end
return sSrc
end
function common.stringGetFilePath(sSrc)
return (sSrc:match("^(.*[/\\])[^/\\]-$") or "")
end
function common.stringGetFileName(sSrc)
if(not (sSrc:find("\\") or sSrc:find("/"))) then return sSrc end
return (sSrc:match("[\\/]([^/\\]+)$") or "")
end
function common.stringGetChunkPath()
local sSrc = debug.getinfo(2).source
return common.stringGetFilePath(sSrc:gsub("@","",1))
end
function common.stringGetFunction(vS, sD)
local iS = math.floor(tonumber(vS) or 0)
local tD = debug.getinfo(common.getClamp(iS, 0))
return (tD and tostring(tD.name or (sD or "")))
end
local function stringParseTableRec(sRc, fCnv, tInfo, nStg)
local sIn = common.stringTrim(tostring(sRc or ""))
if(sIn:sub(1,1)..sIn:sub(-1,-1) ~= "{}") then
return common.logStatus("common.stringTable: Table format invalid <"..sIn..">", false) end
local tIn, tOut = fCnv(common.stringExplode(sIn:sub(2,-2),","), ","), {}
for ID = 1, #tIn do local sVal = common.stringTrim(tIn[ID])
if(not common.isDryString(sVal)) then
local tVal = fCnv(common.stringExplode(sVal,"="), "=")
local kVal, vVal = tVal[1], tVal[2]
if(not vVal) then -- If no key is provided but just value use default integer keys
if(not tInfo[nStg]) then tInfo[nStg] = 0 end
tInfo[nStg] = tInfo[nStg] + 1
kVal, vVal = tInfo[nStg], kVal
end; local skVal = tostring(kVal) -- Handle keys
if(skVal:sub(1,1)..skVal:sub(-1,-1) == "[]") then skVal = skVal:sub(2,-2) end
if(common.isDryString(kVal)) then
return common.logStatus("common.stringTable: Table key fail at <"..vVal..">", false) end
if(skVal:sub(1,1)..skVal:sub(-1,-1) == "\"\"") then kVal = skVal:sub(2,-2)
elseif(tonumber(skVal)) then kVal = tonumber(skVal)
else kVal = skVal end -- Handle values
if(common.isDryString(vVal)) then vVal = nil
elseif(vVal:sub(1,1)..vVal:sub(-1,-1) == "\"\"") then vVal = vVal:sub(2,-2)
elseif(vVal:sub(1,1)..vVal:sub(-1,-1) == "{}") then vVal = stringParseTableRec(vVal, fCnv, tInfo, nStg + 1)
elseif(vVal == "true" or vVal == "false") then vVal = common.toBool(vVal)
else vVal = (tonumber(vVal) or 0) end
-- Write stuff
tOut[kVal] = vVal
end
end; return tOut
end
function common.stringToTable(sRc)
return stringParseTableRec(sRc, function(tIn, sCh)
local aAr, aID, aIN = {}, 1, 0
for ID = 1, #tIn do
local sVal = common.stringTrim(tIn[ID])
if(sVal:find("{")) then aIN = aIN + 1 end
if(sVal:find("}")) then aIN = aIN - 1 end
if(not aAr[aID]) then aAr[aID] = "" end
if(aIN == 0) then aAr[aID] = aAr[aID]..sVal; aID = (aID + 1)
else aAr[aID] = aAr[aID]..sVal..sCh end
end; return aAr
end, {}, 1)
end
function common.stringToNyan(sR)
return sR:gsub("([Nn])(a)", "%1y%2")
end
function common.fileRead(pF, vM, bT)
if(not pF) then
return common.logStatus("common.fileGetLine: No file", "", true) end
local tMd, nM = metaCommon.__rmod, tonumber(vM)
local vMd = common.getPick(nM, nM, tostring(vM or tMd[2][1]))
if(common.isNil(tMd[vMd]) and not nM) then
local sEr = "common.fileRead: Mode missed <"..tostring(vMd)..">"; nM = 1
while(tMd[nM]) do sEr = sEr .."\n"
local com, desc = tMd[nM][1], tMd[nM][2]
com = common.stringPadR("["..com.."]", 9).." > "
sEr = sEr..(" "..com..desc); nM = nM + 1
end; sEr = sEr..("\n "..common.stringPadR("[N]", 9).." > Reads up to N characters")
return common.logStatus(sEr, "", true)
end; local sLn, bEf = pF:read(vMd), false
if(common.isDryString(sLn) and vMd == tMd[1][1]) then return "", true end
if(common.isNil(sLn)) then return "", true end
if(bT) then return common.stringTrim(sLn), bEf end
return sLn, bEf
end
-- https://www.computerhope.com/dirhlp.htm
function common.fileFind(sN, sA)
local tSet, fFoo = metaCommon.__tfil, metaCommon.__ffil
local sNam = sN:gsub("/+","/"):gsub("\\+","/")
local sExt = common.stringGetExtension(sNam)
local sArg = tostring(sA or "")
local sTmp, sMch = os.tmpname(), ("%."..sExt.."$")
os.execute("dir "..sNam:gsub("/","\\").." "..sArg.." >> "..sTmp)
local fT, tO, iD = io.open(sTmp), {}, 0
for line in fT:lines() do
if(line:find(sMch)) then
iD = iD + 1; tO[iD] = {}
tO[iD] = line:sub(37, -1)
end; end; fT:close()
os.execute("del "..sTmp)
return tO
end
function common.isEven(nV)
return ((nV % 2) == 0)
end
function common.isOdd(nV)
return ((nV % 2) ~= 0)
end
function common.getRemap(nV, iH, iL, oH, oL, bR)
if(bR) then
local nK = ((nV - oL) / (oH - oL))
return (nK * (iH - iL) + iL)
else
local nK = ((nV - iL) / (iH - iL))
return (nK * (oH - oL) + oL)
end; return nV
end
function common.getSign(nV)
return ((nV > 0 and 1) or (nV < 0 and -1) or 0)
end
function common.getSignNon(nV)
return ((nV >= 0 and 1) or -1)
end
function common.getSignString(nV)
if(not common.isNumber(nV)) then
return common.logStatus("common.getSignString: Not number", nil) end
return (nV < 0 and "-" or "+")
end
function common.cnvSignString(nV)
local sS = common.getSignString(nV); if(not sS) then
return common.logStatus("common.cnvSignString: Not number", nil) end
return (sS..tostring(math.abs(nV)))
end
function common.getType(o)
local mt = getmetatable(o)
if(mt and mt.__type) then
return tostring(mt.__type)
end; return type(o)
end
-- Defines what should return /false/ when converted to a boolean
local __tobool = {
[0] = true,
["0"] = true,
["false"] = true,
[false] = true
}
-- http://lua-users.org/lists/lua-l/2005-11/msg00207.html
function common.toBool(vAny)
if(common.isNil(vAny)) then return false end
if(__tobool[vAny]) then return false end
return true
end
function common.getPick(bC, vT, vF)
if(bC) then return vT end; return vF
end
-- https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm
function common.getSwitch(vC, ...)
local tV = {...} -- Search and result aruments
local nV, vD = #tV, nil -- Count and defaults
if(nV % 2 ~= 0) then vD = tV[nV]; nV = (nV-1) end
for iD = 1, (nV - 1), 2 do if(vC == tV[iD]) then
return tV[iD + 1] end; end; return vD
end
function common.getValueKeys(tTab, tKeys, aKey)
if(aKey) then return tTab[aKey] end
local out; for ID = 1, #tKeys do
local key = tKeys[ID]; out = (tTab[key] or out)
if(out) then return out, key end
end; return nil
end
function common.getClamp(nN, nL, nH)
if(nL and nN < nL) then return nL end
if(nH and nN > nH) then return nH end
return nN
end
function common.getRoll(nN, nL, nH)
if(nN < nL) then return nH end
if(nN > nH) then return nL end
return nN
end
function common.isAmong(nN, nL, nH)
if(nN < nL) then return false end
if(nN > nH) then return false end
return true
end
function common.isAmongEq(nN, nL, nH)
if(nN <= nL) then return false end
if(nN >= nH) then return false end
return true
end
function common.getRound(nE, nF)
local dF = nF * common.getSign(nE)
if(dF == 0) then return dF end
local q, d = math.modf(nE/dF)
return (dF * (q + (d > 0.5 and 1 or 0)))
end
function common.timeDelay(nD)
if(nD) then local eT = (os.clock() + nD)
while(os.clock() < eT) do end
else while(true) do end end
end
function common.getCall(sNam, ...)
if(not metaCommon.__func[sNam]) then
return common.logStatus("common.getCall: Missed <"..tostring(sNam)..">", nil) end
return pcall(metaCommon.__func[sNam].out, ...)
end
function common.setCall(sNam, fFoo, fOut)
if(metaCommon.__func[sNam]) then
common.logStatus("common.setCall: Replaced <"..tostring(sNam)..">") end
if(not (type(fFoo) == "function")) then
return common.logStatus("common.setCall: Main <"..tostring(sNam)..">", false) end
if(not (type(fOut) == "function")) then
return common.logStatus("common.setCall: Out <"..tostring(sNam)..">", false) end
metaCommon.__func[sNam] = {}
metaCommon.__func[sNam].foo = fFoo
metaCommon.__func[sNam].out = fOut
end
function common.copyItem(obj, ccpy, seen)
if(type(obj) ~= "table") then return obj end
if(seen and seen[obj]) then return seen[obj] end
local c, mt = (ccpy or {}), getmetatable(obj)
-- Copy-constructor linked to the meta table
if(mt) then
if(type(c[mt]) == "function") then
local suc, out = pcall(c[mt], obj); if(suc) then return out end
return common.logStatus("common.copyItem("..tostring(mt).."): "..tostring(out), nil)
elseif(mt.__type) then local mtt = mt.__type
if(type(mtt) == "string" and type(c[mtt]) == "function") then
local suc, out = pcall(c[mtt], obj); if(suc) then return out end
common.logStatus("common.copyItem("..mtt.."): "..tostring(out), nil)
end
end
end
local s, res = (seen or {}), setmetatable({}, mt)
local f = common.copyItem; s[obj] = res
for k, v in pairs(obj) do res[f(k, c, s)] = f(v, c, s) end
return res
end
local function logTableRec(tT, sN, tP, tD)
local tY = metaCommon.__type
local sN, tP = tostring(sN or "Data"), (tP or {})
local vS, vT, vK = type(sN), type(tT), ""
if(vT ~= tY[5]) then
return common.logStatus("{"..vT.."}["..tostring(sN or "Data").."] = <"..tostring(tT)..">", nil) end
if(next(tT) == nil) then
return common.logStatus(sN.." = {}") end; common.logStatus(sN.." = {}", nil)
for k, v in pairs(tT) do
local tk, tv = type(k), type(v)
if(tk== tY[3]) then
vK = sN.."[\""..k.."\"]"
else sK = tostring(k)
if(tP[k]) then sK = tostring(tP[k]) end
vK = sN.."["..sK.."]"
end
if(tv ~= tY[5]) then
if(tv == tY[3]) then
common.logStatus(vK.." = \""..v.."\"")
else sK = tostring(v)
if(tP[v]) then sK = tostring(tP[v]) end
common.logStatus(vK.." = "..sK)
end
else local cT, mT = common.getType(v), getmetatable(v)
if(v == tT) then
common.logStatus(vK.." = "..sN)
elseif(tP[v]) then
common.logStatus(vK.." = "..tostring(tP[v]))
elseif(type(tD) == tY[5] and
(type(tD[cT]) == tY[4] or type(tD[mT]) == tY[4])) then
local vF = common.getPick(tD[cT], tD[cT], tD[mT])
common.logStatus(vK.." = "..vF(v))
else
if(not tP[v]) then tP[v] = vK end
logTableRec(v, vK, tP, tD)
end
end
end
end
--[[ The non-recursive (this) function must be called
tT -> The table to export/printout/log
sS -> A meaningful name for the exported log
tP -> A list of already met values to log correctly pointer to itself
tD -> A string converter to use when a meta table or type is met
]]--
function common.logTable(tT, sS, tP, tD)
local lS, lP = tostring(sS or "Data")
if(tT ~= nil) then lP = {[tT] = lS} end
if(type(tP) == "table" and lP) then
for ptr, abr in pairs(tP) do lP[ptr] = abr end end
logTableRec(tT, lS, lP, tD); return lP
end
function common.addLibrary(sB, ...)
local bas = tostring(sB or ""); if(common.isDryString(bas)) then
common.logStatus("common.addPathLibrary: Missing base path") return end
bas = common.normFolder(bas:sub(-1,-1) == "/" and bas or bas.."/")
local arg, fmt = {...}, metaCommon.__fmdr; bas = common.stringTrim(bas)
if(arg[1] and common.isTable(arg[1])) then arg = arg[1] end
for idx, val in ipairs(arg) do
local ext = common.stringTrim(val)
local dir = ext:gsub("/",""):gsub("%s+", "")
if(not common.isDryString(dir)) then
ext = common.normFolder(ext)
local ok = os.execute("cd "..bas..ext)
if(ok) then ext = (bas..fmt:format(ext))
package.path = package.path..";"..ext
else ext = bas..ext
common.logStatus("common.addLibrary["..tostring(idx).."]: Mismatch: "..ext, nil)
end
else
common.logStatus("common.addLibrary["..tostring(idx).."]: Skipped", nil)
end
end
end
function common.tableClear(tT)
if(not common.isTable(tT)) then
return common.logStatus("common.tableClear: Missing <"..tostring(tT)..">") end
for k, v in pairs(tT) do tT[k] = nil end
end
function common.tableArrGetLinearSpace(nBeg, nEnd, nAmt)
local fAmt = math.floor(tonumber(nAmt) or 0); if(fAmt < 0) then
return common.logStatus("common.tableArrGetLinearSpace: Samples count invalid <"..tostring(fAmt)..">", nil) end
local iAmt, dAmt = (fAmt + 1), (nEnd - nBeg)
local fBeg, fEnd, nAdd = 1, (fAmt+2), (dAmt / iAmt)
local tO = {[fBeg] = nBeg, [fEnd] = nEnd}
while(fBeg <= fEnd) do fBeg, fEnd = (fBeg + 1), (fEnd - 1)
tO[fBeg], tO[fEnd] = (tO[fBeg-1] + nAdd), (tO[fEnd+1] - nAdd)
end return tO
end
function common.tableArrReverse(tT)
if(not common.isTable(tT)) then return end
local nN = #tT -- Store the array length
for ID = 1, #tT do tT[ID] = tT[nN-ID+1] end
end
function common.tableArrMallocDim(vV, ...)
local vA, tA = common.getPick(vV, common.copyItem(vV),0), {...}
local nD, tO = table.remove(tA, 1), {}
if(common.isNil(nD)) then return vA end
for iD = 1, nD do
tO[iD] = common.tableArrMallocDim(vA, unpack(tA))
end; return tO
end
function common.tableArrMalloc(nL)
return common.tableArrMallocDim(0, nL)
end
function common.tableArrMalloc2D(nW, nH)
return common.tableArrMallocDim(0, nH, nW)
end
-- Transfer array data from source to destination
--[[
tD -> Destination array
tS -> Source Array
tC -> Array containing copy methods
]]
function common.tableArrTransfer(tD, tS, tC)
local iD = 1; while(not common.isNil(tS[iD]))do
tD[iD] = common.copyItem(tS[iD], tC); iD = iD + 1 end
end
--[[
* Converts linear array to a 2D array
* arLin -> Linear array in format {1, 2, 3, 4, w = 2, h = 2}
* w, h -> Custom array size
]]
function common.tableArrConvert2D(arLin, vW, vH)
if(not arLin) then return false end
local nW, nH = (vW or arLin.w), (vH or arLin.h)
if(not (nW and nH)) then return false end
if(not (nW > 0 and nH > 0)) then return false end
arRez = common.tableArrMalloc2D(nW, nH)
for i = 0, (nH-1) do for j = 0, (nW-1) do
arRez[i+1][j+1] = (tonumber(arLin[i*nW+j+1]) or 0)
end end; return arRez
end
function common.tableArrRotateR(tArr, sX, sY)
local ii, jj, tTmp = 1, 1, common.tableArrMalloc2D(sY, sX)
for j = 1, sX, 1 do for i = sY, 1, -1 do
if(jj > sY) then ii, jj = (ii + 1), 1 end
tTmp[ii][jj] = tArr[i][j]
tArr[i][j] = nil; jj = (jj + 1)
end end
for i = 1, sX do tArr[i] = {}
for j = 1, sY do tArr[i][j] = tTmp[i][j] end
end
end
function common.tableArrRotateL(tArr, sX, sY)
local ii, jj, tTmp = 1, 1, common.tableArrMalloc2D(sY, sX)
for j = sX, 1, -1 do for i = 1, sY, 1 do
if(jj > sY) then ii, jj = (ii + 1), 1 end
tTmp[ii][jj] = tArr[i][j]
tArr[i][j] = nil; jj = (jj + 1)
end end
for i = 1, sX do tArr[i] = {}
for j = 1, sY do tArr[i][j] = tTmp[i][j] end
end
end
-- Getting a start end and delta used in a for loop
function common.getValuesSED(nVal, nMin, nMax)
local s = (nVal > 0) and nMin or nMax
local e = (nVal > 0) and nMax or nMin
local d = getSign(e - s)
return s, e, d
end
function common.tableArrShift2D(tArr, sX, sY, nX, nY)
if(not (sX > 0 and sY > 0)) then return end
local x = math.floor(nX or 0)
local y = math.floor(nY or 0)
if(x ~= 0) then local M
local sx, ex, dx = common.getValuesSED(x, sX, 1)
for i = 1, sY do for j = sx, ex, dx do
M = (j-x); if(M >= 1 and M <= sX) then
tArr[i][j] = tArr[i][M]
else tArr[i][j] = 0 end
end end
end
if(y ~= 0) then local M
local sy, ey, dy = common.getValuesSED(y, sY, 1)
for i = sy, ey, dy do for j = 1, sX do
M = (i-y); if(M >= 1 and M <= sY) then
tArr[i][j] = tArr[M][j]
else tArr[i][j] = 0 end
end end
end
end
function common.tableArrRoll2D(tArr, sX, sY, nX, nY)
if( not( sX > 0 and sY > 0) ) then return end
local x, y = math.floor(nX or 0), math.floor(nY or 0)
if(y ~= 0) then
local MaxY = (y > 0) and sY or 1
local MinY = (y > 0) and 1 or sY
local siY, y, arTmp = getSign(y), (y * siY), {}
while(y > 0) do
for i = 1, sX do arTmp[i] = tArr[MaxY][i] end
common.tableArrShift2D(tArr, sX, sY, 0, siY)
for i = 1, sX do tArr[MinY][i] = arTmp[i] end
y = y - 1
end
end
if(x ~= 0) then
local MaxX = (x > 0) and sX or 1
local MinX = (x > 0) and 1 or sX
local siX, x, arTmp = getSign(x), (x * siX), {}
while(x > 0) do
for i = 1, sY do arTmp[i] = tArr[i][MaxX] end
common.tableArrShift2D(tArr, sX, sY, siX)
for i = 1, sY do tArr[i][MinX] = arTmp[i] end
x = x - 1
end
end
end
function common.tableArrMirror2D(tArr, sX, sY, fX, fY)
local tTmp, s = 0, 1
if(fY) then local e = sY
while(s < e) do for k = 1, sX do
tTmp = tArr[s][k]
tArr[s][k] = tArr[e][k]
tArr[e][k] = tTmp end
s, e = (s + 1), (e - 1)
end
end
if(fX) then local e = sX
while(s < e) do for k = 1, sY do
tTmp = tArr[k][s]
tArr[k][s] = tArr[k][e]
tArr[k][e] = tTmp
end
s, e = (s + 1), (e - 1)
end
end
end
-- Concatenates the array elements (...) and gives new array
function common.tableArrConcat(...)
local tO, IK = {}, 1
for J = 1, select("#",...) do
local ID, tTab = 1, select(J,...)
if(not common.isNil(tTab)) then
while(tTab[ID]) do tO[IK] = tTab[ID]
IK, ID = (IK + 1), (ID + 1) end
end
end; return tO
end
function common.tableArrExtract2D(tA, sX, eX, sY, eY)
local tO, cX, cY = {}
for iY = sY, eY do for iX = sX, eX do
if(not tO[iY-sY+1]) then tO[iY-sY+1] = {} end
tO[iY-sY+1][iX-sX+1] = tA[iY][iX]
end end; return tO
end
function common.tableArrReverse(tA)
local nS, nE = 1, 1
while(tA[nE]) do nE = nE + 1 end
nS, nE = 1, (nE - 1)
while(nE > nS) do
tA[nE], tA[nS] = tA[nS], tA[nE]
nS, nE = (nS + 1), (nE - 1)
end
end
function common.binaryMirror(nN, nB)
local nO = 0; for iD = 1, nB do local nM = bit.band(nN, 1)
nN = bit.rshift(nN, 1); nO = bit.lshift(nO, 1); nO = bit.bor(nO, nM)
end; return nO
end
-- How many bits are needed to represent /nN/
function common.binaryNeededBits(nN, bE)
local nC = 0; while(nN ~= 0) do
if(bE) then nN = bit.lshift(nN, 1)
else nN = bit.rshift(nN, 1) end
nC = nC + 1
end; return nC
end
-- Calculate the next binary power based on nN
function common.binaryNextBaseTwo(nN)
return (2^(math.floor(math.log(nN,2))+1))
end
-- Check if the number is a binary power
function common.binaryIsPower(nN)
return (bit.band(nN - 1, nN) == 0)
end
function common.bytesGetString(tB)
local tO = {}; for iD = 1, #tB do
tO[iD] = string.char(tB[iD]) end
return table.concat(tO)
end
function common.bytesGetNumber(tB)
local nO = 0; for iD = 1, #tB do
nO = nO * 256; nO = nO + tB[iD]
end; return nO
end
function common.getMargin()
return metaCommon.__marg
end
function common.getDerivative(fF, vX)
local nX, nM, yL, yH = (tonumber(vX) or 0), metaCommon.__marg
local xL, xH, bS = (nX-nM), (nX+nM), true -- Derivative margin
bS, yL = pcall(fF, xL); if(not bS) then
return common.logStatus("common.getDerivative: "..yL ,0) end
bS, yH = pcall(fF, xH); if(not bS) then
return common.logStatus("common.getDerivative: "..yH, 0) end
return (yH - yL) / (xH - xL)
end
local function sortQuick(tD, iL, iH)
if(iL and iH and iL > 0 and iL < iH) then
local iM = common.randomGetNumber(iH-(iL-1))+iL-1
tD[iL], tD[iM] = tD[iM], tD[iL]
local vM, iN = tD[iL].__val, (iL + 1); iM = iL
while(iN <= iH) do
if(tD[iN].__val < vM) then iM = iM + 1
tD[iM], tD[iN] = tD[iN], tD[iM]
end; iN = iN + 1
end
tD[iL], tD[iM] = tD[iM], tD[iL]
sortQuick(tD, iL, iM - 1)
sortQuick(tD, iM + 1, iH)
end
end
local function sortSelect(tD, iL, iH)
if(iL and iH and iL > 0 and iL < iH) then
local iN = 1; while(tD[iN]) do
local iS = iN + 1; while(tD[iS]) do
if(tD[iS].__val <= tD[iN].__val) then
tD[iN], tD[iS] = tD[iS], tD[iN]
end; iS = iS + 1
end; iN = iN + 1
end
end
end
local function sortBubble(tD, iL, iH)
local bE, iN = false
while(not bE) do bE = true
for iN = iL, (iH-1), 1 do
if(tD[iN].__val > tD[iN+1].__val) then bE = false
tD[iN], tD[iN+1] = tD[iN+1], tD[iN]
end
end
end
end
metaCommon.__sort.__ID = 1
metaCommon.__sort.__DV = "#"
metaCommon.__sort[1] = {sortQuick , "Quick sort"}
metaCommon.__sort[2] = {sortSelect, "Selection sort"}
metaCommon.__sort[3] = {sortBubble, "Bubble sort"}
metaCommon.__sort.__top = #metaCommon.__sort
function common.sortList()
local tS = metaCommon.__sort
local iL = tostring(tS.__top):len()
common.logStatus("common.sortList: Sorting algorithms available:")
for iN = 1, tS.__top do local inf = tS[iN][2]
local sS = "] "..((iN == tS.__ID) and "@" or ">").." "..inf
common.logStatus(" ["..common.stringPadL(tostring(iN), iL, " ")..sS)
end
end
function common.normFolder(sD)
local sS = tostring(sD):gsub("\\","/"):gsub("/+","/")
return ((sS:sub(-1,-1) == "/") and sS or (sS.."/"))
end
function common.sortRem(vN)
local tS, iN = metaCommon.__sort, (tonumber(vN) or 0)
if(tS[iN]) then table.remove(tS, iN) end
if(tS.__ID == iN) then tS.__ID = 1 end
if(tS.__top > 0) then tS.__top = (tS.__top - 1) end; return true
end
function common.sortSet(vN)
local tS, iN = metaCommon.__sort, (tonumber(vN) or 0)
if(tS[iN]) then tS.__ID = iN; return true end; tS.__ID = 1
return common.logStatus("common.sortSet: Invalid <"..tostring(iN)..">", 0)
end
function common.sortAdd(fF, sI)
local tS = metaCommon.__sort; if(metaCommon.__type[4] ~= type(fF)) then
return common.logStatus("common.sortAdd: Invalid <"..tostring(fF).."><"..tostring(sI)..">", 0) end
tS.__top = (tS.__top + 1); tS[tS.__top] = {fF, tostring(sI)}; return (tS.__top)
end