RandomWheel

Struct RandomWheel 

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

a little implementation of a random-wheel.

Implementations§

Source§

impl<T> RandomWheel<T>

Source

pub fn from_vec(vector: Vec<T>) -> RandomWheel<T>

create a new random-wheel from vector.

§Example
use random_wheel::RandomWheel;

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

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

pub fn new() -> RandomWheel<T>

create a new empty random-wheel.

§Example
use random_wheel::RandomWheel;

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

pub fn with_capacity(n: usize) -> RandomWheel<T>

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

§Example
use random_wheel::RandomWheel;

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

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

pub 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 random_wheel::RandomWheel;

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

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

pub fn capacity(&self) -> usize

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

§Example
use random_wheel::RandomWheel;

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

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

pub fn len(&self) -> usize

returns the number of elements in the wheel.

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::new();

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

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

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

pub fn clear(&mut self)

remove all elements in this wheel.

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::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);
Source

pub fn is_empty(&self) -> bool

returns true if this wheel is empty else return false.

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::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);
Source

pub fn iter(&self) -> Iter<'_, (f32, T)>

Returns an iterator over the slice.

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::new();

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

let mut iter = rw.iter();

assert_eq!(iter.next(), Some(&(1.0, 'r')));
assert_eq!(iter.next(), Some(&(1.0, 'c')));
assert_eq!(iter.next(), Some(&(1.0, 'a')));
assert_eq!(iter.next(), None);
Source

pub fn iter_mut(&mut self) -> IterMut<'_, (f32, T)>

Returns an iterator that allows modifying each value.

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::new();

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

for a in &mut rw.iter_mut() {
    a.1 = 'm';
}

assert_eq!(rw.peek(), Some((1., &'m')));
Source

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

add an element associated with a probability.

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::new();

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

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

pub fn compute_proba_sum(&mut self)

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

Source

pub fn proba_sum(&self) -> f32

returns total of luck you pushed.

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::new();

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

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

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

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

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::new();

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

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

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

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

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::new();

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

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

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

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

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

§Example
use random_wheel::RandomWheel;

let mut rw = RandomWheel::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§

Source§

impl<T: Clone> Clone for RandomWheel<T>

Source§

fn clone(&self) -> RandomWheel<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> IntoIterator for RandomWheel<T>

Source§

fn into_iter(self) -> IntoIter<(f32, T)>

Creates a consuming iterator, that is, one that moves each value out of the randomWheel (from start to end).

Source§

type Item = (f32, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<(f32, T)>

Which kind of iterator are we turning this into?

Auto Trait Implementations§

§

impl<T> Freeze for RandomWheel<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for RandomWheel<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.