RouletteWheel

Struct RouletteWheel 

Source
pub struct RouletteWheel<T> { /* private fields */ }
Expand description

A roulette-wheel container

Implementations§

Source§

impl<T> RouletteWheel<T>

Source

pub fn new() -> RouletteWheel<T>

create a new empty random-wheel.

§Example
use roulette_wheel::RouletteWheel;

let rw = RouletteWheel::<u8>::new();
Source

pub fn with_capacity(cap: usize) -> RouletteWheel<T>

Creates an empty RouletteWheel with space for at least n elements.

§Example
use roulette_wheel::RouletteWheel;

let rw = RouletteWheel::<u8>::with_capacity(15);

assert_eq!(rw.len(), 0);
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted. The collection may reserve more space to avoid frequent reallocations.

§Example
use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::<u8>::new();
rw.reserve(20);

assert_eq!(rw.len(), 0);
Source

pub fn len(&self) -> usize

Returns the number of elements in the wheel.

§Example
use roulette_wheel::RouletteWheel;

let rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect();

assert_eq!(rw.len(), 3);
Source

pub fn is_empty(&self) -> bool

Returns true if empty else return false.

§Example
use roulette_wheel::RouletteWheel;

let empty_rw = RouletteWheel::<u8>::new();

assert_eq!(empty_rw.is_empty(), true);

let non_empty_rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect();

assert_eq!(non_empty_rw.is_empty(), false);
Source

pub fn clear(&mut self)

Remove all elements in this wheel.

§Example
use roulette_wheel::RouletteWheel;

let mut rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect();

assert_eq!(rw.len(), 3);

rw.clear();

assert_eq!(rw.len(), 0);
Source

pub fn push(&mut self, fitness: f32, individual: T)

Add an element associated with a probability.

§Panics

This function might panic if the fitness is less than zero or if the total fitness gives a non-finite fitness (Inf).

§Example
use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

rw.push(1.0, 'r');
rw.push(1.0, 'c');
rw.push(1.0, 'a');

assert_eq!(rw.len(), 3);
Source

pub unsafe fn unchecked_push(&mut self, fitness: f32, individual: T)

Add an element associated with a probability. This unsafe function doesn’t check for total fitness overflow nether fitness positivity.

§Example
use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

unsafe { rw.unchecked_push(1.0, 'r') };
unsafe { rw.unchecked_push(1.0, 'c') };
unsafe { rw.unchecked_push(1.0, 'a') };

assert_eq!(rw.len(), 3);
Source

pub fn total_fitness(&self) -> f32

Returns the sum of all individual fitnesses.

§Example
use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

rw.push(3.0, 'r');
rw.push(2.0, 'c');
rw.push(1.5, 'a');

assert_eq!(rw.total_fitness(), 6.5);
Source

pub fn select_iter(&self) -> SelectIter<'_, ThreadRng, T>

Returns an iterator over the RouletteWheel.

§Examples
use roulette_wheel::RouletteWheel;

let rw: RouletteWheel<_> = [(0.1, 10), (0.2, 15), (0.5, 20)].iter().cloned().collect();
let mut iterator = rw.select_iter();

assert_eq!(iterator.next(), Some((0.5, &20)));
assert_eq!(iterator.next(), Some((0.1, &10)));
assert_eq!(iterator.next(), Some((0.2, &15)));
assert_eq!(iterator.next(), None);

Trait Implementations§

Source§

impl<T: Clone> Clone for RouletteWheel<T>

Source§

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

Returns a duplicate 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> FromIterator<(f32, T)> for RouletteWheel<T>

Source§

fn from_iter<A>(iter: A) -> Self
where A: IntoIterator<Item = (f32, T)>,

Creates a value from an iterator. Read more
Source§

impl<T> IntoIterator for RouletteWheel<T>

Source§

type Item = (f32, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoSelectIter<ThreadRng, T>

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

fn into_iter(self) -> IntoSelectIter<ThreadRng, T>

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RouletteWheel<T>

§

impl<T> RefUnwindSafe for RouletteWheel<T>
where T: RefUnwindSafe,

§

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

§

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

§

impl<T> Unpin for RouletteWheel<T>
where T: Unpin,

§

impl<T> UnwindSafe for RouletteWheel<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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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