Crate permutator[][src]

This crate provide generic permutators. There's a function that can get a combination at any specific point of lexicographic ordered permutation. There's k-permutation function to generate all possible permutation. There's KPermutation struct that provide Iterator style to do k-Permutation over data. There's HeapPermutation struct that provide Iterator style permutation iterator. There's a combination function to generate all possible combination. There's a GosperCombination struct that provide Iterator style combination iterator.

This crate provides traits that introduce additional functions to [T] and Vec<T>. See trait Permutation and trait Combination for more detail.

The simplest and most efficient usage is call combination for combination, heap_permutation for permutation, and k_permutation function for k-permutation.

Structs

CombinationIterator
GosperCombination

Create a combination iterator. It use Gosper's algorithm to pick a combination out of given data. The produced combination provide no lexicographic order.

HeapPermutation

Heap's permutation in iterator style implementation. It provide two way to iterate over the permutation.

KPermutation

k-Permutation over data of length n where k must be less than n. It'll attempt to permute given data by pick k elements out of data. It use Gosper algorithm to pick the elements. It then use Heap's algorithm to permute those k elements and return each permutation back to caller.

Traits

Combination

Create a combination out of T Normally, it take a [T] or Vec<T> to create a combination.

Permutation

Create a permutation iterator that permute data in place. It return an object of HeapPermutation which can be used as iterator or manual iterate for higher throughput.

Functions

cartesian_product

Create a cartesian product over give slice. The result will be a slice of borrowed T.

combination

Generate a combination out of given domain. It call cb to several times to return each combination.

divide_factorial

Calculate factorial for two factorial division. It'll return 1 if numerator is smaller or equals to denominator. Otherwise, it'll short circuit the calculation by calculate only the undivided remainder.

factorial

Calculate factorial from given value.

get_cartesian_for

Get a cartesian product at specific location. If objects is [1, 2, 3] and degree is 2 then all possible result is [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]

get_cartesian_size

Calculate all possible cartesian combination size. It is always equals to size.pow(degree).

get_permutation_for

Get permutation at specific location. If objects is [1, 2, 3, 4] and degree is 2 then all possible permutation will be [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3].

get_permutation_size

Calculate all possible number of permutation. It's equals to size!/(size - 1).

heap_permutation

Heap permutation which permutate variable d in place and call cb function for each permutation done on d.

k_permutation

Generate k-permutation over slice of d. For example: d = &[1, 2, 3]; k = 2. The result will be [1, 2], [2, 1], [1, 3], [3, 1], [2, 3], [3, 2]

multiply_factorial

Calculate two factorial multiply on each other. It'll try to reduce calculation time by calculate the common value only once.