[−][src]Struct weighted_random_list::WeightedRandomList
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.
Methods
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.
Example
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,
[src]
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); three.set_weight(80); } // 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(); l.entry_first(&42).or_insert_with_weight(10);
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,
T: Send,
impl<T> Sync for WeightedRandomList<T> where
T: Sync,
T: Sync,
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,