[][src]Struct permutator::KPermutationRefIter

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

k-Permutation over data of length "n" where k must be less than n and store result into mutable pointer. 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 *mut [&T]>> parameter to new method of KPermutationRefIter.

Safety

This object use raw mutable pointer provided from user and keep using it in each next iteration. Therefore, all raw pointer conditions are applied up until this object is dropped.

Rationale

It uses unsafe to take a mutable pointer to store the result to avoid the cost of using Rc<RefCell<>>. In uncontroll test environment, this struct perform a complete iteration over 8,648,640 permutations in about 66ms where KPermutationCellIter took about 125 ms. This function is very much alike unsafe_k_permutation function but took Iterator approach.

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.

See

Methods

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

Important traits for KPermutationRefIter<'a, T>
pub unsafe fn new(
    data: &'a [T],
    k: usize,
    result: *mut [&'a T]
) -> KPermutationRefIter<'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 KPermutationRefIter<'a, T>[src]

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

type Item = ()

The type of the elements being iterated over.

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

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for KPermutationRefIter<'a, T> where
    T: RefUnwindSafe

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

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

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

impl<'a, T> !UnwindSafe for KPermutationRefIter<'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.