[][src]Struct permutator::KPermutationCellIter

pub struct KPermutationCellIter<'a, T> where
    T: 'a, 
{ /* fields omitted */ }

k-Permutation over data of length "n" where k must be less than n. It'll attempt to permute given data by pick k elements out of n data. It use Gosper algorithm to pick the elements. It then use Heap's algorithm to permute those k elements and return each permutation back to caller by given Rc<RefCell<&mut [&T]>> parameter to new method of KPermutationCellIter.

Examples

  • Iterator style permit using 'for-in' style loop along with enable usage of functional paradigm over iterator object.
   use permutator::{KPermutationCellIter, IteratorReset};
   use std::cell::RefCell;
   use std::rc::Rc;
   use std::time::Instant;
   let data = [1, 2, 3, 4, 5];
   let mut result : Vec<&i32> = vec![&data[0]; 3];
   let shared = Rc::new(RefCell::new(result.as_mut_slice()));
   let mut permutator = KPermutationCellIter::new(&data, 3, Rc::clone(&shared));
   let mut counter = 0;
   let timer = Instant::now();

   permutator.for_each(|_| {
       println!("{}:{:?}", counter, &*shared.borrow());
       counter += 1;
   });

   println!("Total {} permutations done in {:?}", counter, timer.elapsed());
   assert_eq!(60, counter);

Notes

This struct manual iteration performance is about 110% slower than using k-permutation function while the slowest using Iterator style is about 2300% slower. The additional functionality provided by this struct is that it can be pause or completely stop midway while the k-permutation need to be run from start to finish only.

Warning

This struct implementation use unsafe code. This is because inside the next function, it require a share mutable variable on both the Gosper iterator and Heap permutator. It also require to re-assign the permutator on first call to next which is impossible in current safe Rust. To do it in safe Rust way, it need to copy the data which will hurt performance.

See

Methods

impl<'a, T> KPermutationCellIter<'a, T>[src]

Important traits for KPermutationCellIter<'a, T>
pub fn new(
    data: &'a [T],
    k: usize,
    result: Rc<RefCell<&'a mut [&'a T]>>
) -> KPermutationCellIter<'a, T>
[src]

pub fn into_iter(self) -> Self[src]

Consume then return self immediately. It permit functional style operation over iterator from borrow object as Rust isn't yet support for _ in borrowed_object directly. It need to be for _ in borrowed_object.into_iter().

pub fn len(&self) -> usize[src]

Get total number of permutation this KPermutationIterator object can permute. It'll be equals to number of possible next call.

Trait Implementations

impl<'a, T> ExactSizeIterator for KPermutationCellIter<'a, T>[src]

impl<'a, T> Iterator for KPermutationCellIter<'a, T>[src]

type Item = ()

The type of the elements being iterated over.

impl<'a, T> IteratorReset for KPermutationCellIter<'a, T>[src]

Auto Trait Implementations

impl<'a, T> !RefUnwindSafe for KPermutationCellIter<'a, T>

impl<'a, T> !Send for KPermutationCellIter<'a, T>

impl<'a, T> !Sync for KPermutationCellIter<'a, T>

impl<'a, T> Unpin for KPermutationCellIter<'a, T>

impl<'a, T> !UnwindSafe for KPermutationCellIter<'a, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.