use crate::{Hex, HexBounds};
pub trait HexIterExt: Iterator {
#[doc(alias = "mean")]
fn average(self) -> Hex;
#[doc(alias = "centroid")]
fn center(self) -> Hex;
fn bounds(self) -> HexBounds;
}
impl<I: Iterator<Item = Hex>> HexIterExt for I {
fn average(self) -> Hex {
let mut sum = Hex::ZERO;
let mut count = 0;
for hex in self {
count += 1;
sum += hex;
}
sum / count.max(1)
}
fn center(self) -> Hex {
self.bounds().center
}
fn bounds(self) -> HexBounds {
self.collect()
}
}
#[derive(Debug, Clone)]
pub struct ExactSizeHexIterator<I> {
pub iter: I,
pub count: usize,
}
impl<I, T> Iterator for ExactSizeHexIterator<I>
where
I: Iterator<Item = T>,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
self.count = self.count.saturating_sub(1);
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.count, Some(self.count))
}
}
impl<I> ExactSizeIterator for ExactSizeHexIterator<I> where I: Iterator {}