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 weightsThe 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 new() -> WeightedTable<T>
pub fn new() -> WeightedTable<T>
Creates a new empty WeightedTable
Sourcepub fn from_vec(vec: Vec<(T, u32)>) -> WeightedTable<T>
pub fn from_vec(vec: Vec<(T, u32)>) -> WeightedTable<T>
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_mut(&mut self) -> impl Iterator<Item = (&mut T, &mut u32)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&mut T, &mut u32)>
Returns an iterator over the table mutably
Sourcepub fn combine(&mut self, other: WeightedTable<T>)
pub fn combine(&mut self, other: WeightedTable<T>)
Combines two tables (in place)
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 (const: unstable) · 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§fn default() -> WeightedTable<T>
fn default() -> WeightedTable<T>
Source§impl<'a, T> FromIterator<(T, &'a u32)> for WeightedTable<T>
impl<'a, T> FromIterator<(T, &'a u32)> for WeightedTable<T>
Source§fn from_iter<I>(iter: I) -> WeightedTable<T>where
I: IntoIterator<Item = (T, &'a u32)>,
fn from_iter<I>(iter: I) -> WeightedTable<T>where
I: IntoIterator<Item = (T, &'a u32)>,
Source§impl<T> FromIterator<(T, u32)> for WeightedTable<T>
impl<T> FromIterator<(T, u32)> for WeightedTable<T>
Source§fn from_iter<I>(iter: I) -> WeightedTable<T>where
I: IntoIterator<Item = (T, u32)>,
fn from_iter<I>(iter: I) -> WeightedTable<T>where
I: IntoIterator<Item = (T, u32)>,
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> UnsafeUnpin for WeightedTable<T>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ConditionalSend for Twhere
T: Send,
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
impl<S, T> Duplex<S> for Twhere
T: FromSample<S> + ToSample<S>,
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.