[][src]Struct permutator::CartesianProductRefIter

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

An unsafe way to generate a cartesian product between given domains into *mut [&T] in an iterator style. The struct implement Iterator trait so it can be used as Iterator. The struct provide into_iter() function that return itself.

Unsafe

It took mutable pointer to slice in object instantiation and convert it upon creation into mutable borrowed slice. All unsafe Rust conditions, therefore, applied to entire usage of this struct.

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 12,960 products in about 110ms where CartesianProductCellIter took about 130ms. This function is very much alike unsafe_cartesian_product function but took Iterator approach.

Example

  • Iterator style usage
   use permutator::CartesianProductRefIter;
   use std::time::Instant;
   let data : Vec<&[usize]> = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
   let mut result : Vec<&usize> = vec![&data[0][0]; data.len()];
   unsafe {
        let cart = CartesianProductRefIter::new(&data, result.as_mut_slice() as *mut [&usize]);
        let mut counter = 0;
        let timer = Instant::now();

        for _ in cart {
            println!("{:?}", result);
        counter += 1;
        }
 
        // or functional style like the line below
        // cart.into_iter().for_each(|_| {/* do something iterative style */});

        assert_eq!(data.iter().fold(1, |cum, domain| {cum * domain.len()}), counter);
        println!("Total {} products done in {:?}", counter, timer.elapsed());
   }

Methods

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

Important traits for CartesianProductRefIter<'a, T>
pub unsafe fn new(
    data: &'a [&'a [T]],
    result: *mut [&'a T]
) -> CartesianProductRefIter<'a, T>
[src]

Create an iterator with mutable pointer to store the product. It is unsafe because it convert mutable pointer into mutable borrowed value upon creating the object.

Parameter

  • data a borrowed slice contains borrowed slices. It's domains of cartesian product operation.
  • result a mutable pointer pointed to the slice that will be used to store the cartesian product result. The length of the slice shall equals to len of data.

Return

Each iteration return empty Option and store actual result into result given when construct this Iterator.

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

Trait Implementations

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

impl<'a, T> Iterator for CartesianProductRefIter<'a, T> where
    T: 'a, 
[src]

type Item = ()

The type of the elements being iterated over.

fn next(&mut self) -> Option<()>[src]

Mimic iterator next function but return value into Rc<RefCell<>> that contains mutable slice. It also return an empty Option to tell caller to distinguish if it's put new value or the iterator itself is exhausted.

Paramerter

  • result An Rc<RefCell<>> contains a mutable slice with length equals to number of domains given in CartesianProductIterator::new(). The value inside result will be updated everytime this function is called until the function return None. The performance using this function is on part with cartesian_cell function on uncontrol test environment.

Return

New cartesian product between each domains inside result parameter and also return Some(()) if result is updated or None when there's no new result.

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

Auto Trait Implementations

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

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

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

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

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