Struct roulette_wheel::RouletteWheel [] [src]

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

a little implementation of a random-wheel.

Methods

impl<T> RouletteWheel<T>
[src]

fn from_vec(vector: Vec<T>) -> RouletteWheel<T>

create a new random-wheel from vector.

Example

use roulette_wheel::RouletteWheel;

let numbers: Vec<_> = (0..20).collect();

// default probability is set to 1.0 for each element
let rw: RouletteWheel<u8> = RouletteWheel::from_vec(numbers);

fn new() -> RouletteWheel<T>

create a new empty random-wheel.

Example

use roulette_wheel::RouletteWheel;

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

fn with_capacity(n: usize) -> RouletteWheel<T>

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

Example

use roulette_wheel::RouletteWheel;

let numbers: Vec<_> = (0..20).collect();
let mut rw: RouletteWheel<u8> = RouletteWheel::with_capacity(numbers.len());

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

fn reserve(&mut self, additional: usize)

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

Example

use roulette_wheel::RouletteWheel;

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

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

fn capacity(&self) -> usize

Returns the number of elements the RouletteWheel can hold without reallocating.

Example

use roulette_wheel::RouletteWheel;

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

println!("actual capacity: {}", rw.capacity());

fn len(&self) -> usize

returns the number of elements in the wheel.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

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

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

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

fn clear(&mut self)

remove all elements in this wheel.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

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

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

rw.clear();

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

fn is_empty(&self) -> bool

returns true if this wheel is empty else return false.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

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

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

assert_eq!(rw.is_empty(), false);

fn push(&mut self, proba: f32, data: T)

add an element associated with a probability.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

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

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

fn compute_proba_sum(&mut self)

Will recompute the probabilities sum use it when you iterate through this vector and change proba values

fn proba_sum(&self) -> f32

returns total of luck you pushed.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

rw.push(1.5, 'r');
rw.push(2., 'c');
rw.push(3., 'a');

assert_eq!(rw.proba_sum(), 6.5);

fn peek(&self) -> Option<(f32, &T)>

returns a ref to the randomly peeked element with it's probality to be peeked.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

rw.push(1., 'r');

assert_eq!(rw.peek(), Some((1.0, &'r')));
assert_eq!(rw.peek(), Some((1.0, &'r')));

fn peek_mut(&mut self) -> Option<(f32, &mut T)>

returns a mutable ref to the randomly peeked element with it's probality to be peeked.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

rw.push(1., 'r');

match rw.peek_mut() {
    Some((_, val)) => *val = 'b',
    None => {}
}

assert_eq!(rw.peek(), Some((1.0, &'b')));

fn pop(&mut self) -> Option<(f32, T)>

removes a randomly peeked element and return it with it's probality to be peeked.

Example

use roulette_wheel::RouletteWheel;

let mut rw = RouletteWheel::new();

rw.push(1., 'r');

assert_eq!(rw.pop(), Some((1.0, 'r')));

// once you pop the value, it doesn't exist anymore
assert_eq!(rw.peek(), None);
assert_eq!(rw.pop(), None);

Trait Implementations

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

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

Returns a copy of the value. Read more

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

Performs copy-assignment from source. Read more

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

fn default() -> Self

Returns the "default value" for a type. Read more

impl<'a, T: Clone> From<&'a [T]> for RouletteWheel<T>
[src]

fn from(s: &'a [T]) -> RouletteWheel<T>

Performs the conversion.