Struct random_wheel::RandomWheel
[−]
[src]
pub struct RandomWheel<T> {
// some fields omitted
}a little implementation of a random-wheel.
Methods
impl<T> RandomWheel<T>[src]
fn new() -> RandomWheel<T>
create a new empty random-wheel.
Example
use random_wheel::RandomWheel; let rw: RandomWheel<i32> = RandomWheel::new();
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<i32> = RandomWheel::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 random_wheel::RandomWheel; let mut rw: RandomWheel<i32> = RandomWheel::new(); rw.reserve(20); assert_eq!(rw.len(), 0);
fn capacity(&self) -> usize
Returns the number of elements the RandomWheel can hold without reallocating.
Example
use random_wheel::RandomWheel; let rw: RandomWheel<i32> = RandomWheel::new(); println!("actual capacity: {}", rw.capacity());
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., 20); rw.push(1., 5); rw.push(1., 1); assert_eq!(rw.len(), 3);
fn clear(&mut self)
remove all elements in this wheel.
Example
use random_wheel::RandomWheel; let mut rw = RandomWheel::new(); rw.push(1., 20); rw.push(1., 5); rw.push(1., 1); 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 random_wheel::RandomWheel; let mut rw = RandomWheel::new(); assert_eq!(rw.is_empty(), true); rw.push(1., 20); rw.push(1., 5); rw.push(1., 1); assert_eq!(rw.is_empty(), false);
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., 20); rw.push(1., 5); rw.push(1., 1); assert_eq!(rw.len(), 3);
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, 20); rw.push(2., 5); rw.push(3., 1); assert_eq!(rw.proba_sum(), 6.5);
fn peek(&self) -> Option<&T>
returns a ref to the randomly peeked element.
Example
use random_wheel::RandomWheel; let mut rw = RandomWheel::new(); rw.push(1., 20); assert_eq!(rw.peek(), Some(&20)); assert_eq!(rw.peek(), Some(&20));
fn pop(&mut self) -> Option<T>
removes a randomly peeked element and return it.
Example
use random_wheel::RandomWheel; let mut rw = RandomWheel::new(); rw.push(1., 20); assert_eq!(rw.peek(), Some(&20)); assert_eq!(rw.pop(), Some(20)); // 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 RandomWheel<T>[src]
fn clone(&self) -> RandomWheel<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 RandomWheel<T>[src]
fn default() -> RandomWheel<T>
Returns the "default value" for a type. Read more
impl<T: PartialEq> PartialEq<RandomWheel<T>> for RandomWheel<T>[src]
fn eq(&self, other: &RandomWheel<T>) -> bool
This method tests for self and other values to be equal, and is used by ==. Read more
fn ne(&self, other: &Rhs) -> bool1.0.0
This method tests for !=.
impl<T: Eq> Eq for RandomWheel<T>[src]
impl<T: PartialOrd> PartialOrd<RandomWheel<T>> for RandomWheel<T>[src]
fn partial_cmp(&self, other: &RandomWheel<T>) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool1.0.0
This method tests less than (for self and other) and is used by the < operator. Read more
fn le(&self, other: &Rhs) -> bool1.0.0
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
fn gt(&self, other: &Rhs) -> bool1.0.0
This method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, other: &Rhs) -> bool1.0.0
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl<T: Ord> Ord for RandomWheel<T>[src]
fn cmp(&self, other: &RandomWheel<T>) -> Ordering
This method returns an Ordering between self and other. Read more
impl<T: Hash> Hash for RandomWheel<T>[src]
fn hash<H: Hasher>(&self, state: &mut H)
Feeds this value into the state given, updating the hasher as necessary.
fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher1.3.0
Feeds a slice of this type into the state provided.