VoseAlias

Struct VoseAlias 

Source
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>, /* private fields */ }
Expand description

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§

Source§

impl<T> VoseAlias<T>
where T: Display + Copy + Hash + Eq + Debug,

Source

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

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]);
Source

pub fn sample(&self) -> T

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§

Source§

impl<T> Clone for VoseAlias<T>
where T: Display + Copy + Hash + Eq + Debug + Clone,

Source§

fn clone(&self) -> VoseAlias<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for VoseAlias<T>
where T: Display + Copy + Hash + Eq + Debug + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for VoseAlias<T>
where T: Display + Copy + Hash + Eq + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> PartialEq for VoseAlias<T>
where T: Display + Copy + Hash + Eq + Debug,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Eq for VoseAlias<T>
where T: Display + Copy + Hash + Eq + Debug,

Auto Trait Implementations§

§

impl<T> Freeze for VoseAlias<T>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V