pub fn combination<T>(domain: &[T], r: usize, cb: impl FnMut(&[&T]))
Expand description

Deprecated

This combination family is now deprecated. Consider using large_combination function 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.41s to iterate over 30,045,015 combinations. The large_combination function took only 208.84ms. Beside speed, it also theoritically support up to 2^32 elements. If no more efficient implementation is available for some certain time period, this function will be officially mark with #[deprecated].

Generate a combination out of given domain. It call cb to several times to return each combination. It’s similar to struct GosperCombination but slightly faster in uncontrol test environment.

Parameters

  • domain is a slice containing the source data, AKA ‘domain’
  • r is a size of each combination, AKA ‘range’ size
  • cb is a callback function that will get call several times. Each call will have a slice of combination pass as callback parameter.

Returns

The function will return combination via callback function. It will keep calling until no further combination can be found then it return control to called.

Example

use permutator::combination;
combination(&[1, 2, 3, 4, 5], 3, |c| {
    // called multiple times.
    // Each call have [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],
    // and [3, 4, 5] respectively.
    println!("{:?}", c);
});

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