Struct WeightedTable

Source
pub struct WeightedTable<T>
where T: PartialEq + Clone,
{ /* 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>
where T: PartialEq + Clone,

Source

pub fn new() -> Self

Creates a new empty WeightedTable

Source

pub fn from_vec(vec: Vec<(T, u32)>) -> Self

Creates a new WeightedTable from a vector of tuples of (value, weight)

Source

pub fn insert(&mut self, value: T, weight: u32)

Inserts a new element into the table

Source

pub fn remove(&mut self, value: &T)

Removes an element from the table

Source

pub fn clear(&mut self)

Clears the table

Source

pub fn get_entry(&self, index: usize) -> Option<(T, u32)>

Returns the entry at the specified index

Source

pub fn get_entry_ref(&self, index: usize) -> Option<(&'_ T, &'_ u32)>

Returns the entry at the specified index as a reference

Source

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

Source

pub fn get_weight(&self, value: &T) -> Option<u32>

Returns the weight of the specified value

Source

pub fn get_weight_mut(&mut self, value: &T) -> Option<&mut u32>

Returns the weight of the specified value as a mutable reference

Source

pub fn random_with(&self, n: u32) -> (T, u32)

Returns a random element with the specified weight

Source

pub fn iter(&self) -> impl Iterator<Item = (&'_ T, &'_ u32)>

Returns an iterator over the table

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&'_ mut T, &'_ mut u32)>

Returns an iterator over the table mutably

Source

pub fn combine(&mut self, other: Self)

Combines two tables (in place)

Trait Implementations§

Source§

impl<T> Clone for WeightedTable<T>
where T: PartialEq + Clone + Clone,

Source§

fn clone(&self) -> WeightedTable<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for WeightedTable<T>
where T: PartialEq + Clone + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for WeightedTable<T>
where T: PartialEq + Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T> FromIterator<(T, &'a u32)> for WeightedTable<T>
where T: PartialEq + Clone,

Source§

fn from_iter<I: IntoIterator<Item = (T, &'a u32)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> FromIterator<(T, u32)> for WeightedTable<T>
where T: PartialEq + Clone,

Source§

fn from_iter<I: IntoIterator<Item = (T, u32)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> IntoIterator for WeightedTable<T>
where T: PartialEq + Clone,

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = WeightedTableTupleIntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T> RandomWeightedContainer<T> for WeightedTable<T>
where T: Clone + PartialEq,

Source§

fn max_weight(&self) -> u32

Returns the maximum weight of all elements in the container
Source§

fn weights(&self) -> &Vec<u32>

Returns the weights of all elements in the container as a vector
Source§

fn values(&self) -> &Vec<T>

Returns the values of all elements in the container as a vector
Source§

fn random_weight(&self) -> Option<u32>

Returns a random weight below the maximum weight or None if the container is empty
Source§

fn weighted_random_with_weight(&self, weight: u32) -> Option<Self::Item>

Returns a random element mapping to the specified weight
Source§

fn weighted_random(&self) -> Option<Self::Item>

Returns a random element based on the weights

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

Source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using data from the given World.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> RandomContainer<T> for U
where U: Clone + IntoIterator<Item = T>, T: Clone,

Source§

fn random(&self) -> Option<Self::Item>

Returns a random element
Source§

fn random_with_index(&self) -> Option<(usize, Self::Item)>

Returns a random element and its index
Source§

fn random_index(&self) -> Option<usize>

Returns a random index
Source§

fn random_element(&self) -> Option<Self::Item>

Returns a random element
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ConditionalSend for T
where T: Send,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> Settings for T
where T: 'static + Send + Sync,

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,