diff --git a/src/combinations.rs b/src/combinations.rs index c50e95d01..a5b34fc95 100644 --- a/src/combinations.rs +++ b/src/combinations.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::iter::{Fuse, FusedIterator}; +use std::iter::FusedIterator; use super::lazy_buffer::LazyBuffer; use alloc::vec::Vec; @@ -52,9 +52,9 @@ impl Combinations { #[inline] pub fn n(&self) -> usize { self.pool.len() } - /// Returns a reference to the source iterator. + /// Returns a reference to the source pool. #[inline] - pub(crate) fn src(&self) -> &Fuse { &self.pool.it } + pub(crate) fn src(&self) -> &LazyBuffer { &self.pool } /// Resets this `Combinations` back to an initial state for combinations of length /// `k` over the same pool data source. If `k` is larger than the current length @@ -130,8 +130,7 @@ impl Iterator for Combinations fn count(self) -> usize { let Self { indices, pool, first } = self; - // TODO: make `pool.it` private - let n = pool.len() + pool.it.count(); + let n = pool.count(); remaining_for(n, first, &indices).unwrap() } } diff --git a/src/lazy_buffer.rs b/src/lazy_buffer.rs index 88ee06c7c..80c171896 100644 --- a/src/lazy_buffer.rs +++ b/src/lazy_buffer.rs @@ -6,7 +6,7 @@ use crate::size_hint::{self, SizeHint}; #[derive(Debug, Clone)] pub struct LazyBuffer { - pub it: Fuse, + it: Fuse, buffer: Vec, } @@ -29,6 +29,10 @@ where size_hint::add_scalar(self.it.size_hint(), self.len()) } + pub fn count(self) -> usize { + self.len() + self.it.count() + } + pub fn get_next(&mut self) -> bool { if let Some(x) = self.it.next() { self.buffer.push(x); diff --git a/src/permutations.rs b/src/permutations.rs index d03b85262..a8885a411 100644 --- a/src/permutations.rs +++ b/src/permutations.rs @@ -134,14 +134,14 @@ where let Permutations { vals, state } = self; match state { PermutationState::StartUnknownLen { k } => { - let n = vals.len() + vals.it.count(); + let n = vals.count(); let complete_state = CompleteState::Start { n, k }; from_complete(complete_state) } PermutationState::OngoingUnknownLen { k, min_n } => { let prev_iteration_count = min_n - k + 1; - let n = vals.len() + vals.it.count(); + let n = vals.count(); let complete_state = CompleteState::Start { n, k }; from_complete(complete_state) - prev_iteration_count diff --git a/src/powerset.rs b/src/powerset.rs index 4d7685b12..4175f013b 100644 --- a/src/powerset.rs +++ b/src/powerset.rs @@ -68,7 +68,7 @@ impl Iterator for Powerset fn size_hint(&self) -> (usize, Option) { // Total bounds for source iterator. - let src_total = size_hint::add_scalar(self.combs.src().size_hint(), self.combs.n()); + let src_total = self.combs.src().size_hint(); // Total bounds for self ( length(powerset(set) == 2 ^ length(set) ) let self_total = size_hint::pow_scalar_base(2, src_total);