-
-
Notifications
You must be signed in to change notification settings - Fork 433
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 methods weight, weights, and total_weight to weighted_index.rs #1420
Add methods weight, weights, and total_weight to weighted_index.rs #1420
Conversation
Ah, this is why you didn't expose
An alternative worth considering is to return an iterator over weights: the caller can choose to collect or use on-the-fly and can skip as required. My suggestion:
|
I've changed the name of Also created a custom Iterator type
I'm really not sure if it's worth it to expose that. My problem is solved without it, and it would be the only implementation added in this PR which returns a reference type, meaning |
You're right, it is strange. I think in earlier versions of Rust this derive simply wouldn't have worked. You can read more on this topic here: https://smallcultfollowing.com/babysteps//blog/2022/04/12/implied-bounds-and-perfect-derive/ The really odd part is that now I think the best solution is to write an explicit impl with the correct bounds: impl<'a, X> Debug for WeightedIndexIter<'a, X>
where
X: SampleUniform + PartialOrd + Debug,
X::Sampler: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WeightedIndexIter")
.field("weighted_index", &self.weighted_index)
.field("index", &self.index)
.finish()
}
} Another solution is to use a variant of #[impl_tools::autoimpl(Debug where X: Debug, X::Sampler: Debug)]
Fair point. Lets leave that off then. |
Incidentally, the bounds on the #[impl_tools::autoimpl(Clone)]
#[impl_tools::autoimpl(Debug where X: trait, X::Sampler: trait)] That's another issue though. |
Thanks for the explanation and the interesting link :) I went ahead and added manual implementations for Debug and Clone. A bit of boilerplate code, but I would say adding a new dependency should have its own PR later, if it comes to that 😃 Is there anything else to discuss? |
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.
Looks good. Thanks!
CHANGELOG.md
entrySummary
After facing some difficulties with the implementation in #1403, this PR is an alternative solution to the same problem. If this PR is merged, the other can be closed.
Motivation
Please read the motivation section in #1403. This PR aims to solve the problem mentioned there by exposing insights into the current state of the distribution, allowing the end user to calculate new weights based on the current values and then call the existing
update_weights
method with those.Details
Three new methods have been added to
weighted_index.rs
. Here are their signatures:weights
returns aWeightedIndexIter
which iterates over all the weights in the distribution:weight
returns the weight at a specific index:total_weight
returns the sum of all weights:In my opinion these methods are also not strongly coupled with the current implementation of
WeightedIndex
, as I cannot imagine a future implementation which would not be able to support these operations.I have also added tests for the new methods.
Looking forward to your reviews :)