2023-05-07 12:20:42 +02:00
|
|
|
-- create metatable
|
|
|
|
orgtype = type
|
|
|
|
|
|
|
|
function type(obj)
|
2024-06-23 13:37:16 +02:00
|
|
|
local _type = orgtype(obj)
|
|
|
|
if "table" == _type and obj._cname then
|
|
|
|
return obj._cname
|
|
|
|
end
|
|
|
|
return _type
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function metatable(...)
|
2024-06-23 13:37:16 +02:00
|
|
|
if ... and type(...) == "table" then
|
|
|
|
return setmetatable(..., { __index = table })
|
|
|
|
else
|
|
|
|
return setmetatable({ ... }, { __index = table })
|
|
|
|
end
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- chech metatble
|
|
|
|
function metatable_chk(tab)
|
2024-06-23 13:37:16 +02:00
|
|
|
if "table" == type(tab) then
|
|
|
|
return (tab.each and tab) or metatable(tab)
|
|
|
|
else
|
|
|
|
return tab
|
|
|
|
end
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.eachi = function(tab, func)
|
2024-06-23 13:37:16 +02:00
|
|
|
for i = 1, #tab do
|
|
|
|
func(tab[i], i)
|
|
|
|
end
|
|
|
|
return tab
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.eacha = function(tab, func)
|
2024-06-23 13:37:16 +02:00
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
func(v, i)
|
|
|
|
end
|
|
|
|
return tab
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.each = function(tab, func)
|
2024-06-23 13:37:16 +02:00
|
|
|
for k, v in pairs(tab) do
|
|
|
|
func(v, k)
|
|
|
|
end
|
|
|
|
return tab
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find_index = function(tab, elm, ...)
|
2024-06-23 13:37:16 +02:00
|
|
|
local _, i = table.find(tab, elm, ...)
|
|
|
|
return i
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find = function(tab, elm, func)
|
2024-06-23 13:37:16 +02:00
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
if elm == v then
|
|
|
|
return v, i
|
|
|
|
end
|
|
|
|
end
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find_with_func = function(tab, elm, ...)
|
2024-06-23 13:37:16 +02:00
|
|
|
local i, v = table.find(tab, elm)
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
2024-02-08 11:39:49 +01:00
|
|
|
table.delete = function(tab, elm, ...)
|
2024-06-23 13:37:16 +02:00
|
|
|
local index = table.find_index(tab, elm)
|
|
|
|
return index and table.remove(tab, index)
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.find_all = function(tab, elm, ...)
|
2024-06-23 13:37:16 +02:00
|
|
|
local tmptab = setmetatable({}, { __index = table })
|
|
|
|
local _func = (type(elm) == "function" and elm) or function(v, k, ...)
|
|
|
|
return v == elm
|
|
|
|
end
|
|
|
|
for k, v in pairs(tab) do
|
|
|
|
if _func(v, k, ...) then
|
|
|
|
tmptab:insert(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tmptab
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
table.select = table.find_all
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.reduce = function(tab, func, arg)
|
2024-06-23 13:37:16 +02:00
|
|
|
local new, old = arg, arg
|
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
new, old = func(v, new)
|
|
|
|
end
|
|
|
|
return new, arg
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
2024-02-08 11:39:49 +01:00
|
|
|
table.map = function(tab, func)
|
2024-06-23 13:37:16 +02:00
|
|
|
local newtab = setmetatable({}, { __index = table })
|
|
|
|
func = func or function(v, i)
|
|
|
|
return v, i
|
|
|
|
end
|
|
|
|
for i, v in ipairs(tab) do
|
|
|
|
newtab[i] = func(v, i)
|
|
|
|
end
|
|
|
|
return newtab
|
|
|
|
end
|
|
|
|
table.map_hash = function(tab, func) -- table to list of array { key, v}
|
|
|
|
local newtab = setmetatable({}, { __index = table })
|
|
|
|
func = func or function(k, v)
|
|
|
|
return { k, v }
|
|
|
|
end
|
|
|
|
for k, v in pairs(tab) do
|
|
|
|
newtab:insert(func(k, v))
|
|
|
|
end
|
|
|
|
return newtab
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
function table:push(elm)
|
2024-06-23 13:37:16 +02:00
|
|
|
self:insert(elm)
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
table.append = table.push
|
|
|
|
function table:pop()
|
2024-06-23 13:37:16 +02:00
|
|
|
return self:remove(#self)
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function table:shift()
|
2024-06-23 13:37:16 +02:00
|
|
|
self:remove(1)
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function table:unshift(elm)
|
2024-06-23 13:37:16 +02:00
|
|
|
self:insert(1, elm)
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function table.len(t)
|
2024-06-23 13:37:16 +02:00
|
|
|
local leng = 0
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
leng = leng + 1
|
|
|
|
end
|
|
|
|
return leng
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- table to string 序列化
|
|
|
|
function table.serialize(obj)
|
2024-06-23 13:37:16 +02:00
|
|
|
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
|
2023-05-07 12:20:42 +02:00
|
|
|
end
|