minedmap/region_group: optimize get() implementation

This commit is contained in:
Matthias Schiffer 2023-08-05 11:17:52 +02:00
parent 7d231b92ea
commit cd1a5e869d
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C

View file

@ -40,18 +40,23 @@ impl<T> RegionGroup<T> {
} }
pub fn get(&self, x: i8, z: i8) -> Option<&T> { pub fn get(&self, x: i8, z: i8) -> Option<&T> {
match (x, z) { let index = match (x, z) {
(0, 0) => Some(&self.center), (0, 0) => {
(-1, -1) => self.neighs[0].as_ref(), return Some(&self.center);
(-1, 0) => self.neighs[1].as_ref(), }
(-1, 1) => self.neighs[2].as_ref(), (-1, -1) => 0,
(0, -1) => self.neighs[3].as_ref(), (-1, 0) => 1,
(0, 1) => self.neighs[4].as_ref(), (-1, 1) => 2,
(1, -1) => self.neighs[5].as_ref(), (0, -1) => 3,
(1, 0) => self.neighs[6].as_ref(), (0, 1) => 4,
(1, 1) => self.neighs[7].as_ref(), (1, -1) => 5,
_ => panic!("index out of bounds"), (1, 0) => 6,
} (1, 1) => 7,
_ => {
return None;
}
};
self.neighs[index].as_ref()
} }
pub fn map<U, F>(self, mut f: F) -> RegionGroup<U> pub fn map<U, F>(self, mut f: F) -> RegionGroup<U>