2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass table
|
|
|
|
|
2011-08-28 23:32:24 +02:00
|
|
|
function table.dump(t, depth)
|
|
|
|
if not depth then depth = 0 end
|
|
|
|
for k,v in pairs(t) do
|
2011-12-07 01:31:55 +01:00
|
|
|
str = (' '):rep(depth * 2) .. k .. ': '
|
2011-08-28 23:32:24 +02:00
|
|
|
if type(v) ~= "table" then
|
2011-11-17 21:40:31 +01:00
|
|
|
print(str .. tostring(v))
|
2011-08-28 23:32:24 +02:00
|
|
|
else
|
|
|
|
print(str)
|
|
|
|
table.dump(v, depth+1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function table.copy(t)
|
2012-08-19 10:48:25 +02:00
|
|
|
local res = {}
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
res[k] = v
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
function table.recursivecopy(t)
|
2011-08-28 23:32:24 +02:00
|
|
|
local res = {}
|
|
|
|
for k,v in pairs(t) do
|
2012-08-17 07:31:23 +02:00
|
|
|
if type(v) == "table" then
|
2012-08-19 10:48:25 +02:00
|
|
|
res[k] = table.recursivecopy(v)
|
2012-08-17 07:31:23 +02:00
|
|
|
else
|
|
|
|
res[k] = v
|
|
|
|
end
|
2011-08-28 23:32:24 +02:00
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
2012-01-05 02:55:07 +01:00
|
|
|
function table.selectivecopy(t, keys)
|
2011-08-28 23:32:24 +02:00
|
|
|
local res = { }
|
|
|
|
for i,v in ipairs(keys) do
|
|
|
|
res[v] = t[v]
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
function table.merge(t, src)
|
|
|
|
for k,v in pairs(src) do
|
|
|
|
t[k] = v
|
|
|
|
end
|
|
|
|
end
|
2012-01-05 02:55:07 +01:00
|
|
|
|
2013-01-19 19:02:22 +01:00
|
|
|
function table.find(t, value, lowercase)
|
2012-01-05 02:55:07 +01:00
|
|
|
for k,v in pairs(t) do
|
2013-01-19 19:02:22 +01:00
|
|
|
if lowercase and type(value) == 'string' and type(v) == 'string' then
|
|
|
|
if v:lower() == value:lower() then return k end
|
|
|
|
end
|
2012-01-05 02:55:07 +01:00
|
|
|
if v == value then return k end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-29 07:26:32 +01:00
|
|
|
function table.findbykey(t, key, lowercase)
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if lowercase and type(key) == 'string' and type(k) == 'string' then
|
|
|
|
if k:lower() == key:lower() then return v end
|
|
|
|
end
|
|
|
|
if k == key then return v end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-12 17:14:16 +01:00
|
|
|
function table.contains(t, value, lowercase)
|
|
|
|
return table.find(t, value, lowercase) ~= nil
|
2012-07-20 20:20:06 +02:00
|
|
|
end
|
|
|
|
|
2013-01-29 07:26:32 +01:00
|
|
|
function table.findkey(t, key)
|
2012-07-19 20:54:24 +02:00
|
|
|
if t and type(t) == 'table' then
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if k == key then return k end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-29 07:26:32 +01:00
|
|
|
function table.haskey(t, key)
|
|
|
|
return table.findkey(t, key) ~= nil
|
2012-07-19 20:54:24 +02:00
|
|
|
end
|
|
|
|
|
2012-01-05 02:55:07 +01:00
|
|
|
function table.removevalue(t, value)
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if v == value then
|
2012-01-07 21:00:07 +01:00
|
|
|
table.remove(t, k)
|
2013-01-26 23:12:00 +01:00
|
|
|
return true
|
2012-01-05 02:55:07 +01:00
|
|
|
end
|
|
|
|
end
|
2013-01-26 23:12:00 +01:00
|
|
|
return false
|
2012-01-07 21:00:07 +01:00
|
|
|
end
|
|
|
|
|
2013-01-26 21:10:30 +01:00
|
|
|
function table.popvalue(value)
|
|
|
|
local index = nil
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if v == value or not value then
|
|
|
|
index = k
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if index then
|
|
|
|
table.remove(t, index)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2012-01-07 21:00:07 +01:00
|
|
|
function table.compare(t, other)
|
|
|
|
if #t ~= #other then return false end
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if v ~= other[k] then return false end
|
2012-01-05 02:55:07 +01:00
|
|
|
end
|
2012-01-07 21:00:07 +01:00
|
|
|
return true
|
2012-01-05 02:55:07 +01:00
|
|
|
end
|
2012-07-12 18:45:22 +02:00
|
|
|
|
|
|
|
function table.empty(t)
|
2012-07-19 20:54:24 +02:00
|
|
|
if t and type(t) == 'table' then
|
2012-07-12 18:45:22 +02:00
|
|
|
return next(t) == nil
|
|
|
|
end
|
|
|
|
return true
|
2012-08-19 10:48:25 +02:00
|
|
|
end
|
2012-12-28 12:05:45 +01:00
|
|
|
|
2013-01-16 17:20:17 +01:00
|
|
|
function table.permute(t, n, count)
|
|
|
|
n = n or #t
|
|
|
|
for i=1,count or n do
|
|
|
|
local j = math.random(i, n)
|
|
|
|
t[i], t[j] = t[j], t[i]
|
|
|
|
end
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
function table.findbyfield(t, fieldname, fieldvalue)
|
|
|
|
for _i,subt in pairs(t) do
|
|
|
|
if subt[fieldname] == fieldvalue then
|
|
|
|
return subt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2013-02-20 11:08:03 +01:00
|
|
|
function table.size(t)
|
|
|
|
local size = 0
|
|
|
|
for i, n in pairs(table) do
|
|
|
|
size = size + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
return size
|
|
|
|
end
|
|
|
|
|
2013-01-29 07:26:32 +01:00
|
|
|
function table.tostring(t)
|
2012-12-28 12:05:45 +01:00
|
|
|
local maxn = #t
|
|
|
|
local str = ""
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
if k == maxn and k ~= 1 then
|
|
|
|
str = str .. " and " .. v
|
|
|
|
elseif maxn > 1 and k ~= 1 then
|
|
|
|
str = str .. ", " .. v
|
|
|
|
else
|
|
|
|
str = str .. " " .. v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return str
|
|
|
|
end
|