jdk/src/share/classes/java/util/TimSort.java
author peytoia
Thu, 11 Apr 2013 12:22:23 +0900
changeset 16850 f6f6c2182678
parent 9006 49dc728cdd3e
child 17712 b56c69500af6
permissions -rw-r--r--
8009638: Wrong comment for PL in LocaleISOData, 1989 forward Poland is Republic of Poland Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     1
/*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     2
 * Copyright 2009 Google Inc.  All Rights Reserved.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     4
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    10
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    15
 * accompanied this code).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    16
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    23
 * questions.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    24
 */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    25
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    26
package java.util;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    27
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    28
/**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    29
 * A stable, adaptive, iterative mergesort that requires far fewer than
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    30
 * n lg(n) comparisons when running on partially sorted arrays, while
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    31
 * offering performance comparable to a traditional mergesort when run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    32
 * on random arrays.  Like all proper mergesorts, this sort is stable and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    33
 * runs O(n log n) time (worst case).  In the worst case, this sort requires
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    34
 * temporary storage space for n/2 object references; in the best case,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    35
 * it requires only a small constant amount of space.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    36
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    37
 * This implementation was adapted from Tim Peters's list sort for
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    38
 * Python, which is described in detail here:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    39
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    40
 *   http://svn.python.org/projects/python/trunk/Objects/listsort.txt
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    41
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    42
 * Tim's C code may be found here:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    43
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    44
 *   http://svn.python.org/projects/python/trunk/Objects/listobject.c
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    45
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    46
 * The underlying techniques are described in this paper (and may have
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    47
 * even earlier origins):
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    48
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    49
 *  "Optimistic Sorting and Information Theoretic Complexity"
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    50
 *  Peter McIlroy
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    51
 *  SODA (Fourth Annual ACM-SIAM Symposium on Discrete Algorithms),
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    52
 *  pp 467-474, Austin, Texas, 25-27 January 1993.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    53
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    54
 * While the API to this class consists solely of static methods, it is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    55
 * (privately) instantiable; a TimSort instance holds the state of an ongoing
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    56
 * sort, assuming the input array is large enough to warrant the full-blown
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    57
 * TimSort. Small arrays are sorted in place, using a binary insertion sort.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    58
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    59
 * @author Josh Bloch
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    60
 */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    61
class TimSort<T> {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    62
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    63
     * This is the minimum sized sequence that will be merged.  Shorter
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    64
     * sequences will be lengthened by calling binarySort.  If the entire
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    65
     * array is less than this length, no merges will be performed.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    66
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    67
     * This constant should be a power of two.  It was 64 in Tim Peter's C
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    68
     * implementation, but 32 was empirically determined to work better in
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    69
     * this implementation.  In the unlikely event that you set this constant
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    70
     * to be a number that's not a power of two, you'll need to change the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    71
     * {@link #minRunLength} computation.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    72
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    73
     * If you decrease this constant, you must change the stackLen
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    74
     * computation in the TimSort constructor, or you risk an
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    75
     * ArrayOutOfBounds exception.  See listsort.txt for a discussion
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    76
     * of the minimum stack length required as a function of the length
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    77
     * of the array being sorted and the minimum merge sequence length.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    78
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    79
    private static final int MIN_MERGE = 32;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    80
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    81
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    82
     * The array being sorted.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    83
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    84
    private final T[] a;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    85
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    86
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    87
     * The comparator for this sort.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    88
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    89
    private final Comparator<? super T> c;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    90
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    91
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    92
     * When we get into galloping mode, we stay there until both runs win less
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    93
     * often than MIN_GALLOP consecutive times.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    94
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    95
    private static final int  MIN_GALLOP = 7;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    96
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    97
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    98
     * This controls when we get *into* galloping mode.  It is initialized
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    99
     * to MIN_GALLOP.  The mergeLo and mergeHi methods nudge it higher for
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   100
     * random data, and lower for highly structured data.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   101
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   102
    private int minGallop = MIN_GALLOP;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   103
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   104
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   105
     * Maximum initial size of tmp array, which is used for merging.  The array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   106
     * can grow to accommodate demand.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   107
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   108
     * Unlike Tim's original C version, we do not allocate this much storage
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   109
     * when sorting smaller arrays.  This change was required for performance.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   110
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   111
    private static final int INITIAL_TMP_STORAGE_LENGTH = 256;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   112
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   113
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   114
     * Temp storage for merges.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   115
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   116
    private T[] tmp; // Actual runtime type will be Object[], regardless of T
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   117
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   118
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   119
     * A stack of pending runs yet to be merged.  Run i starts at
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   120
     * address base[i] and extends for len[i] elements.  It's always
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   121
     * true (so long as the indices are in bounds) that:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   122
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   123
     *     runBase[i] + runLen[i] == runBase[i + 1]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   124
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   125
     * so we could cut the storage for this, but it's a minor amount,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   126
     * and keeping all the info explicit simplifies the code.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   127
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   128
    private int stackSize = 0;  // Number of pending runs on stack
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   129
    private final int[] runBase;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   130
    private final int[] runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   131
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   132
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   133
     * Creates a TimSort instance to maintain the state of an ongoing sort.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   134
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   135
     * @param a the array to be sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   136
     * @param c the comparator to determine the order of the sort
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   137
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   138
    private TimSort(T[] a, Comparator<? super T> c) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   139
        this.a = a;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   140
        this.c = c;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   141
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   142
        // Allocate temp storage (which may be increased later if necessary)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   143
        int len = a.length;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   144
        @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   145
        T[] newArray = (T[]) new Object[len < 2 * INITIAL_TMP_STORAGE_LENGTH ?
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   146
                                        len >>> 1 : INITIAL_TMP_STORAGE_LENGTH];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   147
        tmp = newArray;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   148
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   149
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   150
         * Allocate runs-to-be-merged stack (which cannot be expanded).  The
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   151
         * stack length requirements are described in listsort.txt.  The C
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   152
         * version always uses the same stack length (85), but this was
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   153
         * measured to be too expensive when sorting "mid-sized" arrays (e.g.,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   154
         * 100 elements) in Java.  Therefore, we use smaller (but sufficiently
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   155
         * large) stack lengths for smaller arrays.  The "magic numbers" in the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   156
         * computation below must be changed if MIN_MERGE is decreased.  See
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   157
         * the MIN_MERGE declaration above for more information.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   158
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   159
        int stackLen = (len <    120  ?  5 :
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   160
                        len <   1542  ? 10 :
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   161
                        len < 119151  ? 19 : 40);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   162
        runBase = new int[stackLen];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   163
        runLen = new int[stackLen];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   164
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   165
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   166
    /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   167
     * The next two methods (which are package private and static) constitute
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   168
     * the entire API of this class.  Each of these methods obeys the contract
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   169
     * of the public method with the same signature in java.util.Arrays.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   170
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   171
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   172
    static <T> void sort(T[] a, Comparator<? super T> c) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   173
        sort(a, 0, a.length, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   174
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   175
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   176
    static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   177
        if (c == null) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   178
            Arrays.sort(a, lo, hi);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   179
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   180
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   181
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   182
        rangeCheck(a.length, lo, hi);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   183
        int nRemaining  = hi - lo;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   184
        if (nRemaining < 2)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   185
            return;  // Arrays of size 0 and 1 are always sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   186
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   187
        // If array is small, do a "mini-TimSort" with no merges
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   188
        if (nRemaining < MIN_MERGE) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   189
            int initRunLen = countRunAndMakeAscending(a, lo, hi, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   190
            binarySort(a, lo, hi, lo + initRunLen, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   191
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   192
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   193
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   194
        /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   195
         * March over the array once, left to right, finding natural runs,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   196
         * extending short natural runs to minRun elements, and merging runs
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   197
         * to maintain stack invariant.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   198
         */
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7518
diff changeset
   199
        TimSort<T> ts = new TimSort<>(a, c);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   200
        int minRun = minRunLength(nRemaining);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   201
        do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   202
            // Identify next run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   203
            int runLen = countRunAndMakeAscending(a, lo, hi, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   204
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   205
            // If run is short, extend to min(minRun, nRemaining)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   206
            if (runLen < minRun) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   207
                int force = nRemaining <= minRun ? nRemaining : minRun;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   208
                binarySort(a, lo, lo + force, lo + runLen, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   209
                runLen = force;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   210
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   211
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   212
            // Push run onto pending-run stack, and maybe merge
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   213
            ts.pushRun(lo, runLen);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   214
            ts.mergeCollapse();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   215
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   216
            // Advance to find next run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   217
            lo += runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   218
            nRemaining -= runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   219
        } while (nRemaining != 0);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   220
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   221
        // Merge all remaining runs to complete sort
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   222
        assert lo == hi;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   223
        ts.mergeForceCollapse();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   224
        assert ts.stackSize == 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   225
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   226
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   227
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   228
     * Sorts the specified portion of the specified array using a binary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   229
     * insertion sort.  This is the best method for sorting small numbers
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   230
     * of elements.  It requires O(n log n) compares, but O(n^2) data
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   231
     * movement (worst case).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   232
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   233
     * If the initial part of the specified range is already sorted,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   234
     * this method can take advantage of it: the method assumes that the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   235
     * elements from index {@code lo}, inclusive, to {@code start},
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   236
     * exclusive are already sorted.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   237
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   238
     * @param a the array in which a range is to be sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   239
     * @param lo the index of the first element in the range to be sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   240
     * @param hi the index after the last element in the range to be sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   241
     * @param start the index of the first element in the range that is
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   242
     *        not already known to be sorted ({@code lo <= start <= hi})
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   243
     * @param c comparator to used for the sort
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   244
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   245
    @SuppressWarnings("fallthrough")
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   246
    private static <T> void binarySort(T[] a, int lo, int hi, int start,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   247
                                       Comparator<? super T> c) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   248
        assert lo <= start && start <= hi;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   249
        if (start == lo)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   250
            start++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   251
        for ( ; start < hi; start++) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   252
            T pivot = a[start];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   253
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   254
            // Set left (and right) to the index where a[start] (pivot) belongs
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   255
            int left = lo;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   256
            int right = start;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   257
            assert left <= right;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   258
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   259
             * Invariants:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   260
             *   pivot >= all in [lo, left).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   261
             *   pivot <  all in [right, start).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   262
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   263
            while (left < right) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   264
                int mid = (left + right) >>> 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   265
                if (c.compare(pivot, a[mid]) < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   266
                    right = mid;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   267
                else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   268
                    left = mid + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   269
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   270
            assert left == right;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   271
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   272
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   273
             * The invariants still hold: pivot >= all in [lo, left) and
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   274
             * pivot < all in [left, start), so pivot belongs at left.  Note
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   275
             * that if there are elements equal to pivot, left points to the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   276
             * first slot after them -- that's why this sort is stable.
9006
49dc728cdd3e 7031376: Typos in javadoc of TimSort classes
darcy
parents: 7803
diff changeset
   277
             * Slide elements over to make room for pivot.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   278
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   279
            int n = start - left;  // The number of elements to move
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   280
            // Switch is just an optimization for arraycopy in default case
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   281
            switch (n) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   282
                case 2:  a[left + 2] = a[left + 1];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   283
                case 1:  a[left + 1] = a[left];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   284
                         break;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   285
                default: System.arraycopy(a, left, a, left + 1, n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   286
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   287
            a[left] = pivot;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   288
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   289
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   290
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   291
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   292
     * Returns the length of the run beginning at the specified position in
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   293
     * the specified array and reverses the run if it is descending (ensuring
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   294
     * that the run will always be ascending when the method returns).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   295
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   296
     * A run is the longest ascending sequence with:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   297
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   298
     *    a[lo] <= a[lo + 1] <= a[lo + 2] <= ...
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   299
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   300
     * or the longest descending sequence with:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   301
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   302
     *    a[lo] >  a[lo + 1] >  a[lo + 2] >  ...
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   303
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   304
     * For its intended use in a stable mergesort, the strictness of the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   305
     * definition of "descending" is needed so that the call can safely
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   306
     * reverse a descending sequence without violating stability.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   307
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   308
     * @param a the array in which a run is to be counted and possibly reversed
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   309
     * @param lo index of the first element in the run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   310
     * @param hi index after the last element that may be contained in the run.
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   311
              It is required that {@code lo < hi}.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   312
     * @param c the comparator to used for the sort
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   313
     * @return  the length of the run beginning at the specified position in
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   314
     *          the specified array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   315
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   316
    private static <T> int countRunAndMakeAscending(T[] a, int lo, int hi,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   317
                                                    Comparator<? super T> c) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   318
        assert lo < hi;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   319
        int runHi = lo + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   320
        if (runHi == hi)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   321
            return 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   322
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   323
        // Find end of run, and reverse range if descending
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   324
        if (c.compare(a[runHi++], a[lo]) < 0) { // Descending
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   325
            while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) < 0)
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   326
                runHi++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   327
            reverseRange(a, lo, runHi);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   328
        } else {                              // Ascending
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   329
            while (runHi < hi && c.compare(a[runHi], a[runHi - 1]) >= 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   330
                runHi++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   331
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   332
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   333
        return runHi - lo;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   334
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   335
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   336
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   337
     * Reverse the specified range of the specified array.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   338
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   339
     * @param a the array in which a range is to be reversed
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   340
     * @param lo the index of the first element in the range to be reversed
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   341
     * @param hi the index after the last element in the range to be reversed
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   342
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   343
    private static void reverseRange(Object[] a, int lo, int hi) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   344
        hi--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   345
        while (lo < hi) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   346
            Object t = a[lo];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   347
            a[lo++] = a[hi];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   348
            a[hi--] = t;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   349
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   350
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   351
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   352
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   353
     * Returns the minimum acceptable run length for an array of the specified
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   354
     * length. Natural runs shorter than this will be extended with
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   355
     * {@link #binarySort}.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   356
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   357
     * Roughly speaking, the computation is:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   358
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   359
     *  If n < MIN_MERGE, return n (it's too small to bother with fancy stuff).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   360
     *  Else if n is an exact power of 2, return MIN_MERGE/2.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   361
     *  Else return an int k, MIN_MERGE/2 <= k <= MIN_MERGE, such that n/k
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   362
     *   is close to, but strictly less than, an exact power of 2.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   363
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   364
     * For the rationale, see listsort.txt.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   365
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   366
     * @param n the length of the array to be sorted
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   367
     * @return the length of the minimum run to be merged
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   368
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   369
    private static int minRunLength(int n) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   370
        assert n >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   371
        int r = 0;      // Becomes 1 if any 1 bits are shifted off
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   372
        while (n >= MIN_MERGE) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   373
            r |= (n & 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   374
            n >>= 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   375
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   376
        return n + r;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   377
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   378
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   379
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   380
     * Pushes the specified run onto the pending-run stack.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   381
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   382
     * @param runBase index of the first element in the run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   383
     * @param runLen  the number of elements in the run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   384
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   385
    private void pushRun(int runBase, int runLen) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   386
        this.runBase[stackSize] = runBase;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   387
        this.runLen[stackSize] = runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   388
        stackSize++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   389
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   390
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   391
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   392
     * Examines the stack of runs waiting to be merged and merges adjacent runs
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   393
     * until the stack invariants are reestablished:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   394
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   395
     *     1. runLen[i - 3] > runLen[i - 2] + runLen[i - 1]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   396
     *     2. runLen[i - 2] > runLen[i - 1]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   397
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   398
     * This method is called each time a new run is pushed onto the stack,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   399
     * so the invariants are guaranteed to hold for i < stackSize upon
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   400
     * entry to the method.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   401
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   402
    private void mergeCollapse() {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   403
        while (stackSize > 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   404
            int n = stackSize - 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   405
            if (n > 0 && runLen[n-1] <= runLen[n] + runLen[n+1]) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   406
                if (runLen[n - 1] < runLen[n + 1])
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   407
                    n--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   408
                mergeAt(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   409
            } else if (runLen[n] <= runLen[n + 1]) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   410
                mergeAt(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   411
            } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   412
                break; // Invariant is established
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   413
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   414
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   415
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   416
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   417
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   418
     * Merges all runs on the stack until only one remains.  This method is
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   419
     * called once, to complete the sort.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   420
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   421
    private void mergeForceCollapse() {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   422
        while (stackSize > 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   423
            int n = stackSize - 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   424
            if (n > 0 && runLen[n - 1] < runLen[n + 1])
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   425
                n--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   426
            mergeAt(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   427
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   428
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   429
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   430
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   431
     * Merges the two runs at stack indices i and i+1.  Run i must be
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   432
     * the penultimate or antepenultimate run on the stack.  In other words,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   433
     * i must be equal to stackSize-2 or stackSize-3.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   434
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   435
     * @param i stack index of the first of the two runs to merge
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   436
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   437
    private void mergeAt(int i) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   438
        assert stackSize >= 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   439
        assert i >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   440
        assert i == stackSize - 2 || i == stackSize - 3;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   441
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   442
        int base1 = runBase[i];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   443
        int len1 = runLen[i];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   444
        int base2 = runBase[i + 1];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   445
        int len2 = runLen[i + 1];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   446
        assert len1 > 0 && len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   447
        assert base1 + len1 == base2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   448
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   449
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   450
         * Record the length of the combined runs; if i is the 3rd-last
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   451
         * run now, also slide over the last run (which isn't involved
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   452
         * in this merge).  The current run (i+1) goes away in any case.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   453
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   454
        runLen[i] = len1 + len2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   455
        if (i == stackSize - 3) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   456
            runBase[i + 1] = runBase[i + 2];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   457
            runLen[i + 1] = runLen[i + 2];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   458
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   459
        stackSize--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   460
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   461
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   462
         * Find where the first element of run2 goes in run1. Prior elements
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   463
         * in run1 can be ignored (because they're already in place).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   464
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   465
        int k = gallopRight(a[base2], a, base1, len1, 0, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   466
        assert k >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   467
        base1 += k;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   468
        len1 -= k;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   469
        if (len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   470
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   471
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   472
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   473
         * Find where the last element of run1 goes in run2. Subsequent elements
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   474
         * in run2 can be ignored (because they're already in place).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   475
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   476
        len2 = gallopLeft(a[base1 + len1 - 1], a, base2, len2, len2 - 1, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   477
        assert len2 >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   478
        if (len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   479
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   480
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   481
        // Merge remaining runs, using tmp array with min(len1, len2) elements
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   482
        if (len1 <= len2)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   483
            mergeLo(base1, len1, base2, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   484
        else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   485
            mergeHi(base1, len1, base2, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   486
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   487
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   488
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   489
     * Locates the position at which to insert the specified key into the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   490
     * specified sorted range; if the range contains an element equal to key,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   491
     * returns the index of the leftmost equal element.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   492
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   493
     * @param key the key whose insertion point to search for
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   494
     * @param a the array in which to search
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   495
     * @param base the index of the first element in the range
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   496
     * @param len the length of the range; must be > 0
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   497
     * @param hint the index at which to begin the search, 0 <= hint < n.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   498
     *     The closer hint is to the result, the faster this method will run.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   499
     * @param c the comparator used to order the range, and to search
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   500
     * @return the int k,  0 <= k <= n such that a[b + k - 1] < key <= a[b + k],
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   501
     *    pretending that a[b - 1] is minus infinity and a[b + n] is infinity.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   502
     *    In other words, key belongs at index b + k; or in other words,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   503
     *    the first k elements of a should precede key, and the last n - k
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   504
     *    should follow it.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   505
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   506
    private static <T> int gallopLeft(T key, T[] a, int base, int len, int hint,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   507
                                      Comparator<? super T> c) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   508
        assert len > 0 && hint >= 0 && hint < len;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   509
        int lastOfs = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   510
        int ofs = 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   511
        if (c.compare(key, a[base + hint]) > 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   512
            // Gallop right until a[base+hint+lastOfs] < key <= a[base+hint+ofs]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   513
            int maxOfs = len - hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   514
            while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) > 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   515
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   516
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   517
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   518
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   519
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   520
            if (ofs > maxOfs)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   521
                ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   522
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   523
            // Make offsets relative to base
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   524
            lastOfs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   525
            ofs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   526
        } else { // key <= a[base + hint]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   527
            // Gallop left until a[base+hint-ofs] < key <= a[base+hint-lastOfs]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   528
            final int maxOfs = hint + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   529
            while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) <= 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   530
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   531
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   532
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   533
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   534
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   535
            if (ofs > maxOfs)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   536
                ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   537
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   538
            // Make offsets relative to base
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   539
            int tmp = lastOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   540
            lastOfs = hint - ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   541
            ofs = hint - tmp;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   542
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   543
        assert -1 <= lastOfs && lastOfs < ofs && ofs <= len;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   544
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   545
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   546
         * Now a[base+lastOfs] < key <= a[base+ofs], so key belongs somewhere
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   547
         * to the right of lastOfs but no farther right than ofs.  Do a binary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   548
         * search, with invariant a[base + lastOfs - 1] < key <= a[base + ofs].
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   549
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   550
        lastOfs++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   551
        while (lastOfs < ofs) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   552
            int m = lastOfs + ((ofs - lastOfs) >>> 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   553
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   554
            if (c.compare(key, a[base + m]) > 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   555
                lastOfs = m + 1;  // a[base + m] < key
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   556
            else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   557
                ofs = m;          // key <= a[base + m]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   558
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   559
        assert lastOfs == ofs;    // so a[base + ofs - 1] < key <= a[base + ofs]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   560
        return ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   561
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   562
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   563
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   564
     * Like gallopLeft, except that if the range contains an element equal to
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   565
     * key, gallopRight returns the index after the rightmost equal element.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   566
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   567
     * @param key the key whose insertion point to search for
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   568
     * @param a the array in which to search
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   569
     * @param base the index of the first element in the range
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   570
     * @param len the length of the range; must be > 0
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   571
     * @param hint the index at which to begin the search, 0 <= hint < n.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   572
     *     The closer hint is to the result, the faster this method will run.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   573
     * @param c the comparator used to order the range, and to search
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   574
     * @return the int k,  0 <= k <= n such that a[b + k - 1] <= key < a[b + k]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   575
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   576
    private static <T> int gallopRight(T key, T[] a, int base, int len,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   577
                                       int hint, Comparator<? super T> c) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   578
        assert len > 0 && hint >= 0 && hint < len;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   579
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   580
        int ofs = 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   581
        int lastOfs = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   582
        if (c.compare(key, a[base + hint]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   583
            // Gallop left until a[b+hint - ofs] <= key < a[b+hint - lastOfs]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   584
            int maxOfs = hint + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   585
            while (ofs < maxOfs && c.compare(key, a[base + hint - ofs]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   586
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   587
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   588
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   589
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   590
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   591
            if (ofs > maxOfs)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   592
                ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   593
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   594
            // Make offsets relative to b
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   595
            int tmp = lastOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   596
            lastOfs = hint - ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   597
            ofs = hint - tmp;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   598
        } else { // a[b + hint] <= key
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   599
            // Gallop right until a[b+hint + lastOfs] <= key < a[b+hint + ofs]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   600
            int maxOfs = len - hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   601
            while (ofs < maxOfs && c.compare(key, a[base + hint + ofs]) >= 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   602
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   603
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   604
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   605
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   606
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   607
            if (ofs > maxOfs)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   608
                ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   609
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   610
            // Make offsets relative to b
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   611
            lastOfs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   612
            ofs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   613
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   614
        assert -1 <= lastOfs && lastOfs < ofs && ofs <= len;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   615
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   616
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   617
         * Now a[b + lastOfs] <= key < a[b + ofs], so key belongs somewhere to
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   618
         * the right of lastOfs but no farther right than ofs.  Do a binary
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   619
         * search, with invariant a[b + lastOfs - 1] <= key < a[b + ofs].
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   620
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   621
        lastOfs++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   622
        while (lastOfs < ofs) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   623
            int m = lastOfs + ((ofs - lastOfs) >>> 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   624
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   625
            if (c.compare(key, a[base + m]) < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   626
                ofs = m;          // key < a[b + m]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   627
            else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   628
                lastOfs = m + 1;  // a[b + m] <= key
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   629
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   630
        assert lastOfs == ofs;    // so a[b + ofs - 1] <= key < a[b + ofs]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   631
        return ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   632
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   633
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   634
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   635
     * Merges two adjacent runs in place, in a stable fashion.  The first
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   636
     * element of the first run must be greater than the first element of the
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   637
     * second run (a[base1] > a[base2]), and the last element of the first run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   638
     * (a[base1 + len1-1]) must be greater than all elements of the second run.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   639
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   640
     * For performance, this method should be called only when len1 <= len2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   641
     * its twin, mergeHi should be called if len1 >= len2.  (Either method
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   642
     * may be called if len1 == len2.)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   643
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   644
     * @param base1 index of first element in first run to be merged
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   645
     * @param len1  length of first run to be merged (must be > 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   646
     * @param base2 index of first element in second run to be merged
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   647
     *        (must be aBase + aLen)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   648
     * @param len2  length of second run to be merged (must be > 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   649
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   650
    private void mergeLo(int base1, int len1, int base2, int len2) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   651
        assert len1 > 0 && len2 > 0 && base1 + len1 == base2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   652
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   653
        // Copy first run into temp array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   654
        T[] a = this.a; // For performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   655
        T[] tmp = ensureCapacity(len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   656
        System.arraycopy(a, base1, tmp, 0, len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   657
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   658
        int cursor1 = 0;       // Indexes into tmp array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   659
        int cursor2 = base2;   // Indexes int a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   660
        int dest = base1;      // Indexes int a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   661
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   662
        // Move first element of second run and deal with degenerate cases
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   663
        a[dest++] = a[cursor2++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   664
        if (--len2 == 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   665
            System.arraycopy(tmp, cursor1, a, dest, len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   666
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   667
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   668
        if (len1 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   669
            System.arraycopy(a, cursor2, a, dest, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   670
            a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   671
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   672
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   673
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   674
        Comparator<? super T> c = this.c;  // Use local variable for performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   675
        int minGallop = this.minGallop;    //  "    "       "     "      "
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   676
    outer:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   677
        while (true) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   678
            int count1 = 0; // Number of times in a row that first run won
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   679
            int count2 = 0; // Number of times in a row that second run won
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   680
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   681
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   682
             * Do the straightforward thing until (if ever) one run starts
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   683
             * winning consistently.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   684
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   685
            do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   686
                assert len1 > 1 && len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   687
                if (c.compare(a[cursor2], tmp[cursor1]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   688
                    a[dest++] = a[cursor2++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   689
                    count2++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   690
                    count1 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   691
                    if (--len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   692
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   693
                } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   694
                    a[dest++] = tmp[cursor1++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   695
                    count1++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   696
                    count2 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   697
                    if (--len1 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   698
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   699
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   700
            } while ((count1 | count2) < minGallop);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   701
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   702
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   703
             * One run is winning so consistently that galloping may be a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   704
             * huge win. So try that, and continue galloping until (if ever)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   705
             * neither run appears to be winning consistently anymore.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   706
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   707
            do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   708
                assert len1 > 1 && len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   709
                count1 = gallopRight(a[cursor2], tmp, cursor1, len1, 0, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   710
                if (count1 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   711
                    System.arraycopy(tmp, cursor1, a, dest, count1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   712
                    dest += count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   713
                    cursor1 += count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   714
                    len1 -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   715
                    if (len1 <= 1) // len1 == 1 || len1 == 0
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   716
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   717
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   718
                a[dest++] = a[cursor2++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   719
                if (--len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   720
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   721
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   722
                count2 = gallopLeft(tmp[cursor1], a, cursor2, len2, 0, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   723
                if (count2 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   724
                    System.arraycopy(a, cursor2, a, dest, count2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   725
                    dest += count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   726
                    cursor2 += count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   727
                    len2 -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   728
                    if (len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   729
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   730
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   731
                a[dest++] = tmp[cursor1++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   732
                if (--len1 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   733
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   734
                minGallop--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   735
            } while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   736
            if (minGallop < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   737
                minGallop = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   738
            minGallop += 2;  // Penalize for leaving gallop mode
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   739
        }  // End of "outer" loop
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   740
        this.minGallop = minGallop < 1 ? 1 : minGallop;  // Write back to field
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   741
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   742
        if (len1 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   743
            assert len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   744
            System.arraycopy(a, cursor2, a, dest, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   745
            a[dest + len2] = tmp[cursor1]; //  Last elt of run 1 to end of merge
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   746
        } else if (len1 == 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   747
            throw new IllegalArgumentException(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   748
                "Comparison method violates its general contract!");
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   749
        } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   750
            assert len2 == 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   751
            assert len1 > 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   752
            System.arraycopy(tmp, cursor1, a, dest, len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   753
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   754
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   755
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   756
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   757
     * Like mergeLo, except that this method should be called only if
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   758
     * len1 >= len2; mergeLo should be called if len1 <= len2.  (Either method
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   759
     * may be called if len1 == len2.)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   760
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   761
     * @param base1 index of first element in first run to be merged
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   762
     * @param len1  length of first run to be merged (must be > 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   763
     * @param base2 index of first element in second run to be merged
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   764
     *        (must be aBase + aLen)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   765
     * @param len2  length of second run to be merged (must be > 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   766
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   767
    private void mergeHi(int base1, int len1, int base2, int len2) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   768
        assert len1 > 0 && len2 > 0 && base1 + len1 == base2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   769
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   770
        // Copy second run into temp array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   771
        T[] a = this.a; // For performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   772
        T[] tmp = ensureCapacity(len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   773
        System.arraycopy(a, base2, tmp, 0, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   774
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   775
        int cursor1 = base1 + len1 - 1;  // Indexes into a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   776
        int cursor2 = len2 - 1;          // Indexes into tmp array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   777
        int dest = base2 + len2 - 1;     // Indexes into a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   778
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   779
        // Move last element of first run and deal with degenerate cases
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   780
        a[dest--] = a[cursor1--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   781
        if (--len1 == 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   782
            System.arraycopy(tmp, 0, a, dest - (len2 - 1), len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   783
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   784
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   785
        if (len2 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   786
            dest -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   787
            cursor1 -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   788
            System.arraycopy(a, cursor1 + 1, a, dest + 1, len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   789
            a[dest] = tmp[cursor2];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   790
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   791
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   792
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   793
        Comparator<? super T> c = this.c;  // Use local variable for performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   794
        int minGallop = this.minGallop;    //  "    "       "     "      "
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   795
    outer:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   796
        while (true) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   797
            int count1 = 0; // Number of times in a row that first run won
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   798
            int count2 = 0; // Number of times in a row that second run won
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   799
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   800
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   801
             * Do the straightforward thing until (if ever) one run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   802
             * appears to win consistently.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   803
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   804
            do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   805
                assert len1 > 0 && len2 > 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   806
                if (c.compare(tmp[cursor2], a[cursor1]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   807
                    a[dest--] = a[cursor1--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   808
                    count1++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   809
                    count2 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   810
                    if (--len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   811
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   812
                } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   813
                    a[dest--] = tmp[cursor2--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   814
                    count2++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   815
                    count1 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   816
                    if (--len2 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   817
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   818
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   819
            } while ((count1 | count2) < minGallop);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   820
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   821
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   822
             * One run is winning so consistently that galloping may be a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   823
             * huge win. So try that, and continue galloping until (if ever)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   824
             * neither run appears to be winning consistently anymore.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   825
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   826
            do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   827
                assert len1 > 0 && len2 > 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   828
                count1 = len1 - gallopRight(tmp[cursor2], a, base1, len1, len1 - 1, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   829
                if (count1 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   830
                    dest -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   831
                    cursor1 -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   832
                    len1 -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   833
                    System.arraycopy(a, cursor1 + 1, a, dest + 1, count1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   834
                    if (len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   835
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   836
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   837
                a[dest--] = tmp[cursor2--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   838
                if (--len2 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   839
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   840
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   841
                count2 = len2 - gallopLeft(a[cursor1], tmp, 0, len2, len2 - 1, c);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   842
                if (count2 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   843
                    dest -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   844
                    cursor2 -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   845
                    len2 -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   846
                    System.arraycopy(tmp, cursor2 + 1, a, dest + 1, count2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   847
                    if (len2 <= 1)  // len2 == 1 || len2 == 0
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   848
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   849
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   850
                a[dest--] = a[cursor1--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   851
                if (--len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   852
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   853
                minGallop--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   854
            } while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   855
            if (minGallop < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   856
                minGallop = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   857
            minGallop += 2;  // Penalize for leaving gallop mode
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   858
        }  // End of "outer" loop
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   859
        this.minGallop = minGallop < 1 ? 1 : minGallop;  // Write back to field
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   860
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   861
        if (len2 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   862
            assert len1 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   863
            dest -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   864
            cursor1 -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   865
            System.arraycopy(a, cursor1 + 1, a, dest + 1, len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   866
            a[dest] = tmp[cursor2];  // Move first elt of run2 to front of merge
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   867
        } else if (len2 == 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   868
            throw new IllegalArgumentException(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   869
                "Comparison method violates its general contract!");
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   870
        } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   871
            assert len1 == 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   872
            assert len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   873
            System.arraycopy(tmp, 0, a, dest - (len2 - 1), len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   874
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   875
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   876
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   877
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   878
     * Ensures that the external array tmp has at least the specified
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   879
     * number of elements, increasing its size if necessary.  The size
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   880
     * increases exponentially to ensure amortized linear time complexity.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   881
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   882
     * @param minCapacity the minimum required capacity of the tmp array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   883
     * @return tmp, whether or not it grew
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   884
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   885
    private T[] ensureCapacity(int minCapacity) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   886
        if (tmp.length < minCapacity) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   887
            // Compute smallest power of 2 > minCapacity
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   888
            int newSize = minCapacity;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   889
            newSize |= newSize >> 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   890
            newSize |= newSize >> 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   891
            newSize |= newSize >> 4;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   892
            newSize |= newSize >> 8;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   893
            newSize |= newSize >> 16;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   894
            newSize++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   895
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   896
            if (newSize < 0) // Not bloody likely!
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   897
                newSize = minCapacity;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   898
            else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   899
                newSize = Math.min(newSize, a.length >>> 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   900
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   901
            @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   902
            T[] newArray = (T[]) new Object[newSize];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   903
            tmp = newArray;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   904
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   905
        return tmp;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   906
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   907
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   908
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   909
     * Checks that fromIndex and toIndex are in range, and throws an
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   910
     * appropriate exception if they aren't.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   911
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   912
     * @param arrayLen the length of the array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   913
     * @param fromIndex the index of the first element of the range
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   914
     * @param toIndex the index after the last element of the range
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   915
     * @throws IllegalArgumentException if fromIndex > toIndex
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   916
     * @throws ArrayIndexOutOfBoundsException if fromIndex < 0
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   917
     *         or toIndex > arrayLen
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   918
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   919
    private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   920
        if (fromIndex > toIndex)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   921
            throw new IllegalArgumentException("fromIndex(" + fromIndex +
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   922
                       ") > toIndex(" + toIndex+")");
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   923
        if (fromIndex < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   924
            throw new ArrayIndexOutOfBoundsException(fromIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   925
        if (toIndex > arrayLen)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   926
            throw new ArrayIndexOutOfBoundsException(toIndex);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   927
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   928
}