-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.lua
More file actions
52 lines (49 loc) · 1.37 KB
/
Table.lua
File metadata and controls
52 lines (49 loc) · 1.37 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
Table = {}
local function aux(tbl, indent)
local acc = ""
for k, v in pairs(tbl) do
local formatting = string.rep(" ", indent) .. (type(k) == "table" and aux(k, indent) or tostring(k)) .. ": "
if type(v) == "table" then
acc = acc .. formatting .. "\n" .. aux(v, indent+1)
else
acc = acc .. formatting .. tostring(v) .. "\n"
end
end
return acc
end
function Table.toString(tbl) return aux(tbl, 0) end
function Table.print(tbl) print(Table.toString(tbl)) end
function Table.count(tbl)
local res = 0
for k,v in pairs(tbl) do
res = res + 1
end
return res
end
function Table.first(tbl) for k,v in pairs(tbl) do return v end end
function Table.deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[Table.deepcopy(orig_key)] = Table.deepcopy(orig_value)
end
setmetatable(copy, Table.deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function Table.contains(tbl, e)
for _,v in pairs(tbl) do
if v == e then return true end
end
return false
end
function Table.containsKey(tbl, key)
for k,_ in pairs(tbl) do
if k == key then return true end
end
return false
end