grid view: better range()

This commit is contained in:
Michael Sippel 2021-06-12 04:15:32 +02:00
parent 2896799fae
commit 9d91f938b1
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -9,6 +9,7 @@ use {
}; };
pub mod offset; pub mod offset;
pub mod flatten;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
@ -17,21 +18,22 @@ pub trait GridView = IndexView<Point2<i16>>;
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
impl<Item> dyn GridView<Item = Item> { impl<Item> dyn GridView<Item = Item> {
pub fn range(&self) -> RangeInclusive<Point2<i16>> { pub fn range(&self) -> Range<Point2<i16>> {
let area = self.area().unwrap_or(Vec::new()); if let Some(area) = self.area() {
Point2::new(
Point2::new( area.iter().map(|p| p.x).min().unwrap_or(0),
area.iter().map(|p| p.x).min().unwrap_or(i16::MIN), area.iter().map(|p| p.y).min().unwrap_or(0)
area.iter().map(|p| p.y).min().unwrap_or(i16::MIN) ) ..
) ..= Point2::new(
Point2::new( area.iter().map(|p| p.x+1).max().unwrap_or(0),
area.iter().map(|p| p.x).max().unwrap_or(i16::MAX), area.iter().map(|p| p.y+1).max().unwrap_or(0)
area.iter().map(|p| p.y).max().unwrap_or(i16::MAX) )
) } else {
Point2::new(i16::MIN, i16::MIN) .. Point2::new(i16::MAX, i16::MAX)
}
} }
} }
//<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>> //<<<<>>>><<>><><<>><<<*>>><<>><><<>><<<<>>>>
pub struct GridWindowIterator { pub struct GridWindowIterator {