make mut iterator pointer based

Change the ColumnIterMut back to the original impl and manually implement Send
This commit is contained in:
Geo 2022-10-23 13:40:03 +02:00 committed by Sébastien Crozet
parent 42ab3f6903
commit f6461d3862
1 changed files with 28 additions and 25 deletions

View File

@ -226,7 +226,7 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> Iterator for RowIter<'a
} }
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ExactSizeIterator impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ExactSizeIterator
for RowIter<'a, T, R, C, S> for RowIter<'a, T, R, C, S>
{ {
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
@ -257,7 +257,7 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> RowIterMut<'a, T, R,
} }
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Iterator impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Iterator
for RowIterMut<'a, T, R, C, S> for RowIterMut<'a, T, R, C, S>
{ {
type Item = MatrixViewMut<'a, T, U1, C, S::RStride, S::CStride>; type Item = MatrixViewMut<'a, T, U1, C, S::RStride, S::CStride>;
@ -284,7 +284,7 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Iterator
} }
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ExactSizeIterator impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ExactSizeIterator
for RowIterMut<'a, T, R, C, S> for RowIterMut<'a, T, R, C, S>
{ {
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
@ -341,7 +341,7 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> Iterator for ColumnIter
} }
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> DoubleEndedIterator impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> DoubleEndedIterator
for ColumnIter<'a, T, R, C, S> for ColumnIter<'a, T, R, C, S>
{ {
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
debug_assert!(self.range.start <= self.range.end); debug_assert!(self.range.start <= self.range.end);
@ -357,7 +357,7 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> DoubleEndedIterator
} }
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ExactSizeIterator impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ExactSizeIterator
for ColumnIter<'a, T, R, C, S> for ColumnIter<'a, T, R, C, S>
{ {
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
@ -366,7 +366,7 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ExactSizeIterat
} }
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Producer for ColumnIter<'a, T, R, Cols, S> impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Producer for ColumnIter<'a, T, R, Cols, S>
where where
T: Send + Sync + Debug + PartialEq + Clone + 'static, T: Send + Sync + Debug + PartialEq + Clone + 'static,
S: Sync, S: Sync,
{ {
@ -396,31 +396,31 @@ where
/// 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>> {
mat: &'a mut Matrix<T, R, C, S>, mat: *mut Matrix<T, R, C, S>,
phantom: PhantomData<&'a mut Matrix<T, R, C, S>>,
range: Range<usize>, range: Range<usize>,
} }
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> {
pub(crate) fn new(mat: &'a mut Matrix<T, R, C, S>) -> Self { pub(crate) fn new(mat: &'a mut Matrix<T, R, C, S>) -> Self {
let range = 0..mat.ncols(); let range = 0..mat.ncols();
ColumnIterMut { mat, range } ColumnIterMut { mat, range, phantom: Default::default() }
} }
fn ncols(&self) -> usize { fn ncols(&self) -> usize {
self.mat.ncols() unsafe { (*self.mat).ncols() }
} }
} }
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Iterator impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Iterator
for ColumnIterMut<'a, T, R, C, S> for ColumnIterMut<'a, T, R, C, S>
{ {
type Item = MatrixViewMut<'a, T, R, U1, S::RStride, S::CStride>; type Item = MatrixViewMut<'a, T, R, U1, S::RStride, S::CStride>;
#[inline] #[inline]
fn next(&'_ mut self) -> Option<Self::Item> { fn next(&'_ mut self) -> Option<Self::Item> {
if self.range.start < self.ncols() { if self.range.start < self.ncols() {
let pmat: *mut _ = self.mat; let res = unsafe { (*self.mat).column_mut(self.range.start) };
let res = unsafe { (*pmat).column_mut(self.range.start) };
self.range.start += 1; self.range.start += 1;
Some(res) Some(res)
} else { } else {
@ -441,7 +441,7 @@ impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Iterator
} }
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ExactSizeIterator impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ExactSizeIterator
for ColumnIterMut<'a, T, R, C, S> for ColumnIterMut<'a, T, R, C, S>
{ {
#[inline] #[inline]
fn len(&self) -> usize { fn len(&self) -> usize {
@ -450,16 +450,15 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> ExactSizeIte
} }
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> DoubleEndedIterator impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> DoubleEndedIterator
for ColumnIterMut<'a, T, R, C, S> for ColumnIterMut<'a, T, R, C, S>
{ {
fn next_back(&mut self) -> Option<Self::Item> { fn next_back(&mut self) -> Option<Self::Item> {
debug_assert!(self.range.start <= self.range.end); debug_assert!(self.range.start <= self.range.end);
if !self.range.is_empty() { if !self.range.is_empty() {
self.range.end -= 1; self.range.end -= 1;
debug_assert!(self.range.end < self.mat.ncols()); debug_assert!(self.range.end < self.ncols());
debug_assert!(self.range.end >= self.range.start); debug_assert!(self.range.end >= self.range.start);
let pmat: *mut _ = self.mat; Some(unsafe { (*self.mat).column_mut(self.range.end) })
Some(unsafe { (*pmat).column_mut(self.range.end) })
} else { } else {
None None
} }
@ -467,8 +466,8 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> DoubleEndedI
} }
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Producer impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Producer
for ColumnIterMut<'a, T, R, C, S> for ColumnIterMut<'a, T, R, C, S>
where where
T: Send + Sync + Debug + PartialEq + Clone, T: Send + Sync + Debug + PartialEq + Clone,
S: Send + Sync, S: Send + Sync,
{ {
@ -482,17 +481,21 @@ 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 pmat: *mut _ = self.mat;
let left = Self { let left = Self {
mat: unsafe { &mut *pmat }, mat: self.mat,
range: self.range.start..(self.range.start + index), range: self.range.start..(self.range.start + index),
phantom: Default::default(),
}; };
let right = Self { let right = Self {
mat: self.mat, mat: self.mat,
range: (self.range.start + index)..self.range.end, range: (self.range.start + index)..self.range.end,
phantom: Default::default(),
}; };
(left, right) (left, right)
} }
} }
unsafe impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Send
for ColumnIterMut<'a, T, R, C, S> {}