Struct roulette_wheel::RouletteWheel [] [src]

pub struct RouletteWheel<T> { /* fields omitted */ }

A roulette-wheel container

Methods

impl<T> RouletteWheel<T>
[src]

create a new empty random-wheel.

Example

use roulette_wheel::RouletteWheel;

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

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

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

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

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

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

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

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

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

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

impl<T: Clone> Clone for RouletteWheel<T>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T> FromIterator<(f32, T)> for RouletteWheel<T>
[src]

Creates a value from an iterator. Read more

impl<T> IntoIterator for RouletteWheel<T>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

Creates an iterator from a value. Read more