From 1f4ded0c504941603c3a2243c1c06c192a5993c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 14 Jan 2023 15:30:00 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20make=20the=20ColumnIter[Mut]=20?= =?UTF-8?q?fields=20pub(crate)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/iter.rs | 46 ++++++++++++++++++++++++++++---- src/base/par_iter.rs | 62 ++++++++++++++++---------------------------- tests/lib.rs | 4 --- 3 files changed, 63 insertions(+), 49 deletions(-) diff --git a/src/base/iter.rs b/src/base/iter.rs index 4b9536ed..f213f096 100644 --- a/src/base/iter.rs +++ b/src/base/iter.rs @@ -300,8 +300,8 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut> ExactSizeIte #[derive(Clone, Debug)] /// An iterator through the columns of a matrix. pub struct ColumnIter<'a, T, R: Dim, C: Dim, S: RawStorage> { - pub(crate) mat: &'a Matrix, - pub(crate) range: Range, + mat: &'a Matrix, + range: Range, } impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage> ColumnIter<'a, T, R, C, S> { @@ -312,6 +312,22 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage> ColumnIter<'a, T, R, C, range: 0..mat.ncols(), } } + + pub(crate) fn split_at(self, index: usize) -> (Self, Self) { + // SAFETY: it’s OK even if index > self.range.len() because + // the iterations will yield None in this case. + let left_iter = ColumnIter { + mat: self.mat, + range: self.range.start..(self.range.start + index), + }; + + let right_iter = ColumnIter { + mat: self.mat, + range: (self.range.start + index)..self.range.end, + }; + + (left_iter, right_iter) + } } impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage> Iterator for ColumnIter<'a, T, R, C, S> { @@ -369,9 +385,9 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage> ExactSizeIterat /// An iterator through the mutable columns of a matrix. #[derive(Debug)] pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut> { - pub(crate) mat: *mut Matrix, - pub(crate) range: Range, - pub(crate) phantom: PhantomData<&'a mut Matrix>, + mat: *mut Matrix, + range: Range, + phantom: PhantomData<&'a mut Matrix>, } impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut> ColumnIterMut<'a, T, R, C, S> { @@ -384,6 +400,26 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut> ColumnIterMut<'a, T, } } + pub(crate) fn split_at(self, index: usize) -> (Self, Self) { + // SAFETY: it’s OK even if index > self.range.len() because + // the iterations will yield None in this case. + assert!(index <= self.range.len()); + + let left_iter = ColumnIterMut { + mat: self.mat, + range: self.range.start..(self.range.start + index), + phantom: Default::default(), + }; + + let right_iter = ColumnIterMut { + mat: self.mat, + range: (self.range.start + index)..self.range.end, + phantom: Default::default(), + }; + + (left_iter, right_iter) + } + fn ncols(&self) -> usize { unsafe { (*self.mat).ncols() } } diff --git a/src/base/par_iter.rs b/src/base/par_iter.rs index 2a8ca268..820c408a 100644 --- a/src/base/par_iter.rs +++ b/src/base/par_iter.rs @@ -14,7 +14,7 @@ use rayon::{iter::plumbing::bridge, prelude::*}; /// A rayon parallel iterator over the colums of a matrix. It is created /// using the [`par_column_iter`] method of [`Matrix`]. /// -/// *only availabe if compiled with the feature `par-iter`* +/// *Only available if compiled with the feature `par-iter`.* /// [`par_column_iter`]: crate::Matrix::par_column_iter /// [`Matrix`]: crate::Matrix #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] @@ -23,7 +23,7 @@ pub struct ParColumnIter<'a, T, R: Dim, Cols: Dim, S: RawStorage> { } impl<'a, T, R: Dim, Cols: Dim, S: RawStorage> ParColumnIter<'a, T, R, Cols, S> { - /// create a new parallel iterator for the given matrix + /// Create a new parallel iterator for the given matrix. fn new(matrix: &'a Matrix) -> Self { Self { mat: matrix } } @@ -51,7 +51,7 @@ where } #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] -/// *only availabe if compiled with the feature `par-iter`* +/// *Only available if compiled with the feature `par-iter`.* impl<'a, T, R: Dim, Cols: Dim, S: RawStorage> IndexedParallelIterator for ParColumnIter<'a, T, R, Cols, S> where @@ -76,8 +76,8 @@ where } #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] -/// A rayon parallel iterator through the mutable columns of a matrix -/// *only availabe if compiled with the feature `par-iter`* +/// A rayon parallel iterator through the mutable columns of a matrix. +/// *Only available if compiled with the feature `par-iter`.* pub struct ParColumnIterMut< 'a, T, @@ -96,14 +96,14 @@ where Cols: Dim, S: RawStorage + RawStorageMut, { - /// create a new parallel iterator for the given matrix + /// create a new parallel iterator for the given matrix. fn new(mat: &'a mut Matrix) -> Self { Self { mat } } } #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] -/// *only availabe if compiled with the feature `par-iter`* +/// *Only available if compiled with the feature `par-iter`* impl<'a, T, R, Cols, S> ParallelIterator for ParColumnIterMut<'a, T, R, Cols, S> where R: Dim, @@ -126,7 +126,7 @@ where } #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] -/// *only availabe if compiled with the feature `par-iter`* +/// *Only available if compiled with the feature `par-iter`* impl<'a, T, R, Cols, S> IndexedParallelIterator for ParColumnIterMut<'a, T, R, Cols, S> where R: Dim, @@ -154,7 +154,7 @@ where #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] /// # Parallel iterators using `rayon` -/// *Only availabe if compiled with the feature `par-iter`* +/// *Only available if compiled with the feature `par-iter`* impl> Matrix where T: Send + Sync + Scalar, @@ -190,6 +190,7 @@ where pub fn par_column_iter(&self) -> ParColumnIter<'_, T, R, Cols, S> { ParColumnIter::new(self) } + /// Mutably iterate through the columns of this matrix in parallel using rayon. /// Allows mutable access to the columns in parallel using mutable references. /// If mutable access to the columns is not required rather use [`par_column_iter`] @@ -221,9 +222,9 @@ where } } -/// a private helper newtype that wraps the `ColumnIter` and implements +/// A private helper newtype that wraps the `ColumnIter` and implements /// the rayon `Producer` trait. It's just here so we don't have to make the -/// rayon trait part of the public interface of the `ColumnIter` +/// rayon trait part of the public interface of the `ColumnIter`. struct ColumnProducer<'a, T, R: Dim, C: Dim, S: RawStorage>(ColumnIter<'a, T, R, C, S>); #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] @@ -238,24 +239,16 @@ where type IntoIter = ColumnIter<'a, T, R, Cols, S>; #[inline] - fn split_at(self, index: usize) -> (Self, Self) { - // the index is relative to the size of this current iterator - // it will always start at zero so it serves as an offset - let left_iter = ColumnIter { - mat: self.0.mat, - range: self.0.range.start..(self.0.range.start + index), - }; - - let right_iter = ColumnIter { - mat: self.0.mat, - range: (self.0.range.start + index)..self.0.range.end, - }; - (Self(left_iter), Self(right_iter)) + fn into_iter(self) -> Self::IntoIter { + self.0 } #[inline] - fn into_iter(self) -> Self::IntoIter { - self.0 + fn split_at(self, index: usize) -> (Self, Self) { + // The index is relative to the size of this current iterator. + // It will always start at zero so it serves as an offset. + let (left_iter, right_iter) = self.0.split_at(index); + (Self(left_iter), Self(right_iter)) } } @@ -279,20 +272,9 @@ where } fn split_at(self, index: usize) -> (Self, Self) { - // the index is relative to the size of this current iterator - // it will always start at zero so it serves as an offset - - let left_iter = ColumnIterMut { - mat: self.0.mat, - range: self.0.range.start..(self.0.range.start + index), - phantom: Default::default(), - }; - - let right_iter = ColumnIterMut { - mat: self.0.mat, - range: (self.0.range.start + index)..self.0.range.end, - phantom: Default::default(), - }; + // The index is relative to the size of this current iterator + // it will always start at zero so it serves as an offset. + let (left_iter, right_iter) = self.0.split_at(index); (Self(left_iter), Self(right_iter)) } } diff --git a/tests/lib.rs b/tests/lib.rs index 6e4238e5..546aa8a7 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -9,10 +9,6 @@ compile_error!( Example: `cargo test --features debug,compare,rand,macros`" ); -// make sure to test the parallel iterators for all builds that do not require no_std -#[cfg(all(feature = "std", not(feature = "par-iter")))] -compile_error!("Please additionally enable the `par-iter` feature to compile and run the tests"); - #[cfg(all(feature = "debug", feature = "compare", feature = "rand"))] #[macro_use] extern crate approx;