-
Notifications
You must be signed in to change notification settings - Fork 793
Add a disjoint sets (union-find) utility #6797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This will be used in an upcoming type optimization pass and may be generally useful.
src/support/disjoint_sets.h
Outdated
|
||
namespace wasm { | ||
|
||
// A disjoint set forest (a.k.a. union-find) implementation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// A disjoint set forest (a.k.a. union-find) implementation. | |
// A disjoint set forest (a.k.a. union-find) implementation. See | |
// https://en.wikipedia.org/wiki/Disjoint-set_data_structure |
info[root2].parent = root1; | ||
// If the ranks were equal, the new root has a larger rank. | ||
if (info[root1].rank == info[root2].rank) { | ||
++info[root1].rank; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++info[root1].rank; | |
info[root1].rank++; |
Unless there is some reason for it? To me the default ++
reads more clearly in general.
constexpr size_t count = 16; | ||
DisjointSets sets; | ||
size_t elems[count]; | ||
for (size_t i = 0; i < count; ++i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, can we standardize on i++
for such increments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to standardize on ++i
. For integers it obviously doesn't matter, but for nontrivial iterators it can make a big difference because postincrement requires making a copy of the iterator while preincrement does not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general that is true, but in a for loop like this, the output of ++i / i++
is not being read? The next time i
is read is in the condition check, which reads the updated i
anyhow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyhow, I don't mean to block this PR on it, but I think it's separately worth making a choice here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sg 👍
This will be used in an upcoming type optimization pass and may be
generally useful.