Permutation Generator
Generates the basic permutations of n
elements and length n
in a direct fashion. It works index-based, not by iterating over previous permutations.
Currently only available on nightly
, because it relies on #![feature(min_type_alias_impl_trait)]
.
Optimized versions:
PermutationGenerator8
: for basic permutations upto 8 elementsPermutationGenerator16
: for basic permutations upto 16 elementsPermutationGenerator32
: for basic permutations upto 32 elements
Permutations of more than 32 elements are not provided, since the index of permutation cannot be represented by a single u128
.
PermutationGenerator
s implement Iterator<Item = impl Iterator<Item = u8>>
.
Usage
Iterate over the permutations of 4 elements
let mut pg = new.unwrap;
assert_eq!;
If the specified size of the permutations exceeds the capacity of the used PermutationGenerator
, and Err(PermutationError)
is returned.
let pg = new;
assert_eq!;
If only a single permutation is needed for a known idx
let mut pg = new.unwrap;
assert_eq!;
let last_perm_iter = nth_absolute.unwrap;
assert_eq! );
Panics
The number of permutations of size larger than 20, cannot be represented by u128
. Collecting all, or querying the count
, will panic.
let pg = new;
pg.count // -> panics!