mop_structs/matrix/csr_matrix/
csr_matrix_row_par_iter_impls.rs

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