From c8e5e35b22b8a139882fe7fe377be133992456d5 Mon Sep 17 00:00:00 2001 From: xmakro Date: Wed, 7 Feb 2024 17:32:22 +0000 Subject: [PATCH] more comments --- rand_distr/src/weighted_tree.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rand_distr/src/weighted_tree.rs b/rand_distr/src/weighted_tree.rs index 370c38914d..6883967981 100644 --- a/rand_distr/src/weighted_tree.rs +++ b/rand_distr/src/weighted_tree.rs @@ -232,7 +232,7 @@ impl + Weight> /// /// Returns an error if there are no elements or all weights are zero. This /// is unlike [`Distribution::sample`], which panics in those cases. - fn safe_sample(&self, rng: &mut R) -> Result { + fn try_sample(&self, rng: &mut R) -> Result { if self.subtotals.is_empty() { return Err(WeightedError::NoItem); } @@ -270,16 +270,16 @@ impl + Weight> } } +/// Samples a randomly selected index from the weighted distribution. +/// +/// Caution: This method panics if there are no elements or all weights are zero. However, +/// it is guaranteed that this method will not panic if a call to [`WeightedTreeIndex::is_valid`] +/// returns `true`. impl + Weight> Distribution for WeightedTreeIndex { - /// Samples a randomly selected index from the weighted distribution. - /// - /// Caution: This method panics if there are no elements or all weights are zero. However, - /// it is guaranteed that this method will not panic if a call to [`WeightedTreeIndex::is_valid`] - /// returns `true`. fn sample(&self, rng: &mut R) -> usize { - self.safe_sample(rng).unwrap() + self.try_sample(rng).unwrap() } } @@ -292,7 +292,7 @@ mod test { let mut rng = crate::test::rng(0x9c9fa0b0580a7031); let tree = WeightedTreeIndex::::new(&[]).unwrap(); assert_eq!( - tree.safe_sample(&mut rng).unwrap_err(), + tree.try_sample(&mut rng).unwrap_err(), WeightedError::NoItem ); } @@ -314,7 +314,7 @@ mod test { let tree = WeightedTreeIndex::::new(&[0.0, 0.0]).unwrap(); let mut rng = crate::test::rng(0x9c9fa0b0580a7031); assert_eq!( - tree.safe_sample(&mut rng).unwrap_err(), + tree.try_sample(&mut rng).unwrap_err(), WeightedError::AllWeightsZero ); }