Don’t make the ColumnIter[Mut] fields pub(crate)

This commit is contained in:
Sébastien Crozet 2023-01-14 15:30:00 +01:00
parent 5e26b8e121
commit 1f4ded0c50
3 changed files with 63 additions and 49 deletions

View File

@ -300,8 +300,8 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ExactSizeIte
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
/// An iterator through the columns of a matrix. /// An iterator through the columns of a matrix.
pub struct ColumnIter<'a, T, R: Dim, C: Dim, S: RawStorage<T, R, C>> { pub struct ColumnIter<'a, T, R: Dim, C: Dim, S: RawStorage<T, R, C>> {
pub(crate) mat: &'a Matrix<T, R, C, S>, mat: &'a Matrix<T, R, C, S>,
pub(crate) range: Range<usize>, range: Range<usize>,
} }
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ColumnIter<'a, T, R, C, S> { impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ColumnIter<'a, T, R, C, S> {
@ -312,6 +312,22 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ColumnIter<'a, T, R, C,
range: 0..mat.ncols(), range: 0..mat.ncols(),
} }
} }
pub(crate) fn split_at(self, index: usize) -> (Self, Self) {
// SAFETY: its 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<T, R, C>> Iterator for ColumnIter<'a, T, R, C, S> { impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> Iterator for ColumnIter<'a, T, R, C, S> {
@ -369,9 +385,9 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ExactSizeIterat
/// An iterator through the mutable columns of a matrix. /// An iterator through the mutable columns of a matrix.
#[derive(Debug)] #[derive(Debug)]
pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> { pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> {
pub(crate) mat: *mut Matrix<T, R, C, S>, mat: *mut Matrix<T, R, C, S>,
pub(crate) range: Range<usize>, range: Range<usize>,
pub(crate) phantom: PhantomData<&'a mut Matrix<T, R, C, S>>, phantom: PhantomData<&'a mut Matrix<T, R, C, S>>,
} }
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ColumnIterMut<'a, T, R, C, S> { impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ColumnIterMut<'a, T, R, C, S> {
@ -384,6 +400,26 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ColumnIterMut<'a, T,
} }
} }
pub(crate) fn split_at(self, index: usize) -> (Self, Self) {
// SAFETY: its 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 { fn ncols(&self) -> usize {
unsafe { (*self.mat).ncols() } unsafe { (*self.mat).ncols() }
} }

View File

@ -14,7 +14,7 @@ use rayon::{iter::plumbing::bridge, prelude::*};
/// A rayon parallel iterator over the colums of a matrix. It is created /// A rayon parallel iterator over the colums of a matrix. It is created
/// using the [`par_column_iter`] method of [`Matrix`]. /// 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 /// [`par_column_iter`]: crate::Matrix::par_column_iter
/// [`Matrix`]: crate::Matrix /// [`Matrix`]: crate::Matrix
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
@ -23,7 +23,7 @@ pub struct ParColumnIter<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> {
} }
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> ParColumnIter<'a, T, R, Cols, S> { impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> 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<T, R, Cols, S>) -> Self { fn new(matrix: &'a Matrix<T, R, Cols, S>) -> Self {
Self { mat: matrix } Self { mat: matrix }
} }
@ -51,7 +51,7 @@ where
} }
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] #[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<T, R, Cols>> IndexedParallelIterator impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> IndexedParallelIterator
for ParColumnIter<'a, T, R, Cols, S> for ParColumnIter<'a, T, R, Cols, S>
where where
@ -76,8 +76,8 @@ where
} }
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
/// A rayon parallel iterator through the mutable columns of a matrix /// A rayon parallel iterator through the mutable columns of a matrix.
/// *only availabe if compiled with the feature `par-iter`* /// *Only available if compiled with the feature `par-iter`.*
pub struct ParColumnIterMut< pub struct ParColumnIterMut<
'a, 'a,
T, T,
@ -96,14 +96,14 @@ where
Cols: Dim, Cols: Dim,
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>, S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>,
{ {
/// create a new parallel iterator for the given matrix /// create a new parallel iterator for the given matrix.
fn new(mat: &'a mut Matrix<T, R, Cols, S>) -> Self { fn new(mat: &'a mut Matrix<T, R, Cols, S>) -> Self {
Self { mat } Self { mat }
} }
} }
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] #[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> impl<'a, T, R, Cols, S> ParallelIterator for ParColumnIterMut<'a, T, R, Cols, S>
where where
R: Dim, R: Dim,
@ -126,7 +126,7 @@ where
} }
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] #[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> impl<'a, T, R, Cols, S> IndexedParallelIterator for ParColumnIterMut<'a, T, R, Cols, S>
where where
R: Dim, R: Dim,
@ -154,7 +154,7 @@ where
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
/// # Parallel iterators using `rayon` /// # Parallel iterators using `rayon`
/// *Only availabe if compiled with the feature `par-iter`* /// *Only available if compiled with the feature `par-iter`*
impl<T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Matrix<T, R, Cols, S> impl<T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Matrix<T, R, Cols, S>
where where
T: Send + Sync + Scalar, T: Send + Sync + Scalar,
@ -190,6 +190,7 @@ where
pub fn par_column_iter(&self) -> ParColumnIter<'_, T, R, Cols, S> { pub fn par_column_iter(&self) -> ParColumnIter<'_, T, R, Cols, S> {
ParColumnIter::new(self) ParColumnIter::new(self)
} }
/// Mutably iterate through the columns of this matrix in parallel using rayon. /// Mutably iterate through the columns of this matrix in parallel using rayon.
/// Allows mutable access to the columns in parallel using mutable references. /// 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`] /// 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 /// 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<T, R, C>>(ColumnIter<'a, T, R, C, S>); struct ColumnProducer<'a, T, R: Dim, C: Dim, S: RawStorage<T, R, C>>(ColumnIter<'a, T, R, C, S>);
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))] #[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
@ -238,24 +239,16 @@ where
type IntoIter = ColumnIter<'a, T, R, Cols, S>; type IntoIter = ColumnIter<'a, T, R, Cols, S>;
#[inline] #[inline]
fn split_at(self, index: usize) -> (Self, Self) { fn into_iter(self) -> Self::IntoIter {
// the index is relative to the size of this current iterator self.0
// 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))
} }
#[inline] #[inline]
fn into_iter(self) -> Self::IntoIter { fn split_at(self, index: usize) -> (Self, Self) {
self.0 // 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) { fn split_at(self, index: usize) -> (Self, Self) {
// the index is relative to the size of this current iterator // The index is relative to the size of this current iterator
// it will always start at zero so it serves as an offset // it will always start at zero so it serves as an offset.
let (left_iter, right_iter) = self.0.split_at(index);
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(),
};
(Self(left_iter), Self(right_iter)) (Self(left_iter), Self(right_iter))
} }
} }

View File

@ -9,10 +9,6 @@ compile_error!(
Example: `cargo test --features debug,compare,rand,macros`" 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"))] #[cfg(all(feature = "debug", feature = "compare", feature = "rand"))]
#[macro_use] #[macro_use]
extern crate approx; extern crate approx;