grid: add find_all method/iterator
This commit is contained in:
@@ -361,6 +361,13 @@ impl<T: Clone + Eq + PartialEq + Debug> Grid<T> {
|
|||||||
.unwrap_or(-1),
|
.unwrap_or(-1),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
/// Get all of the coordinates having value `needle`
|
||||||
|
pub fn find_all<'a>(&'a self, needle: &'a T) -> impl Iterator<Item = Coord2d> + '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 {
|
pub fn count(&self, haystack: &T) -> usize {
|
||||||
self.data.iter().filter(|item| *item == haystack).count()
|
self.data.iter().filter(|item| *item == haystack).count()
|
||||||
}
|
}
|
||||||
@@ -566,6 +573,15 @@ FBCG";
|
|||||||
assert!(grid.col_iter(4).is_none());
|
assert!(grid.col_iter(4).is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn find_all() {
|
||||||
|
let grid = unchecked_load();
|
||||||
|
assert_eq!(
|
||||||
|
grid.find_all(&b'G').collect::<Vec<_>>(),
|
||||||
|
[Coord2d { x: 2, y: 1 }, Coord2d { x: 3, y: 3 }]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn window_compare() {
|
// fn window_compare() {
|
||||||
// let grid = unchecked_load();
|
// let grid = unchecked_load();
|
||||||
|
|||||||
Reference in New Issue
Block a user