use std::iter::repeat_with;
use std::ops::{Index, IndexMut};
use enum_map::Enum as _;
use itertools::izip;
use crate::enums::EnumSequence as _;
use crate::zones::tableau;
pub trait Deal {
type Index;
type Iter: Iterator<Item = Self::Index>;
fn deal(self) -> Self::Iter;
}
pub trait Dealable {
type Item;
type Iter: Iterator<Item = Self::Item>;
fn deal_out<D, T>(self, deal: D, piles: &mut T) -> Self::Iter
where
D: Deal,
T: Index<D::Index> + IndexMut<D::Index>,
T::Output: Extend<Self::Item>;
}
impl<I> Dealable for I
where
I: IntoIterator,
{
type Item = I::Item;
type Iter = I::IntoIter;
fn deal_out<D, T>(self, deal: D, piles: &mut T) -> Self::Iter
where
D: Deal,
T: Index<D::Index> + IndexMut<D::Index>,
T::Output: Extend<Self::Item>,
{
let mut iter = self.into_iter();
for (id, card) in izip!(deal.deal(), &mut iter) {
piles[id].extend_one(card);
}
iter
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Tableau;
impl Deal for Tableau {
type Index = tableau::Index;
type Iter = impl Iterator<Item = tableau::Index>;
fn deal(self) -> Self::Iter {
repeat_with(tableau::Index::values_iter)
.take(tableau::Index::LENGTH)
.enumerate()
.flat_map(|(offset, values_iter)| values_iter.skip(offset))
}
}