jdk/src/java.base/share/classes/java/util/ComparableTimSort.java
author mchung
Fri, 22 May 2015 16:43:39 -0700
changeset 30789 9eca83469588
parent 28971 53e56ca0e39d
permissions -rw-r--r--
8074431: Remove native2ascii tool Reviewed-by: erikj, alanb, okutsu, mfang, naoto
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
/*
19590
36f2469d5c39 8011944: Sort fails with ArrayIndexOutOfBoundsException
rriggs
parents: 17712
diff changeset
     2
 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     3
 * Copyright 2009 Google Inc.  All Rights Reserved.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     4
 * 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
     5
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
     6
 * 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
     7
 * 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
     8
 * 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
     9
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    10
 * 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
    11
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    12
 * 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
    13
 * 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
    14
 * 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
    15
 * 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
    16
 * accompanied this code).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    17
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    18
 * 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
    19
 * 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
    20
 * 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
    21
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    23
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3420
diff changeset
    24
 * questions.
3420
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
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    27
package java.util;
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
/**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    30
 * This is a near duplicate of {@link TimSort}, modified for use with
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    31
 * arrays of objects that implement {@link Comparable}, instead of using
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    32
 * explicit comparators.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    33
 *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    34
 * <p>If you are using an optimizing VM, you may find that ComparableTimSort
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    35
 * offers no performance benefit over TimSort in conjunction with a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    36
 * comparator that simply returns {@code ((Comparable)first).compareTo(Second)}.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    37
 * If this is the case, you are better off deleting ComparableTimSort to
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    38
 * eliminate the code duplication.  (See Arrays.java for details.)
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
 * @author Josh Bloch
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
class ComparableTimSort {
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
     * 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
    45
     * 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
    46
     * 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
    47
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    48
     * 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
    49
     * 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
    50
     * 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
    51
     * 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
    52
     * {@link #minRunLength} computation.
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
     * 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
    55
     * 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
    56
     * ArrayOutOfBounds exception.  See listsort.txt for a discussion
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    57
     * 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
    58
     * 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
    59
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    60
    private static final int MIN_MERGE = 32;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    61
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
     * The array being sorted.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    64
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    65
    private final Object[] a;
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
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    68
     * 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
    69
     * often than MIN_GALLOP consecutive times.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    70
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    71
    private static final int  MIN_GALLOP = 7;
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
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    74
     * 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
    75
     * 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
    76
     * random data, and lower for highly structured data.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    77
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    78
    private int minGallop = MIN_GALLOP;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    79
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
     * 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
    82
     * can grow to accommodate demand.
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
     * 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
    85
     * 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
    86
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    87
    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
    88
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    89
    /**
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
    90
     * Temp storage for merges. A workspace array may optionally be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
    91
     * provided in constructor, and if so will be used as long as it
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
    92
     * is big enough.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    93
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    94
    private Object[] tmp;
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
    95
    private int tmpBase; // base of tmp array slice
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
    96
    private int tmpLen;  // length of tmp array slice
3420
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
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
    99
     * 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
   100
     * 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
   101
     * 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
   102
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   103
     *     runBase[i] + runLen[i] == runBase[i + 1]
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
     * 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
   106
     * 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
   107
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   108
    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
   109
    private final int[] runBase;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   110
    private final int[] runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   111
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
     * 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
   114
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   115
     * @param a the array to be sorted
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   116
     * @param work a workspace array (slice)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   117
     * @param workBase origin of usable space in work array
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   118
     * @param workLen usable size of work array
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   119
     */
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   120
    private ComparableTimSort(Object[] a, Object[] work, int workBase, int workLen) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   121
        this.a = a;
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
        // 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
   124
        int len = a.length;
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   125
        int tlen = (len < 2 * INITIAL_TMP_STORAGE_LENGTH) ?
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   126
            len >>> 1 : INITIAL_TMP_STORAGE_LENGTH;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   127
        if (work == null || workLen < tlen || workBase + tlen > work.length) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   128
            tmp = new Object[tlen];
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   129
            tmpBase = 0;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   130
            tmpLen = tlen;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   131
        }
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   132
        else {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   133
            tmp = work;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   134
            tmpBase = workBase;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   135
            tmpLen = workLen;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   136
        }
3420
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
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   139
         * 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
   140
         * 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
   141
         * 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
   142
         * 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
   143
         * 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
   144
         * 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
   145
         * 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
   146
         * the MIN_MERGE declaration above for more information.
28971
53e56ca0e39d 8073124: Tune test and document TimSort runs length stack size increase
lpriima
parents: 28871
diff changeset
   147
         * The maximum value of 49 allows for an array up to length
53e56ca0e39d 8073124: Tune test and document TimSort runs length stack size increase
lpriima
parents: 28871
diff changeset
   148
         * Integer.MAX_VALUE-4, if array is filled by the worst case stack size
53e56ca0e39d 8073124: Tune test and document TimSort runs length stack size increase
lpriima
parents: 28871
diff changeset
   149
         * increasing scenario. More explanations are given in section 4 of:
53e56ca0e39d 8073124: Tune test and document TimSort runs length stack size increase
lpriima
parents: 28871
diff changeset
   150
         * http://envisage-project.eu/wp-content/uploads/2015/02/sorting.pdf
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   151
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   152
        int stackLen = (len <    120  ?  5 :
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   153
                        len <   1542  ? 10 :
28871
d04fa3bde1d0 8072909: TimSort fails with ArrayIndexOutOfBoundsException on worst case long arrays
lpriima
parents: 25859
diff changeset
   154
                        len < 119151  ? 24 : 49);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   155
        runBase = new int[stackLen];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   156
        runLen = new int[stackLen];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   157
    }
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
    /*
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   160
     * The next method (package private and static) constitutes the
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   161
     * entire API of this class.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   162
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   163
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   164
    /**
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   165
     * Sorts the given range, using the given workspace array slice
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   166
     * for temp storage when possible. This method is designed to be
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   167
     * invoked from public methods (in class Arrays) after performing
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   168
     * any necessary array bounds checks and expanding parameters into
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   169
     * the required forms.
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   170
     *
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   171
     * @param a the array to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   172
     * @param lo the index of the first element, inclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   173
     * @param hi the index of the last element, exclusive, to be sorted
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   174
     * @param work a workspace array (slice)
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   175
     * @param workBase origin of usable space in work array
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   176
     * @param workLen usable size of work array
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   177
     * @since 1.8
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   178
     */
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   179
    static void sort(Object[] a, int lo, int hi, Object[] work, int workBase, int workLen) {
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   180
        assert a != null && lo >= 0 && lo <= hi && hi <= a.length;
3420
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
        int nRemaining  = hi - lo;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   183
        if (nRemaining < 2)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   184
            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
   185
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   186
        // 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
   187
        if (nRemaining < MIN_MERGE) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   188
            int initRunLen = countRunAndMakeAscending(a, lo, hi);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   189
            binarySort(a, lo, hi, lo + initRunLen);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   190
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   191
        }
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
         * 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
   195
         * 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
   196
         * to maintain stack invariant.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   197
         */
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   198
        ComparableTimSort ts = new ComparableTimSort(a, work, workBase, workLen);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   199
        int minRun = minRunLength(nRemaining);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   200
        do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   201
            // Identify next run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   202
            int runLen = countRunAndMakeAscending(a, lo, hi);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   203
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   204
            // 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
   205
            if (runLen < minRun) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   206
                int force = nRemaining <= minRun ? nRemaining : minRun;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   207
                binarySort(a, lo, lo + force, lo + runLen);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   208
                runLen = force;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   209
            }
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
            // 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
   212
            ts.pushRun(lo, runLen);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   213
            ts.mergeCollapse();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   214
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   215
            // Advance to find next run
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   216
            lo += runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   217
            nRemaining -= runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   218
        } while (nRemaining != 0);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   219
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   220
        // Merge all remaining runs to complete sort
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   221
        assert lo == hi;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   222
        ts.mergeForceCollapse();
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   223
        assert ts.stackSize == 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   224
    }
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
     * 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
   228
     * 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
   229
     * 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
   230
     * movement (worst case).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   231
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   232
     * 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
   233
     * 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
   234
     * 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
   235
     * exclusive are already sorted.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   236
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   237
     * @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
   238
     * @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
   239
     * @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
   240
     * @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
   241
     *        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
   242
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   243
    @SuppressWarnings({"fallthrough", "rawtypes", "unchecked"})
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   244
    private static void binarySort(Object[] a, int lo, int hi, int start) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   245
        assert lo <= start && start <= hi;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   246
        if (start == lo)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   247
            start++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   248
        for ( ; start < hi; start++) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9006
diff changeset
   249
            Comparable pivot = (Comparable) a[start];
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   250
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   251
            // 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
   252
            int left = lo;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   253
            int right = start;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   254
            assert left <= right;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   255
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   256
             * Invariants:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   257
             *   pivot >= all in [lo, left).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   258
             *   pivot <  all in [right, start).
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   259
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   260
            while (left < right) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   261
                int mid = (left + right) >>> 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   262
                if (pivot.compareTo(a[mid]) < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   263
                    right = mid;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   264
                else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   265
                    left = mid + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   266
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   267
            assert left == right;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   268
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
             * 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
   271
             * 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
   272
             * 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
   273
             * first slot after them -- that's why this sort is stable.
9006
49dc728cdd3e 7031376: Typos in javadoc of TimSort classes
darcy
parents: 7518
diff changeset
   274
             * 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
   275
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   276
            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
   277
            // 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
   278
            switch (n) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   279
                case 2:  a[left + 2] = a[left + 1];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   280
                case 1:  a[left + 1] = a[left];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   281
                         break;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   282
                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
   283
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   284
            a[left] = pivot;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   285
        }
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
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
     * 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
   290
     * 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
   291
     * 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
   292
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   293
     * A run is the longest ascending sequence with:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   294
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   295
     *    a[lo] <= a[lo + 1] <= a[lo + 2] <= ...
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   296
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   297
     * or the longest descending sequence with:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   298
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   299
     *    a[lo] >  a[lo + 1] >  a[lo + 2] >  ...
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   300
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   301
     * 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
   302
     * 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
   303
     * reverse a descending sequence without violating stability.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   304
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   305
     * @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
   306
     * @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
   307
     * @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
   308
              It is required that {@code lo < hi}.
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   309
     * @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
   310
     *          the specified array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   311
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   312
    @SuppressWarnings({"unchecked", "rawtypes"})
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   313
    private static int countRunAndMakeAscending(Object[] a, int lo, int hi) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   314
        assert lo < hi;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   315
        int runHi = lo + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   316
        if (runHi == hi)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   317
            return 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   318
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   319
        // 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
   320
        if (((Comparable) a[runHi++]).compareTo(a[lo]) < 0) { // Descending
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
   321
            while (runHi < hi && ((Comparable) a[runHi]).compareTo(a[runHi - 1]) < 0)
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   322
                runHi++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   323
            reverseRange(a, lo, runHi);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   324
        } else {                              // Ascending
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   325
            while (runHi < hi && ((Comparable) a[runHi]).compareTo(a[runHi - 1]) >= 0)
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
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   328
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   329
        return runHi - lo;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   330
    }
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
     * Reverse the specified range of the specified array.
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
     * @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
   336
     * @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
   337
     * @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
   338
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   339
    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
   340
        hi--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   341
        while (lo < hi) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   342
            Object t = a[lo];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   343
            a[lo++] = a[hi];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   344
            a[hi--] = t;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   345
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   346
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   347
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   348
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   349
     * 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
   350
     * 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
   351
     * {@link #binarySort}.
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
     * Roughly speaking, the computation is:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   354
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   355
     *  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
   356
     *  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
   357
     *  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
   358
     *   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
   359
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   360
     * For the rationale, see listsort.txt.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   361
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   362
     * @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
   363
     * @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
   364
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   365
    private static int minRunLength(int n) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   366
        assert n >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   367
        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
   368
        while (n >= MIN_MERGE) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   369
            r |= (n & 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   370
            n >>= 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   371
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   372
        return n + r;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   373
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   374
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
     * 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
   377
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   378
     * @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
   379
     * @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
   380
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   381
    private void pushRun(int runBase, int runLen) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   382
        this.runBase[stackSize] = runBase;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   383
        this.runLen[stackSize] = runLen;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   384
        stackSize++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   385
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   386
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   387
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   388
     * 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
   389
     * until the stack invariants are reestablished:
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
     *     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
   392
     *     2. runLen[i - 2] > runLen[i - 1]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   393
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   394
     * 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
   395
     * 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
   396
     * entry to the method.
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
    private void mergeCollapse() {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   399
        while (stackSize > 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   400
            int n = stackSize - 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   401
            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
   402
                if (runLen[n - 1] < runLen[n + 1])
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   403
                    n--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   404
                mergeAt(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   405
            } else if (runLen[n] <= runLen[n + 1]) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   406
                mergeAt(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   407
            } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   408
                break; // Invariant is established
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   409
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   410
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   411
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   412
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
     * 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
   415
     * called once, to complete the sort.
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
    private void mergeForceCollapse() {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   418
        while (stackSize > 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   419
            int n = stackSize - 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   420
            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
   421
                n--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   422
            mergeAt(n);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   423
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   424
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   425
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   426
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   427
     * 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
   428
     * 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
   429
     * 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
   430
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   431
     * @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
   432
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   433
    @SuppressWarnings("unchecked")
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   434
    private void mergeAt(int i) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   435
        assert stackSize >= 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   436
        assert i >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   437
        assert i == stackSize - 2 || i == stackSize - 3;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   438
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   439
        int base1 = runBase[i];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   440
        int len1 = runLen[i];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   441
        int base2 = runBase[i + 1];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   442
        int len2 = runLen[i + 1];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   443
        assert len1 > 0 && len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   444
        assert base1 + len1 == base2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   445
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   446
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   447
         * 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
   448
         * 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
   449
         * 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
   450
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   451
        runLen[i] = len1 + len2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   452
        if (i == stackSize - 3) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   453
            runBase[i + 1] = runBase[i + 2];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   454
            runLen[i + 1] = runLen[i + 2];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   455
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   456
        stackSize--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   457
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
         * 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
   460
         * 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
   461
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   462
        int k = gallopRight((Comparable<Object>) a[base2], a, base1, len1, 0);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   463
        assert k >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   464
        base1 += k;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   465
        len1 -= k;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   466
        if (len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   467
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   468
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   469
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   470
         * 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
   471
         * 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
   472
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   473
        len2 = gallopLeft((Comparable<Object>) a[base1 + len1 - 1], a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   474
                base2, len2, len2 - 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   475
        assert len2 >= 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   476
        if (len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   477
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   478
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   479
        // 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
   480
        if (len1 <= len2)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   481
            mergeLo(base1, len1, base2, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   482
        else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   483
            mergeHi(base1, len1, base2, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   484
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   485
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
     * 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
   488
     * 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
   489
     * returns the index of the leftmost equal element.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   490
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   491
     * @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
   492
     * @param a the array in which to search
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   493
     * @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
   494
     * @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
   495
     * @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
   496
     *     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
   497
     * @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
   498
     *    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
   499
     *    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
   500
     *    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
   501
     *    should follow it.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   502
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   503
    private static int gallopLeft(Comparable<Object> key, Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   504
            int base, int len, int hint) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   505
        assert len > 0 && hint >= 0 && hint < len;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   506
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   507
        int lastOfs = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   508
        int ofs = 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   509
        if (key.compareTo(a[base + hint]) > 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   510
            // 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
   511
            int maxOfs = len - hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   512
            while (ofs < maxOfs && key.compareTo(a[base + hint + ofs]) > 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   513
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   514
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   515
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   516
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   517
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   518
            if (ofs > maxOfs)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   519
                ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   520
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   521
            // Make offsets relative to base
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   522
            lastOfs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   523
            ofs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   524
        } else { // key <= a[base + hint]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   525
            // 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
   526
            final int maxOfs = hint + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   527
            while (ofs < maxOfs && key.compareTo(a[base + hint - ofs]) <= 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   528
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   529
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   530
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   531
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   532
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   533
            if (ofs > maxOfs)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   534
                ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   535
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   536
            // Make offsets relative to base
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   537
            int tmp = lastOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   538
            lastOfs = hint - ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   539
            ofs = hint - tmp;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   540
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   541
        assert -1 <= lastOfs && lastOfs < ofs && ofs <= len;
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
        /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   544
         * 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
   545
         * 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
   546
         * 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
   547
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   548
        lastOfs++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   549
        while (lastOfs < ofs) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   550
            int m = lastOfs + ((ofs - lastOfs) >>> 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   551
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   552
            if (key.compareTo(a[base + m]) > 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   553
                lastOfs = m + 1;  // a[base + m] < key
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   554
            else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   555
                ofs = m;          // key <= a[base + m]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   556
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   557
        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
   558
        return ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   559
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   560
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
     * 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
   563
     * 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
   564
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   565
     * @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
   566
     * @param a the array in which to search
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   567
     * @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
   568
     * @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
   569
     * @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
   570
     *     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
   571
     * @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
   572
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   573
    private static int gallopRight(Comparable<Object> key, Object[] a,
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   574
            int base, int len, int hint) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   575
        assert len > 0 && hint >= 0 && hint < len;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   576
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   577
        int ofs = 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   578
        int lastOfs = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   579
        if (key.compareTo(a[base + hint]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   580
            // 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
   581
            int maxOfs = hint + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   582
            while (ofs < maxOfs && key.compareTo(a[base + hint - ofs]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   583
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   584
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   585
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   586
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   587
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   588
            if (ofs > maxOfs)
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
            // Make offsets relative to b
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   592
            int tmp = lastOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   593
            lastOfs = hint - ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   594
            ofs = hint - tmp;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   595
        } else { // a[b + hint] <= key
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   596
            // 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
   597
            int maxOfs = len - hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   598
            while (ofs < maxOfs && key.compareTo(a[base + hint + ofs]) >= 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   599
                lastOfs = ofs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   600
                ofs = (ofs << 1) + 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   601
                if (ofs <= 0)   // int overflow
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   602
                    ofs = maxOfs;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   603
            }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   604
            if (ofs > maxOfs)
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
            // Make offsets relative to b
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   608
            lastOfs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   609
            ofs += hint;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   610
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   611
        assert -1 <= lastOfs && lastOfs < ofs && ofs <= len;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   612
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
         * 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
   615
         * 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
   616
         * 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
   617
         */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   618
        lastOfs++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   619
        while (lastOfs < ofs) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   620
            int m = lastOfs + ((ofs - lastOfs) >>> 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   621
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   622
            if (key.compareTo(a[base + m]) < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   623
                ofs = m;          // key < a[b + m]
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   624
            else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   625
                lastOfs = m + 1;  // a[b + m] <= key
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   626
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   627
        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
   628
        return ofs;
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
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   631
    /**
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   632
     * 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
   633
     * 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
   634
     * 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
   635
     * (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
   636
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   637
     * 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
   638
     * 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
   639
     * may be called if len1 == len2.)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   640
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   641
     * @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
   642
     * @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
   643
     * @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
   644
     *        (must be aBase + aLen)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   645
     * @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
   646
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   647
    @SuppressWarnings({"unchecked", "rawtypes"})
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   648
    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
   649
        assert len1 > 0 && len2 > 0 && base1 + len1 == base2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   650
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   651
        // Copy first run into temp array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   652
        Object[] a = this.a; // For performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   653
        Object[] tmp = ensureCapacity(len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   654
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   655
        int cursor1 = tmpBase; // Indexes into tmp array
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   656
        int cursor2 = base2;   // Indexes int a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   657
        int dest = base1;      // Indexes int a
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   658
        System.arraycopy(a, base1, tmp, cursor1, len1);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   659
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   660
        // 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
   661
        a[dest++] = a[cursor2++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   662
        if (--len2 == 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   663
            System.arraycopy(tmp, cursor1, a, dest, len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   664
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   665
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   666
        if (len1 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   667
            System.arraycopy(a, cursor2, a, dest, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   668
            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
   669
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   670
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   671
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   672
        int minGallop = this.minGallop;  // Use local variable for performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   673
    outer:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   674
        while (true) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   675
            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
   676
            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
   677
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   678
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   679
             * 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
   680
             * winning consistently.
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 {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   683
                assert len1 > 1 && len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   684
                if (((Comparable) a[cursor2]).compareTo(tmp[cursor1]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   685
                    a[dest++] = a[cursor2++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   686
                    count2++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   687
                    count1 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   688
                    if (--len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   689
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   690
                } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   691
                    a[dest++] = tmp[cursor1++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   692
                    count1++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   693
                    count2 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   694
                    if (--len1 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   695
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   696
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   697
            } while ((count1 | count2) < minGallop);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   698
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
             * 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
   701
             * 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
   702
             * neither run appears to be winning consistently anymore.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   703
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   704
            do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   705
                assert len1 > 1 && len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   706
                count1 = gallopRight((Comparable) a[cursor2], tmp, cursor1, len1, 0);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   707
                if (count1 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   708
                    System.arraycopy(tmp, cursor1, a, dest, count1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   709
                    dest += count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   710
                    cursor1 += count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   711
                    len1 -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   712
                    if (len1 <= 1)  // len1 == 1 || len1 == 0
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   713
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   714
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   715
                a[dest++] = a[cursor2++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   716
                if (--len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   717
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   718
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   719
                count2 = gallopLeft((Comparable) tmp[cursor1], a, cursor2, len2, 0);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   720
                if (count2 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   721
                    System.arraycopy(a, cursor2, a, dest, count2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   722
                    dest += count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   723
                    cursor2 += count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   724
                    len2 -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   725
                    if (len2 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   726
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   727
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   728
                a[dest++] = tmp[cursor1++];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   729
                if (--len1 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   730
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   731
                minGallop--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   732
            } while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   733
            if (minGallop < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   734
                minGallop = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   735
            minGallop += 2;  // Penalize for leaving gallop mode
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   736
        }  // End of "outer" loop
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   737
        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
   738
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   739
        if (len1 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   740
            assert len2 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   741
            System.arraycopy(a, cursor2, a, dest, len2);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   742
            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
   743
        } else if (len1 == 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   744
            throw new IllegalArgumentException(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   745
                "Comparison method violates its general contract!");
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   746
        } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   747
            assert len2 == 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   748
            assert len1 > 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   749
            System.arraycopy(tmp, cursor1, a, dest, len1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   750
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   751
    }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   752
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
     * 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
   755
     * 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
   756
     * may be called if len1 == len2.)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   757
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   758
     * @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
   759
     * @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
   760
     * @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
   761
     *        (must be aBase + aLen)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   762
     * @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
   763
     */
13795
73850c397272 7193406: Clean-up JDK Build Warnings in java.util, java.io
dxu
parents: 12448
diff changeset
   764
    @SuppressWarnings({"unchecked", "rawtypes"})
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   765
    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
   766
        assert len1 > 0 && len2 > 0 && base1 + len1 == base2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   767
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   768
        // Copy second run into temp array
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   769
        Object[] a = this.a; // For performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   770
        Object[] tmp = ensureCapacity(len2);
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   771
        int tmpBase = this.tmpBase;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   772
        System.arraycopy(a, base2, tmp, tmpBase, len2);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   773
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   774
        int cursor1 = base1 + len1 - 1;  // Indexes into a
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   775
        int cursor2 = tmpBase + len2 - 1; // Indexes into tmp array
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   776
        int dest = base2 + len2 - 1;     // Indexes into a
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   777
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   778
        // 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
   779
        a[dest--] = a[cursor1--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   780
        if (--len1 == 0) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   781
            System.arraycopy(tmp, tmpBase, a, dest - (len2 - 1), len2);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   782
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   783
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   784
        if (len2 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   785
            dest -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   786
            cursor1 -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   787
            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
   788
            a[dest] = tmp[cursor2];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   789
            return;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   790
        }
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
        int minGallop = this.minGallop;  // Use local variable for performance
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   793
    outer:
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   794
        while (true) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   795
            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
   796
            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
   797
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   798
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   799
             * 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
   800
             * appears to win consistently.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   801
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   802
            do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   803
                assert len1 > 0 && len2 > 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   804
                if (((Comparable) tmp[cursor2]).compareTo(a[cursor1]) < 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   805
                    a[dest--] = a[cursor1--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   806
                    count1++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   807
                    count2 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   808
                    if (--len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   809
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   810
                } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   811
                    a[dest--] = tmp[cursor2--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   812
                    count2++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   813
                    count1 = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   814
                    if (--len2 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   815
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   816
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   817
            } while ((count1 | count2) < minGallop);
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
            /*
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   820
             * 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
   821
             * 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
   822
             * neither run appears to be winning consistently anymore.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   823
             */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   824
            do {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   825
                assert len1 > 0 && len2 > 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   826
                count1 = len1 - gallopRight((Comparable) tmp[cursor2], a, base1, len1, len1 - 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   827
                if (count1 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   828
                    dest -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   829
                    cursor1 -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   830
                    len1 -= count1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   831
                    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
   832
                    if (len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   833
                        break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   834
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   835
                a[dest--] = tmp[cursor2--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   836
                if (--len2 == 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   837
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   838
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   839
                count2 = len2 - gallopLeft((Comparable) a[cursor1], tmp, tmpBase, len2, len2 - 1);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   840
                if (count2 != 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   841
                    dest -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   842
                    cursor2 -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   843
                    len2 -= count2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   844
                    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
   845
                    if (len2 <= 1)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   846
                        break outer; // len2 == 1 || len2 == 0
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   847
                }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   848
                a[dest--] = a[cursor1--];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   849
                if (--len1 == 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   850
                    break outer;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   851
                minGallop--;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   852
            } while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   853
            if (minGallop < 0)
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   854
                minGallop = 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   855
            minGallop += 2;  // Penalize for leaving gallop mode
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   856
        }  // End of "outer" loop
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   857
        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
   858
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   859
        if (len2 == 1) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   860
            assert len1 > 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   861
            dest -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   862
            cursor1 -= len1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   863
            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
   864
            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
   865
        } else if (len2 == 0) {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   866
            throw new IllegalArgumentException(
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   867
                "Comparison method violates its general contract!");
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   868
        } else {
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   869
            assert len1 == 0;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   870
            assert len2 > 0;
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   871
            System.arraycopy(tmp, tmpBase, a, dest - (len2 - 1), len2);
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   872
        }
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   873
    }
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
     * 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
   877
     * 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
   878
     * increases exponentially to ensure amortized linear time complexity.
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   879
     *
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   880
     * @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
   881
     * @return tmp, whether or not it grew
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   882
     */
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   883
    private Object[]  ensureCapacity(int minCapacity) {
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   884
        if (tmpLen < minCapacity) {
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   885
            // Compute smallest power of 2 > minCapacity
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   886
            int newSize = minCapacity;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   887
            newSize |= newSize >> 1;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   888
            newSize |= newSize >> 2;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   889
            newSize |= newSize >> 4;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   890
            newSize |= newSize >> 8;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   891
            newSize |= newSize >> 16;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   892
            newSize++;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   893
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   894
            if (newSize < 0) // Not bloody likely!
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   895
                newSize = minCapacity;
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   896
            else
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   897
                newSize = Math.min(newSize, a.length >>> 1);
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   898
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   899
            @SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"})
3420
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   900
            Object[] newArray = new Object[newSize];
bba8035eebfa 6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff changeset
   901
            tmp = newArray;
17712
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   902
            tmpLen = newSize;
b56c69500af6 8014076: Arrays parallel and serial sorting improvements
dl
parents: 13795
diff changeset
   903
            tmpBase = 0;
3420
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
}