There are may operations on arrays that do not depend on the rank of an array. Iterators are also not always a suitable solution. Given the array
double[,] myarray = new double[10,5];
it would be desirable to realize the following workflow:
- Reshape an array of Rank>1 to a linear array with rank=1 with the same number of elements. This should happen in place to be runtime efficient. Copying is not allowed.
- Pass reshaped array to a method defined for Rank=1 arrays only. e.g. Array.copy()
- Reshape result array to original rank and dimensions.
There is a similar question on this topic: How to reshape array in c#. The solutions there use memory copy operation with BlockCopy().
My question are: Can this kind of reshaping be realized without memory copy? Or even in a temporary way like creating a new view on the data?
fixed (double* myflatarray = myarray)), but you won't have an array that can be passed to other functions. If you write your functions to acceptSpan<double>instead of arrays, you can make the pointer "safe" (new Span<double>(myflatarray, myarray.Length)) but you still won't have an actual array. (And that's just for going to rank 1 -- I have no idea how you'd get back.)