-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtableLib.lua
322 lines (237 loc) · 4.9 KB
/
tableLib.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
local t = {}
local function create(t)
if (t == nil) then
return nil
end
if (type(t) == 'table') then
return t
end
return {t}
end
t.create = create
totable = create
local function toLua(t)
local res = {}
for k, v in pairs(t) do
if (type(v) == 'table') then
v = tableToLua(v)
else
if (type(v) == 'string') then
v = string.format('%q', v)
else
v = tostring(v)
end
end
if (type(k) == 'number') then
res[#res + 1] = v
else
res[#res + 1] = k..'='..tostring(v)
end
end
return '{'..table.concat(res, ', ')..'}'
end
t.toLua = toLua
local function contains(t, e)
if (t == nil) then
return false
end
for k, v in pairs(t) do
if (v == e) then
return true
end
end
return false
end
t.contains = contains
local function getSize_nest(t, recursionTable)
if (recursionTable[t] ~= nil) then
return 0
end
local c = 0
for k, val in pairs(t) do
if (type(val) == 'table') then
c = c + getTableSize_nest(val, recursionTable) + 1
else
c = c + 1
end
end
return c
end
local function getSize(t, nested)
assert(t, 'no arg')
assert((type(t) == 'table'), 'no table')
local recursionTable = {}
recursionTable[t] = t
local c = 0
for k, val in pairs(t) do
if (nested and (type(val) == 'table')) then
c = c + getTableSize_nest(val, recursionTable) + 1
else
c = c + 1
end
end
return c
end
t.getSize = getSize
local function copy_nest(source, recursionTable)
if recursionTable[source] then
return recursionTable[source]
end
local result = {}
recursionTable[source] = result
for k, v in pairs(source) do
if (type(v) == "table") then
local add = copy_nest(v, recursionTable)
if (k == v) then
k = add
end
result[k] = add
else
result[k] = v
end
end
return result
end
local function copy(source)
if (source == nil) then
return nil
end
local result = {}
local recursionTable = {}
recursionTable[source] = result
for k, v in pairs(source) do
if (type(v) == "table") then
local add = copy_nest(v, recursionTable)
if (k == v) then
k = add
end
result[k] = add
else
result[k] = v
end
end
return result
end
t.copy = copy
local function merge(source, toAdd)
for k, v in pairs(toAdd) do
if v then
if (type(v) == "table") then
if (type(source[k]) ~= "table") then
source[k] = {}
end
mergeTable(source[k], v)
else
source[k] = v
end
end
end
end
t.merge = merge
io.local_require('stringLib')
local function printTable(t, nestDepth)
nestDepth = nestDepth or 0
for k, v in pairs(t) do
if (type(v) == "table") then
print(string.rep("\t", nestDepth).."//"..k)
printTable(v, nestDepth + 1)
else
if (type(v) == "boolean") then
v = boolToString(v)
end
print(string.rep("\t", nestDepth)..k.." --> "..tostring(v))
end
end
end
t.print = printTable
local function write(fileOrPath, root, nestDepth)
assert(fileOrPath, 'no file/path')
assert(root, 'no root')
local file
if (type(fileOrPath) == 'string') then
file = io.open(fileOrPath, 'w+')
assert(file, 'cannot open '..tostring(fileOrPath))
else
file = fileOrPath
end
local recursionTable = {}
local function writeTable_nest(t, nestDepth)
if recursionTable[t] then
return
end
recursionTable[t] = t
for k, v in pairs(t) do
if (type(v) == "table") then
writeTable_nest(v, nestDepth + 1)
else
file:write(tostring(v), '\n')
end
end
end
if (nestDepth == nil) then
nestDepth = 0
end
writeTable_nest(root, nestDepth)
if (type(fileOrPath) == 'string') then
file:close()
end
end
t.write = write
local function writeEx(fileOrPath, root, nestDepth)
assert(fileOrPath, 'no file/path')
assert(root, 'no root')
local file
if (type(fileOrPath) == 'string') then
file = io.open(fileOrPath, 'w+')
assert(file, 'cannot open '..tostring(fileOrPath))
else
file = fileOrPath
end
local recursionTable = {}
file:write("contents of ", tostring(root), "\n")
local function writeTable_nest(t, nestDepth)
if recursionTable[t] then
file:write("recursion ", tostring(t), "\n")
return--error("writeTable: recursion in table")
end
recursionTable[t] = t
for k, v in pairs(t) do
if (type(v) == "table") then
file:write(string.rep("\t", nestDepth).."//"..k.."\n")
writeTable_nest(v, nestDepth + 1)
else
if (type(v) == "boolean") then
v = boolToString(v)
end
file:write(string.rep("\t", nestDepth)..k.." --> "..tostring(v).."\n")
end
end
end
if (nestDepth == nil) then
nestDepth = 0
end
writeTable_nest(root, nestDepth)
if (type(fileOrPath) == 'string') then
file:close()
end
end
t.writeEx = writeEx
local function mix(t1, t2, sep)
if (sep == nil) then
sep = ""
end
local result = {}
for i = 1, #t1, 1 do
result[i] = t1[i]..t2[i]
end
return result
end
t.mix = mix
local function addToEnv(t)
setmetatable(t, {__index = _G})
setfenv(2, t)
end
t.addToEnv = addToEnv
for k, v in pairs(t) do
table[k] = v
end