-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathtable.lua
More file actions
806 lines (588 loc) · 18.9 KB
/
table.lua
File metadata and controls
806 lines (588 loc) · 18.9 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
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
function table.Pack( ... )
return { ... }, select( "#", ... )
end
--[[---------------------------------------------------------
Name: Inherit( t, base )
Desc: Copies any missing data from base to t
-----------------------------------------------------------]]
function table.Inherit( t, base )
for k, v in pairs( base ) do
if ( t[ k ] == nil ) then t[ k ] = v end
end
t[ "BaseClass" ] = base
return t
end
--[[---------------------------------------------------------
Name: Copy(t, lookup_table)
Desc: Taken straight from http://lua-users.org/wiki/PitLibTablestuff
and modified to the new Lua 5.1 code by me.
Original function by PeterPrade!
-----------------------------------------------------------]]
function table.Copy( t, lookup_table )
if ( t == nil ) then return nil end
local copy = {}
setmetatable( copy, debug.getmetatable( t ) )
for i, v in pairs( t ) do
if ( !istable( v ) ) then
copy[ i ] = v
else
lookup_table = lookup_table or {}
lookup_table[ t ] = copy
if ( lookup_table[ v ] ) then
copy[ i ] = lookup_table[ v ] -- we already copied this table. reuse the copy.
else
copy[ i ] = table.Copy( v, lookup_table ) -- not yet copied. copy it.
end
end
end
return copy
end
--[[---------------------------------------------------------
Name: Empty( tab )
Desc: Empty a table
-----------------------------------------------------------]]
function table.Empty( tab )
for k, v in pairs( tab ) do
tab[ k ] = nil
end
end
--[[---------------------------------------------------------
Name: IsEmpty( tab )
Desc: Returns whether a table has iterable items in it, useful for non-sequential tables
-----------------------------------------------------------]]
function table.IsEmpty( tab )
return next( tab ) == nil
end
--[[---------------------------------------------------------
Name: CopyFromTo( FROM, TO )
Desc: Make TO exactly the same as FROM - but still the same table.
-----------------------------------------------------------]]
function table.CopyFromTo( from, to )
-- Erase values from table TO
table.Empty( to )
-- Copy values over
table.Merge( to, from )
end
--[[---------------------------------------------------------
Name: Merge
Desc: xx
-----------------------------------------------------------]]
function table.Merge( dest, source, forceOverride )
for k, v in pairs( source ) do
if ( !forceOverride and istable( v ) and istable( dest[ k ] ) ) then
-- don't overwrite one table with another
-- instead merge them recurisvely
table.Merge( dest[ k ], v )
else
dest[ k ] = v
end
end
return dest
end
--[[---------------------------------------------------------
Name: HasValue
Desc: Returns whether the value is in given table
-----------------------------------------------------------]]
function table.HasValue( t, val )
for k, v in pairs( t ) do
if ( v == val ) then return true end
end
return false
end
--[[---------------------------------------------------------
Name: table.Add( dest, source )
Desc: Unlike merge this adds the two tables together and discards keys.
-----------------------------------------------------------]]
function table.Add( dest, source )
-- The tables should be different otherwise this will just freeze the whole game
if ( dest == source ) then return dest end
-- At least one of them needs to be a table or this whole thing will fall on its ass
if ( !istable( source ) ) then return dest end
if ( !istable( dest ) ) then dest = {} end
for k, v in pairs( source ) do
table.insert( dest, v )
end
return dest
end
--[[---------------------------------------------------------
Name: table.SortDesc( table )
Desc: Like Lua's default sort, but descending
-----------------------------------------------------------]]
function table.SortDesc( t )
return table.sort( t, function( a, b ) return a > b end )
end
--[[---------------------------------------------------------
Name: table.SortByKey( table )
Desc: Returns a table sorted numerically by Key value
-----------------------------------------------------------]]
function table.SortByKey( t, desc )
local temp = {}
for key, _ in pairs( t ) do table.insert( temp, key ) end
if ( desc ) then
table.sort( temp, function( a, b ) return t[ a ] < t[ b ] end )
else
table.sort( temp, function( a, b ) return t[ a ] > t[ b ] end )
end
return temp
end
--[[---------------------------------------------------------
Name: table.Count( table )
Desc: Returns the number of keys in a table
-----------------------------------------------------------]]
function table.Count( t )
local i = 0
for k in pairs( t ) do i = i + 1 end
return i
end
--[[---------------------------------------------------------
Name: table.Random( table )
Desc: Return a random key
-----------------------------------------------------------]]
function table.Random( t )
local rk = math.random( 1, table.Count( t ) )
local i = 1
for k, v in pairs( t ) do
if ( i == rk ) then return v, k end
i = i + 1
end
end
--[[---------------------------------------------------------
Name: table.Shuffle( table )
Desc: Performs an inline Fisher-Yates shuffle on the table in O(n) time
-----------------------------------------------------------]]
function table.Shuffle( t )
local n = #t
for i = 1, n - 1 do
local j = math.random( i, n )
t[ i ], t[ j ] = t[ j ], t[ i ]
end
end
--[[----------------------------------------------------------------------
Name: table.IsSequential( table )
Desc: Returns true if the tables
keys are sequential
-------------------------------------------------------------------------]]
function table.IsSequential( t )
local i = 1
for key, value in pairs( t ) do
if ( t[ i ] == nil ) then return false end
i = i + 1
end
return true
end
--[[---------------------------------------------------------
Name: table.ToString( table,name,nice )
Desc: Convert a simple table to a string
table = the table you want to convert (table)
name = the name of the table (string)
nice = whether to add line breaks and indents (bool)
-----------------------------------------------------------]]
local function MakeTable( t, nice, indent, done )
local str = ""
done = done or {}
indent = indent or 0
local idt = ""
if ( nice ) then idt = string.rep( "\t", indent ) end
local nl, tab = "", ""
if ( nice ) then nl, tab = "\n", "\t" end
local sequential = table.IsSequential( t )
for key, value in pairs( t ) do
str = str .. idt .. tab .. tab
if !sequential then
if ( isnumber( key ) or isbool( key ) ) then
key = "[" .. tostring( key ) .. "]" .. tab .. "="
else
key = tostring( key ) .. tab .. "="
end
else
key = ""
end
if ( istable( value ) and !done[ value ] ) then
if ( IsColor( value ) ) then
done[ value ] = true
value = "Color(" .. value.r .. "," .. value.g .. "," .. value.b .. "," .. value.a .. ")"
str = str .. key .. tab .. value .. "," .. nl
else
done[ value ] = true
str = str .. key .. tab .. '{' .. nl .. MakeTable( value, nice, indent + 1, done )
str = str .. idt .. tab .. tab .. tab .. tab .. "}," .. nl
end
else
if ( isstring( value ) ) then
value = '"' .. tostring( value ) .. '"'
elseif ( isvector( value ) ) then
value = "Vector(" .. value.x .. "," .. value.y .. "," .. value.z .. ")"
elseif ( isangle( value ) ) then
value = "Angle(" .. value.pitch .. "," .. value.yaw .. "," .. value.roll .. ")"
else
value = tostring( value )
end
str = str .. key .. tab .. value .. "," .. nl
end
end
return str
end
function table.ToString( t, n, nice )
local nl, tab = "", ""
if ( nice ) then nl, tab = "\n", "\t" end
local str = ""
if ( n ) then str = n .. tab .. "=" .. tab end
return str .. "{" .. nl .. MakeTable( t, nice ) .. "}"
end
--[[---------------------------------------------------------
Name: table.Sanitise( table )
Desc: Converts a table containing vectors, angles, bools so it can be converted to and from keyvalues
-----------------------------------------------------------]]
function table.Sanitise( t, done )
done = done or {}
local tbl = {}
for k, v in pairs ( t ) do
if ( istable( v ) and !IsColor( v ) and !done[ v ] ) then
done[ v ] = true
tbl[ k ] = table.Sanitise( v, done )
else
if ( isvector( v ) ) then
local x, y, z = v.x, v.y, v.z
if y == 0 then y = nil end
if z == 0 then z = nil end
tbl[ k ] = { __type = "Vector", x = x, y = y, z = z }
elseif ( isangle( v ) ) then
local p, y, r = v.pitch, v.yaw, v.roll
if p == 0 then p = nil end
if y == 0 then y = nil end
if r == 0 then r = nil end
tbl[ k ] = { __type = "Angle", p = p, y = y, r = r }
elseif ( IsColor( v ) ) then
local r, g, b, a = v.r, v.g, v.b, v.a
if r == 255 then r = nil end
if g == 255 then g = nil end
if b == 255 then b = nil end
if a == 255 then a = nil end
tbl[ k ] = { __type = "Color", r = r, g = g, b = b, a = a }
elseif ( isbool( v ) ) then
tbl[ k ] = { __type = "Bool", tostring( v ) }
else
tbl[ k ] = tostring( v )
end
end
end
return tbl
end
--[[---------------------------------------------------------
Name: table.DeSanitise( table )
Desc: Converts a Sanitised table back
-----------------------------------------------------------]]
function table.DeSanitise( t, done )
done = done or {}
local tbl = {}
for k, v in pairs ( t ) do
if ( istable( v ) and !IsColor( v ) and !done[ v ] ) then
done[ v ] = true
if ( v.__type ) then
if ( v.__type == "Vector" ) then
tbl[ k ] = Vector( v.x or 0, v.y, v.z )
elseif ( v.__type == "Angle" ) then
tbl[ k ] = Angle( v.p or 0, v.y, v.r )
elseif ( v.__type == "Color" ) then
tbl[ k ] = Color( v.r or 255, v.g or 255, v.b or 255, v.a or 255 )
elseif ( v.__type == "Bool" ) then
tbl[ k ] = ( v[ 1 ] == "true" )
end
else
tbl[ k ] = table.DeSanitise( v, done )
end
else
tbl[ k ] = v
end
end
return tbl
end
function table.ForceInsert( t, v )
if ( t == nil ) then t = {} end
table.insert( t, v )
return t
end
--[[---------------------------------------------------------
Name: table.SortByMember( table )
Desc: Sorts table by named member
-----------------------------------------------------------]]
function table.SortByMember( tab, memberName, bAsc )
local TableMemberSort = function( a, b, MemberName, bReverse )
--
-- All this error checking kind of sucks, but really is needed
--
if ( !istable( a ) ) then return !bReverse end
if ( !istable( b ) ) then return bReverse end
if ( !a[ MemberName ] ) then return !bReverse end
if ( !b[ MemberName ] ) then return bReverse end
if ( isstring( a[ MemberName ] ) ) then
if ( bReverse ) then
return a[ MemberName ]:lower() < b[ MemberName ]:lower()
else
return a[ MemberName ]:lower() > b[ MemberName ]:lower()
end
end
if ( bReverse ) then
return a[ MemberName ] < b[ MemberName ]
else
return a[ MemberName ] > b[ MemberName ]
end
end
table.sort( tab, function( a, b ) return TableMemberSort( a, b, memberName, bAsc or false ) end )
end
--[[---------------------------------------------------------
Name: table.LowerKeyNames( table )
Desc: Lowercase the keynames of all tables
-----------------------------------------------------------]]
function table.LowerKeyNames( tab )
local OutTable = {}
for k, v in pairs( tab ) do
-- Recurse
if ( istable( v ) ) then
v = table.LowerKeyNames( v )
end
OutTable[ k ] = v
if ( isstring( k ) ) then
OutTable[ k ] = nil
OutTable[ string.lower( k ) ] = v
end
end
return OutTable
end
--[[---------------------------------------------------------
Name: table.CollapseKeyValue( table )
Desc: Collapses a table with keyvalue structure
-----------------------------------------------------------]]
function table.CollapseKeyValue( Table )
local OutTable = {}
for k, v in pairs( Table ) do
local Val = v.Value
if ( istable( Val ) ) then
Val = table.CollapseKeyValue( Val )
end
OutTable[ v.Key ] = Val
end
return OutTable
end
--[[---------------------------------------------------------
Name: table.ClearKeys( table, bSaveKey )
Desc: Clears the keys, converting to a numbered format
-----------------------------------------------------------]]
function table.ClearKeys( Table, bSaveKey )
local OutTable = {}
for k, v in pairs( Table ) do
if ( bSaveKey ) then
v.__key = k
end
table.insert( OutTable, v )
end
return OutTable
end
local function keyValuePairs( state )
state.Index = state.Index + 1
local keyValue = state.KeyValues[ state.Index ]
if ( !keyValue ) then return end
return keyValue.key, keyValue.val
end
local function toKeyValues( tbl )
local result = {}
for k, v in pairs( tbl ) do
table.insert( result, { key = k, val = v } )
end
return result
end
local function getKeys( tbl )
local keys, i = {}, 0
for k in pairs( tbl ) do
i = i + 1
keys[ i ] = k
end
return keys
end
--[[---------------------------------------------------------
A Pairs function
Sorted by TABLE KEY
-----------------------------------------------------------]]
function SortedPairs( pTable, Desc )
local keys = getKeys( pTable )
if ( Desc ) then
table.sort( keys, function( a, b )
return a > b
end )
else
table.sort( keys, function( a, b )
return a < b
end )
end
local i, key = 1, nil
return function()
key, i = keys[ i ], i + 1
return key, pTable[ key ]
end
end
--[[---------------------------------------------------------
A Pairs function
Sorted by VALUE
-----------------------------------------------------------]]
function SortedPairsByValue( pTable, Desc )
local sortedTbl = toKeyValues( pTable )
if ( Desc ) then
table.sort( sortedTbl, function( a, b ) return a.val > b.val end )
else
table.sort( sortedTbl, function( a, b ) return a.val < b.val end )
end
return keyValuePairs, { Index = 0, KeyValues = sortedTbl }
end
--[[---------------------------------------------------------
A Pairs function
Sorted by Member Value (All table entries must be a table!)
-----------------------------------------------------------]]
function SortedPairsByMemberValue( pTable, pValueName, Desc )
local sortedTbl = toKeyValues( pTable )
for k, v in pairs( sortedTbl ) do
v.member = v.val[ pValueName ]
end
table.SortByMember( sortedTbl, "member", !Desc )
return keyValuePairs, { Index = 0, KeyValues = sortedTbl }
end
--[[---------------------------------------------------------
A Pairs function
-----------------------------------------------------------]]
function RandomPairs( pTable, Desc )
local sortedTbl = toKeyValues( pTable )
for k, v in pairs( sortedTbl ) do
v.rand = math.random( 1, 1000000 )
end
-- descending/ascending for a random order, really?
if ( Desc ) then
table.sort( sortedTbl, function( a, b ) return a.rand > b.rand end )
else
table.sort( sortedTbl, function( a, b ) return a.rand < b.rand end )
end
return keyValuePairs, { Index = 0, KeyValues = sortedTbl }
end
--[[---------------------------------------------------------
GetFirstKey
-----------------------------------------------------------]]
function table.GetFirstKey( t )
local k, _ = next( t )
return k
end
function table.GetFirstValue( t )
local _, v = next( t )
return v
end
function table.GetLastKey( t )
local k, _ = next( t, table.Count( t ) - 1 )
return k
end
function table.GetLastValue( t )
local _, v = next( t, table.Count( t ) - 1 )
return v
end
function table.FindNext( tab, val )
local bfound = false
for k, v in pairs( tab ) do
if ( bfound ) then return v end
if ( val == v ) then bfound = true end
end
return table.GetFirstValue( tab )
end
function table.FindPrev( tab, val )
local last = table.GetLastValue( tab )
for k, v in pairs( tab ) do
if ( val == v ) then return last end
last = v
end
return last
end
function table.GetWinningKey( tab )
local highest = -math.huge
local winner = nil
for k, v in pairs( tab ) do
if ( v > highest ) then
winner = k
highest = v
end
end
return winner
end
function table.KeyFromValue( tbl, val )
for key, value in pairs( tbl ) do
if ( value == val ) then return key end
end
end
function table.RemoveByValue( tbl, val )
local key = table.KeyFromValue( tbl, val )
if ( !key ) then return false end
if ( isnumber( key ) ) then
table.remove( tbl, key )
else
tbl[ key ] = nil
end
return key
end
function table.KeysFromValue( tbl, val )
local res = {}
for key, value in pairs( tbl ) do
if ( value == val ) then res[ #res + 1 ] = key end
end
return res
end
function table.MemberValuesFromKey( tab, key )
local res = {}
for k, v in pairs( tab ) do
if ( istable( v ) and v[ key ] != nil ) then res[ #res + 1 ] = v[ key ] end
end
return res
end
function table.Reverse( tbl )
local len = #tbl
local ret = {}
for i = len, 1, -1 do
ret[ len - i + 1 ] = tbl[ i ]
end
return ret
end
function table.ForEach( tab, funcname )
for k, v in pairs( tab ) do
funcname( k, v )
end
end
function table.GetKeys( tab )
local keys = {}
local id = 1
for k, v in pairs( tab ) do
keys[ id ] = k
id = id + 1
end
return keys
end
function table.Flip( tab )
local res = {}
for k, v in pairs( tab ) do
res[ v ] = k
end
return res
end
-- Polyfill for table.move on 32-bit
-- Don't forget to remove this when it's no longer necessary
if ( !table.move ) then
function table.move( sourceTbl, from, to, dest, destTbl )
if ( !istable( sourceTbl ) ) then error( "bad argument #1 to 'move' (table expected, got " .. type( sourceTbl ) .. ")", 2 ) end
if ( !isnumber( from ) ) then error( "bad argument #2 to 'move' (number expected, got " .. type( from ) .. ")", 2 ) end
if ( !isnumber( to ) ) then error( "bad argument #3 to 'move' (number expected, got " .. type( to ) .. ")", 2 ) end
if ( !isnumber( dest ) ) then error( "bad argument #4 to 'move' (number expected, got " .. type( dest ) .. ")", 2 ) end
if ( destTbl != nil ) then
if ( !istable( destTbl ) ) then error( "bad argument #5 to 'move' (table expected, got " .. type( destTbl ) .. ")", 2 ) end
else
destTbl = sourceTbl
end
local buffer = { unpack( sourceTbl, from, to ) }
dest = math.floor( dest - 1 )
for i, v in ipairs( buffer ) do
destTbl[ dest + i ] = v
end
return destTbl
end
end