[][src]Struct vose_alias::VoseAlias

pub struct VoseAlias<T> where
    T: Display + Copy + Hash + Eq + Debug
{ pub elements: Vec<T>, pub alias: HashMap<T, T>, pub prob: HashMap<T, f32>, // some fields omitted }

A structure containing the necessary Vose-Alias tables.

The structure contains the following attributes:

  1. A vector containing the elements to sample frmo
  2. The Alias table, created from the Vose-Alias initialization step
  3. The Probability table, created frmo the Vose-Alias initialization step

The structure is created by the function vose_alias::new(). See its documentation for more details.

Internally, the elements are used as indexes in HashMap and Vec. Therefore, the type T must implement the following traits:

  • Copy
  • Hash
  • Eq
  • Debug

Fields

elements: Vec<T>alias: HashMap<T, T>prob: HashMap<T, f32>

Implementations

impl<T> VoseAlias<T> where
    T: Display + Copy + Hash + Eq + Debug
[src]

pub fn new(element_vector: Vec<T>, probability_vector: Vec<f32>) -> VoseAlias<T>[src]

Returns the Vose-Alias object containing the element vector as well as the alias and probability tables.

The element_vector contains the list of elements that should be sampled from. The probability_vector contains the probability distribution to be sampled with. element_vector and probability_vector should have the same size and probability_vector should describe a well-formed probability distribution.

Panics

The function panics in two casese:

  1. the element_vector and the probability_vector do not contain the same number of elements
  2. the sum of the elements in probability_vector is not equal to 1 (with a floating number precision of 0.0001), meaning that probability_vector does not describe a well formed probability distribution

Examples

use vose_alias::VoseAlias;
 
// Creates a Vose-Alias object from a list of Integer elements
let va = VoseAlias::new(vec![1, 2, 3, 4], vec![0.5, 0.2, 0.2, 0.1]);

pub fn sample(&self) -> T[src]

Returns a sampled element from a previously created Vose-Alias object.

This function uses a VoseAlias object previously created using the method vose_alias::new() to sample in linear time an element of type T.

Panics

This function panics only if the lists created in vose_alias::new() are not correctly form, which would indicate a internal bug in the code. If your code panics while using this function, please fill in an issue report.

Examples

use vose_alias::VoseAlias;

// Samples an integer from a list and prints it. 
let va = VoseAlias::new(vec![1, 2, 3, 4], vec![0.5, 0.2, 0.2, 0.1]);
let element = va.sample();
println!("{}", element);
 

Trait Implementations

impl<T: Clone> Clone for VoseAlias<T> where
    T: Display + Copy + Hash + Eq + Debug
[src]

impl<T: Debug> Debug for VoseAlias<T> where
    T: Display + Copy + Hash + Eq + Debug
[src]

impl<T> Display for VoseAlias<T> where
    T: Display + Copy + Hash + Eq + Debug
[src]

impl<T> Eq for VoseAlias<T> where
    T: Display + Copy + Hash + Eq + Debug
[src]

impl<T> PartialEq<VoseAlias<T>> for VoseAlias<T> where
    T: Display + Copy + Hash + Eq + Debug
[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for VoseAlias<T> where
    T: RefUnwindSafe

impl<T> Send for VoseAlias<T> where
    T: Send

impl<T> Sync for VoseAlias<T> where
    T: Sync

impl<T> Unpin for VoseAlias<T> where
    T: Unpin

impl<T> UnwindSafe for VoseAlias<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,