Skip to content

Rename Zip::apply to for_each including other apply methods #894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion examples/sort-axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ where
let perm_i = perm.indices[i];
Zip::from(result.index_axis_mut(axis, perm_i))
.and(self.index_axis(axis, i))
.apply(|to, from| {
.for_each(|to, from| {
copy_nonoverlapping(from, to.as_mut_ptr(), 1);
moved_elements += 1;
});
Expand Down
4 changes: 2 additions & 2 deletions examples/zip_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ fn main() {
// Let's imagine we split to parallelize
{
let (x, y) = Zip::indexed(&mut a).split();
x.apply(|(_, j), elt| {
x.for_each(|(_, j), elt| {
*elt = elt.powi(j as i32);
});

y.apply(|(_, j), elt| {
y.for_each(|(_, j), elt| {
*elt = elt.powi(j as i32);
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1985,7 +1985,7 @@ where
let dim = self.raw_dim();
Zip::from(LanesMut::new(self.view_mut(), Axis(n - 1)))
.and(Lanes::new(rhs.broadcast_assume(dim), Axis(n - 1)))
.apply(move |s_row, r_row| Zip::from(s_row).and(r_row).apply(|a, b| f(a, b)));
.for_each(move |s_row, r_row| Zip::from(s_row).and(r_row).for_each(|a, b| f(a, b)));
}

fn zip_mut_with_elem<B, F>(&mut self, rhs_elem: &B, mut f: F)
Expand Down Expand Up @@ -2343,7 +2343,7 @@ where
prev.slice_axis_inplace(axis, Slice::from(..-1));
curr.slice_axis_inplace(axis, Slice::from(1..));
// This implementation relies on `Zip` iterating along `axis` in order.
Zip::from(prev).and(curr).apply(|prev, curr| unsafe {
Zip::from(prev).and(curr).for_each(|prev, curr| unsafe {
// These pointer dereferences and borrows are safe because:
//
// 1. They're pointers to elements in the array.
Expand Down
4 changes: 2 additions & 2 deletions src/linalg/impl_linalg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,11 +679,11 @@ unsafe fn general_mat_vec_mul_impl<A, S1, S2>(

if beta.is_zero() {
// when beta is zero, c may be uninitialized
Zip::from(a.outer_iter()).and(y).apply(|row, elt| {
Zip::from(a.outer_iter()).and(y).for_each(|row, elt| {
elt.write(row.dot(x) * alpha);
});
} else {
Zip::from(a.outer_iter()).and(y).apply(|row, elt| {
Zip::from(a.outer_iter()).and(y).for_each(|row, elt| {
*elt = *elt * beta + row.dot(x) * alpha;
});
}
Expand Down
50 changes: 45 additions & 5 deletions src/parallel/impl_par_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,25 @@ macro_rules! zip_impl {
D: Dimension,
$($p: NdProducer<Dim=D> ,)*
{
/// The `par_for_each` method for `Zip`.
///
/// This is a shorthand for using `.into_par_iter().for_each()` on
/// `Zip`.
///
/// Requires crate feature `rayon`.
pub fn par_for_each<F>(self, function: F)
where F: Fn($($p::Item),*) + Sync + Send
{
self.into_par_iter().for_each(move |($($p,)*)| function($($p),*))
}

/// The `par_apply` method for `Zip`.
///
/// This is a shorthand for using `.into_par_iter().for_each()` on
/// `Zip`.
///
/// Requires crate feature `rayon`.
#[deprecated(note="Renamed to .par_for_each()", since="0.15.0")]
pub fn par_apply<F>(self, function: F)
where F: Fn($($p::Item),*) + Sync + Send
{
Expand All @@ -73,11 +86,11 @@ macro_rules! zip_impl {

expand_if!(@bool [$notlast]

/// Apply and collect the results into a new array, which has the same size as the
/// Map and collect the results into a new array, which has the same size as the
/// inputs.
///
/// If all inputs are c- or f-order respectively, that is preserved in the output.
pub fn par_apply_collect<R>(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
pub fn par_map_collect<R>(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
-> Array<R, D>
where R: Send
{
Expand Down Expand Up @@ -122,21 +135,48 @@ macro_rules! zip_impl {
}
}

/// Apply and assign the results into the producer `into`, which should have the same
/// Map and collect the results into a new array, which has the same size as the
/// inputs.
///
/// If all inputs are c- or f-order respectively, that is preserved in the output.
#[deprecated(note="Renamed to .par_map_collect()", since="0.15.0")]
pub fn par_apply_collect<R>(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
-> Array<R, D>
where R: Send
{
self.par_map_collect(f)
}

/// Map and assign the results into the producer `into`, which should have the same
/// size as the other inputs.
///
/// The producer should have assignable items as dictated by the `AssignElem` trait,
/// for example `&mut R`.
pub fn par_apply_assign_into<R, Q>(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
pub fn par_map_assign_into<R, Q>(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
where Q: IntoNdProducer<Dim=D>,
Q::Item: AssignElem<R> + Send,
Q::Output: Send,
{
self.and(into)
.par_apply(move |$($p, )* output_| {
.par_for_each(move |$($p, )* output_| {
output_.assign_elem(f($($p ),*));
});
}

/// Apply and assign the results into the producer `into`, which should have the same
/// size as the other inputs.
///
/// The producer should have assignable items as dictated by the `AssignElem` trait,
/// for example `&mut R`.
#[deprecated(note="Renamed to .par_map_assign_into()", since="0.15.0")]
pub fn par_apply_assign_into<R, Q>(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
where Q: IntoNdProducer<Dim=D>,
Q::Item: AssignElem<R> + Send,
Q::Output: Send,
{
self.par_map_assign_into(into, f)
}

);
}
)+
Expand Down
4 changes: 2 additions & 2 deletions src/parallel/zipmacro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/// Is equivalent to:
///
/// ```rust,ignore
/// Zip::from(&mut a).and(&b).and(&c).par_apply(|a, &b, &c| {
/// Zip::from(&mut a).and(&b).and(&c).par_for_each(|a, &b, &c| {
/// *a = b + c;
/// });
/// ```
Expand Down Expand Up @@ -54,6 +54,6 @@
/// ```
macro_rules! par_azip {
($($t:tt)*) => {
$crate::azip!(@build par_apply $($t)*)
$crate::azip!(@build par_for_each $($t)*)
};
}
2 changes: 1 addition & 1 deletion src/traversal_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ pub(crate) fn assign_to<'a, P1, P2, A>(from: P1, to: P2)
A: Clone + 'a
{
Zip::from(from)
.apply_assign_into(to, A::clone);
.map_assign_into(to, A::clone);
}

78 changes: 54 additions & 24 deletions src/zip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl<A, D: Dimension> NdProducer for RawArrayViewMut<A, D> {
/// The order elements are visited is not specified. The producers don’t have to
/// have the same item type.
///
/// The `Zip` has two methods for function application: `apply` and
/// The `Zip` has two methods for function application: `for_each` and
/// `fold_while`. The zip object can be split, which allows parallelization.
/// A read-only zip object (no mutable producers) can be cloned.
///
Expand Down Expand Up @@ -546,7 +546,7 @@ impl<A, D: Dimension> NdProducer for RawArrayViewMut<A, D> {
/// .and(&b)
/// .and(&c)
/// .and(&d)
/// .apply(|w, &x, &y, &z| {
/// .for_each(|w, &x, &y, &z| {
/// *w += x + y * z;
/// });
///
Expand All @@ -563,7 +563,7 @@ impl<A, D: Dimension> NdProducer for RawArrayViewMut<A, D> {
///
/// Zip::from(&mut totals)
/// .and(a.rows())
/// .apply(|totals, row| *totals = row.sum());
/// .for_each(|totals, row| *totals = row.sum());
///
/// // Check the result against the built in `.sum_axis()` along axis 1.
/// assert_eq!(totals, a.sum_axis(Axis(1)));
Expand Down Expand Up @@ -692,21 +692,21 @@ impl<P, D> Zip<P, D>
where
D: Dimension,
{
fn apply_core<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
fn for_each_core<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
where
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
P: ZippableTuple<Dim = D>,
{
if self.dimension.ndim() == 0 {
function(acc, unsafe { self.parts.as_ref(self.parts.as_ptr()) })
} else if self.layout.is(CORDER | FORDER) {
self.apply_core_contiguous(acc, function)
self.for_each_core_contiguous(acc, function)
} else {
self.apply_core_strided(acc, function)
self.for_each_core_strided(acc, function)
}
}

fn apply_core_contiguous<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
fn for_each_core_contiguous<F, Acc>(&mut self, acc: Acc, mut function: F) -> FoldWhile<Acc>
where
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
P: ZippableTuple<Dim = D>,
Expand Down Expand Up @@ -744,7 +744,7 @@ where
}


fn apply_core_strided<F, Acc>(&mut self, acc: Acc, function: F) -> FoldWhile<Acc>
fn for_each_core_strided<F, Acc>(&mut self, acc: Acc, function: F) -> FoldWhile<Acc>
where
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
P: ZippableTuple<Dim = D>,
Expand All @@ -754,14 +754,14 @@ where
panic!("Unreachable: ndim == 0 is contiguous")
}
if n == 1 || self.layout_tendency >= 0 {
self.apply_core_strided_c(acc, function)
self.for_each_core_strided_c(acc, function)
} else {
self.apply_core_strided_f(acc, function)
self.for_each_core_strided_f(acc, function)
}
}

// Non-contiguous but preference for C - unroll over Axis(ndim - 1)
fn apply_core_strided_c<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
fn for_each_core_strided_c<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
where
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
P: ZippableTuple<Dim = D>,
Expand All @@ -785,7 +785,7 @@ where
}

// Non-contiguous but preference for F - unroll over Axis(0)
fn apply_core_strided_f<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
fn for_each_core_strided_f<F, Acc>(&mut self, mut acc: Acc, mut function: F) -> FoldWhile<Acc>
where
F: FnMut(Acc, P::Item) -> FoldWhile<Acc>,
P: ZippableTuple<Dim = D>,
Expand Down Expand Up @@ -939,15 +939,24 @@ macro_rules! map_impl {
{
/// Apply a function to all elements of the input arrays,
/// visiting elements in lock step.
pub fn apply<F>(mut self, mut function: F)
pub fn for_each<F>(mut self, mut function: F)
where F: FnMut($($p::Item),*)
{
self.apply_core((), move |(), args| {
self.for_each_core((), move |(), args| {
let ($($p,)*) = args;
FoldWhile::Continue(function($($p),*))
});
}

/// Apply a function to all elements of the input arrays,
/// visiting elements in lock step.
#[deprecated(note="Renamed to .for_each()", since="0.15.0")]
pub fn apply<F>(self, function: F)
where F: FnMut($($p::Item),*)
{
self.for_each(function)
}

/// Apply a fold function to all elements of the input arrays,
/// visiting elements in lock step.
///
Expand Down Expand Up @@ -980,7 +989,7 @@ macro_rules! map_impl {
where
F: FnMut(Acc, $($p::Item),*) -> Acc,
{
self.apply_core(acc, move |acc, args| {
self.for_each_core(acc, move |acc, args| {
let ($($p,)*) = args;
FoldWhile::Continue(function(acc, $($p),*))
}).into_inner()
Expand All @@ -993,7 +1002,7 @@ macro_rules! map_impl {
-> FoldWhile<Acc>
where F: FnMut(Acc, $($p::Item),*) -> FoldWhile<Acc>
{
self.apply_core(acc, move |acc, args| {
self.for_each_core(acc, move |acc, args| {
let ($($p,)*) = args;
function(acc, $($p),*)
})
Expand All @@ -1015,7 +1024,7 @@ macro_rules! map_impl {
pub fn all<F>(mut self, mut predicate: F) -> bool
where F: FnMut($($p::Item),*) -> bool
{
!self.apply_core((), move |_, args| {
!self.for_each_core((), move |_, args| {
let ($($p,)*) = args;
if predicate($($p),*) {
FoldWhile::Continue(())
Expand Down Expand Up @@ -1065,12 +1074,11 @@ macro_rules! map_impl {
}
}

/// Apply and collect the results into a new array, which has the same size as the
/// Map and collect the results into a new array, which has the same size as the
/// inputs.
///
/// If all inputs are c- or f-order respectively, that is preserved in the output.
pub fn apply_collect<R>(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array<R, D>
{
pub fn map_collect<R>(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array<R, D> {
// Make uninit result
let mut output = self.uninitalized_for_current_layout::<R>();

Expand All @@ -1086,21 +1094,43 @@ macro_rules! map_impl {
}
}

/// Apply and assign the results into the producer `into`, which should have the same
/// Map and collect the results into a new array, which has the same size as the
/// inputs.
///
/// If all inputs are c- or f-order respectively, that is preserved in the output.
#[deprecated(note="Renamed to .map_collect()", since="0.15.0")]
pub fn apply_collect<R>(self, f: impl FnMut($($p::Item,)* ) -> R) -> Array<R, D> {
self.map_collect(f)
}

/// Map and assign the results into the producer `into`, which should have the same
/// size as the other inputs.
///
/// The producer should have assignable items as dictated by the `AssignElem` trait,
/// for example `&mut R`.
pub fn apply_assign_into<R, Q>(self, into: Q, mut f: impl FnMut($($p::Item,)* ) -> R)
pub fn map_assign_into<R, Q>(self, into: Q, mut f: impl FnMut($($p::Item,)* ) -> R)
where Q: IntoNdProducer<Dim=D>,
Q::Item: AssignElem<R>
{
self.and(into)
.apply(move |$($p, )* output_| {
.for_each(move |$($p, )* output_| {
output_.assign_elem(f($($p ),*));
});
}

/// Map and assign the results into the producer `into`, which should have the same
/// size as the other inputs.
///
/// The producer should have assignable items as dictated by the `AssignElem` trait,
/// for example `&mut R`.
#[deprecated(note="Renamed to .map_assign_into()", since="0.15.0")]
pub fn apply_assign_into<R, Q>(self, into: Q, f: impl FnMut($($p::Item,)* ) -> R)
where Q: IntoNdProducer<Dim=D>,
Q::Item: AssignElem<R>
{
self.map_assign_into(into, f)
}


);

Expand Down Expand Up @@ -1150,7 +1180,7 @@ macro_rules! map_impl {
// Apply the mapping function on this zip
// if we panic with unwinding; Partial will drop the written elements.
let partial_len = &mut partial.len;
self.apply(move |$($p,)* output_elem: *mut R| {
self.for_each(move |$($p,)* output_elem: *mut R| {
output_elem.write(f($($p),*));
if std::mem::needs_drop::<R>() {
*partial_len += 1;
Expand Down
Loading