-
Notifications
You must be signed in to change notification settings - Fork 694
Shuffle tracks in place #1445
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
Shuffle tracks in place #1445
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c9d7a6d
connect: add shuffle_vec.rs
photovoltex 50e1bd5
connect: shuffle in place
photovoltex 9aa806d
add shuffle with seed option
photovoltex f933e1d
reduce complexity to add new metadata fields
photovoltex 90dd971
add context index to metadata
photovoltex 581e4fb
use seed for shuffle
photovoltex f57fdf9
add log for shuffle seed
photovoltex f596b1c
connect: use small_rng, derive Default
photovoltex 4ddab70
Apply suggestions from code review
photovoltex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use rand::{rngs::SmallRng, Rng, SeedableRng}; | ||
use std::{ | ||
ops::{Deref, DerefMut}, | ||
vec::IntoIter, | ||
}; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct ShuffleVec<T> { | ||
vec: Vec<T>, | ||
indices: Option<Vec<usize>>, | ||
} | ||
|
||
impl<T: PartialEq> PartialEq for ShuffleVec<T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.vec == other.vec | ||
} | ||
} | ||
|
||
impl<T> Deref for ShuffleVec<T> { | ||
type Target = Vec<T>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.vec | ||
} | ||
} | ||
|
||
impl<T> DerefMut for ShuffleVec<T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
self.vec.as_mut() | ||
} | ||
} | ||
|
||
impl<T> IntoIterator for ShuffleVec<T> { | ||
type Item = T; | ||
type IntoIter = IntoIter<T>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.vec.into_iter() | ||
} | ||
} | ||
|
||
impl<T> From<Vec<T>> for ShuffleVec<T> { | ||
fn from(vec: Vec<T>) -> Self { | ||
Self { vec, indices: None } | ||
} | ||
} | ||
|
||
impl<T> ShuffleVec<T> { | ||
pub fn new() -> Self { | ||
Self { | ||
vec: Vec::new(), | ||
indices: None, | ||
} | ||
} | ||
|
||
pub fn shuffle_with_seed(&mut self, seed: u64) { | ||
self.shuffle_with_rng(SmallRng::seed_from_u64(seed)) | ||
} | ||
|
||
pub fn shuffle_with_rng(&mut self, mut rng: impl Rng) { | ||
if self.indices.is_some() { | ||
self.unshuffle() | ||
} | ||
|
||
let indices = { | ||
(1..self.vec.len()) | ||
.rev() | ||
.map(|i| rng.gen_range(0..i + 1)) | ||
.collect() | ||
}; | ||
|
||
for (i, &rnd_ind) in (1..self.vec.len()).rev().zip(&indices) { | ||
self.vec.swap(i, rnd_ind); | ||
} | ||
|
||
self.indices = Some(indices) | ||
} | ||
|
||
pub fn unshuffle(&mut self) { | ||
let indices = match self.indices.take() { | ||
Some(indices) => indices, | ||
None => return, | ||
}; | ||
|
||
for i in 1..self.vec.len() { | ||
let n = indices[self.vec.len() - i - 1]; | ||
self.vec.swap(n, i); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use rand::Rng; | ||
|
||
#[test] | ||
fn test_shuffle_with_seed() { | ||
let seed = rand::thread_rng().gen_range(0..10000000000000); | ||
|
||
let vec = (0..100).collect::<Vec<_>>(); | ||
let base_vec: ShuffleVec<i32> = vec.into(); | ||
|
||
let mut shuffled_vec = base_vec.clone(); | ||
shuffled_vec.shuffle_with_seed(seed); | ||
|
||
let mut different_shuffled_vec = base_vec.clone(); | ||
different_shuffled_vec.shuffle_with_seed(seed); | ||
|
||
assert_eq!(shuffled_vec, different_shuffled_vec); | ||
|
||
let mut unshuffled_vec = shuffled_vec.clone(); | ||
unshuffled_vec.unshuffle(); | ||
|
||
assert_eq!(base_vec, unshuffled_vec); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.