mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-07-04 06:39:07 +02:00
Introduce ChunkArray type
A generic array for per-chunk data, indexed by ChunkCoords.
This commit is contained in:
parent
48e03aa266
commit
28b22ce423
4 changed files with 71 additions and 26 deletions
41
src/types.rs
41
src/types.rs
|
@ -1,4 +1,9 @@
|
|||
use std::fmt::Debug;
|
||||
use std::{
|
||||
fmt::Debug,
|
||||
ops::{Index, IndexMut},
|
||||
};
|
||||
|
||||
use itertools::iproduct;
|
||||
|
||||
pub const CHUNKS_PER_REGION: u8 = 32;
|
||||
|
||||
|
@ -22,3 +27,37 @@ impl Debug for ChunkCoords {
|
|||
write!(f, "({}, {})", self.x.0, self.z.0)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct ChunkArray<T>(pub [[T; CHUNKS_PER_REGION as usize]; CHUNKS_PER_REGION as usize]);
|
||||
|
||||
impl<T> ChunkArray<T> {
|
||||
pub fn keys() -> impl Iterator<Item = ChunkCoords> {
|
||||
iproduct!(0..CHUNKS_PER_REGION, 0..CHUNKS_PER_REGION).map(|(z, x)| ChunkCoords {
|
||||
x: ChunkX(x),
|
||||
z: ChunkZ(z),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn values(&self) -> impl Iterator<Item = &T> {
|
||||
Self::keys().map(|k| &self[k])
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> impl Iterator<Item = (ChunkCoords, &T)> {
|
||||
Self::keys().map(|k| (k, &self[k]))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Index<ChunkCoords> for ChunkArray<T> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, index: ChunkCoords) -> &Self::Output {
|
||||
&self.0[index.z.0 as usize][index.x.0 as usize]
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IndexMut<ChunkCoords> for ChunkArray<T> {
|
||||
fn index_mut(&mut self, index: ChunkCoords) -> &mut Self::Output {
|
||||
&mut self.0[index.z.0 as usize][index.x.0 as usize]
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue