Skip to content

How to create an array with a dimension that is generic? #1158

Answered by jturner314
AdityaGudimella asked this question in Q&A
Discussion options

You must be logged in to vote

The issue is that the Array::zeros(shape) method requires the shape argument to be compatible with the dimension type, but Vec<usize> as a shape is compatible only with IxDyn.

A good alternative is to create an empty shape using D::zeros(ndim), and then assign the axis lengths to the shape. Here's an example:

impl<S, D> CircularBufferND<S, D>
    where
        S: Clone + Zero,
        D: Dimension + RemoveAxis
{
    fn new(capacity: usize, element_shape: Vec<usize>) -> Self {
        let ndim = 1 + element_shape.len();
        let mut final_shape: D = D::zeros(ndim);
        final_shape[0] = capacity;
        final_shape.as_array_view_mut().slice_mut(s![1..]).assign(&ArrayView::from(&elem…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@AdityaGudimella
Comment options

Answer selected by AdityaGudimella
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants