2023-05-07 12:20:42 +02:00
|
|
|
-- create metatable
|
|
|
|
orgtype = type
|
|
|
|
|
|
|
|
function type(obj)
|
|
|
|
local _type = orgtype(obj)
|
|
|
|
if "table" == _type and obj._cname then
|
|
|
|
return obj._cname
|
|
|
|
end
|
|
|
|
return _type
|
|
|
|
end
|
|
|
|
|
|
|
|
function metatable(...)
|
|
|
|
if ... and type(...) == "table" then
|
|
|
|
return setmetatable(..., { __index = table })
|
|
|
|
else
|
|
|
|
return setmetatable({ ... }, { __index = table })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- chech metatble
|
|
|
|
function metatable_chk(tab)
|
|
|
|
if "table" == type(tab)
|
|
|
|
then
|
|
|
|
return (tab.each and tab) or metatable(tab)
|
|
|
|
else
|
|
|
|
return tab
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.eachi = function(tab, func)
|
2023-05-07 12:20:42 +02:00
|
|
|
for i = 1, #tab do
|
|
|
|
func(tab[i], i)
|
|
|
|
end
|
|
|
|
return tab
|
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.eacha = function(tab, func)
|
2023-05-07 12:20:42 +02:00
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
func(v, i)
|
|
|
|
end
|
|
|
|
return tab
|
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.each = function(tab, func)
|
2023-05-07 12:20:42 +02:00
|
|
|
for k, v in pairs(tab) do
|
|
|
|
func(v, k)
|
|
|
|
end
|
|
|
|
return tab
|
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find_index = function(tab, elm, ...)
|
2023-05-07 12:20:42 +02:00
|
|
|
local _, i = table.find(tab, elm, ...)
|
|
|
|
return i
|
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find = function(tab, elm, func)
|
2023-05-07 12:20:42 +02:00
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
if elm == v then
|
|
|
|
return v, i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find_with_func = function(tab, elm, ...)
|
2023-05-07 12:20:42 +02:00
|
|
|
local i, v = table.find(tab, elm)
|
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.delete = function(tab, elm, ...)
|
2023-05-07 12:20:42 +02:00
|
|
|
local index = table.find_index(tab, elm)
|
|
|
|
return index and table.remove(tab, index)
|
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find_all = function(tab, elm, ...)
|
2023-05-07 12:20:42 +02:00
|
|
|
local tmptab = setmetatable({}, { __index = table })
|
2024-02-08 11:39:49 +01:00
|
|
|
local _func = (type(elm) == "function" and elm) or function(v, k, ...) return v == elm end
|
2023-05-07 12:20:42 +02:00
|
|
|
for k, v in pairs(tab) do
|
|
|
|
if _func(v, k, ...) then
|
|
|
|
tmptab:insert(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tmptab
|
|
|
|
end
|
|
|
|
table.select = table.find_all
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.reduce = function(tab, func, arg)
|
2023-05-07 12:20:42 +02:00
|
|
|
local new, old = arg, arg
|
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
new, old = func(v, new)
|
|
|
|
end
|
|
|
|
return new, arg
|
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.map = function(tab, func)
|
2023-05-07 12:20:42 +02:00
|
|
|
local newtab = setmetatable({}, { __index = table })
|
2024-02-08 11:39:49 +01:00
|
|
|
func = func or function(v, i) return v, i end
|
2023-05-07 12:20:42 +02:00
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
newtab[i] = func(v, i)
|
|
|
|
end
|
|
|
|
return newtab
|
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.map_hash = function(tab, func) -- table to list of array { key, v}
|
2023-05-07 12:20:42 +02:00
|
|
|
local newtab = setmetatable({}, { __index = table })
|
2024-02-08 11:39:49 +01:00
|
|
|
func = func or function(k, v) return { k, v } end
|
2023-05-07 12:20:42 +02:00
|
|
|
for k, v in pairs(tab) do
|
|
|
|
newtab:insert(func(k, v))
|
|
|
|
end
|
|
|
|
return newtab
|
|
|
|
end
|
|
|
|
function table:push(elm)
|
|
|
|
self:insert(elm)
|
|
|
|
end
|
|
|
|
|
|
|
|
table.append = table.push
|
|
|
|
function table:pop()
|
|
|
|
return self:remove(#self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function table:shift()
|
|
|
|
self:remove(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
function table:unshift(elm)
|
|
|
|
self:insert(1, elm)
|
|
|
|
end
|
|
|
|
|
|
|
|
function table.len(t)
|
|
|
|
local leng = 0
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
leng = leng + 1
|
|
|
|
end
|
|
|
|
return leng;
|
|
|
|
end
|
|
|
|
|
|
|
|
-- table to string 序列化
|
|
|
|
function table.serialize(obj)
|
|
|
|
local serialize_str = ""
|
|
|
|
local t = type(obj)
|
|
|
|
if t == "number" then
|
|
|
|
serialize_str = serialize_str .. obj
|
|
|
|
elseif t == "boolean" then
|
|
|
|
serialize_str = serialize_str .. tostring(obj)
|
|
|
|
elseif t == "string" then
|
|
|
|
serialize_str = serialize_str .. string.format("%q", obj)
|
|
|
|
elseif t == "table" then
|
|
|
|
serialize_str = serialize_str .. "{ "
|
|
|
|
local record_sep = #obj < 4 and ", " or ",\n"
|
|
|
|
local record_prefix = #obj < 4 and "" or "\t"
|
|
|
|
for k, v in pairs(obj) do
|
|
|
|
if type(k) == "number" then
|
|
|
|
serialize_str = serialize_str .. record_prefix .. '"' .. v .. '"' .. record_sep
|
|
|
|
else
|
|
|
|
serialize_str = serialize_str .. "\t[" .. table.serialize(k) .. "] = " .. table.serialize(v) .. ",\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- local metatable = getmetatable(obj)
|
|
|
|
-- if metatable ~= nil and type(metatable.__index) == "table" then
|
|
|
|
-- for k, v in pairs(metatable.__index) do
|
|
|
|
-- serialize_str = serialize_str .. "[" .. table.serialize(k) .. "]=" .. table.serialize(v) .. ",\n"
|
|
|
|
-- end
|
|
|
|
-- end
|
|
|
|
serialize_str = serialize_str .. "}"
|
|
|
|
elseif t == "nil" then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
error("can not serialize a " .. t .. " type.")
|
|
|
|
end
|
|
|
|
return serialize_str
|
|
|
|
end
|