use core::fmt;
use core::fmt::Debug;
use core::iter::FusedIterator;
use crate::adaptors::generic_combinations::GenericCombinations;
pub trait IterArrayCombinationsWithReps: Iterator {
#[inline]
fn array_combinations_with_reps<const K: usize>(self) -> ArrayCombinationsWithReps<Self, K>
where
Self: Sized,
Self::Item: Clone,
{
ArrayCombinationsWithReps::new(self)
}
}
impl<I: ?Sized> IterArrayCombinationsWithReps for I where I: Iterator {}
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct ArrayCombinationsWithReps<I, const K: usize>(GenericCombinations<I, [usize; K]>)
where
I: Iterator;
impl<I, const K: usize> ArrayCombinationsWithReps<I, K>
where
I: Iterator,
{
#[track_caller]
pub(crate) fn new(iter: I) -> Self {
assert!(K != 0, "combination size must be non-zero");
Self(GenericCombinations::new(iter, [0; K]))
}
}
impl<I, const K: usize> Clone for ArrayCombinationsWithReps<I, K>
where
I: Iterator + Clone,
I::Item: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<I, const K: usize> Debug for ArrayCombinationsWithReps<I, K>
where
I: Iterator + Debug,
I::Item: Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt_with(f, "ArrayCombinationsWithReps")
}
}
impl<I, const K: usize> Iterator for ArrayCombinationsWithReps<I, K>
where
I: Iterator,
I::Item: Clone,
{
type Item = [I::Item; K];
fn next(&mut self) -> Option<Self::Item> {
self.0.fill_next_with_reps().map(|it| {
unsafe { arrays::from_iter_unchecked(it) }
})
}
}
impl<I, const K: usize> FusedIterator for ArrayCombinationsWithReps<I, K>
where
I: Iterator,
I::Item: Clone,
{
}