permutation_generator/lib.rs
1#![feature(impl_trait_in_assoc_type)]
2
3mod error;
4pub(crate) mod factorial;
5mod permutation_generator;
6mod single_permutation;
7
8pub use error::*;
9pub use permutation_generator::*;
10pub use single_permutation::{SinglePermutation8, SinglePermutation16, SinglePermutation32};
11
12// #[derive(Clone)]
13// pub struct PermutationGeneratorWithReferences8<'a, T: 'a + Clone> {
14// pg: PermutationGenerator8,
15// ref_slice: &'a [T],
16// }
17
18// impl<'a, T: 'a + Clone> PermutationGeneratorWithReferences8<'a, T> {
19// pub fn new(ref_slice: &'a [T]) -> PResult<Self> {
20// PermutationGenerator8::new(Self::nb_elems(ref_slice)).map(|pg| Self {
21// pg, ref_slice }) }
22
23// pub fn nb_remaining(&self) -> usize {
24// self.pg.nb_remaining()
25// }
26
27// pub fn nth(&'a mut self, step: u16) -> Option<impl Iterator<Item = &'a T>
28// + 'a> { self.pg .nth(step) .map(move |it| it.map(move |i|
29// self.ref_slice.get(i as
30// usize).unwrap())) }
31
32// pub fn next_permutation(&'a mut self) -> Option<impl Iterator<Item = &'a
33// T> + 'a> { self.nth(0)
34// }
35
36// pub fn nth_absolute(ref_slice: &[T], idx: u16) -> PResult<Option<impl
37// Iterator<Item = &T>>> {
38// PermutationGenerator8::nth_absolute(Self::nb_elems(ref_slice),
39// idx).map(move |pg| { pg.map(move |iter| iter.map(move |i|
40// ref_slice.get(i as usize).unwrap())) })
41// }
42
43// #[inline]
44// fn nb_elems(ref_slice: &[T]) -> u8 {
45// ref_slice.len().try_into().unwrap_or(u8::MAX)
46// }
47// }
48
49// #[cfg(test)]
50// mod tests {
51// use super::*;
52
53// #[test]
54// fn new() {
55// let list = ["foo", "bar", "baz"];
56// let mut pgr =
57// PermutationGeneratorWithReferences8::new(&list).unwrap(); assert_eq!
58// (6, pgr.nb_remaining()); assert_eq!(
59// &["foo", "bar", "baz"],
60// pgr.next_permutation()
61// .unwrap()
62// .cloned()
63// .collect::<Vec<_>>()
64// .as_slice(),
65// );
66// }
67// }