Skip to content

Commit dad5f7c

Browse files
committed
Rename fns IteratorRandom::choose_multiple* -> sample*
1 parent dff13e7 commit dad5f7c

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

benches/benches/seq_choose.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,21 +77,21 @@ pub fn bench(c: &mut Criterion) {
7777
});
7878
}
7979

80-
c.bench_function("seq_iter_choose_multiple_10_of_100", |b| {
80+
c.bench_function("seq_iter_sample_10_of_100", |b| {
8181
let mut rng = Pcg32::from_rng(&mut rand::rng());
8282
let mut buf = [0i32; 100];
8383
rng.fill(&mut buf);
8484
let x = black_box(&buf);
85-
b.iter(|| x.iter().cloned().choose_multiple(&mut rng, 10))
85+
b.iter(|| x.iter().cloned().sample(&mut rng, 10))
8686
});
8787

88-
c.bench_function("seq_iter_choose_multiple_fill_10_of_100", |b| {
88+
c.bench_function("seq_iter_sample_fill_10_of_100", |b| {
8989
let mut rng = Pcg32::from_rng(&mut rand::rng());
9090
let mut buf = [0i32; 100];
9191
rng.fill(&mut buf);
9292
let x = black_box(&buf);
9393
let mut buf = [0; 10];
94-
b.iter(|| x.iter().cloned().choose_multiple_fill(&mut rng, &mut buf))
94+
b.iter(|| x.iter().cloned().sample_fill(&mut rng, &mut buf))
9595
});
9696

9797
bench_rng::<rand_chacha::ChaCha20Rng>(c, "ChaCha20");

src/seq/iterator.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ pub trait IteratorRandom: Iterator + Sized {
195195
/// case this equals the number of elements available.
196196
///
197197
/// Complexity is `O(n)` where `n` is the length of the iterator.
198-
/// For slices, prefer [`IndexedRandom::choose_multiple`].
199-
fn choose_multiple_fill<R>(mut self, rng: &mut R, buf: &mut [Self::Item]) -> usize
198+
/// For slices, prefer [`IndexedRandom::sample`].
199+
fn sample_fill<R>(mut self, rng: &mut R, buf: &mut [Self::Item]) -> usize
200200
where
201201
R: Rng + ?Sized,
202202
{
@@ -224,7 +224,7 @@ pub trait IteratorRandom: Iterator + Sized {
224224

225225
/// Uniformly sample `amount` distinct elements into a [`Vec`]
226226
///
227-
/// This is equivalent to `choose_multiple_fill` except for the result type.
227+
/// This is equivalent to `sample_fill` except for the result type.
228228
///
229229
/// Although the elements are selected randomly, the order of elements in
230230
/// the buffer is neither stable nor fully random. If random ordering is
@@ -235,9 +235,9 @@ pub trait IteratorRandom: Iterator + Sized {
235235
/// elements available.
236236
///
237237
/// Complexity is `O(n)` where `n` is the length of the iterator.
238-
/// For slices, prefer [`IndexedRandom::choose_multiple`].
238+
/// For slices, prefer [`IndexedRandom::sample`].
239239
#[cfg(feature = "alloc")]
240-
fn choose_multiple<R>(mut self, rng: &mut R, amount: usize) -> Vec<Self::Item>
240+
fn sample<R>(mut self, rng: &mut R, amount: usize) -> Vec<Self::Item>
241241
where
242242
R: Rng + ?Sized,
243243
{
@@ -538,8 +538,8 @@ mod test {
538538

539539
let mut r = crate::test::rng(401);
540540
let vals = (min_val..max_val).collect::<Vec<i32>>();
541-
let small_sample = vals.iter().choose_multiple(&mut r, 5);
542-
let large_sample = vals.iter().choose_multiple(&mut r, vals.len() + 5);
541+
let small_sample = vals.iter().sample(&mut r, 5);
542+
let large_sample = vals.iter().sample(&mut r, vals.len() + 5);
543543

544544
assert_eq!(small_sample.len(), 5);
545545
assert_eq!(large_sample.len(), vals.len());
@@ -644,20 +644,17 @@ mod test {
644644
}
645645

646646
#[test]
647-
fn value_stability_choose_multiple() {
647+
fn value_stability_sample() {
648648
fn do_test<I: Clone + Iterator<Item = u32>>(iter: I, v: &[u32]) {
649649
let mut rng = crate::test::rng(412);
650650
let mut buf = [0u32; 8];
651-
assert_eq!(
652-
iter.clone().choose_multiple_fill(&mut rng, &mut buf),
653-
v.len()
654-
);
651+
assert_eq!(iter.clone().sample_fill(&mut rng, &mut buf), v.len());
655652
assert_eq!(&buf[0..v.len()], v);
656653

657654
#[cfg(feature = "alloc")]
658655
{
659656
let mut rng = crate::test::rng(412);
660-
assert_eq!(iter.choose_multiple(&mut rng, v.len()), v);
657+
assert_eq!(iter.sample(&mut rng, v.len()), v);
661658
}
662659
}
663660

0 commit comments

Comments
 (0)