-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableUtil.lua
More file actions
190 lines (163 loc) · 4.09 KB
/
TableUtil.lua
File metadata and controls
190 lines (163 loc) · 4.09 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
TableUtil = {}
local function shadowClone(object)
local target = {}
for key, value in pairs(object) do
target[key] = value
end
return setmetatable(target, getmetatable(object))
end
local function deepClone(object, refTable)
if type(object) ~= "table" then
return object
elseif refTable[object] then
return refTable[object]
end
local target = {}
-- set reference firstly, avoid recursived reference
refTable[object] = target
for key, value in pairs(object) do
target[deepClone(key)] = deepClone(value, refTable)
end
return setmetatable(target, getmetatable(object))
end
local function dumpTable(object, space, name, cache)
local contentTable = {}
for key, value in pairs(object) do
local stringKey = tostring(key)
if cache[value] then
table.insert(contentTable, "+" .. stringKey .. " {" .. cache[value] .. "}")
elseif type(value) == "table" then
local cacheName = name .. "." .. stringKey
cache[value] = cacheName
local nextSpace = (next(object, key) and "|" or " " ) .. string.rep(" ", #stringKey)
local content = dumpTable(value , space .. nextSpace, cacheName, cache)
table.insert(contentTable, "+" .. stringKey .. content)
else
table.insert(contentTable, "+" .. stringKey .. " [" .. tostring(value) .. "]")
end
end
return table.concat(contentTable, "\n" .. space)
end
TableUtil.clone = function (object, deep)
assert(type(object) == "table")
if deep == true then
local refTable = {}
return deepClone(object, refTable)
else
return shadowClone(object)
end
end
TableUtil.print = function(object, printer)
if printer == nil then printer = print end
assert(type(object) == "table")
assert(type(printer) == "function", "Table printer must be a function")
local cache = { [ object ] = "." }
local content = dumpTable(object, "", "", cache)
printer(content)
end
TableUtil.pairsByKey = function(object, keySort)
assert(type(object) == "table")
assert(type(keySort) == "function", "Key sort must be a function")
local keyArray = {}
for key in pairs(object) do
table.insert(keyArray, key)
end
table.sort(keyArray, keySort)
local idx = 0 -- iterator variable
local iter = function() -- iterator function
idx = idx + 1
if keyArray[idx] == nil then
return nil
else
return keyArray[idx], object[keyArray[idx]]
end
end
return iter
end
TableUtil.pairsByValue = function(object, valueSort)
assert(type(object) == "table")
assert(type(valueSort) == "function", "Value sort must be a function")
local valueArray, reversedObject = {}, {}
for key, value in pairs(object) do
table.insert(valueArray, value)
reversedObject[value] = key
end
table.sort(valueArray, valueSort)
local idx = 0
local iter = function()
idx = idx + 1
if valueArray[idx] == nil then
return nil
else
return reversedObject[valueArray[idx]], valueArray[idx]
end
end
return iter
end
-- bubble sort for in-place and stable
TableUtil.sortOn = function(arr, key)
assert(type(arr) == "table")
local n = #arr
for i = 1, n do
local m = i + 1
for j = n, m, -1 do
local obj1 = arr[j]
local obj2 = arr[j - 1]
if obj1[key] < obj2[key] then
arr[j] = obj2
arr[j - 1] = obj1
end
end
end
end
TableUtil.sortOnFunc = function(arr, func, key)
local n = #arr
for i = 1, n do
local m = i + 1
for j = n, m, -1 do
local obj1 = arr[j]
local obj2 = arr[j - 1]
if obj1[func](obj1, key) < obj2[func](obj2, key) then
arr[j] = obj2
arr[j - 1] = obj1
end
end
end
end
TableUtil.reverse = function(arr)
local n = #arr
for i = 1, math.floor(n / 2) do
local k = n - i + 1
local tmp = arr[i]
arr[i] = arr[k]
arr[k] = tmp
end
end
TableUtil.len = function(tbl)
if tbl == nil then return nil end
local i = 0
for k,v in pairs(tbl) do
i = i + 1
end
return i
end
TableUtil.keys = function(tbl)
local kt = {}
local i = 1
for k,_ in pairs(tbl) do
kt[i] = k
i = i + 1
end
return unpack(kt)
end
TableUtil.indexOf = function(arr, obj)
assert(type(arr) == "table")
local n = #arr
for i = 1, n do
local value = arr[i]
if value == obj then
return i
end
end
return -1
end