use crate::{Coord, CoordComponent, Grid, GridIter, GridMut};
use fey_math::Vec2U;
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
pub struct GridBuf<T, S = Vec<T>> {
pub(crate) size: Vec2U,
pub(crate) store: S,
pub(crate) marker: PhantomData<T>,
}
pub type VecGrid<T> = GridBuf<T, Vec<T>>;
pub type ArrGrid<T, const N: usize> = GridBuf<T, [T; N]>;
pub type SliceGrid<'a, T> = GridBuf<T, &'a [T]>;
pub type MutSliceGrid<'a, T> = GridBuf<T, &'a mut [T]>;
impl<T, S> GridBuf<T, S> {
#[inline]
pub fn with_store(size: impl Into<Vec2U>, store: S) -> Self
where
S: AsRef<[T]>,
{
let size = size.into();
assert_eq!(
size.x.checked_mul(size.y),
Some(store.as_ref().len() as u32)
);
Self {
size,
store,
marker: PhantomData,
}
}
#[inline]
pub fn as_slice(&self) -> &[T]
where
S: AsRef<[T]>,
{
self.store.as_ref()
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T]
where
S: AsMut<[T]>,
{
self.store.as_mut()
}
#[inline]
pub fn to_store(self) -> S {
self.store
}
}
impl<T> VecGrid<T> {
#[inline]
pub fn new_from(size: impl Into<Vec2U>, mut fill: impl FnMut(Vec2U) -> T) -> Self {
let size = size.into();
let len = size.x.checked_mul(size.y).expect("grid capacity overflow");
let mut store = Vec::new();
store.reserve(len as usize);
for y in 0..size.y {
for x in 0..size.x {
store.push(fill(Vec2U::new(x, y)));
}
}
Self {
size,
store,
marker: PhantomData,
}
}
#[inline]
pub fn new_with<F: FnMut() -> T>(size: impl Into<Vec2U>, fill: F) -> Self {
let size = size.into();
let len = size.x.checked_mul(size.y).expect("grid capacity overflow");
let mut store = Vec::new();
store.resize_with(len as usize, fill);
Self {
size,
store,
marker: PhantomData,
}
}
#[inline]
pub fn new(size: impl Into<Vec2U>) -> Self
where
T: Default,
{
Self::new_with(size, T::default)
}
#[inline]
pub fn new_empty() -> Self {
Self {
size: Vec2U::ZERO,
store: Vec::new(),
marker: PhantomData,
}
}
}
impl<'a, T> SliceGrid<'a, T> {
#[inline]
pub fn new_slice(size: impl Into<Vec2U>, slice: &'a [T]) -> Self {
Self::with_store(size, slice)
}
}
impl<'a, T> MutSliceGrid<'a, T> {
#[inline]
pub fn new_mut_slice(size: impl Into<Vec2U>, slice: &'a mut [T]) -> Self {
Self::with_store(size, slice)
}
}
impl<T, const N: usize> ArrGrid<T, N> {
#[inline]
pub fn new_arr_with<F: FnMut() -> T>(size: impl Into<Vec2U>, mut fill: F) -> Self {
Self::with_store(size, std::array::from_fn(|_| fill()))
}
#[inline]
pub fn new_arr(size: impl Into<Vec2U>) -> Self
where
T: Default,
{
Self::new_arr_with(size, T::default)
}
}
impl<T, S: AsRef<[T]>> Grid for GridBuf<T, S> {
type Item = T;
type Root = Self;
#[inline]
fn root(&self) -> &Self::Root {
self
}
#[inline]
fn root_x(&self) -> u32 {
0
}
#[inline]
fn root_y(&self) -> u32 {
0
}
#[inline]
fn width(&self) -> u32 {
self.size.x
}
#[inline]
fn height(&self) -> u32 {
self.size.y
}
#[inline]
fn size(&self) -> Vec2U {
self.size
}
#[inline]
fn get(&self, x: u32, y: u32) -> Option<&Self::Item> {
y.checked_mul(self.size.x)
.and_then(|y| y.checked_add(x))
.and_then(|i| self.as_slice().get(i as usize))
}
#[inline]
unsafe fn get_unchecked(&self, x: u32, y: u32) -> &Self::Item {
unsafe {
let i = y.unchecked_mul(self.size.x).unchecked_add(x);
self.as_slice().get_unchecked(i as usize)
}
}
#[inline]
fn row_slice(&self, y: u32) -> Option<&[Self::Item]> {
y.checked_mul(self.size.x).and_then(|i| {
self.as_slice().get({
let i = i as usize;
let len = self.size.x as usize;
i..(i + len)
})
})
}
}
impl<T, S: AsRef<[T]> + AsMut<[T]>> GridMut for GridBuf<T, S> {
type RootMut = Self;
#[inline]
fn root_mut(&mut self) -> &mut Self::RootMut {
self
}
#[inline]
fn get_mut(&mut self, x: u32, y: u32) -> Option<&mut Self::Item> {
y.checked_mul(self.size.x)
.and_then(|y| y.checked_add(x))
.and_then(|i| self.as_mut_slice().get_mut(i as usize))
}
#[inline]
unsafe fn get_unchecked_mut(&mut self, x: u32, y: u32) -> &mut Self::Item {
unsafe {
let i = y.unchecked_mul(self.size.x).unchecked_add(x);
self.as_mut_slice().get_unchecked_mut(i as usize)
}
}
#[inline]
fn row_slice_mut(&mut self, y: u32) -> Option<&mut [Self::Item]> {
let w = self.size.x;
y.checked_mul(w).and_then(|i| {
self.as_mut_slice().get_mut({
let i = i as usize;
let w = w as usize;
i..(i + w)
})
})
}
}
impl<'a, T, S: AsRef<[T]>> IntoIterator for &'a GridBuf<T, S> {
type Item = (&'a T, Vec2U);
type IntoIter = GridIter<&'a GridBuf<T, S>>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, T, S: AsRef<[T]> + AsMut<[T]>> IntoIterator for &'a mut GridBuf<T, S> {
type Item = (&'a mut T, Vec2U);
type IntoIter = GridIter<&'a mut GridBuf<T, S>>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<T, S: Clone> Clone for GridBuf<T, S> {
#[inline]
fn clone(&self) -> Self {
Self {
size: self.size,
store: self.store.clone(),
marker: PhantomData,
}
}
}
impl<T: Debug, S: AsRef<[T]>> Debug for GridBuf<T, S> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.debug_fmt(f)
}
}
impl<C: Coord, T, S: AsRef<[T]>> Index<C> for GridBuf<T, S> {
type Output = T;
#[inline]
fn index(&self, index: C) -> &Self::Output {
let (w, h) = self.size.into();
self.get(
index.x().to_grid(w).expect("invalid x-coordinate"),
index.y().to_grid(h).expect("invalid y-coordinate"),
)
.expect("coordinate out of bounds")
}
}
impl<C: Coord, T, S: AsRef<[T]> + AsMut<[T]>> IndexMut<C> for GridBuf<T, S> {
#[inline]
fn index_mut(&mut self, index: C) -> &mut Self::Output {
let (w, h) = self.size.into();
self.get_mut(
index.x().to_grid(w).expect("invalid x-coordinate"),
index.y().to_grid(h).expect("invalid y-coordinate"),
)
.expect("coordinate out of bounds")
}
}