Skip to main content

igraph_psumtree_t

Struct igraph_psumtree_t 

Source
pub struct igraph_psumtree_t { /* private fields */ }

Implementations§

Source§

impl igraph_psumtree_t

Source

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.

Source

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.

Source

pub fn len(&self) -> usize

The number of items (igraph_psumtree_size).

Source

pub fn is_empty(&self) -> bool

Whether the tree has no items (never true for a successfully created tree).

Source

pub fn get(&self, index: usize) -> Option<f64>

The weight of item index, or None if out of bounds (igraph_psumtree_get). O(1).

Source

pub fn weights(&self) -> Vec<f64>

The weights of all the items, in order.

Source

pub fn sum(&self) -> f64

The total weight of the items (igraph_psumtree_sum). O(1).

Source

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.

Source

pub fn reset(&mut self)

Resets all the weights to zero (igraph_psumtree_reset).

Source

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

Source

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)));

Trait Implementations§

Source§

impl Clone for igraph_psumtree_t

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for igraph_psumtree_t

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for igraph_psumtree_t

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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