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
|
--- Auxiliary functions for table manipulation
--
-- @author Daniel Miller
-- @copyright Same as Nmap--See https://nmap.org/book/man-legal.html
-- @class module
-- @name tableaux
local next = next
local pairs = pairs
local ipairs = ipairs
local type = type
local _ENV = {}
local tcopy_local
--- Recursively copy a table.
--
-- Uses simple assignment to copy keys and values from a table, recursing into
-- subtables as necessary.
-- @param t the table to copy
-- @return a deep copy of the table
function tcopy (t)
local tc = {};
for k,v in pairs(t) do
if type(v) == "table" then
tc[k] = tcopy_local(v);
else
tc[k] = v;
end
end
return tc;
end
tcopy_local = tcopy
--- Copy one level of a table.
--
-- Iterates over the keys of a table and copies their values into a new table.
-- If any values are tables, they are copied by reference only, and modifying
-- the copy will modify the original table value as well.
-- @param t the table to copy
-- @return a shallow copy of the table
function shallow_tcopy(t)
local k = next(t)
local out = {}
while k do
out[k] = t[k]
k = next(t, k)
end
return out
end
--- Invert a one-to-one mapping
-- @param t the table to invert
-- @return an inverted mapping
function invert(t)
local out = {}
for k, v in pairs(t) do
out[v] = k
end
return out
end
--- Check for the presence of a value in a table
--@param t the table to search into
--@param item the searched value
--@array (optional) If true, then use ipairs to only search the array indices of the table.
--@return Boolean true if the item was found, false if not
--@return The index or key where the value was found, or nil
function contains(t, item, array)
local iter = array and ipairs or pairs
for k, val in iter(t) do
if val == item then
return true, k
end
end
return false, nil
end
--- Returns the keys of a table as an array
-- @param t The table
-- @return A table of keys
function keys(t)
local ret = {}
local k, v = next(t)
while k ~= nil do
ret[#ret+1] = k
k, v = next(t, k)
end
return ret
end
return _ENV
|