此问题类似于之前发布的问题How can I deep-compare 2 Lua tables, which may or may not have tables as keys?
问题是,那里的解决方案非常适合简单的深度比较。但是,它不能正确处理循环引用。更具体地说,以下内容:
function table_eq(table1, table2)
local avoid_loops = {}
local function recurse(t1, t2)
-- compare value types
if type(t1) ~= type(t2) then return false end
-- Base case: compare simple values
if type(t1) ~= "table" then return t1 == t2 end
-- Now, on to tables.
-- First, let's avoid looping forever.
if avoid_loops[t1] then return avoid_loops[t1] == t2 end
avoid_loops[t1] = t2
-- Copy keys from t2
local t2keys = {}
local t2tablekeys = {}
for k, _ in pairs(t2) do
if type(k) == "table" then table.insert(t2tablekeys, k) end
t2keys[k] = true
end
-- Let's iterate keys from t1
for k1, v1 in pairs(t1) do
local v2 = t2[k1]
if type(k1) == "table" then
-- if key is a table, we need to find an equivalent one.
local ok = false
for i, tk in ipairs(t2tablekeys) do
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
table.remove(t2tablekeys, i)
t2keys[tk] = nil
ok = true
break
end
end
if not ok then return false end
else
-- t1 has a key which t2 doesn't have, fail.
if v2 == nil then return false end
t2keys[k1] = nil
if not recurse(v1, v2) then return false end
end
end
-- if t2 has a key which t1 doesn't have, fail.
if next(t2keys) then return false end
return true
end
return recurse(table1, table2)
end
local t1 = {}
t1[t1]=t1
t1.x = {[t1] = {1, 2, 3}}
local t2 = {}
local t3 = {}
t2[t3]=t2
t3[t2]=t3
t2.x = {[t3] = {1, 2, 3}}
t3.x = {[t2] = {1, 2, 3}}
print(table_eq(t1, t2))
--[[>
lua: deeptest.lua:15: stack overflow
stack traceback:
deeptest.lua:15: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
...
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:26: in function <deeptest.lua:3>
(...tail calls...)
deeptest.lua:62: in main chunk
[C]: in ?
--]]
产生堆栈溢出。如果不是因为堆栈溢出,它可能会产生误报(而不是我可以测试)。
我该如何处理这个案子? (它甚至可以处理吗?当我想到它时,它听起来像计算机科学中一个未解决的问题......但我对此并不了解)
当我说“结构平等”时,我的意思是下表:
local t = {}
t[{}] = 1
t["1"] = {}
在结构上与下表不同:
local t = {}
local t2 = {}
t[t2] = 1
t["1"] = t2
在“内容平等”中,他们是平等的。
测试用例:
local t1 = {}
t1[t1]=t1
t1.x = {[t1] = {1, 2, 3}}
local t2 = {}
local t3 = {}
t2[t3]=t2
t3[t2]=t3
t2.x = {[t3] = {1, 2, 3}}
t3.x = {[t2] = {1, 2, 3}}
assert(table_eq(t1, t2) == false)
assert(table_eq(t2, t3) == true)
local t4 = {}
t4[{}] = 1
t4["1"] = {}
local t5 = {}
local t6 = {}
t5[t6] = 1
t5["1"] = t6
assert(table_eq(t4, t5) == false)
答案 0 :(得分:1)
通过更改此代码,可以轻松修复堆栈溢出:
-- if key is a table, we need to find an equivalent one.
local ok = false
for i, tk in ipairs(t2tablekeys) do
if table_eq(k1, tk) and recurse(v1, t2[tk]) then
到:
-- if key is a table, we need to find an equivalent one.
local ok = false
for i, tk in ipairs(t2tablekeys) do
if recurse(k1, tk) and recurse(v1, t2[tk]) then
前两个测试用例返回true,但是第三个测试用例失败,因为将表作为键将内容与内容进行比较。如果您希望测试密钥是否与实际表相同,则需要将其更改为:
-- if key is a table, we need to find an equivalent one.
local ok = false
for i, tk in ipairs(t2tablekeys) do
if k1 == tk and recurse(v1, t2[tk]) then
但请记住,第二个测试用例会失败,因为他们希望将表作为关键内容与内容进行比较,而不是实际表。
你的测试用例互相矛盾,认为你认为“平等”,所以没有真正的答案。
P.S。
此函数忽略元数据,这也可以与__eq元方法进行比较。所以它仍然不是一个完整的比较。