-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #945 from dhardy/alias-method
Move Alias-method WeightedIndex to rand_distr
- Loading branch information
Showing
6 changed files
with
98 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2018 Developers of the Rand project. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! Weighted index sampling | ||
//! | ||
//! This module provides two implementations for sampling indices: | ||
//! | ||
//! * [`WeightedIndex`] allows `O(log N)` sampling | ||
//! * [`alias_method::WeightedIndex`] allows `O(1)` sampling, but with | ||
//! much greater set-up cost | ||
//! | ||
//! [`alias_method::WeightedIndex`]: alias_method/struct.WeightedIndex.html | ||
|
||
pub mod alias_method; | ||
|
||
pub use rand::distributions::weighted::{WeightedError, WeightedIndex}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2018 Developers of the Rand project. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//! Weighted index sampling | ||
//! | ||
//! This module is deprecated. Use [`crate::distributions::WeightedIndex`] and | ||
//! [`crate::distributions::WeightedError`] instead. | ||
|
||
pub use super::{WeightedIndex, WeightedError}; | ||
|
||
#[allow(missing_docs)] | ||
#[deprecated(since = "0.8.0", note = "moved to rand_distr crate")] | ||
pub mod alias_method { | ||
// This module exists to provide a deprecation warning which minimises | ||
// compile errors, but still fails to compile if ever used. | ||
use core::marker::PhantomData; | ||
#[cfg(not(feature = "std"))] use crate::alloc::vec::Vec; | ||
use super::WeightedError; | ||
|
||
#[derive(Debug)] | ||
pub struct WeightedIndex<W: Weight> { | ||
_phantom: PhantomData<W>, | ||
} | ||
impl<W: Weight> WeightedIndex<W> { | ||
pub fn new(_weights: Vec<W>) -> Result<Self, WeightedError> { | ||
Err(WeightedError::NoItem) | ||
} | ||
} | ||
|
||
pub trait Weight {} | ||
macro_rules! impl_weight { | ||
() => {}; | ||
($T:ident, $($more:ident,)*) => { | ||
impl Weight for $T {} | ||
impl_weight!($($more,)*); | ||
}; | ||
} | ||
impl_weight!(f64, f32,); | ||
impl_weight!(u8, u16, u32, u64, usize,); | ||
impl_weight!(i8, i16, i32, i64, isize,); | ||
#[cfg(not(target_os = "emscripten"))] | ||
impl_weight!(u128, i128,); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters