Struct flechasdb::distribution::WeightedIndex
source · 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,
impl<X> WeightedIndex<X>where X: SampleUniform + Zero + AddAssign + SubAssign + PartialOrd + Copy,
sourcepub fn new(weights: Vec<X>) -> Result<Self, Error>
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.
sourcepub fn update(&mut self, new_weights: &[(usize, &X)]) -> Result<(), Error>
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.
sourcepub fn get_weight(&self, index: usize) -> X
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,
impl<X> Distribution<usize> for WeightedIndex<X>where X: SampleUniform + Zero + AddAssign + PartialOrd + Copy,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more