From aef6dd715bf45731b5bef39888a73ca5c75f295e Mon Sep 17 00:00:00 2001 From: birds3345 <31601136+birds3345@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:46:00 -0400 Subject: [PATCH] make find iterative --- EqSat/src/UnionFind.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/EqSat/src/UnionFind.cpp b/EqSat/src/UnionFind.cpp index 536de271..ec638628 100644 --- a/EqSat/src/UnionFind.cpp +++ b/EqSat/src/UnionFind.cpp @@ -30,13 +30,18 @@ Id UnionFind::find(Id id) { LUAU_ASSERT(size_t(id) < parents.size()); + Id set = const_cast(this)->find(id); + // An e-class id 𝑎 is canonical iff find(𝑎) = 𝑎. - if (id != parents[size_t(id)]) + while (id != parents[size_t(id)]) + { // Note: we don't update the ranks here since a rank // represents the upper bound on the maximum depth of a tree - parents[size_t(id)] = find(parents[size_t(id)]); + parents[size_t(id)] = set; + id = parents[size_t(id)]; + } - return parents[size_t(id)]; + return set; } void UnionFind::merge(Id a, Id b)