make find iterative

This commit is contained in:
birds3345 2024-07-17 13:46:00 -04:00
parent 9f07cd5e71
commit aef6dd715b

View file

@ -30,13 +30,18 @@ Id UnionFind::find(Id id)
{ {
LUAU_ASSERT(size_t(id) < parents.size()); LUAU_ASSERT(size_t(id) < parents.size());
Id set = const_cast<const UnionFind*>(this)->find(id);
// An e-class id 𝑎 is canonical iff find(𝑎) = 𝑎. // 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 // Note: we don't update the ranks here since a rank
// represents the upper bound on the maximum depth of a tree // 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) void UnionFind::merge(Id a, Id b)