9
9
//! This module contains an implementation of alias method for sampling random
10
10
//! indices with probabilities proportional to a collection of weights.
11
11
12
- use super :: WeightError ;
12
+ use super :: Error ;
13
13
use crate :: { uniform:: SampleUniform , Distribution , Uniform } ;
14
14
use alloc:: { boxed:: Box , vec, vec:: Vec } ;
15
15
use core:: fmt;
@@ -41,7 +41,7 @@ use serde::{Deserialize, Serialize};
41
41
/// # Example
42
42
///
43
43
/// ```
44
- /// use rand_distr::WeightedAliasIndex;
44
+ /// use rand_distr::weighted:: WeightedAliasIndex;
45
45
/// use rand::prelude::*;
46
46
///
47
47
/// let choices = vec!['a', 'b', 'c'];
@@ -85,14 +85,14 @@ impl<W: AliasableWeight> WeightedAliasIndex<W> {
85
85
/// Creates a new [`WeightedAliasIndex`].
86
86
///
87
87
/// 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,
90
90
/// 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 > {
93
93
let n = weights. len ( ) ;
94
94
if n == 0 || n > u32:: MAX as usize {
95
- return Err ( WeightError :: InvalidInput ) ;
95
+ return Err ( Error :: InvalidInput ) ;
96
96
}
97
97
let n = n as u32 ;
98
98
@@ -103,7 +103,7 @@ impl<W: AliasableWeight> WeightedAliasIndex<W> {
103
103
. iter ( )
104
104
. all ( |& w| W :: ZERO <= w && w <= max_weight_size)
105
105
{
106
- return Err ( WeightError :: InvalidWeight ) ;
106
+ return Err ( Error :: InvalidWeight ) ;
107
107
}
108
108
109
109
// The sum of weights will represent 100% of no alias odds.
@@ -115,7 +115,7 @@ impl<W: AliasableWeight> WeightedAliasIndex<W> {
115
115
weight_sum
116
116
} ;
117
117
if weight_sum == W :: ZERO {
118
- return Err ( WeightError :: InsufficientNonZero ) ;
118
+ return Err ( Error :: InsufficientNonZero ) ;
119
119
}
120
120
121
121
// `weight_sum` would have been zero if `try_from_lossy` causes an error here.
@@ -384,23 +384,23 @@ mod test {
384
384
// Floating point special cases
385
385
assert_eq ! (
386
386
WeightedAliasIndex :: new( vec![ f32 :: INFINITY ] ) . unwrap_err( ) ,
387
- WeightError :: InvalidWeight
387
+ Error :: InvalidWeight
388
388
) ;
389
389
assert_eq ! (
390
390
WeightedAliasIndex :: new( vec![ -0_f32 ] ) . unwrap_err( ) ,
391
- WeightError :: InsufficientNonZero
391
+ Error :: InsufficientNonZero
392
392
) ;
393
393
assert_eq ! (
394
394
WeightedAliasIndex :: new( vec![ -1_f32 ] ) . unwrap_err( ) ,
395
- WeightError :: InvalidWeight
395
+ Error :: InvalidWeight
396
396
) ;
397
397
assert_eq ! (
398
398
WeightedAliasIndex :: new( vec![ f32 :: NEG_INFINITY ] ) . unwrap_err( ) ,
399
- WeightError :: InvalidWeight
399
+ Error :: InvalidWeight
400
400
) ;
401
401
assert_eq ! (
402
402
WeightedAliasIndex :: new( vec![ f32 :: NAN ] ) . unwrap_err( ) ,
403
- WeightError :: InvalidWeight
403
+ Error :: InvalidWeight
404
404
) ;
405
405
}
406
406
@@ -418,11 +418,11 @@ mod test {
418
418
// Signed integer special cases
419
419
assert_eq ! (
420
420
WeightedAliasIndex :: new( vec![ -1_i128 ] ) . unwrap_err( ) ,
421
- WeightError :: InvalidWeight
421
+ Error :: InvalidWeight
422
422
) ;
423
423
assert_eq ! (
424
424
WeightedAliasIndex :: new( vec![ i128 :: MIN ] ) . unwrap_err( ) ,
425
- WeightError :: InvalidWeight
425
+ Error :: InvalidWeight
426
426
) ;
427
427
}
428
428
@@ -440,11 +440,11 @@ mod test {
440
440
// Signed integer special cases
441
441
assert_eq ! (
442
442
WeightedAliasIndex :: new( vec![ -1_i8 ] ) . unwrap_err( ) ,
443
- WeightError :: InvalidWeight
443
+ Error :: InvalidWeight
444
444
) ;
445
445
assert_eq ! (
446
446
WeightedAliasIndex :: new( vec![ i8 :: MIN ] ) . unwrap_err( ) ,
447
- WeightError :: InvalidWeight
447
+ Error :: InvalidWeight
448
448
) ;
449
449
}
450
450
@@ -491,15 +491,15 @@ mod test {
491
491
492
492
assert_eq ! (
493
493
WeightedAliasIndex :: <W >:: new( vec![ ] ) . unwrap_err( ) ,
494
- WeightError :: InvalidInput
494
+ Error :: InvalidInput
495
495
) ;
496
496
assert_eq ! (
497
497
WeightedAliasIndex :: new( vec![ W :: ZERO ] ) . unwrap_err( ) ,
498
- WeightError :: InsufficientNonZero
498
+ Error :: InsufficientNonZero
499
499
) ;
500
500
assert_eq ! (
501
501
WeightedAliasIndex :: new( vec![ W :: MAX , W :: MAX ] ) . unwrap_err( ) ,
502
- WeightError :: InvalidWeight
502
+ Error :: InvalidWeight
503
503
) ;
504
504
}
505
505
0 commit comments