]> git.lizzy.rs Git - xdecor.git/blob - handlers/helpers.lua
13d3ed70a8866a04a9b5db35081983ccf87f9b26
[xdecor.git] / handlers / helpers.lua
1 -- Returns the greatest numeric key in a table.
2 function xdecor.maxn(T)
3         local n = 0
4         for k in pairs(T) do
5                 if k > n then n = k end
6         end
7         return n
8 end
9
10 -- Returns the length of an hash table.
11 function xdecor.tablelen(T)
12         local n = 0
13         for _ in pairs(T) do n = n + 1 end
14         return n
15 end
16
17 -- Deep copy of a table. Borrowed from mesecons mod (https://github.com/Jeija/minetest-mod-mesecons).
18 function xdecor.tablecopy(T)
19         if type(T) ~= "table" then return T end -- No need to copy.
20         local new = {}
21
22         for k, v in pairs(T) do
23                 if type(v) == "table" then
24                         new[k] = xdecor.tablecopy(v)
25                 else
26                         new[k] = v
27                 end
28         end
29         return new
30 end
31