[−][src]Struct vose_alias::VoseAlias
A structure containing the necessary Vose-Alias tables.
The structure contains the following attributes:
- A vector containing the elements to sample frmo
- The Alias table, created from the Vose-Alias initialization step
- 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]
T: Display + Copy + Hash + Eq + Debug,
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:
- the
element_vector
and theprobability_vector
do not contain the same number of elements - the sum of the elements in
probability_vector
is not equal to 1 (with a floating number precision of 0.0001), meaning thatprobability_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]
T: Display + Copy + Hash + Eq + Debug,
impl<T: Debug> Debug for VoseAlias<T> where
T: Display + Copy + Hash + Eq + Debug,
[src]
T: Display + Copy + Hash + Eq + Debug,
impl<T> Display for VoseAlias<T> where
T: Display + Copy + Hash + Eq + Debug,
[src]
T: Display + Copy + Hash + Eq + Debug,
impl<T> Eq for VoseAlias<T> where
T: Display + Copy + Hash + Eq + Debug,
[src]
T: Display + Copy + Hash + Eq + Debug,
impl<T> PartialEq<VoseAlias<T>> for VoseAlias<T> where
T: Display + Copy + Hash + Eq + Debug,
[src]
T: Display + Copy + Hash + Eq + Debug,
Auto Trait Implementations
impl<T> RefUnwindSafe for VoseAlias<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for VoseAlias<T> where
T: Send,
T: Send,
impl<T> Sync for VoseAlias<T> where
T: Sync,
T: Sync,
impl<T> Unpin for VoseAlias<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for VoseAlias<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,