This repository has been archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTableUtils.lua
306 lines (275 loc) · 7.14 KB
/
TableUtils.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
local addonName, OQ = ...
local L = OQ._T -- for literal string translations
if (OQ.table == nil) then
OQ.table = {}
end
local tbl = OQ.table
function tbl.clear(t, deep)
if (t) then
if (deep) then
for k in pairs(t) do
if (type(t[k]) == 'table') then
tbl.delete(tbl.clear(t[k], deep)) -- clear sub tables and be able to reclaim
end
t[k] = nil
end
else
wipe(t)
end
end
return t
end
function tbl.copy(src, dest, clear_first)
if (dest == nil) then
dest = tbl.new()
end
if (clear_first) then
tbl.clear(dest, true)
end
for i, v in pairs(src) do
if (type(v) == 'table') then
dest[i] = tbl.copy(v, dest[i])
else
dest[i] = v
end
end
return dest
end
function tbl.delete(t, deep)
if (t) and (type(t) == 'table') then
tbl.clear(t, deep)
if (tbl._watchlist == nil) then
tbl._watchlist = tbl.new()
end
if (tbl._watchlist[t]) then
print(debugstack())
print(L['**warning: returning watched table '] .. tostring(t))
end
tbl.__inuse[t] = nil
tbl.push(tbl.__pool, t)
end
return nil
end
function tbl.dump(t, s)
for i, v in pairs(t) do
if (type(v) == 'table') then
tbl.dump(v, (s or '') .. '-')
else
print((s or '') .. ' ' .. tostring(i) .. ': ' .. tostring(v))
end
end
end
function tbl.fill(t, ...)
local n = 0
if (t) then
if (tbl._watchlist[t]) then
print(debugstack())
print(L['**warning: watched table found '] .. tostring(t))
end
tbl.clear(t)
for i = 1, select('#', ...) do
t[i] = select(i, ...)
n = n + 1
end
end
return n
end
function tbl.fill_match(t, str, ch)
tbl.clear(t, true)
if (str == nil) then
return
end
local n = 0
local p1 = 0
local p2 = 1
while (p2 ~= nil) do
p2 = str:find(ch, p1, true)
n = n + 1
t[n] = str:sub(p1, (p2 or 0) - 1)
if (p2) then
p1 = p2 + 1
end
end
return n
end
function tbl.find_keybyvalue(t, v)
if (t) and (v) then
for key, val in pairs(t) do
if (val == v) then
return key
end
end
end
return nil
end
function tbl.init()
tbl._watchlist = tbl._watchlist or tbl.new()
end
function tbl.new()
if (tbl.__pool == nil) then
tbl.__pool = {}
end
if (tbl.__inuse == nil) then
tbl.__inuse = {}
end
local ndx = tbl.next(tbl.__pool)
local t
if (ndx) then
t = tbl.__pool[ndx]
tbl.__pool[ndx] = nil
else
t = {}
tbl._count = (tbl._count or 0) + 1
end
tbl.track(t) -- debugging
if (tbl.__inuse[t]) then
print(debugstack())
print('**warning: re-issued active table: ' .. tostring(t))
end
tbl.__inuse[t] = 1
return t
end
function tbl.next(t)
if (t) then
for i, _ in pairs(t) do
if (i ~= 0) then
return i
end
end
end
return nil
end
function tbl.push(t, v)
if (t) and (v) then
tbl.__ticker = (tbl.__ticker or 0) + 1
t[tbl.__ticker] = v
end
end
function tbl.size(t, type_)
if (t == nil) then
return nil
end
local n = 0
for _, v in pairs(t) do
if (type_ == nil) or (type(v) == type_) then
n = n + 1
end
end
return n
end
function tbl.track(t)
tbl._track = tbl._track or {}
tbl._track[t] = GetTime()
end
function tbl.dump_track(min_cnt, dump, sub)
tbl._track = tbl._track or {}
min_cnt = tonumber(min_cnt or 0) or 0
print('-- table track dump ')
local dt, n, cnt
local now = GetTime()
for i, _ in pairs(tbl._track) do
n = tbl.size(i)
cnt = (cnt or 0) + 1
if (n >= min_cnt) then
print(' ' .. tostring(i) .. ' sz: ' .. tostring(n))
if (dump) and (type(i) == 'table') then
for j, x in pairs(i) do
if (sub == nil) or ((sub) and (type(j) == 'string') and j:find(sub)) then
print(' ' .. tostring(j) .. ' (' .. type(j) .. ') [' .. tostring(x) .. ']')
end
end
end
end
end
print('# tables: ' .. tostring(cnt))
end
function tbl.watch(t)
tbl._watchlist[t] = 1
end
function tbl.unwatch(t)
tbl._watchlist[t] = nil
end
function tbl.__genOrderedIndex(t, orderedIndex)
if (orderedIndex == nil) then
orderedIndex = tbl.new()
else
tbl.clear(orderedIndex)
end
for key in pairs(t) do
table.insert(orderedIndex, key)
end
table.sort(orderedIndex)
return orderedIndex
end
function tbl.orderedNext(t, state)
-- Equivalent of the next function, but returns the keys in the alphabetic
-- order. We use a temporary ordered key table that is stored in the
-- table being iterated.
if state == nil then
-- the first time, generate the index
t.__orderedIndex = tbl.__genOrderedIndex(t, t.__orderedIndex)
local key = t.__orderedIndex[1]
return key, t[key]
end
-- fetch the next value
local key = nil
for i = 1, table.getn(t.__orderedIndex) do
if t.__orderedIndex[i] == state then
key = t.__orderedIndex[i + 1]
end
end
if key then
return key, t[key]
end
-- no more value to return, cleanup
t.__orderedIndex = tbl.delete(t.__orderedIndex)
return
end
function tbl.__genOrderedValue(t, orderedIndex)
if (orderedIndex == nil) then
orderedIndex = tbl.new()
else
tbl.clear(orderedIndex)
end
for _, value in pairs(t) do
table.insert(orderedIndex, value)
end
table.sort(orderedIndex)
return orderedIndex
end
function tbl.orderedByValueNext(t, state)
-- Equivalent of the next function, but returns the keys in the alphabetic
-- order. We use a temporary ordered key table that is stored in the
-- table being iterated.
local key = nil
local n
if state == nil then
-- the first time, generate the index
t.__orderedIndex = tbl.__genOrderedValue(t, t.__orderedIndex)
key = tbl.find_keybyvalue(t, t.__orderedIndex[1])
return key, t.__orderedIndex[1]
end
-- fetch the next value
for i = 1, table.getn(t.__orderedIndex) do
if t.__orderedIndex[i] == t[state] then
key = tbl.find_keybyvalue(t, t.__orderedIndex[i + 1])
n = i + 1
end
end
if key then
return key, t.__orderedIndex[n]
end
-- no more value to return, cleanup
t.__orderedIndex = tbl.delete(t.__orderedIndex)
return
end
function tbl.orderedPairs(t)
-- Equivalent of the pairs() function on tables. Allows to iterate
-- in order
return tbl.orderedNext, t, nil
end
function tbl.orderedByValuePairs(t)
-- Equivalent of the pairs() function on tables. Allows to iterate
-- in order
return tbl.orderedByValueNext, t, nil
end