Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ExpHP opened this issue Apr 27, 2018 · 2 comments
Closed

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

ExpHP opened this issue Apr 27, 2018 · 2 comments

Comments

@ExpHP
Copy link
Contributor

ExpHP commented Apr 27, 2018

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())
@jturner314
Copy link
Member

jturner314 commented Apr 27, 2018

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
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants