pub struct igraph_psumtree_t { /* private fields */ }Implementations§
Source§impl igraph_psumtree_t
impl igraph_psumtree_t
Sourcepub fn new(size: usize) -> Result<Self>
pub fn new(size: usize) -> Result<Self>
Creates a tree with size items, all of weight zero
(igraph_psumtree_init).
§Errors
ErrorKind::InvalidValue if size
is zero.
Sourcepub fn from_weights(weights: &[f64]) -> Result<Self>
pub fn from_weights(weights: &[f64]) -> Result<Self>
Creates a tree whose items have the given weights.
§Errors
ErrorKind::InvalidValue if
weights is empty, or a weight is negative or not finite.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the tree has no items (never true for a successfully created tree).
Sourcepub fn get(&self, index: usize) -> Option<f64>
pub fn get(&self, index: usize) -> Option<f64>
The weight of item index, or None if out of bounds
(igraph_psumtree_get). O(1).
Sourcepub fn update(&mut self, index: usize, weight: f64) -> Result<()>
pub fn update(&mut self, index: usize, weight: f64) -> Result<()>
Sets the weight of item index (igraph_psumtree_update). O(log n).
§Errors
ErrorKind::InvalidValue if
index is out of bounds or weight is negative, infinite or NaN.
Sourcepub fn search(&self, value: f64) -> Result<usize>
pub fn search(&self, value: f64) -> Result<usize>
Finds the item whose cumulative weight interval contains value
(igraph_psumtree_search). O(log n).
Precisely, it returns the lowest index i such that the total weight
of the items before i is <= value and adding the weight of i
makes it > value. Items of weight zero are therefore never
returned.
§Errors
ErrorKind::InvalidValue unless
0 <= value < self.sum() (in particular when all weights are zero).
Sourcepub fn sample(&self) -> Option<usize>
pub fn sample(&self) -> Option<usize>
Draws an item with probability proportional to its weight, using the
thread’s default random number generator; None if all weights are
zero. O(log n).
§Examples
use igraph::{misc::PsumTree, prelude::*};
rng::seed(3)?;
let tree = PsumTree::from_weights(&[0.0, 1.0, 0.0])?;
assert!((0..100).all(|_| tree.sample() == Some(1)));