minecraft-cc-tools/libs/utils.lua
2024-05-20 20:51:37 +02:00

60 lines
895 B
Lua

local utils = {}
utils.sizeof = function(t)
if not t then
return 0
end
local size = 0
for k,v in pairs(t) do
size = size + 1
end
return size
end
utils.shallowDiff = function(a, b)
local counters = {}
for k,v in pairs(a) do
local currentCounter = counters[v] or 0
counters[v] = currentCounter + 1
end
for k,v in pairs(b) do
if counters[v] then
counters[v] = counters[v] - 1
end
end
local ret = {}
local n = 0
for k,v in pairs(a) do
if counters[v] and counters[v] > 0 then
counters[v] = counters[v] - 1
n = n + 1
ret[n] = v
end
end
return ret
end
utils.shallowClone = function(t)
local res = {}
for k,v in pairs(t) do
res[k] = v
end
return res
end
utils.find = function(t, predicate)
for k,v in pairs(t) do
if predicate(v, k) then
return v, k
end
end
end
return utils