Skip to content

Commit

Permalink
API: Rename from_shape_fn_memory_order to from_shape_simple_fn
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bluss committed Oct 9, 2019
1 parent 6372f1a commit 0e4729d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ndarray-rand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ where
R: Rng + ?Sized,
Sh: ShapeBuilder<Dim = D>,
{
Self::from_shape_fn_memory_order(shape, |_| dist.sample(rng))
Self::from_shape_simple_fn(shape, move || dist.sample(rng))
}

fn sample_axis(&self, axis: Axis, n_samples: usize, strategy: SamplingStrategy) -> Array<A, D>
Expand Down
39 changes: 20 additions & 19 deletions src/impl_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,28 @@ where
where
A: Default,
Sh: ShapeBuilder<Dim = D>,
{
Self::from_shape_simple_fn(shape, A::default)
}

/// Create an array with values created by the function `f`.
///
/// `f` is called with no argument, and it should return the element to
/// create. If the precise index of the element to create is needed,
/// use [`from_shape_fn`](ArrayBase::from_shape_fn) instead.
///
/// This constructor can be useful if the element order is not important,
/// for example if they are identical or random.
///
/// **Panics** if the product of non-zero axis lengths overflows `isize`.
pub fn from_shape_simple_fn<Sh, F>(shape: Sh, mut f: F) -> Self
where
Sh: ShapeBuilder<Dim = D>,
F: FnMut() -> A,
{
let shape = shape.into_shape();
let size = size_of_shape_checked_unwrap!(&shape.dim);
let v = to_vec((0..size).map(|_| A::default()));
let len = size_of_shape_checked_unwrap!(&shape.dim);
let v = to_vec_mapped(0..len, move |_| f());
unsafe { Self::from_shape_vec_unchecked(shape, v) }
}

Expand All @@ -335,23 +353,6 @@ where
}
}

/// Create an array with values created by the function `f`.
///
/// `f` is called with the *linear index* (one dimensional) of the element
/// to create; the elements are visited in memory order.
///
/// **Panics** if the product of non-zero axis lengths overflows `isize`.
pub fn from_shape_fn_memory_order<Sh, F>(shape: Sh, f: F) -> Self
where
Sh: ShapeBuilder<Dim = D>,
F: FnMut(usize) -> A,
{
let shape = shape.into_shape();
let len = size_of_shape_checked_unwrap!(&shape.dim);
let v = to_vec_mapped(0..len, f);
unsafe { Self::from_shape_vec_unchecked(shape, v) }
}

/// Create an array with the given shape from a vector. (No cloning of
/// elements needed.)
///
Expand Down
4 changes: 2 additions & 2 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2187,7 +2187,7 @@ where
let view_stride = self.strides.axis(axis);
if view_len == 0 {
let new_dim = self.dim.remove_axis(axis);
Array::from_shape_fn(new_dim, move |_| mapping(ArrayView::from(&[])))
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayView::from(&[])))
} else {
// use the 0th subview as a map to each 1d array view extended from
// the 0th element.
Expand Down Expand Up @@ -2218,7 +2218,7 @@ where
let view_stride = self.strides.axis(axis);
if view_len == 0 {
let new_dim = self.dim.remove_axis(axis);
Array::from_shape_fn(new_dim, move |_| mapping(ArrayViewMut::from(&mut [])))
Array::from_shape_simple_fn(new_dim, move || mapping(ArrayViewMut::from(&mut [])))
} else {
// use the 0th subview as a map to each 1d array view extended from
// the 0th element.
Expand Down

0 comments on commit 0e4729d

Please sign in to comment.