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