mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
minedmap/region_group: make indexing into the neighbor array more uniform
Add a 9th element, so the 3x3 entries can always be indexed the same way, leading to a slight performance improvement. The center element is always None.
This commit is contained in:
parent
7b46adf6e7
commit
521e799d4b
1 changed files with 9 additions and 18 deletions
|
@ -11,7 +11,7 @@ use anyhow::Result;
|
|||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct RegionGroup<T> {
|
||||
center: T,
|
||||
neighs: [Option<T>; 8],
|
||||
neighs: [Option<T>; 9],
|
||||
}
|
||||
|
||||
impl<T> RegionGroup<T> {
|
||||
|
@ -26,6 +26,7 @@ impl<T> RegionGroup<T> {
|
|||
Some((-1, 0)),
|
||||
Some((-1, 1)),
|
||||
Some((0, -1)),
|
||||
None,
|
||||
Some((0, 1)),
|
||||
Some((1, -1)),
|
||||
Some((1, 0)),
|
||||
|
@ -40,23 +41,13 @@ impl<T> RegionGroup<T> {
|
|||
}
|
||||
|
||||
pub fn get(&self, x: i8, z: i8) -> Option<&T> {
|
||||
let index = match (x, z) {
|
||||
(0, 0) => {
|
||||
return Some(&self.center);
|
||||
}
|
||||
(-1, -1) => 0,
|
||||
(-1, 0) => 1,
|
||||
(-1, 1) => 2,
|
||||
(0, -1) => 3,
|
||||
(0, 1) => 4,
|
||||
(1, -1) => 5,
|
||||
(1, 0) => 6,
|
||||
(1, 1) => 7,
|
||||
_ => {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
self.neighs[index].as_ref()
|
||||
if (x, z) == (0, 0) {
|
||||
return Some(&self.center);
|
||||
}
|
||||
if !(-1..=1).contains(&x) || !(-1..=1).contains(&z) {
|
||||
return None;
|
||||
}
|
||||
self.neighs.get((3 * x + z + 4) as usize)?.as_ref()
|
||||
}
|
||||
|
||||
pub fn map<U, F>(self, mut f: F) -> RegionGroup<U>
|
||||
|
|
Loading…
Add table
Reference in a new issue