mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2025-04-22 23:25:15 +02:00
Optimize table.intersect
This commit is contained in:
parent
e4b5673642
commit
ee386080eb
1 changed files with 4 additions and 3 deletions
|
@ -58,12 +58,13 @@ function table.intersect(a, b)
|
|||
local values_map = {}
|
||||
|
||||
for _,v in pairs(a) do values_map[v] = 1 end
|
||||
for _,v in pairs(b) do values_map[v] = (values_map[v] or 0) + 1 end
|
||||
|
||||
-- Get all the values that are in both tables
|
||||
local result = {}
|
||||
for v,count in pairs(values_map) do
|
||||
if count == 2 then result[#result + 1] = v end
|
||||
for _,v in pairs(b) do
|
||||
if values_map[v] then
|
||||
result[#result + 1] = v
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue