From 0ddc554ac0261e84f9dafb40067ba834cd4509fe Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Fri, 25 Dec 2020 17:54:36 +0100 Subject: [PATCH] Add table.is_circular --- table.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/table.lua b/table.lua index df30411..4ce7dff 100644 --- a/table.lua +++ b/table.lua @@ -39,6 +39,26 @@ function shuffle(table) return table end +function is_circular(table) + assert(type(table) == "table") + local known = {} + local function _is_circular(value) + if type(value) ~= "table" then + return false + end + if known[value] then + return true + end + known[value] = true + for key, value in pairs(value) do + if _is_circular(key) or _is_circular(value) then + return true + end + end + end + return _is_circular(table) +end + function equals_noncircular(table_1, table_2) local is_equal = table_1 == table_2 if is_equal or type(table_1) ~= "table" or type(table_2) ~= "table" then