rasters/chunking/
par_iters.rs

1use rayon::iter::Map;
2use rayon::prelude::*;
3use rayon::range::Iter;
4
5use super::*;
6
7impl ChunkConfig {
8    /// Create an [ `IndexedParallelIterator` ] from the configuration.
9    ///
10    /// This function is only available with the "use-rayon" feature.
11    pub fn par_iter(&self) -> impl IndexedParallelIterator<Item = ChunkWindow> {
12        let (count, func) = self.iter_mapper();
13        (0..count).into_par_iter().map(func)
14    }
15}
16
17impl<'a> IntoParallelIterator for &'a ChunkConfig {
18    type Item = ChunkWindow<'a>;
19    type Iter = Map<Iter<usize>, Box<dyn Fn(usize) -> ChunkWindow<'a> + Send + Sync + 'a>>;
20
21    fn into_par_iter(self) -> Self::Iter {
22        let (count, func) = self.iter_mapper();
23        (0..count).into_par_iter().map(Box::new(func))
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_same_output() {
33        let cfg = ChunkConfig::with_dims(1024, 1024)
34            .add_block_size(7)
35            .with_min_data_size(0x1000)
36            .with_padding(3)
37            .with_start(13)
38            .with_end(999);
39
40        let output1: Vec<_> = cfg
41            .into_iter()
42            // .map(|(_, a, b)| (a, b))
43            .collect();
44
45        let mut output2 = vec![];
46        cfg.into_par_iter()
47            // .map(|(_, a, b)| (a, b))
48            .collect_into_vec(&mut output2);
49
50        assert_eq!(output1, output2);
51    }
52}