diff --git a/src/lib.rs b/src/lib.rs index 3b7d18b..aeb7898 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,32 +32,6 @@ pub struct ChoosePermute<'a, T, const SIZE: usize> { permute: Option>, } -impl<'a, T: Clone, const SIZE: usize> Iterator for ChoosePermute<'a, T, SIZE> { - type Item = [T; SIZE]; - - fn next(&mut self) -> Option { - loop { - if let Some(permute) = self.permute.as_mut() { - if let Some(item) = permute.next() { - return Some(item); - } - - self.permute.take(); - } - - let chosen = self.choose.next()?; - - self.permute = Some(permute(chosen)); - } - } - - fn size_hint(&self) -> (usize, Option) { - let size = self.choose.size_hint().0 * factorial(SIZE); - - (size, Some(size)) - } -} - pub struct Choose<'a, T, const SIZE: usize> { source: &'a [T], count: [usize; SIZE], @@ -102,30 +76,6 @@ impl<'a, T: Clone, const SIZE: usize> Choose<'a, T, SIZE> { }) } } - -impl<'a, T: Clone, const SIZE: usize> Iterator for Choose<'a, T, SIZE> { - type Item = [T; SIZE]; - - fn next(&mut self) -> Option { - if self.closed { - None - } else { - self.increment(); - - Some(self.choose()) - } - } - - fn size_hint(&self) -> (usize, Option) { - let numerator = factorial(self.source.len()); - let denominator = factorial(SIZE) * factorial(self.source.len() - SIZE); - - let size = numerator / denominator; - - (size, Some(size)) - } -} - impl Permute { fn increment(&mut self) { let mut any_updated = false; @@ -164,6 +114,55 @@ fn factorial(num: usize) -> usize { (1..=num).fold(1, |acc, item| acc * item) } +impl<'a, T: Clone, const SIZE: usize> Iterator for ChoosePermute<'a, T, SIZE> { + type Item = [T; SIZE]; + + fn next(&mut self) -> Option { + loop { + if let Some(permute) = self.permute.as_mut() { + if let Some(item) = permute.next() { + return Some(item); + } + + self.permute.take(); + } + + let chosen = self.choose.next()?; + + self.permute = Some(permute(chosen)); + } + } + + fn size_hint(&self) -> (usize, Option) { + let size = self.choose.size_hint().0 * factorial(SIZE); + + (size, Some(size)) + } +} + +impl<'a, T: Clone, const SIZE: usize> Iterator for Choose<'a, T, SIZE> { + type Item = [T; SIZE]; + + fn next(&mut self) -> Option { + if self.closed { + None + } else { + self.increment(); + + Some(self.choose()) + } + } + + fn size_hint(&self) -> (usize, Option) { + let numerator = factorial(self.source.len()); + let denominator = factorial(SIZE) * factorial(self.source.len() - SIZE); + + let size = numerator / denominator; + + (size, Some(size)) + } +} + impl Iterator for Permute { type Item = [T; SIZE];