public static Array GetMultiDimensionalRange1(Array source, int[] ranges, bool[] compute_end, bool[] collapse)
{
int rankSrc = source.Rank;
int[] lensSrc = new int[rankSrc];
int collapseSize = 0;
for (int i = 0; i < rankSrc; i++)
{
ranges[2 * i] = NormalizeIndex(source.GetLength(i), ranges[2 * i]);
if (compute_end[i])
ranges[2*i + 1] = source.GetLength(i);
else
ranges[2 * i + 1] = NormalizeIndex(source.GetLength(i), ranges[2 * i + 1]);
lensSrc[i] = ranges[2 * i + 1] - ranges[2 * i];
collapseSize += collapse[i] ? 1 : 0;
}
int rankDest = rankSrc - collapseSize;
int[] lensDest = new int[rankDest];
int rankIndex = 0;
for (int i = 0; i < rankSrc; i++)
{
if (!collapse[i])
{
lensDest[rankIndex] = lensSrc[i];
rankIndex++;
}
}
if (rankDest == 0)
{
rankDest = 1;
lensDest = new int[1];
lensDest[0] = 1;
}
Array dest = Array.CreateInstance(source.GetType().GetElementType(), lensDest);
int[] modInd = new int[rankSrc];
int[] indicesDest = new int[rankDest];
int[] indicesSrc = new int[rankSrc];
for (int i = 0; i < rankSrc; i++)
{
if (i == 0)
modInd[i] = dest.Length;
else
modInd[i] = modInd[i - 1]/lensSrc[i - 1];
}
for (int i = 0; i < dest.Length; i++)
{
int destIndex = 0;
for (int j = 0; j < rankSrc; j++)
{
int index = (i % modInd[j]) / (modInd[j] / lensSrc[j]);
indicesSrc[j] = ranges[2 * j] + index;
if (!collapse[j])
{
indicesDest[destIndex] = indicesSrc[j] - ranges[2 * j];
destIndex++;
}
}
dest.SetValue(source.GetValue(indicesSrc), indicesDest);
}
return dest;
}