Crates.io | permutation-generator |
lib.rs | permutation-generator |
version | |
source | src |
created_at | 2021-06-20 08:40:19.926238 |
updated_at | 2024-10-27 09:30:41.374644 |
description | A direct permutation generator |
homepage | https://github.com/thomvil/permutation-generator-rs |
repository | https://github.com/thomvil/permutation-generator-rs |
max_upload_size | |
id | 412349 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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 elementsPermutations 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>>
.
Iterate over the permutations of 4 elements
let mut pg = PermutationGenerator8::new(4).unwrap();
assert_eq!(&[0, 1, 2, 3], pg.next().unwrap().collect::<Vec<_>>().as_slice());
If the specified size of the permutations exceeds the capacity of the used PermutationGenerator
, and Err(PermutationError)
is returned.
let pg = PermutationGenerator8::new(14);
assert_eq!(PermutationGeneratorError::TooManyElements, pg.unwrap_err());
If only a single permutation is needed for a known idx
let mut pg = PermutationGenerator8::new(4).unwrap();
assert_eq!(&[0, 1, 2, 3], pg.next().unwrap().collect::<Vec<_>>().as_slice());
let last_perm_iter = PermutationGenerator8::nth_absolute(4, 4*3*2*1 - 1).unwrap();
assert_eq!(&[3, 2, 1, 0], last_perm_iter.collect::<Vec<_>>().as_slice()));
The number of permutations of size larger than 20, cannot be represented by u128
. Collecting all, or querying the count
, will panic.
let pg = PermutationGenerator32::new(30);
pg.count() // -> panics!