I’m having trouble thinking of a way to do this without turning it into spaghetti code. Is there a simple way to accomplish this?
also an example of a cyclic table is:
local a = {}
local b = {}
a[1] = b
b[1] = a
I’m having trouble thinking of a way to do this without turning it into spaghetti code. Is there a simple way to accomplish this?
also an example of a cyclic table is:
local a = {}
local b = {}
a[1] = b
b[1] = a
There are no built-in ways to detect cyclic tables, but I threw together a quick recursive function that seems to work. It’s as easy as I could get it. Recursively checks all tables and those marked as tracked have already been visited. You’ll have to take some consumed memory into account. Please let me know if you find any mistakes or the output isn’t correct.
To improve efficiency, going by layers might improve execution time. Depends on extents of the table and the number of nested tables. I used Depth-first search - Wikipedia.
If your tables are small, the difference is almost negligible. Otherwise, different algorithms suit different tables. The polar opposite of Depth-first search is Breadth-first search - Wikipedia.
local function ContainsCyclicReference(tbl)
local tracked = {} -- all visited tables
local function DeepSearch(t)
tracked[t] = true
for _,v in t do
if typeof(v) ~= "table" then continue; end
return tracked[v] and true or DeepSearch(v)
end
return false
end
return DeepSearch(tbl)
end
local a = {}
local b = {}
local c = {}
a[1] = b
b[1] = {[1] = {[2] = c}}
c[1] = a
print(ContainsCyclicReference(a)) --> true
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.