Skip to content

Commit 0e4729d

Browse files
committed
API: Rename from_shape_fn_memory_order to from_shape_simple_fn
And let the closure take no argument. The rationale is that if a counter is needed, it can easily be added by the caller. It's a duplicate counter then, but with luck it could be conflated with the other by the compiler? Use it in Array::default, the other usages are not significant.
1 parent 6372f1a commit 0e4729d

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

ndarray-rand/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ where
246246
R: Rng + ?Sized,
247247
Sh: ShapeBuilder<Dim = D>,
248248
{
249-
Self::from_shape_fn_memory_order(shape, |_| dist.sample(rng))
249+
Self::from_shape_simple_fn(shape, move || dist.sample(rng))
250250
}
251251

252252
fn sample_axis(&self, axis: Axis, n_samples: usize, strategy: SamplingStrategy) -> Array<A, D>

src/impl_constructors.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,28 @@ where
305305
where
306306
A: Default,
307307
Sh: ShapeBuilder<Dim = D>,
308+
{
309+
Self::from_shape_simple_fn(shape, A::default)
310+
}
311+
312+
/// Create an array with values created by the function `f`.
313+
///
314+
/// `f` is called with no argument, and it should return the element to
315+
/// create. If the precise index of the element to create is needed,
316+
/// use [`from_shape_fn`](ArrayBase::from_shape_fn) instead.
317+
///
318+
/// This constructor can be useful if the element order is not important,
319+
/// for example if they are identical or random.
320+
///
321+
/// **Panics** if the product of non-zero axis lengths overflows `isize`.
322+
pub fn from_shape_simple_fn<Sh, F>(shape: Sh, mut f: F) -> Self
323+
where
324+
Sh: ShapeBuilder<Dim = D>,
325+
F: FnMut() -> A,
308326
{
309327
let shape = shape.into_shape();
310-
let size = size_of_shape_checked_unwrap!(&shape.dim);
311-
let v = to_vec((0..size).map(|_| A::default()));
328+
let len = size_of_shape_checked_unwrap!(&shape.dim);
329+
let v = to_vec_mapped(0..len, move |_| f());
312330
unsafe { Self::from_shape_vec_unchecked(shape, v) }
313331
}
314332

@@ -335,23 +353,6 @@ where
335353
}
336354
}
337355

338-
/// Create an array with values created by the function `f`.
339-
///
340-
/// `f` is called with the *linear index* (one dimensional) of the element
341-
/// to create; the elements are visited in memory order.
342-
///
343-
/// **Panics** if the product of non-zero axis lengths overflows `isize`.
344-
pub fn from_shape_fn_memory_order<Sh, F>(shape: Sh, f: F) -> Self
345-
where
346-
Sh: ShapeBuilder<Dim = D>,
347-
F: FnMut(usize) -> A,
348-
{
349-
let shape = shape.into_shape();
350-
let len = size_of_shape_checked_unwrap!(&shape.dim);
351-
let v = to_vec_mapped(0..len, f);
352-
unsafe { Self::from_shape_vec_unchecked(shape, v) }
353-
}
354-
355356
/// Create an array with the given shape from a vector. (No cloning of
356357
/// elements needed.)
357358
///

src/impl_methods.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2187,7 +2187,7 @@ where
21872187
let view_stride = self.strides.axis(axis);
21882188
if view_len == 0 {
21892189
let new_dim = self.dim.remove_axis(axis);
2190-
Array::from_shape_fn(new_dim, move |_| mapping(ArrayView::from(&[])))
2190+
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayView::from(&[])))
21912191
} else {
21922192
// use the 0th subview as a map to each 1d array view extended from
21932193
// the 0th element.
@@ -2218,7 +2218,7 @@ where
22182218
let view_stride = self.strides.axis(axis);
22192219
if view_len == 0 {
22202220
let new_dim = self.dim.remove_axis(axis);
2221-
Array::from_shape_fn(new_dim, move |_| mapping(ArrayViewMut::from(&mut [])))
2221+
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayViewMut::from(&mut [])))
22222222
} else {
22232223
// use the 0th subview as a map to each 1d array view extended from
22242224
// the 0th element.

0 commit comments

Comments
 (0)