[][src]Struct weighted_random_list::WeightedRandomList

pub struct WeightedRandomList<T> { /* fields omitted */ }

Stores the data and keeps track of the chances. It is built so that you can add and remove elements withouth the need of recalculation.


impl<T> WeightedRandomList<T>[src]

pub fn new() -> WeightedRandomList<T>[src]

pub fn push(&mut self, weight: usize, new: T)[src]

Add an element to the collection with a given weight

Panics if the sum of the weights exceed the holding datatype

pub fn get_random(&self) -> Option<&T>[src]

Get a random value from the collection or None if it is empty.

Accsess time is O(n), however the linear storage compensates for this overhead.


let mut l = WeightedRandomList::new();
assert_eq!(None, l.get_random());

l.push(100, 42usize);
assert_eq!(Some(&42), l.get_random());

impl<T> WeightedRandomList<T> where
    T: PartialEq + Clone

If the stored type T supports PartialEq and Clone we can manipulate the contents of this container. Therefore shifting the chances of one element to be selected.

Example adjust weight after insertion

let mut l = WeightedRandomList::new();
l.push(10, 1);
l.push(10, 2);
l.push(100, 3);
// total weights is now 120

    let mut three = l.entry_first(&3);
// total weights is now 100

pub fn entry_first(&mut self, needle: &T) -> Entry<T>[src]

Find the first entry in the list matching needle and manipulate potential entries:

let mut l = WeightedRandomList::new();


Trait Implementations

impl<T> FromIterator<(usize, T)> for WeightedRandomList<T>[src]

Enable clollect

Auto Trait Implementations

impl<T> Send for WeightedRandomList<T> where
    T: Send

impl<T> Sync for WeightedRandomList<T> where
    T: Sync

Blanket Implementations

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

impl<T> From for T[src]

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

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized

impl<T> BorrowMut for T where
    T: ?Sized

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

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized