forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableUtils.lua
More file actions
389 lines (324 loc) · 9.07 KB
/
TableUtils.lua
File metadata and controls
389 lines (324 loc) · 9.07 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
----------------------------------------------------------------------------------
--- Mudlet Table Utils
----------------------------------------------------------------------------------
--- Tests if a table is empty: this is useful in situations where you find
--- yourself wanting to do 'if my_table == {}' and such.
---
--- @usage Testing if the table is empty.
--- <pre>
--- myTable = {}
--- if table.is_empty(myTable) then
--- echo("myTable is empty")
--- end
--- </pre>
function table.is_empty(tbl)
if next(tbl) == nil then return true else return false end
end
--- Lua debug function that prints the content of a Lua table on the screen, split up in keys and values.
--- Useful if you want to see what the capture groups contain i. e. the Lua table "matches".
---
--- @see display
function printTable( map )
echo("-------------------------------------------------------\n");
for k, v in pairs( map ) do
echo( "key=" .. k .. " value=" .. v .. "\n" )
end
echo("-------------------------------------------------------\n");
end
-- NOT LUADOC
-- This is supporting function for printTable().
function __printTable( k, v )
insertText ("\nkey = " .. tostring (k) .. " value = " .. tostring( v ) )
end
--- Lua debug function that prints the content of a Lua table on the screen. <br/>
--- There are currently 3 functions with similar behaviour.
---
--- @see display
--- @see printTable
function listPrint( map )
echo("-------------------------------------------------------\n");
for k,v in ipairs( map ) do
echo( k .. ". ) "..v .. "\n" );
end
echo("-------------------------------------------------------\n");
end
--- <b><u>TODO</u></b> listAdd( list, what )
function listAdd( list, what )
table.insert( list, what );
end
--- <b><u>TODO</u></b> listRemove( list, what )
function listRemove( list, what )
for k,v in ipairs( list ) do
if v == what then
table.remove( list, k )
end
end
end
--- Gets the actual size of non-index based tables. <br/><br/>
---
--- For index based tables you can get the size with the # operator: <br/>
--- This is the standard Lua way of getting the size of index tables i.e. ipairs() type of tables with
--- numerical indices. To get the size of tables that use user defined keys instead of automatic indices
--- (pairs() type) you need to use the function table.size() referenced above.
--- <pre>
--- myTableSize = # myTable
--- </pre>
function table.size(t)
if not t then
return 0
end
local i = 0
for k, v in pairs(t) do
i = i + 1
end
return i
end
--- Determines if a table contains a value as a key or as a value (recursive).
function table.contains(t, value)
if type(t) ~= "table" then return nil, "first parameter passed isn't a table" end
for k, v in pairs(t) do
if v == value then
return true
elseif k == value then
return true
elseif type(v) == "table" then
if table.contains(v, value) then return true end
end
end
return false
end
--- Table Union.
---
--- @return Returns a table that is the union of the provided tables. This is a union of key/value
--- pairs. If two or more tables contain different values associated with the same key,
--- that key in the returned table will contain a subtable containing all relevant values.
--- See table.n_union() for a union of values. Note that the resulting table may not be
--- reliably traversable with ipairs() due to the fact that it preserves keys. If there
--- is a gap in numerical indices, ipairs() will cease traversal.
---
--- @usage Example:
--- <pre>
--- tableA = {
--- [1] = 123,
--- [2] = 456,
--- ["test"] = "test",
--- }
---
--- tableB = {
--- [1] = 23,
--- [3] = 7,
--- ["test2"] = function() return true end,
--- }
---
--- tableC = {
--- [5] = "c",
--- }
---
--- table.union(tableA, tableB, tableC) will return:
--- {
--- [1] = {
--- 123,
--- 23,
--- },
--- [2] = 456,
--- [3] = 7,
--- [5] = "c",
--- ["test"] = "test",
--- ["test2"] = function() return true end,
--- }
--- </pre>
function table.union(...)
local sets = {...}
local union = {}
for _, set in ipairs(sets) do
for key, val in pairs(set) do
if union[key] and union[key] ~= val then
if type(union[key]) == 'table' then
table.insert(union[key], val)
else
union[key] = { union[key], val }
end
else
union[key] = val
end
end
end
return union
end
--- Table Union.
---
--- @return Returns a numerically indexed table that is the union of the provided tables. This is
--- a union of unique values. The order and keys of the input tables are not preserved.
function table.n_union(...)
local sets = {...}
local union = {}
local union_keys = {}
for _, set in ipairs(sets) do
for key, val in pairs(set) do
if not union_keys[val] then
union_keys[val] = true
table.insert(union, val)
end
end
end
return union
end
--- Table Intersection.
---
--- @return Returns a table that is the intersection of the provided tables. This is an
--- intersection of key/value pairs. See table.n_intersection() for an intersection of values.
--- Note that the resulting table may not be reliably traversable with ipairs() due to
--- the fact that it preserves keys. If there is a gap in numerical indices, ipairs() will
--- cease traversal.
---
--- @usage Example:
--- <pre>
--- tableA = {
--- [1] = 123,
--- [2] = 456,
--- [4] = { 1, 2 },
--- [5] = "c",
--- ["test"] = "test",
--- }
---
--- tableB = {
--- [1] = 123,
--- [2] = 4,
--- [3] = 7,
--- [4] = { 1, 2 },
--- ["test"] = function() return true end,
--- }
---
--- tableC = {
--- [1] = 123,
--- [4] = { 1, 2 },
--- [5] = "c",
--- }
---
--- table.intersection(tableA, tableB, tableC) will return:
--- {
--- [1] = 123,
--- [4] = { 1, 2 },
--- }
--- </pre>
function table.intersection(...)
sets = {...}
if #sets < 2 then return false end
local intersection = {}
local function intersect(set1, set2)
local result = {}
for key, val in pairs(set1) do
if set2[key] then
if _comp(val, set2[key]) then result[key] = val end
end
end
return result
end
intersection = intersect(sets[1], sets[2])
for i, _ in ipairs(sets) do
if i > 2 then
intersection = intersect(intersection, sets[i])
end
end
return intersection
end
--- Table Intersection.
---
--- @return Returns a numerically indexed table that is the intersection of the provided tables.
--- This is an intersection of unique values. The order and keys of the input tables are
--- not preserved.
function table.n_intersection(...)
sets = {...}
if #sets < 2 then return false end
local intersection = {}
local function intersect(set1, set2)
local intersection_keys = {}
local result = {}
for _, val1 in pairs(set1) do
for _, val2 in pairs(set2) do
if _comp(val1, val2) and not intersection_keys[val1] then
table.insert(result, val1)
intersection_keys[val1] = true
end
end
end
return result
end
intersection = intersect(sets[1], sets[2])
for i, _ in ipairs(sets) do
if i > 2 then
intersection = intersect(intersection, sets[i])
end
end
return intersection
end
--- Table Complement.
---
--- @return Returns a table that is the relative complement of the first table with respect to
--- the second table. Returns a complement of key/value pairs.
function table.complement(set1, set2)
if not set1 and set2 then return false end
if type(set1) ~= 'table' or type(set2) ~= 'table' then return false end
local complement = {}
for key, val in pairs(set1) do
if not _comp(set2[key], val) then
complement[key] = val
end
end
return complement
end
--- Table Complement.
---
--- @return Returns a table that is the relative complement of the first table with respect to
--- the second table. Returns a complement of values.
function table.n_complement(set1, set2)
if not set1 and set2 then return false end
local complement = {}
for _, val1 in pairs(set1) do
local insert = true
for _, val2 in pairs(set2) do
if _comp(val1, val2) then
insert = false
end
end
if insert then table.insert(complement, val1) end
end
return complement
end
function table.update(t1, t2)
local tbl = {}
for k,v in pairs(t1) do
tbl[k] = v
end
for k,v in pairs(t2) do
if type(v) == "table" then
tbl[k] = table.update(tbl[k] or {}, v)
else
tbl[k] = v
end
end
return tbl
end
---Returns the index of the value in a table
function table.index_of(table, element)
for index, value in ipairs(table) do
if value == element then
return index
end
end
return nil
end
-- returns a deep copy of the table with the metatable intact. Credit to Steve Donovan of Penlight.
function table.deepcopy(t)
if type(t) ~= 'table' then return t end
local mt = getmetatable(t)
local res = {}
for k,v in pairs(t) do
if type(v) == 'table' then
v = table.deepcopy(v)
end
res[k] = v
end
setmetatable(res,mt)
return res
end