问题
I need to iterate over an array of arbitrary rank. This is for both reading and writing, so GetEnumerator
will not work.
Array.SetValue(object, int)
doesn't work on multidimensional arrays.
Array.SetValue(object, params int[])
would require excessive arithmetic for iterating through the multidimensional space. It would also require dynamic invocation to get around the params
part of the signature.
I'm tempted to pin the array and iterate over it with a pointer, but I can't find any documentation that says that multidimensional arrays are guaranteed to be contiguous. If they have padding at the end of a dimension then that won't work. I'd also prefer to avoid unsafe code.
Is there an easy way to sequentially address a multidimensional array using only a single index?
回答1:
Multidimensional arrays are guaranteed to be contiguous. From ECMA-335:
Array elements shall be laid out within the array object in row-major order (i.e., the elements associated with the rightmost array dimension shall be laid out contiguously from lowest to highest index).
So this works:
int[,,,] array = new int[10, 10, 10, 10];
fixed (int* ptr = array)
{
ptr[10] = 42;
}
int result = array[0, 0, 1, 0]; // == 42
回答2:
You can use the Rank
and GetUpperBound
property/method to create an indices array that you can pass to the array's SetValue
and GetValue
methods:
int[] Indices(Array a, int idx)
{
var indices = new int[a.Rank];
for (var i = 0; i < a.Rank; i++)
{
var div = 1;
for (var j = i + 1; j < a.Rank; j++)
{
div *= a.GetLength(j);
}
indices[i] = a.GetLowerBound(i) + idx / div % a.GetLength(i);
}
return indices;
}
..and use it like so:
for (var i = 0; i < array.Length; i++)
{
var indices = Indices(array, i);
array.SetValue(i, indices);
var val = array.GetValue(indices);
}
回答3:
Perhaps you could join them all into the a single temporary collection, and just iterate over that.
来源:https://stackoverflow.com/questions/3408655/indexing-into-arrays-of-arbitrary-rank-in-c-sharp