[][src]Struct permutator::KPermutationIterator

pub struct KPermutationIterator<'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 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.

Examples

  • Iterator style permit using 'for-in' style loop along with enable usage of functional paradigm over iterator object.
   use permutator::KPermutationIterator;
   use std::time::Instant;
   let data = [1, 2, 3, 4, 5];
   let permutator = KPermutationIterator::new(&data, 3);
   let mut counter = 0;
   // println!("Begin testing KPermutation");
   let timer = Instant::now();

   for permuted in permutator {
       // uncomment a line below to print all permutation.
       // println!("{}:{:?}", counter, permuted);
       counter += 1;
   }
 
   // Or simply use functional paradigm of iterator like below
   // permutator.into_iter().any(|item| {item == [7, 8, 9]});

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

Notes

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.

Safety

This struct implementation use unsafe code internally. It use unsafe because it uses Vec<T> to own a combination for each permutation. Rust cannot derive lifetime of mutable slice created inside next and store it into struct object itself. This is because next signature have no lifetime associated with self. To get around this, the implementation convert Vec to *mut [T] then perform &mut * on it.

See

Methods

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

Important traits for KPermutationIterator<'a, T>
pub fn new(data: &[T], k: usize) -> KPermutationIterator<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().

Trait Implementations

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

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

type Item = Vec<&'a T>

The type of the elements being iterated over.

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

Auto Trait Implementations

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

impl<'a, T> Send for KPermutationIterator<'a, T> where
    T: Sync

impl<'a, T> Sync for KPermutationIterator<'a, T> where
    T: Sync

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

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