DigitBinIndexGeneric

Struct DigitBinIndexGeneric 

Source
pub struct DigitBinIndexGeneric<B: DigitBin> {
    pub root: Node<B>,
    pub precision: u8,
    /* private fields */
}
Expand description

A data structure that organizes weighted items into bins based on their decimal digits to enable fast weighted random selection and updates.

This structure is a specialized radix tree optimized for sequential sampling (like in Wallenius’ distribution). It makes a deliberate engineering trade-off: it sacrifices a small, controllable amount of precision by binning items, but in return, it achieves O(P) performance for its core operations, where P is the configured precision. This is significantly faster than the O(log N) performance of general-purpose structures like a Fenwick Tree for its ideal use case.

§Type Parameters

  • B - The bin container type for leaf nodes. Must implement the DigitBin trait. Common choices are Vec<u32> for maximum speed with small bins, or RoaringBitmap for memory efficiency with large, sparse bins.

§Examples

use digit_bin_index::DigitBinIndexGeneric;
// Use Vec<u32> for leaf bins
let mut index = DigitBinIndexGeneric::<Vec<u32>>::new();
// Or use RoaringBitmap for leaf bins
// let mut index = DigitBinIndexGeneric::<roaring::RoaringBitmap>::new();

Fields§

§root: Node<B>

The root node of the tree.

§precision: u8

The precision (number of decimal places) used for binning.

Implementations§

Source§

impl<B: DigitBin> DigitBinIndexGeneric<B>

Source

pub fn new() -> Self

Source

pub fn with_precision(precision: u8) -> Self

Source

pub fn add(&mut self, individual_id: u64, weight: f64)

Source

pub fn add_many(&mut self, items: &[(u64, f64)])

Adds multiple items to the index in a highly optimized batch operation.

This method is significantly faster than calling add in a loop for large collections of items. It works by first pre-processing the input items, grouping them by their shared weight path (e.g., all items with weight 0.123…). It then traverses the tree once per group, rather than once per item, drastically reducing function call overhead and improving CPU cache performance by performing aggregated updates at each node.

§Arguments
  • items - A slice of (individual_id, weight) tuples to add to the index.
Source

pub fn remove(&mut self, individual_id: u64, weight: f64) -> bool

Source

pub fn remove_many(&mut self, items: &[(u64, f64)]) -> bool

Removes multiple items from the index in a highly optimized batch operation.

This method is significantly faster than calling remove in a loop. It groups the items to be removed by their weight path and traverses the tree only once per group, performing aggregated updates on the way up.

The (id, weight) pairs must match items that are currently in the index. If a given pair is not found, it is silently ignored.

§Arguments
  • items - A slice of (id, weight) tuples to remove from the index.
Source

pub fn select(&mut self) -> Option<(u64, f64)>

Source

pub fn select_many(&mut self, num_to_draw: u64) -> Option<Vec<(u64, f64)>>

Source

pub fn select_and_remove(&mut self) -> Option<(u64, f64)>

Source

pub fn select_and_optionally_remove( &mut self, with_removal: bool, ) -> Option<(u64, f64)>

Source

pub fn select_many_and_remove( &mut self, num_to_draw: u64, ) -> Option<Vec<(u64, f64)>>

Source

pub fn select_many_and_optionally_remove( &mut self, num_to_draw: u64, with_removal: bool, ) -> Option<Vec<(u64, f64)>>

Source

pub fn count(&self) -> u64

Source

pub fn total_weight(&self) -> f64

Source

pub fn print_stats_generic(&self)

Prints detailed statistics about the tree: node count, bin stats, and weight stats.

Trait Implementations§

Source§

impl<B: Clone + DigitBin> Clone for DigitBinIndexGeneric<B>

Source§

fn clone(&self) -> DigitBinIndexGeneric<B>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<B: Debug + DigitBin> Debug for DigitBinIndexGeneric<B>

Source§

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

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

impl<B: DigitBin> Default for DigitBinIndexGeneric<B>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<B> Freeze for DigitBinIndexGeneric<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for DigitBinIndexGeneric<B>
where B: RefUnwindSafe,

§

impl<B> Send for DigitBinIndexGeneric<B>
where B: Send,

§

impl<B> Sync for DigitBinIndexGeneric<B>
where B: Sync,

§

impl<B> Unpin for DigitBinIndexGeneric<B>
where B: Unpin,

§

impl<B> UnwindSafe for DigitBinIndexGeneric<B>
where B: 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<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.
Source§

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

Source§

fn vzip(self) -> V