Skip to content

Commit b22e2b1

Browse files
committed
FEAT: Add new methods .assign_into() and .map_assign_into
These do essentially the same job as Zip::from(a).map_assign_into(f, b) Comparison with existing assign: - Reverse argument order: source, destination - These new methods don't broadcast. - These new methods are generic over AssignElem (assign into &Cell, &mut ManuallyUninit etc).
1 parent 09884fc commit b22e2b1

File tree

5 files changed

+41
-49
lines changed

5 files changed

+41
-49
lines changed

src/impl_constructors.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -531,30 +531,14 @@ where
531531
/// // two first columns in b are two last in a
532532
/// // rest of columns in b are the initial columns in a
533533
///
534-
/// assign_to(a.slice(s![.., -2..]), b.slice_mut(s![.., ..2]));
535-
/// assign_to(a.slice(s![.., 2..]), b.slice_mut(s![.., ..-2]));
534+
/// a.slice(s![.., -2..]).assign_into(b.slice_mut(s![.., ..2]));
535+
/// a.slice(s![.., 2..]).assign_into(b.slice_mut(s![.., ..-2]));
536536
///
537537
/// // Now we can promise that `b` is safe to use with all operations
538538
/// unsafe {
539539
/// b.assume_init()
540540
/// }
541541
/// }
542-
///
543-
/// use ndarray::{IntoNdProducer, AssignElem};
544-
///
545-
/// // This function clones elements from the first input to the second;
546-
/// // the two producers must have the same shape
547-
/// fn assign_to<'a, P1, P2, A>(from: P1, to: P2)
548-
/// where P1: IntoNdProducer<Item = &'a A>,
549-
/// P2: IntoNdProducer<Dim = P1::Dim>,
550-
/// P2::Item: AssignElem<A>,
551-
/// A: Clone + 'a
552-
/// {
553-
/// Zip::from(from)
554-
/// .map_assign_into(to, A::clone);
555-
/// }
556-
///
557-
/// # shift_by_two(&Array2::zeros((8, 8)));
558542
/// ```
559543
pub fn uninit<Sh>(shape: Sh) -> ArrayBase<S::MaybeUninit, D>
560544
where

src/impl_methods.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rawpointer::PointerExt;
1515
use crate::imp_prelude::*;
1616

1717
use crate::{arraytraits, DimMax};
18+
use crate::argument_traits::AssignElem;
1819
use crate::dimension;
1920
use crate::dimension::IntoDimension;
2021
use crate::dimension::{
@@ -25,7 +26,7 @@ use crate::dimension::broadcast::co_broadcast;
2526
use crate::error::{self, ErrorKind, ShapeError, from_kind};
2627
use crate::math_cell::MathCell;
2728
use crate::itertools::zip;
28-
use crate::zip::Zip;
29+
use crate::zip::{IntoNdProducer, Zip};
2930
use crate::AxisDescription;
3031

3132
use crate::iter::{
@@ -2049,6 +2050,41 @@ where
20492050
self.zip_mut_with(rhs, |x, y| *x = y.clone());
20502051
}
20512052

2053+
/// Perform an elementwise assigment of values from `self` into array or producer `to`.
2054+
///
2055+
/// `to` can be another array or a producer of assignable elements.
2056+
/// [`AssignElem`] determines how elements are assigned.
2057+
///
2058+
/// **Panics** if shapes disagree.
2059+
pub fn assign_into<P>(&self, to: P)
2060+
where
2061+
S: Data,
2062+
P: IntoNdProducer<Dim = D>,
2063+
P::Item: AssignElem<A>,
2064+
A: Clone,
2065+
{
2066+
self.map_assign_into(A::clone, to)
2067+
}
2068+
2069+
/// Perform an elementwise assigment of values mapped with `f` from `self` into array or
2070+
/// producer `to`.
2071+
///
2072+
/// `to` can be another array or a producer of assignable elements.
2073+
/// [`AssignElem`] determines how elements are assigned.
2074+
///
2075+
/// **Panics** if shapes disagree.
2076+
pub fn map_assign_into<'a, F, B, P>(&'a self, f: F, to: P)
2077+
where
2078+
S: Data,
2079+
F: FnMut(&'a A) -> B,
2080+
P: IntoNdProducer<Dim = D>,
2081+
P::Item: AssignElem<B>,
2082+
A: 'a,
2083+
{
2084+
Zip::from(self)
2085+
.map_assign_into(to, f);
2086+
}
2087+
20522088
/// Perform an elementwise assigment to `self` from element `x`.
20532089
pub fn fill(&mut self, x: A)
20542090
where

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ mod shape_builder;
206206
mod slice;
207207
mod split_at;
208208
mod stacking;
209-
mod traversal_utils;
210209
#[macro_use]
211210
mod zip;
212211

src/stacking.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
use crate::error::{from_kind, ErrorKind, ShapeError};
1010
use crate::imp_prelude::*;
11-
use crate::traversal_utils::assign_to;
1211

1312
/// Stack arrays along the new axis.
1413
///
@@ -99,7 +98,7 @@ where
9998
for array in arrays {
10099
let len = array.len_of(axis);
101100
let (front, rest) = assign_view.split_at(axis, len);
102-
assign_to(array, front);
101+
array.assign_into(front);
103102
assign_view = rest;
104103
}
105104
debug_assert_eq!(assign_view.len(), 0);
@@ -171,7 +170,7 @@ where
171170
// but same number of axes).
172171
let assign_view = assign_view.into_dimensionality::<D>()
173172
.expect("same-dimensionality cast");
174-
assign_to(array, assign_view);
173+
array.assign_into(assign_view);
175174
});
176175

177176
unsafe {

src/traversal_utils.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)