Skip to content

Commit dff13e7

Browse files
committed
Rename fns IndexedRandom::choose_multiple* -> sample*
1 parent db993ec commit dff13e7

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

benches/benches/seq_choose.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn bench(c: &mut Criterion) {
3030

3131
let lens = [(1, 1000), (950, 1000), (10, 100), (90, 100)];
3232
for (amount, len) in lens {
33-
let name = format!("seq_slice_choose_multiple_{}_of_{}", amount, len);
33+
let name = format!("seq_slice_sample_{}_of_{}", amount, len);
3434
c.bench_function(name.as_str(), |b| {
3535
let mut rng = Pcg32::from_rng(&mut rand::rng());
3636
let mut buf = [0i32; 1000];
@@ -44,7 +44,7 @@ pub fn bench(c: &mut Criterion) {
4444
b.iter(|| {
4545
// Collect full result to prevent unwanted shortcuts getting
4646
// first element (in case sample_indices returns an iterator).
47-
for (slot, sample) in y.iter_mut().zip(x.choose_multiple(&mut rng, amount)) {
47+
for (slot, sample) in y.iter_mut().zip(x.sample(&mut rng, amount)) {
4848
*slot = *sample;
4949
}
5050
y[amount - 1]
@@ -54,7 +54,7 @@ pub fn bench(c: &mut Criterion) {
5454

5555
let lens = [(1, 1000), (950, 1000), (10, 100), (90, 100)];
5656
for (amount, len) in lens {
57-
let name = format!("seq_slice_choose_multiple_weighted_{}_of_{}", amount, len);
57+
let name = format!("seq_slice_sample_weighted_{}_of_{}", amount, len);
5858
c.bench_function(name.as_str(), |b| {
5959
let mut rng = Pcg32::from_rng(&mut rand::rng());
6060
let mut buf = [0i32; 1000];
@@ -68,7 +68,7 @@ pub fn bench(c: &mut Criterion) {
6868
b.iter(|| {
6969
// Collect full result to prevent unwanted shortcuts getting
7070
// first element (in case sample_indices returns an iterator).
71-
let samples_iter = x.choose_multiple_weighted(&mut rng, amount, |_| 1.0).unwrap();
71+
let samples_iter = x.sample_weighted(&mut rng, amount, |_| 1.0).unwrap();
7272
for (slot, sample) in y.iter_mut().zip(samples_iter) {
7373
*slot = *sample;
7474
}

src/seq/slice.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ pub trait IndexedRandom: Index<usize> {
7878
/// let sample = "Hello, audience!".as_bytes();
7979
///
8080
/// // collect the results into a vector:
81-
/// let v: Vec<u8> = sample.choose_multiple(&mut rng, 3).cloned().collect();
81+
/// let v: Vec<u8> = sample.sample(&mut rng, 3).cloned().collect();
8282
///
8383
/// // store in a buffer:
8484
/// let mut buf = [0u8; 5];
85-
/// for (b, slot) in sample.choose_multiple(&mut rng, buf.len()).zip(buf.iter_mut()) {
85+
/// for (b, slot) in sample.sample(&mut rng, buf.len()).zip(buf.iter_mut()) {
8686
/// *slot = *b;
8787
/// }
8888
/// ```
8989
#[cfg(feature = "alloc")]
90-
fn choose_multiple<R>(&self, rng: &mut R, amount: usize) -> SliceChooseIter<Self, Self::Output>
90+
fn sample<R>(&self, rng: &mut R, amount: usize) -> SliceChooseIter<Self, Self::Output>
9191
where
9292
Self::Output: Sized,
9393
R: Rng + ?Sized,
@@ -114,9 +114,9 @@ pub trait IndexedRandom: Index<usize> {
114114
/// let mut rng = &mut rand::rng();
115115
/// let sample = "Hello, audience!".as_bytes();
116116
///
117-
/// let a: [u8; 3] = sample.choose_multiple_array(&mut rng).unwrap();
117+
/// let a: [u8; 3] = sample.sample_array(&mut rng).unwrap();
118118
/// ```
119-
fn choose_multiple_array<R, const N: usize>(&self, rng: &mut R) -> Option<[Self::Output; N]>
119+
fn sample_array<R, const N: usize>(&self, rng: &mut R) -> Option<[Self::Output; N]>
120120
where
121121
Self::Output: Clone + Sized,
122122
R: Rng + ?Sized,
@@ -173,7 +173,7 @@ pub trait IndexedRandom: Index<usize> {
173173

174174
/// Biased sampling of `amount` distinct elements
175175
///
176-
/// Similar to [`choose_multiple`], but where the likelihood of each
176+
/// Similar to [`sample`], but where the likelihood of each
177177
/// element's inclusion in the output may be specified. Zero-weighted
178178
/// elements are never returned; the result may therefore contain fewer
179179
/// elements than `amount` even when `self.len() >= amount`. The elements
@@ -198,13 +198,13 @@ pub trait IndexedRandom: Index<usize> {
198198
/// // (50% * 50%) + (25% * 67%) = 41.7% chance that the output is `['a', 'b']` in some order.
199199
/// // (50% * 50%) + (25% * 67%) = 41.7% chance that the output is `['a', 'c']` in some order.
200200
/// // (25% * 33%) + (25% * 33%) = 16.6% chance that the output is `['b', 'c']` in some order.
201-
/// println!("{:?}", choices.choose_multiple_weighted(&mut rng, 2, |item| item.1).unwrap().collect::<Vec<_>>());
201+
/// println!("{:?}", choices.sample_weighted(&mut rng, 2, |item| item.1).unwrap().collect::<Vec<_>>());
202202
/// ```
203-
/// [`choose_multiple`]: IndexedRandom::choose_multiple
203+
/// [`sample`]: IndexedRandom::sample
204204
// Note: this is feature-gated on std due to usage of f64::powf.
205205
// If necessary, we may use alloc+libm as an alternative (see PR #1089).
206206
#[cfg(feature = "std")]
207-
fn choose_multiple_weighted<R, F, X>(
207+
fn sample_weighted<R, F, X>(
208208
&self,
209209
rng: &mut R,
210210
amount: usize,
@@ -412,7 +412,7 @@ impl<T> SliceRandom for [T] {
412412
/// An iterator over multiple slice elements.
413413
///
414414
/// This struct is created by
415-
/// [`IndexedRandom::choose_multiple`](trait.IndexedRandom.html#tymethod.choose_multiple).
415+
/// [`IndexedRandom::sample`](trait.IndexedRandom.html#tymethod.sample).
416416
#[cfg(feature = "alloc")]
417417
#[derive(Debug)]
418418
pub struct SliceChooseIter<'a, S: ?Sized + 'a, T: 'a> {
@@ -492,16 +492,13 @@ mod test {
492492
assert_eq!(nums.choose_mut(&mut r), Some(&mut 3));
493493

494494
assert_eq!(
495-
&chars.choose_multiple_array(&mut r),
495+
&chars.sample_array(&mut r),
496496
&Some(['f', 'i', 'd', 'b', 'c', 'm', 'j', 'k'])
497497
);
498498

499499
#[cfg(feature = "alloc")]
500500
assert_eq!(
501-
&chars
502-
.choose_multiple(&mut r, 8)
503-
.cloned()
504-
.collect::<Vec<char>>(),
501+
&chars.sample(&mut r, 8).cloned().collect::<Vec<char>>(),
505502
&['h', 'm', 'd', 'b', 'c', 'e', 'n', 'f']
506503
);
507504

@@ -668,7 +665,7 @@ mod test {
668665
let choices = [('a', 2), ('b', 1), ('c', 0)];
669666
for _ in 0..100 {
670667
let result = choices
671-
.choose_multiple_weighted(&mut rng, 2, |item| item.1)
668+
.sample_weighted(&mut rng, 2, |item| item.1)
672669
.unwrap()
673670
.collect::<Vec<_>>();
674671

@@ -678,29 +675,29 @@ mod test {
678675

679676
// Case 2: All of the weights are 0
680677
let choices = [('a', 0), ('b', 0), ('c', 0)];
681-
let r = choices.choose_multiple_weighted(&mut rng, 2, |item| item.1);
678+
let r = choices.sample_weighted(&mut rng, 2, |item| item.1);
682679
assert_eq!(r.unwrap().len(), 0);
683680

684681
// Case 3: Negative weights
685682
let choices = [('a', -1), ('b', 1), ('c', 1)];
686-
let r = choices.choose_multiple_weighted(&mut rng, 2, |item| item.1);
683+
let r = choices.sample_weighted(&mut rng, 2, |item| item.1);
687684
assert_eq!(r.unwrap_err(), WeightError::InvalidWeight);
688685

689686
// Case 4: Empty list
690687
let choices = [];
691-
let r = choices.choose_multiple_weighted(&mut rng, 0, |_: &()| 0);
688+
let r = choices.sample_weighted(&mut rng, 0, |_: &()| 0);
692689
assert_eq!(r.unwrap().count(), 0);
693690

694691
// Case 5: NaN weights
695692
let choices = [('a', f64::NAN), ('b', 1.0), ('c', 1.0)];
696-
let r = choices.choose_multiple_weighted(&mut rng, 2, |item| item.1);
693+
let r = choices.sample_weighted(&mut rng, 2, |item| item.1);
697694
assert_eq!(r.unwrap_err(), WeightError::InvalidWeight);
698695

699696
// Case 6: +infinity weights
700697
let choices = [('a', f64::INFINITY), ('b', 1.0), ('c', 1.0)];
701698
for _ in 0..100 {
702699
let result = choices
703-
.choose_multiple_weighted(&mut rng, 2, |item| item.1)
700+
.sample_weighted(&mut rng, 2, |item| item.1)
704701
.unwrap()
705702
.collect::<Vec<_>>();
706703
assert_eq!(result.len(), 2);
@@ -709,12 +706,12 @@ mod test {
709706

710707
// Case 7: -infinity weights
711708
let choices = [('a', f64::NEG_INFINITY), ('b', 1.0), ('c', 1.0)];
712-
let r = choices.choose_multiple_weighted(&mut rng, 2, |item| item.1);
709+
let r = choices.sample_weighted(&mut rng, 2, |item| item.1);
713710
assert_eq!(r.unwrap_err(), WeightError::InvalidWeight);
714711

715712
// Case 8: -0 weights
716713
let choices = [('a', -0.0), ('b', 1.0), ('c', 1.0)];
717-
let r = choices.choose_multiple_weighted(&mut rng, 2, |item| item.1);
714+
let r = choices.sample_weighted(&mut rng, 2, |item| item.1);
718715
assert!(r.is_ok());
719716
}
720717

@@ -737,7 +734,7 @@ mod test {
737734
let expected_results = [5833, 2667, 1500];
738735
for _ in 0..10000 {
739736
let result = choices
740-
.choose_multiple_weighted(&mut rng, 2, |item| item.1)
737+
.sample_weighted(&mut rng, 2, |item| item.1)
741738
.unwrap()
742739
.collect::<Vec<_>>();
743740

0 commit comments

Comments
 (0)