pub trait CartesianProduct<'a> {
    type Producer: Iterator;
    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());

Associated Types

Required methods

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

Implementations on Foreign Types

Implementors

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.

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.