-
Notifications
You must be signed in to change notification settings - Fork 153
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
Shuffling an IndexSet #171
Comments
A shuffle method is too application specific, a more general method is wanted for the map and set. The closest we have today is sorting, which can inform the design. However, we might not even want to commit to exposing all entries in a slice (which is what sort uses internally). |
You could implement a shuffle with a series of If we did this internally, it could be a bit faster because we would be able to shuffle the entries vector and then bulk-rebuild the indices table. That is basically what the sort methods do. However, we would also need randomness to do a shuffle -- perhaps that could be a callback: fn shuffle(&mut self, random_index: impl FnMut(Range<usize>)); Alternative hack: you could call |
I really like that use indexmap::IndexSet;
use rand::{
distributions::{Distribution, Standard},
Rng,
};
use std::cmp::Ordering;
struct RandomOrdering(Ordering);
impl Into<Ordering> for RandomOrdering {
fn into(self) -> Ordering {
self.0
}
}
impl Distribution<RandomOrdering> for Standard {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> RandomOrdering {
RandomOrdering(match rng.gen_range(0..2) {
0 => Ordering::Less,
1 => Ordering::Equal,
_ => Ordering::Greater,
})
}
}
fn main() {
let mut letters: IndexSet<char> = ('a'..='z').collect();
println!("{:?}", letters);
letters.sort_by(|_, _| rand::random::<RandomOrdering>().into());
println!("{:?}", letters);
} |
No guarantees for the uniformity of the shuffle with such an ordering function in sort. There should be literature on it; IUUC it is biased. Sorting by a cached random key should be the way to go if needed. |
Hmm, we could wrap |
Since #244, you can write something like |
I have a need to periodically shuffle my
IndexSet
s. Currently I built this by iterating, collecting to a vec, shuffling, iterating, collecting back into anIndexSet
.Is there a better way to do this? Could the
IndexSet/Map
expose ashuffle
method?The text was updated successfully, but these errors were encountered: