From 521e799d4b7c071bfef4645f06f8c8f9b9ca4d84 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 5 Aug 2023 14:36:59 +0200 Subject: [PATCH] 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. --- src/bin/minedmap/region_group.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/bin/minedmap/region_group.rs b/src/bin/minedmap/region_group.rs index 4e7c636..aa7088c 100644 --- a/src/bin/minedmap/region_group.rs +++ b/src/bin/minedmap/region_group.rs @@ -11,7 +11,7 @@ use anyhow::Result; #[derive(Debug, Clone, Copy)] pub struct RegionGroup { center: T, - neighs: [Option; 8], + neighs: [Option; 9], } impl RegionGroup { @@ -26,6 +26,7 @@ impl RegionGroup { Some((-1, 0)), Some((-1, 1)), Some((0, -1)), + None, Some((0, 1)), Some((1, -1)), Some((1, 0)), @@ -40,23 +41,13 @@ impl RegionGroup { } 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(self, mut f: F) -> RegionGroup