Skip to main content

AliasTable

Struct AliasTable 

Source
pub struct AliasTable<T> { /* private fields */ }
Expand description

Generic alias table for O(1) weighted sampling.

Construct with AliasTable::from_weights or AliasTable::uniform, then sample with AliasTable::sample.

Implementations§

Source§

impl<T: Clone> AliasTable<T>

Source

pub fn from_weights(outcomes: &[T], weights: &[f64]) -> Self

Build an alias table from outcomes and their weights.

Weights do not need to be normalized — they are scaled internally. All weights must be non-negative; at least one must be positive.

Source

pub fn uniform(outcomes: &[T]) -> Self

Build a uniform alias table (all outcomes equally weighted).

Source

pub fn sample(&self, input: u64) -> &T

Sample an outcome from a uniform u64 input.

The input is split into two independent parts: the low bits select a slot, the high bits determine the bias test. This avoids correlation between slot selection and the bias coin flip.

Source

pub fn len(&self) -> usize

Number of outcomes in the table.

Source

pub fn is_empty(&self) -> bool

Whether the table has no outcomes.

Auto Trait Implementations§

§

impl<T> Freeze for AliasTable<T>
where Vec<AliasSlot<T>>: Freeze,

§

impl<T> RefUnwindSafe for AliasTable<T>
where Vec<AliasSlot<T>>: RefUnwindSafe,

§

impl<T> Send for AliasTable<T>
where Vec<AliasSlot<T>>: Send,

§

impl<T> Sync for AliasTable<T>
where Vec<AliasSlot<T>>: Sync,

§

impl<T> Unpin for AliasTable<T>
where Vec<AliasSlot<T>>: Unpin,

§

impl<T> UnsafeUnpin for AliasTable<T>
where Vec<AliasSlot<T>>: UnsafeUnpin,

§

impl<T> UnwindSafe for AliasTable<T>
where Vec<AliasSlot<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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.