Skip to content

Commit 846d1c7

Browse files
committed
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.
1 parent c5d8556 commit 846d1c7

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/librand/lib.rs

+29-25
Original file line numberDiff line numberDiff line change
@@ -293,21 +293,7 @@ pub trait Rng {
293293
}
294294
}
295295

296-
/// Shuffle a vec
297-
///
298-
/// # Example
299-
///
300-
/// ```rust
301-
/// use rand::{task_rng, Rng};
302-
///
303-
/// println!("{}", task_rng().shuffle(vec!(1,2,3)));
304-
/// ```
305-
fn shuffle<T>(&mut self, mut values: Vec<T>) -> Vec<T> {
306-
self.shuffle_mut(values.as_mut_slice());
307-
values
308-
}
309-
310-
/// Shuffle a mutable vector in place.
296+
/// Shuffle a mutable slice in place.
311297
///
312298
/// # Example
313299
///
@@ -316,12 +302,12 @@ pub trait Rng {
316302
///
317303
/// let mut rng = task_rng();
318304
/// let mut y = [1,2,3];
319-
/// rng.shuffle_mut(y);
320-
/// println!("{:?}", y);
321-
/// rng.shuffle_mut(y);
322-
/// println!("{:?}", y);
305+
/// rng.shuffle(y);
306+
/// println!("{}", y.as_slice());
307+
/// rng.shuffle(y);
308+
/// println!("{}", y.as_slice());
323309
/// ```
324-
fn shuffle_mut<T>(&mut self, values: &mut [T]) {
310+
fn shuffle<T>(&mut self, values: &mut [T]) {
325311
let mut i = values.len();
326312
while i >= 2u {
327313
// invariant: elements with index >= i have been locked in place.
@@ -331,6 +317,12 @@ pub trait Rng {
331317
}
332318
}
333319

320+
/// Shuffle a mutable slice in place.
321+
#[deprecated="renamed to `.shuffle`"]
322+
fn shuffle_mut<T>(&mut self, values: &mut [T]) {
323+
self.shuffle(values)
324+
}
325+
334326
/// Randomly sample up to `n` elements from an iterator.
335327
///
336328
/// # Example
@@ -811,16 +803,28 @@ mod test {
811803
#[test]
812804
fn test_shuffle() {
813805
let mut r = task_rng();
814-
let empty = Vec::<int>::new();
815-
assert_eq!(r.shuffle(vec!()), empty);
816-
assert_eq!(r.shuffle(vec!(1, 1, 1)), vec!(1, 1, 1));
806+
let mut empty: &mut [int] = &mut [];
807+
r.shuffle(empty);
808+
let mut one = [1];
809+
r.shuffle(one);
810+
assert_eq!(one.as_slice(), &[1]);
811+
812+
let mut two = [1, 2];
813+
r.shuffle(two);
814+
assert!(two == [1, 2] || two == [2, 1]);
815+
816+
let mut x = [1, 1, 1];
817+
r.shuffle(x);
818+
assert_eq!(x.as_slice(), &[1, 1, 1]);
817819
}
818820

819821
#[test]
820822
fn test_task_rng() {
821823
let mut r = task_rng();
822824
r.gen::<int>();
823-
assert_eq!(r.shuffle(vec!(1, 1, 1)), vec!(1, 1, 1));
825+
let mut v = [1, 1, 1];
826+
r.shuffle(v);
827+
assert_eq!(v.as_slice(), &[1, 1, 1]);
824828
assert_eq!(r.gen_range(0u, 1u), 0u);
825829
}
826830

@@ -934,7 +938,7 @@ mod bench {
934938
let mut rng = XorShiftRng::new().unwrap();
935939
let x : &mut[uint] = [1,..100];
936940
bh.iter(|| {
937-
rng.shuffle_mut(x);
941+
rng.shuffle(x);
938942
})
939943
}
940944
}

0 commit comments

Comments
 (0)