4.4 Determining the Current Element Index Within a foreach Loop

 <  Day Day Up  >  

4.4 Determining the Current Element Index Within a foreach Loop

You want to avoid using a series of for loops to iterate the elements of an array, but you also need to know the location (index values) of an element in multiple dimensions of an array.

Technique

The System.Array class that serves as the base object for all arrays contains a few properties and methods to help you examine various attributes about an array. They include the Rank property, which tells you how many dimensions an array has, and the GetUpperBound method to determine the maximum number of elements within a certain dimension. Using this information, you can create an algorithm that calculates the index of each dimension given a single flat index value:

 
 static int[] GetDimensionIndices( int flatIndex, Array array ) {     int[] indices = new int[ array.Rank ];     int p = 1;     for( int i = array.Rank-1; i >= 0; i-- )     {         indices[i] = (((flatIndex/p)) % (array.GetUpperBound(i)+1));         if( i > 0)         {             p *= array.GetUpperBound( i )+1;         }     }     return indices; } 

Comments

This challenge wasn't a trivial thing to figure out. If there were ever an ultimate interview question, this problem is it. Given a multidimensional array that has been flattened out into a single-dimension array, determine the indices of each dimension given only the single-dimension array index. Let's take a look at the algorithm.

The first thing you see is the creation of an integer array that contains the amount of elements as specified by the rank of the array being passed in. In other words, if a three-dimensional array is passed in, a separate array that holds three values is created. This array corresponds to the indices of each dimension the element belongs to and is used as the return object.

Next, you see the declaration of a variable named p , which is initialized to 1. This variable is known as the period of a certain dimension. A period is how long it takes a single dimension's index to change when the lower indices are enumerated. You can find a dimension's period by multiplying the upper bounds of the dimensions following it. For instance, given an array such as

 
 int[,,] array[3, 2, 3] 

the third dimension's period is 1 because no dimensions follow it. The second dimension's period is 3, and the first dimension's period is 2 x 3, which is 6. In effect, this means that for every six elements, the index of that dimension will change.

A for loop loops based on the number of dimensions. As mentioned earlier, you can retrieve the number of dimensions using the Rank property of the Array object. For a three-dimensional array, the loop body will execute three times. Also, the for loop counter is initialized to the rightmost dimension and decrements upon each subsequent iteration of the loop, working back toward the leftmost dimension.

Next is the calculation of the index of the element in the current dimension. Using the information about periods and the upper bound of the current dimension, you can create an equation to calculate the index of that dimension. This equation takes the original single-dimension index variable and divides that by the current period. The resultant value is used with the modulus operator on the current upper bound of that dimension (plus 1 to compensate for the zero-based array index). The process is then repeated for the remaining dimensions after the new period value for that dimension is calculated. Figure 4.1 shows the result of using a foreach loop to print out an array and also calculating the dimension indices for each element. The array is a three-dimensional string array, where each string is simply initialized to the value of the indices of the corresponding dimension.

Figure 4.1. Using the foreach operator to print an array of values and their corresponding dimension indices.

graphics/04fig01.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net