Created
February 22, 2021 18:14
-
-
Save lua-playground/9f47671144f38d98f398d89e8f2a5ea8 to your computer and use it in GitHub Desktop.
Code shared from the Lua Playground(https://lua-playground.abby.md)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function equivalent(stack) | |
local a, b = stack.a, stack.b | |
if type(a) ~= 'table' then return a == b end | |
if a == b then return true end | |
local top = stack.next | |
while top ~= nil do | |
if stack.a == a and stack.b == b then return true end | |
top = stack.next | |
end | |
local counta, countb = 0, 0 | |
for k,va in pairs(a) do | |
if not equivalent({a = va, b = b[k], next = stack}) then return false end | |
counta = counta + 1 | |
end | |
for _,_ in pairs(b) do countb = countb + 1 end | |
return counta == countb | |
end | |
t = setmetatable({}, { | |
__newindex = function(tbl, key, value) | |
for k, _ in pairs(tbl) do | |
if equivalent({a = k, b = key}) then rawset(tbl, k, value); return end | |
end | |
rawset(tbl, key, value) | |
end, | |
__index = function(tbl, key) | |
for k, v in pairs(tbl) do | |
if equivalent({a = k, b = key}) then return v end | |
end | |
end | |
}) | |
s = {} | |
s[s] = s | |
r = {} | |
r[r] = r | |
q = {} | |
q[s] = q | |
print(equivalent({a = s, b = s}), equivalent({a = r, b = q}), '<br />') | |
t[{a = 1}] = 4 | |
t[{a = 1}] = 5 | |
t[{a = 2}] = 6 | |
t[q] = 7 | |
t[s] = 6 | |
print(t[{a = 1}], '<br />') | |
print(t[{a = 1, b = 2}], '<br />') | |
print(t[r], '<br />') | |
for k, v in pairs(t) do | |
print(k, v, '<br />') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment