rand: replace Rng.shuffle's functionality with .shuffle_mut.
Deprecates the `shuffle_mut` name in favour of `shuffle` too. In future there will be many different types of owned vectors/vector-likes (e.g. DST's ~[], Vec, SmallVec, Rope, ...), and so privileging just `Vec` with the "functional" `shuffle` method is silly.
This commit is contained in:
parent
c5d85560a9
commit
846d1c7d0d
@ -293,21 +293,7 @@ pub trait Rng {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shuffle a vec
|
/// Shuffle a mutable slice in place.
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```rust
|
|
||||||
/// use rand::{task_rng, Rng};
|
|
||||||
///
|
|
||||||
/// println!("{}", task_rng().shuffle(vec!(1,2,3)));
|
|
||||||
/// ```
|
|
||||||
fn shuffle<T>(&mut self, mut values: Vec<T>) -> Vec<T> {
|
|
||||||
self.shuffle_mut(values.as_mut_slice());
|
|
||||||
values
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Shuffle a mutable vector in place.
|
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
@ -316,12 +302,12 @@ pub trait Rng {
|
|||||||
///
|
///
|
||||||
/// let mut rng = task_rng();
|
/// let mut rng = task_rng();
|
||||||
/// let mut y = [1,2,3];
|
/// let mut y = [1,2,3];
|
||||||
/// rng.shuffle_mut(y);
|
/// rng.shuffle(y);
|
||||||
/// println!("{:?}", y);
|
/// println!("{}", y.as_slice());
|
||||||
/// rng.shuffle_mut(y);
|
/// rng.shuffle(y);
|
||||||
/// println!("{:?}", y);
|
/// println!("{}", y.as_slice());
|
||||||
/// ```
|
/// ```
|
||||||
fn shuffle_mut<T>(&mut self, values: &mut [T]) {
|
fn shuffle<T>(&mut self, values: &mut [T]) {
|
||||||
let mut i = values.len();
|
let mut i = values.len();
|
||||||
while i >= 2u {
|
while i >= 2u {
|
||||||
// invariant: elements with index >= i have been locked in place.
|
// invariant: elements with index >= i have been locked in place.
|
||||||
@ -331,6 +317,12 @@ pub trait Rng {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shuffle a mutable slice in place.
|
||||||
|
#[deprecated="renamed to `.shuffle`"]
|
||||||
|
fn shuffle_mut<T>(&mut self, values: &mut [T]) {
|
||||||
|
self.shuffle(values)
|
||||||
|
}
|
||||||
|
|
||||||
/// Randomly sample up to `n` elements from an iterator.
|
/// Randomly sample up to `n` elements from an iterator.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
@ -811,16 +803,28 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_shuffle() {
|
fn test_shuffle() {
|
||||||
let mut r = task_rng();
|
let mut r = task_rng();
|
||||||
let empty = Vec::<int>::new();
|
let mut empty: &mut [int] = &mut [];
|
||||||
assert_eq!(r.shuffle(vec!()), empty);
|
r.shuffle(empty);
|
||||||
assert_eq!(r.shuffle(vec!(1, 1, 1)), vec!(1, 1, 1));
|
let mut one = [1];
|
||||||
|
r.shuffle(one);
|
||||||
|
assert_eq!(one.as_slice(), &[1]);
|
||||||
|
|
||||||
|
let mut two = [1, 2];
|
||||||
|
r.shuffle(two);
|
||||||
|
assert!(two == [1, 2] || two == [2, 1]);
|
||||||
|
|
||||||
|
let mut x = [1, 1, 1];
|
||||||
|
r.shuffle(x);
|
||||||
|
assert_eq!(x.as_slice(), &[1, 1, 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_task_rng() {
|
fn test_task_rng() {
|
||||||
let mut r = task_rng();
|
let mut r = task_rng();
|
||||||
r.gen::<int>();
|
r.gen::<int>();
|
||||||
assert_eq!(r.shuffle(vec!(1, 1, 1)), vec!(1, 1, 1));
|
let mut v = [1, 1, 1];
|
||||||
|
r.shuffle(v);
|
||||||
|
assert_eq!(v.as_slice(), &[1, 1, 1]);
|
||||||
assert_eq!(r.gen_range(0u, 1u), 0u);
|
assert_eq!(r.gen_range(0u, 1u), 0u);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -934,7 +938,7 @@ mod bench {
|
|||||||
let mut rng = XorShiftRng::new().unwrap();
|
let mut rng = XorShiftRng::new().unwrap();
|
||||||
let x : &mut[uint] = [1,..100];
|
let x : &mut[uint] = [1,..100];
|
||||||
bh.iter(|| {
|
bh.iter(|| {
|
||||||
rng.shuffle_mut(x);
|
rng.shuffle(x);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user