Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/dimension/dimension_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ pub trait Dimension:
#[doc(hidden)]
/// Iteration -- Use self as size, and return next index after `index`
/// or None if there are no more.
// FIXME: use &Self for index or even &mut?
#[inline]
fn next_for(&self, index: Self) -> Option<Self>
{
Expand All @@ -217,6 +216,24 @@ pub trait Dimension:
}
}

#[doc(hidden)]
/// Iteration -- Similar to `next_for`, but addresses the index as mutable reference.
#[inline]
fn next_for_mut(&self, index: &mut Self) -> bool
{
let mut end_iteration = true;
for (&dim, ix) in zip(self.slice(), index.slice_mut()).rev() {
*ix += 1;
if *ix == dim {
*ix = 0;
} else {
end_iteration = false;
break;
}
}
!end_iteration
}

#[doc(hidden)]
/// Iteration -- Use self as size, and create the next index after `index`
/// Return false if iteration is done
Expand Down
6 changes: 4 additions & 2 deletions src/iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ impl<A, D: Dimension> Iterator for Baseiter<A, D>
{
let index = match self.index {
None => return None,
Some(ref ix) => ix.clone(),
Some(ref mut ix) => ix,
};
let offset = D::stride_offset(&index, &self.strides);
self.index = self.dim.next_for(index);
if !self.dim.next_for_mut(index) {
self.index = None;
}
unsafe { Some(self.ptr.offset(offset)) }
}

Expand Down