From 2d9abb841ba5ea047da176fbe4d3b153312806df Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Wed, 3 Dec 2025 22:22:53 -0800 Subject: [PATCH] grid: add find_all method/iterator --- utils/grid/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/utils/grid/lib.rs b/utils/grid/lib.rs index 38d395a..ff5f605 100644 --- a/utils/grid/lib.rs +++ b/utils/grid/lib.rs @@ -361,6 +361,13 @@ impl Grid { .unwrap_or(-1), ) } + /// Get all of the coordinates having value `needle` + pub fn find_all<'a>(&'a self, needle: &'a T) -> impl Iterator + 'a { + self.data + .iter() + .enumerate() + .filter_map(|(pos, val)| (*val == *needle).then_some(self.coord(pos as i64).unwrap())) + } pub fn count(&self, haystack: &T) -> usize { self.data.iter().filter(|item| *item == haystack).count() } @@ -566,6 +573,15 @@ FBCG"; assert!(grid.col_iter(4).is_none()); } + #[test] + fn find_all() { + let grid = unchecked_load(); + assert_eq!( + grid.find_all(&b'G').collect::>(), + [Coord2d { x: 2, y: 1 }, Coord2d { x: 3, y: 3 }] + ) + } + // #[test] // fn window_compare() { // let grid = unchecked_load();