-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablib.lua
More file actions
196 lines (172 loc) · 3.44 KB
/
tablib.lua
File metadata and controls
196 lines (172 loc) · 3.44 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
--=========== Copyright © 2017, Planimeter, All rights reserved. =============--
--
-- Purpose: Extends the table library
--
--============================================================================--
require( "table" )
function table.append( a, b )
for _, v in ipairs( b ) do
a[ #a + 1 ] = v
end
end
function table.clear( t )
for k in pairs( t ) do
t[ k ] = nil
end
end
function table.copy( t, recursive )
if ( t == nil ) then
return nil
end
local copy = {}
setmetatable( copy, getmetatable( t ) )
for i, v in pairs( t ) do
if ( type( v ) ~= "table" ) then
copy[ i ] = v
else
recursive = recursive or {}
recursive[ t ] = copy
if ( recursive[ v ] ) then
copy[ i ] = recursive[ v ]
else
copy[ i ] = table.copy( v, recursive )
end
end
end
return copy
end
function table.shallowcopy( t )
if ( type( t ) ~= "table" ) then
typerror( 1, "table", t )
end
local copy = {}
for k, v in pairs( t ) do
copy[ k ] = v
end
return copy
end
function table.count( t, value )
local count = 0
for _, v in pairs( t ) do
if ( typeof( v, "table" ) and
typeof( value, "table" ) ) then
if ( table.equal( v, value ) ) then
count = count + 1
end
elseif ( v == value ) then
count = count + 1
end
end
return count
end
function table.equal( a, b )
if ( getmetatable( a ) ~= getmetatable( b ) ) then
return false
end
for k, v in pairs( a ) do
if ( typeof( v, "table" ) and
typeof( b[ k ], "table" ) and
not table.equal( v, b[ k ] ) ) then
return false
elseif ( v ~= b[ k ] ) then
return false
end
end
for k, v in pairs( b ) do
if ( typeof( v, "table" ) and
typeof( a[ k ], "table" ) and
not table.equal( v, a[ k ] ) ) then
return false
elseif ( v ~= a[ k ] ) then
return false
end
end
return true
end
function table.flatten( t )
for k, v in pairs( t ) do
if ( type( v ) == "table" ) then
table.flatten( v )
for l, w in pairs( v ) do
t[ k .. "." .. l ] = w
end
t[ k ] = nil
end
end
end
function table.hasvalue( t, value )
for k, v in pairs( t ) do
if ( v == value ) then
return k
end
end
return nil
end
function table.len( t )
local c = 0
for _ in pairs( t ) do
c = c + 1
end
return c
end
function table.merge( a, b )
for k, v in pairs( b ) do
a[ k ] = v
end
end
function table.prepend( a, b )
for i = #b, 1, -1 do
table.insert( a, 1, b[ i ] )
end
end
function table.print( t, i, printed )
if ( t == nil ) then
print( nil )
return
end
printed = printed or {}
printed[ t ] = true
i = i or 0
local indent = string.rep( "\t", i )
for k, v in pairs( t ) do
if ( type( v ) == "table" and not printed[ v ] ) then
print( indent .. k )
table.print( v, i + 1, printed )
else
print( indent .. tostring( k ), v )
end
end
end
function table.raise( t )
local sub, key
for k, v in pairs( t ) do
sub, key = string.match( k, "(.-)%.(.+)" )
if ( sub and key ) then
sub = tonumber( sub ) or sub
t[ sub ] = t[ sub ] or {}
t[ sub ][ key ] = v
t[ k ] = nil
table.raise( t[ sub ] )
table.raise( t )
end
end
end
function table.irandom( t )
return t[ math.random( #t ) ]
end
function table.removevalue( t, v )
for i, w in ipairs( t ) do
if ( w == v ) then
table.remove( t, i )
end
end
end
function table.unique( t )
local copy = {}
for _, v in ipairs( t ) do
if ( table.count( copy, v ) == 0 ) then
copy[ #copy + 1 ] = v
end
end
return copy
end