2017-02-13 01:17:09 +08:00
|
|
|
//! Matrix iterators.
|
|
|
|
|
2020-11-20 05:00:28 +08:00
|
|
|
use std::iter::FusedIterator;
|
2016-12-05 05:44:42 +08:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::mem;
|
|
|
|
|
2019-03-23 21:29:07 +08:00
|
|
|
use crate::base::dimension::{Dim, U1};
|
|
|
|
use crate::base::storage::{Storage, StorageMut};
|
2020-04-06 00:49:48 +08:00
|
|
|
use crate::base::{Matrix, MatrixSlice, MatrixSliceMut, Scalar};
|
2016-12-05 05:44:42 +08:00
|
|
|
|
|
|
|
macro_rules! iterator {
|
|
|
|
(struct $Name:ident for $Storage:ident.$ptr: ident -> $Ptr:ty, $Ref:ty, $SRef: ty) => {
|
|
|
|
/// An iterator through a dense matrix with arbitrary strides matrix.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub struct $Name<'a, N: Scalar, R: Dim, C: Dim, S: 'a + $Storage<N, R, C>> {
|
2018-10-22 13:00:10 +08:00
|
|
|
ptr: $Ptr,
|
2016-12-05 05:44:42 +08:00
|
|
|
inner_ptr: $Ptr,
|
|
|
|
inner_end: $Ptr,
|
2018-10-22 13:00:10 +08:00
|
|
|
size: usize, // We can't use an end pointer here because a stride might be zero.
|
|
|
|
strides: (S::RStride, S::CStride),
|
|
|
|
_phantoms: PhantomData<($Ref, R, C, S)>,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
2020-11-15 23:57:49 +08:00
|
|
|
// TODO: we need to specialize for the case where the matrix storage is owned (in which
|
2016-12-05 05:44:42 +08:00
|
|
|
// case the iterator is trivial because it does not have any stride).
|
2019-12-17 07:09:14 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + $Storage<N, R, C>> $Name<'a, N, R, C, S> {
|
2016-12-05 05:44:42 +08:00
|
|
|
/// Creates a new iterator for the given matrix storage.
|
|
|
|
pub fn new(storage: $SRef) -> $Name<'a, N, R, C, S> {
|
2018-10-22 13:00:10 +08:00
|
|
|
let shape = storage.shape();
|
2016-12-05 05:44:42 +08:00
|
|
|
let strides = storage.strides();
|
|
|
|
let inner_offset = shape.0.value() * strides.0.value();
|
2019-08-04 23:20:19 +08:00
|
|
|
let size = shape.0.value() * shape.1.value();
|
2016-12-05 05:44:42 +08:00
|
|
|
let ptr = storage.$ptr();
|
|
|
|
|
2019-08-04 23:20:19 +08:00
|
|
|
// If we have a size of 0, 'ptr' must be
|
|
|
|
// dangling. Howver, 'inner_offset' might
|
|
|
|
// not be zero if only one dimension is zero, so
|
|
|
|
// we don't want to call 'offset'.
|
|
|
|
// This pointer will never actually get used
|
|
|
|
// if our size is '0', so it's fine to use
|
|
|
|
// 'ptr' for both the start and end.
|
|
|
|
let inner_end = if size == 0 {
|
|
|
|
ptr
|
|
|
|
} else {
|
|
|
|
// Safety:
|
|
|
|
// If 'size' is non-zero, we know that 'ptr'
|
|
|
|
// is not dangling, and 'inner_offset' must lie
|
|
|
|
// within the allocation
|
2020-11-16 21:59:53 +08:00
|
|
|
unsafe { ptr.add(inner_offset) }
|
2019-08-04 23:20:19 +08:00
|
|
|
};
|
|
|
|
|
2016-12-05 05:44:42 +08:00
|
|
|
$Name {
|
2020-10-26 20:25:55 +08:00
|
|
|
ptr,
|
2016-12-05 05:44:42 +08:00
|
|
|
inner_ptr: ptr,
|
2019-08-04 23:20:19 +08:00
|
|
|
inner_end,
|
2018-10-22 13:00:10 +08:00
|
|
|
size: shape.0.value() * shape.1.value(),
|
2020-10-26 20:25:55 +08:00
|
|
|
strides,
|
2018-10-22 13:00:10 +08:00
|
|
|
_phantoms: PhantomData,
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + $Storage<N, R, C>> Iterator
|
2018-10-22 13:00:10 +08:00
|
|
|
for $Name<'a, N, R, C, S>
|
|
|
|
{
|
2016-12-05 05:44:42 +08:00
|
|
|
type Item = $Ref;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<$Ref> {
|
|
|
|
unsafe {
|
|
|
|
if self.size == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
self.size -= 1;
|
|
|
|
|
|
|
|
// Jump to the next outer dimension if needed.
|
|
|
|
if self.ptr == self.inner_end {
|
|
|
|
let stride = self.strides.1.value() as isize;
|
2019-11-22 06:22:00 +08:00
|
|
|
// This might go past the end of the allocation,
|
|
|
|
// depending on the value of 'size'. We use
|
|
|
|
// `wrapping_offset` to avoid UB
|
|
|
|
self.inner_end = self.ptr.wrapping_offset(stride);
|
|
|
|
// This will always be in bounds, since
|
|
|
|
// we're going to dereference it
|
2016-12-05 05:44:42 +08:00
|
|
|
self.ptr = self.inner_ptr.offset(stride);
|
|
|
|
self.inner_ptr = self.ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go to the next element.
|
|
|
|
let old = self.ptr;
|
|
|
|
|
2019-11-22 06:22:00 +08:00
|
|
|
// Don't offset `self.ptr` for the last element,
|
|
|
|
// as this will be out of bounds. Iteration is done
|
|
|
|
// at this point (the next call to `next` will return `None`)
|
|
|
|
// so this is not observable.
|
|
|
|
if self.size != 0 {
|
2020-11-16 21:59:53 +08:00
|
|
|
let stride = self.strides.0.value();
|
|
|
|
self.ptr = self.ptr.add(stride);
|
2019-11-22 06:22:00 +08:00
|
|
|
}
|
2016-12-05 05:44:42 +08:00
|
|
|
Some(mem::transmute(old))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.size, Some(self.size))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
|
|
|
self.size_hint().0
|
|
|
|
}
|
|
|
|
}
|
2017-08-03 01:37:44 +08:00
|
|
|
|
2020-11-20 01:35:43 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + $Storage<N, R, C>> DoubleEndedIterator
|
|
|
|
for $Name<'a, N, R, C, S>
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn next_back(&mut self) -> Option<$Ref> {
|
2020-11-20 03:27:59 +08:00
|
|
|
unsafe {
|
|
|
|
if self.size == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
// Pre-decrement `size` such that it now counts to the
|
|
|
|
// element we want to return.
|
|
|
|
self.size -= 1;
|
|
|
|
|
2020-11-20 04:43:29 +08:00
|
|
|
// Compute number of rows
|
|
|
|
let inner_size = self.inner_end.offset_from(self.inner_ptr) as usize;
|
|
|
|
|
|
|
|
// Compute rows and cols remaining
|
|
|
|
let outer_remaining = self.size / inner_size;
|
|
|
|
let inner_remaining = self.size % inner_size;
|
|
|
|
|
2020-11-20 03:27:59 +08:00
|
|
|
// Fetch strides
|
|
|
|
let inner_stride = self.strides.0.value();
|
|
|
|
let outer_stride = self.strides.1.value();
|
|
|
|
|
|
|
|
// Compute pointer to last element
|
|
|
|
let last = self.ptr.offset(
|
|
|
|
(outer_remaining * outer_stride + inner_remaining * inner_stride)
|
|
|
|
as isize,
|
|
|
|
);
|
|
|
|
|
|
|
|
// We want either `& *last` or `&mut *last` here, depending
|
|
|
|
// on the mutability of `$Ref`.
|
|
|
|
Some(mem::transmute(last))
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 01:35:43 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-20 05:00:28 +08:00
|
|
|
|
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + $Storage<N, R, C>> ExactSizeIterator
|
|
|
|
for $Name<'a, N, R, C, S>
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + $Storage<N, R, C>> FusedIterator
|
|
|
|
for $Name<'a, N, R, C, S>
|
|
|
|
{
|
|
|
|
}
|
2018-10-22 13:00:10 +08:00
|
|
|
};
|
2016-12-05 05:44:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator!(struct MatrixIter for Storage.ptr -> *const N, &'a N, &'a S);
|
|
|
|
iterator!(struct MatrixIterMut for StorageMut.ptr_mut -> *mut N, &'a mut N, &'a mut S);
|
2018-12-09 22:21:05 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Row iterators.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#[derive(Clone)]
|
2019-02-03 15:33:07 +08:00
|
|
|
/// An iterator through the rows of a matrix.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub struct RowIter<'a, N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> {
|
2018-12-09 22:21:05 +08:00
|
|
|
mat: &'a Matrix<N, R, C, S>,
|
2020-04-06 00:49:48 +08:00
|
|
|
curr: usize,
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + Storage<N, R, C>> RowIter<'a, N, R, C, S> {
|
2019-02-03 15:33:07 +08:00
|
|
|
pub(crate) fn new(mat: &'a Matrix<N, R, C, S>) -> Self {
|
2020-04-06 00:49:48 +08:00
|
|
|
RowIter { mat, curr: 0 }
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + Storage<N, R, C>> Iterator for RowIter<'a, N, R, C, S> {
|
2018-12-09 22:21:05 +08:00
|
|
|
type Item = MatrixSlice<'a, N, U1, C, S::RStride, S::CStride>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.curr < self.mat.nrows() {
|
|
|
|
let res = self.mat.row(self.curr);
|
|
|
|
self.curr += 1;
|
|
|
|
Some(res)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2020-04-06 00:49:48 +08:00
|
|
|
(
|
|
|
|
self.mat.nrows() - self.curr,
|
|
|
|
Some(self.mat.nrows() - self.curr),
|
|
|
|
)
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
|
|
|
self.mat.nrows() - self.curr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + Storage<N, R, C>> ExactSizeIterator
|
|
|
|
for RowIter<'a, N, R, C, S>
|
|
|
|
{
|
2018-12-09 22:21:05 +08:00
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.mat.nrows() - self.curr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:33:07 +08:00
|
|
|
/// An iterator through the mutable rows of a matrix.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub struct RowIterMut<'a, N: Scalar, R: Dim, C: Dim, S: StorageMut<N, R, C>> {
|
2018-12-09 22:21:05 +08:00
|
|
|
mat: *mut Matrix<N, R, C, S>,
|
|
|
|
curr: usize,
|
2020-04-06 00:49:48 +08:00
|
|
|
phantom: PhantomData<&'a mut Matrix<N, R, C, S>>,
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + StorageMut<N, R, C>> RowIterMut<'a, N, R, C, S> {
|
2019-02-03 15:33:07 +08:00
|
|
|
pub(crate) fn new(mat: &'a mut Matrix<N, R, C, S>) -> Self {
|
2018-12-09 22:21:05 +08:00
|
|
|
RowIterMut {
|
|
|
|
mat,
|
|
|
|
curr: 0,
|
2020-04-06 00:49:48 +08:00
|
|
|
phantom: PhantomData,
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nrows(&self) -> usize {
|
2020-04-06 00:49:48 +08:00
|
|
|
unsafe { (*self.mat).nrows() }
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + StorageMut<N, R, C>> Iterator
|
|
|
|
for RowIterMut<'a, N, R, C, S>
|
|
|
|
{
|
2018-12-09 22:21:05 +08:00
|
|
|
type Item = MatrixSliceMut<'a, N, U1, C, S::RStride, S::CStride>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.curr < self.nrows() {
|
|
|
|
let res = unsafe { (*self.mat).row_mut(self.curr) };
|
|
|
|
self.curr += 1;
|
|
|
|
Some(res)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.nrows() - self.curr, Some(self.nrows() - self.curr))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
|
|
|
self.nrows() - self.curr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + StorageMut<N, R, C>> ExactSizeIterator
|
|
|
|
for RowIterMut<'a, N, R, C, S>
|
|
|
|
{
|
2018-12-09 22:21:05 +08:00
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.nrows() - self.curr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Column iterators.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#[derive(Clone)]
|
2019-02-03 15:33:07 +08:00
|
|
|
/// An iterator through the columns of a matrix.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub struct ColumnIter<'a, N: Scalar, R: Dim, C: Dim, S: Storage<N, R, C>> {
|
2018-12-09 22:21:05 +08:00
|
|
|
mat: &'a Matrix<N, R, C, S>,
|
2020-04-06 00:49:48 +08:00
|
|
|
curr: usize,
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + Storage<N, R, C>> ColumnIter<'a, N, R, C, S> {
|
2019-02-03 15:33:07 +08:00
|
|
|
pub(crate) fn new(mat: &'a Matrix<N, R, C, S>) -> Self {
|
2020-04-06 00:49:48 +08:00
|
|
|
ColumnIter { mat, curr: 0 }
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + Storage<N, R, C>> Iterator
|
|
|
|
for ColumnIter<'a, N, R, C, S>
|
|
|
|
{
|
2018-12-09 22:21:05 +08:00
|
|
|
type Item = MatrixSlice<'a, N, R, U1, S::RStride, S::CStride>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.curr < self.mat.ncols() {
|
|
|
|
let res = self.mat.column(self.curr);
|
|
|
|
self.curr += 1;
|
|
|
|
Some(res)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
2020-04-06 00:49:48 +08:00
|
|
|
(
|
|
|
|
self.mat.ncols() - self.curr,
|
|
|
|
Some(self.mat.ncols() - self.curr),
|
|
|
|
)
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
|
|
|
self.mat.ncols() - self.curr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + Storage<N, R, C>> ExactSizeIterator
|
|
|
|
for ColumnIter<'a, N, R, C, S>
|
|
|
|
{
|
2018-12-09 22:21:05 +08:00
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.mat.ncols() - self.curr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:33:07 +08:00
|
|
|
/// An iterator through the mutable columns of a matrix.
|
2019-12-17 07:09:14 +08:00
|
|
|
pub struct ColumnIterMut<'a, N: Scalar, R: Dim, C: Dim, S: StorageMut<N, R, C>> {
|
2018-12-09 22:21:05 +08:00
|
|
|
mat: *mut Matrix<N, R, C, S>,
|
|
|
|
curr: usize,
|
2020-04-06 00:49:48 +08:00
|
|
|
phantom: PhantomData<&'a mut Matrix<N, R, C, S>>,
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
|
2019-12-17 07:09:14 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + StorageMut<N, R, C>> ColumnIterMut<'a, N, R, C, S> {
|
2019-02-03 15:33:07 +08:00
|
|
|
pub(crate) fn new(mat: &'a mut Matrix<N, R, C, S>) -> Self {
|
2018-12-09 22:21:05 +08:00
|
|
|
ColumnIterMut {
|
|
|
|
mat,
|
|
|
|
curr: 0,
|
2020-04-06 00:49:48 +08:00
|
|
|
phantom: PhantomData,
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ncols(&self) -> usize {
|
2020-04-06 00:49:48 +08:00
|
|
|
unsafe { (*self.mat).ncols() }
|
2018-12-09 22:21:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + StorageMut<N, R, C>> Iterator
|
|
|
|
for ColumnIterMut<'a, N, R, C, S>
|
|
|
|
{
|
2018-12-09 22:21:05 +08:00
|
|
|
type Item = MatrixSliceMut<'a, N, R, U1, S::RStride, S::CStride>;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
if self.curr < self.ncols() {
|
|
|
|
let res = unsafe { (*self.mat).column_mut(self.curr) };
|
|
|
|
self.curr += 1;
|
|
|
|
Some(res)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.ncols() - self.curr, Some(self.ncols() - self.curr))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(self) -> usize {
|
|
|
|
self.ncols() - self.curr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 00:49:48 +08:00
|
|
|
impl<'a, N: Scalar, R: Dim, C: Dim, S: 'a + StorageMut<N, R, C>> ExactSizeIterator
|
|
|
|
for ColumnIterMut<'a, N, R, C, S>
|
|
|
|
{
|
2018-12-09 22:21:05 +08:00
|
|
|
#[inline]
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.ncols() - self.curr
|
|
|
|
}
|
|
|
|
}
|