99//! This module contains an implementation of alias method for sampling random
1010//! indices with probabilities proportional to a collection of weights.
1111
12- use super :: WeightError ;
12+ use super :: Error ;
1313use crate :: { uniform:: SampleUniform , Distribution , Uniform } ;
1414use alloc:: { boxed:: Box , vec, vec:: Vec } ;
1515use core:: fmt;
@@ -41,7 +41,7 @@ use serde::{Deserialize, Serialize};
4141/// # Example
4242///
4343/// ```
44- /// use rand_distr::WeightedAliasIndex;
44+ /// use rand_distr::weighted:: WeightedAliasIndex;
4545/// use rand::prelude::*;
4646///
4747/// let choices = vec!['a', 'b', 'c'];
@@ -85,14 +85,14 @@ impl<W: AliasableWeight> WeightedAliasIndex<W> {
8585 /// Creates a new [`WeightedAliasIndex`].
8686 ///
8787 /// Error cases:
88- /// - [`WeightError ::InvalidInput`] when `weights.len()` is zero or greater than `u32::MAX`.
89- /// - [`WeightError ::InvalidWeight`] when a weight is not-a-number,
88+ /// - [`Error ::InvalidInput`] when `weights.len()` is zero or greater than `u32::MAX`.
89+ /// - [`Error ::InvalidWeight`] when a weight is not-a-number,
9090 /// negative or greater than `max = W::MAX / weights.len()`.
91- /// - [`WeightError ::InsufficientNonZero`] when the sum of all weights is zero.
92- pub fn new ( weights : Vec < W > ) -> Result < Self , WeightError > {
91+ /// - [`Error ::InsufficientNonZero`] when the sum of all weights is zero.
92+ pub fn new ( weights : Vec < W > ) -> Result < Self , Error > {
9393 let n = weights. len ( ) ;
9494 if n == 0 || n > u32:: MAX as usize {
95- return Err ( WeightError :: InvalidInput ) ;
95+ return Err ( Error :: InvalidInput ) ;
9696 }
9797 let n = n as u32 ;
9898
@@ -103,7 +103,7 @@ impl<W: AliasableWeight> WeightedAliasIndex<W> {
103103 . iter ( )
104104 . all ( |& w| W :: ZERO <= w && w <= max_weight_size)
105105 {
106- return Err ( WeightError :: InvalidWeight ) ;
106+ return Err ( Error :: InvalidWeight ) ;
107107 }
108108
109109 // The sum of weights will represent 100% of no alias odds.
@@ -115,7 +115,7 @@ impl<W: AliasableWeight> WeightedAliasIndex<W> {
115115 weight_sum
116116 } ;
117117 if weight_sum == W :: ZERO {
118- return Err ( WeightError :: InsufficientNonZero ) ;
118+ return Err ( Error :: InsufficientNonZero ) ;
119119 }
120120
121121 // `weight_sum` would have been zero if `try_from_lossy` causes an error here.
@@ -384,23 +384,23 @@ mod test {
384384 // Floating point special cases
385385 assert_eq ! (
386386 WeightedAliasIndex :: new( vec![ f32 :: INFINITY ] ) . unwrap_err( ) ,
387- WeightError :: InvalidWeight
387+ Error :: InvalidWeight
388388 ) ;
389389 assert_eq ! (
390390 WeightedAliasIndex :: new( vec![ -0_f32 ] ) . unwrap_err( ) ,
391- WeightError :: InsufficientNonZero
391+ Error :: InsufficientNonZero
392392 ) ;
393393 assert_eq ! (
394394 WeightedAliasIndex :: new( vec![ -1_f32 ] ) . unwrap_err( ) ,
395- WeightError :: InvalidWeight
395+ Error :: InvalidWeight
396396 ) ;
397397 assert_eq ! (
398398 WeightedAliasIndex :: new( vec![ f32 :: NEG_INFINITY ] ) . unwrap_err( ) ,
399- WeightError :: InvalidWeight
399+ Error :: InvalidWeight
400400 ) ;
401401 assert_eq ! (
402402 WeightedAliasIndex :: new( vec![ f32 :: NAN ] ) . unwrap_err( ) ,
403- WeightError :: InvalidWeight
403+ Error :: InvalidWeight
404404 ) ;
405405 }
406406
@@ -418,11 +418,11 @@ mod test {
418418 // Signed integer special cases
419419 assert_eq ! (
420420 WeightedAliasIndex :: new( vec![ -1_i128 ] ) . unwrap_err( ) ,
421- WeightError :: InvalidWeight
421+ Error :: InvalidWeight
422422 ) ;
423423 assert_eq ! (
424424 WeightedAliasIndex :: new( vec![ i128 :: MIN ] ) . unwrap_err( ) ,
425- WeightError :: InvalidWeight
425+ Error :: InvalidWeight
426426 ) ;
427427 }
428428
@@ -440,11 +440,11 @@ mod test {
440440 // Signed integer special cases
441441 assert_eq ! (
442442 WeightedAliasIndex :: new( vec![ -1_i8 ] ) . unwrap_err( ) ,
443- WeightError :: InvalidWeight
443+ Error :: InvalidWeight
444444 ) ;
445445 assert_eq ! (
446446 WeightedAliasIndex :: new( vec![ i8 :: MIN ] ) . unwrap_err( ) ,
447- WeightError :: InvalidWeight
447+ Error :: InvalidWeight
448448 ) ;
449449 }
450450
@@ -491,15 +491,15 @@ mod test {
491491
492492 assert_eq ! (
493493 WeightedAliasIndex :: <W >:: new( vec![ ] ) . unwrap_err( ) ,
494- WeightError :: InvalidInput
494+ Error :: InvalidInput
495495 ) ;
496496 assert_eq ! (
497497 WeightedAliasIndex :: new( vec![ W :: ZERO ] ) . unwrap_err( ) ,
498- WeightError :: InsufficientNonZero
498+ Error :: InsufficientNonZero
499499 ) ;
500500 assert_eq ! (
501501 WeightedAliasIndex :: new( vec![ W :: MAX , W :: MAX ] ) . unwrap_err( ) ,
502- WeightError :: InvalidWeight
502+ Error :: InvalidWeight
503503 ) ;
504504 }
505505
0 commit comments