diff options
author | Dennis Kobert <dennis@kobert.dev> | 2020-01-05 22:39:18 +0100 |
---|---|---|
committer | Dennis Kobert <dennis@kobert.dev> | 2020-01-05 22:39:18 +0100 |
commit | d343b5e10ec2dbd526decbfa984168cf2509f48c (patch) | |
tree | 6d62761a15a93d70ba68214cdb4f6f64a2e96071 /src/permutations.rs | |
parent | c583ee4faa552962594f7d5bf9b57bf62b6db5c0 (diff) | |
parent | 492045e538cf806bb49631dfbbaabbd8b566147e (diff) |
Merge branch 'master' of kobert:/var/repos/babel
Diffstat (limited to 'src/permutations.rs')
-rw-r--r-- | src/permutations.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/permutations.rs b/src/permutations.rs new file mode 100644 index 0000000..7b8e4d3 --- /dev/null +++ b/src/permutations.rs @@ -0,0 +1,45 @@ +use permutohedron::{Heap, LexicalPermutation}; + +pub trait PermutationGenerator { + fn permutations(n: u32) -> Vec<Vec<u32>>; +} + +pub struct HeapsPermutations; + +impl PermutationGenerator for HeapsPermutations { + fn permutations(n: u32) -> Vec<Vec<u32>> { + Heap::<'_, Vec<u32>, _>::new(&mut (1..=n).collect()).collect() + } +} + +pub struct LexicalPermutations { + state: Vec<u32>, + is_ended: bool, +} + +impl Iterator for LexicalPermutations { + type Item = Vec<u32>; + fn next(&mut self) -> Option<Self::Item> { + if self.is_ended { + None + } else { + self.is_ended = self.state.next_permutation(); + Some(self.state.clone()) + } + } +} + +impl LexicalPermutations { + fn new(n: u32) -> Self { + Self { + state: (1..=n).collect(), + is_ended: false + } + } +} + +impl PermutationGenerator for LexicalPermutations { + fn permutations(n: u32) -> Vec<Vec<u32>> { + Self::new(n).collect() + } +} |