jaroslav@559: /* jaroslav@559: * Copyright 2009 Google Inc. All Rights Reserved. jaroslav@559: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@559: * jaroslav@559: * This code is free software; you can redistribute it and/or modify it jaroslav@559: * under the terms of the GNU General Public License version 2 only, as jaroslav@559: * published by the Free Software Foundation. Oracle designates this jaroslav@559: * particular file as subject to the "Classpath" exception as provided jaroslav@559: * by Oracle in the LICENSE file that accompanied this code. jaroslav@559: * jaroslav@559: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@559: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@559: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@559: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@559: * accompanied this code). jaroslav@559: * jaroslav@559: * You should have received a copy of the GNU General Public License version jaroslav@559: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@559: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@559: * jaroslav@559: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@559: * or visit www.oracle.com if you need additional information or have any jaroslav@559: * questions. jaroslav@559: */ jaroslav@559: jaroslav@559: package java.util; jaroslav@559: jaroslav@568: jaroslav@559: /** jaroslav@559: * A stable, adaptive, iterative mergesort that requires far fewer than jaroslav@559: * n lg(n) comparisons when running on partially sorted arrays, while jaroslav@559: * offering performance comparable to a traditional mergesort when run jaroslav@559: * on random arrays. Like all proper mergesorts, this sort is stable and jaroslav@559: * runs O(n log n) time (worst case). In the worst case, this sort requires jaroslav@559: * temporary storage space for n/2 object references; in the best case, jaroslav@559: * it requires only a small constant amount of space. jaroslav@559: * jaroslav@559: * This implementation was adapted from Tim Peters's list sort for jaroslav@559: * Python, which is described in detail here: jaroslav@559: * jaroslav@559: * http://svn.python.org/projects/python/trunk/Objects/listsort.txt jaroslav@559: * jaroslav@559: * Tim's C code may be found here: jaroslav@559: * jaroslav@559: * http://svn.python.org/projects/python/trunk/Objects/listobject.c jaroslav@559: * jaroslav@559: * The underlying techniques are described in this paper (and may have jaroslav@559: * even earlier origins): jaroslav@559: * jaroslav@559: * "Optimistic Sorting and Information Theoretic Complexity" jaroslav@559: * Peter McIlroy jaroslav@559: * SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms), jaroslav@559: * pp 467-474, Austin, Texas, 25-27 January 1993. jaroslav@559: * jaroslav@559: * While the API to this class consists solely of static methods, it is jaroslav@559: * (privately) instantiable; a TimSort instance holds the state of an ongoing jaroslav@559: * sort, assuming the input array is large enough to warrant the full-blown jaroslav@559: * TimSort. Small arrays are sorted in place, using a binary insertion sort. jaroslav@559: * jaroslav@559: * @author Josh Bloch jaroslav@559: */ jaroslav@559: class TimSort { jaroslav@559: /** jaroslav@559: * This is the minimum sized sequence that will be merged. Shorter jaroslav@559: * sequences will be lengthened by calling binarySort. If the entire jaroslav@559: * array is less than this length, no merges will be performed. jaroslav@559: * jaroslav@559: * This constant should be a power of two. It was 64 in Tim Peter's C jaroslav@559: * implementation, but 32 was empirically determined to work better in jaroslav@559: * this implementation. In the unlikely event that you set this constant jaroslav@559: * to be a number that's not a power of two, you'll need to change the jaroslav@559: * {@link #minRunLength} computation. jaroslav@559: * jaroslav@559: * If you decrease this constant, you must change the stackLen jaroslav@559: * computation in the TimSort constructor, or you risk an jaroslav@559: * ArrayOutOfBounds exception. See listsort.txt for a discussion jaroslav@559: * of the minimum stack length required as a function of the length jaroslav@559: * of the array being sorted and the minimum merge sequence length. jaroslav@559: */ jaroslav@559: private static final int MIN_MERGE = 32; jaroslav@559: jaroslav@559: /** jaroslav@559: * The array being sorted. jaroslav@559: */ jaroslav@559: private final T[] a; jaroslav@559: jaroslav@559: /** jaroslav@559: * The comparator for this sort. jaroslav@559: */ jaroslav@559: private final Comparator c; jaroslav@559: jaroslav@559: /** jaroslav@559: * When we get into galloping mode, we stay there until both runs win less jaroslav@559: * often than MIN_GALLOP consecutive times. jaroslav@559: */ jaroslav@559: private static final int MIN_GALLOP = 7; jaroslav@559: jaroslav@559: /** jaroslav@559: * This controls when we get *into* galloping mode. It is initialized jaroslav@559: * to MIN_GALLOP. The mergeLo and mergeHi methods nudge it higher for jaroslav@559: * random data, and lower for highly structured data. jaroslav@559: */ jaroslav@559: private int minGallop = MIN_GALLOP; jaroslav@559: jaroslav@559: /** jaroslav@559: * Maximum initial size of tmp array, which is used for merging. The array jaroslav@559: * can grow to accommodate demand. jaroslav@559: * jaroslav@559: * Unlike Tim's original C version, we do not allocate this much storage jaroslav@559: * when sorting smaller arrays. This change was required for performance. jaroslav@559: */ jaroslav@559: private static final int INITIAL_TMP_STORAGE_LENGTH = 256; jaroslav@559: jaroslav@559: /** jaroslav@559: * Temp storage for merges. jaroslav@559: */ jaroslav@559: private T[] tmp; // Actual runtime type will be Object[], regardless of T jaroslav@559: jaroslav@559: /** jaroslav@559: * A stack of pending runs yet to be merged. Run i starts at jaroslav@559: * address base[i] and extends for len[i] elements. It's always jaroslav@559: * true (so long as the indices are in bounds) that: jaroslav@559: * jaroslav@559: * runBase[i] + runLen[i] == runBase[i + 1] jaroslav@559: * jaroslav@559: * so we could cut the storage for this, but it's a minor amount, jaroslav@559: * and keeping all the info explicit simplifies the code. jaroslav@559: */ jaroslav@559: private int stackSize = 0; // Number of pending runs on stack jaroslav@559: private final int[] runBase; jaroslav@559: private final int[] runLen; jaroslav@559: jaroslav@559: /** jaroslav@559: * Creates a TimSort instance to maintain the state of an ongoing sort. jaroslav@559: * jaroslav@559: * @param a the array to be sorted jaroslav@559: * @param c the comparator to determine the order of the sort jaroslav@559: */ jaroslav@559: private TimSort(T[] a, Comparator c) { jaroslav@559: this.a = a; jaroslav@559: this.c = c; jaroslav@559: jaroslav@559: // Allocate temp storage (which may be increased later if necessary) jaroslav@559: int len = a.length; jaroslav@559: @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) jaroslav@559: T[] newArray = (T[]) new Object[len < 2 * INITIAL_TMP_STORAGE_LENGTH ? jaroslav@559: len >>> 1 : INITIAL_TMP_STORAGE_LENGTH]; jaroslav@559: tmp = newArray; jaroslav@559: jaroslav@559: /* jaroslav@559: * Allocate runs-to-be-merged stack (which cannot be expanded). The jaroslav@559: * stack length requirements are described in listsort.txt. The C jaroslav@559: * version always uses the same stack length (85), but this was jaroslav@559: * measured to be too expensive when sorting "mid-sized" arrays (e.g., jaroslav@559: * 100 elements) in Java. Therefore, we use smaller (but sufficiently jaroslav@559: * large) stack lengths for smaller arrays. The "magic numbers" in the jaroslav@559: * computation below must be changed if MIN_MERGE is decreased. See jaroslav@559: * the MIN_MERGE declaration above for more information. jaroslav@559: */ jaroslav@559: int stackLen = (len < 120 ? 5 : jaroslav@559: len < 1542 ? 10 : jaroslav@559: len < 119151 ? 19 : 40); jaroslav@559: runBase = new int[stackLen]; jaroslav@559: runLen = new int[stackLen]; jaroslav@559: } jaroslav@559: jaroslav@559: /* jaroslav@559: * The next two methods (which are package private and static) constitute jaroslav@559: * the entire API of this class. Each of these methods obeys the contract jaroslav@559: * of the public method with the same signature in java.util.Arrays. jaroslav@559: */ jaroslav@559: jaroslav@559: static void sort(T[] a, Comparator c) { jaroslav@559: sort(a, 0, a.length, c); jaroslav@559: } jaroslav@559: jaroslav@559: static void sort(T[] a, int lo, int hi, Comparator c) { jaroslav@559: if (c == null) { jaroslav@559: Arrays.sort(a, lo, hi); jaroslav@559: return; jaroslav@559: } jaroslav@559: jaroslav@559: rangeCheck(a.length, lo, hi); jaroslav@559: int nRemaining = hi - lo; jaroslav@559: if (nRemaining < 2) jaroslav@559: return; // Arrays of size 0 and 1 are always sorted jaroslav@559: jaroslav@559: // If array is small, do a "mini-TimSort" with no merges jaroslav@559: if (nRemaining < MIN_MERGE) { jaroslav@559: int initRunLen = countRunAndMakeAscending(a, lo, hi, c); jaroslav@559: binarySort(a, lo, hi, lo + initRunLen, c); jaroslav@559: return; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * March over the array once, left to right, finding natural runs, jaroslav@559: * extending short natural runs to minRun elements, and merging runs jaroslav@559: * to maintain stack invariant. jaroslav@559: */ jaroslav@559: TimSort ts = new TimSort<>(a, c); jaroslav@559: int minRun = minRunLength(nRemaining); jaroslav@559: do { jaroslav@559: // Identify next run jaroslav@559: int runLen = countRunAndMakeAscending(a, lo, hi, c); jaroslav@559: jaroslav@559: // If run is short, extend to min(minRun, nRemaining) jaroslav@559: if (runLen < minRun) { jaroslav@559: int force = nRemaining <= minRun ? nRemaining : minRun; jaroslav@559: binarySort(a, lo, lo + force, lo + runLen, c); jaroslav@559: runLen = force; jaroslav@559: } jaroslav@559: jaroslav@559: // Push run onto pending-run stack, and maybe merge jaroslav@559: ts.pushRun(lo, runLen); jaroslav@559: ts.mergeCollapse(); jaroslav@559: jaroslav@559: // Advance to find next run jaroslav@559: lo += runLen; jaroslav@559: nRemaining -= runLen; jaroslav@559: } while (nRemaining != 0); jaroslav@559: jaroslav@559: // Merge all remaining runs to complete sort jaroslav@559: assert lo == hi; jaroslav@559: ts.mergeForceCollapse(); jaroslav@559: assert ts.stackSize == 1; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Sorts the specified portion of the specified array using a binary jaroslav@559: * insertion sort. This is the best method for sorting small numbers jaroslav@559: * of elements. It requires O(n log n) compares, but O(n^2) data jaroslav@559: * movement (worst case). jaroslav@559: * jaroslav@559: * If the initial part of the specified range is already sorted, jaroslav@559: * this method can take advantage of it: the method assumes that the jaroslav@559: * elements from index {@code lo}, inclusive, to {@code start}, jaroslav@559: * exclusive are already sorted. jaroslav@559: * jaroslav@559: * @param a the array in which a range is to be sorted jaroslav@559: * @param lo the index of the first element in the range to be sorted jaroslav@559: * @param hi the index after the last element in the range to be sorted jaroslav@559: * @param start the index of the first element in the range that is jaroslav@559: * not already known to be sorted ({@code lo <= start <= hi}) jaroslav@559: * @param c comparator to used for the sort jaroslav@559: */ jaroslav@559: @SuppressWarnings("fallthrough") jaroslav@559: private static void binarySort(T[] a, int lo, int hi, int start, jaroslav@559: Comparator c) { jaroslav@559: assert lo <= start && start <= hi; jaroslav@559: if (start == lo) jaroslav@559: start++; jaroslav@559: for ( ; start < hi; start++) { jaroslav@559: T pivot = a[start]; jaroslav@559: jaroslav@559: // Set left (and right) to the index where a[start] (pivot) belongs jaroslav@559: int left = lo; jaroslav@559: int right = start; jaroslav@559: assert left <= right; jaroslav@559: /* jaroslav@559: * Invariants: jaroslav@559: * pivot >= all in [lo, left). jaroslav@559: * pivot < all in [right, start). jaroslav@559: */ jaroslav@559: while (left < right) { jaroslav@559: int mid = (left + right) >>> 1; jaroslav@559: if (c.compare(pivot, a[mid]) < 0) jaroslav@559: right = mid; jaroslav@559: else jaroslav@559: left = mid + 1; jaroslav@559: } jaroslav@559: assert left == right; jaroslav@559: jaroslav@559: /* jaroslav@559: * The invariants still hold: pivot >= all in [lo, left) and jaroslav@559: * pivot < all in [left, start), so pivot belongs at left. Note jaroslav@559: * that if there are elements equal to pivot, left points to the jaroslav@559: * first slot after them -- that's why this sort is stable. jaroslav@559: * Slide elements over to make room for pivot. jaroslav@559: */ jaroslav@559: int n = start - left; // The number of elements to move jaroslav@559: // Switch is just an optimization for arraycopy in default case jaroslav@559: switch (n) { jaroslav@559: case 2: a[left + 2] = a[left + 1]; jaroslav@559: case 1: a[left + 1] = a[left]; jaroslav@559: break; jaroslav@559: default: System.arraycopy(a, left, a, left + 1, n); jaroslav@559: } jaroslav@559: a[left] = pivot; jaroslav@559: } jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Returns the length of the run beginning at the specified position in jaroslav@559: * the specified array and reverses the run if it is descending (ensuring jaroslav@559: * that the run will always be ascending when the method returns). jaroslav@559: * jaroslav@559: * A run is the longest ascending sequence with: jaroslav@559: * jaroslav@559: * a[lo] <= a[lo + 1] <= a[lo + 2] <= ... jaroslav@559: * jaroslav@559: * or the longest descending sequence with: jaroslav@559: * jaroslav@559: * a[lo] > a[lo + 1] > a[lo + 2] > ... jaroslav@559: * jaroslav@559: * For its intended use in a stable mergesort, the strictness of the jaroslav@559: * definition of "descending" is needed so that the call can safely jaroslav@559: * reverse a descending sequence without violating stability. jaroslav@559: * jaroslav@559: * @param a the array in which a run is to be counted and possibly reversed jaroslav@559: * @param lo index of the first element in the run jaroslav@559: * @param hi index after the last element that may be contained in the run. jaroslav@559: It is required that {@code lo < hi}. jaroslav@559: * @param c the comparator to used for the sort jaroslav@559: * @return the length of the run beginning at the specified position in jaroslav@559: * the specified array jaroslav@559: */ jaroslav@559: private static int countRunAndMakeAscending(T[] a, int lo, int hi, jaroslav@559: Comparator c) { jaroslav@559: assert lo < hi; jaroslav@559: int runHi = lo + 1; jaroslav@559: if (runHi == hi) jaroslav@559: return 1; jaroslav@559: jaroslav@559: // Find end of run, and reverse range if descending jaroslav@559: if (c.compare(a[runHi++], a[lo]) < 0) { // Descending jaroslav@559: while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) < 0) jaroslav@559: runHi++; jaroslav@559: reverseRange(a, lo, runHi); jaroslav@559: } else { // Ascending jaroslav@559: while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) >= 0) jaroslav@559: runHi++; jaroslav@559: } jaroslav@559: jaroslav@559: return runHi - lo; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Reverse the specified range of the specified array. jaroslav@559: * jaroslav@559: * @param a the array in which a range is to be reversed jaroslav@559: * @param lo the index of the first element in the range to be reversed jaroslav@559: * @param hi the index after the last element in the range to be reversed jaroslav@559: */ jaroslav@559: private static void reverseRange(Object[] a, int lo, int hi) { jaroslav@559: hi--; jaroslav@559: while (lo < hi) { jaroslav@559: Object t = a[lo]; jaroslav@559: a[lo++] = a[hi]; jaroslav@559: a[hi--] = t; jaroslav@559: } jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Returns the minimum acceptable run length for an array of the specified jaroslav@559: * length. Natural runs shorter than this will be extended with jaroslav@559: * {@link #binarySort}. jaroslav@559: * jaroslav@559: * Roughly speaking, the computation is: jaroslav@559: * jaroslav@559: * If n < MIN_MERGE, return n (it's too small to bother with fancy stuff). jaroslav@559: * Else if n is an exact power of 2, return MIN_MERGE/2. jaroslav@559: * Else return an int k, MIN_MERGE/2 <= k <= MIN_MERGE, such that n/k jaroslav@559: * is close to, but strictly less than, an exact power of 2. jaroslav@559: * jaroslav@559: * For the rationale, see listsort.txt. jaroslav@559: * jaroslav@559: * @param n the length of the array to be sorted jaroslav@559: * @return the length of the minimum run to be merged jaroslav@559: */ jaroslav@559: private static int minRunLength(int n) { jaroslav@559: assert n >= 0; jaroslav@559: int r = 0; // Becomes 1 if any 1 bits are shifted off jaroslav@559: while (n >= MIN_MERGE) { jaroslav@559: r |= (n & 1); jaroslav@559: n >>= 1; jaroslav@559: } jaroslav@559: return n + r; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Pushes the specified run onto the pending-run stack. jaroslav@559: * jaroslav@559: * @param runBase index of the first element in the run jaroslav@559: * @param runLen the number of elements in the run jaroslav@559: */ jaroslav@559: private void pushRun(int runBase, int runLen) { jaroslav@559: this.runBase[stackSize] = runBase; jaroslav@559: this.runLen[stackSize] = runLen; jaroslav@559: stackSize++; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Examines the stack of runs waiting to be merged and merges adjacent runs jaroslav@559: * until the stack invariants are reestablished: jaroslav@559: * jaroslav@559: * 1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1] jaroslav@559: * 2. runLen[i - 2] > runLen[i - 1] jaroslav@559: * jaroslav@559: * This method is called each time a new run is pushed onto the stack, jaroslav@559: * so the invariants are guaranteed to hold for i < stackSize upon jaroslav@559: * entry to the method. jaroslav@559: */ jaroslav@559: private void mergeCollapse() { jaroslav@559: while (stackSize > 1) { jaroslav@559: int n = stackSize - 2; jaroslav@559: if (n > 0 && runLen[n-1] <= runLen[n] + runLen[n+1]) { jaroslav@559: if (runLen[n - 1] < runLen[n + 1]) jaroslav@559: n--; jaroslav@559: mergeAt(n); jaroslav@559: } else if (runLen[n] <= runLen[n + 1]) { jaroslav@559: mergeAt(n); jaroslav@559: } else { jaroslav@559: break; // Invariant is established jaroslav@559: } jaroslav@559: } jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Merges all runs on the stack until only one remains. This method is jaroslav@559: * called once, to complete the sort. jaroslav@559: */ jaroslav@559: private void mergeForceCollapse() { jaroslav@559: while (stackSize > 1) { jaroslav@559: int n = stackSize - 2; jaroslav@559: if (n > 0 && runLen[n - 1] < runLen[n + 1]) jaroslav@559: n--; jaroslav@559: mergeAt(n); jaroslav@559: } jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Merges the two runs at stack indices i and i+1. Run i must be jaroslav@559: * the penultimate or antepenultimate run on the stack. In other words, jaroslav@559: * i must be equal to stackSize-2 or stackSize-3. jaroslav@559: * jaroslav@559: * @param i stack index of the first of the two runs to merge jaroslav@559: */ jaroslav@559: private void mergeAt(int i) { jaroslav@559: assert stackSize >= 2; jaroslav@559: assert i >= 0; jaroslav@559: assert i == stackSize - 2 || i == stackSize - 3; jaroslav@559: jaroslav@559: int base1 = runBase[i]; jaroslav@559: int len1 = runLen[i]; jaroslav@559: int base2 = runBase[i + 1]; jaroslav@559: int len2 = runLen[i + 1]; jaroslav@559: assert len1 > 0 && len2 > 0; jaroslav@559: assert base1 + len1 == base2; jaroslav@559: jaroslav@559: /* jaroslav@559: * Record the length of the combined runs; if i is the 3rd-last jaroslav@559: * run now, also slide over the last run (which isn't involved jaroslav@559: * in this merge). The current run (i+1) goes away in any case. jaroslav@559: */ jaroslav@559: runLen[i] = len1 + len2; jaroslav@559: if (i == stackSize - 3) { jaroslav@559: runBase[i + 1] = runBase[i + 2]; jaroslav@559: runLen[i + 1] = runLen[i + 2]; jaroslav@559: } jaroslav@559: stackSize--; jaroslav@559: jaroslav@559: /* jaroslav@559: * Find where the first element of run2 goes in run1. Prior elements jaroslav@559: * in run1 can be ignored (because they're already in place). jaroslav@559: */ jaroslav@559: int k = gallopRight(a[base2], a, base1, len1, 0, c); jaroslav@559: assert k >= 0; jaroslav@559: base1 += k; jaroslav@559: len1 -= k; jaroslav@559: if (len1 == 0) jaroslav@559: return; jaroslav@559: jaroslav@559: /* jaroslav@559: * Find where the last element of run1 goes in run2. Subsequent elements jaroslav@559: * in run2 can be ignored (because they're already in place). jaroslav@559: */ jaroslav@559: len2 = gallopLeft(a[base1 + len1 - 1], a, base2, len2, len2 - 1, c); jaroslav@559: assert len2 >= 0; jaroslav@559: if (len2 == 0) jaroslav@559: return; jaroslav@559: jaroslav@559: // Merge remaining runs, using tmp array with min(len1, len2) elements jaroslav@559: if (len1 <= len2) jaroslav@559: mergeLo(base1, len1, base2, len2); jaroslav@559: else jaroslav@559: mergeHi(base1, len1, base2, len2); jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Locates the position at which to insert the specified key into the jaroslav@559: * specified sorted range; if the range contains an element equal to key, jaroslav@559: * returns the index of the leftmost equal element. jaroslav@559: * jaroslav@559: * @param key the key whose insertion point to search for jaroslav@559: * @param a the array in which to search jaroslav@559: * @param base the index of the first element in the range jaroslav@559: * @param len the length of the range; must be > 0 jaroslav@559: * @param hint the index at which to begin the search, 0 <= hint < n. jaroslav@559: * The closer hint is to the result, the faster this method will run. jaroslav@559: * @param c the comparator used to order the range, and to search jaroslav@559: * @return the int k, 0 <= k <= n such that a[b + k - 1] < key <= a[b + k], jaroslav@559: * pretending that a[b - 1] is minus infinity and a[b + n] is infinity. jaroslav@559: * In other words, key belongs at index b + k; or in other words, jaroslav@559: * the first k elements of a should precede key, and the last n - k jaroslav@559: * should follow it. jaroslav@559: */ jaroslav@559: private static int gallopLeft(T key, T[] a, int base, int len, int hint, jaroslav@559: Comparator c) { jaroslav@559: assert len > 0 && hint >= 0 && hint < len; jaroslav@559: int lastOfs = 0; jaroslav@559: int ofs = 1; jaroslav@559: if (c.compare(key, a[base + hint]) > 0) { jaroslav@559: // Gallop right until a[base+hint+lastOfs] < key <= a[base+hint+ofs] jaroslav@559: int maxOfs = len - hint; jaroslav@559: while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) > 0) { jaroslav@559: lastOfs = ofs; jaroslav@559: ofs = (ofs << 1) + 1; jaroslav@559: if (ofs <= 0) // int overflow jaroslav@559: ofs = maxOfs; jaroslav@559: } jaroslav@559: if (ofs > maxOfs) jaroslav@559: ofs = maxOfs; jaroslav@559: jaroslav@559: // Make offsets relative to base jaroslav@559: lastOfs += hint; jaroslav@559: ofs += hint; jaroslav@559: } else { // key <= a[base + hint] jaroslav@559: // Gallop left until a[base+hint-ofs] < key <= a[base+hint-lastOfs] jaroslav@559: final int maxOfs = hint + 1; jaroslav@559: while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) <= 0) { jaroslav@559: lastOfs = ofs; jaroslav@559: ofs = (ofs << 1) + 1; jaroslav@559: if (ofs <= 0) // int overflow jaroslav@559: ofs = maxOfs; jaroslav@559: } jaroslav@559: if (ofs > maxOfs) jaroslav@559: ofs = maxOfs; jaroslav@559: jaroslav@559: // Make offsets relative to base jaroslav@559: int tmp = lastOfs; jaroslav@559: lastOfs = hint - ofs; jaroslav@559: ofs = hint - tmp; jaroslav@559: } jaroslav@559: assert -1 <= lastOfs && lastOfs < ofs && ofs <= len; jaroslav@559: jaroslav@559: /* jaroslav@559: * Now a[base+lastOfs] < key <= a[base+ofs], so key belongs somewhere jaroslav@559: * to the right of lastOfs but no farther right than ofs. Do a binary jaroslav@559: * search, with invariant a[base + lastOfs - 1] < key <= a[base + ofs]. jaroslav@559: */ jaroslav@559: lastOfs++; jaroslav@559: while (lastOfs < ofs) { jaroslav@559: int m = lastOfs + ((ofs - lastOfs) >>> 1); jaroslav@559: jaroslav@559: if (c.compare(key, a[base + m]) > 0) jaroslav@559: lastOfs = m + 1; // a[base + m] < key jaroslav@559: else jaroslav@559: ofs = m; // key <= a[base + m] jaroslav@559: } jaroslav@559: assert lastOfs == ofs; // so a[base + ofs - 1] < key <= a[base + ofs] jaroslav@559: return ofs; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Like gallopLeft, except that if the range contains an element equal to jaroslav@559: * key, gallopRight returns the index after the rightmost equal element. jaroslav@559: * jaroslav@559: * @param key the key whose insertion point to search for jaroslav@559: * @param a the array in which to search jaroslav@559: * @param base the index of the first element in the range jaroslav@559: * @param len the length of the range; must be > 0 jaroslav@559: * @param hint the index at which to begin the search, 0 <= hint < n. jaroslav@559: * The closer hint is to the result, the faster this method will run. jaroslav@559: * @param c the comparator used to order the range, and to search jaroslav@559: * @return the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k] jaroslav@559: */ jaroslav@559: private static int gallopRight(T key, T[] a, int base, int len, jaroslav@559: int hint, Comparator c) { jaroslav@559: assert len > 0 && hint >= 0 && hint < len; jaroslav@559: jaroslav@559: int ofs = 1; jaroslav@559: int lastOfs = 0; jaroslav@559: if (c.compare(key, a[base + hint]) < 0) { jaroslav@559: // Gallop left until a[b+hint - ofs] <= key < a[b+hint - lastOfs] jaroslav@559: int maxOfs = hint + 1; jaroslav@559: while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) < 0) { jaroslav@559: lastOfs = ofs; jaroslav@559: ofs = (ofs << 1) + 1; jaroslav@559: if (ofs <= 0) // int overflow jaroslav@559: ofs = maxOfs; jaroslav@559: } jaroslav@559: if (ofs > maxOfs) jaroslav@559: ofs = maxOfs; jaroslav@559: jaroslav@559: // Make offsets relative to b jaroslav@559: int tmp = lastOfs; jaroslav@559: lastOfs = hint - ofs; jaroslav@559: ofs = hint - tmp; jaroslav@559: } else { // a[b + hint] <= key jaroslav@559: // Gallop right until a[b+hint + lastOfs] <= key < a[b+hint + ofs] jaroslav@559: int maxOfs = len - hint; jaroslav@559: while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) >= 0) { jaroslav@559: lastOfs = ofs; jaroslav@559: ofs = (ofs << 1) + 1; jaroslav@559: if (ofs <= 0) // int overflow jaroslav@559: ofs = maxOfs; jaroslav@559: } jaroslav@559: if (ofs > maxOfs) jaroslav@559: ofs = maxOfs; jaroslav@559: jaroslav@559: // Make offsets relative to b jaroslav@559: lastOfs += hint; jaroslav@559: ofs += hint; jaroslav@559: } jaroslav@559: assert -1 <= lastOfs && lastOfs < ofs && ofs <= len; jaroslav@559: jaroslav@559: /* jaroslav@559: * Now a[b + lastOfs] <= key < a[b + ofs], so key belongs somewhere to jaroslav@559: * the right of lastOfs but no farther right than ofs. Do a binary jaroslav@559: * search, with invariant a[b + lastOfs - 1] <= key < a[b + ofs]. jaroslav@559: */ jaroslav@559: lastOfs++; jaroslav@559: while (lastOfs < ofs) { jaroslav@559: int m = lastOfs + ((ofs - lastOfs) >>> 1); jaroslav@559: jaroslav@559: if (c.compare(key, a[base + m]) < 0) jaroslav@559: ofs = m; // key < a[b + m] jaroslav@559: else jaroslav@559: lastOfs = m + 1; // a[b + m] <= key jaroslav@559: } jaroslav@559: assert lastOfs == ofs; // so a[b + ofs - 1] <= key < a[b + ofs] jaroslav@559: return ofs; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Merges two adjacent runs in place, in a stable fashion. The first jaroslav@559: * element of the first run must be greater than the first element of the jaroslav@559: * second run (a[base1] > a[base2]), and the last element of the first run jaroslav@559: * (a[base1 + len1-1]) must be greater than all elements of the second run. jaroslav@559: * jaroslav@559: * For performance, this method should be called only when len1 <= len2; jaroslav@559: * its twin, mergeHi should be called if len1 >= len2. (Either method jaroslav@559: * may be called if len1 == len2.) jaroslav@559: * jaroslav@559: * @param base1 index of first element in first run to be merged jaroslav@559: * @param len1 length of first run to be merged (must be > 0) jaroslav@559: * @param base2 index of first element in second run to be merged jaroslav@559: * (must be aBase + aLen) jaroslav@559: * @param len2 length of second run to be merged (must be > 0) jaroslav@559: */ jaroslav@559: private void mergeLo(int base1, int len1, int base2, int len2) { jaroslav@559: assert len1 > 0 && len2 > 0 && base1 + len1 == base2; jaroslav@559: jaroslav@559: // Copy first run into temp array jaroslav@559: T[] a = this.a; // For performance jaroslav@559: T[] tmp = ensureCapacity(len1); jaroslav@559: System.arraycopy(a, base1, tmp, 0, len1); jaroslav@559: jaroslav@559: int cursor1 = 0; // Indexes into tmp array jaroslav@559: int cursor2 = base2; // Indexes int a jaroslav@559: int dest = base1; // Indexes int a jaroslav@559: jaroslav@559: // Move first element of second run and deal with degenerate cases jaroslav@559: a[dest++] = a[cursor2++]; jaroslav@559: if (--len2 == 0) { jaroslav@559: System.arraycopy(tmp, cursor1, a, dest, len1); jaroslav@559: return; jaroslav@559: } jaroslav@559: if (len1 == 1) { jaroslav@559: System.arraycopy(a, cursor2, a, dest, len2); jaroslav@559: a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge jaroslav@559: return; jaroslav@559: } jaroslav@559: jaroslav@559: Comparator c = this.c; // Use local variable for performance jaroslav@559: int minGallop = this.minGallop; // " " " " " jaroslav@559: outer: jaroslav@559: while (true) { jaroslav@559: int count1 = 0; // Number of times in a row that first run won jaroslav@559: int count2 = 0; // Number of times in a row that second run won jaroslav@559: jaroslav@559: /* jaroslav@559: * Do the straightforward thing until (if ever) one run starts jaroslav@559: * winning consistently. jaroslav@559: */ jaroslav@559: do { jaroslav@559: assert len1 > 1 && len2 > 0; jaroslav@559: if (c.compare(a[cursor2], tmp[cursor1]) < 0) { jaroslav@559: a[dest++] = a[cursor2++]; jaroslav@559: count2++; jaroslav@559: count1 = 0; jaroslav@559: if (--len2 == 0) jaroslav@559: break outer; jaroslav@559: } else { jaroslav@559: a[dest++] = tmp[cursor1++]; jaroslav@559: count1++; jaroslav@559: count2 = 0; jaroslav@559: if (--len1 == 1) jaroslav@559: break outer; jaroslav@559: } jaroslav@559: } while ((count1 | count2) < minGallop); jaroslav@559: jaroslav@559: /* jaroslav@559: * One run is winning so consistently that galloping may be a jaroslav@559: * huge win. So try that, and continue galloping until (if ever) jaroslav@559: * neither run appears to be winning consistently anymore. jaroslav@559: */ jaroslav@559: do { jaroslav@559: assert len1 > 1 && len2 > 0; jaroslav@559: count1 = gallopRight(a[cursor2], tmp, cursor1, len1, 0, c); jaroslav@559: if (count1 != 0) { jaroslav@559: System.arraycopy(tmp, cursor1, a, dest, count1); jaroslav@559: dest += count1; jaroslav@559: cursor1 += count1; jaroslav@559: len1 -= count1; jaroslav@559: if (len1 <= 1) // len1 == 1 || len1 == 0 jaroslav@559: break outer; jaroslav@559: } jaroslav@559: a[dest++] = a[cursor2++]; jaroslav@559: if (--len2 == 0) jaroslav@559: break outer; jaroslav@559: jaroslav@559: count2 = gallopLeft(tmp[cursor1], a, cursor2, len2, 0, c); jaroslav@559: if (count2 != 0) { jaroslav@559: System.arraycopy(a, cursor2, a, dest, count2); jaroslav@559: dest += count2; jaroslav@559: cursor2 += count2; jaroslav@559: len2 -= count2; jaroslav@559: if (len2 == 0) jaroslav@559: break outer; jaroslav@559: } jaroslav@559: a[dest++] = tmp[cursor1++]; jaroslav@559: if (--len1 == 1) jaroslav@559: break outer; jaroslav@559: minGallop--; jaroslav@559: } while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP); jaroslav@559: if (minGallop < 0) jaroslav@559: minGallop = 0; jaroslav@559: minGallop += 2; // Penalize for leaving gallop mode jaroslav@559: } // End of "outer" loop jaroslav@559: this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field jaroslav@559: jaroslav@559: if (len1 == 1) { jaroslav@559: assert len2 > 0; jaroslav@559: System.arraycopy(a, cursor2, a, dest, len2); jaroslav@559: a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge jaroslav@559: } else if (len1 == 0) { jaroslav@559: throw new IllegalArgumentException( jaroslav@559: "Comparison method violates its general contract!"); jaroslav@559: } else { jaroslav@559: assert len2 == 0; jaroslav@559: assert len1 > 1; jaroslav@559: System.arraycopy(tmp, cursor1, a, dest, len1); jaroslav@559: } jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Like mergeLo, except that this method should be called only if jaroslav@559: * len1 >= len2; mergeLo should be called if len1 <= len2. (Either method jaroslav@559: * may be called if len1 == len2.) jaroslav@559: * jaroslav@559: * @param base1 index of first element in first run to be merged jaroslav@559: * @param len1 length of first run to be merged (must be > 0) jaroslav@559: * @param base2 index of first element in second run to be merged jaroslav@559: * (must be aBase + aLen) jaroslav@559: * @param len2 length of second run to be merged (must be > 0) jaroslav@559: */ jaroslav@559: private void mergeHi(int base1, int len1, int base2, int len2) { jaroslav@559: assert len1 > 0 && len2 > 0 && base1 + len1 == base2; jaroslav@559: jaroslav@559: // Copy second run into temp array jaroslav@559: T[] a = this.a; // For performance jaroslav@559: T[] tmp = ensureCapacity(len2); jaroslav@559: System.arraycopy(a, base2, tmp, 0, len2); jaroslav@559: jaroslav@559: int cursor1 = base1 + len1 - 1; // Indexes into a jaroslav@559: int cursor2 = len2 - 1; // Indexes into tmp array jaroslav@559: int dest = base2 + len2 - 1; // Indexes into a jaroslav@559: jaroslav@559: // Move last element of first run and deal with degenerate cases jaroslav@559: a[dest--] = a[cursor1--]; jaroslav@559: if (--len1 == 0) { jaroslav@559: System.arraycopy(tmp, 0, a, dest - (len2 - 1), len2); jaroslav@559: return; jaroslav@559: } jaroslav@559: if (len2 == 1) { jaroslav@559: dest -= len1; jaroslav@559: cursor1 -= len1; jaroslav@559: System.arraycopy(a, cursor1 + 1, a, dest + 1, len1); jaroslav@559: a[dest] = tmp[cursor2]; jaroslav@559: return; jaroslav@559: } jaroslav@559: jaroslav@559: Comparator c = this.c; // Use local variable for performance jaroslav@559: int minGallop = this.minGallop; // " " " " " jaroslav@559: outer: jaroslav@559: while (true) { jaroslav@559: int count1 = 0; // Number of times in a row that first run won jaroslav@559: int count2 = 0; // Number of times in a row that second run won jaroslav@559: jaroslav@559: /* jaroslav@559: * Do the straightforward thing until (if ever) one run jaroslav@559: * appears to win consistently. jaroslav@559: */ jaroslav@559: do { jaroslav@559: assert len1 > 0 && len2 > 1; jaroslav@559: if (c.compare(tmp[cursor2], a[cursor1]) < 0) { jaroslav@559: a[dest--] = a[cursor1--]; jaroslav@559: count1++; jaroslav@559: count2 = 0; jaroslav@559: if (--len1 == 0) jaroslav@559: break outer; jaroslav@559: } else { jaroslav@559: a[dest--] = tmp[cursor2--]; jaroslav@559: count2++; jaroslav@559: count1 = 0; jaroslav@559: if (--len2 == 1) jaroslav@559: break outer; jaroslav@559: } jaroslav@559: } while ((count1 | count2) < minGallop); jaroslav@559: jaroslav@559: /* jaroslav@559: * One run is winning so consistently that galloping may be a jaroslav@559: * huge win. So try that, and continue galloping until (if ever) jaroslav@559: * neither run appears to be winning consistently anymore. jaroslav@559: */ jaroslav@559: do { jaroslav@559: assert len1 > 0 && len2 > 1; jaroslav@559: count1 = len1 - gallopRight(tmp[cursor2], a, base1, len1, len1 - 1, c); jaroslav@559: if (count1 != 0) { jaroslav@559: dest -= count1; jaroslav@559: cursor1 -= count1; jaroslav@559: len1 -= count1; jaroslav@559: System.arraycopy(a, cursor1 + 1, a, dest + 1, count1); jaroslav@559: if (len1 == 0) jaroslav@559: break outer; jaroslav@559: } jaroslav@559: a[dest--] = tmp[cursor2--]; jaroslav@559: if (--len2 == 1) jaroslav@559: break outer; jaroslav@559: jaroslav@559: count2 = len2 - gallopLeft(a[cursor1], tmp, 0, len2, len2 - 1, c); jaroslav@559: if (count2 != 0) { jaroslav@559: dest -= count2; jaroslav@559: cursor2 -= count2; jaroslav@559: len2 -= count2; jaroslav@559: System.arraycopy(tmp, cursor2 + 1, a, dest + 1, count2); jaroslav@559: if (len2 <= 1) // len2 == 1 || len2 == 0 jaroslav@559: break outer; jaroslav@559: } jaroslav@559: a[dest--] = a[cursor1--]; jaroslav@559: if (--len1 == 0) jaroslav@559: break outer; jaroslav@559: minGallop--; jaroslav@559: } while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP); jaroslav@559: if (minGallop < 0) jaroslav@559: minGallop = 0; jaroslav@559: minGallop += 2; // Penalize for leaving gallop mode jaroslav@559: } // End of "outer" loop jaroslav@559: this.minGallop = minGallop < 1 ? 1 : minGallop; // Write back to field jaroslav@559: jaroslav@559: if (len2 == 1) { jaroslav@559: assert len1 > 0; jaroslav@559: dest -= len1; jaroslav@559: cursor1 -= len1; jaroslav@559: System.arraycopy(a, cursor1 + 1, a, dest + 1, len1); jaroslav@559: a[dest] = tmp[cursor2]; // Move first elt of run2 to front of merge jaroslav@559: } else if (len2 == 0) { jaroslav@559: throw new IllegalArgumentException( jaroslav@559: "Comparison method violates its general contract!"); jaroslav@559: } else { jaroslav@559: assert len1 == 0; jaroslav@559: assert len2 > 0; jaroslav@559: System.arraycopy(tmp, 0, a, dest - (len2 - 1), len2); jaroslav@559: } jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Ensures that the external array tmp has at least the specified jaroslav@559: * number of elements, increasing its size if necessary. The size jaroslav@559: * increases exponentially to ensure amortized linear time complexity. jaroslav@559: * jaroslav@559: * @param minCapacity the minimum required capacity of the tmp array jaroslav@559: * @return tmp, whether or not it grew jaroslav@559: */ jaroslav@559: private T[] ensureCapacity(int minCapacity) { jaroslav@559: if (tmp.length < minCapacity) { jaroslav@559: // Compute smallest power of 2 > minCapacity jaroslav@559: int newSize = minCapacity; jaroslav@559: newSize |= newSize >> 1; jaroslav@559: newSize |= newSize >> 2; jaroslav@559: newSize |= newSize >> 4; jaroslav@559: newSize |= newSize >> 8; jaroslav@559: newSize |= newSize >> 16; jaroslav@559: newSize++; jaroslav@559: jaroslav@559: if (newSize < 0) // Not bloody likely! jaroslav@559: newSize = minCapacity; jaroslav@559: else jaroslav@559: newSize = Math.min(newSize, a.length >>> 1); jaroslav@559: jaroslav@559: @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) jaroslav@559: T[] newArray = (T[]) new Object[newSize]; jaroslav@559: tmp = newArray; jaroslav@559: } jaroslav@559: return tmp; jaroslav@559: } jaroslav@559: jaroslav@559: /** jaroslav@559: * Checks that fromIndex and toIndex are in range, and throws an jaroslav@559: * appropriate exception if they aren't. jaroslav@559: * jaroslav@559: * @param arrayLen the length of the array jaroslav@559: * @param fromIndex the index of the first element of the range jaroslav@559: * @param toIndex the index after the last element of the range jaroslav@559: * @throws IllegalArgumentException if fromIndex > toIndex jaroslav@559: * @throws ArrayIndexOutOfBoundsException if fromIndex < 0 jaroslav@559: * or toIndex > arrayLen jaroslav@559: */ jaroslav@559: private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) { jaroslav@559: if (fromIndex > toIndex) jaroslav@559: throw new IllegalArgumentException("fromIndex(" + fromIndex + jaroslav@559: ") > toIndex(" + toIndex+")"); jaroslav@559: if (fromIndex < 0) jaroslav@559: throw new ArrayIndexOutOfBoundsException(fromIndex); jaroslav@559: if (toIndex > arrayLen) jaroslav@559: throw new ArrayIndexOutOfBoundsException(toIndex); jaroslav@559: } jaroslav@559: }