mop_structs/matrix/dr_matrix/
dr_matrix_row_par_iter_impls.rs1use crate::matrix::dr_matrix::{DrMatrixRowIter, DrMatrixRowIterMut};
2use crate::utils::{ParallelIteratorWrapper, ParallelProducerWrapper};
3use mop_common_deps::rayon::iter::{
4 plumbing::bridge,
5 plumbing::Producer,
6 plumbing::ProducerCallback,
7 plumbing::{Consumer, UnindexedConsumer},
8 IndexedParallelIterator, ParallelIterator,
9};
10
11macro_rules! impl_par_iter {
12 ($dr_matrix_row_iter:ident, $return_type:ty) => {
13 impl<'a, T> ParallelIterator for ParallelIteratorWrapper<$dr_matrix_row_iter<'a, T>>
14 where
15 T: Send + Sync,
16 {
17 type Item = $return_type;
18 fn drive_unindexed<C>(self, consumer: C) -> C::Result
19 where
20 C: UnindexedConsumer<Self::Item>,
21 {
22 bridge(self, consumer)
23 }
24
25 fn opt_len(&self) -> Option<usize> {
26 Some(self.0.len())
27 }
28 }
29
30 impl<'a, T> IndexedParallelIterator for ParallelIteratorWrapper<$dr_matrix_row_iter<'a, T>>
31 where
32 T: Send + Sync,
33 {
34 fn with_producer<Cb>(self, callback: Cb) -> Cb::Output
35 where
36 Cb: ProducerCallback<Self::Item>,
37 {
38 callback.callback(ParallelProducerWrapper(self.0))
39 }
40
41 fn len(&self) -> usize {
42 ExactSizeIterator::len(&self.0)
43 }
44
45 fn drive<C>(self, consumer: C) -> C::Result
46 where
47 C: Consumer<Self::Item>,
48 {
49 bridge(self, consumer)
50 }
51 }
52
53 impl<'a, T> IntoIterator for ParallelProducerWrapper<$dr_matrix_row_iter<'a, T>> {
54 type IntoIter = $dr_matrix_row_iter<'a, T>;
55 type Item = <Self::IntoIter as Iterator>::Item;
56
57 fn into_iter(self) -> Self::IntoIter {
58 self.0
59 }
60 }
61
62 impl<'a, T> Producer for ParallelProducerWrapper<$dr_matrix_row_iter<'a, T>> {
63 type IntoIter = $dr_matrix_row_iter<'a, T>;
64 type Item = <Self::IntoIter as Iterator>::Item;
65
66 fn into_iter(self) -> Self::IntoIter {
67 self.0
68 }
69
70 fn split_at(self, i: usize) -> (Self, Self) {
71 let (a, b) = self.0.split_at(i);
72 (ParallelProducerWrapper(a), ParallelProducerWrapper(b))
73 }
74 }
75 };
76}
77
78impl_par_iter!(DrMatrixRowIter, &'a [T]);
79impl_par_iter!(DrMatrixRowIterMut, &'a mut [T]);