Skip to content
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

Add WeightedIndexTree to rand_distr #1372

Merged
merged 20 commits into from
Feb 8, 2024
Merged

Conversation

xmakro
Copy link
Contributor

@xmakro xmakro commented Jan 10, 2024

As discussed in #1053

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

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

Incomplete (haven't fully reviewed code).

rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
Comment on lines 231 to 232
impl<W: Clone + PartialEq + PartialOrd + SampleUniform + SubAssign<W> + Weight>
Distribution<Result<usize, WeightedError>> for WeightedTreeIndex<W>
Copy link
Member

Choose a reason for hiding this comment

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

We don't usually implement Distribution<Result<..>>. Do we need to?

I think I would prefer to panic on error, but guarantee no panic if self.is_valid() (self.can_sample()).

@vks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the Result<>, so sample now panics instead. I added the info about is_valid to the doc string and introduced safe_sample as an alternative that does not panic.

@xmakro
Copy link
Contributor Author

xmakro commented Feb 7, 2024

Incomplete (haven't fully reviewed code).

Thank you for the review. I addressed all the comments

rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
@xmakro
Copy link
Contributor Author

xmakro commented Feb 7, 2024

Thanks, that is much better, changed to try_sample

rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
rand_distr/src/weighted_tree.rs Outdated Show resolved Hide resolved
// Otherwise we found the index with the target weight.
break;
}
assert!(target_weight >= W::ZERO);
Copy link
Member

Choose a reason for hiding this comment

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

If I understand correctly, this must be strictly > 0?

Copy link
Contributor Author

@xmakro xmakro Feb 8, 2024

Choose a reason for hiding this comment

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

As an intermediate state, this can be zero if all weights are zero or there are no elements. is_valid would then return false. Allowing this is useful, so that the user does not have to apply the updates in the right order to avoid intermediate zero states.

Copy link
Member

Choose a reason for hiding this comment

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

In this case the function will already have returned an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I mixed up what this comment was referring to.

I think target_weight can be 0 in this line, for example: if we have a tree with only one node and gen_range samples 0, then the loop block is a no-op and after we hit this line with target_weight = 0.

However, I realized the line right after was incorrect, it should be: assert!(target_weight < self.get(index)). Should be correct now.

Copy link
Member

Choose a reason for hiding this comment

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

I agree

xmakro added 2 commits February 8, 2024 13:00
@dhardy
Copy link
Member

dhardy commented Feb 8, 2024

We can ignore the zerocopy failures (bug there which is fixed in a pre-release version).

Thanks for the implementation, @xmakro.

@dhardy dhardy merged commit f827c00 into rust-random:master Feb 8, 2024
10 of 12 checks passed
@dhardy dhardy changed the title Add WeightedIndexTree to dist_randr Add WeightedIndexTree to rand_distr Feb 13, 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