-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
507bff9
commit 7786a6b
Showing
8 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use super::Peekable; | ||
|
||
/// An iterator adapter that places a separator between all elements. | ||
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "none")] | ||
#[derive(Debug, Clone)] | ||
pub struct Intersperse<I: Iterator> | ||
where | ||
I::Item: Clone, | ||
{ | ||
separator: I::Item, | ||
iter: Peekable<I>, | ||
needs_sep: bool, | ||
} | ||
|
||
impl<I: Iterator> Intersperse<I> | ||
where | ||
I::Item: Clone, | ||
{ | ||
pub(in crate::iter) fn new(iter: I, separator: I::Item) -> Self { | ||
Self { iter: iter.peekable(), separator, needs_sep: false } | ||
} | ||
} | ||
|
||
#[unstable(feature = "iter_intersperse", reason = "recently added", issue = "none")] | ||
impl<I> Iterator for Intersperse<I> | ||
where | ||
I: Iterator, | ||
I::Item: Clone, | ||
{ | ||
type Item = I::Item; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<I::Item> { | ||
if self.needs_sep && self.iter.peek().is_some() { | ||
self.needs_sep = false; | ||
Some(self.separator.clone()) | ||
} else { | ||
self.needs_sep = true; | ||
self.iter.next() | ||
} | ||
} | ||
|
||
fn fold<B, F>(mut self, init: B, mut f: F) -> B | ||
where | ||
Self: Sized, | ||
F: FnMut(B, Self::Item) -> B, | ||
{ | ||
let mut accum = init; | ||
|
||
// Use `peek()` first to avoid calling `next()` on an empty iterator. | ||
if !self.needs_sep || self.iter.peek().is_some() { | ||
if let Some(x) = self.iter.next() { | ||
accum = f(accum, x); | ||
} | ||
} | ||
|
||
let element = &self.separator; | ||
|
||
self.iter.fold(accum, |mut accum, x| { | ||
accum = f(accum, element.clone()); | ||
accum = f(accum, x); | ||
accum | ||
}) | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let (lo, hi) = self.iter.size_hint(); | ||
let next_is_elem = !self.needs_sep; | ||
let lo = lo.saturating_sub(next_is_elem as usize).saturating_add(lo); | ||
let hi = match hi { | ||
Some(hi) => hi.saturating_sub(next_is_elem as usize).checked_add(hi), | ||
None => None, | ||
}; | ||
(lo, hi) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters