/// <summary> /// Standard qquicksort algorithm. /// </summary> /// <param name="left"></param> /// <param name="right"></param> private static void QuickSortIntFastConnections(int left, int right) { do { int i = left; int j = right; IntegerFastConnection x = intFastConnectionArray[(i + j) >> 1]; do { while (CompareKeys(ref intFastConnectionArray[i], ref x) < 0) { i++; } while (CompareKeys(ref x, ref intFastConnectionArray[j]) < 0) { j--; } System.Diagnostics.Debug.Assert(i >= left && j <= right, "(i>=left && j<=right) Sort failed - Is your IComparer bogus?"); if (i > j) { break; } if (i < j) { IntegerFastConnection key = intFastConnectionArray[i]; intFastConnectionArray[i] = intFastConnectionArray[j]; intFastConnectionArray[j] = key; } i++; j--; } while (i <= j); if (j - left <= right - i) { if (left < j) { QuickSortIntFastConnections(left, j); } left = i; } else { if (i < right) { QuickSortIntFastConnections(i, right); } right = j; } } while (left < right); }
// This is a quick sort algorithm that manipulates FastConnection structures. Although this // is the same sorting technique used internally by Array.Sort this is approximately 10 times // faster because it eliminates the need for boxing and unboxing of the structs. // So although this code could be replcaed by a single Array.Sort statement, the pay off // was though to be worth it. private static int CompareKeys(ref IntegerFastConnection a, ref IntegerFastConnection b) { int diff = a.sourceNeuronIdx - b.sourceNeuronIdx; if (diff == 0) { // Secondary sort on targetNeuronIdx. return(a.targetNeuronIdx - b.targetNeuronIdx); } else { return(diff); } }