CartesianProduct

Trait CartesianProduct 

Source
pub trait CartesianProduct<'a> {
    type Producer: Iterator;

    // Required method
    fn cart_prod(&'a self) -> Self::Producer;
}
Expand description

Create a cartesian product out of T. For example,

  • T can be a slice of slices so the product can be created between all the slices.
  • T can be a pair of slice to slices and Rc<RefCell<>> contains a mutable product from slices.

§Examples

  • Create a cartesian product and return it as new owned value
   use std::time::Instant;
   use permutator::CartesianProduct;
    
   let mut counter = 0;
   let timer = Instant::now();
   let data : &[&[u8]]= &[&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];

   data.cart_prod().for_each(|p| {
       counter += 1;
   });

   assert_eq!(data.iter().fold(1, |cum, domain| {cum * domain.len()}), counter);
   println!("Total {} products done in {:?}", counter, timer.elapsed());
  • Create a cartesian product and return result inside Rc<RefCell<>>
   use std::cell::RefCell;
   use std::rc::Rc;
   use std::time::Instant;
   use permutator::CartesianProduct;
    
   let mut counter = 0;
   let timer = Instant::now();
   let data : &[&[u8]]= &[&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
   let mut result = vec![&data[0][0]; data.len()];
   let shared = Rc::new(RefCell::new(result.as_mut_slice()));

   (data, Rc::clone(&shared)).cart_prod().for_each(|_| {
       counter += 1;
   });

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

Required Associated Types§

Required Methods§

Source

fn cart_prod(&'a self) -> Self::Producer

Create a cartesian product producer which can be used to iterate over each product.

Implementations on Foreign Types§

Source§

impl<'a, T> CartesianProduct<'a> for Vec<&'a [T]>
where T: 'a,

Source§

impl<'a, T> CartesianProduct<'a> for [&'a [T]]
where T: 'a,

Implementors§

Source§

impl<'a, 'b: 'a, T> CartesianProduct<'a> for CartesianProductIntoCellParams<'b, T>
where T: 'b,

Source§

impl<'a, 'b: 'a, T> CartesianProduct<'a> for CartesianProductIntoRefParams<'b, T>
where T: 'b,

An implementation for convenient use of CartesianProductRefIter

§Warning

It hid unsafe object instantiation of CartesianProductRefIter from user but all unsafe conditions are still applied as long as the the life of object itself.

Source§

impl<'a, 'b: 'a, T> CartesianProduct<'a> for SelfCartesianProduct<'b, T>
where T: 'b,

Source§

impl<'a, 'b: 'a, T> CartesianProduct<'a> for SelfCartesianProductIntoCellParams<'b, T>
where T: 'b,

Source§

impl<'a, 'b: 'a, T> CartesianProduct<'a> for SelfCartesianProductIntoRefParams<'b, T>
where T: 'b,

An implementation for convenient use of SelfCartesianProductRefIter

§Warning

It hid unsafe object instantiation of SelfCartesianProductRefIter from user but all unsafe conditions are still applied as long as the life of object itself.