2025-01-09 13:36:39 +08:00

259 lines
5.8 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 拷贝table
function table.DeepCopy(object)
-- 已经复制过的tablekey为复制源tablevalue为复制后的table
-- 为了防止table中的某个属性为自身时出现死循环
-- 避免本该是同一个table的属性在复制时变成2个不同的table(内容同,但是地址关系和原来的不一样了)
local lookup_table = {}
local function _copy(object)
if type(object) ~= 'table' then -- 非table类型都直接返回
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for k, v in pairs(object) do
new_table[_copy(k)] = _copy(v)
end
-- 这里直接拿mt来用是因为一般对table操作不会很粗暴的修改mt的相关内容
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
function table.NewLuaObj(InObj)
return setmetatable({}, {
__index = InObj,
__metatable = InObj,
})
end
---获取表元素个数
function table.getCount(t)
if type(t) ~= "table" then
return -1
end
local Length = 0
for i, v in pairs(t) do
Length = Length + 1
end
return Length
end
---获取表中所有的Key
function table.getKeys(t)
if type(t) ~= "table" then
return {}
end
local keys = {}
for k,v in pairs(t) do
keys[#keys + 1] = k
end
return keys
end
---获取对应元素的Index
function table.getIndex(t, v)
if type(t) ~= "table" then
return nil
end
for index, value in pairs(t) do
if value == v then
return index
end
end
return nil
end
---获取表中是否有对应Key
function table.hasKey(t, k)
if type(t) ~= "table" then
return false
end
for key, value in pairs(t) do
if k == key then
return true
end
end
return false
end
---按Key删除表中元素
function table.removeKey(t, k)
if t == nil then
return nil
end
local v = t[k]
t[k] = nil
return v
end
---获取表中是否有对应的Value
function table.hasValue(t, value)
if t == nil then return false end
for k, v in pairs(t) do
if v == value then
return true
end
end
return false
end
---按Value删除表中元素
function table.removeValue(t, value, removeAll)
local deleteNum = 0
local i = 1
local max = table.getCount(t)
while i <= max do
if t[i] == value then
table.remove(t,i)
deleteNum = deleteNum + 1
i = i - 1
max = max - 1
if not removeAll then break end
end
i = i + 1
end
return deleteNum
end
---返回是否是空表
function table.isEmpty(t)
if type(t) ~= "table" then
return true
end
return next(t) == nil
end
function table.Rand(t)
if type(t) ~= "table" or #t <= 0 then return nil end
return t[math.random(1, #t)]
end
function table.GetMaxValueResKey(t, fun)
if fun == nil then fun = function(p1, p2) return p1 < p2 end end
if type(t) ~= "table" or type(fun) ~= "function" or table.getCount(t) <= 0 then return end
local res = nil
for k, v in pairs(t) do
if not res or fun(t[res], v) then
res = k
end
end
return res
end
function table.FindKey(t, value)
if type(t) ~= "table" then return nil end
for k, v in pairs(t) do
if v == value then return k end
end
return nil
end
function table.GetMaxValue(t)
if type(t) ~= "table" then return nil end
local Res = nil
for k, v in pairs(t) do
if Res == nil then Res = v
elseif Res < v then Res = v
end
end
return Res
end
function table.Swap(t, i, k)
if type(t) ~= "table" then
return false
end
local temp = t[i]
t[i] = t[k]
t[k] = temp
end
--- 打乱整个table 洗牌算法
function table.Shuffle(t)
if type(t) ~= "table" then
return false
end
for i = 1, #t - 1 do
local SwapIndex = math.random(i + 1, #t)
table.Swap(t, i, SwapIndex)
end
end
--- 判断table相等
function table.tablesEqual(table1, table2)
-- 检查table1和table2是否为同一个table
if table1 == table2 then
return true
end
-- 检查table类型
if type(table1) ~= 'table' or type(table2) ~= 'table' then
return false
end
-- 检查table1中的键值对是否在table2中
for key, value in pairs(table1) do
if type(value) == 'table' then
if not tablesEqual(value, table2[key]) then
return false
end
else
if table2[key] ~= value then
return false
end
end
end
-- 检查table2中是否有table1中没有的键
for key in pairs(table2) do
if table1[key] == nil then
return false
end
end
return true
end
--- 判断table集合相等
function table.collectionsEqual(collection1, collection2)
local count1 = 0
local count2 = 0
-- 计算第一个集合的大小
for _ in pairs(collection1) do
count1 = count1 + 1
end
-- 计算第二个集合的大小
for _ in pairs(collection2) do
count2 = count2 + 1
end
-- 如果大小不同,则集合不同
if count1 ~= count2 then
return false
end
-- 检查collection1中的每个元素是否也在collection2中
for key1, value1 in pairs(collection1) do
local found = false
for key2, value2 in pairs(collection2) do
if value1 == value2 then
found = true
break
end
end
if not found then
return false
end
end
return true
end