pub struct WeightedIndex<X>where
    X: SampleUniform + PartialOrd,{ /* private fields */ }
Expand description

Straightforward weighted distribution.

Unlike rand::distributions::weighted::WeightedIndex, this distribution won’t precalculate the cumulative weights, but will do it on the fly.

Implementations§

source§

impl<X> WeightedIndex<X>where X: SampleUniform + Zero + AddAssign + SubAssign + PartialOrd + Copy,

source

pub fn new(weights: Vec<X>) -> Result<Self, Error>

Creates a new WeightedIndex from a given Vec.

Fails if weights is empty, or if any of the weights is negative, or if the sum of all weights is zero.

source

pub fn update(&mut self, new_weights: &[(usize, &X)]) -> Result<(), Error>

Updates the weights.

Fails if the total weight will become zero, or if any of the new weight is negative, or if any of the weight indices is out of range.

In case update fails, the weights are not changed.

source

pub fn get_weight(&self, index: usize) -> X

Returns the weight.

Trait Implementations§

source§

impl<X> Distribution<usize> for WeightedIndex<X>where X: SampleUniform + Zero + AddAssign + PartialOrd + Copy,

source§

fn sample<R>(&self, rng: &mut R) -> usizewhere R: Rng + ?Sized,

Samples a value from the distribution.

source§

fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T>where R: Rng, Self: Sized,

Create an iterator that generates random values of T, using rng as the source of randomness. Read more
source§

fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S>where F: Fn(T) -> S, Self: Sized,

Create a distribution of values of ‘S’ by mapping the output of Self through the closure F Read more

Auto Trait Implementations§

§

impl<X> RefUnwindSafe for WeightedIndex<X>where X: RefUnwindSafe, <X as SampleUniform>::Sampler: RefUnwindSafe,

§

impl<X> Send for WeightedIndex<X>where X: Send, <X as SampleUniform>::Sampler: Send,

§

impl<X> Sync for WeightedIndex<X>where X: Sync, <X as SampleUniform>::Sampler: Sync,

§

impl<X> Unpin for WeightedIndex<X>where X: Unpin, <X as SampleUniform>::Sampler: Unpin,

§

impl<X> UnwindSafe for WeightedIndex<X>where X: UnwindSafe, <X as SampleUniform>::Sampler: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

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

§

fn vzip(self) -> V