Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/array_impl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Itertools;
use crate::{size_hint, Itertools};
use std::iter::Fuse;

/// An iterator over all contiguous windows of the input iterator,
Expand Down Expand Up @@ -76,6 +76,19 @@ where
},
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
let inner_sh = self.iter.size_hint();
if self.inner.is_none() {
if N == 0 {
size_hint::add_scalar(inner_sh, 1)
} else {
size_hint::sub_scalar(inner_sh, N - 1)
}
} else {
inner_sh
}
}
}

pub fn array_windows<I, const N: usize>(iter: I) -> ArrayWindows<I, N>
Expand All @@ -89,6 +102,17 @@ where
}
}

macro_rules! array_windows_exact_size {
($($n: literal),*) => {
$(
impl<I: ExactSizeIterator<Item: Clone>> ExactSizeIterator for ArrayWindows<I, $n> {}
)*
};
}

// cannot be implemented for `N == 0` since it increases the length
array_windows_exact_size!(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);

/// An iterator over all windows, wrapping back to the first elements when the
/// window would otherwise exceed the length of the iterator, producing arrays
/// of a specific size.
Expand Down
9 changes: 9 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,15 @@ quickcheck! {
true
}

fn array_windows_exact_size_1(a: Vec<u8>) -> bool {
exact_size(a.iter().array_windows::<1>())
}

fn array_windows_exact_size_4(a: Vec<u8>) -> bool {
exact_size(a.iter().array_windows::<4>())
}


fn equal_circular_array_windows_0(a: Vec<u8>) -> bool {
let x = a.iter().map(|_| [&0u8; 0] );
let y = a.iter().circular_array_windows::<0>();
Expand Down
Loading