Skip to content

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

Merged
merged 3 commits into from
Aug 1, 2024
Merged

Add a disjoint sets (union-find) utility #6797

merged 3 commits into from
Aug 1, 2024

Conversation

tlively
Copy link
Member

@tlively tlively commented Aug 1, 2024

This will be used in an upcoming type optimization pass and may be
generally useful.

This will be used in an upcoming type optimization pass and may be
generally useful.
@tlively tlively requested a review from kripken August 1, 2024 01:23

namespace wasm {

// A disjoint set forest (a.k.a. union-find) implementation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
++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) {
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sg 👍

@tlively tlively merged commit 2a7c093 into main Aug 1, 2024
13 checks passed
@tlively tlively deleted the disjoint-sets branch August 1, 2024 19:08
@gkdn gkdn mentioned this pull request Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants