diff --git a/src/adaptors/coalesce.rs b/src/adaptors/coalesce.rs index 3df7cc582..21eaa016d 100644 --- a/src/adaptors/coalesce.rs +++ b/src/adaptors/coalesce.rs @@ -4,26 +4,33 @@ use std::iter::FusedIterator; use crate::size_hint; #[must_use = "iterator adaptors are lazy and do nothing unless consumed"] -pub struct CoalesceBy +pub struct CoalesceBy where I: Iterator, + C: CountItem, { iter: I, - last: Option, + /// `last` is `None` while no item have been taken out of `iter` (at definition). + /// Then `last` will be `Some(Some(item))` until `iter` is exhausted, + /// in which case `last` will be `Some(None)`. + last: Option>, f: F, } -impl Clone for CoalesceBy +impl Clone for CoalesceBy where I: Iterator, + C: CountItem, + C::CItem: Clone, { clone_fields!(last, iter, f); } -impl fmt::Debug for CoalesceBy +impl fmt::Debug for CoalesceBy where I: Iterator + fmt::Debug, - T: fmt::Debug, + C: CountItem, + C::CItem: fmt::Debug, { debug_fmt_fields!(CoalesceBy, iter); } @@ -32,34 +39,42 @@ pub trait CoalescePredicate { fn coalesce_pair(&mut self, t: T, item: Item) -> Result; } -impl Iterator for CoalesceBy +impl Iterator for CoalesceBy where I: Iterator, - F: CoalescePredicate, + F: CoalescePredicate, + C: CountItem, { - type Item = T; + type Item = C::CItem; fn next(&mut self) -> Option { + let Self { iter, last, f } = self; // this fuses the iterator - let last = self.last.take()?; + let init = match last { + Some(elt) => elt.take(), + None => { + *last = Some(None); + iter.next().map(C::new) + } + }?; - let self_last = &mut self.last; - let self_f = &mut self.f; Some( - self.iter - .try_fold(last, |last, next| match self_f.coalesce_pair(last, next) { - Ok(joined) => Ok(joined), - Err((last_, next_)) => { - *self_last = Some(next_); - Err(last_) - } - }) - .unwrap_or_else(|x| x), + iter.try_fold(init, |accum, next| match f.coalesce_pair(accum, next) { + Ok(joined) => Ok(joined), + Err((last_, next_)) => { + *last = Some(Some(next_)); + Err(last_) + } + }) + .unwrap_or_else(|x| x), ) } fn size_hint(&self) -> (usize, Option) { - let (low, hi) = size_hint::add_scalar(self.iter.size_hint(), self.last.is_some() as usize); + let (low, hi) = size_hint::add_scalar( + self.iter.size_hint(), + matches!(self.last, Some(Some(_))) as usize, + ); ((low > 0) as usize, hi) } @@ -67,9 +82,13 @@ where where FnAcc: FnMut(Acc, Self::Item) -> Acc, { - if let Some(last) = self.last { - let mut f = self.f; - let (last, acc) = self.iter.fold((last, acc), |(last, acc), elt| { + let Self { + mut iter, + last, + mut f, + } = self; + if let Some(last) = last.unwrap_or_else(|| iter.next().map(C::new)) { + let (last, acc) = iter.fold((last, acc), |(last, acc), elt| { match f.coalesce_pair(last, elt) { Ok(joined) => (joined, acc), Err((last_, next_)) => (next_, fn_acc(acc, last_)), @@ -82,12 +101,43 @@ where } } -impl, T> FusedIterator for CoalesceBy {} +impl FusedIterator for CoalesceBy +where + I: Iterator, + F: CoalescePredicate, + C: CountItem, +{ +} + +pub struct NoCount; + +pub struct WithCount; + +pub trait CountItem { + type CItem; + fn new(t: T) -> Self::CItem; +} + +impl CountItem for NoCount { + type CItem = T; + #[inline(always)] + fn new(t: T) -> T { + t + } +} + +impl CountItem for WithCount { + type CItem = (usize, T); + #[inline(always)] + fn new(t: T) -> (usize, T) { + (1, t) + } +} /// An iterator adaptor that may join together adjacent elements. /// /// See [`.coalesce()`](crate::Itertools::coalesce) for more information. -pub type Coalesce = CoalesceBy::Item>; +pub type Coalesce = CoalesceBy; impl CoalescePredicate for F where @@ -99,12 +149,12 @@ where } /// Create a new `Coalesce`. -pub fn coalesce(mut iter: I, f: F) -> Coalesce +pub fn coalesce(iter: I, f: F) -> Coalesce where I: Iterator, { Coalesce { - last: iter.next(), + last: None, iter, f, } @@ -113,7 +163,7 @@ where /// An iterator adaptor that removes repeated duplicates, determining equality using a comparison function. /// /// See [`.dedup_by()`](crate::Itertools::dedup_by) or [`.dedup()`](crate::Itertools::dedup) for more information. -pub type DedupBy = CoalesceBy, ::Item>; +pub type DedupBy = CoalesceBy, NoCount>; #[derive(Clone)] pub struct DedupPred2CoalescePred(DP); @@ -156,12 +206,12 @@ impl bool> DedupPredicate for F { } /// Create a new `DedupBy`. -pub fn dedup_by(mut iter: I, dedup_pred: Pred) -> DedupBy +pub fn dedup_by(iter: I, dedup_pred: Pred) -> DedupBy where I: Iterator, { DedupBy { - last: iter.next(), + last: None, iter, f: DedupPred2CoalescePred(dedup_pred), } @@ -186,7 +236,7 @@ where /// See [`.dedup_by_with_count()`](crate::Itertools::dedup_by_with_count) or /// [`.dedup_with_count()`](crate::Itertools::dedup_with_count) for more information. pub type DedupByWithCount = - CoalesceBy, (usize, ::Item)>; + CoalesceBy, WithCount>; #[derive(Clone, Debug)] pub struct DedupPredWithCount2CoalescePred(DP); @@ -215,12 +265,12 @@ where pub type DedupWithCount = DedupByWithCount; /// Create a new `DedupByWithCount`. -pub fn dedup_by_with_count(mut iter: I, dedup_pred: Pred) -> DedupByWithCount +pub fn dedup_by_with_count(iter: I, dedup_pred: Pred) -> DedupByWithCount where I: Iterator, { DedupByWithCount { - last: iter.next().map(|v| (1, v)), + last: None, iter, f: DedupPredWithCount2CoalescePred(dedup_pred), }