-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
442248d
commit 6f19676
Showing
7 changed files
with
222 additions
and
3 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,102 @@ | ||
use crate::iter::{ExactSizeIterator, Fuse, FusedIterator, Iterator, TrustedLen}; | ||
|
||
use crate::array; | ||
|
||
/// An iterator over all contiguous windows of length `N`. The windows overlap. | ||
/// If the iterator is shorter than `N`, the iterator returns no values. | ||
/// | ||
/// This `struct` is created by [`Iterator::array_windows`]. See its | ||
/// documentation for more. | ||
#[derive(Debug, Clone)] | ||
#[must_use = "iterators are lazy and do nothing unless consumed"] | ||
#[unstable(feature = "iter_array_windows", reason = "recently added", issue = "none")] | ||
pub struct ArrayWindows<I, const N: usize> | ||
where | ||
I: Iterator, | ||
I::Item: Clone, | ||
{ | ||
iter: Fuse<I>, | ||
last: Option<[I::Item; N]>, | ||
} | ||
|
||
impl<I, const N: usize> ArrayWindows<I, N> | ||
where | ||
I: Iterator, | ||
I::Item: Clone, | ||
{ | ||
#[inline] | ||
pub(in crate::iter) fn new(iter: I) -> Self { | ||
assert!(N != 0); | ||
Self { iter: iter.fuse(), last: None } | ||
} | ||
} | ||
|
||
#[unstable(feature = "iter_array_windows", reason = "recently added", issue = "none")] | ||
impl<I: Iterator, const N: usize> Iterator for ArrayWindows<I, N> | ||
where | ||
I: Iterator, | ||
I::Item: Clone, | ||
{ | ||
type Item = [I::Item; N]; | ||
|
||
#[inline] | ||
fn next(&mut self) -> Option<Self::Item> { | ||
let Self { iter, last } = self; | ||
|
||
match last { | ||
Some(last) => { | ||
let item = iter.next()?; | ||
last.rotate_left(1); | ||
if let Some(end) = last.last_mut() { | ||
*end = item; | ||
} | ||
Some(last.clone()) | ||
} | ||
None => { | ||
let tmp = array::try_from_fn(|_| iter.next())?; | ||
*last = Some(tmp.clone()); | ||
Some(tmp) | ||
} | ||
} | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let (lower, upper) = self.iter.size_hint(); | ||
// Keep infinite iterator size hint lower bound as `usize::MAX` | ||
if lower == usize::MAX { | ||
(lower, upper) | ||
} else { | ||
(lower.saturating_sub(N - 1), upper.map(|n| n.saturating_sub(N - 1))) | ||
} | ||
} | ||
|
||
#[inline] | ||
fn count(self) -> usize { | ||
self.iter.count().saturating_sub(N - 1) | ||
} | ||
} | ||
|
||
#[unstable(feature = "iter_array_windows", reason = "recently added", issue = "none")] | ||
impl<I, const N: usize> FusedIterator for ArrayWindows<I, N> | ||
where | ||
I: FusedIterator, | ||
I::Item: Clone, | ||
{ | ||
} | ||
|
||
#[unstable(feature = "iter_array_windows", reason = "recently added", issue = "none")] | ||
impl<I, const N: usize> ExactSizeIterator for ArrayWindows<I, N> | ||
where | ||
I: ExactSizeIterator, | ||
I::Item: Clone, | ||
{ | ||
} | ||
|
||
#[unstable(feature = "trusted_len", issue = "37572")] | ||
unsafe impl<I, const N: usize> TrustedLen for ArrayWindows<I, N> | ||
where | ||
I: TrustedLen, | ||
I::Item: Clone, | ||
{ | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use core::iter; | ||
|
||
#[test] | ||
fn test_array_windows_infer() { | ||
let s = [0, 1, 0, 1, 0, 1]; | ||
for [a, b] in s.iter().copied().array_windows() { | ||
assert_eq!(a + b, 1); | ||
} | ||
for [a, b, c, d] in s.iter().copied().array_windows() { | ||
assert_eq!(a + b + c + d, 2); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_array_windows_size_hint() { | ||
let iter = (0..6).array_windows::<1>(); | ||
assert_eq!(iter.size_hint(), (6, Some(6))); | ||
|
||
let iter = (0..6).array_windows::<3>(); | ||
assert_eq!(iter.size_hint(), (4, Some(4))); | ||
|
||
let iter = (0..6).array_windows::<5>(); | ||
assert_eq!(iter.size_hint(), (2, Some(2))); | ||
|
||
let iter = (0..6).array_windows::<7>(); | ||
assert_eq!(iter.size_hint(), (0, Some(0))); | ||
|
||
let iter = (1..).array_windows::<2>(); | ||
assert_eq!(iter.size_hint(), (usize::MAX, None)); | ||
|
||
let iter = (1..).filter(|x| x % 2 != 0).array_windows::<2>(); | ||
assert_eq!(iter.size_hint(), (0, None)); | ||
} | ||
|
||
#[test] | ||
fn test_array_windows_count() { | ||
let iter = (0..6).array_windows::<1>(); | ||
assert_eq!(iter.count(), 6); | ||
|
||
let iter = (0..6).array_windows::<3>(); | ||
assert_eq!(iter.count(), 4); | ||
|
||
let iter = (0..6).array_windows::<5>(); | ||
assert_eq!(iter.count(), 2); | ||
|
||
let iter = (0..6).array_windows::<7>(); | ||
assert_eq!(iter.count(), 0); | ||
|
||
let iter = (0..6).filter(|x| x % 2 == 0).array_windows::<2>(); | ||
assert_eq!(iter.count(), 2); | ||
|
||
let iter = iter::empty::<i32>().array_windows::<2>(); | ||
assert_eq!(iter.count(), 0); | ||
|
||
let iter = [(); usize::MAX].iter().array_windows::<2>(); | ||
assert_eq!(iter.count(), usize::MAX - 1); | ||
} | ||
|
||
#[test] | ||
fn test_array_windows_nth() { | ||
let mut iter = (0..6).array_windows::<4>(); | ||
assert_eq!(iter.nth(1), Some([1, 2, 3, 4])); | ||
assert_eq!(iter.nth(0), Some([2, 3, 4, 5])); | ||
assert_eq!(iter.nth(1), None); | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod array_windows; | ||
mod chain; | ||
mod cloned; | ||
mod copied; | ||
|
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