Skip to content

Instantly share code, notes, and snippets.

@jandk
Created June 10, 2012 12:03
Show Gist options
  • Select an option

  • Save jandk/2905170 to your computer and use it in GitHub Desktop.

Select an option

Save jandk/2905170 to your computer and use it in GitHub Desktop.
TimSort port of java version
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Sorting
{
/// <summary>
/// A stable, adaptive, iterative mergesort that requires far fewer than
/// n lg(n) comparisons when running on partially sorted arrays, while
/// offering performance comparable to a traditional mergesort when run
/// on random arrays. Like all proper mergesorts, this Sort is stable and
/// runs O(n log n) time (worst case). In the worst case, this Sort requires
/// temporary storage space for n/2 object references; in the best case,
/// it requires only a small constant amount of space.
///
/// This implementation was adapted from Tim Peters's list Sort for
/// Python, which is described in detail here:
///
/// http://svn.python.org/projects/python/trunk/Objects/listsort.txt
///
/// Tim's C code may be found here:
///
/// http://svn.python.org/projects/python/trunk/Objects/listobject.c
///
/// The underlying techniques are described in this paper (and may have
/// even earlier origins):
///
/// "Optimistic Sorting and Information Theoretic Complexity"
/// Peter McIlroy
/// SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms),
/// pp 467-474, Austin, Texas, 25-27 January 1993.
///
/// While the API to this class consists solely of static methods, it is
/// (privately) instantiable; a TimSort instance holds the state of an ongoing
/// Sort, assuming the input array is large enough to warrant the full-blown
/// TimSort. Small arrays are sorted in place, using a binary insertion Sort.
/// </summary>
public class TimSort<T>
where T : IComparable<T>
{
#region Private Members
/// <summary>
/// This is the minimum sized sequence that will be merged. Shorter
/// sequences will be lengthened by calling BinarySort. If the entire
/// array is less than this length, no merges will be performed.
///
/// This constant should be a power of two. It was 64 in Tim Peter's C
/// implementation, but 32 was empirically determined to work better in
/// this implementation. In the unlikely event that you set this constant
/// to be a number that's not a power of two, you'll need to change the
/// <see cref="MinRunLength"/> computation.
///
/// If you decrease this constant, you must change the stackLen
/// computation in the TimSort constructor, or you risk an
/// ArrayOutOfBounds exception. See listsort.txt for a discussion
/// of the minimum stack length required as a function of the length
/// of the array being sorted and the minimum merge sequence length.
/// </summary>
private const int MIN_MERGE = 32;
/// <summary>
/// When we get into galloping mode, we stay there until both runs win less
/// often than MIN_GALLOP consecutive times.
/// </summary>
private const int MIN_GALLOP = 7;
/// <summary>
/// Maximum initial size of tmp array, which is used for merging. The array
/// can grow to accommodate demand.
///
/// Unlike Tim's original C version, we do not allocate this much storage
/// when sorting smaller arrays. This change was required for performance.
/// </summary>
private const int INITIAL_TMP_STORAGE_LENGTH = 256;
/// <summary>
/// The array being sorted
/// </summary>
private readonly T[] a;
/// <summary>
/// Temp storage for merges.
/// </summary>
private T[] tmp;
/// <summary>
/// This controls when we get *into* galloping mode. It is initialized
/// to MIN_GALLOP. The MergeLo and MergeHi methods nudge it higher for
/// random data, and lower for highly structured data.
/// </summary>
private int minGallop = MIN_GALLOP;
/// <summary>
/// A stack of pending runs yet to be merged. Run i starts at
/// address base[i] and extends for len[i] elements. It's always
/// true (so long as the indices are in bounds) that:
/// <code>
/// runBase[i] + runLen[i] == runBase[i + 1]
/// </code>
/// so we could cut the storage for this, but it's a minor amount,
/// and keeping all the info explicit simplifies the code.
/// </summary>
private int stackSize = 0;
private readonly int[] runBase;
private readonly int[] runLen;
#endregion
#region Constructor
/// <summary>
/// Creates a TimSort instance to maintain the state of an ongoing Sort.
/// </summary>
/// <param name="a">
/// The array to be sorted
/// </param>
/// <param name="c">
/// The comparer to determine the order of the Sort
/// </param>
private TimSort(T[] a)
{
this.a = a;
// Allocate temp storage (which may be increased later if necessary)
int len = a.Length;
T[] newArray = new T[len < 2 * INITIAL_TMP_STORAGE_LENGTH ?
len >> 1 : INITIAL_TMP_STORAGE_LENGTH];
tmp = newArray;
/*
* Allocate runs-to-be-merged stack (which cannot be expanded). The
* stack length requirements are described in listsort.txt. The C
* version always uses the same stack length (85), but this was
* measured to be too expensive when sorting "mid-sized" arrays (e.g.,
* 100 elements) in Java. Therefore, we use smaller (but sufficiently
* large) stack lengths for smaller arrays. The "magic numbers" in the
* computation below must be changed if MIN_MERGE is decreased. See
* the MIN_MERGE declaration above for more information.
*/
int stackLen = (len < 120 ? 5 :
len < 1542 ? 10 :
len < 119151 ? 19 : 40);
runBase = new int[stackLen];
runLen = new int[stackLen];
}
#endregion
#region Public Methods
public static void Sort(T[] a)
{
Sort(a, 0, a.Length);
}
public static void Sort(T[] a, int lo, int hi)
{
RangeCheck(a.Length, lo, hi);
int nRemaining = hi - lo;
if (nRemaining < 2)
// Arrays of size 0 and 1 are always sorted
return;
// If array is small, do a "mini-TimSort" with no merges
if (nRemaining < MIN_MERGE)
{
int initRunLen = CountRunAndMakeAscending(a, lo, hi);
BinarySort(a, lo, hi, lo + initRunLen);
return;
}
/**
* March over the array once, left to right, finding natural runs,
* extending short natural runs to minRun elements, and merging runs
* to maintain stack invariant.
*/
TimSort<T> ts = new TimSort<T>(a);
int minRun = MinRunLength(nRemaining);
do
{
// Identify next run
int runLen = CountRunAndMakeAscending(a, lo, hi);
// If run is short, extend to min(minRun, nRemaining)
if (runLen < minRun)
{
int force = nRemaining <= minRun ? nRemaining : minRun;
BinarySort(a, lo, lo + force, lo + runLen);
runLen = force;
}
// Push run onto pending-run stack, and maybe merge
ts.PushRun(lo, runLen);
ts.MergeCollapse();
// Advance to find next run
lo += runLen;
nRemaining -= runLen;
} while (nRemaining != 0);
// Merge all remaining runs to complete Sort
Debug.Assert(lo == hi);
ts.MergeForceCollapse();
Debug.Assert(ts.stackSize == 1);
}
#endregion
/// <summary>
/// Sorts the specified portion of the specified array using a binary
/// insertion Sort. This is the best method for sorting small numbers
/// of elements. It requires O(n log n) compares, but O(n^2) data
/// movement (worst case).
///
/// If the initial part of the specified range is already sorted,
/// this method can take advantage of it: the method assumes that the
/// elements from index {@code lo}, inclusive, to {@code start},
/// exclusive are already sorted.
/// </summary>
/// <param name="a">
/// The array in which a range is to be sorted
/// </param>
/// <param name="lo">
/// The index of the first element in the range to be sorted
/// </param>
/// <param name="hi">
/// The index after the last element in the range to be sorted
/// </param>
/// <param name="start">
/// The index of the first element in the range that is
/// not already known to be sorted <code>lo &lt;= start &lt;= hi</code>
/// </param>
/// <param name="c">
/// Comparer to used for the Sort
/// </param>
private static void BinarySort(T[] a, int lo, int hi, int start)
{
Debug.Assert(lo <= start && start <= hi);
if (start == lo)
start++;
for (; start < hi; start++)
{
T pivot = a[start];
// Set left (and right) to the index where a[start] (pivot) belongs
int left = lo;
int right = start;
Debug.Assert(left <= right);
/*
* Invariants:
* pivot >= all in [lo, left).
* pivot < all in [right, start).
*/
while (left < right)
{
int mid = (left + right) >> 1;
if (pivot.CompareTo(a[mid]) < 0)
right = mid;
else
left = mid + 1;
}
Debug.Assert(left == right);
/*
* The invariants still hold: pivot >= all in [lo, left) and
* pivot < all in [left, start), so pivot belongs at left. Note
* that if there are elements equal to pivot, left points to the
* first slot after them -- that's why this Sort is stable.
* Slide elements over to make room to make room for pivot.
*/
// The number of elements to move
int n = start - left;
// Switch is just an optimization for arraycopy in default case
switch (n)
{
case 2:
a[left + 2] = a[left + 1];
goto case 1;
case 1: a[left + 1] = a[left];
break;
default:
Array.Copy(a, left, a, left + 1, n);
break;
}
a[left] = pivot;
}
}
/// <summary>
/// Returns the length of the run beginning at the specified position in
/// the specified array and reverses the run if it is descending (ensuring
/// that the run will always be ascending when the method returns).
///
/// A run is the longest ascending sequence with:
/// <code>
/// a[lo] &lt;= a[lo + 1] &lt;= a[lo + 2] &lt;= ...
/// </code>
/// or the longest descending sequence with:
/// <code>
/// a[lo] > a[lo + 1] > a[lo + 2] > ...
/// </code>
/// For its intended use in a stable mergesort, the strictness of the
/// definition of "descending" is needed so that the call can safely
/// reverse a descending sequence without violating stability.
/// </summary>
/// <param name="a">
/// The array in which a run is to be counted and possibly reversed
/// </param>
/// <param name="lo">
/// Index of the first element in the run
/// </param>
/// <param name="hi">
/// Index after the last element that may be contained in the run
/// It is required that <code>lo &lt; hi</code>.
/// </param>
/// <param name="c">
/// The comparer to used for the Sort
/// </param>
/// <returns>
/// The length of the run beginning at the specified position in
/// the specified array
/// </returns>
private static int CountRunAndMakeAscending(T[] a, int lo, int hi)
{
Debug.Assert(lo < hi);
int runHi = lo + 1;
if (runHi == hi)
return 1;
// Find end of run, and reverse range if descending
if (a[runHi++].CompareTo(a[lo]) < 0)
{
// Descending
while (runHi < hi && a[runHi].CompareTo(a[runHi - 1]) < 0)
runHi++;
ReverseRange(a, lo, runHi);
}
else
{
// Ascending
while (runHi < hi && a[runHi].CompareTo(a[runHi - 1]) >= 0)
runHi++;
}
return runHi - lo;
}
/// <summary>
/// Reverse the specified range of the specified array.
/// </summary>
/// <param name="a">
/// The array in which a range is to be reversed
/// </param>
/// <param name="lo">
/// The index of the first element in the range to be reversed
/// </param>
/// <param name="hi">
/// The index after the last element in the range to be reversed
/// </param>
private static void ReverseRange(T[] a, int lo, int hi)
{
hi--;
while (lo < hi)
{
T tempT = a[lo];
a[lo++] = a[hi];
a[hi--] = tempT;
}
}
/// <summary>
/// Returns the minimum acceptable run length for an array of the specified
/// length. Natural runs shorter than this will be extended with
/// {@link #BinarySort}.
///
/// Roughly speaking, the computation is:
///
/// If n &lt; MIN_MERGE, return n (it's too small to bother with fancy stuff).
/// Else if n is an exact power of 2, return MIN_MERGE/2.
/// Else return an int k, MIN_MERGE/2 &lt;= k &lt;= MIN_MERGE, such that n/k
/// is close to, but strictly less than, an exact power of 2.
///
/// For the rationale, see listsort.txt.
/// </summary>
/// <param name="n">
/// The length of the array to be sorted
/// </param>
/// <returns>
/// The length of the minimum run to be merged
/// </returns>
private static int MinRunLength(int n)
{
Debug.Assert(n >= 0);
// Becomes 1 if any 1 bits are shifted off
int r = 0;
while (n >= MIN_MERGE)
{
r |= (n & 1);
n >>= 1;
}
return n + r;
}
/// <summary>
/// Pushes the specified run onto the pending-run stack.
/// </summary>
/// <param name="runBase">
/// Index of the first element in the run
/// </param>
/// <param name="runLen">
/// The number of elements in the run
/// </param>
private void PushRun(int runBase, int runLen)
{
this.runBase[stackSize] = runBase;
this.runLen[stackSize] = runLen;
stackSize++;
}
#region Merge
/// <summary>
/// Examines the stack of runs waiting to be merged and merges adjacent runs
/// until the stack invariants are reestablished:
///
/// 1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1]
/// 2. runLen[i - 2] > runLen[i - 1]
///
/// This method is called each time a new run is pushed onto the stack,
/// so the invariants are guaranteed to hold for i &lt; stackSize upon
/// entry to the method.
/// </summary>
private void MergeCollapse()
{
while (stackSize > 1)
{
int n = stackSize - 2;
if (n > 0 && runLen[n - 1] <= runLen[n] + runLen[n + 1])
{
if (runLen[n - 1] < runLen[n + 1])
n--;
MergeAt(n);
}
else if (runLen[n] <= runLen[n + 1])
MergeAt(n);
else
break;
// Invariant is established
}
}
/// <summary>
/// Merges all runs on the stack until only one remains. This method is
/// called once, to complete the Sort.
/// </summary>
private void MergeForceCollapse()
{
while (stackSize > 1)
{
int n = stackSize - 2;
if (n > 0 && runLen[n - 1] < runLen[n + 1])
n--;
MergeAt(n);
}
}
/// <summary>
/// Merges the two runs at stack indices i and i+1. Run i must be
/// the penultimate or antepenultimate run on the stack. In other words,
/// i must be equal to stackSize-2 or stackSize-3.
/// </summary>
/// <param name="i">
/// Stack index of the first of the two runs to merge
/// </param>
private void MergeAt(int i)
{
Debug.Assert(stackSize >= 2);
Debug.Assert(i >= 0);
Debug.Assert(i == stackSize - 2 || i == stackSize - 3);
int base1 = runBase[i];
int len1 = runLen[i];
int base2 = runBase[i + 1];
int len2 = runLen[i + 1];
Debug.Assert(len1 > 0 && len2 > 0);
Debug.Assert(base1 + len1 == base2);
/*
* Record the length of the combined runs; if i is the 3rd-last
* run now, also slide over the last run (which isn't involved
* in this merge). The current run (i+1) goes away in any case.
*/
runLen[i] = len1 + len2;
if (i == stackSize - 3)
{
runBase[i + 1] = runBase[i + 2];
runLen[i + 1] = runLen[i + 2];
}
stackSize--;
/*
* Find where the first element of run2 goes in run1. Prior elements
* in run1 can be ignored (because they're already in place).
*/
int k = GallopRight(a[base2], a, base1, len1, 0);
Debug.Assert(k >= 0);
base1 += k;
len1 -= k;
if (len1 == 0)
return;
/*
* Find where the last element of run1 goes in run2. Subsequent elements
* in run2 can be ignored (because they're already in place).
*/
len2 = GallopLeft(a[base1 + len1 - 1], a, base2, len2, len2 - 1);
Debug.Assert(len2 >= 0);
if (len2 == 0)
return;
// Merge remaining runs, using tmp array with min(len1, len2) elements
if (len1 <= len2)
MergeLo(base1, len1, base2, len2);
else
MergeHi(base1, len1, base2, len2);
}
#endregion
#region Gallop
/// <summary>
/// Locates the position at which to insert the specified key into the
/// specified sorted range; if the range contains an element equal to key,
/// returns the index of the leftmost equal element.
/// </summary>
/// <param name="key">
/// The key whose insertion point to search for
/// </param>
/// <param name="a">
/// The array in which to search
/// </param>
/// <param name="bas">
/// The index of the first element in the range
/// </param>
/// <param name="len">
/// The length of the range; must be > 0
/// </param>
/// <param name="hint">
/// The index at which to begin the search, 0 &lt;= hint &lt; n.
/// The closer hint is to the result, the faster this method will run.
/// </param>
/// <param name="c">
/// The comparer used to order the range, and to search
/// </param>
/// <returns>
/// The int k, 0 &lt;= k &lt;= n such that a[b + k - 1] &lt; key &lt;= a[b + k],
/// pretending that a[b - 1] is minus infinity and a[b + n] is infinity.
/// In other words, key belongs at index b + k; or in other words,
/// the first k elements of a should precede key, and the last n - k
/// should follow it.
/// </returns>
private static int GallopLeft(T key, T[] a, int bas, int len, int hint)
{
Debug.Assert(len > 0 && hint >= 0 && hint < len);
int lastOfs = 0;
int ofs = 1;
if (key.CompareTo(a[bas + hint]) > 0)
{
// Gallop right until a[base+hint+lastOfs] < key <= a[base+hint+ofs]
int maxOfs = len - hint;
while (ofs < maxOfs && key.CompareTo(a[bas + hint + ofs]) > 0)
{
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0)
// int overflow
ofs = maxOfs;
}
if (ofs > maxOfs)
ofs = maxOfs;
// Make offsets relative to base
lastOfs += hint;
ofs += hint;
}
else
{
// key <= a[base + hint]
// Gallop left until a[base+hint-ofs] < key <= a[base+hint-lastOfs]
int maxOfs = hint + 1;
while (ofs < maxOfs && key.CompareTo(a[bas + hint - ofs]) <= 0)
{
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0)
// int overflow
ofs = maxOfs;
}
if (ofs > maxOfs)
ofs = maxOfs;
// Make offsets relative to base
int tmp = lastOfs;
lastOfs = hint - ofs;
ofs = hint - tmp;
}
Debug.Assert(-1 <= lastOfs && lastOfs < ofs && ofs <= len);
/*
* Now a[base+lastOfs] < key <= a[base+ofs], so key belongs somewhere
* to the right of lastOfs but no farther right than ofs. Do a binary
* search, with invariant a[base + lastOfs - 1] < key <= a[base + ofs].
*/
lastOfs++;
while (lastOfs < ofs)
{
int m = lastOfs + ((ofs - lastOfs) >> 1);
if (key.CompareTo(a[bas + m]) > 0)
// a[base + m] < key
lastOfs = m + 1;
else
// key <= a[base + m]
ofs = m;
}
// so a[base + ofs - 1] < key <= a[base + ofs]
Debug.Assert(lastOfs == ofs);
return ofs;
}
/// <summary>
/// Like GallopLeft, except that if the range contains an element equal to
/// key, GallopRight returns the index after the rightmost equal element.
/// </summary>
/// <param name="key">
/// The key whose insertion point to search for
/// </param>
/// <param name="a">
/// The array in which to search
/// </param>
/// <param name="bas">
/// The index of the first element in the range
/// </param>
/// <param name="len">
/// The length of the range; must be > 0
/// </param>
/// <param name="hint">
/// The index at which to begin the search, 0 &lt;= hint &lt; n.
/// The closer hint is to the result, the faster this method will run.
/// </param>
/// <param name="c">
/// The comparer used to order the range, and to search
/// </param>
/// <returns>
/// The int k, 0 &lt;= k &lt;= n such that a[b + k - 1] &lt;= key &lt; a[b + k]
/// </returns>
private static int GallopRight(T key, T[] a, int bas, int len, int hint)
{
Debug.Assert(len > 0 && hint >= 0 && hint < len);
int ofs = 1;
int lastOfs = 0;
if (key.CompareTo(a[bas + hint]) < 0)
{
// Gallop left until a[b+hint - ofs] <= key < a[b+hint - lastOfs]
int maxOfs = hint + 1;
while (ofs < maxOfs && key.CompareTo(a[bas + hint - ofs]) < 0)
{
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0)
// int overflow
ofs = maxOfs;
}
if (ofs > maxOfs)
ofs = maxOfs;
// Make offsets relative to b
int tmp = lastOfs;
lastOfs = hint - ofs;
ofs = hint - tmp;
}
else
{
// a[b + hint] <= key
// Gallop right until a[b+hint + lastOfs] <= key < a[b+hint + ofs]
int maxOfs = len - hint;
while (ofs < maxOfs && key.CompareTo(a[bas + hint + ofs]) >= 0)
{
lastOfs = ofs;
ofs = (ofs << 1) + 1;
if (ofs <= 0)
// int overflow
ofs = maxOfs;
}
if (ofs > maxOfs)
ofs = maxOfs;
// Make offsets relative to b
lastOfs += hint;
ofs += hint;
}
Debug.Assert(-1 <= lastOfs && lastOfs < ofs && ofs <= len);
/*
* Now a[b + lastOfs] <= key < a[b + ofs], so key belongs somewhere to
* the right of lastOfs but no farther right than ofs. Do a binary
* search, with invariant a[b + lastOfs - 1] <= key < a[b + ofs].
*/
lastOfs++;
while (lastOfs < ofs)
{
int m = lastOfs + ((ofs - lastOfs) >> 1);
if (key.CompareTo(a[bas + m]) < 0)
// key < a[b + m]
ofs = m;
else
// a[b + m] <= key
lastOfs = m + 1;
}
// so a[b + ofs - 1] <= key < a[b + ofs]
Debug.Assert(lastOfs == ofs);
return ofs;
}
#endregion
#region Merge
/// <summary>
/// Merges two adjacent runs in place, in a stable fashion. The first
/// element of the first run must be greater than the first element of the
/// second run <code>(a[base1] > a[base2])</code>, and the last element of the first run
/// <code>(a[base1 + len1-1])</code> must be greater than all elements of the second run.
///
/// For performance, this method should be called only when <code>len1 &lt;= len2;</code>
/// its twin, MergeHi should be called if <code>len1 >= len2</code>. (Either method
/// may be called if <code>len1 == len2</code>.)
/// </summary>
/// <param name="base1">
/// Index of first element in first run to be merged
/// </param>
/// <param name="len1">
/// Length of first run to be merged (must be > 0)
/// </param>
/// <param name="base2">
/// Index of first element in second run to be merged
/// (must be aBase + aLen)
/// </param>
/// <param name="len2">
/// Length of second run to be merged (must be > 0)
/// </param>
private void MergeLo(int base1, int len1, int base2, int len2)
{
Debug.Assert(len1 > 0 && len2 > 0 && base1 + len1 == base2);
// Copy first run into temp array
T[] a = this.a; // For performance
T[] tmp = EnsureCapacity(len1);
Array.Copy(a, base1, tmp, 0, len1);
int cursor1 = 0; // Indexes into tmp array
int cursor2 = base2; // Indexes int a
int dest = base1; // Indexes int a
// Move first element of second run and deal with degenerate cases
a[dest++] = a[cursor2++];
if (--len2 == 0)
{
Array.Copy(tmp, cursor1, a, dest, len1);
return;
}
if (len1 == 1)
{
Array.Copy(a, cursor2, a, dest, len2);
a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge
return;
}
int minGallop = this.minGallop; // Use local variable for performance
while (true)
{
int count1 = 0; // Number of times in a row that first run won
int count2 = 0; // Number of times in a row that second run won
/*
* Do the straightforward thing until (if ever) one run starts
* winning consistently.
*/
do
{
Debug.Assert(len1 > 1 && len2 > 0);
if (a[cursor2].CompareTo(tmp[cursor1]) < 0)
{
a[dest++] = a[cursor2++];
count2++;
count1 = 0;
if (--len2 == 0)
goto outer;
}
else
{
a[dest++] = tmp[cursor1++];
count1++;
count2 = 0;
if (--len1 == 1)
goto outer;
}
} while ((count1 | count2) < minGallop);
/*
* One run is winning so consistently that galloping may be a
* huge win. So try that, and continue galloping until (if ever)
* neither run appears to be winning consistently anymore.
*/
do
{
Debug.Assert(len1 > 1 && len2 > 0);
count1 = GallopRight(a[cursor2], tmp, cursor1, len1, 0);
if (count1 != 0)
{
Array.Copy(tmp, cursor1, a, dest, count1);
dest += count1;
cursor1 += count1;
len1 -= count1;
if (len1 <= 1) // len1 == 1 || len1 == 0
goto outer;
}
a[dest++] = a[cursor2++];
if (--len2 == 0)
goto outer;
count2 = GallopLeft(tmp[cursor1], a, cursor2, len2, 0);
if (count2 != 0)
{
Array.Copy(a, cursor2, a, dest, count2);
dest += count2;
cursor2 += count2;
len2 -= count2;
if (len2 == 0)
goto outer;
}
a[dest++] = tmp[cursor1++];
if (--len1 == 1)
goto outer;
minGallop--;
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
if (minGallop < 0)
minGallop = 0;
minGallop += 2; // Penalize for leaving gallop mode
} // End of "outer" loop
outer:
this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field
if (len1 == 1)
{
Debug.Assert(len2 > 0);
Array.Copy(a, cursor2, a, dest, len2);
a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge
}
else if (len1 == 0)
{
throw new ArgumentException(
"Comparison method violates its general contract!");
}
else
{
Debug.Assert(len2 == 0);
Debug.Assert(len1 > 1);
Array.Copy(tmp, cursor1, a, dest, len1);
}
}
/// <summary>
/// Like MergeLo, except that this method should be called only if
/// <code>len1 >= len2</code>; MergeLo should be called if <code>len1 &lt;= len2</code>. (Either method
/// may be called if <code>len1 == len2</code>.)
/// </summary>
/// <param name="base1">
/// Index of first element in first run to be merged
/// </param>
/// <param name="len1">
/// Length of first run to be merged (must be > 0)
/// </param>
/// <param name="base2">
/// Index of first element in second run to be merged
/// (must be aBase + aLen)
/// </param>
/// <param name="len2">
/// Length of second run to be merged (must be > 0)
/// </param>
private void MergeHi(int base1, int len1, int base2, int len2)
{
Debug.Assert(len1 > 0 && len2 > 0 && base1 + len1 == base2);
// Copy second run into temp array
T[] a = this.a; // For performance
T[] tmp = EnsureCapacity(len2);
Array.Copy(a, base2, tmp, 0, len2);
int cursor1 = base1 + len1 - 1; // Indexes into a
int cursor2 = len2 - 1; // Indexes into tmp array
int dest = base2 + len2 - 1; // Indexes into a
// Move last element of first run and deal with degenerate cases
a[dest--] = a[cursor1--];
if (--len1 == 0)
{
Array.Copy(tmp, 0, a, dest - (len2 - 1), len2);
return;
}
if (len2 == 1)
{
dest -= len1;
cursor1 -= len1;
Array.Copy(a, cursor1 + 1, a, dest + 1, len1);
a[dest] = tmp[cursor2];
return;
}
int minGallop = this.minGallop; // Use local variable for performance
while (true)
{
int count1 = 0; // Number of times in a row that first run won
int count2 = 0; // Number of times in a row that second run won
/*
* Do the straightforward thing until (if ever) one run
* appears to win consistently.
*/
do
{
Debug.Assert(len1 > 0 && len2 > 1);
if (tmp[cursor2].CompareTo(a[cursor1]) < 0)
{
a[dest--] = a[cursor1--];
count1++;
count2 = 0;
if (--len1 == 0)
goto outer;
}
else
{
a[dest--] = tmp[cursor2--];
count2++;
count1 = 0;
if (--len2 == 1)
goto outer;
}
} while ((count1 | count2) < minGallop);
/*
* One run is winning so consistently that galloping may be a
* huge win. So try that, and continue galloping until (if ever)
* neither run appears to be winning consistently anymore.
*/
do
{
Debug.Assert(len1 > 0 && len2 > 1);
count1 = len1 - GallopRight(tmp[cursor2], a, base1, len1, len1 - 1);
if (count1 != 0)
{
dest -= count1;
cursor1 -= count1;
len1 -= count1;
Array.Copy(a, cursor1 + 1, a, dest + 1, count1);
if (len1 == 0)
goto outer;
}
a[dest--] = tmp[cursor2--];
if (--len2 == 1)
goto outer;
count2 = len2 - GallopLeft(a[cursor1], tmp, 0, len2, len2 - 1);
if (count2 != 0)
{
dest -= count2;
cursor2 -= count2;
len2 -= count2;
Array.Copy(tmp, cursor2 + 1, a, dest + 1, count2);
if (len2 <= 1) // len2 == 1 || len2 == 0
goto outer;
}
a[dest--] = a[cursor1--];
if (--len1 == 0)
goto outer;
minGallop--;
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
if (minGallop < 0)
minGallop = 0;
minGallop += 2; // Penalize for leaving gallop mode
} // End of "outer" loop
outer:
this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field
if (len2 == 1)
{
Debug.Assert(len1 > 0);
dest -= len1;
cursor1 -= len1;
Array.Copy(a, cursor1 + 1, a, dest + 1, len1);
a[dest] = tmp[cursor2]; // Move first elt of run2 to front of merge
}
else if (len2 == 0)
{
throw new ArgumentException(
"Comparison method violates its general contract!");
}
else
{
Debug.Assert(len1 == 0);
Debug.Assert(len2 > 0);
Array.Copy(tmp, 0, a, dest - (len2 - 1), len2);
}
}
#endregion
/// <summary>
/// Ensures that the external array tmp has at least the specified
/// number of elements, increasing its size if necessary. The size
/// increases exponentially to ensure amortized linear time complexity.
/// </summary>
/// <param name="minCapacity">
/// The minimum required capacity of the tmp array
/// </param>
/// <returns>
/// tmp, whether or not it grew
/// </returns>
private T[] EnsureCapacity(int minCapacity)
{
if (tmp.Length < minCapacity)
{
// Compute smallest power of 2 > minCapacity
int newSize = minCapacity;
newSize |= newSize >> 1;
newSize |= newSize >> 2;
newSize |= newSize >> 4;
newSize |= newSize >> 8;
newSize |= newSize >> 16;
newSize++;
if (newSize < 0) // Not bloody likely!
newSize = minCapacity;
else
newSize = Math.Min(newSize, a.Length >> 1);
tmp = new T[newSize];
}
return tmp;
}
/// <summary>
/// Checks that fromIndex and toIndex are in range, and throws an
/// appropriate exception if they aren't.
/// </summary>
/// <param name="arrayLen">
/// The length of the array
/// </param>
/// <param name="fromIndex">
/// The index of the first element of the range
/// </param>
/// <param name="toIndex">
/// The index after the last element of the range
/// </param>
/// <exception cref="ArgumentException">
/// If fromIndex > toIndex
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// If fromIndex &lt; 0 or toIndex > arrayLen
/// </exception>
private static void RangeCheck(int arrayLen, int fromIndex, int toIndex)
{
if (fromIndex > toIndex)
throw new ArgumentException(String.Format(
"fromIndex({0}) > toIndex({1})",
fromIndex, toIndex
));
if (fromIndex < 0)
throw new ArgumentOutOfRangeException("fromIndex");
if (toIndex > arrayLen)
throw new ArgumentOutOfRangeException("toIndex");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment