/// <summary> /// Modifies the specified array by applying the specified function to each element. /// </summary> /// <param name="a"></param> /// <param name="func">object delegate(object o){}</param> /// <returns></returns> public static void ForEach(Array a, ForEachFunction func) { long[] ix = new long[a.Rank]; //Init index for (int i = 0; i < ix.Length; i++) ix[i] = a.GetLowerBound(i); //Loop through all items for (long i = 0; i < a.LongLength; i++) { a.SetValue(func(a.GetValue(ix)), ix); //Increment ix, the index for (int j = 0; j < ix.Length; j++) { if (ix[j] < a.GetUpperBound(j)) { ix[j]++; break; //We're done incrementing. } else { //Ok, reset this one and increment the next. ix[j] = a.GetLowerBound(j); //If this is the last dimension, assert //that we are at the last element if (j == ix.Length - 1) { if (i < a.LongLength - 1) throw new Exception(); } continue; } } } return; }
/// <summary> /// Modifies the specified array by applying the specified function to each element. /// </summary> /// <param name="a"></param> /// <param name="func">object delegate(object o){}</param> /// <returns></returns> public static void ForEach(Array a, ForEachFunction func) { long[] ix = new long[a.Rank]; //Init index for (int i = 0; i < ix.Length; i++) { ix[i] = a.GetLowerBound(i); } //Loop through all items for (long i = 0; i < a.LongLength; i++) { a.SetValue(func(a.GetValue(ix)), ix); //Increment ix, the index for (int j = 0; j < ix.Length; j++) { if (ix[j] < a.GetUpperBound(j)) { ix[j]++; break; //We're done incrementing. } else { //Ok, reset this one and increment the next. ix[j] = a.GetLowerBound(j); //If this is the last dimension, assert //that we are at the last element if (j == ix.Length - 1) { if (i < a.LongLength - 1) { throw new Exception(); } } continue; } } } return; }