|
1 | | -use std::collections::{HashMap, HashSet}; |
| 1 | +use std::collections::HashMap; |
2 | 2 | use std::convert::Infallible; |
3 | 3 | use std::fmt::{Debug, Display, Formatter}; |
4 | 4 | use std::hash::Hash; |
@@ -179,48 +179,38 @@ where |
179 | 179 |
|
180 | 180 | /// Given a two-dimensional matrix of elements, returns a map of |
181 | 181 | /// [`Pt`] associated with the element at that position in the matrix. |
182 | | -pub fn matrix_to_map<M, R, T, PT>(matrix: M) -> HashMap<Pt<PT>, T> |
| 182 | +pub fn matrix_to_map<T, M, R, PT>(matrix: M) -> HashMap<Pt<PT>, T> |
183 | 183 | where |
184 | 184 | M: IntoIterator<Item = R>, |
185 | 185 | R: IntoIterator<Item = T>, |
186 | 186 | PT: TryFrom<usize>, |
187 | 187 | <PT as TryFrom<usize>>::Error: Debug, |
188 | | - Pt<PT>: Hash + Eq, |
| 188 | + Pt<PT>: Copy + Hash + Eq, |
189 | 189 | { |
190 | | - matrix |
191 | | - .into_iter() |
192 | | - .enumerate() |
193 | | - .flat_map(|(y, row)| { |
194 | | - row.into_iter() |
195 | | - .enumerate() |
196 | | - .map(move |(x, t)| (Pt::new(x.try_into().unwrap(), y.try_into().unwrap()), t)) |
197 | | - }) |
198 | | - .collect() |
| 190 | + filtered_matrix_to_map(matrix, |_, _| true) |
199 | 191 | } |
200 | 192 |
|
201 | 193 | /// Given a two-dimensional matrix of elements, returns a map of |
202 | | -/// [`Pt`] associated with the element at that position in the matrix, |
203 | | -/// ignoring any element that are in `skips`. |
204 | | -pub fn filtered_matrix_to_map<M, R, T, PT, S>(matrix: M, skips: S) -> HashMap<Pt<PT>, T> |
| 194 | +/// [`Pt`] associated with the element at that position in the matrix |
| 195 | +/// for all elements for which the predicate `f` returns `true`. |
| 196 | +pub fn filtered_matrix_to_map<T, M, R, PT, F>(matrix: M, f: F) -> HashMap<Pt<PT>, T> |
205 | 197 | where |
206 | 198 | M: IntoIterator<Item = R>, |
207 | 199 | R: IntoIterator<Item = T>, |
208 | 200 | PT: TryFrom<usize>, |
209 | 201 | <PT as TryFrom<usize>>::Error: Debug, |
210 | | - Pt<PT>: Hash + Eq, |
211 | | - S: IntoIterator<Item = T>, |
212 | | - T: Hash + Eq, |
| 202 | + Pt<PT>: Copy + Hash + Eq, |
| 203 | + F: FnMut(Pt<PT>, &T) -> bool + Clone, |
213 | 204 | { |
214 | | - let skips: HashSet<_> = skips.into_iter().collect(); |
215 | | - |
216 | 205 | matrix |
217 | 206 | .into_iter() |
218 | 207 | .enumerate() |
219 | 208 | .flat_map(|(y, row)| { |
| 209 | + let mut f = f.clone(); |
220 | 210 | row.into_iter() |
221 | 211 | .enumerate() |
222 | | - .filter(|(_, t)| !skips.contains(t)) |
223 | 212 | .map(move |(x, t)| (Pt::new(x.try_into().unwrap(), y.try_into().unwrap()), t)) |
| 213 | + .filter(move |(pt, t)| f(*pt, t)) |
224 | 214 | }) |
225 | 215 | .collect() |
226 | 216 | } |
0 commit comments