pub struct WeightedCollection<T> {
items: Vec<(T, f64)>,
total_weight: f64,
}Expand description
A collection of items with associated weights for probabilistic selection.
Weights must be positive (> 0.0); items with non-positive weights are
treated as having zero probability for pick_one /
pick_index and are placed at the end of the shuffled
output in into_shuffled.
§Examples
use hopr_utils::statistics::WeightedCollection;
let wc = WeightedCollection::from(vec![("rare", 0.1), ("common", 10.0)]);
let picked = wc.pick_one().expect("non-empty collection");
assert!(picked == "rare" || picked == "common");Fields§
§items: Vec<(T, f64)>§total_weight: f64Pre-computed sum of positive weights (cached to avoid recomputing on every pick).
Implementations§
Source§impl<T> WeightedCollection<T>
impl<T> WeightedCollection<T>
Sourcepub fn total_weight(&self) -> f64
pub fn total_weight(&self) -> f64
Returns the sum of positive weights in this collection.
Sourcepub fn probability_of(&self, weight: f64) -> f64
pub fn probability_of(&self, weight: f64) -> f64
Returns the sampling probability that an entry carrying weight would
receive in this collection — i.e. weight.max(0.0) / total_positive_weight.
Returns 0.0 if weight <= 0.0 or if the collection holds no positive-weight
entries (empty or fully non-positive collection).
Source§impl<T> WeightedCollection<T>
impl<T> WeightedCollection<T>
Sourcepub fn pick_index(&self) -> Option<usize>
pub fn pick_index(&self) -> Option<usize>
Returns the index of a randomly selected item, weighted by probability proportional to its weight.
Returns None if the collection is empty or all weights are non-positive.
Source§impl<T> WeightedCollection<T>
impl<T> WeightedCollection<T>
Source§impl<T: Clone> WeightedCollection<T>
impl<T: Clone> WeightedCollection<T>
Source§impl<T> WeightedCollection<T>
impl<T> WeightedCollection<T>
Sourcepub fn into_shuffled(self) -> Vec<T>
pub fn into_shuffled(self) -> Vec<T>
Consume the collection and return items in a weighted random permutation.
Uses the Efraimidis–Spirakis algorithm: each item is assigned a key
random()^(1/weight) and the items are sorted by descending key.
Higher-weight items appear earlier with higher probability, but all
items retain a nonzero chance of appearing at any position.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for WeightedCollection<T>
impl<T> RefUnwindSafe for WeightedCollection<T>where
T: RefUnwindSafe,
impl<T> Send for WeightedCollection<T>where
T: Send,
impl<T> Sync for WeightedCollection<T>where
T: Sync,
impl<T> Unpin for WeightedCollection<T>where
T: Unpin,
impl<T> UnwindSafe for WeightedCollection<T>where
T: 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
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more