pub fn cartesian_product_cell<'a, T>(
    sets: &'a [&[T]],
    result: Rc<RefCell<&'a mut [&'a T]>>,
    cb: impl FnMut()
)
Expand description

Similar to safe cartesian_product function except the way it return the product. It return result through Rc<RefCell<>> to mutable slice of result. It’ll notify caller on each new result via empty callback function.

Parameters

  • sets A raw sets of data to get a cartesian product.
  • result An Rc<RefCell<>> contains mutable slice of length equals to sets.len()
  • cb A callback function which will be called after new product in result is set.

Return

This function return result through function’s parameter result and notify caller that new result is available through cb callback function.

Rationale

The safe cartesian product function return value in callback parameter. It limit the lifetime of return combination to be valid only inside it callback. To use it outside callback scope, it need to copy the value which will have performance penalty. Therefore, jeopardize it own goal of being fast. This function provide alternative safe way to share result which is roughly 50% slower to unsafe counterpart. The performance is on par with using CartesianProduct iterator.

Example

The scenario is we want to get cartesian product from single source of data then distribute the product to two workers which read each combination then do something about it, which in this example, simply print it.

   use permutator::cartesian_product_cell;
   use std::fmt::Debug;
   use std::rc::Rc;
   use std::cell::RefCell;
 
   // All shared data consumer will get call throught this trait
   trait Consumer {
       fn consume(&self); // need to be ref due to rule of only ref mut is permit at a time
   }
 
   struct Worker1<'a, T : 'a> {
       data : Rc<RefCell<&'a mut[&'a T]>> // Store ref to cartesian product.
   }
 
   impl<'a, T : 'a + Debug> Consumer for Worker1<'a, T> {
       fn consume(&self) {
           // read new share cartesian product and do something about it, in this case simply print it.
           println!("Work1 has {:?}", self.data.borrow());
       }
   }

   struct Worker2<'a, T : 'a> {
       data : Rc<RefCell<&'a mut[&'a T]>> // Store ref to cartesian product.
   }
 
   impl<'a, T : 'a + Debug> Consumer for Worker2<'a, T> {
       fn consume(&self) {
           // read new share cartesian product and do something about it, in this case simply print it.
           println!("Work2 has {:?}", self.data.borrow());
       }
   }

   fn start_cartesian_product_process<'a>(data : &'a[&'a[i32]], cur_result : Rc<RefCell<&'a mut [&'a i32]>>, consumers : Vec<Box<Consumer + 'a>>) {
       cartesian_product_cell(data, cur_result, || {
           consumers.iter().for_each(|c| {
               c.consume();
           })
       });
   }
 
   let data : &[&[i32]] = &[&[1, 2], &[3, 4, 5], &[6]];
   let mut result = vec![&data[0][0]; data.len()];

   let shared = Rc::new(RefCell::new(result.as_mut_slice()));
   let worker1 = Worker1 {
       data : Rc::clone(&shared)
   };
   let worker2 = Worker2 {
       data : Rc::clone(&shared)
   };
   let consumers : Vec<Box<Consumer>> = vec![Box::new(worker1), Box::new(worker2)];
   start_cartesian_product_process(data, shared, consumers);

See