author | jjb |
Wed, 29 Jul 2009 14:24:19 -0700 | |
changeset 3420 | bba8035eebfa |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
1 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
2 |
* Copyright 2009 Google Inc. All Rights Reserved. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
4 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Sun designates this |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
10 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
15 |
* accompanied this code). |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
16 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
20 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
23 |
* have any questions. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
24 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
25 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
26 |
package java.util; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
27 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
28 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
29 |
* 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
|
30 |
* 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
|
31 |
* explicit comparators. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
32 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
33 |
* <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
|
34 |
* 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
|
35 |
* 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
|
36 |
* 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
|
37 |
* 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
|
38 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
39 |
* @author Josh Bloch |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
40 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
41 |
class ComparableTimSort { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
42 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
43 |
* 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
|
44 |
* 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
|
45 |
* 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
|
46 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
47 |
* 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
|
48 |
* 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
|
49 |
* 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
|
50 |
* 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
|
51 |
* {@link #minRunLength} computation. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
52 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
53 |
* 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
|
54 |
* 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
|
55 |
* ArrayOutOfBounds exception. See listsort.txt for a discussion |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
56 |
* 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
|
57 |
* 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
|
58 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
59 |
private static final int MIN_MERGE = 32; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
60 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
61 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
62 |
* The array being sorted. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
63 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
64 |
private final Object[] a; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
65 |
|
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 |
* 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
|
68 |
* often than MIN_GALLOP consecutive times. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
69 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
70 |
private static final int MIN_GALLOP = 7; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
71 |
|
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 |
* 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
|
74 |
* 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
|
75 |
* random data, and lower for highly structured data. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
76 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
77 |
private int minGallop = MIN_GALLOP; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
78 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
79 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
80 |
* 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
|
81 |
* can grow to accommodate demand. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
82 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
83 |
* 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
|
84 |
* 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
|
85 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
86 |
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
|
87 |
|
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 |
* Temp storage for merges. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
90 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
91 |
private Object[] tmp; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
92 |
|
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 |
* 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
|
95 |
* 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
|
96 |
* 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
|
97 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
98 |
* runBase[i] + runLen[i] == runBase[i + 1] |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
99 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
100 |
* 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
|
101 |
* 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
|
102 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
103 |
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
|
104 |
private final int[] runBase; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
105 |
private final int[] runLen; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
106 |
|
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 |
* 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
|
109 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
110 |
* @param a the array to be sorted |
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 |
private ComparableTimSort(Object[] a) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
113 |
this.a = a; |
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 |
// 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
|
116 |
int len = a.length; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
117 |
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
118 |
Object[] newArray = new Object[len < 2 * INITIAL_TMP_STORAGE_LENGTH ? |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
119 |
len >>> 1 : INITIAL_TMP_STORAGE_LENGTH]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
120 |
tmp = newArray; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
121 |
|
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 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
|
124 |
* 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
|
125 |
* 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
|
126 |
* 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
|
127 |
* 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
|
128 |
* 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
|
129 |
* 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
|
130 |
* the MIN_MERGE declaration above for more information. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
131 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
132 |
int stackLen = (len < 120 ? 5 : |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
133 |
len < 1542 ? 10 : |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
134 |
len < 119151 ? 19 : 40); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
135 |
runBase = new int[stackLen]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
136 |
runLen = new int[stackLen]; |
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 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
140 |
* The next two methods (which are package private and static) constitute |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
141 |
* the entire API of this class. Each of these methods obeys the contract |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
142 |
* of the public method with the same signature in java.util.Arrays. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
143 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
144 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
145 |
static void sort(Object[] a) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
146 |
sort(a, 0, a.length); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
147 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
148 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
149 |
static void sort(Object[] a, int lo, int hi) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
150 |
rangeCheck(a.length, lo, hi); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
151 |
int nRemaining = hi - lo; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
152 |
if (nRemaining < 2) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
153 |
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
|
154 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
155 |
// 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
|
156 |
if (nRemaining < MIN_MERGE) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
157 |
int initRunLen = countRunAndMakeAscending(a, lo, hi); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
158 |
binarySort(a, lo, hi, lo + initRunLen); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
159 |
return; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
160 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
161 |
|
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 |
* 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
|
164 |
* 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
|
165 |
* to maintain stack invariant. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
166 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
167 |
ComparableTimSort ts = new ComparableTimSort(a); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
168 |
int minRun = minRunLength(nRemaining); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
169 |
do { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
170 |
// Identify next run |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
171 |
int runLen = countRunAndMakeAscending(a, lo, hi); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
172 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
173 |
// 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
|
174 |
if (runLen < minRun) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
175 |
int force = nRemaining <= minRun ? nRemaining : minRun; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
176 |
binarySort(a, lo, lo + force, lo + runLen); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
177 |
runLen = force; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
178 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
179 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
180 |
// 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
|
181 |
ts.pushRun(lo, runLen); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
182 |
ts.mergeCollapse(); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
183 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
184 |
// Advance to find next run |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
185 |
lo += runLen; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
186 |
nRemaining -= runLen; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
187 |
} while (nRemaining != 0); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
188 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
189 |
// Merge all remaining runs to complete sort |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
190 |
assert lo == hi; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
191 |
ts.mergeForceCollapse(); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
192 |
assert ts.stackSize == 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
193 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
194 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
195 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
196 |
* 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
|
197 |
* 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
|
198 |
* 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
|
199 |
* movement (worst case). |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
200 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
201 |
* 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
|
202 |
* 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
|
203 |
* 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
|
204 |
* exclusive are already sorted. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
205 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
206 |
* @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
|
207 |
* @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
|
208 |
* @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
|
209 |
* @param start the index of the first element in the range that is |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
210 |
* not already known to be sorted (@code lo <= start <= hi} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
211 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
212 |
@SuppressWarnings("fallthrough") |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
213 |
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
|
214 |
assert lo <= start && start <= hi; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
215 |
if (start == lo) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
216 |
start++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
217 |
for ( ; start < hi; start++) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
218 |
@SuppressWarnings("unchecked") |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
219 |
Comparable<Object> pivot = (Comparable) a[start]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
220 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
221 |
// 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
|
222 |
int left = lo; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
223 |
int right = start; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
224 |
assert left <= right; |
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 |
* Invariants: |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
227 |
* pivot >= all in [lo, left). |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
228 |
* pivot < all in [right, start). |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
229 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
230 |
while (left < right) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
231 |
int mid = (left + right) >>> 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
232 |
if (pivot.compareTo(a[mid]) < 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
233 |
right = mid; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
234 |
else |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
235 |
left = mid + 1; |
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 |
assert left == right; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
238 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
239 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
240 |
* 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
|
241 |
* 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
|
242 |
* 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
|
243 |
* first slot after them -- that's why this sort is stable. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
244 |
* Slide elements over to make room to make room for pivot. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
245 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
246 |
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
|
247 |
// Switch is just an optimization for arraycopy in default case |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
248 |
switch(n) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
249 |
case 2: a[left + 2] = a[left + 1]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
250 |
case 1: a[left + 1] = a[left]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
251 |
break; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
252 |
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
|
253 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
254 |
a[left] = pivot; |
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 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
257 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
258 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
259 |
* 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
|
260 |
* 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
|
261 |
* 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
|
262 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
263 |
* A run is the longest ascending sequence with: |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
264 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
265 |
* a[lo] <= a[lo + 1] <= a[lo + 2] <= ... |
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 |
* or the longest descending sequence with: |
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 |
* a[lo] > a[lo + 1] > a[lo + 2] > ... |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
270 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
271 |
* 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
|
272 |
* 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
|
273 |
* reverse a descending sequence without violating stability. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
274 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
275 |
* @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
|
276 |
* @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
|
277 |
* @param hi index after the last element that may be contained in the run. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
278 |
It is required that @code{lo < hi}. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
279 |
* @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
|
280 |
* the specified array |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
281 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
282 |
@SuppressWarnings("unchecked") |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
283 |
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
|
284 |
assert lo < hi; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
285 |
int runHi = lo + 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
286 |
if (runHi == hi) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
287 |
return 1; |
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 |
// 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
|
290 |
if (((Comparable) a[runHi++]).compareTo(a[lo]) < 0) { // Descending |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
291 |
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
|
292 |
runHi++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
293 |
reverseRange(a, lo, runHi); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
294 |
} else { // Ascending |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
295 |
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
|
296 |
runHi++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
297 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
298 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
299 |
return runHi - lo; |
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 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
302 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
303 |
* Reverse the specified range of the specified array. |
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 range is to be reversed |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
306 |
* @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
|
307 |
* @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
|
308 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
309 |
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
|
310 |
hi--; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
311 |
while (lo < hi) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
312 |
Object t = a[lo]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
313 |
a[lo++] = a[hi]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
314 |
a[hi--] = t; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
315 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
316 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
317 |
|
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 |
* 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
|
320 |
* 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
|
321 |
* {@link #binarySort}. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
322 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
323 |
* Roughly speaking, the computation is: |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
324 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
325 |
* 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
|
326 |
* 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
|
327 |
* 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
|
328 |
* 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
|
329 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
330 |
* For the rationale, see listsort.txt. |
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 |
* @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
|
333 |
* @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
|
334 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
335 |
private static int minRunLength(int n) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
336 |
assert n >= 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
337 |
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
|
338 |
while (n >= MIN_MERGE) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
339 |
r |= (n & 1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
340 |
n >>= 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
341 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
342 |
return n + r; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
343 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
344 |
|
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 |
* 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
|
347 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
348 |
* @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
|
349 |
* @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
|
350 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
351 |
private void pushRun(int runBase, int runLen) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
352 |
this.runBase[stackSize] = runBase; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
353 |
this.runLen[stackSize] = runLen; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
354 |
stackSize++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
355 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
356 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
357 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
358 |
* 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
|
359 |
* until the stack invariants are reestablished: |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
360 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
361 |
* 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
|
362 |
* 2. runLen[i - 2] > runLen[i - 1] |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
363 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
364 |
* 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
|
365 |
* 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
|
366 |
* entry to the method. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
367 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
368 |
private void mergeCollapse() { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
369 |
while (stackSize > 1) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
370 |
int n = stackSize - 2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
371 |
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
|
372 |
if (runLen[n - 1] < runLen[n + 1]) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
373 |
n--; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
374 |
mergeAt(n); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
375 |
} else if (runLen[n] <= runLen[n + 1]) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
376 |
mergeAt(n); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
377 |
} else { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
378 |
break; // Invariant is established |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
379 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
380 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
381 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
382 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
383 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
384 |
* 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
|
385 |
* called once, to complete the sort. |
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 |
private void mergeForceCollapse() { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
388 |
while (stackSize > 1) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
389 |
int n = stackSize - 2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
390 |
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
|
391 |
n--; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
392 |
mergeAt(n); |
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 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
395 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
396 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
397 |
* 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
|
398 |
* 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
|
399 |
* 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
|
400 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
401 |
* @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
|
402 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
403 |
@SuppressWarnings("unchecked") |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
404 |
private void mergeAt(int i) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
405 |
assert stackSize >= 2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
406 |
assert i >= 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
407 |
assert i == stackSize - 2 || i == stackSize - 3; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
408 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
409 |
int base1 = runBase[i]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
410 |
int len1 = runLen[i]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
411 |
int base2 = runBase[i + 1]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
412 |
int len2 = runLen[i + 1]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
413 |
assert len1 > 0 && len2 > 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
414 |
assert base1 + len1 == base2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
415 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
416 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
417 |
* 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
|
418 |
* 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
|
419 |
* 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
|
420 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
421 |
runLen[i] = len1 + len2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
422 |
if (i == stackSize - 3) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
423 |
runBase[i + 1] = runBase[i + 2]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
424 |
runLen[i + 1] = runLen[i + 2]; |
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 |
stackSize--; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
427 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
428 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
429 |
* 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
|
430 |
* 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
|
431 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
432 |
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
|
433 |
assert k >= 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
434 |
base1 += k; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
435 |
len1 -= k; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
436 |
if (len1 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
437 |
return; |
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 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
440 |
* 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
|
441 |
* 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
|
442 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
443 |
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
|
444 |
base2, len2, len2 - 1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
445 |
assert len2 >= 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
446 |
if (len2 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
447 |
return; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
448 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
449 |
// 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
|
450 |
if (len1 <= len2) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
451 |
mergeLo(base1, len1, base2, len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
452 |
else |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
453 |
mergeHi(base1, len1, base2, len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
454 |
} |
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 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
457 |
* 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
|
458 |
* 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
|
459 |
* returns the index of the leftmost equal element. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
460 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
461 |
* @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
|
462 |
* @param a the array in which to search |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
463 |
* @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
|
464 |
* @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
|
465 |
* @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
|
466 |
* 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
|
467 |
* @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
|
468 |
* 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
|
469 |
* 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
|
470 |
* 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
|
471 |
* should follow it. |
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 |
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
|
474 |
int base, int len, int hint) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
475 |
assert len > 0 && hint >= 0 && hint < len; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
476 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
477 |
int lastOfs = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
478 |
int ofs = 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
479 |
if (key.compareTo(a[base + hint]) > 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
480 |
// 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
|
481 |
int maxOfs = len - hint; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
482 |
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
|
483 |
lastOfs = ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
484 |
ofs = (ofs << 1) + 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
485 |
if (ofs <= 0) // int overflow |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
486 |
ofs = maxOfs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
487 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
488 |
if (ofs > maxOfs) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
489 |
ofs = maxOfs; |
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 |
// Make offsets relative to base |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
492 |
lastOfs += hint; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
493 |
ofs += hint; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
494 |
} else { // key <= a[base + hint] |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
495 |
// 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
|
496 |
final int maxOfs = hint + 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
497 |
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
|
498 |
lastOfs = ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
499 |
ofs = (ofs << 1) + 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
500 |
if (ofs <= 0) // int overflow |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
501 |
ofs = maxOfs; |
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 |
if (ofs > maxOfs) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
504 |
ofs = maxOfs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
505 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
506 |
// Make offsets relative to base |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
507 |
int tmp = lastOfs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
508 |
lastOfs = hint - ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
509 |
ofs = hint - tmp; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
510 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
511 |
assert -1 <= lastOfs && lastOfs < ofs && ofs <= len; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
512 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
513 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
514 |
* 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
|
515 |
* 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
|
516 |
* 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
|
517 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
518 |
lastOfs++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
519 |
while (lastOfs < ofs) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
520 |
int m = lastOfs + ((ofs - lastOfs) >>> 1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
521 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
522 |
if (key.compareTo(a[base + m]) > 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
523 |
lastOfs = m + 1; // a[base + m] < key |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
524 |
else |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
525 |
ofs = m; // key <= a[base + m] |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
526 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
527 |
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
|
528 |
return ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
529 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
530 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
531 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
532 |
* 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
|
533 |
* 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
|
534 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
535 |
* @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
|
536 |
* @param a the array in which to search |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
537 |
* @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
|
538 |
* @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
|
539 |
* @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
|
540 |
* 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
|
541 |
* @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
|
542 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
543 |
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
|
544 |
int base, int len, int hint) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
545 |
assert len > 0 && hint >= 0 && hint < len; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
546 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
547 |
int ofs = 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
548 |
int lastOfs = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
549 |
if (key.compareTo(a[base + hint]) < 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
550 |
// 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
|
551 |
int maxOfs = hint + 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
552 |
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
|
553 |
lastOfs = ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
554 |
ofs = (ofs << 1) + 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
555 |
if (ofs <= 0) // int overflow |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
556 |
ofs = maxOfs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
557 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
558 |
if (ofs > maxOfs) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
559 |
ofs = maxOfs; |
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 |
// Make offsets relative to b |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
562 |
int tmp = lastOfs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
563 |
lastOfs = hint - ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
564 |
ofs = hint - tmp; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
565 |
} else { // a[b + hint] <= key |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
566 |
// 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
|
567 |
int maxOfs = len - hint; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
568 |
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
|
569 |
lastOfs = ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
570 |
ofs = (ofs << 1) + 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
571 |
if (ofs <= 0) // int overflow |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
572 |
ofs = maxOfs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
573 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
574 |
if (ofs > maxOfs) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
575 |
ofs = maxOfs; |
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 |
// Make offsets relative to b |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
578 |
lastOfs += hint; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
579 |
ofs += hint; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
580 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
581 |
assert -1 <= lastOfs && lastOfs < ofs && ofs <= len; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
582 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
583 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
584 |
* 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
|
585 |
* 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
|
586 |
* 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
|
587 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
588 |
lastOfs++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
589 |
while (lastOfs < ofs) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
590 |
int m = lastOfs + ((ofs - lastOfs) >>> 1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
591 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
592 |
if (key.compareTo(a[base + m]) < 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
593 |
ofs = m; // key < a[b + m] |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
594 |
else |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
595 |
lastOfs = m + 1; // a[b + m] <= key |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
596 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
597 |
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
|
598 |
return ofs; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
599 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
600 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
601 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
602 |
* 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
|
603 |
* 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
|
604 |
* 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
|
605 |
* (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
|
606 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
607 |
* 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
|
608 |
* 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
|
609 |
* may be called if len1 == len2.) |
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 |
* @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
|
612 |
* @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
|
613 |
* @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
|
614 |
* (must be aBase + aLen) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
615 |
* @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
|
616 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
617 |
@SuppressWarnings("unchecked") |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
618 |
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
|
619 |
assert len1 > 0 && len2 > 0 && base1 + len1 == base2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
620 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
621 |
// Copy first run into temp array |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
622 |
Object[] a = this.a; // For performance |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
623 |
Object[] tmp = ensureCapacity(len1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
624 |
System.arraycopy(a, base1, tmp, 0, len1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
625 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
626 |
int cursor1 = 0; // Indexes into tmp array |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
627 |
int cursor2 = base2; // Indexes int a |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
628 |
int dest = base1; // Indexes int a |
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 |
// 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
|
631 |
a[dest++] = a[cursor2++]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
632 |
if (--len2 == 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
633 |
System.arraycopy(tmp, cursor1, a, dest, len1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
634 |
return; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
635 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
636 |
if (len1 == 1) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
637 |
System.arraycopy(a, cursor2, a, dest, len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
638 |
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
|
639 |
return; |
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 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
642 |
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
|
643 |
outer: |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
644 |
while (true) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
645 |
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
|
646 |
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
|
647 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
648 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
649 |
* 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
|
650 |
* winning consistently. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
651 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
652 |
do { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
653 |
assert len1 > 1 && len2 > 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
654 |
if (((Comparable) a[cursor2]).compareTo(tmp[cursor1]) < 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
655 |
a[dest++] = a[cursor2++]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
656 |
count2++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
657 |
count1 = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
658 |
if (--len2 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
659 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
660 |
} else { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
661 |
a[dest++] = tmp[cursor1++]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
662 |
count1++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
663 |
count2 = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
664 |
if (--len1 == 1) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
665 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
666 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
667 |
} while ((count1 | count2) < minGallop); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
668 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
669 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
670 |
* 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
|
671 |
* 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
|
672 |
* neither run appears to be winning consistently anymore. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
673 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
674 |
do { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
675 |
assert len1 > 1 && len2 > 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
676 |
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
|
677 |
if (count1 != 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
678 |
System.arraycopy(tmp, cursor1, a, dest, count1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
679 |
dest += count1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
680 |
cursor1 += count1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
681 |
len1 -= count1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
682 |
if (len1 <= 1) // len1 == 1 || len1 == 0 |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
683 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
684 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
685 |
a[dest++] = a[cursor2++]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
686 |
if (--len2 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
687 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
688 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
689 |
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
|
690 |
if (count2 != 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
691 |
System.arraycopy(a, cursor2, a, dest, count2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
692 |
dest += count2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
693 |
cursor2 += count2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
694 |
len2 -= count2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
695 |
if (len2 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
696 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
697 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
698 |
a[dest++] = tmp[cursor1++]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
699 |
if (--len1 == 1) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
700 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
701 |
minGallop--; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
702 |
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
703 |
if (minGallop < 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
704 |
minGallop = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
705 |
minGallop += 2; // Penalize for leaving gallop mode |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
706 |
} // End of "outer" loop |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
707 |
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
|
708 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
709 |
if (len1 == 1) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
710 |
assert len2 > 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
711 |
System.arraycopy(a, cursor2, a, dest, len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
712 |
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
|
713 |
} else if (len1 == 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
714 |
throw new IllegalArgumentException( |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
715 |
"Comparison method violates its general contract!"); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
716 |
} else { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
717 |
assert len2 == 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
718 |
assert len1 > 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
719 |
System.arraycopy(tmp, cursor1, a, dest, len1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
720 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
721 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
722 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
723 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
724 |
* 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
|
725 |
* 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
|
726 |
* may be called if len1 == len2.) |
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 |
* @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
|
729 |
* @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
|
730 |
* @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
|
731 |
* (must be aBase + aLen) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
732 |
* @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
|
733 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
734 |
@SuppressWarnings("unchecked") |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
735 |
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
|
736 |
assert len1 > 0 && len2 > 0 && base1 + len1 == base2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
737 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
738 |
// Copy second run into temp array |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
739 |
Object[] a = this.a; // For performance |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
740 |
Object[] tmp = ensureCapacity(len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
741 |
System.arraycopy(a, base2, tmp, 0, len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
742 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
743 |
int cursor1 = base1 + len1 - 1; // Indexes into a |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
744 |
int cursor2 = len2 - 1; // Indexes into tmp array |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
745 |
int dest = base2 + len2 - 1; // Indexes into a |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
746 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
747 |
// 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
|
748 |
a[dest--] = a[cursor1--]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
749 |
if (--len1 == 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
750 |
System.arraycopy(tmp, 0, a, dest - (len2 - 1), len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
751 |
return; |
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 |
if (len2 == 1) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
754 |
dest -= len1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
755 |
cursor1 -= len1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
756 |
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
|
757 |
a[dest] = tmp[cursor2]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
758 |
return; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
759 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
760 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
761 |
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
|
762 |
outer: |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
763 |
while (true) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
764 |
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
|
765 |
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
|
766 |
|
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 |
* 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
|
769 |
* appears to win consistently. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
770 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
771 |
do { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
772 |
assert len1 > 0 && len2 > 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
773 |
if (((Comparable) tmp[cursor2]).compareTo(a[cursor1]) < 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
774 |
a[dest--] = a[cursor1--]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
775 |
count1++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
776 |
count2 = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
777 |
if (--len1 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
778 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
779 |
} else { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
780 |
a[dest--] = tmp[cursor2--]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
781 |
count2++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
782 |
count1 = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
783 |
if (--len2 == 1) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
784 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
785 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
786 |
} while ((count1 | count2) < minGallop); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
787 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
788 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
789 |
* 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
|
790 |
* 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
|
791 |
* neither run appears to be winning consistently anymore. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
792 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
793 |
do { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
794 |
assert len1 > 0 && len2 > 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
795 |
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
|
796 |
if (count1 != 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
797 |
dest -= count1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
798 |
cursor1 -= count1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
799 |
len1 -= count1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
800 |
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
|
801 |
if (len1 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
802 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
803 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
804 |
a[dest--] = tmp[cursor2--]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
805 |
if (--len2 == 1) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
806 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
807 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
808 |
count2 = len2 - gallopLeft((Comparable) a[cursor1], tmp, 0, len2, len2 - 1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
809 |
if (count2 != 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
810 |
dest -= count2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
811 |
cursor2 -= count2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
812 |
len2 -= count2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
813 |
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
|
814 |
if (len2 <= 1) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
815 |
break outer; // len2 == 1 || len2 == 0 |
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 |
a[dest--] = a[cursor1--]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
818 |
if (--len1 == 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
819 |
break outer; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
820 |
minGallop--; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
821 |
} while (count1 >= MIN_GALLOP | count2 >= MIN_GALLOP); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
822 |
if (minGallop < 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
823 |
minGallop = 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
824 |
minGallop += 2; // Penalize for leaving gallop mode |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
825 |
} // End of "outer" loop |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
826 |
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
|
827 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
828 |
if (len2 == 1) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
829 |
assert len1 > 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
830 |
dest -= len1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
831 |
cursor1 -= len1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
832 |
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
|
833 |
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
|
834 |
} else if (len2 == 0) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
835 |
throw new IllegalArgumentException( |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
836 |
"Comparison method violates its general contract!"); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
837 |
} else { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
838 |
assert len1 == 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
839 |
assert len2 > 0; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
840 |
System.arraycopy(tmp, 0, a, dest - (len2 - 1), len2); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
841 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
842 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
843 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
844 |
/** |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
845 |
* 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
|
846 |
* 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
|
847 |
* increases exponentially to ensure amortized linear time complexity. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
848 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
849 |
* @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
|
850 |
* @return tmp, whether or not it grew |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
851 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
852 |
private Object[] ensureCapacity(int minCapacity) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
853 |
if (tmp.length < minCapacity) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
854 |
// Compute smallest power of 2 > minCapacity |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
855 |
int newSize = minCapacity; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
856 |
newSize |= newSize >> 1; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
857 |
newSize |= newSize >> 2; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
858 |
newSize |= newSize >> 4; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
859 |
newSize |= newSize >> 8; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
860 |
newSize |= newSize >> 16; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
861 |
newSize++; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
862 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
863 |
if (newSize < 0) // Not bloody likely! |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
864 |
newSize = minCapacity; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
865 |
else |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
866 |
newSize = Math.min(newSize, a.length >>> 1); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
867 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
868 |
@SuppressWarnings({"unchecked", "UnnecessaryLocalVariable"}) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
869 |
Object[] newArray = new Object[newSize]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
870 |
tmp = newArray; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
871 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
872 |
return tmp; |
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 |
* Checks that fromIndex and toIndex are in range, and throws an |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
877 |
* appropriate exception if they aren't. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
878 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
879 |
* @param arrayLen the length of the array |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
880 |
* @param fromIndex the index of the first element of the range |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
881 |
* @param toIndex the index after the last element of the range |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
882 |
* @throws IllegalArgumentException if fromIndex > toIndex |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
883 |
* @throws ArrayIndexOutOfBoundsException if fromIndex < 0 |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
884 |
* or toIndex > arrayLen |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
885 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
886 |
private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
887 |
if (fromIndex > toIndex) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
888 |
throw new IllegalArgumentException("fromIndex(" + fromIndex + |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
889 |
") > toIndex(" + toIndex+")"); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
890 |
if (fromIndex < 0) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
891 |
throw new ArrayIndexOutOfBoundsException(fromIndex); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
892 |
if (toIndex > arrayLen) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
893 |
throw new ArrayIndexOutOfBoundsException(toIndex); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
894 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
895 |
} |