remove const_cast

This commit is contained in:
birds3345 2024-07-17 17:09:02 -04:00
parent 9b401ae1d6
commit 1cb6973100
2 changed files with 16 additions and 8 deletions

View file

@ -19,6 +19,9 @@ struct UnionFind final
private: private:
std::vector<Id> parents; std::vector<Id> parents;
std::vector<unsigned int> ranks; std::vector<unsigned int> ranks;
private:
Id canonicalize(Id id) const;
}; };
} // namespace Luau::EqSat } // namespace Luau::EqSat

View file

@ -17,20 +17,14 @@ Id UnionFind::makeSet()
Id UnionFind::find(Id id) const Id UnionFind::find(Id id) const
{ {
LUAU_ASSERT(size_t(id) < parents.size()); return canonicalize(id);
// An e-class id 𝑎 is canonical iff find(𝑎) = 𝑎.
while (id != parents[size_t(id)])
id = parents[size_t(id)];
return id;
} }
Id UnionFind::find(Id id) 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); Id set = canonicalize(id);
// An e-class id 𝑎 is canonical iff find(𝑎) = 𝑎. // An e-class id 𝑎 is canonical iff find(𝑎) = 𝑎.
while (id != parents[size_t(id)]) while (id != parents[size_t(id)])
@ -62,4 +56,15 @@ void UnionFind::merge(Id a, Id b)
ranks[size_t(aSet)]++; ranks[size_t(aSet)]++;
} }
Id UnionFind::canonicalize(Id id) const
{
LUAU_ASSERT(size_t(id) < parents.size());
// An e-class id 𝑎 is canonical iff find(𝑎) = 𝑎.
while (id != parents[size_t(id)])
id = parents[size_t(id)];
return id;
}
} // namespace Luau::EqSat } // namespace Luau::EqSat