Skip to content

What is the standard way to bring arrays into standard order? #441

@ExpHP

Description

@ExpHP
Contributor

Suppose I have an arbitrary array; any model of ownership, any number of dimensions, possibly non-contiguous, with strides in whatever order. How do I get an owned array in standard layout? And could it be better documented?

Based on the documentation of ArrayBase alone, it seems to me that the following is the only safe bet:

Array::from_shape_vec(arr.raw_dim(), arr.iter().cloned().collect())

Activity

jturner314

jturner314 commented on Apr 27, 2018

@jturner314
Member

Yeah, that's the simplest way, I think. The other obvious things to try (.to_owned() and .map()/.mapv()) don't alter the layout of contiguous arrays, so they don't help in this case.

Something like this might be faster for handling contiguous arrays in standard layout, but you'd have to benchmark to know for sure:

if let Some(slc) = self.as_slice() {
    Array::from_shape_vec(arr.raw_dim(), slc.to_vec())
} else {
    Array::from_shape_vec(arr.raw_dim(), arr.iter().cloned().collect())
}

If your element type is Copy, an alternative approach is to allocate a new array with ::uninitialized() and then .assign() the data.

And could it be better documented?

I've created #442. What do you think?

jturner314

jturner314 commented on May 4, 2018

@jturner314
Member

Resolved by #442.

It would be nice in the future to provide a more concise way to convert between memory layouts, possibly using something like the Cow-like types described in #390.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ExpHP@jturner314

        Issue actions

          What is the standard way to bring arrays into standard order? · Issue #441 · rust-ndarray/ndarray