-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
88 lines (74 loc) · 1.56 KB
/
util.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
function table.pack(...)
return {n=select('#', ...), ...}
end
table.unpack = unpack
function table.copy(t)
local c = {}
for k, v in pairs(t) do
c[k] = v
end
return c
end
-- won't be copied by deepcopy()
function table.makestatic(t)
t._static = true
end
function table.deepcopy(t, ref)
if t._static then
return t
end
if not ref then
ref = {} -- {original_table = copy_table}
end
if ref[t] then
return ref[t]
end
local c = {}
ref[t] = c
for k, v in pairs(t) do
if type(k) == 'table' then
k = table.deepcopy(k, ref)
end
if type(v) == 'table' then
v = table.deepcopy(v, ref)
end
c[k] = v
end
setmetatable(c, getmetatable(t))
return c
end
function table.dump(t, depth, ref)
if not depth then depth = 0 end
if not ref then ref = {} end
local indent = string.rep(' ', depth)
if ref[t] then
print(indent..'('..tostring(t)..')')
return
end
ref[t] = true
if type(t) == 'table' then
print(indent .. '{')
for k, v in pairs(t) do
local s = indent..' '..k..' = '
local tv = type(v)
if tv ~= 'table' then
s = s .. tostring(v)
end
print(s)
if tv == 'table' then
table.dump(t, depth + 1, ref)
end
end
print(indent .. '}')
else
print(indent .. t)
end
end
function table.removeValue(t, v)
for k, tv in pairs(t) do
if v == tv then
t[k] = nil
return
end
end
end