pub struct WeightedTable<T>{ /* private fields */ }
Expand description
WeightedTable
This is a simple implementation of a weighted table. It is designed to be used with a random number generator. It can be used as follows:
use rantz_random::prelude::{WeightedTable, RandomContainer};
let mut table = WeightedTable::new();
table.insert("Bob", 10); // Bob has a weight of 10
table.insert("Alice", 20); // Alice has a weight of 20
table.remove(&"Bob"); // Bob is removed
table.random(); // Returns a random element from the table based on the weights
The table is iterable and returns owned tuples of (value, weight)
For references, use the iter method
For mutable references, use the [iter_mut]
(WeightedTable::iter_mut
) method
use rantz_random::prelude::WeightedTable;
let mut table = WeightedTable::new();
table.insert("Bob".to_string(), 10); // Bob has a weight of 10
table.insert("Alice".to_string(), 20); // Alice has a weight of 20
for (value, weight) in table.iter() {
println!("Reference {} has a weight of {}", value, weight);
}
for (value, weight) in table.iter_mut() {
*value += " Test";
*weight += 10;
println!("Reference {} has a weight of {}", value, weight);
}
for value in table { // Must be done last as consumes the table
println!("Owned {}", value);
}
Adding an element that already exists will update the weight of the existing element.
Implementations§
Source§impl<T> WeightedTable<T>
impl<T> WeightedTable<T>
Sourcepub fn from_vec(vec: Vec<(T, u32)>) -> Self
pub fn from_vec(vec: Vec<(T, u32)>) -> Self
Creates a new WeightedTable
from a vector of tuples
of (value, weight)
Sourcepub fn get_entry(&self, index: usize) -> Option<(T, u32)>
pub fn get_entry(&self, index: usize) -> Option<(T, u32)>
Returns the entry at the specified index
Sourcepub fn get_entry_ref(&self, index: usize) -> Option<(&'_ T, &'_ u32)>
pub fn get_entry_ref(&self, index: usize) -> Option<(&'_ T, &'_ u32)>
Returns the entry at the specified index as a reference
Sourcepub fn get_entry_mut(
&mut self,
index: usize,
) -> Option<(&'_ mut T, &'_ mut u32)>
pub fn get_entry_mut( &mut self, index: usize, ) -> Option<(&'_ mut T, &'_ mut u32)>
Returns the entry at the specified index as a mutable reference
Sourcepub fn get_weight(&self, value: &T) -> Option<u32>
pub fn get_weight(&self, value: &T) -> Option<u32>
Returns the weight of the specified value
Sourcepub fn get_weight_mut(&mut self, value: &T) -> Option<&mut u32>
pub fn get_weight_mut(&mut self, value: &T) -> Option<&mut u32>
Returns the weight of the specified value as a mutable reference
Sourcepub fn random_with(&self, n: u32) -> (T, u32)
pub fn random_with(&self, n: u32) -> (T, u32)
Returns a random element with the specified weight
Sourcepub fn iter(&self) -> impl Iterator<Item = (&'_ T, &'_ u32)>
pub fn iter(&self) -> impl Iterator<Item = (&'_ T, &'_ u32)>
Returns an iterator over the table
Trait Implementations§
Source§impl<T> Clone for WeightedTable<T>
impl<T> Clone for WeightedTable<T>
Source§fn clone(&self) -> WeightedTable<T>
fn clone(&self) -> WeightedTable<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T> Debug for WeightedTable<T>
impl<T> Debug for WeightedTable<T>
Source§impl<T> Default for WeightedTable<T>
impl<T> Default for WeightedTable<T>
Source§impl<'a, T> FromIterator<(T, &'a u32)> for WeightedTable<T>
impl<'a, T> FromIterator<(T, &'a u32)> for WeightedTable<T>
Source§impl<T> FromIterator<(T, u32)> for WeightedTable<T>
impl<T> FromIterator<(T, u32)> for WeightedTable<T>
Source§impl<T> IntoIterator for WeightedTable<T>
impl<T> IntoIterator for WeightedTable<T>
Source§impl<T> RandomWeightedContainer<T> for WeightedTable<T>
impl<T> RandomWeightedContainer<T> for WeightedTable<T>
Source§fn max_weight(&self) -> u32
fn max_weight(&self) -> u32
Source§fn weights(&self) -> &Vec<u32>
fn weights(&self) -> &Vec<u32>
Source§fn random_weight(&self) -> Option<u32>
fn random_weight(&self) -> Option<u32>
Source§fn weighted_random_with_weight(&self, weight: u32) -> Option<Self::Item>
fn weighted_random_with_weight(&self, weight: u32) -> Option<Self::Item>
Source§fn weighted_random(&self) -> Option<Self::Item>
fn weighted_random(&self) -> Option<Self::Item>
Auto Trait Implementations§
impl<T> Freeze for WeightedTable<T>
impl<T> RefUnwindSafe for WeightedTable<T>where
T: RefUnwindSafe,
impl<T> Send for WeightedTable<T>where
T: Send,
impl<T> Sync for WeightedTable<T>where
T: Sync,
impl<T> Unpin for WeightedTable<T>where
T: Unpin,
impl<T> UnwindSafe for WeightedTable<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T
ShaderType
for self
. When used in AsBindGroup
derives, it is safe to assume that all images in self
exist.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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Self
using data from the given World
.