hashgrid 0.1.0

A 2D spatial-hash grid library
Documentation
use std::ops::{Div, RangeInclusive};

use crate::IntoCellIter;

/// A range of coordinates inclusively.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Range<A> {
    pub min: [A; 2],
    pub max: [A; 2],
}

impl<A> Range<A> {
    pub fn new(min: [A; 2], max: [A; 2]) -> Self {
        Self { min, max }
    }
}

impl<A: Copy> Range<A>
where
    RangeInclusive<A>: Iterator<Item = A>,
{
    #[inline]
    pub fn iter_range(self) -> impl Iterator<Item = [A; 2]> {
        (self.min[0]..=self.max[0])
            .flat_map(move |x| (self.min[1]..=self.max[1]).map(move |y| [x, y]))
    }
}

impl<A> Range<A>
where
    A: Copy + Div<Output = A>,
{
    #[inline]
    pub fn snapped(self, step: A) -> Self {
        Self::new(self.min.map(|x| x / step), self.max.map(|x| x / step))
    }
}

impl<A: Copy> IntoCellIter<A> for Range<A>
where
    RangeInclusive<A>: Iterator<Item = A>,
{
    #[inline]
    fn iter_cells(self) -> impl Iterator<Item = [A; 2]> {
        self.iter_range()
    }
}