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)]
/// An iterator through the columns of a matrix.
pub struct ColumnIter<'a, T, R: Dim, C: Dim, S: RawStorage<T, R, C>> {
pub(crate) mat: &'a Matrix<T, R, C, S>,
pub(crate) range: Range<usize>,
mat: &'a Matrix<T, R, C, S>,
range: Range<usize>,
}
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(),
}
}
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> {
@ -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.
#[derive(Debug)]
pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> {
pub(crate) mat: *mut Matrix<T, R, C, S>,
pub(crate) range: Range<usize>,
pub(crate) phantom: PhantomData<&'a mut Matrix<T, R, C, S>>,
mat: *mut Matrix<T, R, C, S>,
range: Range<usize>,
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> {
@ -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 {
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
/// 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<T, R, Cols>> {
}
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 {
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<T, R, Cols>> 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<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 {
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<T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Matrix<T, R, Cols, S>
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<T, R, C>>(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))
}
}

View File

@ -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;