Function permutator::combination

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

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