Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 1 | use std::iter::Fuse; |
Joel Galenson | 6f79871 | 2021-04-01 17:03:06 -0700 | [diff] [blame] | 2 | use alloc::collections::VecDeque; |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 3 | use crate::size_hint; |
| 4 | use crate::PeekingNext; |
| 5 | |
Joel Galenson | b593e25 | 2021-06-21 13:15:57 -0700 | [diff] [blame] | 6 | /// See [`multipeek()`] for more information. |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 7 | #[derive(Clone, Debug)] |
| 8 | pub struct MultiPeek<I> |
| 9 | where I: Iterator |
| 10 | { |
| 11 | iter: Fuse<I>, |
| 12 | buf: VecDeque<I::Item>, |
| 13 | index: usize, |
| 14 | } |
| 15 | |
| 16 | /// An iterator adaptor that allows the user to peek at multiple `.next()` |
| 17 | /// values without advancing the base iterator. |
| 18 | pub fn multipeek<I>(iterable: I) -> MultiPeek<I::IntoIter> |
| 19 | where I: IntoIterator |
| 20 | { |
| 21 | MultiPeek { |
| 22 | iter: iterable.into_iter().fuse(), |
| 23 | buf: VecDeque::new(), |
| 24 | index: 0, |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl<I> MultiPeek<I> |
| 29 | where I: Iterator |
| 30 | { |
| 31 | /// Reset the peeking “cursor” |
| 32 | pub fn reset_peek(&mut self) { |
| 33 | self.index = 0; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<I: Iterator> MultiPeek<I> { |
| 38 | /// Works exactly like `.next()` with the only difference that it doesn't |
| 39 | /// advance itself. `.peek()` can be called multiple times, to peek |
| 40 | /// further ahead. |
David LeGare | b72e905 | 2022-03-02 16:21:08 +0000 | [diff] [blame] | 41 | /// When `.next()` is called, reset the peeking “cursor”. |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 42 | pub fn peek(&mut self) -> Option<&I::Item> { |
| 43 | let ret = if self.index < self.buf.len() { |
| 44 | Some(&self.buf[self.index]) |
| 45 | } else { |
| 46 | match self.iter.next() { |
| 47 | Some(x) => { |
| 48 | self.buf.push_back(x); |
| 49 | Some(&self.buf[self.index]) |
| 50 | } |
| 51 | None => return None, |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | self.index += 1; |
| 56 | ret |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | impl<I> PeekingNext for MultiPeek<I> |
| 61 | where I: Iterator, |
| 62 | { |
| 63 | fn peeking_next<F>(&mut self, accept: F) -> Option<Self::Item> |
| 64 | where F: FnOnce(&Self::Item) -> bool |
| 65 | { |
| 66 | if self.buf.is_empty() { |
| 67 | if let Some(r) = self.peek() { |
| 68 | if !accept(r) { return None } |
| 69 | } |
| 70 | } else { |
| 71 | if let Some(r) = self.buf.get(0) { |
| 72 | if !accept(r) { return None } |
| 73 | } |
| 74 | } |
| 75 | self.next() |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl<I> Iterator for MultiPeek<I> |
| 80 | where I: Iterator |
| 81 | { |
| 82 | type Item = I::Item; |
| 83 | |
Joel Galenson | 6f79871 | 2021-04-01 17:03:06 -0700 | [diff] [blame] | 84 | fn next(&mut self) -> Option<Self::Item> { |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 85 | self.index = 0; |
Joel Galenson | 6f79871 | 2021-04-01 17:03:06 -0700 | [diff] [blame] | 86 | self.buf.pop_front().or_else(|| self.iter.next()) |
Jakub Kotur | a425e55 | 2020-12-21 17:28:15 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 90 | size_hint::add_scalar(self.iter.size_hint(), self.buf.len()) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Same size |
| 95 | impl<I> ExactSizeIterator for MultiPeek<I> |
| 96 | where I: ExactSizeIterator |
| 97 | {} |
| 98 | |
| 99 | |