pub struct RandomWheel<T> { /* private fields */ }Expand description
a little implementation of a random-wheel.
Implementations§
Source§impl<T> RandomWheel<T>
impl<T> RandomWheel<T>
Sourcepub fn from_vec(vector: Vec<T>) -> RandomWheel<T>
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);Sourcepub fn new() -> RandomWheel<T>
pub fn new() -> RandomWheel<T>
create a new empty random-wheel.
§Example
use random_wheel::RandomWheel;
let rw: RandomWheel<u8> = RandomWheel::new();Sourcepub fn with_capacity(n: usize) -> RandomWheel<T>
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);Sourcepub fn reserve(&mut self, additional: usize)
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);Sourcepub fn capacity(&self) -> usize
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());Sourcepub fn len(&self) -> usize
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);Sourcepub fn clear(&mut self)
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);Sourcepub fn is_empty(&self) -> bool
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);Sourcepub fn iter(&self) -> Iter<'_, (f32, T)>
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);Sourcepub fn iter_mut(&mut self) -> IterMut<'_, (f32, T)>
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')));Sourcepub fn push(&mut self, proba: f32, data: T)
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);Sourcepub fn compute_proba_sum(&mut self)
pub fn compute_proba_sum(&mut self)
Will recompute the probabilities sum use it when you iterate through this vector and change proba values
Sourcepub fn proba_sum(&self) -> f32
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);Sourcepub fn peek(&self) -> Option<(f32, &T)>
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')));Sourcepub fn peek_mut(&mut self) -> Option<(f32, &mut T)>
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')));Sourcepub fn pop(&mut self) -> Option<(f32, T)>
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>
impl<T: Clone> Clone for RandomWheel<T>
Source§fn clone(&self) -> RandomWheel<T>
fn clone(&self) -> RandomWheel<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more