Skip to content

Commit e870f63

Browse files
committed
FEAT: Add new method .assign_to()
This method does the same job as Zip::from(a).map_assign_into(f, b) with cloning as the function f. Comparison with existing assign: - Reverse argument order: source, destination - The new method doesn't broadcast. - The new method is generic over AssignElem (assign into &Cell, &mut ManuallyUninit etc).
1 parent 09884fc commit e870f63

File tree

7 files changed

+57
-74
lines changed

7 files changed

+57
-74
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_to(b.slice_mut(s![.., ..2]));
535+
/// a.slice(s![.., 2..]).assign_to(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: 19 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,23 @@ where
20492050
self.zip_mut_with(rhs, |x, y| *x = y.clone());
20502051
}
20512052

2053+
/// Perform an elementwise assigment of values cloned from `self` into array or producer `to`.
2054+
///
2055+
/// The destination `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_to<P>(&self, to: P)
2060+
where
2061+
S: Data,
2062+
P: IntoNdProducer<Dim = D>,
2063+
P::Item: AssignElem<A>,
2064+
A: Clone,
2065+
{
2066+
Zip::from(self)
2067+
.map_assign_into(to, A::clone);
2068+
}
2069+
20522070
/// Perform an elementwise assigment to `self` from element `x`.
20532071
pub fn fill(&mut self, x: A)
20542072
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_to(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_to(assign_view);
175174
});
176175

177176
unsafe {

src/traversal_utils.rs

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

tests/array.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -900,31 +900,6 @@ fn standard_layout() {
900900
assert!(x4.is_standard_layout());
901901
}
902902

903-
#[test]
904-
fn assign() {
905-
let mut a = arr2(&[[1., 2.], [3., 4.]]);
906-
let b = arr2(&[[1., 3.], [2., 4.]]);
907-
a.assign(&b);
908-
assert_eq!(a, b);
909-
910-
/* Test broadcasting */
911-
a.assign(&ArcArray::zeros(1));
912-
assert_eq!(a, ArcArray::zeros((2, 2)));
913-
914-
/* Test other type */
915-
a.assign(&Array::from_elem((2, 2), 3.));
916-
assert_eq!(a, ArcArray::from_elem((2, 2), 3.));
917-
918-
/* Test mut view */
919-
let mut a = arr2(&[[1, 2], [3, 4]]);
920-
{
921-
let mut v = a.view_mut();
922-
v.slice_collapse(s![..1, ..]);
923-
v.fill(0);
924-
}
925-
assert_eq!(a, arr2(&[[0, 0], [3, 4]]));
926-
}
927-
928903
#[test]
929904
fn iter_size_hint() {
930905
let mut a = arr2(&[[1., 2.], [3., 4.]]);

tests/higher_order_f.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,37 @@ fn test_fold_axis_oob() {
66
let a = arr2(&[[1., 2.], [3., 4.]]);
77
a.fold_axis(Axis(2), 0., |x, y| x + y);
88
}
9+
10+
#[test]
11+
fn assign() {
12+
let mut a = arr2(&[[1., 2.], [3., 4.]]);
13+
let b = arr2(&[[1., 3.], [2., 4.]]);
14+
a.assign(&b);
15+
assert_eq!(a, b);
16+
17+
/* Test broadcasting */
18+
a.assign(&ArcArray::zeros(1));
19+
assert_eq!(a, ArcArray::zeros((2, 2)));
20+
21+
/* Test other type */
22+
a.assign(&Array::from_elem((2, 2), 3.));
23+
assert_eq!(a, ArcArray::from_elem((2, 2), 3.));
24+
25+
/* Test mut view */
26+
let mut a = arr2(&[[1, 2], [3, 4]]);
27+
{
28+
let mut v = a.view_mut();
29+
v.slice_collapse(s![..1, ..]);
30+
v.fill(0);
31+
}
32+
assert_eq!(a, arr2(&[[0, 0], [3, 4]]));
33+
}
34+
35+
36+
#[test]
37+
fn assign_to() {
38+
let mut a = arr2(&[[1., 2.], [3., 4.]]);
39+
let b = arr2(&[[0., 3.], [2., 0.]]);
40+
b.assign_to(&mut a);
41+
assert_eq!(a, b);
42+
}

0 commit comments

Comments
 (0)