Skip to content

Commit 7ac0600

Browse files
authored
Merge pull request #460 from LukeMathWalker/map_axis_mut
Add map_mut and map_axis_mut
2 parents e3e9511 + 2bf5cc9 commit 7ac0600

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/impl_methods.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,6 +1694,34 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
16941694
}
16951695
}
16961696

1697+
/// Call `f` on a mutable reference of each element and create a new array
1698+
/// with the new values.
1699+
///
1700+
/// Elements are visited in arbitrary order.
1701+
///
1702+
/// Return an array with the same shape as `self`.
1703+
pub fn map_mut<'a, B, F>(&'a mut self, f: F) -> Array<B, D>
1704+
where F: FnMut(&'a mut A) -> B,
1705+
A: 'a,
1706+
S: DataMut
1707+
{
1708+
let dim = self.dim.clone();
1709+
if self.is_contiguous() {
1710+
let strides = self.strides.clone();
1711+
let slc = self.as_slice_memory_order_mut().unwrap();
1712+
let v = ::iterators::to_vec_mapped(slc.iter_mut(), f);
1713+
unsafe {
1714+
ArrayBase::from_shape_vec_unchecked(
1715+
dim.strides(strides), v)
1716+
}
1717+
} else {
1718+
let v = ::iterators::to_vec_mapped(self.iter_mut(), f);
1719+
unsafe {
1720+
ArrayBase::from_shape_vec_unchecked(dim, v)
1721+
}
1722+
}
1723+
}
1724+
16971725
/// Call `f` by **v**alue on each element and create a new array
16981726
/// with the new values.
16991727
///
@@ -1819,4 +1847,32 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
18191847
}
18201848
})
18211849
}
1850+
1851+
/// Reduce the values along an axis into just one value, producing a new
1852+
/// array with one less dimension.
1853+
/// 1-dimensional lanes are passed as mutable references to the reducer,
1854+
/// allowing for side-effects.
1855+
///
1856+
/// Elements are visited in arbitrary order.
1857+
///
1858+
/// Return the result as an `Array`.
1859+
///
1860+
/// **Panics** if `axis` is out of bounds.
1861+
pub fn map_axis_mut<'a, B, F>(&'a mut self, axis: Axis, mut mapping: F)
1862+
-> Array<B, D::Smaller>
1863+
where D: RemoveAxis,
1864+
F: FnMut(ArrayViewMut1<'a, A>) -> B,
1865+
A: 'a,
1866+
S: DataMut,
1867+
{
1868+
let view_len = self.len_of(axis);
1869+
let view_stride = self.strides.axis(axis);
1870+
// use the 0th subview as a map to each 1d array view extended from
1871+
// the 0th element.
1872+
self.subview_mut(axis, 0).map_mut(|first_elt: &mut A| {
1873+
unsafe {
1874+
mapping(ArrayViewMut::new_(first_elt, Ix1(view_len), Ix1(view_stride)))
1875+
}
1876+
})
1877+
}
18221878
}

src/iterators/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,9 +1180,11 @@ use indexes::IndicesIterF;
11801180

11811181
unsafe impl<F> TrustedIterator for Linspace<F> { }
11821182
unsafe impl<'a, A, D> TrustedIterator for Iter<'a, A, D> { }
1183+
unsafe impl<'a, A, D> TrustedIterator for IterMut<'a, A, D> { }
11831184
unsafe impl<I, F> TrustedIterator for std::iter::Map<I, F>
11841185
where I: TrustedIterator { }
11851186
unsafe impl<'a, A> TrustedIterator for slice::Iter<'a, A> { }
1187+
unsafe impl<'a, A> TrustedIterator for slice::IterMut<'a, A> { }
11861188
unsafe impl TrustedIterator for ::std::ops::Range<usize> { }
11871189
// FIXME: These indices iter are dubious -- size needs to be checked up front.
11881190
unsafe impl<D> TrustedIterator for IndicesIter<D> where D: Dimension { }

0 commit comments

Comments
 (0)