Struct permutator::GosperCombinationCellIter
source · pub struct GosperCombinationCellIter<'a, T>where
T: 'a,{ /* private fields */ }
Expand description
Deprecated
This iterator family is now deprecated. Consider using LargeCombinationCellIter instead. This is because current implementation need to copy every ref on every iteration which is inefficient. On uncontroll test environment, this iterator take 2.49s to iterate over 30,045,015 combinations. The LargeCombinationCellIter took only 446.39ms.
Create a combination iterator. It use Gosper’s algorithm to pick a combination out of given data. The produced combination provide no lexicographic order.
The returned combination will be a reference into given data. Each combination return from iterator by storing into given Rc<RefCell<&mut [&T]>> along with empty Option.
Examples
Given slice of [1, 2, 3, 4, 5]. It will produce following combinations: [1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4], [1, 2, 5], [1, 3, 5], [2, 3, 5], [1, 4, 5], [2, 4, 5], [3, 4, 5] Here’s an example of code printing above combination.
use permutator::{GosperCombinationCellIter};
use std::cell::RefCell;
use std::rc::Rc;
use std::time::{Instant};
let data = &[1, 2, 3, 4, 5];
let mut result : &mut[&i32] = &mut [&data[0]; 3];
let shared = Rc::new(RefCell::new(result));
let mut gosper = GosperCombinationCellIter::new(&[1, 2, 3, 4, 5], 3, Rc::clone(&shared));
let mut counter = 0;
let timer = Instant::now();
for _ in gosper {
println!("{}:{:?}", counter, shared);
counter += 1;
}
println!("Total {} combinations in {:?}", counter, timer.elapsed());
Limitation
Gosper algorithm need to know the MSB (most significant bit). The current largest known MSB data type is u128. This make the implementation support up to 128 elements slice.
See
Implementations
sourceimpl<'a, T> GosperCombinationCellIter<'a, T>
impl<'a, T> GosperCombinationCellIter<'a, T>
sourcepub fn new(
data: &'a [T],
r: usize,
result: Rc<RefCell<&'a mut [&'a T]>>
) -> GosperCombinationCellIter<'a, T>
pub fn new(
data: &'a [T],
r: usize,
result: Rc<RefCell<&'a mut [&'a T]>>
) -> GosperCombinationCellIter<'a, T>
Create new combination generator using Gosper’s algorithm.
r
shall be smaller than data.len().
Note: It perform no check on given parameter. If r is larger than length of data then iterate over it will not occur. The iteration will be end upon enter.