Skip to content

Commit 443266c

Browse files
committed
FEAT: Add new constructor from_shape_fn_memory_order
This is like from_shape_fn, but avoids the overhead of iterating the indices for the elements - instead elements are just visited in memory order.
1 parent a7d4988 commit 443266c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/impl_constructors.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,23 @@ where
335335
}
336336
}
337337

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+
338355
/// Create an array with the given shape from a vector. (No cloning of
339356
/// elements needed.)
340357
///

0 commit comments

Comments
 (0)