1 /*
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import jdk.internal.HotSpotIntrinsicCandidate;
29
30 import java.lang.reflect.Array;
31 import java.util.concurrent.ForkJoinPool;
32 import java.util.function.BinaryOperator;
33 import java.util.function.Consumer;
34 import java.util.function.DoubleBinaryOperator;
35 import java.util.function.IntBinaryOperator;
36 import java.util.function.IntFunction;
37 import java.util.function.IntToDoubleFunction;
38 import java.util.function.IntToLongFunction;
39 import java.util.function.IntUnaryOperator;
40 import java.util.function.LongBinaryOperator;
41 import java.util.function.UnaryOperator;
42 import java.util.stream.DoubleStream;
43 import java.util.stream.IntStream;
44 import java.util.stream.LongStream;
45 import java.util.stream.Stream;
46 import java.util.stream.StreamSupport;
47
48 /**
49 * This class contains various methods for manipulating arrays (such as
50 * sorting and searching). This class also contains a static factory
51 * that allows arrays to be viewed as lists.
52 *
53 * <p>The methods in this class all throw a {@code NullPointerException},
54 * if the specified array reference is null, except where noted.
55 *
56 * <p>The documentation for the methods contained in this class includes
57 * brief descriptions of the <i>implementations</i>. Such descriptions should
58 * be regarded as <i>implementation notes</i>, rather than parts of the
59 * <i>specification</i>. Implementors should feel free to substitute other
60 * algorithms, so long as the specification itself is adhered to. (For
61 * example, the algorithm used by {@code sort(Object[])} does not have to be
62 * a MergeSort, but it does have to be <i>stable</i>.)
63 *
64 * <p>This class is a member of the
65 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
66 * Java Collections Framework</a>.
67 *
68 * @author Josh Bloch
69 * @author Neal Gafter
70 * @author John Rose
71 * @since 1.2
72 */
73 public class Arrays {
74
75 /**
76 * The minimum array length below which a parallel sorting
77 * algorithm will not further partition the sorting task. Using
78 * smaller sizes typically results in memory contention across
79 * tasks that makes parallel speedups unlikely.
80 */
81 private static final int MIN_ARRAY_SORT_GRAN = 1 << 13;
82
83 // Suppresses default constructor, ensuring non-instantiability.
84 private Arrays() {}
85
86 /**
87 * A comparator that implements the natural ordering of a group of
88 * mutually comparable elements. May be used when a supplied
89 * comparator is null. To simplify code-sharing within underlying
90 * implementations, the compare method only declares type Object
91 * for its second argument.
92 *
93 * Arrays class implementor's note: It is an empirical matter
94 * whether ComparableTimSort offers any performance benefit over
95 * TimSort used with this comparator. If not, you are better off
96 * deleting or bypassing ComparableTimSort. There is currently no
97 * empirical case for separating them for parallel sorting, so all
98 * public Object parallelSort methods use the same comparator
99 * based implementation.
100 */
101 static final class NaturalOrder implements Comparator<Object> {
102 @SuppressWarnings("unchecked")
103 public int compare(Object first, Object second) {
104 return ((Comparable<Object>)first).compareTo(second);
105 }
106 static final NaturalOrder INSTANCE = new NaturalOrder();
107 }
108
109 /**
110 * Checks that {@code fromIndex} and {@code toIndex} are in
111 * the range and throws an exception if they aren't.
112 */
113 static void rangeCheck(int arrayLength, int fromIndex, int toIndex) {
114 if (fromIndex > toIndex) {
115 throw new IllegalArgumentException(
116 "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
117 }
118 if (fromIndex < 0) {
119 throw new ArrayIndexOutOfBoundsException(fromIndex);
120 }
121 if (toIndex > arrayLength) {
122 throw new ArrayIndexOutOfBoundsException(toIndex);
123 }
124 }
125
126 /*
127 * Sorting methods. Note that all public "sort" methods take the
128 * same form: Performing argument checks if necessary, and then
129 * expanding arguments into those required for the internal
130 * implementation methods residing in other package-private
131 * classes (except for legacyMergeSort, included in this class).
132 */
133
134 /**
135 * Sorts the specified array into ascending numerical order.
136 *
137 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
138 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
139 * offers O(n log(n)) performance on many data sets that cause other
140 * quicksorts to degrade to quadratic performance, and is typically
141 * faster than traditional (one-pivot) Quicksort implementations.
142 *
143 * @param a the array to be sorted
144 */
145 public static void sort(int[] a) {
146 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
147 }
148
149 /**
150 * Sorts the specified range of the array into ascending order. The range
151 * to be sorted extends from the index {@code fromIndex}, inclusive, to
152 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
153 * the range to be sorted is empty.
154 *
155 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
156 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
157 * offers O(n log(n)) performance on many data sets that cause other
158 * quicksorts to degrade to quadratic performance, and is typically
159 * faster than traditional (one-pivot) Quicksort implementations.
160 *
161 * @param a the array to be sorted
162 * @param fromIndex the index of the first element, inclusive, to be sorted
163 * @param toIndex the index of the last element, exclusive, to be sorted
164 *
165 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
166 * @throws ArrayIndexOutOfBoundsException
167 * if {@code fromIndex < 0} or {@code toIndex > a.length}
168 */
169 public static void sort(int[] a, int fromIndex, int toIndex) {
170 rangeCheck(a.length, fromIndex, toIndex);
171 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
172 }
173
174 /**
175 * Sorts the specified array into ascending numerical order.
176 *
177 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
178 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
179 * offers O(n log(n)) performance on many data sets that cause other
180 * quicksorts to degrade to quadratic performance, and is typically
181 * faster than traditional (one-pivot) Quicksort implementations.
182 *
183 * @param a the array to be sorted
184 */
185 public static void sort(long[] a) {
186 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
187 }
188
189 /**
190 * Sorts the specified range of the array into ascending order. The range
191 * to be sorted extends from the index {@code fromIndex}, inclusive, to
192 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
193 * the range to be sorted is empty.
194 *
195 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
196 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
197 * offers O(n log(n)) performance on many data sets that cause other
198 * quicksorts to degrade to quadratic performance, and is typically
199 * faster than traditional (one-pivot) Quicksort implementations.
200 *
201 * @param a the array to be sorted
202 * @param fromIndex the index of the first element, inclusive, to be sorted
203 * @param toIndex the index of the last element, exclusive, to be sorted
204 *
205 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
206 * @throws ArrayIndexOutOfBoundsException
207 * if {@code fromIndex < 0} or {@code toIndex > a.length}
208 */
209 public static void sort(long[] a, int fromIndex, int toIndex) {
210 rangeCheck(a.length, fromIndex, toIndex);
211 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
212 }
213
214 /**
215 * Sorts the specified array into ascending numerical order.
216 *
217 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
218 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
219 * offers O(n log(n)) performance on many data sets that cause other
220 * quicksorts to degrade to quadratic performance, and is typically
221 * faster than traditional (one-pivot) Quicksort implementations.
222 *
223 * @param a the array to be sorted
224 */
225 public static void sort(short[] a) {
226 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
227 }
228
229 /**
230 * Sorts the specified range of the array into ascending order. The range
231 * to be sorted extends from the index {@code fromIndex}, inclusive, to
232 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
233 * the range to be sorted is empty.
234 *
235 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
236 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
237 * offers O(n log(n)) performance on many data sets that cause other
238 * quicksorts to degrade to quadratic performance, and is typically
239 * faster than traditional (one-pivot) Quicksort implementations.
240 *
241 * @param a the array to be sorted
242 * @param fromIndex the index of the first element, inclusive, to be sorted
243 * @param toIndex the index of the last element, exclusive, to be sorted
244 *
245 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
246 * @throws ArrayIndexOutOfBoundsException
247 * if {@code fromIndex < 0} or {@code toIndex > a.length}
248 */
249 public static void sort(short[] a, int fromIndex, int toIndex) {
250 rangeCheck(a.length, fromIndex, toIndex);
251 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
252 }
253
254 /**
255 * Sorts the specified array into ascending numerical order.
256 *
257 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
258 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
259 * offers O(n log(n)) performance on many data sets that cause other
260 * quicksorts to degrade to quadratic performance, and is typically
261 * faster than traditional (one-pivot) Quicksort implementations.
262 *
263 * @param a the array to be sorted
264 */
265 public static void sort(char[] a) {
266 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
267 }
268
269 /**
270 * Sorts the specified range of the array into ascending order. The range
271 * to be sorted extends from the index {@code fromIndex}, inclusive, to
272 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
273 * the range to be sorted is empty.
274 *
275 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
276 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
277 * offers O(n log(n)) performance on many data sets that cause other
278 * quicksorts to degrade to quadratic performance, and is typically
279 * faster than traditional (one-pivot) Quicksort implementations.
280 *
281 * @param a the array to be sorted
282 * @param fromIndex the index of the first element, inclusive, to be sorted
283 * @param toIndex the index of the last element, exclusive, to be sorted
284 *
285 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
286 * @throws ArrayIndexOutOfBoundsException
287 * if {@code fromIndex < 0} or {@code toIndex > a.length}
288 */
289 public static void sort(char[] a, int fromIndex, int toIndex) {
290 rangeCheck(a.length, fromIndex, toIndex);
291 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
292 }
293
294 /**
295 * Sorts the specified array into ascending numerical order.
296 *
297 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
298 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
299 * offers O(n log(n)) performance on many data sets that cause other
300 * quicksorts to degrade to quadratic performance, and is typically
301 * faster than traditional (one-pivot) Quicksort implementations.
302 *
303 * @param a the array to be sorted
304 */
305 public static void sort(byte[] a) {
306 DualPivotQuicksort.sort(a, 0, a.length - 1);
307 }
308
309 /**
310 * Sorts the specified range of the array into ascending order. The range
311 * to be sorted extends from the index {@code fromIndex}, inclusive, to
312 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
313 * the range to be sorted is empty.
314 *
315 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
316 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
317 * offers O(n log(n)) performance on many data sets that cause other
318 * quicksorts to degrade to quadratic performance, and is typically
319 * faster than traditional (one-pivot) Quicksort implementations.
320 *
321 * @param a the array to be sorted
322 * @param fromIndex the index of the first element, inclusive, to be sorted
323 * @param toIndex the index of the last element, exclusive, to be sorted
324 *
325 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
326 * @throws ArrayIndexOutOfBoundsException
327 * if {@code fromIndex < 0} or {@code toIndex > a.length}
328 */
329 public static void sort(byte[] a, int fromIndex, int toIndex) {
330 rangeCheck(a.length, fromIndex, toIndex);
331 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
332 }
333
334 /**
335 * Sorts the specified array into ascending numerical order.
336 *
337 * <p>The {@code <} relation does not provide a total order on all float
338 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
339 * value compares neither less than, greater than, nor equal to any value,
340 * even itself. This method uses the total order imposed by the method
341 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
342 * {@code 0.0f} and {@code Float.NaN} is considered greater than any
343 * other value and all {@code Float.NaN} values are considered equal.
344 *
345 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
346 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
347 * offers O(n log(n)) performance on many data sets that cause other
348 * quicksorts to degrade to quadratic performance, and is typically
349 * faster than traditional (one-pivot) Quicksort implementations.
350 *
351 * @param a the array to be sorted
352 */
353 public static void sort(float[] a) {
354 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
355 }
356
357 /**
358 * Sorts the specified range of the array into ascending order. The range
359 * to be sorted extends from the index {@code fromIndex}, inclusive, to
360 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
361 * the range to be sorted is empty.
362 *
363 * <p>The {@code <} relation does not provide a total order on all float
364 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
365 * value compares neither less than, greater than, nor equal to any value,
366 * even itself. This method uses the total order imposed by the method
367 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
368 * {@code 0.0f} and {@code Float.NaN} is considered greater than any
369 * other value and all {@code Float.NaN} values are considered equal.
370 *
371 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
372 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
373 * offers O(n log(n)) performance on many data sets that cause other
374 * quicksorts to degrade to quadratic performance, and is typically
375 * faster than traditional (one-pivot) Quicksort implementations.
376 *
377 * @param a the array to be sorted
378 * @param fromIndex the index of the first element, inclusive, to be sorted
379 * @param toIndex the index of the last element, exclusive, to be sorted
380 *
381 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
382 * @throws ArrayIndexOutOfBoundsException
383 * if {@code fromIndex < 0} or {@code toIndex > a.length}
384 */
385 public static void sort(float[] a, int fromIndex, int toIndex) {
386 rangeCheck(a.length, fromIndex, toIndex);
387 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
388 }
389
390 /**
391 * Sorts the specified array into ascending numerical order.
392 *
393 * <p>The {@code <} relation does not provide a total order on all double
394 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
395 * value compares neither less than, greater than, nor equal to any value,
396 * even itself. This method uses the total order imposed by the method
397 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
398 * {@code 0.0d} and {@code Double.NaN} is considered greater than any
399 * other value and all {@code Double.NaN} values are considered equal.
400 *
401 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
402 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
403 * offers O(n log(n)) performance on many data sets that cause other
404 * quicksorts to degrade to quadratic performance, and is typically
405 * faster than traditional (one-pivot) Quicksort implementations.
406 *
407 * @param a the array to be sorted
408 */
409 public static void sort(double[] a) {
410 DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
411 }
412
413 /**
414 * Sorts the specified range of the array into ascending order. The range
415 * to be sorted extends from the index {@code fromIndex}, inclusive, to
416 * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex},
417 * the range to be sorted is empty.
418 *
419 * <p>The {@code <} relation does not provide a total order on all double
420 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
421 * value compares neither less than, greater than, nor equal to any value,
422 * even itself. This method uses the total order imposed by the method
423 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
424 * {@code 0.0d} and {@code Double.NaN} is considered greater than any
425 * other value and all {@code Double.NaN} values are considered equal.
426 *
427 * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort
428 * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm
429 * offers O(n log(n)) performance on many data sets that cause other
430 * quicksorts to degrade to quadratic performance, and is typically
431 * faster than traditional (one-pivot) Quicksort implementations.
432 *
433 * @param a the array to be sorted
434 * @param fromIndex the index of the first element, inclusive, to be sorted
435 * @param toIndex the index of the last element, exclusive, to be sorted
436 *
437 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
438 * @throws ArrayIndexOutOfBoundsException
439 * if {@code fromIndex < 0} or {@code toIndex > a.length}
440 */
441 public static void sort(double[] a, int fromIndex, int toIndex) {
442 rangeCheck(a.length, fromIndex, toIndex);
443 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
444 }
445
446 /**
447 * Sorts the specified array into ascending numerical order.
448 *
449 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
450 * array into sub-arrays that are themselves sorted and then merged. When
451 * the sub-array length reaches a minimum granularity, the sub-array is
452 * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
453 * method. If the length of the specified array is less than the minimum
454 * granularity, then it is sorted using the appropriate {@link
455 * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a
456 * working space no greater than the size of the original array. The
457 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
458 * execute any parallel tasks.
459 *
460 * @param a the array to be sorted
461 *
462 * @since 1.8
463 */
464 public static void parallelSort(byte[] a) {
465 int n = a.length, p, g;
466 if (n <= MIN_ARRAY_SORT_GRAN ||
467 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
468 DualPivotQuicksort.sort(a, 0, n - 1);
469 else
470 new ArraysParallelSortHelpers.FJByte.Sorter
471 (null, a, new byte[n], 0, n, 0,
472 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
473 MIN_ARRAY_SORT_GRAN : g).invoke();
474 }
475
476 /**
477 * Sorts the specified range of the array into ascending numerical order.
478 * The range to be sorted extends from the index {@code fromIndex},
479 * inclusive, to the index {@code toIndex}, exclusive. If
480 * {@code fromIndex == toIndex}, the range to be sorted is empty.
481 *
482 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
483 * array into sub-arrays that are themselves sorted and then merged. When
484 * the sub-array length reaches a minimum granularity, the sub-array is
485 * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort}
486 * method. If the length of the specified array is less than the minimum
487 * granularity, then it is sorted using the appropriate {@link
488 * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working
489 * space no greater than the size of the specified range of the original
490 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
491 * used to execute any parallel tasks.
492 *
493 * @param a the array to be sorted
494 * @param fromIndex the index of the first element, inclusive, to be sorted
495 * @param toIndex the index of the last element, exclusive, to be sorted
496 *
497 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
498 * @throws ArrayIndexOutOfBoundsException
499 * if {@code fromIndex < 0} or {@code toIndex > a.length}
500 *
501 * @since 1.8
502 */
503 public static void parallelSort(byte[] a, int fromIndex, int toIndex) {
504 rangeCheck(a.length, fromIndex, toIndex);
505 int n = toIndex - fromIndex, p, g;
506 if (n <= MIN_ARRAY_SORT_GRAN ||
507 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
508 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1);
509 else
510 new ArraysParallelSortHelpers.FJByte.Sorter
511 (null, a, new byte[n], fromIndex, n, 0,
512 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
513 MIN_ARRAY_SORT_GRAN : g).invoke();
514 }
515
516 /**
517 * Sorts the specified array into ascending numerical order.
518 *
519 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
520 * array into sub-arrays that are themselves sorted and then merged. When
521 * the sub-array length reaches a minimum granularity, the sub-array is
522 * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
523 * method. If the length of the specified array is less than the minimum
524 * granularity, then it is sorted using the appropriate {@link
525 * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a
526 * working space no greater than the size of the original array. The
527 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
528 * execute any parallel tasks.
529 *
530 * @param a the array to be sorted
531 *
532 * @since 1.8
533 */
534 public static void parallelSort(char[] a) {
535 int n = a.length, p, g;
536 if (n <= MIN_ARRAY_SORT_GRAN ||
537 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
538 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
539 else
540 new ArraysParallelSortHelpers.FJChar.Sorter
541 (null, a, new char[n], 0, n, 0,
542 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
543 MIN_ARRAY_SORT_GRAN : g).invoke();
544 }
545
546 /**
547 * Sorts the specified range of the array into ascending numerical order.
548 * The range to be sorted extends from the index {@code fromIndex},
549 * inclusive, to the index {@code toIndex}, exclusive. If
550 * {@code fromIndex == toIndex}, the range to be sorted is empty.
551 *
552 @implNote The sorting algorithm is a parallel sort-merge that breaks the
553 * array into sub-arrays that are themselves sorted and then merged. When
554 * the sub-array length reaches a minimum granularity, the sub-array is
555 * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort}
556 * method. If the length of the specified array is less than the minimum
557 * granularity, then it is sorted using the appropriate {@link
558 * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working
559 * space no greater than the size of the specified range of the original
560 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
561 * used to execute any parallel tasks.
562 *
563 * @param a the array to be sorted
564 * @param fromIndex the index of the first element, inclusive, to be sorted
565 * @param toIndex the index of the last element, exclusive, to be sorted
566 *
567 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
568 * @throws ArrayIndexOutOfBoundsException
569 * if {@code fromIndex < 0} or {@code toIndex > a.length}
570 *
571 * @since 1.8
572 */
573 public static void parallelSort(char[] a, int fromIndex, int toIndex) {
574 rangeCheck(a.length, fromIndex, toIndex);
575 int n = toIndex - fromIndex, p, g;
576 if (n <= MIN_ARRAY_SORT_GRAN ||
577 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
578 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
579 else
580 new ArraysParallelSortHelpers.FJChar.Sorter
581 (null, a, new char[n], fromIndex, n, 0,
582 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
583 MIN_ARRAY_SORT_GRAN : g).invoke();
584 }
585
586 /**
587 * Sorts the specified array into ascending numerical order.
588 *
589 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
590 * array into sub-arrays that are themselves sorted and then merged. When
591 * the sub-array length reaches a minimum granularity, the sub-array is
592 * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
593 * method. If the length of the specified array is less than the minimum
594 * granularity, then it is sorted using the appropriate {@link
595 * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a
596 * working space no greater than the size of the original array. The
597 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
598 * execute any parallel tasks.
599 *
600 * @param a the array to be sorted
601 *
602 * @since 1.8
603 */
604 public static void parallelSort(short[] a) {
605 int n = a.length, p, g;
606 if (n <= MIN_ARRAY_SORT_GRAN ||
607 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
608 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
609 else
610 new ArraysParallelSortHelpers.FJShort.Sorter
611 (null, a, new short[n], 0, n, 0,
612 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
613 MIN_ARRAY_SORT_GRAN : g).invoke();
614 }
615
616 /**
617 * Sorts the specified range of the array into ascending numerical order.
618 * The range to be sorted extends from the index {@code fromIndex},
619 * inclusive, to the index {@code toIndex}, exclusive. If
620 * {@code fromIndex == toIndex}, the range to be sorted is empty.
621 *
622 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
623 * array into sub-arrays that are themselves sorted and then merged. When
624 * the sub-array length reaches a minimum granularity, the sub-array is
625 * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort}
626 * method. If the length of the specified array is less than the minimum
627 * granularity, then it is sorted using the appropriate {@link
628 * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working
629 * space no greater than the size of the specified range of the original
630 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
631 * used to execute any parallel tasks.
632 *
633 * @param a the array to be sorted
634 * @param fromIndex the index of the first element, inclusive, to be sorted
635 * @param toIndex the index of the last element, exclusive, to be sorted
636 *
637 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
638 * @throws ArrayIndexOutOfBoundsException
639 * if {@code fromIndex < 0} or {@code toIndex > a.length}
640 *
641 * @since 1.8
642 */
643 public static void parallelSort(short[] a, int fromIndex, int toIndex) {
644 rangeCheck(a.length, fromIndex, toIndex);
645 int n = toIndex - fromIndex, p, g;
646 if (n <= MIN_ARRAY_SORT_GRAN ||
647 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
648 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
649 else
650 new ArraysParallelSortHelpers.FJShort.Sorter
651 (null, a, new short[n], fromIndex, n, 0,
652 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
653 MIN_ARRAY_SORT_GRAN : g).invoke();
654 }
655
656 /**
657 * Sorts the specified array into ascending numerical order.
658 *
659 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
660 * array into sub-arrays that are themselves sorted and then merged. When
661 * the sub-array length reaches a minimum granularity, the sub-array is
662 * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
663 * method. If the length of the specified array is less than the minimum
664 * granularity, then it is sorted using the appropriate {@link
665 * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a
666 * working space no greater than the size of the original array. The
667 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
668 * execute any parallel tasks.
669 *
670 * @param a the array to be sorted
671 *
672 * @since 1.8
673 */
674 public static void parallelSort(int[] a) {
675 int n = a.length, p, g;
676 if (n <= MIN_ARRAY_SORT_GRAN ||
677 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
678 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
679 else
680 new ArraysParallelSortHelpers.FJInt.Sorter
681 (null, a, new int[n], 0, n, 0,
682 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
683 MIN_ARRAY_SORT_GRAN : g).invoke();
684 }
685
686 /**
687 * Sorts the specified range of the array into ascending numerical order.
688 * The range to be sorted extends from the index {@code fromIndex},
689 * inclusive, to the index {@code toIndex}, exclusive. If
690 * {@code fromIndex == toIndex}, the range to be sorted is empty.
691 *
692 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
693 * array into sub-arrays that are themselves sorted and then merged. When
694 * the sub-array length reaches a minimum granularity, the sub-array is
695 * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort}
696 * method. If the length of the specified array is less than the minimum
697 * granularity, then it is sorted using the appropriate {@link
698 * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working
699 * space no greater than the size of the specified range of the original
700 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
701 * used to execute any parallel tasks.
702 *
703 * @param a the array to be sorted
704 * @param fromIndex the index of the first element, inclusive, to be sorted
705 * @param toIndex the index of the last element, exclusive, to be sorted
706 *
707 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
708 * @throws ArrayIndexOutOfBoundsException
709 * if {@code fromIndex < 0} or {@code toIndex > a.length}
710 *
711 * @since 1.8
712 */
713 public static void parallelSort(int[] a, int fromIndex, int toIndex) {
714 rangeCheck(a.length, fromIndex, toIndex);
715 int n = toIndex - fromIndex, p, g;
716 if (n <= MIN_ARRAY_SORT_GRAN ||
717 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
718 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
719 else
720 new ArraysParallelSortHelpers.FJInt.Sorter
721 (null, a, new int[n], fromIndex, n, 0,
722 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
723 MIN_ARRAY_SORT_GRAN : g).invoke();
724 }
725
726 /**
727 * Sorts the specified array into ascending numerical order.
728 *
729 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
730 * array into sub-arrays that are themselves sorted and then merged. When
731 * the sub-array length reaches a minimum granularity, the sub-array is
732 * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
733 * method. If the length of the specified array is less than the minimum
734 * granularity, then it is sorted using the appropriate {@link
735 * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a
736 * working space no greater than the size of the original array. The
737 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
738 * execute any parallel tasks.
739 *
740 * @param a the array to be sorted
741 *
742 * @since 1.8
743 */
744 public static void parallelSort(long[] a) {
745 int n = a.length, p, g;
746 if (n <= MIN_ARRAY_SORT_GRAN ||
747 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
748 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
749 else
750 new ArraysParallelSortHelpers.FJLong.Sorter
751 (null, a, new long[n], 0, n, 0,
752 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
753 MIN_ARRAY_SORT_GRAN : g).invoke();
754 }
755
756 /**
757 * Sorts the specified range of the array into ascending numerical order.
758 * The range to be sorted extends from the index {@code fromIndex},
759 * inclusive, to the index {@code toIndex}, exclusive. If
760 * {@code fromIndex == toIndex}, the range to be sorted is empty.
761 *
762 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
763 * array into sub-arrays that are themselves sorted and then merged. When
764 * the sub-array length reaches a minimum granularity, the sub-array is
765 * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort}
766 * method. If the length of the specified array is less than the minimum
767 * granularity, then it is sorted using the appropriate {@link
768 * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working
769 * space no greater than the size of the specified range of the original
770 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
771 * used to execute any parallel tasks.
772 *
773 * @param a the array to be sorted
774 * @param fromIndex the index of the first element, inclusive, to be sorted
775 * @param toIndex the index of the last element, exclusive, to be sorted
776 *
777 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
778 * @throws ArrayIndexOutOfBoundsException
779 * if {@code fromIndex < 0} or {@code toIndex > a.length}
780 *
781 * @since 1.8
782 */
783 public static void parallelSort(long[] a, int fromIndex, int toIndex) {
784 rangeCheck(a.length, fromIndex, toIndex);
785 int n = toIndex - fromIndex, p, g;
786 if (n <= MIN_ARRAY_SORT_GRAN ||
787 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
788 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
789 else
790 new ArraysParallelSortHelpers.FJLong.Sorter
791 (null, a, new long[n], fromIndex, n, 0,
792 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
793 MIN_ARRAY_SORT_GRAN : g).invoke();
794 }
795
796 /**
797 * Sorts the specified array into ascending numerical order.
798 *
799 * <p>The {@code <} relation does not provide a total order on all float
800 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
801 * value compares neither less than, greater than, nor equal to any value,
802 * even itself. This method uses the total order imposed by the method
803 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
804 * {@code 0.0f} and {@code Float.NaN} is considered greater than any
805 * other value and all {@code Float.NaN} values are considered equal.
806 *
807 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
808 * array into sub-arrays that are themselves sorted and then merged. When
809 * the sub-array length reaches a minimum granularity, the sub-array is
810 * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
811 * method. If the length of the specified array is less than the minimum
812 * granularity, then it is sorted using the appropriate {@link
813 * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a
814 * working space no greater than the size of the original array. The
815 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
816 * execute any parallel tasks.
817 *
818 * @param a the array to be sorted
819 *
820 * @since 1.8
821 */
822 public static void parallelSort(float[] a) {
823 int n = a.length, p, g;
824 if (n <= MIN_ARRAY_SORT_GRAN ||
825 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
826 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
827 else
828 new ArraysParallelSortHelpers.FJFloat.Sorter
829 (null, a, new float[n], 0, n, 0,
830 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
831 MIN_ARRAY_SORT_GRAN : g).invoke();
832 }
833
834 /**
835 * Sorts the specified range of the array into ascending numerical order.
836 * The range to be sorted extends from the index {@code fromIndex},
837 * inclusive, to the index {@code toIndex}, exclusive. If
838 * {@code fromIndex == toIndex}, the range to be sorted is empty.
839 *
840 * <p>The {@code <} relation does not provide a total order on all float
841 * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN}
842 * value compares neither less than, greater than, nor equal to any value,
843 * even itself. This method uses the total order imposed by the method
844 * {@link Float#compareTo}: {@code -0.0f} is treated as less than value
845 * {@code 0.0f} and {@code Float.NaN} is considered greater than any
846 * other value and all {@code Float.NaN} values are considered equal.
847 *
848 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
849 * array into sub-arrays that are themselves sorted and then merged. When
850 * the sub-array length reaches a minimum granularity, the sub-array is
851 * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort}
852 * method. If the length of the specified array is less than the minimum
853 * granularity, then it is sorted using the appropriate {@link
854 * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working
855 * space no greater than the size of the specified range of the original
856 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
857 * used to execute any parallel tasks.
858 *
859 * @param a the array to be sorted
860 * @param fromIndex the index of the first element, inclusive, to be sorted
861 * @param toIndex the index of the last element, exclusive, to be sorted
862 *
863 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
864 * @throws ArrayIndexOutOfBoundsException
865 * if {@code fromIndex < 0} or {@code toIndex > a.length}
866 *
867 * @since 1.8
868 */
869 public static void parallelSort(float[] a, int fromIndex, int toIndex) {
870 rangeCheck(a.length, fromIndex, toIndex);
871 int n = toIndex - fromIndex, p, g;
872 if (n <= MIN_ARRAY_SORT_GRAN ||
873 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
874 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
875 else
876 new ArraysParallelSortHelpers.FJFloat.Sorter
877 (null, a, new float[n], fromIndex, n, 0,
878 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
879 MIN_ARRAY_SORT_GRAN : g).invoke();
880 }
881
882 /**
883 * Sorts the specified array into ascending numerical order.
884 *
885 * <p>The {@code <} relation does not provide a total order on all double
886 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
887 * value compares neither less than, greater than, nor equal to any value,
888 * even itself. This method uses the total order imposed by the method
889 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
890 * {@code 0.0d} and {@code Double.NaN} is considered greater than any
891 * other value and all {@code Double.NaN} values are considered equal.
892 *
893 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
894 * array into sub-arrays that are themselves sorted and then merged. When
895 * the sub-array length reaches a minimum granularity, the sub-array is
896 * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
897 * method. If the length of the specified array is less than the minimum
898 * granularity, then it is sorted using the appropriate {@link
899 * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a
900 * working space no greater than the size of the original array. The
901 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
902 * execute any parallel tasks.
903 *
904 * @param a the array to be sorted
905 *
906 * @since 1.8
907 */
908 public static void parallelSort(double[] a) {
909 int n = a.length, p, g;
910 if (n <= MIN_ARRAY_SORT_GRAN ||
911 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
912 DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0);
913 else
914 new ArraysParallelSortHelpers.FJDouble.Sorter
915 (null, a, new double[n], 0, n, 0,
916 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
917 MIN_ARRAY_SORT_GRAN : g).invoke();
918 }
919
920 /**
921 * Sorts the specified range of the array into ascending numerical order.
922 * The range to be sorted extends from the index {@code fromIndex},
923 * inclusive, to the index {@code toIndex}, exclusive. If
924 * {@code fromIndex == toIndex}, the range to be sorted is empty.
925 *
926 * <p>The {@code <} relation does not provide a total order on all double
927 * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN}
928 * value compares neither less than, greater than, nor equal to any value,
929 * even itself. This method uses the total order imposed by the method
930 * {@link Double#compareTo}: {@code -0.0d} is treated as less than value
931 * {@code 0.0d} and {@code Double.NaN} is considered greater than any
932 * other value and all {@code Double.NaN} values are considered equal.
933 *
934 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
935 * array into sub-arrays that are themselves sorted and then merged. When
936 * the sub-array length reaches a minimum granularity, the sub-array is
937 * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort}
938 * method. If the length of the specified array is less than the minimum
939 * granularity, then it is sorted using the appropriate {@link
940 * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working
941 * space no greater than the size of the specified range of the original
942 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
943 * used to execute any parallel tasks.
944 *
945 * @param a the array to be sorted
946 * @param fromIndex the index of the first element, inclusive, to be sorted
947 * @param toIndex the index of the last element, exclusive, to be sorted
948 *
949 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
950 * @throws ArrayIndexOutOfBoundsException
951 * if {@code fromIndex < 0} or {@code toIndex > a.length}
952 *
953 * @since 1.8
954 */
955 public static void parallelSort(double[] a, int fromIndex, int toIndex) {
956 rangeCheck(a.length, fromIndex, toIndex);
957 int n = toIndex - fromIndex, p, g;
958 if (n <= MIN_ARRAY_SORT_GRAN ||
959 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
960 DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
961 else
962 new ArraysParallelSortHelpers.FJDouble.Sorter
963 (null, a, new double[n], fromIndex, n, 0,
964 ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
965 MIN_ARRAY_SORT_GRAN : g).invoke();
966 }
967
968 /**
969 * Sorts the specified array of objects into ascending order, according
970 * to the {@linkplain Comparable natural ordering} of its elements.
971 * All elements in the array must implement the {@link Comparable}
972 * interface. Furthermore, all elements in the array must be
973 * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
974 * not throw a {@code ClassCastException} for any elements {@code e1}
975 * and {@code e2} in the array).
976 *
977 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
978 * not be reordered as a result of the sort.
979 *
980 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
981 * array into sub-arrays that are themselves sorted and then merged. When
982 * the sub-array length reaches a minimum granularity, the sub-array is
983 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
984 * method. If the length of the specified array is less than the minimum
985 * granularity, then it is sorted using the appropriate {@link
986 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
987 * working space no greater than the size of the original array. The
988 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
989 * execute any parallel tasks.
990 *
991 * @param <T> the class of the objects to be sorted
992 * @param a the array to be sorted
993 *
994 * @throws ClassCastException if the array contains elements that are not
995 * <i>mutually comparable</i> (for example, strings and integers)
996 * @throws IllegalArgumentException (optional) if the natural
997 * ordering of the array elements is found to violate the
998 * {@link Comparable} contract
999 *
1000 * @since 1.8
1001 */
1002 @SuppressWarnings("unchecked")
1003 public static <T extends Comparable<? super T>> void parallelSort(T[] a) {
1004 int n = a.length, p, g;
1005 if (n <= MIN_ARRAY_SORT_GRAN ||
1006 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1007 TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0);
1008 else
1009 new ArraysParallelSortHelpers.FJObject.Sorter<>
1010 (null, a,
1011 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1012 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1013 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
1014 }
1015
1016 /**
1017 * Sorts the specified range of the specified array of objects into
1018 * ascending order, according to the
1019 * {@linkplain Comparable natural ordering} of its
1020 * elements. The range to be sorted extends from index
1021 * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
1022 * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All
1023 * elements in this range must implement the {@link Comparable}
1024 * interface. Furthermore, all elements in this range must be <i>mutually
1025 * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
1026 * {@code ClassCastException} for any elements {@code e1} and
1027 * {@code e2} in the array).
1028 *
1029 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
1030 * not be reordered as a result of the sort.
1031 *
1032 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
1033 * array into sub-arrays that are themselves sorted and then merged. When
1034 * the sub-array length reaches a minimum granularity, the sub-array is
1035 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
1036 * method. If the length of the specified array is less than the minimum
1037 * granularity, then it is sorted using the appropriate {@link
1038 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
1039 * space no greater than the size of the specified range of the original
1040 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
1041 * used to execute any parallel tasks.
1042 *
1043 * @param <T> the class of the objects to be sorted
1044 * @param a the array to be sorted
1045 * @param fromIndex the index of the first element (inclusive) to be
1046 * sorted
1047 * @param toIndex the index of the last element (exclusive) to be sorted
1048 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
1049 * (optional) if the natural ordering of the array elements is
1050 * found to violate the {@link Comparable} contract
1051 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
1052 * {@code toIndex > a.length}
1053 * @throws ClassCastException if the array contains elements that are
1054 * not <i>mutually comparable</i> (for example, strings and
1055 * integers).
1056 *
1057 * @since 1.8
1058 */
1059 @SuppressWarnings("unchecked")
1060 public static <T extends Comparable<? super T>>
1061 void parallelSort(T[] a, int fromIndex, int toIndex) {
1062 rangeCheck(a.length, fromIndex, toIndex);
1063 int n = toIndex - fromIndex, p, g;
1064 if (n <= MIN_ARRAY_SORT_GRAN ||
1065 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1066 TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0);
1067 else
1068 new ArraysParallelSortHelpers.FJObject.Sorter<>
1069 (null, a,
1070 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1071 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1072 MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke();
1073 }
1074
1075 /**
1076 * Sorts the specified array of objects according to the order induced by
1077 * the specified comparator. All elements in the array must be
1078 * <i>mutually comparable</i> by the specified comparator (that is,
1079 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
1080 * for any elements {@code e1} and {@code e2} in the array).
1081 *
1082 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
1083 * not be reordered as a result of the sort.
1084 *
1085 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
1086 * array into sub-arrays that are themselves sorted and then merged. When
1087 * the sub-array length reaches a minimum granularity, the sub-array is
1088 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
1089 * method. If the length of the specified array is less than the minimum
1090 * granularity, then it is sorted using the appropriate {@link
1091 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a
1092 * working space no greater than the size of the original array. The
1093 * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to
1094 * execute any parallel tasks.
1095 *
1096 * @param <T> the class of the objects to be sorted
1097 * @param a the array to be sorted
1098 * @param cmp the comparator to determine the order of the array. A
1099 * {@code null} value indicates that the elements'
1100 * {@linkplain Comparable natural ordering} should be used.
1101 * @throws ClassCastException if the array contains elements that are
1102 * not <i>mutually comparable</i> using the specified comparator
1103 * @throws IllegalArgumentException (optional) if the comparator is
1104 * found to violate the {@link java.util.Comparator} contract
1105 *
1106 * @since 1.8
1107 */
1108 @SuppressWarnings("unchecked")
1109 public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) {
1110 if (cmp == null)
1111 cmp = NaturalOrder.INSTANCE;
1112 int n = a.length, p, g;
1113 if (n <= MIN_ARRAY_SORT_GRAN ||
1114 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1115 TimSort.sort(a, 0, n, cmp, null, 0, 0);
1116 else
1117 new ArraysParallelSortHelpers.FJObject.Sorter<>
1118 (null, a,
1119 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1120 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1121 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
1122 }
1123
1124 /**
1125 * Sorts the specified range of the specified array of objects according
1126 * to the order induced by the specified comparator. The range to be
1127 * sorted extends from index {@code fromIndex}, inclusive, to index
1128 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
1129 * range to be sorted is empty.) All elements in the range must be
1130 * <i>mutually comparable</i> by the specified comparator (that is,
1131 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
1132 * for any elements {@code e1} and {@code e2} in the range).
1133 *
1134 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
1135 * not be reordered as a result of the sort.
1136 *
1137 * @implNote The sorting algorithm is a parallel sort-merge that breaks the
1138 * array into sub-arrays that are themselves sorted and then merged. When
1139 * the sub-array length reaches a minimum granularity, the sub-array is
1140 * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort}
1141 * method. If the length of the specified array is less than the minimum
1142 * granularity, then it is sorted using the appropriate {@link
1143 * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working
1144 * space no greater than the size of the specified range of the original
1145 * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is
1146 * used to execute any parallel tasks.
1147 *
1148 * @param <T> the class of the objects to be sorted
1149 * @param a the array to be sorted
1150 * @param fromIndex the index of the first element (inclusive) to be
1151 * sorted
1152 * @param toIndex the index of the last element (exclusive) to be sorted
1153 * @param cmp the comparator to determine the order of the array. A
1154 * {@code null} value indicates that the elements'
1155 * {@linkplain Comparable natural ordering} should be used.
1156 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
1157 * (optional) if the natural ordering of the array elements is
1158 * found to violate the {@link Comparable} contract
1159 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
1160 * {@code toIndex > a.length}
1161 * @throws ClassCastException if the array contains elements that are
1162 * not <i>mutually comparable</i> (for example, strings and
1163 * integers).
1164 *
1165 * @since 1.8
1166 */
1167 @SuppressWarnings("unchecked")
1168 public static <T> void parallelSort(T[] a, int fromIndex, int toIndex,
1169 Comparator<? super T> cmp) {
1170 rangeCheck(a.length, fromIndex, toIndex);
1171 if (cmp == null)
1172 cmp = NaturalOrder.INSTANCE;
1173 int n = toIndex - fromIndex, p, g;
1174 if (n <= MIN_ARRAY_SORT_GRAN ||
1175 (p = ForkJoinPool.getCommonPoolParallelism()) == 1)
1176 TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0);
1177 else
1178 new ArraysParallelSortHelpers.FJObject.Sorter<>
1179 (null, a,
1180 (T[])Array.newInstance(a.getClass().getComponentType(), n),
1181 fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ?
1182 MIN_ARRAY_SORT_GRAN : g, cmp).invoke();
1183 }
1184
1185 /*
1186 * Sorting of complex type arrays.
1187 */
1188
1189 /**
1190 * Old merge sort implementation can be selected (for
1191 * compatibility with broken comparators) using a system property.
1192 * Cannot be a static boolean in the enclosing class due to
1193 * circular dependencies. To be removed in a future release.
1194 */
1195 static final class LegacyMergeSort {
1196 private static final boolean userRequested =
1197 java.security.AccessController.doPrivileged(
1198 new sun.security.action.GetBooleanAction(
1199 "java.util.Arrays.useLegacyMergeSort")).booleanValue();
1200 }
1201
1202 /**
1203 * Sorts the specified array of objects into ascending order, according
1204 * to the {@linkplain Comparable natural ordering} of its elements.
1205 * All elements in the array must implement the {@link Comparable}
1206 * interface. Furthermore, all elements in the array must be
1207 * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must
1208 * not throw a {@code ClassCastException} for any elements {@code e1}
1209 * and {@code e2} in the array).
1210 *
1211 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
1212 * not be reordered as a result of the sort.
1213 *
1214 * <p>Implementation note: This implementation is a stable, adaptive,
1215 * iterative mergesort that requires far fewer than n lg(n) comparisons
1216 * when the input array is partially sorted, while offering the
1217 * performance of a traditional mergesort when the input array is
1218 * randomly ordered. If the input array is nearly sorted, the
1219 * implementation requires approximately n comparisons. Temporary
1220 * storage requirements vary from a small constant for nearly sorted
1221 * input arrays to n/2 object references for randomly ordered input
1222 * arrays.
1223 *
1224 * <p>The implementation takes equal advantage of ascending and
1225 * descending order in its input array, and can take advantage of
1226 * ascending and descending order in different parts of the same
1227 * input array. It is well-suited to merging two or more sorted arrays:
1228 * simply concatenate the arrays and sort the resulting array.
1229 *
1230 * <p>The implementation was adapted from Tim Peters's list sort for Python
1231 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1232 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic
1233 * Sorting and Information Theoretic Complexity", in Proceedings of the
1234 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
1235 * January 1993.
1236 *
1237 * @param a the array to be sorted
1238 * @throws ClassCastException if the array contains elements that are not
1239 * <i>mutually comparable</i> (for example, strings and integers)
1240 * @throws IllegalArgumentException (optional) if the natural
1241 * ordering of the array elements is found to violate the
1242 * {@link Comparable} contract
1243 */
1244 public static void sort(Object[] a) {
1245 if (LegacyMergeSort.userRequested)
1246 legacyMergeSort(a);
1247 else
1248 ComparableTimSort.sort(a, 0, a.length, null, 0, 0);
1249 }
1250
1251 /** To be removed in a future release. */
1252 private static void legacyMergeSort(Object[] a) {
1253 Object[] aux = a.clone();
1254 mergeSort(aux, a, 0, a.length, 0);
1255 }
1256
1257 /**
1258 * Sorts the specified range of the specified array of objects into
1259 * ascending order, according to the
1260 * {@linkplain Comparable natural ordering} of its
1261 * elements. The range to be sorted extends from index
1262 * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive.
1263 * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All
1264 * elements in this range must implement the {@link Comparable}
1265 * interface. Furthermore, all elements in this range must be <i>mutually
1266 * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a
1267 * {@code ClassCastException} for any elements {@code e1} and
1268 * {@code e2} in the array).
1269 *
1270 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
1271 * not be reordered as a result of the sort.
1272 *
1273 * <p>Implementation note: This implementation is a stable, adaptive,
1274 * iterative mergesort that requires far fewer than n lg(n) comparisons
1275 * when the input array is partially sorted, while offering the
1276 * performance of a traditional mergesort when the input array is
1277 * randomly ordered. If the input array is nearly sorted, the
1278 * implementation requires approximately n comparisons. Temporary
1279 * storage requirements vary from a small constant for nearly sorted
1280 * input arrays to n/2 object references for randomly ordered input
1281 * arrays.
1282 *
1283 * <p>The implementation takes equal advantage of ascending and
1284 * descending order in its input array, and can take advantage of
1285 * ascending and descending order in different parts of the same
1286 * input array. It is well-suited to merging two or more sorted arrays:
1287 * simply concatenate the arrays and sort the resulting array.
1288 *
1289 * <p>The implementation was adapted from Tim Peters's list sort for Python
1290 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1291 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic
1292 * Sorting and Information Theoretic Complexity", in Proceedings of the
1293 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
1294 * January 1993.
1295 *
1296 * @param a the array to be sorted
1297 * @param fromIndex the index of the first element (inclusive) to be
1298 * sorted
1299 * @param toIndex the index of the last element (exclusive) to be sorted
1300 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
1301 * (optional) if the natural ordering of the array elements is
1302 * found to violate the {@link Comparable} contract
1303 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
1304 * {@code toIndex > a.length}
1305 * @throws ClassCastException if the array contains elements that are
1306 * not <i>mutually comparable</i> (for example, strings and
1307 * integers).
1308 */
1309 public static void sort(Object[] a, int fromIndex, int toIndex) {
1310 rangeCheck(a.length, fromIndex, toIndex);
1311 if (LegacyMergeSort.userRequested)
1312 legacyMergeSort(a, fromIndex, toIndex);
1313 else
1314 ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0);
1315 }
1316
1317 /** To be removed in a future release. */
1318 private static void legacyMergeSort(Object[] a,
1319 int fromIndex, int toIndex) {
1320 Object[] aux = copyOfRange(a, fromIndex, toIndex);
1321 mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
1322 }
1323
1324 /**
1325 * Tuning parameter: list size at or below which insertion sort will be
1326 * used in preference to mergesort.
1327 * To be removed in a future release.
1328 */
1329 private static final int INSERTIONSORT_THRESHOLD = 7;
1330
1331 /**
1332 * Src is the source array that starts at index 0
1333 * Dest is the (possibly larger) array destination with a possible offset
1334 * low is the index in dest to start sorting
1335 * high is the end index in dest to end sorting
1336 * off is the offset to generate corresponding low, high in src
1337 * To be removed in a future release.
1338 */
1339 @SuppressWarnings({"unchecked", "rawtypes"})
1340 private static void mergeSort(Object[] src,
1341 Object[] dest,
1342 int low,
1343 int high,
1344 int off) {
1345 int length = high - low;
1346
1347 // Insertion sort on smallest arrays
1348 if (length < INSERTIONSORT_THRESHOLD) {
1349 for (int i=low; i<high; i++)
1350 for (int j=i; j>low &&
1351 ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
1352 swap(dest, j, j-1);
1353 return;
1354 }
1355
1356 // Recursively sort halves of dest into src
1357 int destLow = low;
1358 int destHigh = high;
1359 low += off;
1360 high += off;
1361 int mid = (low + high) >>> 1;
1362 mergeSort(dest, src, low, mid, -off);
1363 mergeSort(dest, src, mid, high, -off);
1364
1365 // If list is already sorted, just copy from src to dest. This is an
1366 // optimization that results in faster sorts for nearly ordered lists.
1367 if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
1368 System.arraycopy(src, low, dest, destLow, length);
1369 return;
1370 }
1371
1372 // Merge sorted halves (now in src) into dest
1373 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
1374 if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
1375 dest[i] = src[p++];
1376 else
1377 dest[i] = src[q++];
1378 }
1379 }
1380
1381 /**
1382 * Swaps x[a] with x[b].
1383 */
1384 private static void swap(Object[] x, int a, int b) {
1385 Object t = x[a];
1386 x[a] = x[b];
1387 x[b] = t;
1388 }
1389
1390 /**
1391 * Sorts the specified array of objects according to the order induced by
1392 * the specified comparator. All elements in the array must be
1393 * <i>mutually comparable</i> by the specified comparator (that is,
1394 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
1395 * for any elements {@code e1} and {@code e2} in the array).
1396 *
1397 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
1398 * not be reordered as a result of the sort.
1399 *
1400 * <p>Implementation note: This implementation is a stable, adaptive,
1401 * iterative mergesort that requires far fewer than n lg(n) comparisons
1402 * when the input array is partially sorted, while offering the
1403 * performance of a traditional mergesort when the input array is
1404 * randomly ordered. If the input array is nearly sorted, the
1405 * implementation requires approximately n comparisons. Temporary
1406 * storage requirements vary from a small constant for nearly sorted
1407 * input arrays to n/2 object references for randomly ordered input
1408 * arrays.
1409 *
1410 * <p>The implementation takes equal advantage of ascending and
1411 * descending order in its input array, and can take advantage of
1412 * ascending and descending order in different parts of the same
1413 * input array. It is well-suited to merging two or more sorted arrays:
1414 * simply concatenate the arrays and sort the resulting array.
1415 *
1416 * <p>The implementation was adapted from Tim Peters's list sort for Python
1417 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1418 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic
1419 * Sorting and Information Theoretic Complexity", in Proceedings of the
1420 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
1421 * January 1993.
1422 *
1423 * @param <T> the class of the objects to be sorted
1424 * @param a the array to be sorted
1425 * @param c the comparator to determine the order of the array. A
1426 * {@code null} value indicates that the elements'
1427 * {@linkplain Comparable natural ordering} should be used.
1428 * @throws ClassCastException if the array contains elements that are
1429 * not <i>mutually comparable</i> using the specified comparator
1430 * @throws IllegalArgumentException (optional) if the comparator is
1431 * found to violate the {@link Comparator} contract
1432 */
1433 public static <T> void sort(T[] a, Comparator<? super T> c) {
1434 if (c == null) {
1435 sort(a);
1436 } else {
1437 if (LegacyMergeSort.userRequested)
1438 legacyMergeSort(a, c);
1439 else
1440 TimSort.sort(a, 0, a.length, c, null, 0, 0);
1441 }
1442 }
1443
1444 /** To be removed in a future release. */
1445 private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) {
1446 T[] aux = a.clone();
1447 if (c==null)
1448 mergeSort(aux, a, 0, a.length, 0);
1449 else
1450 mergeSort(aux, a, 0, a.length, 0, c);
1451 }
1452
1453 /**
1454 * Sorts the specified range of the specified array of objects according
1455 * to the order induced by the specified comparator. The range to be
1456 * sorted extends from index {@code fromIndex}, inclusive, to index
1457 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
1458 * range to be sorted is empty.) All elements in the range must be
1459 * <i>mutually comparable</i> by the specified comparator (that is,
1460 * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException}
1461 * for any elements {@code e1} and {@code e2} in the range).
1462 *
1463 * <p>This sort is guaranteed to be <i>stable</i>: equal elements will
1464 * not be reordered as a result of the sort.
1465 *
1466 * <p>Implementation note: This implementation is a stable, adaptive,
1467 * iterative mergesort that requires far fewer than n lg(n) comparisons
1468 * when the input array is partially sorted, while offering the
1469 * performance of a traditional mergesort when the input array is
1470 * randomly ordered. If the input array is nearly sorted, the
1471 * implementation requires approximately n comparisons. Temporary
1472 * storage requirements vary from a small constant for nearly sorted
1473 * input arrays to n/2 object references for randomly ordered input
1474 * arrays.
1475 *
1476 * <p>The implementation takes equal advantage of ascending and
1477 * descending order in its input array, and can take advantage of
1478 * ascending and descending order in different parts of the same
1479 * input array. It is well-suited to merging two or more sorted arrays:
1480 * simply concatenate the arrays and sort the resulting array.
1481 *
1482 * <p>The implementation was adapted from Tim Peters's list sort for Python
1483 * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
1484 * TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic
1485 * Sorting and Information Theoretic Complexity", in Proceedings of the
1486 * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
1487 * January 1993.
1488 *
1489 * @param <T> the class of the objects to be sorted
1490 * @param a the array to be sorted
1491 * @param fromIndex the index of the first element (inclusive) to be
1492 * sorted
1493 * @param toIndex the index of the last element (exclusive) to be sorted
1494 * @param c the comparator to determine the order of the array. A
1495 * {@code null} value indicates that the elements'
1496 * {@linkplain Comparable natural ordering} should be used.
1497 * @throws ClassCastException if the array contains elements that are not
1498 * <i>mutually comparable</i> using the specified comparator.
1499 * @throws IllegalArgumentException if {@code fromIndex > toIndex} or
1500 * (optional) if the comparator is found to violate the
1501 * {@link Comparator} contract
1502 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
1503 * {@code toIndex > a.length}
1504 */
1505 public static <T> void sort(T[] a, int fromIndex, int toIndex,
1506 Comparator<? super T> c) {
1507 if (c == null) {
1508 sort(a, fromIndex, toIndex);
1509 } else {
1510 rangeCheck(a.length, fromIndex, toIndex);
1511 if (LegacyMergeSort.userRequested)
1512 legacyMergeSort(a, fromIndex, toIndex, c);
1513 else
1514 TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0);
1515 }
1516 }
1517
1518 /** To be removed in a future release. */
1519 private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex,
1520 Comparator<? super T> c) {
1521 T[] aux = copyOfRange(a, fromIndex, toIndex);
1522 if (c==null)
1523 mergeSort(aux, a, fromIndex, toIndex, -fromIndex);
1524 else
1525 mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c);
1526 }
1527
1528 /**
1529 * Src is the source array that starts at index 0
1530 * Dest is the (possibly larger) array destination with a possible offset
1531 * low is the index in dest to start sorting
1532 * high is the end index in dest to end sorting
1533 * off is the offset into src corresponding to low in dest
1534 * To be removed in a future release.
1535 */
1536 @SuppressWarnings({"rawtypes", "unchecked"})
1537 private static void mergeSort(Object[] src,
1538 Object[] dest,
1539 int low, int high, int off,
1540 Comparator c) {
1541 int length = high - low;
1542
1543 // Insertion sort on smallest arrays
1544 if (length < INSERTIONSORT_THRESHOLD) {
1545 for (int i=low; i<high; i++)
1546 for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
1547 swap(dest, j, j-1);
1548 return;
1549 }
1550
1551 // Recursively sort halves of dest into src
1552 int destLow = low;
1553 int destHigh = high;
1554 low += off;
1555 high += off;
1556 int mid = (low + high) >>> 1;
1557 mergeSort(dest, src, low, mid, -off, c);
1558 mergeSort(dest, src, mid, high, -off, c);
1559
1560 // If list is already sorted, just copy from src to dest. This is an
1561 // optimization that results in faster sorts for nearly ordered lists.
1562 if (c.compare(src[mid-1], src[mid]) <= 0) {
1563 System.arraycopy(src, low, dest, destLow, length);
1564 return;
1565 }
1566
1567 // Merge sorted halves (now in src) into dest
1568 for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
1569 if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
1570 dest[i] = src[p++];
1571 else
1572 dest[i] = src[q++];
1573 }
1574 }
1575
1576 // Parallel prefix
1577
1578 /**
1579 * Cumulates, in parallel, each element of the given array in place,
1580 * using the supplied function. For example if the array initially
1581 * holds {@code [2, 1, 0, 3]} and the operation performs addition,
1582 * then upon return the array holds {@code [2, 3, 3, 6]}.
1583 * Parallel prefix computation is usually more efficient than
1584 * sequential loops for large arrays.
1585 *
1586 * @param <T> the class of the objects in the array
1587 * @param array the array, which is modified in-place by this method
1588 * @param op a side-effect-free, associative function to perform the
1589 * cumulation
1590 * @throws NullPointerException if the specified array or function is null
1591 * @since 1.8
1592 */
1593 public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) {
1594 Objects.requireNonNull(op);
1595 if (array.length > 0)
1596 new ArrayPrefixHelpers.CumulateTask<>
1597 (null, op, array, 0, array.length).invoke();
1598 }
1599
1600 /**
1601 * Performs {@link #parallelPrefix(Object[], BinaryOperator)}
1602 * for the given subrange of the array.
1603 *
1604 * @param <T> the class of the objects in the array
1605 * @param array the array
1606 * @param fromIndex the index of the first element, inclusive
1607 * @param toIndex the index of the last element, exclusive
1608 * @param op a side-effect-free, associative function to perform the
1609 * cumulation
1610 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
1611 * @throws ArrayIndexOutOfBoundsException
1612 * if {@code fromIndex < 0} or {@code toIndex > array.length}
1613 * @throws NullPointerException if the specified array or function is null
1614 * @since 1.8
1615 */
1616 public static <T> void parallelPrefix(T[] array, int fromIndex,
1617 int toIndex, BinaryOperator<T> op) {
1618 Objects.requireNonNull(op);
1619 rangeCheck(array.length, fromIndex, toIndex);
1620 if (fromIndex < toIndex)
1621 new ArrayPrefixHelpers.CumulateTask<>
1622 (null, op, array, fromIndex, toIndex).invoke();
1623 }
1624
1625 /**
1626 * Cumulates, in parallel, each element of the given array in place,
1627 * using the supplied function. For example if the array initially
1628 * holds {@code [2, 1, 0, 3]} and the operation performs addition,
1629 * then upon return the array holds {@code [2, 3, 3, 6]}.
1630 * Parallel prefix computation is usually more efficient than
1631 * sequential loops for large arrays.
1632 *
1633 * @param array the array, which is modified in-place by this method
1634 * @param op a side-effect-free, associative function to perform the
1635 * cumulation
1636 * @throws NullPointerException if the specified array or function is null
1637 * @since 1.8
1638 */
1639 public static void parallelPrefix(long[] array, LongBinaryOperator op) {
1640 Objects.requireNonNull(op);
1641 if (array.length > 0)
1642 new ArrayPrefixHelpers.LongCumulateTask
1643 (null, op, array, 0, array.length).invoke();
1644 }
1645
1646 /**
1647 * Performs {@link #parallelPrefix(long[], LongBinaryOperator)}
1648 * for the given subrange of the array.
1649 *
1650 * @param array the array
1651 * @param fromIndex the index of the first element, inclusive
1652 * @param toIndex the index of the last element, exclusive
1653 * @param op a side-effect-free, associative function to perform the
1654 * cumulation
1655 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
1656 * @throws ArrayIndexOutOfBoundsException
1657 * if {@code fromIndex < 0} or {@code toIndex > array.length}
1658 * @throws NullPointerException if the specified array or function is null
1659 * @since 1.8
1660 */
1661 public static void parallelPrefix(long[] array, int fromIndex,
1662 int toIndex, LongBinaryOperator op) {
1663 Objects.requireNonNull(op);
1664 rangeCheck(array.length, fromIndex, toIndex);
1665 if (fromIndex < toIndex)
1666 new ArrayPrefixHelpers.LongCumulateTask
1667 (null, op, array, fromIndex, toIndex).invoke();
1668 }
1669
1670 /**
1671 * Cumulates, in parallel, each element of the given array in place,
1672 * using the supplied function. For example if the array initially
1673 * holds {@code [2.0, 1.0, 0.0, 3.0]} and the operation performs addition,
1674 * then upon return the array holds {@code [2.0, 3.0, 3.0, 6.0]}.
1675 * Parallel prefix computation is usually more efficient than
1676 * sequential loops for large arrays.
1677 *
1678 * <p> Because floating-point operations may not be strictly associative,
1679 * the returned result may not be identical to the value that would be
1680 * obtained if the operation was performed sequentially.
1681 *
1682 * @param array the array, which is modified in-place by this method
1683 * @param op a side-effect-free function to perform the cumulation
1684 * @throws NullPointerException if the specified array or function is null
1685 * @since 1.8
1686 */
1687 public static void parallelPrefix(double[] array, DoubleBinaryOperator op) {
1688 Objects.requireNonNull(op);
1689 if (array.length > 0)
1690 new ArrayPrefixHelpers.DoubleCumulateTask
1691 (null, op, array, 0, array.length).invoke();
1692 }
1693
1694 /**
1695 * Performs {@link #parallelPrefix(double[], DoubleBinaryOperator)}
1696 * for the given subrange of the array.
1697 *
1698 * @param array the array
1699 * @param fromIndex the index of the first element, inclusive
1700 * @param toIndex the index of the last element, exclusive
1701 * @param op a side-effect-free, associative function to perform the
1702 * cumulation
1703 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
1704 * @throws ArrayIndexOutOfBoundsException
1705 * if {@code fromIndex < 0} or {@code toIndex > array.length}
1706 * @throws NullPointerException if the specified array or function is null
1707 * @since 1.8
1708 */
1709 public static void parallelPrefix(double[] array, int fromIndex,
1710 int toIndex, DoubleBinaryOperator op) {
1711 Objects.requireNonNull(op);
1712 rangeCheck(array.length, fromIndex, toIndex);
1713 if (fromIndex < toIndex)
1714 new ArrayPrefixHelpers.DoubleCumulateTask
1715 (null, op, array, fromIndex, toIndex).invoke();
1716 }
1717
1718 /**
1719 * Cumulates, in parallel, each element of the given array in place,
1720 * using the supplied function. For example if the array initially
1721 * holds {@code [2, 1, 0, 3]} and the operation performs addition,
1722 * then upon return the array holds {@code [2, 3, 3, 6]}.
1723 * Parallel prefix computation is usually more efficient than
1724 * sequential loops for large arrays.
1725 *
1726 * @param array the array, which is modified in-place by this method
1727 * @param op a side-effect-free, associative function to perform the
1728 * cumulation
1729 * @throws NullPointerException if the specified array or function is null
1730 * @since 1.8
1731 */
1732 public static void parallelPrefix(int[] array, IntBinaryOperator op) {
1733 Objects.requireNonNull(op);
1734 if (array.length > 0)
1735 new ArrayPrefixHelpers.IntCumulateTask
1736 (null, op, array, 0, array.length).invoke();
1737 }
1738
1739 /**
1740 * Performs {@link #parallelPrefix(int[], IntBinaryOperator)}
1741 * for the given subrange of the array.
1742 *
1743 * @param array the array
1744 * @param fromIndex the index of the first element, inclusive
1745 * @param toIndex the index of the last element, exclusive
1746 * @param op a side-effect-free, associative function to perform the
1747 * cumulation
1748 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
1749 * @throws ArrayIndexOutOfBoundsException
1750 * if {@code fromIndex < 0} or {@code toIndex > array.length}
1751 * @throws NullPointerException if the specified array or function is null
1752 * @since 1.8
1753 */
1754 public static void parallelPrefix(int[] array, int fromIndex,
1755 int toIndex, IntBinaryOperator op) {
1756 Objects.requireNonNull(op);
1757 rangeCheck(array.length, fromIndex, toIndex);
1758 if (fromIndex < toIndex)
1759 new ArrayPrefixHelpers.IntCumulateTask
1760 (null, op, array, fromIndex, toIndex).invoke();
1761 }
1762
1763 // Searching
1764
1765 /**
1766 * Searches the specified array of longs for the specified value using the
1767 * binary search algorithm. The array must be sorted (as
1768 * by the {@link #sort(long[])} method) prior to making this call. If it
1769 * is not sorted, the results are undefined. If the array contains
1770 * multiple elements with the specified value, there is no guarantee which
1771 * one will be found.
1772 *
1773 * @param a the array to be searched
1774 * @param key the value to be searched for
1775 * @return index of the search key, if it is contained in the array;
1776 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
1777 * <i>insertion point</i> is defined as the point at which the
1778 * key would be inserted into the array: the index of the first
1779 * element greater than the key, or {@code a.length} if all
1780 * elements in the array are less than the specified key. Note
1781 * that this guarantees that the return value will be >= 0 if
1782 * and only if the key is found.
1783 */
1784 public static int binarySearch(long[] a, long key) {
1785 return binarySearch0(a, 0, a.length, key);
1786 }
1787
1788 /**
1789 * Searches a range of
1790 * the specified array of longs for the specified value using the
1791 * binary search algorithm.
1792 * The range must be sorted (as
1793 * by the {@link #sort(long[], int, int)} method)
1794 * prior to making this call. If it
1795 * is not sorted, the results are undefined. If the range contains
1796 * multiple elements with the specified value, there is no guarantee which
1797 * one will be found.
1798 *
1799 * @param a the array to be searched
1800 * @param fromIndex the index of the first element (inclusive) to be
1801 * searched
1802 * @param toIndex the index of the last element (exclusive) to be searched
1803 * @param key the value to be searched for
1804 * @return index of the search key, if it is contained in the array
1805 * within the specified range;
1806 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
1807 * <i>insertion point</i> is defined as the point at which the
1808 * key would be inserted into the array: the index of the first
1809 * element in the range greater than the key,
1810 * or {@code toIndex} if all
1811 * elements in the range are less than the specified key. Note
1812 * that this guarantees that the return value will be >= 0 if
1813 * and only if the key is found.
1814 * @throws IllegalArgumentException
1815 * if {@code fromIndex > toIndex}
1816 * @throws ArrayIndexOutOfBoundsException
1817 * if {@code fromIndex < 0 or toIndex > a.length}
1818 * @since 1.6
1819 */
1820 public static int binarySearch(long[] a, int fromIndex, int toIndex,
1821 long key) {
1822 rangeCheck(a.length, fromIndex, toIndex);
1823 return binarySearch0(a, fromIndex, toIndex, key);
1824 }
1825
1826 // Like public version, but without range checks.
1827 private static int binarySearch0(long[] a, int fromIndex, int toIndex,
1828 long key) {
1829 int low = fromIndex;
1830 int high = toIndex - 1;
1831
1832 while (low <= high) {
1833 int mid = (low + high) >>> 1;
1834 long midVal = a[mid];
1835
1836 if (midVal < key)
1837 low = mid + 1;
1838 else if (midVal > key)
1839 high = mid - 1;
1840 else
1841 return mid; // key found
1842 }
1843 return -(low + 1); // key not found.
1844 }
1845
1846 /**
1847 * Searches the specified array of ints for the specified value using the
1848 * binary search algorithm. The array must be sorted (as
1849 * by the {@link #sort(int[])} method) prior to making this call. If it
1850 * is not sorted, the results are undefined. If the array contains
1851 * multiple elements with the specified value, there is no guarantee which
1852 * one will be found.
1853 *
1854 * @param a the array to be searched
1855 * @param key the value to be searched for
1856 * @return index of the search key, if it is contained in the array;
1857 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
1858 * <i>insertion point</i> is defined as the point at which the
1859 * key would be inserted into the array: the index of the first
1860 * element greater than the key, or {@code a.length} if all
1861 * elements in the array are less than the specified key. Note
1862 * that this guarantees that the return value will be >= 0 if
1863 * and only if the key is found.
1864 */
1865 public static int binarySearch(int[] a, int key) {
1866 return binarySearch0(a, 0, a.length, key);
1867 }
1868
1869 /**
1870 * Searches a range of
1871 * the specified array of ints for the specified value using the
1872 * binary search algorithm.
1873 * The range must be sorted (as
1874 * by the {@link #sort(int[], int, int)} method)
1875 * prior to making this call. If it
1876 * is not sorted, the results are undefined. If the range contains
1877 * multiple elements with the specified value, there is no guarantee which
1878 * one will be found.
1879 *
1880 * @param a the array to be searched
1881 * @param fromIndex the index of the first element (inclusive) to be
1882 * searched
1883 * @param toIndex the index of the last element (exclusive) to be searched
1884 * @param key the value to be searched for
1885 * @return index of the search key, if it is contained in the array
1886 * within the specified range;
1887 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
1888 * <i>insertion point</i> is defined as the point at which the
1889 * key would be inserted into the array: the index of the first
1890 * element in the range greater than the key,
1891 * or {@code toIndex} if all
1892 * elements in the range are less than the specified key. Note
1893 * that this guarantees that the return value will be >= 0 if
1894 * and only if the key is found.
1895 * @throws IllegalArgumentException
1896 * if {@code fromIndex > toIndex}
1897 * @throws ArrayIndexOutOfBoundsException
1898 * if {@code fromIndex < 0 or toIndex > a.length}
1899 * @since 1.6
1900 */
1901 public static int binarySearch(int[] a, int fromIndex, int toIndex,
1902 int key) {
1903 rangeCheck(a.length, fromIndex, toIndex);
1904 return binarySearch0(a, fromIndex, toIndex, key);
1905 }
1906
1907 // Like public version, but without range checks.
1908 private static int binarySearch0(int[] a, int fromIndex, int toIndex,
1909 int key) {
1910 int low = fromIndex;
1911 int high = toIndex - 1;
1912
1913 while (low <= high) {
1914 int mid = (low + high) >>> 1;
1915 int midVal = a[mid];
1916
1917 if (midVal < key)
1918 low = mid + 1;
1919 else if (midVal > key)
1920 high = mid - 1;
1921 else
1922 return mid; // key found
1923 }
1924 return -(low + 1); // key not found.
1925 }
1926
1927 /**
1928 * Searches the specified array of shorts for the specified value using
1929 * the binary search algorithm. The array must be sorted
1930 * (as by the {@link #sort(short[])} method) prior to making this call. If
1931 * it is not sorted, the results are undefined. If the array contains
1932 * multiple elements with the specified value, there is no guarantee which
1933 * one will be found.
1934 *
1935 * @param a the array to be searched
1936 * @param key the value to be searched for
1937 * @return index of the search key, if it is contained in the array;
1938 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
1939 * <i>insertion point</i> is defined as the point at which the
1940 * key would be inserted into the array: the index of the first
1941 * element greater than the key, or {@code a.length} if all
1942 * elements in the array are less than the specified key. Note
1943 * that this guarantees that the return value will be >= 0 if
1944 * and only if the key is found.
1945 */
1946 public static int binarySearch(short[] a, short key) {
1947 return binarySearch0(a, 0, a.length, key);
1948 }
1949
1950 /**
1951 * Searches a range of
1952 * the specified array of shorts for the specified value using
1953 * the binary search algorithm.
1954 * The range must be sorted
1955 * (as by the {@link #sort(short[], int, int)} method)
1956 * prior to making this call. If
1957 * it is not sorted, the results are undefined. If the range contains
1958 * multiple elements with the specified value, there is no guarantee which
1959 * one will be found.
1960 *
1961 * @param a the array to be searched
1962 * @param fromIndex the index of the first element (inclusive) to be
1963 * searched
1964 * @param toIndex the index of the last element (exclusive) to be searched
1965 * @param key the value to be searched for
1966 * @return index of the search key, if it is contained in the array
1967 * within the specified range;
1968 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
1969 * <i>insertion point</i> is defined as the point at which the
1970 * key would be inserted into the array: the index of the first
1971 * element in the range greater than the key,
1972 * or {@code toIndex} if all
1973 * elements in the range are less than the specified key. Note
1974 * that this guarantees that the return value will be >= 0 if
1975 * and only if the key is found.
1976 * @throws IllegalArgumentException
1977 * if {@code fromIndex > toIndex}
1978 * @throws ArrayIndexOutOfBoundsException
1979 * if {@code fromIndex < 0 or toIndex > a.length}
1980 * @since 1.6
1981 */
1982 public static int binarySearch(short[] a, int fromIndex, int toIndex,
1983 short key) {
1984 rangeCheck(a.length, fromIndex, toIndex);
1985 return binarySearch0(a, fromIndex, toIndex, key);
1986 }
1987
1988 // Like public version, but without range checks.
1989 private static int binarySearch0(short[] a, int fromIndex, int toIndex,
1990 short key) {
1991 int low = fromIndex;
1992 int high = toIndex - 1;
1993
1994 while (low <= high) {
1995 int mid = (low + high) >>> 1;
1996 short midVal = a[mid];
1997
1998 if (midVal < key)
1999 low = mid + 1;
2000 else if (midVal > key)
2001 high = mid - 1;
2002 else
2003 return mid; // key found
2004 }
2005 return -(low + 1); // key not found.
2006 }
2007
2008 /**
2009 * Searches the specified array of chars for the specified value using the
2010 * binary search algorithm. The array must be sorted (as
2011 * by the {@link #sort(char[])} method) prior to making this call. If it
2012 * is not sorted, the results are undefined. If the array contains
2013 * multiple elements with the specified value, there is no guarantee which
2014 * one will be found.
2015 *
2016 * @param a the array to be searched
2017 * @param key the value to be searched for
2018 * @return index of the search key, if it is contained in the array;
2019 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2020 * <i>insertion point</i> is defined as the point at which the
2021 * key would be inserted into the array: the index of the first
2022 * element greater than the key, or {@code a.length} if all
2023 * elements in the array are less than the specified key. Note
2024 * that this guarantees that the return value will be >= 0 if
2025 * and only if the key is found.
2026 */
2027 public static int binarySearch(char[] a, char key) {
2028 return binarySearch0(a, 0, a.length, key);
2029 }
2030
2031 /**
2032 * Searches a range of
2033 * the specified array of chars for the specified value using the
2034 * binary search algorithm.
2035 * The range must be sorted (as
2036 * by the {@link #sort(char[], int, int)} method)
2037 * prior to making this call. If it
2038 * is not sorted, the results are undefined. If the range contains
2039 * multiple elements with the specified value, there is no guarantee which
2040 * one will be found.
2041 *
2042 * @param a the array to be searched
2043 * @param fromIndex the index of the first element (inclusive) to be
2044 * searched
2045 * @param toIndex the index of the last element (exclusive) to be searched
2046 * @param key the value to be searched for
2047 * @return index of the search key, if it is contained in the array
2048 * within the specified range;
2049 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2050 * <i>insertion point</i> is defined as the point at which the
2051 * key would be inserted into the array: the index of the first
2052 * element in the range greater than the key,
2053 * or {@code toIndex} if all
2054 * elements in the range are less than the specified key. Note
2055 * that this guarantees that the return value will be >= 0 if
2056 * and only if the key is found.
2057 * @throws IllegalArgumentException
2058 * if {@code fromIndex > toIndex}
2059 * @throws ArrayIndexOutOfBoundsException
2060 * if {@code fromIndex < 0 or toIndex > a.length}
2061 * @since 1.6
2062 */
2063 public static int binarySearch(char[] a, int fromIndex, int toIndex,
2064 char key) {
2065 rangeCheck(a.length, fromIndex, toIndex);
2066 return binarySearch0(a, fromIndex, toIndex, key);
2067 }
2068
2069 // Like public version, but without range checks.
2070 private static int binarySearch0(char[] a, int fromIndex, int toIndex,
2071 char key) {
2072 int low = fromIndex;
2073 int high = toIndex - 1;
2074
2075 while (low <= high) {
2076 int mid = (low + high) >>> 1;
2077 char midVal = a[mid];
2078
2079 if (midVal < key)
2080 low = mid + 1;
2081 else if (midVal > key)
2082 high = mid - 1;
2083 else
2084 return mid; // key found
2085 }
2086 return -(low + 1); // key not found.
2087 }
2088
2089 /**
2090 * Searches the specified array of bytes for the specified value using the
2091 * binary search algorithm. The array must be sorted (as
2092 * by the {@link #sort(byte[])} method) prior to making this call. If it
2093 * is not sorted, the results are undefined. If the array contains
2094 * multiple elements with the specified value, there is no guarantee which
2095 * one will be found.
2096 *
2097 * @param a the array to be searched
2098 * @param key the value to be searched for
2099 * @return index of the search key, if it is contained in the array;
2100 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2101 * <i>insertion point</i> is defined as the point at which the
2102 * key would be inserted into the array: the index of the first
2103 * element greater than the key, or {@code a.length} if all
2104 * elements in the array are less than the specified key. Note
2105 * that this guarantees that the return value will be >= 0 if
2106 * and only if the key is found.
2107 */
2108 public static int binarySearch(byte[] a, byte key) {
2109 return binarySearch0(a, 0, a.length, key);
2110 }
2111
2112 /**
2113 * Searches a range of
2114 * the specified array of bytes for the specified value using the
2115 * binary search algorithm.
2116 * The range must be sorted (as
2117 * by the {@link #sort(byte[], int, int)} method)
2118 * prior to making this call. If it
2119 * is not sorted, the results are undefined. If the range contains
2120 * multiple elements with the specified value, there is no guarantee which
2121 * one will be found.
2122 *
2123 * @param a the array to be searched
2124 * @param fromIndex the index of the first element (inclusive) to be
2125 * searched
2126 * @param toIndex the index of the last element (exclusive) to be searched
2127 * @param key the value to be searched for
2128 * @return index of the search key, if it is contained in the array
2129 * within the specified range;
2130 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2131 * <i>insertion point</i> is defined as the point at which the
2132 * key would be inserted into the array: the index of the first
2133 * element in the range greater than the key,
2134 * or {@code toIndex} if all
2135 * elements in the range are less than the specified key. Note
2136 * that this guarantees that the return value will be >= 0 if
2137 * and only if the key is found.
2138 * @throws IllegalArgumentException
2139 * if {@code fromIndex > toIndex}
2140 * @throws ArrayIndexOutOfBoundsException
2141 * if {@code fromIndex < 0 or toIndex > a.length}
2142 * @since 1.6
2143 */
2144 public static int binarySearch(byte[] a, int fromIndex, int toIndex,
2145 byte key) {
2146 rangeCheck(a.length, fromIndex, toIndex);
2147 return binarySearch0(a, fromIndex, toIndex, key);
2148 }
2149
2150 // Like public version, but without range checks.
2151 private static int binarySearch0(byte[] a, int fromIndex, int toIndex,
2152 byte key) {
2153 int low = fromIndex;
2154 int high = toIndex - 1;
2155
2156 while (low <= high) {
2157 int mid = (low + high) >>> 1;
2158 byte midVal = a[mid];
2159
2160 if (midVal < key)
2161 low = mid + 1;
2162 else if (midVal > key)
2163 high = mid - 1;
2164 else
2165 return mid; // key found
2166 }
2167 return -(low + 1); // key not found.
2168 }
2169
2170 /**
2171 * Searches the specified array of doubles for the specified value using
2172 * the binary search algorithm. The array must be sorted
2173 * (as by the {@link #sort(double[])} method) prior to making this call.
2174 * If it is not sorted, the results are undefined. If the array contains
2175 * multiple elements with the specified value, there is no guarantee which
2176 * one will be found. This method considers all NaN values to be
2177 * equivalent and equal.
2178 *
2179 * @param a the array to be searched
2180 * @param key the value to be searched for
2181 * @return index of the search key, if it is contained in the array;
2182 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2183 * <i>insertion point</i> is defined as the point at which the
2184 * key would be inserted into the array: the index of the first
2185 * element greater than the key, or {@code a.length} if all
2186 * elements in the array are less than the specified key. Note
2187 * that this guarantees that the return value will be >= 0 if
2188 * and only if the key is found.
2189 */
2190 public static int binarySearch(double[] a, double key) {
2191 return binarySearch0(a, 0, a.length, key);
2192 }
2193
2194 /**
2195 * Searches a range of
2196 * the specified array of doubles for the specified value using
2197 * the binary search algorithm.
2198 * The range must be sorted
2199 * (as by the {@link #sort(double[], int, int)} method)
2200 * prior to making this call.
2201 * If it is not sorted, the results are undefined. If the range contains
2202 * multiple elements with the specified value, there is no guarantee which
2203 * one will be found. This method considers all NaN values to be
2204 * equivalent and equal.
2205 *
2206 * @param a the array to be searched
2207 * @param fromIndex the index of the first element (inclusive) to be
2208 * searched
2209 * @param toIndex the index of the last element (exclusive) to be searched
2210 * @param key the value to be searched for
2211 * @return index of the search key, if it is contained in the array
2212 * within the specified range;
2213 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2214 * <i>insertion point</i> is defined as the point at which the
2215 * key would be inserted into the array: the index of the first
2216 * element in the range greater than the key,
2217 * or {@code toIndex} if all
2218 * elements in the range are less than the specified key. Note
2219 * that this guarantees that the return value will be >= 0 if
2220 * and only if the key is found.
2221 * @throws IllegalArgumentException
2222 * if {@code fromIndex > toIndex}
2223 * @throws ArrayIndexOutOfBoundsException
2224 * if {@code fromIndex < 0 or toIndex > a.length}
2225 * @since 1.6
2226 */
2227 public static int binarySearch(double[] a, int fromIndex, int toIndex,
2228 double key) {
2229 rangeCheck(a.length, fromIndex, toIndex);
2230 return binarySearch0(a, fromIndex, toIndex, key);
2231 }
2232
2233 // Like public version, but without range checks.
2234 private static int binarySearch0(double[] a, int fromIndex, int toIndex,
2235 double key) {
2236 int low = fromIndex;
2237 int high = toIndex - 1;
2238
2239 while (low <= high) {
2240 int mid = (low + high) >>> 1;
2241 double midVal = a[mid];
2242
2243 if (midVal < key)
2244 low = mid + 1; // Neither val is NaN, thisVal is smaller
2245 else if (midVal > key)
2246 high = mid - 1; // Neither val is NaN, thisVal is larger
2247 else {
2248 long midBits = Double.doubleToLongBits(midVal);
2249 long keyBits = Double.doubleToLongBits(key);
2250 if (midBits == keyBits) // Values are equal
2251 return mid; // Key found
2252 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
2253 low = mid + 1;
2254 else // (0.0, -0.0) or (NaN, !NaN)
2255 high = mid - 1;
2256 }
2257 }
2258 return -(low + 1); // key not found.
2259 }
2260
2261 /**
2262 * Searches the specified array of floats for the specified value using
2263 * the binary search algorithm. The array must be sorted
2264 * (as by the {@link #sort(float[])} method) prior to making this call. If
2265 * it is not sorted, the results are undefined. If the array contains
2266 * multiple elements with the specified value, there is no guarantee which
2267 * one will be found. This method considers all NaN values to be
2268 * equivalent and equal.
2269 *
2270 * @param a the array to be searched
2271 * @param key the value to be searched for
2272 * @return index of the search key, if it is contained in the array;
2273 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2274 * <i>insertion point</i> is defined as the point at which the
2275 * key would be inserted into the array: the index of the first
2276 * element greater than the key, or {@code a.length} if all
2277 * elements in the array are less than the specified key. Note
2278 * that this guarantees that the return value will be >= 0 if
2279 * and only if the key is found.
2280 */
2281 public static int binarySearch(float[] a, float key) {
2282 return binarySearch0(a, 0, a.length, key);
2283 }
2284
2285 /**
2286 * Searches a range of
2287 * the specified array of floats for the specified value using
2288 * the binary search algorithm.
2289 * The range must be sorted
2290 * (as by the {@link #sort(float[], int, int)} method)
2291 * prior to making this call. If
2292 * it is not sorted, the results are undefined. If the range contains
2293 * multiple elements with the specified value, there is no guarantee which
2294 * one will be found. This method considers all NaN values to be
2295 * equivalent and equal.
2296 *
2297 * @param a the array to be searched
2298 * @param fromIndex the index of the first element (inclusive) to be
2299 * searched
2300 * @param toIndex the index of the last element (exclusive) to be searched
2301 * @param key the value to be searched for
2302 * @return index of the search key, if it is contained in the array
2303 * within the specified range;
2304 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2305 * <i>insertion point</i> is defined as the point at which the
2306 * key would be inserted into the array: the index of the first
2307 * element in the range greater than the key,
2308 * or {@code toIndex} if all
2309 * elements in the range are less than the specified key. Note
2310 * that this guarantees that the return value will be >= 0 if
2311 * and only if the key is found.
2312 * @throws IllegalArgumentException
2313 * if {@code fromIndex > toIndex}
2314 * @throws ArrayIndexOutOfBoundsException
2315 * if {@code fromIndex < 0 or toIndex > a.length}
2316 * @since 1.6
2317 */
2318 public static int binarySearch(float[] a, int fromIndex, int toIndex,
2319 float key) {
2320 rangeCheck(a.length, fromIndex, toIndex);
2321 return binarySearch0(a, fromIndex, toIndex, key);
2322 }
2323
2324 // Like public version, but without range checks.
2325 private static int binarySearch0(float[] a, int fromIndex, int toIndex,
2326 float key) {
2327 int low = fromIndex;
2328 int high = toIndex - 1;
2329
2330 while (low <= high) {
2331 int mid = (low + high) >>> 1;
2332 float midVal = a[mid];
2333
2334 if (midVal < key)
2335 low = mid + 1; // Neither val is NaN, thisVal is smaller
2336 else if (midVal > key)
2337 high = mid - 1; // Neither val is NaN, thisVal is larger
2338 else {
2339 int midBits = Float.floatToIntBits(midVal);
2340 int keyBits = Float.floatToIntBits(key);
2341 if (midBits == keyBits) // Values are equal
2342 return mid; // Key found
2343 else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN)
2344 low = mid + 1;
2345 else // (0.0, -0.0) or (NaN, !NaN)
2346 high = mid - 1;
2347 }
2348 }
2349 return -(low + 1); // key not found.
2350 }
2351
2352 /**
2353 * Searches the specified array for the specified object using the binary
2354 * search algorithm. The array must be sorted into ascending order
2355 * according to the
2356 * {@linkplain Comparable natural ordering}
2357 * of its elements (as by the
2358 * {@link #sort(Object[])} method) prior to making this call.
2359 * If it is not sorted, the results are undefined.
2360 * (If the array contains elements that are not mutually comparable (for
2361 * example, strings and integers), it <i>cannot</i> be sorted according
2362 * to the natural ordering of its elements, hence results are undefined.)
2363 * If the array contains multiple
2364 * elements equal to the specified object, there is no guarantee which
2365 * one will be found.
2366 *
2367 * @param a the array to be searched
2368 * @param key the value to be searched for
2369 * @return index of the search key, if it is contained in the array;
2370 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2371 * <i>insertion point</i> is defined as the point at which the
2372 * key would be inserted into the array: the index of the first
2373 * element greater than the key, or {@code a.length} if all
2374 * elements in the array are less than the specified key. Note
2375 * that this guarantees that the return value will be >= 0 if
2376 * and only if the key is found.
2377 * @throws ClassCastException if the search key is not comparable to the
2378 * elements of the array.
2379 */
2380 public static int binarySearch(Object[] a, Object key) {
2381 return binarySearch0(a, 0, a.length, key);
2382 }
2383
2384 /**
2385 * Searches a range of
2386 * the specified array for the specified object using the binary
2387 * search algorithm.
2388 * The range must be sorted into ascending order
2389 * according to the
2390 * {@linkplain Comparable natural ordering}
2391 * of its elements (as by the
2392 * {@link #sort(Object[], int, int)} method) prior to making this
2393 * call. If it is not sorted, the results are undefined.
2394 * (If the range contains elements that are not mutually comparable (for
2395 * example, strings and integers), it <i>cannot</i> be sorted according
2396 * to the natural ordering of its elements, hence results are undefined.)
2397 * If the range contains multiple
2398 * elements equal to the specified object, there is no guarantee which
2399 * one will be found.
2400 *
2401 * @param a the array to be searched
2402 * @param fromIndex the index of the first element (inclusive) to be
2403 * searched
2404 * @param toIndex the index of the last element (exclusive) to be searched
2405 * @param key the value to be searched for
2406 * @return index of the search key, if it is contained in the array
2407 * within the specified range;
2408 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2409 * <i>insertion point</i> is defined as the point at which the
2410 * key would be inserted into the array: the index of the first
2411 * element in the range greater than the key,
2412 * or {@code toIndex} if all
2413 * elements in the range are less than the specified key. Note
2414 * that this guarantees that the return value will be >= 0 if
2415 * and only if the key is found.
2416 * @throws ClassCastException if the search key is not comparable to the
2417 * elements of the array within the specified range.
2418 * @throws IllegalArgumentException
2419 * if {@code fromIndex > toIndex}
2420 * @throws ArrayIndexOutOfBoundsException
2421 * if {@code fromIndex < 0 or toIndex > a.length}
2422 * @since 1.6
2423 */
2424 public static int binarySearch(Object[] a, int fromIndex, int toIndex,
2425 Object key) {
2426 rangeCheck(a.length, fromIndex, toIndex);
2427 return binarySearch0(a, fromIndex, toIndex, key);
2428 }
2429
2430 // Like public version, but without range checks.
2431 private static int binarySearch0(Object[] a, int fromIndex, int toIndex,
2432 Object key) {
2433 int low = fromIndex;
2434 int high = toIndex - 1;
2435
2436 while (low <= high) {
2437 int mid = (low + high) >>> 1;
2438 @SuppressWarnings("rawtypes")
2439 Comparable midVal = (Comparable)a[mid];
2440 @SuppressWarnings("unchecked")
2441 int cmp = midVal.compareTo(key);
2442
2443 if (cmp < 0)
2444 low = mid + 1;
2445 else if (cmp > 0)
2446 high = mid - 1;
2447 else
2448 return mid; // key found
2449 }
2450 return -(low + 1); // key not found.
2451 }
2452
2453 /**
2454 * Searches the specified array for the specified object using the binary
2455 * search algorithm. The array must be sorted into ascending order
2456 * according to the specified comparator (as by the
2457 * {@link #sort(Object[], Comparator) sort(T[], Comparator)}
2458 * method) prior to making this call. If it is
2459 * not sorted, the results are undefined.
2460 * If the array contains multiple
2461 * elements equal to the specified object, there is no guarantee which one
2462 * will be found.
2463 *
2464 * @param <T> the class of the objects in the array
2465 * @param a the array to be searched
2466 * @param key the value to be searched for
2467 * @param c the comparator by which the array is ordered. A
2468 * {@code null} value indicates that the elements'
2469 * {@linkplain Comparable natural ordering} should be used.
2470 * @return index of the search key, if it is contained in the array;
2471 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2472 * <i>insertion point</i> is defined as the point at which the
2473 * key would be inserted into the array: the index of the first
2474 * element greater than the key, or {@code a.length} if all
2475 * elements in the array are less than the specified key. Note
2476 * that this guarantees that the return value will be >= 0 if
2477 * and only if the key is found.
2478 * @throws ClassCastException if the array contains elements that are not
2479 * <i>mutually comparable</i> using the specified comparator,
2480 * or the search key is not comparable to the
2481 * elements of the array using this comparator.
2482 */
2483 public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) {
2484 return binarySearch0(a, 0, a.length, key, c);
2485 }
2486
2487 /**
2488 * Searches a range of
2489 * the specified array for the specified object using the binary
2490 * search algorithm.
2491 * The range must be sorted into ascending order
2492 * according to the specified comparator (as by the
2493 * {@link #sort(Object[], int, int, Comparator)
2494 * sort(T[], int, int, Comparator)}
2495 * method) prior to making this call.
2496 * If it is not sorted, the results are undefined.
2497 * If the range contains multiple elements equal to the specified object,
2498 * there is no guarantee which one will be found.
2499 *
2500 * @param <T> the class of the objects in the array
2501 * @param a the array to be searched
2502 * @param fromIndex the index of the first element (inclusive) to be
2503 * searched
2504 * @param toIndex the index of the last element (exclusive) to be searched
2505 * @param key the value to be searched for
2506 * @param c the comparator by which the array is ordered. A
2507 * {@code null} value indicates that the elements'
2508 * {@linkplain Comparable natural ordering} should be used.
2509 * @return index of the search key, if it is contained in the array
2510 * within the specified range;
2511 * otherwise, <code>(-(<i>insertion point</i>) - 1)</code>. The
2512 * <i>insertion point</i> is defined as the point at which the
2513 * key would be inserted into the array: the index of the first
2514 * element in the range greater than the key,
2515 * or {@code toIndex} if all
2516 * elements in the range are less than the specified key. Note
2517 * that this guarantees that the return value will be >= 0 if
2518 * and only if the key is found.
2519 * @throws ClassCastException if the range contains elements that are not
2520 * <i>mutually comparable</i> using the specified comparator,
2521 * or the search key is not comparable to the
2522 * elements in the range using this comparator.
2523 * @throws IllegalArgumentException
2524 * if {@code fromIndex > toIndex}
2525 * @throws ArrayIndexOutOfBoundsException
2526 * if {@code fromIndex < 0 or toIndex > a.length}
2527 * @since 1.6
2528 */
2529 public static <T> int binarySearch(T[] a, int fromIndex, int toIndex,
2530 T key, Comparator<? super T> c) {
2531 rangeCheck(a.length, fromIndex, toIndex);
2532 return binarySearch0(a, fromIndex, toIndex, key, c);
2533 }
2534
2535 // Like public version, but without range checks.
2536 private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex,
2537 T key, Comparator<? super T> c) {
2538 if (c == null) {
2539 return binarySearch0(a, fromIndex, toIndex, key);
2540 }
2541 int low = fromIndex;
2542 int high = toIndex - 1;
2543
2544 while (low <= high) {
2545 int mid = (low + high) >>> 1;
2546 T midVal = a[mid];
2547 int cmp = c.compare(midVal, key);
2548 if (cmp < 0)
2549 low = mid + 1;
2550 else if (cmp > 0)
2551 high = mid - 1;
2552 else
2553 return mid; // key found
2554 }
2555 return -(low + 1); // key not found.
2556 }
2557
2558 // Equality Testing
2559
2560 /**
2561 * Returns {@code true} if the two specified arrays of longs are
2562 * <i>equal</i> to one another. Two arrays are considered equal if both
2563 * arrays contain the same number of elements, and all corresponding pairs
2564 * of elements in the two arrays are equal. In other words, two arrays
2565 * are equal if they contain the same elements in the same order. Also,
2566 * two array references are considered equal if both are {@code null}.
2567 *
2568 * @param a one array to be tested for equality
2569 * @param a2 the other array to be tested for equality
2570 * @return {@code true} if the two arrays are equal
2571 */
2572 public static boolean equals(long[] a, long[] a2) {
2573 if (a==a2)
2574 return true;
2575 if (a==null || a2==null)
2576 return false;
2577
2578 int length = a.length;
2579 if (a2.length != length)
2580 return false;
2581
2582 return ArraysSupport.mismatch(a, a2, length) < 0;
2583 }
2584
2585 /**
2586 * Returns true if the two specified arrays of longs, over the specified
2587 * ranges, are <i>equal</i> to one another.
2588 *
2589 * <p>Two arrays are considered equal if the number of elements covered by
2590 * each range is the same, and all corresponding pairs of elements over the
2591 * specified ranges in the two arrays are equal. In other words, two arrays
2592 * are equal if they contain, over the specified ranges, the same elements
2593 * in the same order.
2594 *
2595 * @param a the first array to be tested for equality
2596 * @param aFromIndex the index (inclusive) of the first element in the
2597 * first array to be tested
2598 * @param aToIndex the index (exclusive) of the last element in the
2599 * first array to be tested
2600 * @param b the second array to be tested fro equality
2601 * @param bFromIndex the index (inclusive) of the first element in the
2602 * second array to be tested
2603 * @param bToIndex the index (exclusive) of the last element in the
2604 * second array to be tested
2605 * @return {@code true} if the two arrays, over the specified ranges, are
2606 * equal
2607 * @throws IllegalArgumentException
2608 * if {@code aFromIndex > aToIndex} or
2609 * if {@code bFromIndex > bToIndex}
2610 * @throws ArrayIndexOutOfBoundsException
2611 * if {@code aFromIndex < 0 or aToIndex > a.length} or
2612 * if {@code bFromIndex < 0 or bToIndex > b.length}
2613 * @throws NullPointerException
2614 * if either array is {@code null}
2615 * @since 9
2616 */
2617 public static boolean equals(long[] a, int aFromIndex, int aToIndex,
2618 long[] b, int bFromIndex, int bToIndex) {
2619 rangeCheck(a.length, aFromIndex, aToIndex);
2620 rangeCheck(b.length, bFromIndex, bToIndex);
2621
2622 int aLength = aToIndex - aFromIndex;
2623 int bLength = bToIndex - bFromIndex;
2624 if (aLength != bLength)
2625 return false;
2626
2627 return ArraysSupport.mismatch(a, aFromIndex,
2628 b, bFromIndex,
2629 aLength) < 0;
2630 }
2631
2632 /**
2633 * Returns {@code true} if the two specified arrays of ints are
2634 * <i>equal</i> to one another. Two arrays are considered equal if both
2635 * arrays contain the same number of elements, and all corresponding pairs
2636 * of elements in the two arrays are equal. In other words, two arrays
2637 * are equal if they contain the same elements in the same order. Also,
2638 * two array references are considered equal if both are {@code null}.
2639 *
2640 * @param a one array to be tested for equality
2641 * @param a2 the other array to be tested for equality
2642 * @return {@code true} if the two arrays are equal
2643 */
2644 public static boolean equals(int[] a, int[] a2) {
2645 if (a==a2)
2646 return true;
2647 if (a==null || a2==null)
2648 return false;
2649
2650 int length = a.length;
2651 if (a2.length != length)
2652 return false;
2653
2654 return ArraysSupport.mismatch(a, a2, length) < 0;
2655 }
2656
2657 /**
2658 * Returns true if the two specified arrays of ints, over the specified
2659 * ranges, are <i>equal</i> to one another.
2660 *
2661 * <p>Two arrays are considered equal if the number of elements covered by
2662 * each range is the same, and all corresponding pairs of elements over the
2663 * specified ranges in the two arrays are equal. In other words, two arrays
2664 * are equal if they contain, over the specified ranges, the same elements
2665 * in the same order.
2666 *
2667 * @param a the first array to be tested for equality
2668 * @param aFromIndex the index (inclusive) of the first element in the
2669 * first array to be tested
2670 * @param aToIndex the index (exclusive) of the last element in the
2671 * first array to be tested
2672 * @param b the second array to be tested fro equality
2673 * @param bFromIndex the index (inclusive) of the first element in the
2674 * second array to be tested
2675 * @param bToIndex the index (exclusive) of the last element in the
2676 * second array to be tested
2677 * @return {@code true} if the two arrays, over the specified ranges, are
2678 * equal
2679 * @throws IllegalArgumentException
2680 * if {@code aFromIndex > aToIndex} or
2681 * if {@code bFromIndex > bToIndex}
2682 * @throws ArrayIndexOutOfBoundsException
2683 * if {@code aFromIndex < 0 or aToIndex > a.length} or
2684 * if {@code bFromIndex < 0 or bToIndex > b.length}
2685 * @throws NullPointerException
2686 * if either array is {@code null}
2687 * @since 9
2688 */
2689 public static boolean equals(int[] a, int aFromIndex, int aToIndex,
2690 int[] b, int bFromIndex, int bToIndex) {
2691 rangeCheck(a.length, aFromIndex, aToIndex);
2692 rangeCheck(b.length, bFromIndex, bToIndex);
2693
2694 int aLength = aToIndex - aFromIndex;
2695 int bLength = bToIndex - bFromIndex;
2696 if (aLength != bLength)
2697 return false;
2698
2699 return ArraysSupport.mismatch(a, aFromIndex,
2700 b, bFromIndex,
2701 aLength) < 0;
2702 }
2703
2704 /**
2705 * Returns {@code true} if the two specified arrays of shorts are
2706 * <i>equal</i> to one another. Two arrays are considered equal if both
2707 * arrays contain the same number of elements, and all corresponding pairs
2708 * of elements in the two arrays are equal. In other words, two arrays
2709 * are equal if they contain the same elements in the same order. Also,
2710 * two array references are considered equal if both are {@code null}.
2711 *
2712 * @param a one array to be tested for equality
2713 * @param a2 the other array to be tested for equality
2714 * @return {@code true} if the two arrays are equal
2715 */
2716 public static boolean equals(short[] a, short a2[]) {
2717 if (a==a2)
2718 return true;
2719 if (a==null || a2==null)
2720 return false;
2721
2722 int length = a.length;
2723 if (a2.length != length)
2724 return false;
2725
2726 return ArraysSupport.mismatch(a, a2, length) < 0;
2727 }
2728
2729 /**
2730 * Returns true if the two specified arrays of shorts, over the specified
2731 * ranges, are <i>equal</i> to one another.
2732 *
2733 * <p>Two arrays are considered equal if the number of elements covered by
2734 * each range is the same, and all corresponding pairs of elements over the
2735 * specified ranges in the two arrays are equal. In other words, two arrays
2736 * are equal if they contain, over the specified ranges, the same elements
2737 * in the same order.
2738 *
2739 * @param a the first array to be tested for equality
2740 * @param aFromIndex the index (inclusive) of the first element in the
2741 * first array to be tested
2742 * @param aToIndex the index (exclusive) of the last element in the
2743 * first array to be tested
2744 * @param b the second array to be tested fro equality
2745 * @param bFromIndex the index (inclusive) of the first element in the
2746 * second array to be tested
2747 * @param bToIndex the index (exclusive) of the last element in the
2748 * second array to be tested
2749 * @return {@code true} if the two arrays, over the specified ranges, are
2750 * equal
2751 * @throws IllegalArgumentException
2752 * if {@code aFromIndex > aToIndex} or
2753 * if {@code bFromIndex > bToIndex}
2754 * @throws ArrayIndexOutOfBoundsException
2755 * if {@code aFromIndex < 0 or aToIndex > a.length} or
2756 * if {@code bFromIndex < 0 or bToIndex > b.length}
2757 * @throws NullPointerException
2758 * if either array is {@code null}
2759 * @since 9
2760 */
2761 public static boolean equals(short[] a, int aFromIndex, int aToIndex,
2762 short[] b, int bFromIndex, int bToIndex) {
2763 rangeCheck(a.length, aFromIndex, aToIndex);
2764 rangeCheck(b.length, bFromIndex, bToIndex);
2765
2766 int aLength = aToIndex - aFromIndex;
2767 int bLength = bToIndex - bFromIndex;
2768 if (aLength != bLength)
2769 return false;
2770
2771 return ArraysSupport.mismatch(a, aFromIndex,
2772 b, bFromIndex,
2773 aLength) < 0;
2774 }
2775
2776 /**
2777 * Returns {@code true} if the two specified arrays of chars are
2778 * <i>equal</i> to one another. Two arrays are considered equal if both
2779 * arrays contain the same number of elements, and all corresponding pairs
2780 * of elements in the two arrays are equal. In other words, two arrays
2781 * are equal if they contain the same elements in the same order. Also,
2782 * two array references are considered equal if both are {@code null}.
2783 *
2784 * @param a one array to be tested for equality
2785 * @param a2 the other array to be tested for equality
2786 * @return {@code true} if the two arrays are equal
2787 */
2788 @HotSpotIntrinsicCandidate
2789 public static boolean equals(char[] a, char[] a2) {
2790 if (a==a2)
2791 return true;
2792 if (a==null || a2==null)
2793 return false;
2794
2795 int length = a.length;
2796 if (a2.length != length)
2797 return false;
2798
2799 return ArraysSupport.mismatch(a, a2, length) < 0;
2800 }
2801
2802 /**
2803 * Returns true if the two specified arrays of chars, over the specified
2804 * ranges, are <i>equal</i> to one another.
2805 *
2806 * <p>Two arrays are considered equal if the number of elements covered by
2807 * each range is the same, and all corresponding pairs of elements over the
2808 * specified ranges in the two arrays are equal. In other words, two arrays
2809 * are equal if they contain, over the specified ranges, the same elements
2810 * in the same order.
2811 *
2812 * @param a the first array to be tested for equality
2813 * @param aFromIndex the index (inclusive) of the first element in the
2814 * first array to be tested
2815 * @param aToIndex the index (exclusive) of the last element in the
2816 * first array to be tested
2817 * @param b the second array to be tested fro equality
2818 * @param bFromIndex the index (inclusive) of the first element in the
2819 * second array to be tested
2820 * @param bToIndex the index (exclusive) of the last element in the
2821 * second array to be tested
2822 * @return {@code true} if the two arrays, over the specified ranges, are
2823 * equal
2824 * @throws IllegalArgumentException
2825 * if {@code aFromIndex > aToIndex} or
2826 * if {@code bFromIndex > bToIndex}
2827 * @throws ArrayIndexOutOfBoundsException
2828 * if {@code aFromIndex < 0 or aToIndex > a.length} or
2829 * if {@code bFromIndex < 0 or bToIndex > b.length}
2830 * @throws NullPointerException
2831 * if either array is {@code null}
2832 * @since 9
2833 */
2834 public static boolean equals(char[] a, int aFromIndex, int aToIndex,
2835 char[] b, int bFromIndex, int bToIndex) {
2836 rangeCheck(a.length, aFromIndex, aToIndex);
2837 rangeCheck(b.length, bFromIndex, bToIndex);
2838
2839 int aLength = aToIndex - aFromIndex;
2840 int bLength = bToIndex - bFromIndex;
2841 if (aLength != bLength)
2842 return false;
2843
2844 return ArraysSupport.mismatch(a, aFromIndex,
2845 b, bFromIndex,
2846 aLength) < 0;
2847 }
2848
2849 /**
2850 * Returns {@code true} if the two specified arrays of bytes are
2851 * <i>equal</i> to one another. Two arrays are considered equal if both
2852 * arrays contain the same number of elements, and all corresponding pairs
2853 * of elements in the two arrays are equal. In other words, two arrays
2854 * are equal if they contain the same elements in the same order. Also,
2855 * two array references are considered equal if both are {@code null}.
2856 *
2857 * @param a one array to be tested for equality
2858 * @param a2 the other array to be tested for equality
2859 * @return {@code true} if the two arrays are equal
2860 */
2861 @HotSpotIntrinsicCandidate
2862 public static boolean equals(byte[] a, byte[] a2) {
2863 if (a==a2)
2864 return true;
2865 if (a==null || a2==null)
2866 return false;
2867
2868 int length = a.length;
2869 if (a2.length != length)
2870 return false;
2871
2872 return ArraysSupport.mismatch(a, a2, length) < 0;
2873 }
2874
2875 /**
2876 * Returns true if the two specified arrays of bytes, over the specified
2877 * ranges, are <i>equal</i> to one another.
2878 *
2879 * <p>Two arrays are considered equal if the number of elements covered by
2880 * each range is the same, and all corresponding pairs of elements over the
2881 * specified ranges in the two arrays are equal. In other words, two arrays
2882 * are equal if they contain, over the specified ranges, the same elements
2883 * in the same order.
2884 *
2885 * @param a the first array to be tested for equality
2886 * @param aFromIndex the index (inclusive) of the first element in the
2887 * first array to be tested
2888 * @param aToIndex the index (exclusive) of the last element in the
2889 * first array to be tested
2890 * @param b the second array to be tested fro equality
2891 * @param bFromIndex the index (inclusive) of the first element in the
2892 * second array to be tested
2893 * @param bToIndex the index (exclusive) of the last element in the
2894 * second array to be tested
2895 * @return {@code true} if the two arrays, over the specified ranges, are
2896 * equal
2897 * @throws IllegalArgumentException
2898 * if {@code aFromIndex > aToIndex} or
2899 * if {@code bFromIndex > bToIndex}
2900 * @throws ArrayIndexOutOfBoundsException
2901 * if {@code aFromIndex < 0 or aToIndex > a.length} or
2902 * if {@code bFromIndex < 0 or bToIndex > b.length}
2903 * @throws NullPointerException
2904 * if either array is {@code null}
2905 * @since 9
2906 */
2907 public static boolean equals(byte[] a, int aFromIndex, int aToIndex,
2908 byte[] b, int bFromIndex, int bToIndex) {
2909 rangeCheck(a.length, aFromIndex, aToIndex);
2910 rangeCheck(b.length, bFromIndex, bToIndex);
2911
2912 int aLength = aToIndex - aFromIndex;
2913 int bLength = bToIndex - bFromIndex;
2914 if (aLength != bLength)
2915 return false;
2916
2917 return ArraysSupport.mismatch(a, aFromIndex,
2918 b, bFromIndex,
2919 aLength) < 0;
2920 }
2921
2922 /**
2923 * Returns {@code true} if the two specified arrays of booleans are
2924 * <i>equal</i> to one another. Two arrays are considered equal if both
2925 * arrays contain the same number of elements, and all corresponding pairs
2926 * of elements in the two arrays are equal. In other words, two arrays
2927 * are equal if they contain the same elements in the same order. Also,
2928 * two array references are considered equal if both are {@code null}.
2929 *
2930 * @param a one array to be tested for equality
2931 * @param a2 the other array to be tested for equality
2932 * @return {@code true} if the two arrays are equal
2933 */
2934 public static boolean equals(boolean[] a, boolean[] a2) {
2935 if (a==a2)
2936 return true;
2937 if (a==null || a2==null)
2938 return false;
2939
2940 int length = a.length;
2941 if (a2.length != length)
2942 return false;
2943
2944 return ArraysSupport.mismatch(a, a2, length) < 0;
2945 }
2946
2947 /**
2948 * Returns true if the two specified arrays of booleans, over the specified
2949 * ranges, are <i>equal</i> to one another.
2950 *
2951 * <p>Two arrays are considered equal if the number of elements covered by
2952 * each range is the same, and all corresponding pairs of elements over the
2953 * specified ranges in the two arrays are equal. In other words, two arrays
2954 * are equal if they contain, over the specified ranges, the same elements
2955 * in the same order.
2956 *
2957 * @param a the first array to be tested for equality
2958 * @param aFromIndex the index (inclusive) of the first element in the
2959 * first array to be tested
2960 * @param aToIndex the index (exclusive) of the last element in the
2961 * first array to be tested
2962 * @param b the second array to be tested fro equality
2963 * @param bFromIndex the index (inclusive) of the first element in the
2964 * second array to be tested
2965 * @param bToIndex the index (exclusive) of the last element in the
2966 * second array to be tested
2967 * @return {@code true} if the two arrays, over the specified ranges, are
2968 * equal
2969 * @throws IllegalArgumentException
2970 * if {@code aFromIndex > aToIndex} or
2971 * if {@code bFromIndex > bToIndex}
2972 * @throws ArrayIndexOutOfBoundsException
2973 * if {@code aFromIndex < 0 or aToIndex > a.length} or
2974 * if {@code bFromIndex < 0 or bToIndex > b.length}
2975 * @throws NullPointerException
2976 * if either array is {@code null}
2977 * @since 9
2978 */
2979 public static boolean equals(boolean[] a, int aFromIndex, int aToIndex,
2980 boolean[] b, int bFromIndex, int bToIndex) {
2981 rangeCheck(a.length, aFromIndex, aToIndex);
2982 rangeCheck(b.length, bFromIndex, bToIndex);
2983
2984 int aLength = aToIndex - aFromIndex;
2985 int bLength = bToIndex - bFromIndex;
2986 if (aLength != bLength)
2987 return false;
2988
2989 return ArraysSupport.mismatch(a, aFromIndex,
2990 b, bFromIndex,
2991 aLength) < 0;
2992 }
2993
2994 /**
2995 * Returns {@code true} if the two specified arrays of doubles are
2996 * <i>equal</i> to one another. Two arrays are considered equal if both
2997 * arrays contain the same number of elements, and all corresponding pairs
2998 * of elements in the two arrays are equal. In other words, two arrays
2999 * are equal if they contain the same elements in the same order. Also,
3000 * two array references are considered equal if both are {@code null}.
3001 *
3002 * Two doubles {@code d1} and {@code d2} are considered equal if:
3003 * <pre> {@code new Double(d1).equals(new Double(d2))}</pre>
3004 * (Unlike the {@code ==} operator, this method considers
3005 * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
3006 *
3007 * @param a one array to be tested for equality
3008 * @param a2 the other array to be tested for equality
3009 * @return {@code true} if the two arrays are equal
3010 * @see Double#equals(Object)
3011 */
3012 public static boolean equals(double[] a, double[] a2) {
3013 if (a==a2)
3014 return true;
3015 if (a==null || a2==null)
3016 return false;
3017
3018 int length = a.length;
3019 if (a2.length != length)
3020 return false;
3021
3022 return ArraysSupport.mismatch(a, a2, length) < 0;
3023 }
3024
3025 /**
3026 * Returns true if the two specified arrays of doubles, over the specified
3027 * ranges, are <i>equal</i> to one another.
3028 *
3029 * <p>Two arrays are considered equal if the number of elements covered by
3030 * each range is the same, and all corresponding pairs of elements over the
3031 * specified ranges in the two arrays are equal. In other words, two arrays
3032 * are equal if they contain, over the specified ranges, the same elements
3033 * in the same order.
3034 *
3035 * <p>Two doubles {@code d1} and {@code d2} are considered equal if:
3036 * <pre> {@code new Double(d1).equals(new Double(d2))}</pre>
3037 * (Unlike the {@code ==} operator, this method considers
3038 * {@code NaN} equals to itself, and 0.0d unequal to -0.0d.)
3039 *
3040 * @param a the first array to be tested for equality
3041 * @param aFromIndex the index (inclusive) of the first element in the
3042 * first array to be tested
3043 * @param aToIndex the index (exclusive) of the last element in the
3044 * first array to be tested
3045 * @param b the second array to be tested fro equality
3046 * @param bFromIndex the index (inclusive) of the first element in the
3047 * second array to be tested
3048 * @param bToIndex the index (exclusive) of the last element in the
3049 * second array to be tested
3050 * @return {@code true} if the two arrays, over the specified ranges, are
3051 * equal
3052 * @throws IllegalArgumentException
3053 * if {@code aFromIndex > aToIndex} or
3054 * if {@code bFromIndex > bToIndex}
3055 * @throws ArrayIndexOutOfBoundsException
3056 * if {@code aFromIndex < 0 or aToIndex > a.length} or
3057 * if {@code bFromIndex < 0 or bToIndex > b.length}
3058 * @throws NullPointerException
3059 * if either array is {@code null}
3060 * @see Double#equals(Object)
3061 * @since 9
3062 */
3063 public static boolean equals(double[] a, int aFromIndex, int aToIndex,
3064 double[] b, int bFromIndex, int bToIndex) {
3065 rangeCheck(a.length, aFromIndex, aToIndex);
3066 rangeCheck(b.length, bFromIndex, bToIndex);
3067
3068 int aLength = aToIndex - aFromIndex;
3069 int bLength = bToIndex - bFromIndex;
3070 if (aLength != bLength)
3071 return false;
3072
3073 return ArraysSupport.mismatch(a, aFromIndex,
3074 b, bFromIndex, aLength) < 0;
3075 }
3076
3077 /**
3078 * Returns {@code true} if the two specified arrays of floats are
3079 * <i>equal</i> to one another. Two arrays are considered equal if both
3080 * arrays contain the same number of elements, and all corresponding pairs
3081 * of elements in the two arrays are equal. In other words, two arrays
3082 * are equal if they contain the same elements in the same order. Also,
3083 * two array references are considered equal if both are {@code null}.
3084 *
3085 * Two floats {@code f1} and {@code f2} are considered equal if:
3086 * <pre> {@code new Float(f1).equals(new Float(f2))}</pre>
3087 * (Unlike the {@code ==} operator, this method considers
3088 * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
3089 *
3090 * @param a one array to be tested for equality
3091 * @param a2 the other array to be tested for equality
3092 * @return {@code true} if the two arrays are equal
3093 * @see Float#equals(Object)
3094 */
3095 public static boolean equals(float[] a, float[] a2) {
3096 if (a==a2)
3097 return true;
3098 if (a==null || a2==null)
3099 return false;
3100
3101 int length = a.length;
3102 if (a2.length != length)
3103 return false;
3104
3105 return ArraysSupport.mismatch(a, a2, length) < 0;
3106 }
3107
3108 /**
3109 * Returns true if the two specified arrays of floats, over the specified
3110 * ranges, are <i>equal</i> to one another.
3111 *
3112 * <p>Two arrays are considered equal if the number of elements covered by
3113 * each range is the same, and all corresponding pairs of elements over the
3114 * specified ranges in the two arrays are equal. In other words, two arrays
3115 * are equal if they contain, over the specified ranges, the same elements
3116 * in the same order.
3117 *
3118 * <p>Two floats {@code f1} and {@code f2} are considered equal if:
3119 * <pre> {@code new Float(f1).equals(new Float(f2))}</pre>
3120 * (Unlike the {@code ==} operator, this method considers
3121 * {@code NaN} equals to itself, and 0.0f unequal to -0.0f.)
3122 *
3123 * @param a the first array to be tested for equality
3124 * @param aFromIndex the index (inclusive) of the first element in the
3125 * first array to be tested
3126 * @param aToIndex the index (exclusive) of the last element in the
3127 * first array to be tested
3128 * @param b the second array to be tested fro equality
3129 * @param bFromIndex the index (inclusive) of the first element in the
3130 * second array to be tested
3131 * @param bToIndex the index (exclusive) of the last element in the
3132 * second array to be tested
3133 * @return {@code true} if the two arrays, over the specified ranges, are
3134 * equal
3135 * @throws IllegalArgumentException
3136 * if {@code aFromIndex > aToIndex} or
3137 * if {@code bFromIndex > bToIndex}
3138 * @throws ArrayIndexOutOfBoundsException
3139 * if {@code aFromIndex < 0 or aToIndex > a.length} or
3140 * if {@code bFromIndex < 0 or bToIndex > b.length}
3141 * @throws NullPointerException
3142 * if either array is {@code null}
3143 * @see Float#equals(Object)
3144 * @since 9
3145 */
3146 public static boolean equals(float[] a, int aFromIndex, int aToIndex,
3147 float[] b, int bFromIndex, int bToIndex) {
3148 rangeCheck(a.length, aFromIndex, aToIndex);
3149 rangeCheck(b.length, bFromIndex, bToIndex);
3150
3151 int aLength = aToIndex - aFromIndex;
3152 int bLength = bToIndex - bFromIndex;
3153 if (aLength != bLength)
3154 return false;
3155
3156 return ArraysSupport.mismatch(a, aFromIndex,
3157 b, bFromIndex, aLength) < 0;
3158 }
3159
3160 /**
3161 * Returns {@code true} if the two specified arrays of Objects are
3162 * <i>equal</i> to one another. The two arrays are considered equal if
3163 * both arrays contain the same number of elements, and all corresponding
3164 * pairs of elements in the two arrays are equal. Two objects {@code e1}
3165 * and {@code e2} are considered <i>equal</i> if
3166 * {@code Objects.equals(e1, e2)}.
3167 * In other words, the two arrays are equal if
3168 * they contain the same elements in the same order. Also, two array
3169 * references are considered equal if both are {@code null}.
3170 *
3171 * @param a one array to be tested for equality
3172 * @param a2 the other array to be tested for equality
3173 * @return {@code true} if the two arrays are equal
3174 */
3175 public static boolean equals(Object[] a, Object[] a2) {
3176 if (a==a2)
3177 return true;
3178 if (a==null || a2==null)
3179 return false;
3180
3181 int length = a.length;
3182 if (a2.length != length)
3183 return false;
3184
3185 for (int i=0; i<length; i++) {
3186 if (!Objects.equals(a[i], a2[i]))
3187 return false;
3188 }
3189
3190 return true;
3191 }
3192
3193 /**
3194 * Returns true if the two specified arrays of Objects, over the specified
3195 * ranges, are <i>equal</i> to one another.
3196 *
3197 * <p>Two arrays are considered equal if the number of elements covered by
3198 * each range is the same, and all corresponding pairs of elements over the
3199 * specified ranges in the two arrays are equal. In other words, two arrays
3200 * are equal if they contain, over the specified ranges, the same elements
3201 * in the same order.
3202 *
3203 * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if
3204 * {@code Objects.equals(e1, e2)}.
3205 *
3206 * @param a the first array to be tested for equality
3207 * @param aFromIndex the index (inclusive) of the first element in the
3208 * first array to be tested
3209 * @param aToIndex the index (exclusive) of the last element in the
3210 * first array to be tested
3211 * @param b the second array to be tested fro equality
3212 * @param bFromIndex the index (inclusive) of the first element in the
3213 * second array to be tested
3214 * @param bToIndex the index (exclusive) of the last element in the
3215 * second array to be tested
3216 * @return {@code true} if the two arrays, over the specified ranges, are
3217 * equal
3218 * @throws IllegalArgumentException
3219 * if {@code aFromIndex > aToIndex} or
3220 * if {@code bFromIndex > bToIndex}
3221 * @throws ArrayIndexOutOfBoundsException
3222 * if {@code aFromIndex < 0 or aToIndex > a.length} or
3223 * if {@code bFromIndex < 0 or bToIndex > b.length}
3224 * @throws NullPointerException
3225 * if either array is {@code null}
3226 * @since 9
3227 */
3228 public static boolean equals(Object[] a, int aFromIndex, int aToIndex,
3229 Object[] b, int bFromIndex, int bToIndex) {
3230 rangeCheck(a.length, aFromIndex, aToIndex);
3231 rangeCheck(b.length, bFromIndex, bToIndex);
3232
3233 int aLength = aToIndex - aFromIndex;
3234 int bLength = bToIndex - bFromIndex;
3235 if (aLength != bLength)
3236 return false;
3237
3238 for (int i = 0; i < aLength; i++) {
3239 if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
3240 return false;
3241 }
3242
3243 return true;
3244 }
3245
3246 /**
3247 * Returns {@code true} if the two specified arrays of Objects are
3248 * <i>equal</i> to one another.
3249 *
3250 * <p>Two arrays are considered equal if both arrays contain the same number
3251 * of elements, and all corresponding pairs of elements in the two arrays
3252 * are equal. In other words, the two arrays are equal if they contain the
3253 * same elements in the same order. Also, two array references are
3254 * considered equal if both are {@code null}.
3255 *
3256 * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if,
3257 * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
3258 *
3259 * @param a one array to be tested for equality
3260 * @param a2 the other array to be tested for equality
3261 * @param cmp the comparator to compare array elements
3262 * @param <T> the type of array elements
3263 * @return {@code true} if the two arrays are equal
3264 * @throws NullPointerException if the comparator is {@code null}
3265 * @since 9
3266 */
3267 public static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) {
3268 Objects.requireNonNull(cmp);
3269 if (a==a2)
3270 return true;
3271 if (a==null || a2==null)
3272 return false;
3273
3274 int length = a.length;
3275 if (a2.length != length)
3276 return false;
3277
3278 for (int i=0; i<length; i++) {
3279 if (cmp.compare(a[i], a2[i]) != 0)
3280 return false;
3281 }
3282
3283 return true;
3284 }
3285
3286 /**
3287 * Returns true if the two specified arrays of Objects, over the specified
3288 * ranges, are <i>equal</i> to one another.
3289 *
3290 * <p>Two arrays are considered equal if the number of elements covered by
3291 * each range is the same, and all corresponding pairs of elements over the
3292 * specified ranges in the two arrays are equal. In other words, two arrays
3293 * are equal if they contain, over the specified ranges, the same elements
3294 * in the same order.
3295 *
3296 * <p>Two objects {@code e1} and {@code e2} are considered <i>equal</i> if,
3297 * given the specified comparator, {@code cmp.compare(e1, e2) == 0}.
3298 *
3299 * @param a the first array to be tested for equality
3300 * @param aFromIndex the index (inclusive) of the first element in the
3301 * first array to be tested
3302 * @param aToIndex the index (exclusive) of the last element in the
3303 * first array to be tested
3304 * @param b the second array to be tested fro equality
3305 * @param bFromIndex the index (inclusive) of the first element in the
3306 * second array to be tested
3307 * @param bToIndex the index (exclusive) of the last element in the
3308 * second array to be tested
3309 * @param cmp the comparator to compare array elements
3310 * @param <T> the type of array elements
3311 * @return {@code true} if the two arrays, over the specified ranges, are
3312 * equal
3313 * @throws IllegalArgumentException
3314 * if {@code aFromIndex > aToIndex} or
3315 * if {@code bFromIndex > bToIndex}
3316 * @throws ArrayIndexOutOfBoundsException
3317 * if {@code aFromIndex < 0 or aToIndex > a.length} or
3318 * if {@code bFromIndex < 0 or bToIndex > b.length}
3319 * @throws NullPointerException
3320 * if either array or the comparator is {@code null}
3321 * @since 9
3322 */
3323 public static <T> boolean equals(T[] a, int aFromIndex, int aToIndex,
3324 T[] b, int bFromIndex, int bToIndex,
3325 Comparator<? super T> cmp) {
3326 Objects.requireNonNull(cmp);
3327 rangeCheck(a.length, aFromIndex, aToIndex);
3328 rangeCheck(b.length, bFromIndex, bToIndex);
3329
3330 int aLength = aToIndex - aFromIndex;
3331 int bLength = bToIndex - bFromIndex;
3332 if (aLength != bLength)
3333 return false;
3334
3335 for (int i = 0; i < aLength; i++) {
3336 if (cmp.compare(a[aFromIndex++], b[bFromIndex++]) != 0)
3337 return false;
3338 }
3339
3340 return true;
3341 }
3342
3343 // Filling
3344
3345 /**
3346 * Assigns the specified long value to each element of the specified array
3347 * of longs.
3348 *
3349 * @param a the array to be filled
3350 * @param val the value to be stored in all elements of the array
3351 */
3352 public static void fill(long[] a, long val) {
3353 for (int i = 0, len = a.length; i < len; i++)
3354 a[i] = val;
3355 }
3356
3357 /**
3358 * Assigns the specified long value to each element of the specified
3359 * range of the specified array of longs. The range to be filled
3360 * extends from index {@code fromIndex}, inclusive, to index
3361 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3362 * range to be filled is empty.)
3363 *
3364 * @param a the array to be filled
3365 * @param fromIndex the index of the first element (inclusive) to be
3366 * filled with the specified value
3367 * @param toIndex the index of the last element (exclusive) to be
3368 * filled with the specified value
3369 * @param val the value to be stored in all elements of the array
3370 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3371 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3372 * {@code toIndex > a.length}
3373 */
3374 public static void fill(long[] a, int fromIndex, int toIndex, long val) {
3375 rangeCheck(a.length, fromIndex, toIndex);
3376 for (int i = fromIndex; i < toIndex; i++)
3377 a[i] = val;
3378 }
3379
3380 /**
3381 * Assigns the specified int value to each element of the specified array
3382 * of ints.
3383 *
3384 * @param a the array to be filled
3385 * @param val the value to be stored in all elements of the array
3386 */
3387 public static void fill(int[] a, int val) {
3388 for (int i = 0, len = a.length; i < len; i++)
3389 a[i] = val;
3390 }
3391
3392 /**
3393 * Assigns the specified int value to each element of the specified
3394 * range of the specified array of ints. The range to be filled
3395 * extends from index {@code fromIndex}, inclusive, to index
3396 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3397 * range to be filled is empty.)
3398 *
3399 * @param a the array to be filled
3400 * @param fromIndex the index of the first element (inclusive) to be
3401 * filled with the specified value
3402 * @param toIndex the index of the last element (exclusive) to be
3403 * filled with the specified value
3404 * @param val the value to be stored in all elements of the array
3405 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3406 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3407 * {@code toIndex > a.length}
3408 */
3409 public static void fill(int[] a, int fromIndex, int toIndex, int val) {
3410 rangeCheck(a.length, fromIndex, toIndex);
3411 for (int i = fromIndex; i < toIndex; i++)
3412 a[i] = val;
3413 }
3414
3415 /**
3416 * Assigns the specified short value to each element of the specified array
3417 * of shorts.
3418 *
3419 * @param a the array to be filled
3420 * @param val the value to be stored in all elements of the array
3421 */
3422 public static void fill(short[] a, short val) {
3423 for (int i = 0, len = a.length; i < len; i++)
3424 a[i] = val;
3425 }
3426
3427 /**
3428 * Assigns the specified short value to each element of the specified
3429 * range of the specified array of shorts. The range to be filled
3430 * extends from index {@code fromIndex}, inclusive, to index
3431 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3432 * range to be filled is empty.)
3433 *
3434 * @param a the array to be filled
3435 * @param fromIndex the index of the first element (inclusive) to be
3436 * filled with the specified value
3437 * @param toIndex the index of the last element (exclusive) to be
3438 * filled with the specified value
3439 * @param val the value to be stored in all elements of the array
3440 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3441 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3442 * {@code toIndex > a.length}
3443 */
3444 public static void fill(short[] a, int fromIndex, int toIndex, short val) {
3445 rangeCheck(a.length, fromIndex, toIndex);
3446 for (int i = fromIndex; i < toIndex; i++)
3447 a[i] = val;
3448 }
3449
3450 /**
3451 * Assigns the specified char value to each element of the specified array
3452 * of chars.
3453 *
3454 * @param a the array to be filled
3455 * @param val the value to be stored in all elements of the array
3456 */
3457 public static void fill(char[] a, char val) {
3458 for (int i = 0, len = a.length; i < len; i++)
3459 a[i] = val;
3460 }
3461
3462 /**
3463 * Assigns the specified char value to each element of the specified
3464 * range of the specified array of chars. The range to be filled
3465 * extends from index {@code fromIndex}, inclusive, to index
3466 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3467 * range to be filled is empty.)
3468 *
3469 * @param a the array to be filled
3470 * @param fromIndex the index of the first element (inclusive) to be
3471 * filled with the specified value
3472 * @param toIndex the index of the last element (exclusive) to be
3473 * filled with the specified value
3474 * @param val the value to be stored in all elements of the array
3475 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3476 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3477 * {@code toIndex > a.length}
3478 */
3479 public static void fill(char[] a, int fromIndex, int toIndex, char val) {
3480 rangeCheck(a.length, fromIndex, toIndex);
3481 for (int i = fromIndex; i < toIndex; i++)
3482 a[i] = val;
3483 }
3484
3485 /**
3486 * Assigns the specified byte value to each element of the specified array
3487 * of bytes.
3488 *
3489 * @param a the array to be filled
3490 * @param val the value to be stored in all elements of the array
3491 */
3492 public static void fill(byte[] a, byte val) {
3493 for (int i = 0, len = a.length; i < len; i++)
3494 a[i] = val;
3495 }
3496
3497 /**
3498 * Assigns the specified byte value to each element of the specified
3499 * range of the specified array of bytes. The range to be filled
3500 * extends from index {@code fromIndex}, inclusive, to index
3501 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3502 * range to be filled is empty.)
3503 *
3504 * @param a the array to be filled
3505 * @param fromIndex the index of the first element (inclusive) to be
3506 * filled with the specified value
3507 * @param toIndex the index of the last element (exclusive) to be
3508 * filled with the specified value
3509 * @param val the value to be stored in all elements of the array
3510 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3511 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3512 * {@code toIndex > a.length}
3513 */
3514 public static void fill(byte[] a, int fromIndex, int toIndex, byte val) {
3515 rangeCheck(a.length, fromIndex, toIndex);
3516 for (int i = fromIndex; i < toIndex; i++)
3517 a[i] = val;
3518 }
3519
3520 /**
3521 * Assigns the specified boolean value to each element of the specified
3522 * array of booleans.
3523 *
3524 * @param a the array to be filled
3525 * @param val the value to be stored in all elements of the array
3526 */
3527 public static void fill(boolean[] a, boolean val) {
3528 for (int i = 0, len = a.length; i < len; i++)
3529 a[i] = val;
3530 }
3531
3532 /**
3533 * Assigns the specified boolean value to each element of the specified
3534 * range of the specified array of booleans. The range to be filled
3535 * extends from index {@code fromIndex}, inclusive, to index
3536 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3537 * range to be filled is empty.)
3538 *
3539 * @param a the array to be filled
3540 * @param fromIndex the index of the first element (inclusive) to be
3541 * filled with the specified value
3542 * @param toIndex the index of the last element (exclusive) to be
3543 * filled with the specified value
3544 * @param val the value to be stored in all elements of the array
3545 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3546 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3547 * {@code toIndex > a.length}
3548 */
3549 public static void fill(boolean[] a, int fromIndex, int toIndex,
3550 boolean val) {
3551 rangeCheck(a.length, fromIndex, toIndex);
3552 for (int i = fromIndex; i < toIndex; i++)
3553 a[i] = val;
3554 }
3555
3556 /**
3557 * Assigns the specified double value to each element of the specified
3558 * array of doubles.
3559 *
3560 * @param a the array to be filled
3561 * @param val the value to be stored in all elements of the array
3562 */
3563 public static void fill(double[] a, double val) {
3564 for (int i = 0, len = a.length; i < len; i++)
3565 a[i] = val;
3566 }
3567
3568 /**
3569 * Assigns the specified double value to each element of the specified
3570 * range of the specified array of doubles. The range to be filled
3571 * extends from index {@code fromIndex}, inclusive, to index
3572 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3573 * range to be filled is empty.)
3574 *
3575 * @param a the array to be filled
3576 * @param fromIndex the index of the first element (inclusive) to be
3577 * filled with the specified value
3578 * @param toIndex the index of the last element (exclusive) to be
3579 * filled with the specified value
3580 * @param val the value to be stored in all elements of the array
3581 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3582 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3583 * {@code toIndex > a.length}
3584 */
3585 public static void fill(double[] a, int fromIndex, int toIndex,double val){
3586 rangeCheck(a.length, fromIndex, toIndex);
3587 for (int i = fromIndex; i < toIndex; i++)
3588 a[i] = val;
3589 }
3590
3591 /**
3592 * Assigns the specified float value to each element of the specified array
3593 * of floats.
3594 *
3595 * @param a the array to be filled
3596 * @param val the value to be stored in all elements of the array
3597 */
3598 public static void fill(float[] a, float val) {
3599 for (int i = 0, len = a.length; i < len; i++)
3600 a[i] = val;
3601 }
3602
3603 /**
3604 * Assigns the specified float value to each element of the specified
3605 * range of the specified array of floats. The range to be filled
3606 * extends from index {@code fromIndex}, inclusive, to index
3607 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3608 * range to be filled is empty.)
3609 *
3610 * @param a the array to be filled
3611 * @param fromIndex the index of the first element (inclusive) to be
3612 * filled with the specified value
3613 * @param toIndex the index of the last element (exclusive) to be
3614 * filled with the specified value
3615 * @param val the value to be stored in all elements of the array
3616 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3617 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3618 * {@code toIndex > a.length}
3619 */
3620 public static void fill(float[] a, int fromIndex, int toIndex, float val) {
3621 rangeCheck(a.length, fromIndex, toIndex);
3622 for (int i = fromIndex; i < toIndex; i++)
3623 a[i] = val;
3624 }
3625
3626 /**
3627 * Assigns the specified Object reference to each element of the specified
3628 * array of Objects.
3629 *
3630 * @param a the array to be filled
3631 * @param val the value to be stored in all elements of the array
3632 * @throws ArrayStoreException if the specified value is not of a
3633 * runtime type that can be stored in the specified array
3634 */
3635 public static void fill(Object[] a, Object val) {
3636 for (int i = 0, len = a.length; i < len; i++)
3637 a[i] = val;
3638 }
3639
3640 /**
3641 * Assigns the specified Object reference to each element of the specified
3642 * range of the specified array of Objects. The range to be filled
3643 * extends from index {@code fromIndex}, inclusive, to index
3644 * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the
3645 * range to be filled is empty.)
3646 *
3647 * @param a the array to be filled
3648 * @param fromIndex the index of the first element (inclusive) to be
3649 * filled with the specified value
3650 * @param toIndex the index of the last element (exclusive) to be
3651 * filled with the specified value
3652 * @param val the value to be stored in all elements of the array
3653 * @throws IllegalArgumentException if {@code fromIndex > toIndex}
3654 * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or
3655 * {@code toIndex > a.length}
3656 * @throws ArrayStoreException if the specified value is not of a
3657 * runtime type that can be stored in the specified array
3658 */
3659 public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
3660 rangeCheck(a.length, fromIndex, toIndex);
3661 for (int i = fromIndex; i < toIndex; i++)
3662 a[i] = val;
3663 }
3664
3665 // Cloning
3666
3667 /**
3668 * Copies the specified array, truncating or padding with nulls (if necessary)
3669 * so the copy has the specified length. For all indices that are
3670 * valid in both the original array and the copy, the two arrays will
3671 * contain identical values. For any indices that are valid in the
3672 * copy but not the original, the copy will contain {@code null}.
3673 * Such indices will exist if and only if the specified length
3674 * is greater than that of the original array.
3675 * The resulting array is of exactly the same class as the original array.
3676 *
3677 * @param <T> the class of the objects in the array
3678 * @param original the array to be copied
3679 * @param newLength the length of the copy to be returned
3680 * @return a copy of the original array, truncated or padded with nulls
3681 * to obtain the specified length
3682 * @throws NegativeArraySizeException if {@code newLength} is negative
3683 * @throws NullPointerException if {@code original} is null
3684 * @since 1.6
3685 */
3686 @SuppressWarnings("unchecked")
3687 public static <T> T[] copyOf(T[] original, int newLength) {
3688 return (T[]) copyOf(original, newLength, original.getClass());
3689 }
3690
3691 /**
3692 * Copies the specified array, truncating or padding with nulls (if necessary)
3693 * so the copy has the specified length. For all indices that are
3694 * valid in both the original array and the copy, the two arrays will
3695 * contain identical values. For any indices that are valid in the
3696 * copy but not the original, the copy will contain {@code null}.
3697 * Such indices will exist if and only if the specified length
3698 * is greater than that of the original array.
3699 * The resulting array is of the class {@code newType}.
3700 *
3701 * @param <U> the class of the objects in the original array
3702 * @param <T> the class of the objects in the returned array
3703 * @param original the array to be copied
3704 * @param newLength the length of the copy to be returned
3705 * @param newType the class of the copy to be returned
3706 * @return a copy of the original array, truncated or padded with nulls
3707 * to obtain the specified length
3708 * @throws NegativeArraySizeException if {@code newLength} is negative
3709 * @throws NullPointerException if {@code original} is null
3710 * @throws ArrayStoreException if an element copied from
3711 * {@code original} is not of a runtime type that can be stored in
3712 * an array of class {@code newType}
3713 * @since 1.6
3714 */
3715 @HotSpotIntrinsicCandidate
3716 public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
3717 @SuppressWarnings("unchecked")
3718 T[] copy = ((Object)newType == (Object)Object[].class)
3719 ? (T[]) new Object[newLength]
3720 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3721 System.arraycopy(original, 0, copy, 0,
3722 Math.min(original.length, newLength));
3723 return copy;
3724 }
3725
3726 /**
3727 * Copies the specified array, truncating or padding with zeros (if necessary)
3728 * so the copy has the specified length. For all indices that are
3729 * valid in both the original array and the copy, the two arrays will
3730 * contain identical values. For any indices that are valid in the
3731 * copy but not the original, the copy will contain {@code (byte)0}.
3732 * Such indices will exist if and only if the specified length
3733 * is greater than that of the original array.
3734 *
3735 * @param original the array to be copied
3736 * @param newLength the length of the copy to be returned
3737 * @return a copy of the original array, truncated or padded with zeros
3738 * to obtain the specified length
3739 * @throws NegativeArraySizeException if {@code newLength} is negative
3740 * @throws NullPointerException if {@code original} is null
3741 * @since 1.6
3742 */
3743 public static byte[] copyOf(byte[] original, int newLength) {
3744 byte[] copy = new byte[newLength];
3745 System.arraycopy(original, 0, copy, 0,
3746 Math.min(original.length, newLength));
3747 return copy;
3748 }
3749
3750 /**
3751 * Copies the specified array, truncating or padding with zeros (if necessary)
3752 * so the copy has the specified length. For all indices that are
3753 * valid in both the original array and the copy, the two arrays will
3754 * contain identical values. For any indices that are valid in the
3755 * copy but not the original, the copy will contain {@code (short)0}.
3756 * Such indices will exist if and only if the specified length
3757 * is greater than that of the original array.
3758 *
3759 * @param original the array to be copied
3760 * @param newLength the length of the copy to be returned
3761 * @return a copy of the original array, truncated or padded with zeros
3762 * to obtain the specified length
3763 * @throws NegativeArraySizeException if {@code newLength} is negative
3764 * @throws NullPointerException if {@code original} is null
3765 * @since 1.6
3766 */
3767 public static short[] copyOf(short[] original, int newLength) {
3768 short[] copy = new short[newLength];
3769 System.arraycopy(original, 0, copy, 0,
3770 Math.min(original.length, newLength));
3771 return copy;
3772 }
3773
3774 /**
3775 * Copies the specified array, truncating or padding with zeros (if necessary)
3776 * so the copy has the specified length. For all indices that are
3777 * valid in both the original array and the copy, the two arrays will
3778 * contain identical values. For any indices that are valid in the
3779 * copy but not the original, the copy will contain {@code 0}.
3780 * Such indices will exist if and only if the specified length
3781 * is greater than that of the original array.
3782 *
3783 * @param original the array to be copied
3784 * @param newLength the length of the copy to be returned
3785 * @return a copy of the original array, truncated or padded with zeros
3786 * to obtain the specified length
3787 * @throws NegativeArraySizeException if {@code newLength} is negative
3788 * @throws NullPointerException if {@code original} is null
3789 * @since 1.6
3790 */
3791 public static int[] copyOf(int[] original, int newLength) {
3792 int[] copy = new int[newLength];
3793 System.arraycopy(original, 0, copy, 0,
3794 Math.min(original.length, newLength));
3795 return copy;
3796 }
3797
3798 /**
3799 * Copies the specified array, truncating or padding with zeros (if necessary)
3800 * so the copy has the specified length. For all indices that are
3801 * valid in both the original array and the copy, the two arrays will
3802 * contain identical values. For any indices that are valid in the
3803 * copy but not the original, the copy will contain {@code 0L}.
3804 * Such indices will exist if and only if the specified length
3805 * is greater than that of the original array.
3806 *
3807 * @param original the array to be copied
3808 * @param newLength the length of the copy to be returned
3809 * @return a copy of the original array, truncated or padded with zeros
3810 * to obtain the specified length
3811 * @throws NegativeArraySizeException if {@code newLength} is negative
3812 * @throws NullPointerException if {@code original} is null
3813 * @since 1.6
3814 */
3815 public static long[] copyOf(long[] original, int newLength) {
3816 long[] copy = new long[newLength];
3817 System.arraycopy(original, 0, copy, 0,
3818 Math.min(original.length, newLength));
3819 return copy;
3820 }
3821
3822 /**
3823 * Copies the specified array, truncating or padding with null characters (if necessary)
3824 * so the copy has the specified length. For all indices that are valid
3825 * in both the original array and the copy, the two arrays will contain
3826 * identical values. For any indices that are valid in the copy but not
3827 * the original, the copy will contain {@code '\\u000'}. Such indices
3828 * will exist if and only if the specified length is greater than that of
3829 * the original array.
3830 *
3831 * @param original the array to be copied
3832 * @param newLength the length of the copy to be returned
3833 * @return a copy of the original array, truncated or padded with null characters
3834 * to obtain the specified length
3835 * @throws NegativeArraySizeException if {@code newLength} is negative
3836 * @throws NullPointerException if {@code original} is null
3837 * @since 1.6
3838 */
3839 public static char[] copyOf(char[] original, int newLength) {
3840 char[] copy = new char[newLength];
3841 System.arraycopy(original, 0, copy, 0,
3842 Math.min(original.length, newLength));
3843 return copy;
3844 }
3845
3846 /**
3847 * Copies the specified array, truncating or padding with zeros (if necessary)
3848 * so the copy has the specified length. For all indices that are
3849 * valid in both the original array and the copy, the two arrays will
3850 * contain identical values. For any indices that are valid in the
3851 * copy but not the original, the copy will contain {@code 0f}.
3852 * Such indices will exist if and only if the specified length
3853 * is greater than that of the original array.
3854 *
3855 * @param original the array to be copied
3856 * @param newLength the length of the copy to be returned
3857 * @return a copy of the original array, truncated or padded with zeros
3858 * to obtain the specified length
3859 * @throws NegativeArraySizeException if {@code newLength} is negative
3860 * @throws NullPointerException if {@code original} is null
3861 * @since 1.6
3862 */
3863 public static float[] copyOf(float[] original, int newLength) {
3864 float[] copy = new float[newLength];
3865 System.arraycopy(original, 0, copy, 0,
3866 Math.min(original.length, newLength));
3867 return copy;
3868 }
3869
3870 /**
3871 * Copies the specified array, truncating or padding with zeros (if necessary)
3872 * so the copy has the specified length. For all indices that are
3873 * valid in both the original array and the copy, the two arrays will
3874 * contain identical values. For any indices that are valid in the
3875 * copy but not the original, the copy will contain {@code 0d}.
3876 * Such indices will exist if and only if the specified length
3877 * is greater than that of the original array.
3878 *
3879 * @param original the array to be copied
3880 * @param newLength the length of the copy to be returned
3881 * @return a copy of the original array, truncated or padded with zeros
3882 * to obtain the specified length
3883 * @throws NegativeArraySizeException if {@code newLength} is negative
3884 * @throws NullPointerException if {@code original} is null
3885 * @since 1.6
3886 */
3887 public static double[] copyOf(double[] original, int newLength) {
3888 double[] copy = new double[newLength];
3889 System.arraycopy(original, 0, copy, 0,
3890 Math.min(original.length, newLength));
3891 return copy;
3892 }
3893
3894 /**
3895 * Copies the specified array, truncating or padding with {@code false} (if necessary)
3896 * so the copy has the specified length. For all indices that are
3897 * valid in both the original array and the copy, the two arrays will
3898 * contain identical values. For any indices that are valid in the
3899 * copy but not the original, the copy will contain {@code false}.
3900 * Such indices will exist if and only if the specified length
3901 * is greater than that of the original array.
3902 *
3903 * @param original the array to be copied
3904 * @param newLength the length of the copy to be returned
3905 * @return a copy of the original array, truncated or padded with false elements
3906 * to obtain the specified length
3907 * @throws NegativeArraySizeException if {@code newLength} is negative
3908 * @throws NullPointerException if {@code original} is null
3909 * @since 1.6
3910 */
3911 public static boolean[] copyOf(boolean[] original, int newLength) {
3912 boolean[] copy = new boolean[newLength];
3913 System.arraycopy(original, 0, copy, 0,
3914 Math.min(original.length, newLength));
3915 return copy;
3916 }
3917
3918 /**
3919 * Copies the specified range of the specified array into a new array.
3920 * The initial index of the range ({@code from}) must lie between zero
3921 * and {@code original.length}, inclusive. The value at
3922 * {@code original[from]} is placed into the initial element of the copy
3923 * (unless {@code from == original.length} or {@code from == to}).
3924 * Values from subsequent elements in the original array are placed into
3925 * subsequent elements in the copy. The final index of the range
3926 * ({@code to}), which must be greater than or equal to {@code from},
3927 * may be greater than {@code original.length}, in which case
3928 * {@code null} is placed in all elements of the copy whose index is
3929 * greater than or equal to {@code original.length - from}. The length
3930 * of the returned array will be {@code to - from}.
3931 * <p>
3932 * The resulting array is of exactly the same class as the original array.
3933 *
3934 * @param <T> the class of the objects in the array
3935 * @param original the array from which a range is to be copied
3936 * @param from the initial index of the range to be copied, inclusive
3937 * @param to the final index of the range to be copied, exclusive.
3938 * (This index may lie outside the array.)
3939 * @return a new array containing the specified range from the original array,
3940 * truncated or padded with nulls to obtain the required length
3941 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3942 * or {@code from > original.length}
3943 * @throws IllegalArgumentException if {@code from > to}
3944 * @throws NullPointerException if {@code original} is null
3945 * @since 1.6
3946 */
3947 @SuppressWarnings("unchecked")
3948 public static <T> T[] copyOfRange(T[] original, int from, int to) {
3949 return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
3950 }
3951
3952 /**
3953 * Copies the specified range of the specified array into a new array.
3954 * The initial index of the range ({@code from}) must lie between zero
3955 * and {@code original.length}, inclusive. The value at
3956 * {@code original[from]} is placed into the initial element of the copy
3957 * (unless {@code from == original.length} or {@code from == to}).
3958 * Values from subsequent elements in the original array are placed into
3959 * subsequent elements in the copy. The final index of the range
3960 * ({@code to}), which must be greater than or equal to {@code from},
3961 * may be greater than {@code original.length}, in which case
3962 * {@code null} is placed in all elements of the copy whose index is
3963 * greater than or equal to {@code original.length - from}. The length
3964 * of the returned array will be {@code to - from}.
3965 * The resulting array is of the class {@code newType}.
3966 *
3967 * @param <U> the class of the objects in the original array
3968 * @param <T> the class of the objects in the returned array
3969 * @param original the array from which a range is to be copied
3970 * @param from the initial index of the range to be copied, inclusive
3971 * @param to the final index of the range to be copied, exclusive.
3972 * (This index may lie outside the array.)
3973 * @param newType the class of the copy to be returned
3974 * @return a new array containing the specified range from the original array,
3975 * truncated or padded with nulls to obtain the required length
3976 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
3977 * or {@code from > original.length}
3978 * @throws IllegalArgumentException if {@code from > to}
3979 * @throws NullPointerException if {@code original} is null
3980 * @throws ArrayStoreException if an element copied from
3981 * {@code original} is not of a runtime type that can be stored in
3982 * an array of class {@code newType}.
3983 * @since 1.6
3984 */
3985 @HotSpotIntrinsicCandidate
3986 public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3987 int newLength = to - from;
3988 if (newLength < 0)
3989 throw new IllegalArgumentException(from + " > " + to);
3990 @SuppressWarnings("unchecked")
3991 T[] copy = ((Object)newType == (Object)Object[].class)
3992 ? (T[]) new Object[newLength]
3993 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3994 System.arraycopy(original, from, copy, 0,
3995 Math.min(original.length - from, newLength));
3996 return copy;
3997 }
3998
3999 /**
4000 * Copies the specified range of the specified array into a new array.
4001 * The initial index of the range ({@code from}) must lie between zero
4002 * and {@code original.length}, inclusive. The value at
4003 * {@code original[from]} is placed into the initial element of the copy
4004 * (unless {@code from == original.length} or {@code from == to}).
4005 * Values from subsequent elements in the original array are placed into
4006 * subsequent elements in the copy. The final index of the range
4007 * ({@code to}), which must be greater than or equal to {@code from},
4008 * may be greater than {@code original.length}, in which case
4009 * {@code (byte)0} is placed in all elements of the copy whose index is
4010 * greater than or equal to {@code original.length - from}. The length
4011 * of the returned array will be {@code to - from}.
4012 *
4013 * @param original the array from which a range is to be copied
4014 * @param from the initial index of the range to be copied, inclusive
4015 * @param to the final index of the range to be copied, exclusive.
4016 * (This index may lie outside the array.)
4017 * @return a new array containing the specified range from the original array,
4018 * truncated or padded with zeros to obtain the required length
4019 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4020 * or {@code from > original.length}
4021 * @throws IllegalArgumentException if {@code from > to}
4022 * @throws NullPointerException if {@code original} is null
4023 * @since 1.6
4024 */
4025 public static byte[] copyOfRange(byte[] original, int from, int to) {
4026 int newLength = to - from;
4027 if (newLength < 0)
4028 throw new IllegalArgumentException(from + " > " + to);
4029 byte[] copy = new byte[newLength];
4030 System.arraycopy(original, from, copy, 0,
4031 Math.min(original.length - from, newLength));
4032 return copy;
4033 }
4034
4035 /**
4036 * Copies the specified range of the specified array into a new array.
4037 * The initial index of the range ({@code from}) must lie between zero
4038 * and {@code original.length}, inclusive. The value at
4039 * {@code original[from]} is placed into the initial element of the copy
4040 * (unless {@code from == original.length} or {@code from == to}).
4041 * Values from subsequent elements in the original array are placed into
4042 * subsequent elements in the copy. The final index of the range
4043 * ({@code to}), which must be greater than or equal to {@code from},
4044 * may be greater than {@code original.length}, in which case
4045 * {@code (short)0} is placed in all elements of the copy whose index is
4046 * greater than or equal to {@code original.length - from}. The length
4047 * of the returned array will be {@code to - from}.
4048 *
4049 * @param original the array from which a range is to be copied
4050 * @param from the initial index of the range to be copied, inclusive
4051 * @param to the final index of the range to be copied, exclusive.
4052 * (This index may lie outside the array.)
4053 * @return a new array containing the specified range from the original array,
4054 * truncated or padded with zeros to obtain the required length
4055 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4056 * or {@code from > original.length}
4057 * @throws IllegalArgumentException if {@code from > to}
4058 * @throws NullPointerException if {@code original} is null
4059 * @since 1.6
4060 */
4061 public static short[] copyOfRange(short[] original, int from, int to) {
4062 int newLength = to - from;
4063 if (newLength < 0)
4064 throw new IllegalArgumentException(from + " > " + to);
4065 short[] copy = new short[newLength];
4066 System.arraycopy(original, from, copy, 0,
4067 Math.min(original.length - from, newLength));
4068 return copy;
4069 }
4070
4071 /**
4072 * Copies the specified range of the specified array into a new array.
4073 * The initial index of the range ({@code from}) must lie between zero
4074 * and {@code original.length}, inclusive. The value at
4075 * {@code original[from]} is placed into the initial element of the copy
4076 * (unless {@code from == original.length} or {@code from == to}).
4077 * Values from subsequent elements in the original array are placed into
4078 * subsequent elements in the copy. The final index of the range
4079 * ({@code to}), which must be greater than or equal to {@code from},
4080 * may be greater than {@code original.length}, in which case
4081 * {@code 0} is placed in all elements of the copy whose index is
4082 * greater than or equal to {@code original.length - from}. The length
4083 * of the returned array will be {@code to - from}.
4084 *
4085 * @param original the array from which a range is to be copied
4086 * @param from the initial index of the range to be copied, inclusive
4087 * @param to the final index of the range to be copied, exclusive.
4088 * (This index may lie outside the array.)
4089 * @return a new array containing the specified range from the original array,
4090 * truncated or padded with zeros to obtain the required length
4091 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4092 * or {@code from > original.length}
4093 * @throws IllegalArgumentException if {@code from > to}
4094 * @throws NullPointerException if {@code original} is null
4095 * @since 1.6
4096 */
4097 public static int[] copyOfRange(int[] original, int from, int to) {
4098 int newLength = to - from;
4099 if (newLength < 0)
4100 throw new IllegalArgumentException(from + " > " + to);
4101 int[] copy = new int[newLength];
4102 System.arraycopy(original, from, copy, 0,
4103 Math.min(original.length - from, newLength));
4104 return copy;
4105 }
4106
4107 /**
4108 * Copies the specified range of the specified array into a new array.
4109 * The initial index of the range ({@code from}) must lie between zero
4110 * and {@code original.length}, inclusive. The value at
4111 * {@code original[from]} is placed into the initial element of the copy
4112 * (unless {@code from == original.length} or {@code from == to}).
4113 * Values from subsequent elements in the original array are placed into
4114 * subsequent elements in the copy. The final index of the range
4115 * ({@code to}), which must be greater than or equal to {@code from},
4116 * may be greater than {@code original.length}, in which case
4117 * {@code 0L} is placed in all elements of the copy whose index is
4118 * greater than or equal to {@code original.length - from}. The length
4119 * of the returned array will be {@code to - from}.
4120 *
4121 * @param original the array from which a range is to be copied
4122 * @param from the initial index of the range to be copied, inclusive
4123 * @param to the final index of the range to be copied, exclusive.
4124 * (This index may lie outside the array.)
4125 * @return a new array containing the specified range from the original array,
4126 * truncated or padded with zeros to obtain the required length
4127 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4128 * or {@code from > original.length}
4129 * @throws IllegalArgumentException if {@code from > to}
4130 * @throws NullPointerException if {@code original} is null
4131 * @since 1.6
4132 */
4133 public static long[] copyOfRange(long[] original, int from, int to) {
4134 int newLength = to - from;
4135 if (newLength < 0)
4136 throw new IllegalArgumentException(from + " > " + to);
4137 long[] copy = new long[newLength];
4138 System.arraycopy(original, from, copy, 0,
4139 Math.min(original.length - from, newLength));
4140 return copy;
4141 }
4142
4143 /**
4144 * Copies the specified range of the specified array into a new array.
4145 * The initial index of the range ({@code from}) must lie between zero
4146 * and {@code original.length}, inclusive. The value at
4147 * {@code original[from]} is placed into the initial element of the copy
4148 * (unless {@code from == original.length} or {@code from == to}).
4149 * Values from subsequent elements in the original array are placed into
4150 * subsequent elements in the copy. The final index of the range
4151 * ({@code to}), which must be greater than or equal to {@code from},
4152 * may be greater than {@code original.length}, in which case
4153 * {@code '\\u000'} is placed in all elements of the copy whose index is
4154 * greater than or equal to {@code original.length - from}. The length
4155 * of the returned array will be {@code to - from}.
4156 *
4157 * @param original the array from which a range is to be copied
4158 * @param from the initial index of the range to be copied, inclusive
4159 * @param to the final index of the range to be copied, exclusive.
4160 * (This index may lie outside the array.)
4161 * @return a new array containing the specified range from the original array,
4162 * truncated or padded with null characters to obtain the required length
4163 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4164 * or {@code from > original.length}
4165 * @throws IllegalArgumentException if {@code from > to}
4166 * @throws NullPointerException if {@code original} is null
4167 * @since 1.6
4168 */
4169 public static char[] copyOfRange(char[] original, int from, int to) {
4170 int newLength = to - from;
4171 if (newLength < 0)
4172 throw new IllegalArgumentException(from + " > " + to);
4173 char[] copy = new char[newLength];
4174 System.arraycopy(original, from, copy, 0,
4175 Math.min(original.length - from, newLength));
4176 return copy;
4177 }
4178
4179 /**
4180 * Copies the specified range of the specified array into a new array.
4181 * The initial index of the range ({@code from}) must lie between zero
4182 * and {@code original.length}, inclusive. The value at
4183 * {@code original[from]} is placed into the initial element of the copy
4184 * (unless {@code from == original.length} or {@code from == to}).
4185 * Values from subsequent elements in the original array are placed into
4186 * subsequent elements in the copy. The final index of the range
4187 * ({@code to}), which must be greater than or equal to {@code from},
4188 * may be greater than {@code original.length}, in which case
4189 * {@code 0f} is placed in all elements of the copy whose index is
4190 * greater than or equal to {@code original.length - from}. The length
4191 * of the returned array will be {@code to - from}.
4192 *
4193 * @param original the array from which a range is to be copied
4194 * @param from the initial index of the range to be copied, inclusive
4195 * @param to the final index of the range to be copied, exclusive.
4196 * (This index may lie outside the array.)
4197 * @return a new array containing the specified range from the original array,
4198 * truncated or padded with zeros to obtain the required length
4199 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4200 * or {@code from > original.length}
4201 * @throws IllegalArgumentException if {@code from > to}
4202 * @throws NullPointerException if {@code original} is null
4203 * @since 1.6
4204 */
4205 public static float[] copyOfRange(float[] original, int from, int to) {
4206 int newLength = to - from;
4207 if (newLength < 0)
4208 throw new IllegalArgumentException(from + " > " + to);
4209 float[] copy = new float[newLength];
4210 System.arraycopy(original, from, copy, 0,
4211 Math.min(original.length - from, newLength));
4212 return copy;
4213 }
4214
4215 /**
4216 * Copies the specified range of the specified array into a new array.
4217 * The initial index of the range ({@code from}) must lie between zero
4218 * and {@code original.length}, inclusive. The value at
4219 * {@code original[from]} is placed into the initial element of the copy
4220 * (unless {@code from == original.length} or {@code from == to}).
4221 * Values from subsequent elements in the original array are placed into
4222 * subsequent elements in the copy. The final index of the range
4223 * ({@code to}), which must be greater than or equal to {@code from},
4224 * may be greater than {@code original.length}, in which case
4225 * {@code 0d} is placed in all elements of the copy whose index is
4226 * greater than or equal to {@code original.length - from}. The length
4227 * of the returned array will be {@code to - from}.
4228 *
4229 * @param original the array from which a range is to be copied
4230 * @param from the initial index of the range to be copied, inclusive
4231 * @param to the final index of the range to be copied, exclusive.
4232 * (This index may lie outside the array.)
4233 * @return a new array containing the specified range from the original array,
4234 * truncated or padded with zeros to obtain the required length
4235 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4236 * or {@code from > original.length}
4237 * @throws IllegalArgumentException if {@code from > to}
4238 * @throws NullPointerException if {@code original} is null
4239 * @since 1.6
4240 */
4241 public static double[] copyOfRange(double[] original, int from, int to) {
4242 int newLength = to - from;
4243 if (newLength < 0)
4244 throw new IllegalArgumentException(from + " > " + to);
4245 double[] copy = new double[newLength];
4246 System.arraycopy(original, from, copy, 0,
4247 Math.min(original.length - from, newLength));
4248 return copy;
4249 }
4250
4251 /**
4252 * Copies the specified range of the specified array into a new array.
4253 * The initial index of the range ({@code from}) must lie between zero
4254 * and {@code original.length}, inclusive. The value at
4255 * {@code original[from]} is placed into the initial element of the copy
4256 * (unless {@code from == original.length} or {@code from == to}).
4257 * Values from subsequent elements in the original array are placed into
4258 * subsequent elements in the copy. The final index of the range
4259 * ({@code to}), which must be greater than or equal to {@code from},
4260 * may be greater than {@code original.length}, in which case
4261 * {@code false} is placed in all elements of the copy whose index is
4262 * greater than or equal to {@code original.length - from}. The length
4263 * of the returned array will be {@code to - from}.
4264 *
4265 * @param original the array from which a range is to be copied
4266 * @param from the initial index of the range to be copied, inclusive
4267 * @param to the final index of the range to be copied, exclusive.
4268 * (This index may lie outside the array.)
4269 * @return a new array containing the specified range from the original array,
4270 * truncated or padded with false elements to obtain the required length
4271 * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
4272 * or {@code from > original.length}
4273 * @throws IllegalArgumentException if {@code from > to}
4274 * @throws NullPointerException if {@code original} is null
4275 * @since 1.6
4276 */
4277 public static boolean[] copyOfRange(boolean[] original, int from, int to) {
4278 int newLength = to - from;
4279 if (newLength < 0)
4280 throw new IllegalArgumentException(from + " > " + to);
4281 boolean[] copy = new boolean[newLength];
4282 System.arraycopy(original, from, copy, 0,
4283 Math.min(original.length - from, newLength));
4284 return copy;
4285 }
4286
4287 // Misc
4288
4289 /**
4290 * Returns a fixed-size list backed by the specified array. (Changes to
4291 * the returned list "write through" to the array.) This method acts
4292 * as bridge between array-based and collection-based APIs, in
4293 * combination with {@link Collection#toArray}. The returned list is
4294 * serializable and implements {@link RandomAccess}.
4295 *
4296 * <p>This method also provides a convenient way to create a fixed-size
4297 * list initialized to contain several elements:
4298 * <pre>
4299 * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
4300 * </pre>
4301 *
4302 * @param <T> the class of the objects in the array
4303 * @param a the array by which the list will be backed
4304 * @return a list view of the specified array
4305 */
4306 @SafeVarargs
4307 @SuppressWarnings("varargs")
4308 public static <T> List<T> asList(T... a) {
4309 return new ArrayList<>(a);
4310 }
4311
4312 /**
4313 * @serial include
4314 */
4315 private static class ArrayList<E> extends AbstractList<E>
4316 implements RandomAccess, java.io.Serializable
4317 {
4318 private static final long serialVersionUID = -2764017481108945198L;
4319 private final E[] a;
4320
4321 ArrayList(E[] array) {
4322 a = Objects.requireNonNull(array);
4323 }
4324
4325 @Override
4326 public int size() {
4327 return a.length;
4328 }
4329
4330 @Override
4331 public Object[] toArray() {
4332 return Arrays.copyOf(a, a.length, Object[].class);
4333 }
4334
4335 @Override
4336 @SuppressWarnings("unchecked")
4337 public <T> T[] toArray(T[] a) {
4338 int size = size();
4339 if (a.length < size)
4340 return Arrays.copyOf(this.a, size,
4341 (Class<? extends T[]>) a.getClass());
4342 System.arraycopy(this.a, 0, a, 0, size);
4343 if (a.length > size)
4344 a[size] = null;
4345 return a;
4346 }
4347
4348 @Override
4349 public E get(int index) {
4350 return a[index];
4351 }
4352
4353 @Override
4354 public E set(int index, E element) {
4355 E oldValue = a[index];
4356 a[index] = element;
4357 return oldValue;
4358 }
4359
4360 @Override
4361 public int indexOf(Object o) {
4362 E[] a = this.a;
4363 if (o == null) {
4364 for (int i = 0; i < a.length; i++)
4365 if (a[i] == null)
4366 return i;
4367 } else {
4368 for (int i = 0; i < a.length; i++)
4369 if (o.equals(a[i]))
4370 return i;
4371 }
4372 return -1;
4373 }
4374
4375 @Override
4376 public boolean contains(Object o) {
4377 return indexOf(o) >= 0;
4378 }
4379
4380 @Override
4381 public Spliterator<E> spliterator() {
4382 return Spliterators.spliterator(a, Spliterator.ORDERED);
4383 }
4384
4385 @Override
4386 public void forEach(Consumer<? super E> action) {
4387 Objects.requireNonNull(action);
4388 for (E e : a) {
4389 action.accept(e);
4390 }
4391 }
4392
4393 @Override
4394 public void replaceAll(UnaryOperator<E> operator) {
4395 Objects.requireNonNull(operator);
4396 E[] a = this.a;
4397 for (int i = 0; i < a.length; i++) {
4398 a[i] = operator.apply(a[i]);
4399 }
4400 }
4401
4402 @Override
4403 public void sort(Comparator<? super E> c) {
4404 Arrays.sort(a, c);
4405 }
4406 }
4407
4408 /**
4409 * Returns a hash code based on the contents of the specified array.
4410 * For any two {@code long} arrays {@code a} and {@code b}
4411 * such that {@code Arrays.equals(a, b)}, it is also the case that
4412 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4413 *
4414 * <p>The value returned by this method is the same value that would be
4415 * obtained by invoking the {@link List#hashCode() hashCode}
4416 * method on a {@link List} containing a sequence of {@link Long}
4417 * instances representing the elements of {@code a} in the same order.
4418 * If {@code a} is {@code null}, this method returns 0.
4419 *
4420 * @param a the array whose hash value to compute
4421 * @return a content-based hash code for {@code a}
4422 * @since 1.5
4423 */
4424 public static int hashCode(long a[]) {
4425 if (a == null)
4426 return 0;
4427
4428 int result = 1;
4429 for (long element : a) {
4430 int elementHash = (int)(element ^ (element >>> 32));
4431 result = 31 * result + elementHash;
4432 }
4433
4434 return result;
4435 }
4436
4437 /**
4438 * Returns a hash code based on the contents of the specified array.
4439 * For any two non-null {@code int} arrays {@code a} and {@code b}
4440 * such that {@code Arrays.equals(a, b)}, it is also the case that
4441 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4442 *
4443 * <p>The value returned by this method is the same value that would be
4444 * obtained by invoking the {@link List#hashCode() hashCode}
4445 * method on a {@link List} containing a sequence of {@link Integer}
4446 * instances representing the elements of {@code a} in the same order.
4447 * If {@code a} is {@code null}, this method returns 0.
4448 *
4449 * @param a the array whose hash value to compute
4450 * @return a content-based hash code for {@code a}
4451 * @since 1.5
4452 */
4453 public static int hashCode(int a[]) {
4454 if (a == null)
4455 return 0;
4456
4457 int result = 1;
4458 for (int element : a)
4459 result = 31 * result + element;
4460
4461 return result;
4462 }
4463
4464 /**
4465 * Returns a hash code based on the contents of the specified array.
4466 * For any two {@code short} arrays {@code a} and {@code b}
4467 * such that {@code Arrays.equals(a, b)}, it is also the case that
4468 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4469 *
4470 * <p>The value returned by this method is the same value that would be
4471 * obtained by invoking the {@link List#hashCode() hashCode}
4472 * method on a {@link List} containing a sequence of {@link Short}
4473 * instances representing the elements of {@code a} in the same order.
4474 * If {@code a} is {@code null}, this method returns 0.
4475 *
4476 * @param a the array whose hash value to compute
4477 * @return a content-based hash code for {@code a}
4478 * @since 1.5
4479 */
4480 public static int hashCode(short a[]) {
4481 if (a == null)
4482 return 0;
4483
4484 int result = 1;
4485 for (short element : a)
4486 result = 31 * result + element;
4487
4488 return result;
4489 }
4490
4491 /**
4492 * Returns a hash code based on the contents of the specified array.
4493 * For any two {@code char} arrays {@code a} and {@code b}
4494 * such that {@code Arrays.equals(a, b)}, it is also the case that
4495 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4496 *
4497 * <p>The value returned by this method is the same value that would be
4498 * obtained by invoking the {@link List#hashCode() hashCode}
4499 * method on a {@link List} containing a sequence of {@link Character}
4500 * instances representing the elements of {@code a} in the same order.
4501 * If {@code a} is {@code null}, this method returns 0.
4502 *
4503 * @param a the array whose hash value to compute
4504 * @return a content-based hash code for {@code a}
4505 * @since 1.5
4506 */
4507 public static int hashCode(char a[]) {
4508 if (a == null)
4509 return 0;
4510
4511 int result = 1;
4512 for (char element : a)
4513 result = 31 * result + element;
4514
4515 return result;
4516 }
4517
4518 /**
4519 * Returns a hash code based on the contents of the specified array.
4520 * For any two {@code byte} arrays {@code a} and {@code b}
4521 * such that {@code Arrays.equals(a, b)}, it is also the case that
4522 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4523 *
4524 * <p>The value returned by this method is the same value that would be
4525 * obtained by invoking the {@link List#hashCode() hashCode}
4526 * method on a {@link List} containing a sequence of {@link Byte}
4527 * instances representing the elements of {@code a} in the same order.
4528 * If {@code a} is {@code null}, this method returns 0.
4529 *
4530 * @param a the array whose hash value to compute
4531 * @return a content-based hash code for {@code a}
4532 * @since 1.5
4533 */
4534 public static int hashCode(byte a[]) {
4535 if (a == null)
4536 return 0;
4537
4538 int result = 1;
4539 for (byte element : a)
4540 result = 31 * result + element;
4541
4542 return result;
4543 }
4544
4545 /**
4546 * Returns a hash code based on the contents of the specified array.
4547 * For any two {@code boolean} arrays {@code a} and {@code b}
4548 * such that {@code Arrays.equals(a, b)}, it is also the case that
4549 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4550 *
4551 * <p>The value returned by this method is the same value that would be
4552 * obtained by invoking the {@link List#hashCode() hashCode}
4553 * method on a {@link List} containing a sequence of {@link Boolean}
4554 * instances representing the elements of {@code a} in the same order.
4555 * If {@code a} is {@code null}, this method returns 0.
4556 *
4557 * @param a the array whose hash value to compute
4558 * @return a content-based hash code for {@code a}
4559 * @since 1.5
4560 */
4561 public static int hashCode(boolean a[]) {
4562 if (a == null)
4563 return 0;
4564
4565 int result = 1;
4566 for (boolean element : a)
4567 result = 31 * result + (element ? 1231 : 1237);
4568
4569 return result;
4570 }
4571
4572 /**
4573 * Returns a hash code based on the contents of the specified array.
4574 * For any two {@code float} arrays {@code a} and {@code b}
4575 * such that {@code Arrays.equals(a, b)}, it is also the case that
4576 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4577 *
4578 * <p>The value returned by this method is the same value that would be
4579 * obtained by invoking the {@link List#hashCode() hashCode}
4580 * method on a {@link List} containing a sequence of {@link Float}
4581 * instances representing the elements of {@code a} in the same order.
4582 * If {@code a} is {@code null}, this method returns 0.
4583 *
4584 * @param a the array whose hash value to compute
4585 * @return a content-based hash code for {@code a}
4586 * @since 1.5
4587 */
4588 public static int hashCode(float a[]) {
4589 if (a == null)
4590 return 0;
4591
4592 int result = 1;
4593 for (float element : a)
4594 result = 31 * result + Float.floatToIntBits(element);
4595
4596 return result;
4597 }
4598
4599 /**
4600 * Returns a hash code based on the contents of the specified array.
4601 * For any two {@code double} arrays {@code a} and {@code b}
4602 * such that {@code Arrays.equals(a, b)}, it is also the case that
4603 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4604 *
4605 * <p>The value returned by this method is the same value that would be
4606 * obtained by invoking the {@link List#hashCode() hashCode}
4607 * method on a {@link List} containing a sequence of {@link Double}
4608 * instances representing the elements of {@code a} in the same order.
4609 * If {@code a} is {@code null}, this method returns 0.
4610 *
4611 * @param a the array whose hash value to compute
4612 * @return a content-based hash code for {@code a}
4613 * @since 1.5
4614 */
4615 public static int hashCode(double a[]) {
4616 if (a == null)
4617 return 0;
4618
4619 int result = 1;
4620 for (double element : a) {
4621 long bits = Double.doubleToLongBits(element);
4622 result = 31 * result + (int)(bits ^ (bits >>> 32));
4623 }
4624 return result;
4625 }
4626
4627 /**
4628 * Returns a hash code based on the contents of the specified array. If
4629 * the array contains other arrays as elements, the hash code is based on
4630 * their identities rather than their contents. It is therefore
4631 * acceptable to invoke this method on an array that contains itself as an
4632 * element, either directly or indirectly through one or more levels of
4633 * arrays.
4634 *
4635 * <p>For any two arrays {@code a} and {@code b} such that
4636 * {@code Arrays.equals(a, b)}, it is also the case that
4637 * {@code Arrays.hashCode(a) == Arrays.hashCode(b)}.
4638 *
4639 * <p>The value returned by this method is equal to the value that would
4640 * be returned by {@code Arrays.asList(a).hashCode()}, unless {@code a}
4641 * is {@code null}, in which case {@code 0} is returned.
4642 *
4643 * @param a the array whose content-based hash code to compute
4644 * @return a content-based hash code for {@code a}
4645 * @see #deepHashCode(Object[])
4646 * @since 1.5
4647 */
4648 public static int hashCode(Object a[]) {
4649 if (a == null)
4650 return 0;
4651
4652 int result = 1;
4653
4654 for (Object element : a)
4655 result = 31 * result + (element == null ? 0 : element.hashCode());
4656
4657 return result;
4658 }
4659
4660 /**
4661 * Returns a hash code based on the "deep contents" of the specified
4662 * array. If the array contains other arrays as elements, the
4663 * hash code is based on their contents and so on, ad infinitum.
4664 * It is therefore unacceptable to invoke this method on an array that
4665 * contains itself as an element, either directly or indirectly through
4666 * one or more levels of arrays. The behavior of such an invocation is
4667 * undefined.
4668 *
4669 * <p>For any two arrays {@code a} and {@code b} such that
4670 * {@code Arrays.deepEquals(a, b)}, it is also the case that
4671 * {@code Arrays.deepHashCode(a) == Arrays.deepHashCode(b)}.
4672 *
4673 * <p>The computation of the value returned by this method is similar to
4674 * that of the value returned by {@link List#hashCode()} on a list
4675 * containing the same elements as {@code a} in the same order, with one
4676 * difference: If an element {@code e} of {@code a} is itself an array,
4677 * its hash code is computed not by calling {@code e.hashCode()}, but as
4678 * by calling the appropriate overloading of {@code Arrays.hashCode(e)}
4679 * if {@code e} is an array of a primitive type, or as by calling
4680 * {@code Arrays.deepHashCode(e)} recursively if {@code e} is an array
4681 * of a reference type. If {@code a} is {@code null}, this method
4682 * returns 0.
4683 *
4684 * @param a the array whose deep-content-based hash code to compute
4685 * @return a deep-content-based hash code for {@code a}
4686 * @see #hashCode(Object[])
4687 * @since 1.5
4688 */
4689 public static int deepHashCode(Object a[]) {
4690 if (a == null)
4691 return 0;
4692
4693 int result = 1;
4694
4695 for (Object element : a) {
4696 int elementHash = 0;
4697 if (element instanceof Object[])
4698 elementHash = deepHashCode((Object[]) element);
4699 else if (element instanceof byte[])
4700 elementHash = hashCode((byte[]) element);
4701 else if (element instanceof short[])
4702 elementHash = hashCode((short[]) element);
4703 else if (element instanceof int[])
4704 elementHash = hashCode((int[]) element);
4705 else if (element instanceof long[])
4706 elementHash = hashCode((long[]) element);
4707 else if (element instanceof char[])
4708 elementHash = hashCode((char[]) element);
4709 else if (element instanceof float[])
4710 elementHash = hashCode((float[]) element);
4711 else if (element instanceof double[])
4712 elementHash = hashCode((double[]) element);
4713 else if (element instanceof boolean[])
4714 elementHash = hashCode((boolean[]) element);
4715 else if (element != null)
4716 elementHash = element.hashCode();
4717
4718 result = 31 * result + elementHash;
4719 }
4720
4721 return result;
4722 }
4723
4724 /**
4725 * Returns {@code true} if the two specified arrays are <i>deeply
4726 * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])}
4727 * method, this method is appropriate for use with nested arrays of
4728 * arbitrary depth.
4729 *
4730 * <p>Two array references are considered deeply equal if both
4731 * are {@code null}, or if they refer to arrays that contain the same
4732 * number of elements and all corresponding pairs of elements in the two
4733 * arrays are deeply equal.
4734 *
4735 * <p>Two possibly {@code null} elements {@code e1} and {@code e2} are
4736 * deeply equal if any of the following conditions hold:
4737 * <ul>
4738 * <li> {@code e1} and {@code e2} are both arrays of object reference
4739 * types, and {@code Arrays.deepEquals(e1, e2) would return true}
4740 * <li> {@code e1} and {@code e2} are arrays of the same primitive
4741 * type, and the appropriate overloading of
4742 * {@code Arrays.equals(e1, e2)} would return true.
4743 * <li> {@code e1 == e2}
4744 * <li> {@code e1.equals(e2)} would return true.
4745 * </ul>
4746 * Note that this definition permits {@code null} elements at any depth.
4747 *
4748 * <p>If either of the specified arrays contain themselves as elements
4749 * either directly or indirectly through one or more levels of arrays,
4750 * the behavior of this method is undefined.
4751 *
4752 * @param a1 one array to be tested for equality
4753 * @param a2 the other array to be tested for equality
4754 * @return {@code true} if the two arrays are equal
4755 * @see #equals(Object[],Object[])
4756 * @see Objects#deepEquals(Object, Object)
4757 * @since 1.5
4758 */
4759 public static boolean deepEquals(Object[] a1, Object[] a2) {
4760 if (a1 == a2)
4761 return true;
4762 if (a1 == null || a2==null)
4763 return false;
4764 int length = a1.length;
4765 if (a2.length != length)
4766 return false;
4767
4768 for (int i = 0; i < length; i++) {
4769 Object e1 = a1[i];
4770 Object e2 = a2[i];
4771
4772 if (e1 == e2)
4773 continue;
4774 if (e1 == null)
4775 return false;
4776
4777 // Figure out whether the two elements are equal
4778 boolean eq = deepEquals0(e1, e2);
4779
4780 if (!eq)
4781 return false;
4782 }
4783 return true;
4784 }
4785
4786 static boolean deepEquals0(Object e1, Object e2) {
4787 assert e1 != null;
4788 boolean eq;
4789 if (e1 instanceof Object[] && e2 instanceof Object[])
4790 eq = deepEquals ((Object[]) e1, (Object[]) e2);
4791 else if (e1 instanceof byte[] && e2 instanceof byte[])
4792 eq = equals((byte[]) e1, (byte[]) e2);
4793 else if (e1 instanceof short[] && e2 instanceof short[])
4794 eq = equals((short[]) e1, (short[]) e2);
4795 else if (e1 instanceof int[] && e2 instanceof int[])
4796 eq = equals((int[]) e1, (int[]) e2);
4797 else if (e1 instanceof long[] && e2 instanceof long[])
4798 eq = equals((long[]) e1, (long[]) e2);
4799 else if (e1 instanceof char[] && e2 instanceof char[])
4800 eq = equals((char[]) e1, (char[]) e2);
4801 else if (e1 instanceof float[] && e2 instanceof float[])
4802 eq = equals((float[]) e1, (float[]) e2);
4803 else if (e1 instanceof double[] && e2 instanceof double[])
4804 eq = equals((double[]) e1, (double[]) e2);
4805 else if (e1 instanceof boolean[] && e2 instanceof boolean[])
4806 eq = equals((boolean[]) e1, (boolean[]) e2);
4807 else
4808 eq = e1.equals(e2);
4809 return eq;
4810 }
4811
4812 /**
4813 * Returns a string representation of the contents of the specified array.
4814 * The string representation consists of a list of the array's elements,
4815 * enclosed in square brackets ({@code "[]"}). Adjacent elements are
4816 * separated by the characters {@code ", "} (a comma followed by a
4817 * space). Elements are converted to strings as by
4818 * {@code String.valueOf(long)}. Returns {@code "null"} if {@code a}
4819 * is {@code null}.
4820 *
4821 * @param a the array whose string representation to return
4822 * @return a string representation of {@code a}
4823 * @since 1.5
4824 */
4825 public static String toString(long[] a) {
4826 if (a == null)
4827 return "null";
4828 int iMax = a.length - 1;
4829 if (iMax == -1)
4830 return "[]";
4831
4832 StringBuilder b = new StringBuilder();
4833 b.append('[');
4834 for (int i = 0; ; i++) {
4835 b.append(a[i]);
4836 if (i == iMax)
4837 return b.append(']').toString();
4838 b.append(", ");
4839 }
4840 }
4841
4842 /**
4843 * Returns a string representation of the contents of the specified array.
4844 * The string representation consists of a list of the array's elements,
4845 * enclosed in square brackets ({@code "[]"}). Adjacent elements are
4846 * separated by the characters {@code ", "} (a comma followed by a
4847 * space). Elements are converted to strings as by
4848 * {@code String.valueOf(int)}. Returns {@code "null"} if {@code a} is
4849 * {@code null}.
4850 *
4851 * @param a the array whose string representation to return
4852 * @return a string representation of {@code a}
4853 * @since 1.5
4854 */
4855 public static String toString(int[] a) {
4856 if (a == null)
4857 return "null";
4858 int iMax = a.length - 1;
4859 if (iMax == -1)
4860 return "[]";
4861
4862 StringBuilder b = new StringBuilder();
4863 b.append('[');
4864 for (int i = 0; ; i++) {
4865 b.append(a[i]);
4866 if (i == iMax)
4867 return b.append(']').toString();
4868 b.append(", ");
4869 }
4870 }
4871
4872 /**
4873 * Returns a string representation of the contents of the specified array.
4874 * The string representation consists of a list of the array's elements,
4875 * enclosed in square brackets ({@code "[]"}). Adjacent elements are
4876 * separated by the characters {@code ", "} (a comma followed by a
4877 * space). Elements are converted to strings as by
4878 * {@code String.valueOf(short)}. Returns {@code "null"} if {@code a}
4879 * is {@code null}.
4880 *
4881 * @param a the array whose string representation to return
4882 * @return a string representation of {@code a}
4883 * @since 1.5
4884 */
4885 public static String toString(short[] a) {
4886 if (a == null)
4887 return "null";
4888 int iMax = a.length - 1;
4889 if (iMax == -1)
4890 return "[]";
4891
4892 StringBuilder b = new StringBuilder();
4893 b.append('[');
4894 for (int i = 0; ; i++) {
4895 b.append(a[i]);
4896 if (i == iMax)
4897 return b.append(']').toString();
4898 b.append(", ");
4899 }
4900 }
4901
4902 /**
4903 * Returns a string representation of the contents of the specified array.
4904 * The string representation consists of a list of the array's elements,
4905 * enclosed in square brackets ({@code "[]"}). Adjacent elements are
4906 * separated by the characters {@code ", "} (a comma followed by a
4907 * space). Elements are converted to strings as by
4908 * {@code String.valueOf(char)}. Returns {@code "null"} if {@code a}
4909 * is {@code null}.
4910 *
4911 * @param a the array whose string representation to return
4912 * @return a string representation of {@code a}
4913 * @since 1.5
4914 */
4915 public static String toString(char[] a) {
4916 if (a == null)
4917 return "null";
4918 int iMax = a.length - 1;
4919 if (iMax == -1)
4920 return "[]";
4921
4922 StringBuilder b = new StringBuilder();
4923 b.append('[');
4924 for (int i = 0; ; i++) {
4925 b.append(a[i]);
4926 if (i == iMax)
4927 return b.append(']').toString();
4928 b.append(", ");
4929 }
4930 }
4931
4932 /**
4933 * Returns a string representation of the contents of the specified array.
4934 * The string representation consists of a list of the array's elements,
4935 * enclosed in square brackets ({@code "[]"}). Adjacent elements
4936 * are separated by the characters {@code ", "} (a comma followed
4937 * by a space). Elements are converted to strings as by
4938 * {@code String.valueOf(byte)}. Returns {@code "null"} if
4939 * {@code a} is {@code null}.
4940 *
4941 * @param a the array whose string representation to return
4942 * @return a string representation of {@code a}
4943 * @since 1.5
4944 */
4945 public static String toString(byte[] a) {
4946 if (a == null)
4947 return "null";
4948 int iMax = a.length - 1;
4949 if (iMax == -1)
4950 return "[]";
4951
4952 StringBuilder b = new StringBuilder();
4953 b.append('[');
4954 for (int i = 0; ; i++) {
4955 b.append(a[i]);
4956 if (i == iMax)
4957 return b.append(']').toString();
4958 b.append(", ");
4959 }
4960 }
4961
4962 /**
4963 * Returns a string representation of the contents of the specified array.
4964 * The string representation consists of a list of the array's elements,
4965 * enclosed in square brackets ({@code "[]"}). Adjacent elements are
4966 * separated by the characters {@code ", "} (a comma followed by a
4967 * space). Elements are converted to strings as by
4968 * {@code String.valueOf(boolean)}. Returns {@code "null"} if
4969 * {@code a} is {@code null}.
4970 *
4971 * @param a the array whose string representation to return
4972 * @return a string representation of {@code a}
4973 * @since 1.5
4974 */
4975 public static String toString(boolean[] a) {
4976 if (a == null)
4977 return "null";
4978 int iMax = a.length - 1;
4979 if (iMax == -1)
4980 return "[]";
4981
4982 StringBuilder b = new StringBuilder();
4983 b.append('[');
4984 for (int i = 0; ; i++) {
4985 b.append(a[i]);
4986 if (i == iMax)
4987 return b.append(']').toString();
4988 b.append(", ");
4989 }
4990 }
4991
4992 /**
4993 * Returns a string representation of the contents of the specified array.
4994 * The string representation consists of a list of the array's elements,
4995 * enclosed in square brackets ({@code "[]"}). Adjacent elements are
4996 * separated by the characters {@code ", "} (a comma followed by a
4997 * space). Elements are converted to strings as by
4998 * {@code String.valueOf(float)}. Returns {@code "null"} if {@code a}
4999 * is {@code null}.
5000 *
5001 * @param a the array whose string representation to return
5002 * @return a string representation of {@code a}
5003 * @since 1.5
5004 */
5005 public static String toString(float[] a) {
5006 if (a == null)
5007 return "null";
5008
5009 int iMax = a.length - 1;
5010 if (iMax == -1)
5011 return "[]";
5012
5013 StringBuilder b = new StringBuilder();
5014 b.append('[');
5015 for (int i = 0; ; i++) {
5016 b.append(a[i]);
5017 if (i == iMax)
5018 return b.append(']').toString();
5019 b.append(", ");
5020 }
5021 }
5022
5023 /**
5024 * Returns a string representation of the contents of the specified array.
5025 * The string representation consists of a list of the array's elements,
5026 * enclosed in square brackets ({@code "[]"}). Adjacent elements are
5027 * separated by the characters {@code ", "} (a comma followed by a
5028 * space). Elements are converted to strings as by
5029 * {@code String.valueOf(double)}. Returns {@code "null"} if {@code a}
5030 * is {@code null}.
5031 *
5032 * @param a the array whose string representation to return
5033 * @return a string representation of {@code a}
5034 * @since 1.5
5035 */
5036 public static String toString(double[] a) {
5037 if (a == null)
5038 return "null";
5039 int iMax = a.length - 1;
5040 if (iMax == -1)
5041 return "[]";
5042
5043 StringBuilder b = new StringBuilder();
5044 b.append('[');
5045 for (int i = 0; ; i++) {
5046 b.append(a[i]);
5047 if (i == iMax)
5048 return b.append(']').toString();
5049 b.append(", ");
5050 }
5051 }
5052
5053 /**
5054 * Returns a string representation of the contents of the specified array.
5055 * If the array contains other arrays as elements, they are converted to
5056 * strings by the {@link Object#toString} method inherited from
5057 * {@code Object}, which describes their <i>identities</i> rather than
5058 * their contents.
5059 *
5060 * <p>The value returned by this method is equal to the value that would
5061 * be returned by {@code Arrays.asList(a).toString()}, unless {@code a}
5062 * is {@code null}, in which case {@code "null"} is returned.
5063 *
5064 * @param a the array whose string representation to return
5065 * @return a string representation of {@code a}
5066 * @see #deepToString(Object[])
5067 * @since 1.5
5068 */
5069 public static String toString(Object[] a) {
5070 if (a == null)
5071 return "null";
5072
5073 int iMax = a.length - 1;
5074 if (iMax == -1)
5075 return "[]";
5076
5077 StringBuilder b = new StringBuilder();
5078 b.append('[');
5079 for (int i = 0; ; i++) {
5080 b.append(String.valueOf(a[i]));
5081 if (i == iMax)
5082 return b.append(']').toString();
5083 b.append(", ");
5084 }
5085 }
5086
5087 /**
5088 * Returns a string representation of the "deep contents" of the specified
5089 * array. If the array contains other arrays as elements, the string
5090 * representation contains their contents and so on. This method is
5091 * designed for converting multidimensional arrays to strings.
5092 *
5093 * <p>The string representation consists of a list of the array's
5094 * elements, enclosed in square brackets ({@code "[]"}). Adjacent
5095 * elements are separated by the characters {@code ", "} (a comma
5096 * followed by a space). Elements are converted to strings as by
5097 * {@code String.valueOf(Object)}, unless they are themselves
5098 * arrays.
5099 *
5100 * <p>If an element {@code e} is an array of a primitive type, it is
5101 * converted to a string as by invoking the appropriate overloading of
5102 * {@code Arrays.toString(e)}. If an element {@code e} is an array of a
5103 * reference type, it is converted to a string as by invoking
5104 * this method recursively.
5105 *
5106 * <p>To avoid infinite recursion, if the specified array contains itself
5107 * as an element, or contains an indirect reference to itself through one
5108 * or more levels of arrays, the self-reference is converted to the string
5109 * {@code "[...]"}. For example, an array containing only a reference
5110 * to itself would be rendered as {@code "[[...]]"}.
5111 *
5112 * <p>This method returns {@code "null"} if the specified array
5113 * is {@code null}.
5114 *
5115 * @param a the array whose string representation to return
5116 * @return a string representation of {@code a}
5117 * @see #toString(Object[])
5118 * @since 1.5
5119 */
5120 public static String deepToString(Object[] a) {
5121 if (a == null)
5122 return "null";
5123
5124 int bufLen = 20 * a.length;
5125 if (a.length != 0 && bufLen <= 0)
5126 bufLen = Integer.MAX_VALUE;
5127 StringBuilder buf = new StringBuilder(bufLen);
5128 deepToString(a, buf, new HashSet<>());
5129 return buf.toString();
5130 }
5131
5132 private static void deepToString(Object[] a, StringBuilder buf,
5133 Set<Object[]> dejaVu) {
5134 if (a == null) {
5135 buf.append("null");
5136 return;
5137 }
5138 int iMax = a.length - 1;
5139 if (iMax == -1) {
5140 buf.append("[]");
5141 return;
5142 }
5143
5144 dejaVu.add(a);
5145 buf.append('[');
5146 for (int i = 0; ; i++) {
5147
5148 Object element = a[i];
5149 if (element == null) {
5150 buf.append("null");
5151 } else {
5152 Class<?> eClass = element.getClass();
5153
5154 if (eClass.isArray()) {
5155 if (eClass == byte[].class)
5156 buf.append(toString((byte[]) element));
5157 else if (eClass == short[].class)
5158 buf.append(toString((short[]) element));
5159 else if (eClass == int[].class)
5160 buf.append(toString((int[]) element));
5161 else if (eClass == long[].class)
5162 buf.append(toString((long[]) element));
5163 else if (eClass == char[].class)
5164 buf.append(toString((char[]) element));
5165 else if (eClass == float[].class)
5166 buf.append(toString((float[]) element));
5167 else if (eClass == double[].class)
5168 buf.append(toString((double[]) element));
5169 else if (eClass == boolean[].class)
5170 buf.append(toString((boolean[]) element));
5171 else { // element is an array of object references
5172 if (dejaVu.contains(element))
5173 buf.append("[...]");
5174 else
5175 deepToString((Object[])element, buf, dejaVu);
5176 }
5177 } else { // element is non-null and not an array
5178 buf.append(element.toString());
5179 }
5180 }
5181 if (i == iMax)
5182 break;
5183 buf.append(", ");
5184 }
5185 buf.append(']');
5186 dejaVu.remove(a);
5187 }
5188
5189
5190 /**
5191 * Set all elements of the specified array, using the provided
5192 * generator function to compute each element.
5193 *
5194 * <p>If the generator function throws an exception, it is relayed to
5195 * the caller and the array is left in an indeterminate state.
5196 *
5197 * @apiNote
5198 * Setting a subrange of an array, using a generator function to compute
5199 * each element, can be written as follows:
5200 * <pre>{@code
5201 * IntStream.range(startInclusive, endExclusive)
5202 * .forEach(i -> array[i] = generator.apply(i));
5203 * }</pre>
5204 *
5205 * @param <T> type of elements of the array
5206 * @param array array to be initialized
5207 * @param generator a function accepting an index and producing the desired
5208 * value for that position
5209 * @throws NullPointerException if the generator is null
5210 * @since 1.8
5211 */
5212 public static <T> void setAll(T[] array, IntFunction<? extends T> generator) {
5213 Objects.requireNonNull(generator);
5214 for (int i = 0; i < array.length; i++)
5215 array[i] = generator.apply(i);
5216 }
5217
5218 /**
5219 * Set all elements of the specified array, in parallel, using the
5220 * provided generator function to compute each element.
5221 *
5222 * <p>If the generator function throws an exception, an unchecked exception
5223 * is thrown from {@code parallelSetAll} and the array is left in an
5224 * indeterminate state.
5225 *
5226 * @apiNote
5227 * Setting a subrange of an array, in parallel, using a generator function
5228 * to compute each element, can be written as follows:
5229 * <pre>{@code
5230 * IntStream.range(startInclusive, endExclusive)
5231 * .parallel()
5232 * .forEach(i -> array[i] = generator.apply(i));
5233 * }</pre>
5234 *
5235 * @param <T> type of elements of the array
5236 * @param array array to be initialized
5237 * @param generator a function accepting an index and producing the desired
5238 * value for that position
5239 * @throws NullPointerException if the generator is null
5240 * @since 1.8
5241 */
5242 public static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) {
5243 Objects.requireNonNull(generator);
5244 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.apply(i); });
5245 }
5246
5247 /**
5248 * Set all elements of the specified array, using the provided
5249 * generator function to compute each element.
5250 *
5251 * <p>If the generator function throws an exception, it is relayed to
5252 * the caller and the array is left in an indeterminate state.
5253 *
5254 * @apiNote
5255 * Setting a subrange of an array, using a generator function to compute
5256 * each element, can be written as follows:
5257 * <pre>{@code
5258 * IntStream.range(startInclusive, endExclusive)
5259 * .forEach(i -> array[i] = generator.applyAsInt(i));
5260 * }</pre>
5261 *
5262 * @param array array to be initialized
5263 * @param generator a function accepting an index and producing the desired
5264 * value for that position
5265 * @throws NullPointerException if the generator is null
5266 * @since 1.8
5267 */
5268 public static void setAll(int[] array, IntUnaryOperator generator) {
5269 Objects.requireNonNull(generator);
5270 for (int i = 0; i < array.length; i++)
5271 array[i] = generator.applyAsInt(i);
5272 }
5273
5274 /**
5275 * Set all elements of the specified array, in parallel, using the
5276 * provided generator function to compute each element.
5277 *
5278 * <p>If the generator function throws an exception, an unchecked exception
5279 * is thrown from {@code parallelSetAll} and the array is left in an
5280 * indeterminate state.
5281 *
5282 * @apiNote
5283 * Setting a subrange of an array, in parallel, using a generator function
5284 * to compute each element, can be written as follows:
5285 * <pre>{@code
5286 * IntStream.range(startInclusive, endExclusive)
5287 * .parallel()
5288 * .forEach(i -> array[i] = generator.applyAsInt(i));
5289 * }</pre>
5290 *
5291 * @param array array to be initialized
5292 * @param generator a function accepting an index and producing the desired
5293 * value for that position
5294 * @throws NullPointerException if the generator is null
5295 * @since 1.8
5296 */
5297 public static void parallelSetAll(int[] array, IntUnaryOperator generator) {
5298 Objects.requireNonNull(generator);
5299 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsInt(i); });
5300 }
5301
5302 /**
5303 * Set all elements of the specified array, using the provided
5304 * generator function to compute each element.
5305 *
5306 * <p>If the generator function throws an exception, it is relayed to
5307 * the caller and the array is left in an indeterminate state.
5308 *
5309 * @apiNote
5310 * Setting a subrange of an array, using a generator function to compute
5311 * each element, can be written as follows:
5312 * <pre>{@code
5313 * IntStream.range(startInclusive, endExclusive)
5314 * .forEach(i -> array[i] = generator.applyAsLong(i));
5315 * }</pre>
5316 *
5317 * @param array array to be initialized
5318 * @param generator a function accepting an index and producing the desired
5319 * value for that position
5320 * @throws NullPointerException if the generator is null
5321 * @since 1.8
5322 */
5323 public static void setAll(long[] array, IntToLongFunction generator) {
5324 Objects.requireNonNull(generator);
5325 for (int i = 0; i < array.length; i++)
5326 array[i] = generator.applyAsLong(i);
5327 }
5328
5329 /**
5330 * Set all elements of the specified array, in parallel, using the
5331 * provided generator function to compute each element.
5332 *
5333 * <p>If the generator function throws an exception, an unchecked exception
5334 * is thrown from {@code parallelSetAll} and the array is left in an
5335 * indeterminate state.
5336 *
5337 * @apiNote
5338 * Setting a subrange of an array, in parallel, using a generator function
5339 * to compute each element, can be written as follows:
5340 * <pre>{@code
5341 * IntStream.range(startInclusive, endExclusive)
5342 * .parallel()
5343 * .forEach(i -> array[i] = generator.applyAsLong(i));
5344 * }</pre>
5345 *
5346 * @param array array to be initialized
5347 * @param generator a function accepting an index and producing the desired
5348 * value for that position
5349 * @throws NullPointerException if the generator is null
5350 * @since 1.8
5351 */
5352 public static void parallelSetAll(long[] array, IntToLongFunction generator) {
5353 Objects.requireNonNull(generator);
5354 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsLong(i); });
5355 }
5356
5357 /**
5358 * Set all elements of the specified array, using the provided
5359 * generator function to compute each element.
5360 *
5361 * <p>If the generator function throws an exception, it is relayed to
5362 * the caller and the array is left in an indeterminate state.
5363 *
5364 * @apiNote
5365 * Setting a subrange of an array, using a generator function to compute
5366 * each element, can be written as follows:
5367 * <pre>{@code
5368 * IntStream.range(startInclusive, endExclusive)
5369 * .forEach(i -> array[i] = generator.applyAsDouble(i));
5370 * }</pre>
5371 *
5372 * @param array array to be initialized
5373 * @param generator a function accepting an index and producing the desired
5374 * value for that position
5375 * @throws NullPointerException if the generator is null
5376 * @since 1.8
5377 */
5378 public static void setAll(double[] array, IntToDoubleFunction generator) {
5379 Objects.requireNonNull(generator);
5380 for (int i = 0; i < array.length; i++)
5381 array[i] = generator.applyAsDouble(i);
5382 }
5383
5384 /**
5385 * Set all elements of the specified array, in parallel, using the
5386 * provided generator function to compute each element.
5387 *
5388 * <p>If the generator function throws an exception, an unchecked exception
5389 * is thrown from {@code parallelSetAll} and the array is left in an
5390 * indeterminate state.
5391 *
5392 * @apiNote
5393 * Setting a subrange of an array, in parallel, using a generator function
5394 * to compute each element, can be written as follows:
5395 * <pre>{@code
5396 * IntStream.range(startInclusive, endExclusive)
5397 * .parallel()
5398 * .forEach(i -> array[i] = generator.applyAsDouble(i));
5399 * }</pre>
5400 *
5401 * @param array array to be initialized
5402 * @param generator a function accepting an index and producing the desired
5403 * value for that position
5404 * @throws NullPointerException if the generator is null
5405 * @since 1.8
5406 */
5407 public static void parallelSetAll(double[] array, IntToDoubleFunction generator) {
5408 Objects.requireNonNull(generator);
5409 IntStream.range(0, array.length).parallel().forEach(i -> { array[i] = generator.applyAsDouble(i); });
5410 }
5411
5412 /**
5413 * Returns a {@link Spliterator} covering all of the specified array.
5414 *
5415 * <p>The spliterator reports {@link Spliterator#SIZED},
5416 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5417 * {@link Spliterator#IMMUTABLE}.
5418 *
5419 * @param <T> type of elements
5420 * @param array the array, assumed to be unmodified during use
5421 * @return a spliterator for the array elements
5422 * @since 1.8
5423 */
5424 public static <T> Spliterator<T> spliterator(T[] array) {
5425 return Spliterators.spliterator(array,
5426 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5427 }
5428
5429 /**
5430 * Returns a {@link Spliterator} covering the specified range of the
5431 * specified array.
5432 *
5433 * <p>The spliterator reports {@link Spliterator#SIZED},
5434 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5435 * {@link Spliterator#IMMUTABLE}.
5436 *
5437 * @param <T> type of elements
5438 * @param array the array, assumed to be unmodified during use
5439 * @param startInclusive the first index to cover, inclusive
5440 * @param endExclusive index immediately past the last index to cover
5441 * @return a spliterator for the array elements
5442 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5443 * negative, {@code endExclusive} is less than
5444 * {@code startInclusive}, or {@code endExclusive} is greater than
5445 * the array size
5446 * @since 1.8
5447 */
5448 public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) {
5449 return Spliterators.spliterator(array, startInclusive, endExclusive,
5450 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5451 }
5452
5453 /**
5454 * Returns a {@link Spliterator.OfInt} covering all of the specified array.
5455 *
5456 * <p>The spliterator reports {@link Spliterator#SIZED},
5457 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5458 * {@link Spliterator#IMMUTABLE}.
5459 *
5460 * @param array the array, assumed to be unmodified during use
5461 * @return a spliterator for the array elements
5462 * @since 1.8
5463 */
5464 public static Spliterator.OfInt spliterator(int[] array) {
5465 return Spliterators.spliterator(array,
5466 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5467 }
5468
5469 /**
5470 * Returns a {@link Spliterator.OfInt} covering the specified range of the
5471 * specified array.
5472 *
5473 * <p>The spliterator reports {@link Spliterator#SIZED},
5474 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5475 * {@link Spliterator#IMMUTABLE}.
5476 *
5477 * @param array the array, assumed to be unmodified during use
5478 * @param startInclusive the first index to cover, inclusive
5479 * @param endExclusive index immediately past the last index to cover
5480 * @return a spliterator for the array elements
5481 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5482 * negative, {@code endExclusive} is less than
5483 * {@code startInclusive}, or {@code endExclusive} is greater than
5484 * the array size
5485 * @since 1.8
5486 */
5487 public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) {
5488 return Spliterators.spliterator(array, startInclusive, endExclusive,
5489 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5490 }
5491
5492 /**
5493 * Returns a {@link Spliterator.OfLong} covering all of the specified array.
5494 *
5495 * <p>The spliterator reports {@link Spliterator#SIZED},
5496 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5497 * {@link Spliterator#IMMUTABLE}.
5498 *
5499 * @param array the array, assumed to be unmodified during use
5500 * @return the spliterator for the array elements
5501 * @since 1.8
5502 */
5503 public static Spliterator.OfLong spliterator(long[] array) {
5504 return Spliterators.spliterator(array,
5505 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5506 }
5507
5508 /**
5509 * Returns a {@link Spliterator.OfLong} covering the specified range of the
5510 * specified array.
5511 *
5512 * <p>The spliterator reports {@link Spliterator#SIZED},
5513 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5514 * {@link Spliterator#IMMUTABLE}.
5515 *
5516 * @param array the array, assumed to be unmodified during use
5517 * @param startInclusive the first index to cover, inclusive
5518 * @param endExclusive index immediately past the last index to cover
5519 * @return a spliterator for the array elements
5520 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5521 * negative, {@code endExclusive} is less than
5522 * {@code startInclusive}, or {@code endExclusive} is greater than
5523 * the array size
5524 * @since 1.8
5525 */
5526 public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) {
5527 return Spliterators.spliterator(array, startInclusive, endExclusive,
5528 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5529 }
5530
5531 /**
5532 * Returns a {@link Spliterator.OfDouble} covering all of the specified
5533 * array.
5534 *
5535 * <p>The spliterator reports {@link Spliterator#SIZED},
5536 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5537 * {@link Spliterator#IMMUTABLE}.
5538 *
5539 * @param array the array, assumed to be unmodified during use
5540 * @return a spliterator for the array elements
5541 * @since 1.8
5542 */
5543 public static Spliterator.OfDouble spliterator(double[] array) {
5544 return Spliterators.spliterator(array,
5545 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5546 }
5547
5548 /**
5549 * Returns a {@link Spliterator.OfDouble} covering the specified range of
5550 * the specified array.
5551 *
5552 * <p>The spliterator reports {@link Spliterator#SIZED},
5553 * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and
5554 * {@link Spliterator#IMMUTABLE}.
5555 *
5556 * @param array the array, assumed to be unmodified during use
5557 * @param startInclusive the first index to cover, inclusive
5558 * @param endExclusive index immediately past the last index to cover
5559 * @return a spliterator for the array elements
5560 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5561 * negative, {@code endExclusive} is less than
5562 * {@code startInclusive}, or {@code endExclusive} is greater than
5563 * the array size
5564 * @since 1.8
5565 */
5566 public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) {
5567 return Spliterators.spliterator(array, startInclusive, endExclusive,
5568 Spliterator.ORDERED | Spliterator.IMMUTABLE);
5569 }
5570
5571 /**
5572 * Returns a sequential {@link Stream} with the specified array as its
5573 * source.
5574 *
5575 * @param <T> The type of the array elements
5576 * @param array The array, assumed to be unmodified during use
5577 * @return a {@code Stream} for the array
5578 * @since 1.8
5579 */
5580 public static <T> Stream<T> stream(T[] array) {
5581 return stream(array, 0, array.length);
5582 }
5583
5584 /**
5585 * Returns a sequential {@link Stream} with the specified range of the
5586 * specified array as its source.
5587 *
5588 * @param <T> the type of the array elements
5589 * @param array the array, assumed to be unmodified during use
5590 * @param startInclusive the first index to cover, inclusive
5591 * @param endExclusive index immediately past the last index to cover
5592 * @return a {@code Stream} for the array range
5593 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5594 * negative, {@code endExclusive} is less than
5595 * {@code startInclusive}, or {@code endExclusive} is greater than
5596 * the array size
5597 * @since 1.8
5598 */
5599 public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) {
5600 return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false);
5601 }
5602
5603 /**
5604 * Returns a sequential {@link IntStream} with the specified array as its
5605 * source.
5606 *
5607 * @param array the array, assumed to be unmodified during use
5608 * @return an {@code IntStream} for the array
5609 * @since 1.8
5610 */
5611 public static IntStream stream(int[] array) {
5612 return stream(array, 0, array.length);
5613 }
5614
5615 /**
5616 * Returns a sequential {@link IntStream} with the specified range of the
5617 * specified array as its source.
5618 *
5619 * @param array the array, assumed to be unmodified during use
5620 * @param startInclusive the first index to cover, inclusive
5621 * @param endExclusive index immediately past the last index to cover
5622 * @return an {@code IntStream} for the array range
5623 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5624 * negative, {@code endExclusive} is less than
5625 * {@code startInclusive}, or {@code endExclusive} is greater than
5626 * the array size
5627 * @since 1.8
5628 */
5629 public static IntStream stream(int[] array, int startInclusive, int endExclusive) {
5630 return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false);
5631 }
5632
5633 /**
5634 * Returns a sequential {@link LongStream} with the specified array as its
5635 * source.
5636 *
5637 * @param array the array, assumed to be unmodified during use
5638 * @return a {@code LongStream} for the array
5639 * @since 1.8
5640 */
5641 public static LongStream stream(long[] array) {
5642 return stream(array, 0, array.length);
5643 }
5644
5645 /**
5646 * Returns a sequential {@link LongStream} with the specified range of the
5647 * specified array as its source.
5648 *
5649 * @param array the array, assumed to be unmodified during use
5650 * @param startInclusive the first index to cover, inclusive
5651 * @param endExclusive index immediately past the last index to cover
5652 * @return a {@code LongStream} for the array range
5653 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5654 * negative, {@code endExclusive} is less than
5655 * {@code startInclusive}, or {@code endExclusive} is greater than
5656 * the array size
5657 * @since 1.8
5658 */
5659 public static LongStream stream(long[] array, int startInclusive, int endExclusive) {
5660 return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false);
5661 }
5662
5663 /**
5664 * Returns a sequential {@link DoubleStream} with the specified array as its
5665 * source.
5666 *
5667 * @param array the array, assumed to be unmodified during use
5668 * @return a {@code DoubleStream} for the array
5669 * @since 1.8
5670 */
5671 public static DoubleStream stream(double[] array) {
5672 return stream(array, 0, array.length);
5673 }
5674
5675 /**
5676 * Returns a sequential {@link DoubleStream} with the specified range of the
5677 * specified array as its source.
5678 *
5679 * @param array the array, assumed to be unmodified during use
5680 * @param startInclusive the first index to cover, inclusive
5681 * @param endExclusive index immediately past the last index to cover
5682 * @return a {@code DoubleStream} for the array range
5683 * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is
5684 * negative, {@code endExclusive} is less than
5685 * {@code startInclusive}, or {@code endExclusive} is greater than
5686 * the array size
5687 * @since 1.8
5688 */
5689 public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) {
5690 return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false);
5691 }
5692
5693
5694 // Comparison methods
5695
5696 // Compare boolean
5697
5698 /**
5699 * Compares two {@code boolean} arrays lexicographically.
5700 *
5701 * <p>If the two arrays share a common prefix then the lexicographic
5702 * comparison is the result of comparing two elements, as if by
5703 * {@link Boolean#compare(boolean, boolean)}, at an index within the
5704 * respective arrays that is the prefix length.
5705 * Otherwise, one array is a proper prefix of the other and, lexicographic
5706 * comparison is the result of comparing the two array lengths.
5707 * (See {@link #mismatch(boolean[], boolean[])} for the definition of a
5708 * common and proper prefix.)
5709 *
5710 * <p>A {@code null} array reference is considered lexicographically less
5711 * than a non-{@code null} array reference. Two {@code null} array
5712 * references are considered equal.
5713 *
5714 * <p>The comparison is consistent with {@link #equals(boolean[], boolean[]) equals},
5715 * more specifically the following holds for arrays {@code a} and {@code b}:
5716 * <pre>{@code
5717 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
5718 * }</pre>
5719 *
5720 * @apiNote
5721 * <p>This method behaves as if (for non-{@code null} array references):
5722 * <pre>{@code
5723 * int i = Arrays.mismatch(a, b);
5724 * if (i >= 0 && i < Math.min(a.length, b.length))
5725 * return Boolean.compare(a[i], b[i]);
5726 * return a.length - b.length;
5727 * }</pre>
5728 *
5729 * @param a the first array to compare
5730 * @param b the second array to compare
5731 * @return the value {@code 0} if the first and second array are equal and
5732 * contain the same elements in the same order;
5733 * a value less than {@code 0} if the first array is
5734 * lexicographically less than the second array; and
5735 * a value greater than {@code 0} if the first array is
5736 * lexicographically greater than the second array
5737 * @since 9
5738 */
5739 public static int compare(boolean[] a, boolean[] b) {
5740 if (a == b)
5741 return 0;
5742 if (a == null || b == null)
5743 return a == null ? -1 : 1;
5744
5745 int i = ArraysSupport.mismatch(a, b,
5746 Math.min(a.length, b.length));
5747 if (i >= 0) {
5748 return Boolean.compare(a[i], b[i]);
5749 }
5750
5751 return a.length - b.length;
5752 }
5753
5754 /**
5755 * Compares two {@code boolean} arrays lexicographically over the specified
5756 * ranges.
5757 *
5758 * <p>If the two arrays, over the specified ranges, share a common prefix
5759 * then the lexicographic comparison is the result of comparing two
5760 * elements, as if by {@link Boolean#compare(boolean, boolean)}, at a
5761 * relative index within the respective arrays that is the length of the
5762 * prefix.
5763 * Otherwise, one array is a proper prefix of the other and, lexicographic
5764 * comparison is the result of comparing the two range lengths.
5765 * (See {@link #mismatch(boolean[], int, int, boolean[], int, int)} for the
5766 * definition of a common and proper prefix.)
5767 *
5768 * <p>The comparison is consistent with
5769 * {@link #equals(boolean[], int, int, boolean[], int, int) equals}, more
5770 * specifically the following holds for arrays {@code a} and {@code b} with
5771 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
5772 * [{@code bFromIndex}, {@code btoIndex}) respectively:
5773 * <pre>{@code
5774 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
5775 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
5776 * }</pre>
5777 *
5778 * @apiNote
5779 * <p>This method behaves as if:
5780 * <pre>{@code
5781 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
5782 * b, bFromIndex, bToIndex);
5783 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
5784 * return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
5785 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
5786 * }</pre>
5787 *
5788 * @param a the first array to compare
5789 * @param aFromIndex the index (inclusive) of the first element in the
5790 * first array to be compared
5791 * @param aToIndex the index (exclusive) of the last element in the
5792 * first array to be compared
5793 * @param b the second array to compare
5794 * @param bFromIndex the index (inclusive) of the first element in the
5795 * second array to be compared
5796 * @param bToIndex the index (exclusive) of the last element in the
5797 * second array to be compared
5798 * @return the value {@code 0} if, over the specified ranges, the first and
5799 * second array are equal and contain the same elements in the same
5800 * order;
5801 * a value less than {@code 0} if, over the specified ranges, the
5802 * first array is lexicographically less than the second array; and
5803 * a value greater than {@code 0} if, over the specified ranges, the
5804 * first array is lexicographically greater than the second array
5805 * @throws IllegalArgumentException
5806 * if {@code aFromIndex > aToIndex} or
5807 * if {@code bFromIndex > bToIndex}
5808 * @throws ArrayIndexOutOfBoundsException
5809 * if {@code aFromIndex < 0 or aToIndex > a.length} or
5810 * if {@code bFromIndex < 0 or bToIndex > b.length}
5811 * @throws NullPointerException
5812 * if either array is {@code null}
5813 * @since 9
5814 */
5815 public static int compare(boolean[] a, int aFromIndex, int aToIndex,
5816 boolean[] b, int bFromIndex, int bToIndex) {
5817 rangeCheck(a.length, aFromIndex, aToIndex);
5818 rangeCheck(b.length, bFromIndex, bToIndex);
5819
5820 int aLength = aToIndex - aFromIndex;
5821 int bLength = bToIndex - bFromIndex;
5822 int i = ArraysSupport.mismatch(a, aFromIndex,
5823 b, bFromIndex,
5824 Math.min(aLength, bLength));
5825 if (i >= 0) {
5826 return Boolean.compare(a[aFromIndex + i], b[bFromIndex + i]);
5827 }
5828
5829 return aLength - bLength;
5830 }
5831
5832 // Compare byte
5833
5834 /**
5835 * Compares two {@code byte} arrays lexicographically.
5836 *
5837 * <p>If the two arrays share a common prefix then the lexicographic
5838 * comparison is the result of comparing two elements, as if by
5839 * {@link Byte#compare(byte, byte)}, at an index within the respective
5840 * arrays that is the prefix length.
5841 * Otherwise, one array is a proper prefix of the other and, lexicographic
5842 * comparison is the result of comparing the two array lengths.
5843 * (See {@link #mismatch(byte[], byte[])} for the definition of a common and
5844 * proper prefix.)
5845 *
5846 * <p>A {@code null} array reference is considered lexicographically less
5847 * than a non-{@code null} array reference. Two {@code null} array
5848 * references are considered equal.
5849 *
5850 * <p>The comparison is consistent with {@link #equals(byte[], byte[]) equals},
5851 * more specifically the following holds for arrays {@code a} and {@code b}:
5852 * <pre>{@code
5853 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
5854 * }</pre>
5855 *
5856 * @apiNote
5857 * <p>This method behaves as if (for non-{@code null} array references):
5858 * <pre>{@code
5859 * int i = Arrays.mismatch(a, b);
5860 * if (i >= 0 && i < Math.min(a.length, b.length))
5861 * return Byte.compare(a[i], b[i]);
5862 * return a.length - b.length;
5863 * }</pre>
5864 *
5865 * @param a the first array to compare
5866 * @param b the second array to compare
5867 * @return the value {@code 0} if the first and second array are equal and
5868 * contain the same elements in the same order;
5869 * a value less than {@code 0} if the first array is
5870 * lexicographically less than the second array; and
5871 * a value greater than {@code 0} if the first array is
5872 * lexicographically greater than the second array
5873 * @since 9
5874 */
5875 public static int compare(byte[] a, byte[] b) {
5876 if (a == b)
5877 return 0;
5878 if (a == null || b == null)
5879 return a == null ? -1 : 1;
5880
5881 int i = ArraysSupport.mismatch(a, b,
5882 Math.min(a.length, b.length));
5883 if (i >= 0) {
5884 return Byte.compare(a[i], b[i]);
5885 }
5886
5887 return a.length - b.length;
5888 }
5889
5890 /**
5891 * Compares two {@code byte} arrays lexicographically over the specified
5892 * ranges.
5893 *
5894 * <p>If the two arrays, over the specified ranges, share a common prefix
5895 * then the lexicographic comparison is the result of comparing two
5896 * elements, as if by {@link Byte#compare(byte, byte)}, at a relative index
5897 * within the respective arrays that is the length of the prefix.
5898 * Otherwise, one array is a proper prefix of the other and, lexicographic
5899 * comparison is the result of comparing the two range lengths.
5900 * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
5901 * definition of a common and proper prefix.)
5902 *
5903 * <p>The comparison is consistent with
5904 * {@link #equals(byte[], int, int, byte[], int, int) equals}, more
5905 * specifically the following holds for arrays {@code a} and {@code b} with
5906 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
5907 * [{@code bFromIndex}, {@code btoIndex}) respectively:
5908 * <pre>{@code
5909 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
5910 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
5911 * }</pre>
5912 *
5913 * @apiNote
5914 * <p>This method behaves as if:
5915 * <pre>{@code
5916 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
5917 * b, bFromIndex, bToIndex);
5918 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
5919 * return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
5920 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
5921 * }</pre>
5922 *
5923 * @param a the first array to compare
5924 * @param aFromIndex the index (inclusive) of the first element in the
5925 * first array to be compared
5926 * @param aToIndex the index (exclusive) of the last element in the
5927 * first array to be compared
5928 * @param b the second array to compare
5929 * @param bFromIndex the index (inclusive) of the first element in the
5930 * second array to be compared
5931 * @param bToIndex the index (exclusive) of the last element in the
5932 * second array to be compared
5933 * @return the value {@code 0} if, over the specified ranges, the first and
5934 * second array are equal and contain the same elements in the same
5935 * order;
5936 * a value less than {@code 0} if, over the specified ranges, the
5937 * first array is lexicographically less than the second array; and
5938 * a value greater than {@code 0} if, over the specified ranges, the
5939 * first array is lexicographically greater than the second array
5940 * @throws IllegalArgumentException
5941 * if {@code aFromIndex > aToIndex} or
5942 * if {@code bFromIndex > bToIndex}
5943 * @throws ArrayIndexOutOfBoundsException
5944 * if {@code aFromIndex < 0 or aToIndex > a.length} or
5945 * if {@code bFromIndex < 0 or bToIndex > b.length}
5946 * @throws NullPointerException
5947 * if either array is {@code null}
5948 * @since 9
5949 */
5950 public static int compare(byte[] a, int aFromIndex, int aToIndex,
5951 byte[] b, int bFromIndex, int bToIndex) {
5952 rangeCheck(a.length, aFromIndex, aToIndex);
5953 rangeCheck(b.length, bFromIndex, bToIndex);
5954
5955 int aLength = aToIndex - aFromIndex;
5956 int bLength = bToIndex - bFromIndex;
5957 int i = ArraysSupport.mismatch(a, aFromIndex,
5958 b, bFromIndex,
5959 Math.min(aLength, bLength));
5960 if (i >= 0) {
5961 return Byte.compare(a[aFromIndex + i], b[bFromIndex + i]);
5962 }
5963
5964 return aLength - bLength;
5965 }
5966
5967 /**
5968 * Compares two {@code byte} arrays lexicographically, numerically treating
5969 * elements as unsigned.
5970 *
5971 * <p>If the two arrays share a common prefix then the lexicographic
5972 * comparison is the result of comparing two elements, as if by
5973 * {@link Byte#compareUnsigned(byte, byte)}, at an index within the
5974 * respective arrays that is the prefix length.
5975 * Otherwise, one array is a proper prefix of the other and, lexicographic
5976 * comparison is the result of comparing the two array lengths.
5977 * (See {@link #mismatch(byte[], byte[])} for the definition of a common
5978 * and proper prefix.)
5979 *
5980 * <p>A {@code null} array reference is considered lexicographically less
5981 * than a non-{@code null} array reference. Two {@code null} array
5982 * references are considered equal.
5983 *
5984 * @apiNote
5985 * <p>This method behaves as if (for non-{@code null} array references):
5986 * <pre>{@code
5987 * int i = Arrays.mismatch(a, b);
5988 * if (i >= 0 && i < Math.min(a.length, b.length))
5989 * return Byte.compareUnsigned(a[i], b[i]);
5990 * return a.length - b.length;
5991 * }</pre>
5992 *
5993 * @param a the first array to compare
5994 * @param b the second array to compare
5995 * @return the value {@code 0} if the first and second array are
5996 * equal and contain the same elements in the same order;
5997 * a value less than {@code 0} if the first array is
5998 * lexicographically less than the second array; and
5999 * a value greater than {@code 0} if the first array is
6000 * lexicographically greater than the second array
6001 * @since 9
6002 */
6003 public static int compareUnsigned(byte[] a, byte[] b) {
6004 if (a == b)
6005 return 0;
6006 if (a == null || b == null)
6007 return a == null ? -1 : 1;
6008
6009 int i = ArraysSupport.mismatch(a, b,
6010 Math.min(a.length, b.length));
6011 if (i >= 0) {
6012 return Byte.compareUnsigned(a[i], b[i]);
6013 }
6014
6015 return a.length - b.length;
6016 }
6017
6018
6019 /**
6020 * Compares two {@code byte} arrays lexicographically over the specified
6021 * ranges, numerically treating elements as unsigned.
6022 *
6023 * <p>If the two arrays, over the specified ranges, share a common prefix
6024 * then the lexicographic comparison is the result of comparing two
6025 * elements, as if by {@link Byte#compareUnsigned(byte, byte)}, at a
6026 * relative index within the respective arrays that is the length of the
6027 * prefix.
6028 * Otherwise, one array is a proper prefix of the other and, lexicographic
6029 * comparison is the result of comparing the two range lengths.
6030 * (See {@link #mismatch(byte[], int, int, byte[], int, int)} for the
6031 * definition of a common and proper prefix.)
6032 *
6033 * @apiNote
6034 * <p>This method behaves as if:
6035 * <pre>{@code
6036 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6037 * b, bFromIndex, bToIndex);
6038 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6039 * return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6040 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6041 * }</pre>
6042 *
6043 * @param a the first array to compare
6044 * @param aFromIndex the index (inclusive) of the first element in the
6045 * first array to be compared
6046 * @param aToIndex the index (exclusive) of the last element in the
6047 * first array to be compared
6048 * @param b the second array to compare
6049 * @param bFromIndex the index (inclusive) of the first element in the
6050 * second array to be compared
6051 * @param bToIndex the index (exclusive) of the last element in the
6052 * second array to be compared
6053 * @return the value {@code 0} if, over the specified ranges, the first and
6054 * second array are equal and contain the same elements in the same
6055 * order;
6056 * a value less than {@code 0} if, over the specified ranges, the
6057 * first array is lexicographically less than the second array; and
6058 * a value greater than {@code 0} if, over the specified ranges, the
6059 * first array is lexicographically greater than the second array
6060 * @throws IllegalArgumentException
6061 * if {@code aFromIndex > aToIndex} or
6062 * if {@code bFromIndex > bToIndex}
6063 * @throws ArrayIndexOutOfBoundsException
6064 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6065 * if {@code bFromIndex < 0 or bToIndex > b.length}
6066 * @throws NullPointerException
6067 * if either array is null
6068 * @since 9
6069 */
6070 public static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex,
6071 byte[] b, int bFromIndex, int bToIndex) {
6072 rangeCheck(a.length, aFromIndex, aToIndex);
6073 rangeCheck(b.length, bFromIndex, bToIndex);
6074
6075 int aLength = aToIndex - aFromIndex;
6076 int bLength = bToIndex - bFromIndex;
6077 int i = ArraysSupport.mismatch(a, aFromIndex,
6078 b, bFromIndex,
6079 Math.min(aLength, bLength));
6080 if (i >= 0) {
6081 return Byte.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6082 }
6083
6084 return aLength - bLength;
6085 }
6086
6087 // Compare short
6088
6089 /**
6090 * Compares two {@code short} arrays lexicographically.
6091 *
6092 * <p>If the two arrays share a common prefix then the lexicographic
6093 * comparison is the result of comparing two elements, as if by
6094 * {@link Short#compare(short, short)}, at an index within the respective
6095 * arrays that is the prefix length.
6096 * Otherwise, one array is a proper prefix of the other and, lexicographic
6097 * comparison is the result of comparing the two array lengths.
6098 * (See {@link #mismatch(short[], short[])} for the definition of a common
6099 * and proper prefix.)
6100 *
6101 * <p>A {@code null} array reference is considered lexicographically less
6102 * than a non-{@code null} array reference. Two {@code null} array
6103 * references are considered equal.
6104 *
6105 * <p>The comparison is consistent with {@link #equals(short[], short[]) equals},
6106 * more specifically the following holds for arrays {@code a} and {@code b}:
6107 * <pre>{@code
6108 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6109 * }</pre>
6110 *
6111 * @apiNote
6112 * <p>This method behaves as if (for non-{@code null} array references):
6113 * <pre>{@code
6114 * int i = Arrays.mismatch(a, b);
6115 * if (i >= 0 && i < Math.min(a.length, b.length))
6116 * return Short.compare(a[i], b[i]);
6117 * return a.length - b.length;
6118 * }</pre>
6119 *
6120 * @param a the first array to compare
6121 * @param b the second array to compare
6122 * @return the value {@code 0} if the first and second array are equal and
6123 * contain the same elements in the same order;
6124 * a value less than {@code 0} if the first array is
6125 * lexicographically less than the second array; and
6126 * a value greater than {@code 0} if the first array is
6127 * lexicographically greater than the second array
6128 * @since 9
6129 */
6130 public static int compare(short[] a, short[] b) {
6131 if (a == b)
6132 return 0;
6133 if (a == null || b == null)
6134 return a == null ? -1 : 1;
6135
6136 int i = ArraysSupport.mismatch(a, b,
6137 Math.min(a.length, b.length));
6138 if (i >= 0) {
6139 return Short.compare(a[i], b[i]);
6140 }
6141
6142 return a.length - b.length;
6143 }
6144
6145 /**
6146 * Compares two {@code short} arrays lexicographically over the specified
6147 * ranges.
6148 *
6149 * <p>If the two arrays, over the specified ranges, share a common prefix
6150 * then the lexicographic comparison is the result of comparing two
6151 * elements, as if by {@link Short#compare(short, short)}, at a relative
6152 * index within the respective arrays that is the length of the prefix.
6153 * Otherwise, one array is a proper prefix of the other and, lexicographic
6154 * comparison is the result of comparing the two range lengths.
6155 * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
6156 * definition of a common and proper prefix.)
6157 *
6158 * <p>The comparison is consistent with
6159 * {@link #equals(short[], int, int, short[], int, int) equals}, more
6160 * specifically the following holds for arrays {@code a} and {@code b} with
6161 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6162 * [{@code bFromIndex}, {@code btoIndex}) respectively:
6163 * <pre>{@code
6164 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6165 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6166 * }</pre>
6167 *
6168 * @apiNote
6169 * <p>This method behaves as if:
6170 * <pre>{@code
6171 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6172 * b, bFromIndex, bToIndex);
6173 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6174 * return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
6175 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6176 * }</pre>
6177 *
6178 * @param a the first array to compare
6179 * @param aFromIndex the index (inclusive) of the first element in the
6180 * first array to be compared
6181 * @param aToIndex the index (exclusive) of the last element in the
6182 * first array to be compared
6183 * @param b the second array to compare
6184 * @param bFromIndex the index (inclusive) of the first element in the
6185 * second array to be compared
6186 * @param bToIndex the index (exclusive) of the last element in the
6187 * second array to be compared
6188 * @return the value {@code 0} if, over the specified ranges, the first and
6189 * second array are equal and contain the same elements in the same
6190 * order;
6191 * a value less than {@code 0} if, over the specified ranges, the
6192 * first array is lexicographically less than the second array; and
6193 * a value greater than {@code 0} if, over the specified ranges, the
6194 * first array is lexicographically greater than the second array
6195 * @throws IllegalArgumentException
6196 * if {@code aFromIndex > aToIndex} or
6197 * if {@code bFromIndex > bToIndex}
6198 * @throws ArrayIndexOutOfBoundsException
6199 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6200 * if {@code bFromIndex < 0 or bToIndex > b.length}
6201 * @throws NullPointerException
6202 * if either array is {@code null}
6203 * @since 9
6204 */
6205 public static int compare(short[] a, int aFromIndex, int aToIndex,
6206 short[] b, int bFromIndex, int bToIndex) {
6207 rangeCheck(a.length, aFromIndex, aToIndex);
6208 rangeCheck(b.length, bFromIndex, bToIndex);
6209
6210 int aLength = aToIndex - aFromIndex;
6211 int bLength = bToIndex - bFromIndex;
6212 int i = ArraysSupport.mismatch(a, aFromIndex,
6213 b, bFromIndex,
6214 Math.min(aLength, bLength));
6215 if (i >= 0) {
6216 return Short.compare(a[aFromIndex + i], b[bFromIndex + i]);
6217 }
6218
6219 return aLength - bLength;
6220 }
6221
6222 /**
6223 * Compares two {@code short} arrays lexicographically, numerically treating
6224 * elements as unsigned.
6225 *
6226 * <p>If the two arrays share a common prefix then the lexicographic
6227 * comparison is the result of comparing two elements, as if by
6228 * {@link Short#compareUnsigned(short, short)}, at an index within the
6229 * respective arrays that is the prefix length.
6230 * Otherwise, one array is a proper prefix of the other and, lexicographic
6231 * comparison is the result of comparing the two array lengths.
6232 * (See {@link #mismatch(short[], short[])} for the definition of a common
6233 * and proper prefix.)
6234 *
6235 * <p>A {@code null} array reference is considered lexicographically less
6236 * than a non-{@code null} array reference. Two {@code null} array
6237 * references are considered equal.
6238 *
6239 * @apiNote
6240 * <p>This method behaves as if (for non-{@code null} array references):
6241 * <pre>{@code
6242 * int i = Arrays.mismatch(a, b);
6243 * if (i >= 0 && i < Math.min(a.length, b.length))
6244 * return Short.compareUnsigned(a[i], b[i]);
6245 * return a.length - b.length;
6246 * }</pre>
6247 *
6248 * @param a the first array to compare
6249 * @param b the second array to compare
6250 * @return the value {@code 0} if the first and second array are
6251 * equal and contain the same elements in the same order;
6252 * a value less than {@code 0} if the first array is
6253 * lexicographically less than the second array; and
6254 * a value greater than {@code 0} if the first array is
6255 * lexicographically greater than the second array
6256 * @since 9
6257 */
6258 public static int compareUnsigned(short[] a, short[] b) {
6259 if (a == b)
6260 return 0;
6261 if (a == null || b == null)
6262 return a == null ? -1 : 1;
6263
6264 int i = ArraysSupport.mismatch(a, b,
6265 Math.min(a.length, b.length));
6266 if (i >= 0) {
6267 return Short.compareUnsigned(a[i], b[i]);
6268 }
6269
6270 return a.length - b.length;
6271 }
6272
6273 /**
6274 * Compares two {@code short} arrays lexicographically over the specified
6275 * ranges, numerically treating elements as unsigned.
6276 *
6277 * <p>If the two arrays, over the specified ranges, share a common prefix
6278 * then the lexicographic comparison is the result of comparing two
6279 * elements, as if by {@link Short#compareUnsigned(short, short)}, at a
6280 * relative index within the respective arrays that is the length of the
6281 * prefix.
6282 * Otherwise, one array is a proper prefix of the other and, lexicographic
6283 * comparison is the result of comparing the two range lengths.
6284 * (See {@link #mismatch(short[], int, int, short[], int, int)} for the
6285 * definition of a common and proper prefix.)
6286 *
6287 * @apiNote
6288 * <p>This method behaves as if:
6289 * <pre>{@code
6290 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6291 * b, bFromIndex, bToIndex);
6292 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6293 * return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6294 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6295 * }</pre>
6296 *
6297 * @param a the first array to compare
6298 * @param aFromIndex the index (inclusive) of the first element in the
6299 * first array to be compared
6300 * @param aToIndex the index (exclusive) of the last element in the
6301 * first array to be compared
6302 * @param b the second array to compare
6303 * @param bFromIndex the index (inclusive) of the first element in the
6304 * second array to be compared
6305 * @param bToIndex the index (exclusive) of the last element in the
6306 * second array to be compared
6307 * @return the value {@code 0} if, over the specified ranges, the first and
6308 * second array are equal and contain the same elements in the same
6309 * order;
6310 * a value less than {@code 0} if, over the specified ranges, the
6311 * first array is lexicographically less than the second array; and
6312 * a value greater than {@code 0} if, over the specified ranges, the
6313 * first array is lexicographically greater than the second array
6314 * @throws IllegalArgumentException
6315 * if {@code aFromIndex > aToIndex} or
6316 * if {@code bFromIndex > bToIndex}
6317 * @throws ArrayIndexOutOfBoundsException
6318 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6319 * if {@code bFromIndex < 0 or bToIndex > b.length}
6320 * @throws NullPointerException
6321 * if either array is null
6322 * @since 9
6323 */
6324 public static int compareUnsigned(short[] a, int aFromIndex, int aToIndex,
6325 short[] b, int bFromIndex, int bToIndex) {
6326 rangeCheck(a.length, aFromIndex, aToIndex);
6327 rangeCheck(b.length, bFromIndex, bToIndex);
6328
6329 int aLength = aToIndex - aFromIndex;
6330 int bLength = bToIndex - bFromIndex;
6331 int i = ArraysSupport.mismatch(a, aFromIndex,
6332 b, bFromIndex,
6333 Math.min(aLength, bLength));
6334 if (i >= 0) {
6335 return Short.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6336 }
6337
6338 return aLength - bLength;
6339 }
6340
6341 // Compare char
6342
6343 /**
6344 * Compares two {@code char} arrays lexicographically.
6345 *
6346 * <p>If the two arrays share a common prefix then the lexicographic
6347 * comparison is the result of comparing two elements, as if by
6348 * {@link Character#compare(char, char)}, at an index within the respective
6349 * arrays that is the prefix length.
6350 * Otherwise, one array is a proper prefix of the other and, lexicographic
6351 * comparison is the result of comparing the two array lengths.
6352 * (See {@link #mismatch(char[], char[])} for the definition of a common and
6353 * proper prefix.)
6354 *
6355 * <p>A {@code null} array reference is considered lexicographically less
6356 * than a non-{@code null} array reference. Two {@code null} array
6357 * references are considered equal.
6358 *
6359 * <p>The comparison is consistent with {@link #equals(char[], char[]) equals},
6360 * more specifically the following holds for arrays {@code a} and {@code b}:
6361 * <pre>{@code
6362 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6363 * }</pre>
6364 *
6365 * @apiNote
6366 * <p>This method behaves as if (for non-{@code null} array references):
6367 * <pre>{@code
6368 * int i = Arrays.mismatch(a, b);
6369 * if (i >= 0 && i < Math.min(a.length, b.length))
6370 * return Character.compare(a[i], b[i]);
6371 * return a.length - b.length;
6372 * }</pre>
6373 *
6374 * @param a the first array to compare
6375 * @param b the second array to compare
6376 * @return the value {@code 0} if the first and second array are equal and
6377 * contain the same elements in the same order;
6378 * a value less than {@code 0} if the first array is
6379 * lexicographically less than the second array; and
6380 * a value greater than {@code 0} if the first array is
6381 * lexicographically greater than the second array
6382 * @since 9
6383 */
6384 public static int compare(char[] a, char[] b) {
6385 if (a == b)
6386 return 0;
6387 if (a == null || b == null)
6388 return a == null ? -1 : 1;
6389
6390 int i = ArraysSupport.mismatch(a, b,
6391 Math.min(a.length, b.length));
6392 if (i >= 0) {
6393 return Character.compare(a[i], b[i]);
6394 }
6395
6396 return a.length - b.length;
6397 }
6398
6399 /**
6400 * Compares two {@code char} arrays lexicographically over the specified
6401 * ranges.
6402 *
6403 * <p>If the two arrays, over the specified ranges, share a common prefix
6404 * then the lexicographic comparison is the result of comparing two
6405 * elements, as if by {@link Character#compare(char, char)}, at a relative
6406 * index within the respective arrays that is the length of the prefix.
6407 * Otherwise, one array is a proper prefix of the other and, lexicographic
6408 * comparison is the result of comparing the two range lengths.
6409 * (See {@link #mismatch(char[], int, int, char[], int, int)} for the
6410 * definition of a common and proper prefix.)
6411 *
6412 * <p>The comparison is consistent with
6413 * {@link #equals(char[], int, int, char[], int, int) equals}, more
6414 * specifically the following holds for arrays {@code a} and {@code b} with
6415 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6416 * [{@code bFromIndex}, {@code btoIndex}) respectively:
6417 * <pre>{@code
6418 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6419 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6420 * }</pre>
6421 *
6422 * @apiNote
6423 * <p>This method behaves as if:
6424 * <pre>{@code
6425 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6426 * b, bFromIndex, bToIndex);
6427 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6428 * return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
6429 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6430 * }</pre>
6431 *
6432 * @param a the first array to compare
6433 * @param aFromIndex the index (inclusive) of the first element in the
6434 * first array to be compared
6435 * @param aToIndex the index (exclusive) of the last element in the
6436 * first array to be compared
6437 * @param b the second array to compare
6438 * @param bFromIndex the index (inclusive) of the first element in the
6439 * second array to be compared
6440 * @param bToIndex the index (exclusive) of the last element in the
6441 * second array to be compared
6442 * @return the value {@code 0} if, over the specified ranges, the first and
6443 * second array are equal and contain the same elements in the same
6444 * order;
6445 * a value less than {@code 0} if, over the specified ranges, the
6446 * first array is lexicographically less than the second array; and
6447 * a value greater than {@code 0} if, over the specified ranges, the
6448 * first array is lexicographically greater than the second array
6449 * @throws IllegalArgumentException
6450 * if {@code aFromIndex > aToIndex} or
6451 * if {@code bFromIndex > bToIndex}
6452 * @throws ArrayIndexOutOfBoundsException
6453 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6454 * if {@code bFromIndex < 0 or bToIndex > b.length}
6455 * @throws NullPointerException
6456 * if either array is {@code null}
6457 * @since 9
6458 */
6459 public static int compare(char[] a, int aFromIndex, int aToIndex,
6460 char[] b, int bFromIndex, int bToIndex) {
6461 rangeCheck(a.length, aFromIndex, aToIndex);
6462 rangeCheck(b.length, bFromIndex, bToIndex);
6463
6464 int aLength = aToIndex - aFromIndex;
6465 int bLength = bToIndex - bFromIndex;
6466 int i = ArraysSupport.mismatch(a, aFromIndex,
6467 b, bFromIndex,
6468 Math.min(aLength, bLength));
6469 if (i >= 0) {
6470 return Character.compare(a[aFromIndex + i], b[bFromIndex + i]);
6471 }
6472
6473 return aLength - bLength;
6474 }
6475
6476 // Compare int
6477
6478 /**
6479 * Compares two {@code int} arrays lexicographically.
6480 *
6481 * <p>If the two arrays share a common prefix then the lexicographic
6482 * comparison is the result of comparing two elements, as if by
6483 * {@link Integer#compare(int, int)}, at an index within the respective
6484 * arrays that is the prefix length.
6485 * Otherwise, one array is a proper prefix of the other and, lexicographic
6486 * comparison is the result of comparing the two array lengths.
6487 * (See {@link #mismatch(int[], int[])} for the definition of a common and
6488 * proper prefix.)
6489 *
6490 * <p>A {@code null} array reference is considered lexicographically less
6491 * than a non-{@code null} array reference. Two {@code null} array
6492 * references are considered equal.
6493 *
6494 * <p>The comparison is consistent with {@link #equals(int[], int[]) equals},
6495 * more specifically the following holds for arrays {@code a} and {@code b}:
6496 * <pre>{@code
6497 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6498 * }</pre>
6499 *
6500 * @apiNote
6501 * <p>This method behaves as if (for non-{@code null} array references):
6502 * <pre>{@code
6503 * int i = Arrays.mismatch(a, b);
6504 * if (i >= 0 && i < Math.min(a.length, b.length))
6505 * return Integer.compare(a[i], b[i]);
6506 * return a.length - b.length;
6507 * }</pre>
6508 *
6509 * @param a the first array to compare
6510 * @param b the second array to compare
6511 * @return the value {@code 0} if the first and second array are equal and
6512 * contain the same elements in the same order;
6513 * a value less than {@code 0} if the first array is
6514 * lexicographically less than the second array; and
6515 * a value greater than {@code 0} if the first array is
6516 * lexicographically greater than the second array
6517 * @since 9
6518 */
6519 public static int compare(int[] a, int[] b) {
6520 if (a == b)
6521 return 0;
6522 if (a == null || b == null)
6523 return a == null ? -1 : 1;
6524
6525 int i = ArraysSupport.mismatch(a, b,
6526 Math.min(a.length, b.length));
6527 if (i >= 0) {
6528 return Integer.compare(a[i], b[i]);
6529 }
6530
6531 return a.length - b.length;
6532 }
6533
6534 /**
6535 * Compares two {@code int} arrays lexicographically over the specified
6536 * ranges.
6537 *
6538 * <p>If the two arrays, over the specified ranges, share a common prefix
6539 * then the lexicographic comparison is the result of comparing two
6540 * elements, as if by {@link Integer#compare(int, int)}, at a relative index
6541 * within the respective arrays that is the length of the prefix.
6542 * Otherwise, one array is a proper prefix of the other and, lexicographic
6543 * comparison is the result of comparing the two range lengths.
6544 * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
6545 * definition of a common and proper prefix.)
6546 *
6547 * <p>The comparison is consistent with
6548 * {@link #equals(int[], int, int, int[], int, int) equals}, more
6549 * specifically the following holds for arrays {@code a} and {@code b} with
6550 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6551 * [{@code bFromIndex}, {@code btoIndex}) respectively:
6552 * <pre>{@code
6553 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6554 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6555 * }</pre>
6556 *
6557 * @apiNote
6558 * <p>This method behaves as if:
6559 * <pre>{@code
6560 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6561 * b, bFromIndex, bToIndex);
6562 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6563 * return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
6564 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6565 * }</pre>
6566 *
6567 * @param a the first array to compare
6568 * @param aFromIndex the index (inclusive) of the first element in the
6569 * first array to be compared
6570 * @param aToIndex the index (exclusive) of the last element in the
6571 * first array to be compared
6572 * @param b the second array to compare
6573 * @param bFromIndex the index (inclusive) of the first element in the
6574 * second array to be compared
6575 * @param bToIndex the index (exclusive) of the last element in the
6576 * second array to be compared
6577 * @return the value {@code 0} if, over the specified ranges, the first and
6578 * second array are equal and contain the same elements in the same
6579 * order;
6580 * a value less than {@code 0} if, over the specified ranges, the
6581 * first array is lexicographically less than the second array; and
6582 * a value greater than {@code 0} if, over the specified ranges, the
6583 * first array is lexicographically greater than the second array
6584 * @throws IllegalArgumentException
6585 * if {@code aFromIndex > aToIndex} or
6586 * if {@code bFromIndex > bToIndex}
6587 * @throws ArrayIndexOutOfBoundsException
6588 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6589 * if {@code bFromIndex < 0 or bToIndex > b.length}
6590 * @throws NullPointerException
6591 * if either array is {@code null}
6592 * @since 9
6593 */
6594 public static int compare(int[] a, int aFromIndex, int aToIndex,
6595 int[] b, int bFromIndex, int bToIndex) {
6596 rangeCheck(a.length, aFromIndex, aToIndex);
6597 rangeCheck(b.length, bFromIndex, bToIndex);
6598
6599 int aLength = aToIndex - aFromIndex;
6600 int bLength = bToIndex - bFromIndex;
6601 int i = ArraysSupport.mismatch(a, aFromIndex,
6602 b, bFromIndex,
6603 Math.min(aLength, bLength));
6604 if (i >= 0) {
6605 return Integer.compare(a[aFromIndex + i], b[bFromIndex + i]);
6606 }
6607
6608 return aLength - bLength;
6609 }
6610
6611 /**
6612 * Compares two {@code int} arrays lexicographically, numerically treating
6613 * elements as unsigned.
6614 *
6615 * <p>If the two arrays share a common prefix then the lexicographic
6616 * comparison is the result of comparing two elements, as if by
6617 * {@link Integer#compareUnsigned(int, int)}, at an index within the
6618 * respective arrays that is the prefix length.
6619 * Otherwise, one array is a proper prefix of the other and, lexicographic
6620 * comparison is the result of comparing the two array lengths.
6621 * (See {@link #mismatch(int[], int[])} for the definition of a common
6622 * and proper prefix.)
6623 *
6624 * <p>A {@code null} array reference is considered lexicographically less
6625 * than a non-{@code null} array reference. Two {@code null} array
6626 * references are considered equal.
6627 *
6628 * @apiNote
6629 * <p>This method behaves as if (for non-{@code null} array references):
6630 * <pre>{@code
6631 * int i = Arrays.mismatch(a, b);
6632 * if (i >= 0 && i < Math.min(a.length, b.length))
6633 * return Integer.compareUnsigned(a[i], b[i]);
6634 * return a.length - b.length;
6635 * }</pre>
6636 *
6637 * @param a the first array to compare
6638 * @param b the second array to compare
6639 * @return the value {@code 0} if the first and second array are
6640 * equal and contain the same elements in the same order;
6641 * a value less than {@code 0} if the first array is
6642 * lexicographically less than the second array; and
6643 * a value greater than {@code 0} if the first array is
6644 * lexicographically greater than the second array
6645 * @since 9
6646 */
6647 public static int compareUnsigned(int[] a, int[] b) {
6648 if (a == b)
6649 return 0;
6650 if (a == null || b == null)
6651 return a == null ? -1 : 1;
6652
6653 int i = ArraysSupport.mismatch(a, b,
6654 Math.min(a.length, b.length));
6655 if (i >= 0) {
6656 return Integer.compareUnsigned(a[i], b[i]);
6657 }
6658
6659 return a.length - b.length;
6660 }
6661
6662 /**
6663 * Compares two {@code int} arrays lexicographically over the specified
6664 * ranges, numerically treating elements as unsigned.
6665 *
6666 * <p>If the two arrays, over the specified ranges, share a common prefix
6667 * then the lexicographic comparison is the result of comparing two
6668 * elements, as if by {@link Integer#compareUnsigned(int, int)}, at a
6669 * relative index within the respective arrays that is the length of the
6670 * prefix.
6671 * Otherwise, one array is a proper prefix of the other and, lexicographic
6672 * comparison is the result of comparing the two range lengths.
6673 * (See {@link #mismatch(int[], int, int, int[], int, int)} for the
6674 * definition of a common and proper prefix.)
6675 *
6676 * @apiNote
6677 * <p>This method behaves as if:
6678 * <pre>{@code
6679 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6680 * b, bFromIndex, bToIndex);
6681 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6682 * return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6683 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6684 * }</pre>
6685 *
6686 * @param a the first array to compare
6687 * @param aFromIndex the index (inclusive) of the first element in the
6688 * first array to be compared
6689 * @param aToIndex the index (exclusive) of the last element in the
6690 * first array to be compared
6691 * @param b the second array to compare
6692 * @param bFromIndex the index (inclusive) of the first element in the
6693 * second array to be compared
6694 * @param bToIndex the index (exclusive) of the last element in the
6695 * second array to be compared
6696 * @return the value {@code 0} if, over the specified ranges, the first and
6697 * second array are equal and contain the same elements in the same
6698 * order;
6699 * a value less than {@code 0} if, over the specified ranges, the
6700 * first array is lexicographically less than the second array; and
6701 * a value greater than {@code 0} if, over the specified ranges, the
6702 * first array is lexicographically greater than the second array
6703 * @throws IllegalArgumentException
6704 * if {@code aFromIndex > aToIndex} or
6705 * if {@code bFromIndex > bToIndex}
6706 * @throws ArrayIndexOutOfBoundsException
6707 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6708 * if {@code bFromIndex < 0 or bToIndex > b.length}
6709 * @throws NullPointerException
6710 * if either array is null
6711 * @since 9
6712 */
6713 public static int compareUnsigned(int[] a, int aFromIndex, int aToIndex,
6714 int[] b, int bFromIndex, int bToIndex) {
6715 rangeCheck(a.length, aFromIndex, aToIndex);
6716 rangeCheck(b.length, bFromIndex, bToIndex);
6717
6718 int aLength = aToIndex - aFromIndex;
6719 int bLength = bToIndex - bFromIndex;
6720 int i = ArraysSupport.mismatch(a, aFromIndex,
6721 b, bFromIndex,
6722 Math.min(aLength, bLength));
6723 if (i >= 0) {
6724 return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6725 }
6726
6727 return aLength - bLength;
6728 }
6729
6730 // Compare long
6731
6732 /**
6733 * Compares two {@code long} arrays lexicographically.
6734 *
6735 * <p>If the two arrays share a common prefix then the lexicographic
6736 * comparison is the result of comparing two elements, as if by
6737 * {@link Long#compare(long, long)}, at an index within the respective
6738 * arrays that is the prefix length.
6739 * Otherwise, one array is a proper prefix of the other and, lexicographic
6740 * comparison is the result of comparing the two array lengths.
6741 * (See {@link #mismatch(long[], long[])} for the definition of a common and
6742 * proper prefix.)
6743 *
6744 * <p>A {@code null} array reference is considered lexicographically less
6745 * than a non-{@code null} array reference. Two {@code null} array
6746 * references are considered equal.
6747 *
6748 * <p>The comparison is consistent with {@link #equals(long[], long[]) equals},
6749 * more specifically the following holds for arrays {@code a} and {@code b}:
6750 * <pre>{@code
6751 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
6752 * }</pre>
6753 *
6754 * @apiNote
6755 * <p>This method behaves as if (for non-{@code null} array references):
6756 * <pre>{@code
6757 * int i = Arrays.mismatch(a, b);
6758 * if (i >= 0 && i < Math.min(a.length, b.length))
6759 * return Long.compare(a[i], b[i]);
6760 * return a.length - b.length;
6761 * }</pre>
6762 *
6763 * @param a the first array to compare
6764 * @param b the second array to compare
6765 * @return the value {@code 0} if the first and second array are equal and
6766 * contain the same elements in the same order;
6767 * a value less than {@code 0} if the first array is
6768 * lexicographically less than the second array; and
6769 * a value greater than {@code 0} if the first array is
6770 * lexicographically greater than the second array
6771 * @since 9
6772 */
6773 public static int compare(long[] a, long[] b) {
6774 if (a == b)
6775 return 0;
6776 if (a == null || b == null)
6777 return a == null ? -1 : 1;
6778
6779 int i = ArraysSupport.mismatch(a, b,
6780 Math.min(a.length, b.length));
6781 if (i >= 0) {
6782 return Long.compare(a[i], b[i]);
6783 }
6784
6785 return a.length - b.length;
6786 }
6787
6788 /**
6789 * Compares two {@code long} arrays lexicographically over the specified
6790 * ranges.
6791 *
6792 * <p>If the two arrays, over the specified ranges, share a common prefix
6793 * then the lexicographic comparison is the result of comparing two
6794 * elements, as if by {@link Long#compare(long, long)}, at a relative index
6795 * within the respective arrays that is the length of the prefix.
6796 * Otherwise, one array is a proper prefix of the other and, lexicographic
6797 * comparison is the result of comparing the two range lengths.
6798 * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
6799 * definition of a common and proper prefix.)
6800 *
6801 * <p>The comparison is consistent with
6802 * {@link #equals(long[], int, int, long[], int, int) equals}, more
6803 * specifically the following holds for arrays {@code a} and {@code b} with
6804 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
6805 * [{@code bFromIndex}, {@code btoIndex}) respectively:
6806 * <pre>{@code
6807 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
6808 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
6809 * }</pre>
6810 *
6811 * @apiNote
6812 * <p>This method behaves as if:
6813 * <pre>{@code
6814 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6815 * b, bFromIndex, bToIndex);
6816 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6817 * return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
6818 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6819 * }</pre>
6820 *
6821 * @param a the first array to compare
6822 * @param aFromIndex the index (inclusive) of the first element in the
6823 * first array to be compared
6824 * @param aToIndex the index (exclusive) of the last element in the
6825 * first array to be compared
6826 * @param b the second array to compare
6827 * @param bFromIndex the index (inclusive) of the first element in the
6828 * second array to be compared
6829 * @param bToIndex the index (exclusive) of the last element in the
6830 * second array to be compared
6831 * @return the value {@code 0} if, over the specified ranges, the first and
6832 * second array are equal and contain the same elements in the same
6833 * order;
6834 * a value less than {@code 0} if, over the specified ranges, the
6835 * first array is lexicographically less than the second array; and
6836 * a value greater than {@code 0} if, over the specified ranges, the
6837 * first array is lexicographically greater than the second array
6838 * @throws IllegalArgumentException
6839 * if {@code aFromIndex > aToIndex} or
6840 * if {@code bFromIndex > bToIndex}
6841 * @throws ArrayIndexOutOfBoundsException
6842 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6843 * if {@code bFromIndex < 0 or bToIndex > b.length}
6844 * @throws NullPointerException
6845 * if either array is {@code null}
6846 * @since 9
6847 */
6848 public static int compare(long[] a, int aFromIndex, int aToIndex,
6849 long[] b, int bFromIndex, int bToIndex) {
6850 rangeCheck(a.length, aFromIndex, aToIndex);
6851 rangeCheck(b.length, bFromIndex, bToIndex);
6852
6853 int aLength = aToIndex - aFromIndex;
6854 int bLength = bToIndex - bFromIndex;
6855 int i = ArraysSupport.mismatch(a, aFromIndex,
6856 b, bFromIndex,
6857 Math.min(aLength, bLength));
6858 if (i >= 0) {
6859 return Long.compare(a[aFromIndex + i], b[bFromIndex + i]);
6860 }
6861
6862 return aLength - bLength;
6863 }
6864
6865 /**
6866 * Compares two {@code long} arrays lexicographically, numerically treating
6867 * elements as unsigned.
6868 *
6869 * <p>If the two arrays share a common prefix then the lexicographic
6870 * comparison is the result of comparing two elements, as if by
6871 * {@link Long#compareUnsigned(long, long)}, at an index within the
6872 * respective arrays that is the prefix length.
6873 * Otherwise, one array is a proper prefix of the other and, lexicographic
6874 * comparison is the result of comparing the two array lengths.
6875 * (See {@link #mismatch(long[], long[])} for the definition of a common
6876 * and proper prefix.)
6877 *
6878 * <p>A {@code null} array reference is considered lexicographically less
6879 * than a non-{@code null} array reference. Two {@code null} array
6880 * references are considered equal.
6881 *
6882 * @apiNote
6883 * <p>This method behaves as if (for non-{@code null} array references):
6884 * <pre>{@code
6885 * int i = Arrays.mismatch(a, b);
6886 * if (i >= 0 && i < Math.min(a.length, b.length))
6887 * return Long.compareUnsigned(a[i], b[i]);
6888 * return a.length - b.length;
6889 * }</pre>
6890 *
6891 * @param a the first array to compare
6892 * @param b the second array to compare
6893 * @return the value {@code 0} if the first and second array are
6894 * equal and contain the same elements in the same order;
6895 * a value less than {@code 0} if the first array is
6896 * lexicographically less than the second array; and
6897 * a value greater than {@code 0} if the first array is
6898 * lexicographically greater than the second array
6899 * @since 9
6900 */
6901 public static int compareUnsigned(long[] a, long[] b) {
6902 if (a == b)
6903 return 0;
6904 if (a == null || b == null)
6905 return a == null ? -1 : 1;
6906
6907 int i = ArraysSupport.mismatch(a, b,
6908 Math.min(a.length, b.length));
6909 if (i >= 0) {
6910 return Long.compareUnsigned(a[i], b[i]);
6911 }
6912
6913 return a.length - b.length;
6914 }
6915
6916 /**
6917 * Compares two {@code long} arrays lexicographically over the specified
6918 * ranges, numerically treating elements as unsigned.
6919 *
6920 * <p>If the two arrays, over the specified ranges, share a common prefix
6921 * then the lexicographic comparison is the result of comparing two
6922 * elements, as if by {@link Long#compareUnsigned(long, long)}, at a
6923 * relative index within the respective arrays that is the length of the
6924 * prefix.
6925 * Otherwise, one array is a proper prefix of the other and, lexicographic
6926 * comparison is the result of comparing the two range lengths.
6927 * (See {@link #mismatch(long[], int, int, long[], int, int)} for the
6928 * definition of a common and proper prefix.)
6929 *
6930 * @apiNote
6931 * <p>This method behaves as if:
6932 * <pre>{@code
6933 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
6934 * b, bFromIndex, bToIndex);
6935 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
6936 * return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6937 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
6938 * }</pre>
6939 *
6940 * @param a the first array to compare
6941 * @param aFromIndex the index (inclusive) of the first element in the
6942 * first array to be compared
6943 * @param aToIndex the index (exclusive) of the last element in the
6944 * first array to be compared
6945 * @param b the second array to compare
6946 * @param bFromIndex the index (inclusive) of the first element in the
6947 * second array to be compared
6948 * @param bToIndex the index (exclusive) of the last element in the
6949 * second array to be compared
6950 * @return the value {@code 0} if, over the specified ranges, the first and
6951 * second array are equal and contain the same elements in the same
6952 * order;
6953 * a value less than {@code 0} if, over the specified ranges, the
6954 * first array is lexicographically less than the second array; and
6955 * a value greater than {@code 0} if, over the specified ranges, the
6956 * first array is lexicographically greater than the second array
6957 * @throws IllegalArgumentException
6958 * if {@code aFromIndex > aToIndex} or
6959 * if {@code bFromIndex > bToIndex}
6960 * @throws ArrayIndexOutOfBoundsException
6961 * if {@code aFromIndex < 0 or aToIndex > a.length} or
6962 * if {@code bFromIndex < 0 or bToIndex > b.length}
6963 * @throws NullPointerException
6964 * if either array is null
6965 * @since 9
6966 */
6967 public static int compareUnsigned(long[] a, int aFromIndex, int aToIndex,
6968 long[] b, int bFromIndex, int bToIndex) {
6969 rangeCheck(a.length, aFromIndex, aToIndex);
6970 rangeCheck(b.length, bFromIndex, bToIndex);
6971
6972 int aLength = aToIndex - aFromIndex;
6973 int bLength = bToIndex - bFromIndex;
6974 int i = ArraysSupport.mismatch(a, aFromIndex,
6975 b, bFromIndex,
6976 Math.min(aLength, bLength));
6977 if (i >= 0) {
6978 return Long.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
6979 }
6980
6981 return aLength - bLength;
6982 }
6983
6984 // Compare float
6985
6986 /**
6987 * Compares two {@code float} arrays lexicographically.
6988 *
6989 * <p>If the two arrays share a common prefix then the lexicographic
6990 * comparison is the result of comparing two elements, as if by
6991 * {@link Float#compare(float, float)}, at an index within the respective
6992 * arrays that is the prefix length.
6993 * Otherwise, one array is a proper prefix of the other and, lexicographic
6994 * comparison is the result of comparing the two array lengths.
6995 * (See {@link #mismatch(float[], float[])} for the definition of a common
6996 * and proper prefix.)
6997 *
6998 * <p>A {@code null} array reference is considered lexicographically less
6999 * than a non-{@code null} array reference. Two {@code null} array
7000 * references are considered equal.
7001 *
7002 * <p>The comparison is consistent with {@link #equals(float[], float[]) equals},
7003 * more specifically the following holds for arrays {@code a} and {@code b}:
7004 * <pre>{@code
7005 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
7006 * }</pre>
7007 *
7008 * @apiNote
7009 * <p>This method behaves as if (for non-{@code null} array references):
7010 * <pre>{@code
7011 * int i = Arrays.mismatch(a, b);
7012 * if (i >= 0 && i < Math.min(a.length, b.length))
7013 * return Float.compare(a[i], b[i]);
7014 * return a.length - b.length;
7015 * }</pre>
7016 *
7017 * @param a the first array to compare
7018 * @param b the second array to compare
7019 * @return the value {@code 0} if the first and second array are equal and
7020 * contain the same elements in the same order;
7021 * a value less than {@code 0} if the first array is
7022 * lexicographically less than the second array; and
7023 * a value greater than {@code 0} if the first array is
7024 * lexicographically greater than the second array
7025 * @since 9
7026 */
7027 public static int compare(float[] a, float[] b) {
7028 if (a == b)
7029 return 0;
7030 if (a == null || b == null)
7031 return a == null ? -1 : 1;
7032
7033 int i = ArraysSupport.mismatch(a, b,
7034 Math.min(a.length, b.length));
7035 if (i >= 0) {
7036 return Float.compare(a[i], b[i]);
7037 }
7038
7039 return a.length - b.length;
7040 }
7041
7042 /**
7043 * Compares two {@code float} arrays lexicographically over the specified
7044 * ranges.
7045 *
7046 * <p>If the two arrays, over the specified ranges, share a common prefix
7047 * then the lexicographic comparison is the result of comparing two
7048 * elements, as if by {@link Float#compare(float, float)}, at a relative
7049 * index within the respective arrays that is the length of the prefix.
7050 * Otherwise, one array is a proper prefix of the other and, lexicographic
7051 * comparison is the result of comparing the two range lengths.
7052 * (See {@link #mismatch(float[], int, int, float[], int, int)} for the
7053 * definition of a common and proper prefix.)
7054 *
7055 * <p>The comparison is consistent with
7056 * {@link #equals(float[], int, int, float[], int, int) equals}, more
7057 * specifically the following holds for arrays {@code a} and {@code b} with
7058 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
7059 * [{@code bFromIndex}, {@code btoIndex}) respectively:
7060 * <pre>{@code
7061 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
7062 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
7063 * }</pre>
7064 *
7065 * @apiNote
7066 * <p>This method behaves as if:
7067 * <pre>{@code
7068 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7069 * b, bFromIndex, bToIndex);
7070 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7071 * return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
7072 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7073 * }</pre>
7074 *
7075 * @param a the first array to compare
7076 * @param aFromIndex the index (inclusive) of the first element in the
7077 * first array to be compared
7078 * @param aToIndex the index (exclusive) of the last element in the
7079 * first array to be compared
7080 * @param b the second array to compare
7081 * @param bFromIndex the index (inclusive) of the first element in the
7082 * second array to be compared
7083 * @param bToIndex the index (exclusive) of the last element in the
7084 * second array to be compared
7085 * @return the value {@code 0} if, over the specified ranges, the first and
7086 * second array are equal and contain the same elements in the same
7087 * order;
7088 * a value less than {@code 0} if, over the specified ranges, the
7089 * first array is lexicographically less than the second array; and
7090 * a value greater than {@code 0} if, over the specified ranges, the
7091 * first array is lexicographically greater than the second array
7092 * @throws IllegalArgumentException
7093 * if {@code aFromIndex > aToIndex} or
7094 * if {@code bFromIndex > bToIndex}
7095 * @throws ArrayIndexOutOfBoundsException
7096 * if {@code aFromIndex < 0 or aToIndex > a.length} or
7097 * if {@code bFromIndex < 0 or bToIndex > b.length}
7098 * @throws NullPointerException
7099 * if either array is {@code null}
7100 * @since 9
7101 */
7102 public static int compare(float[] a, int aFromIndex, int aToIndex,
7103 float[] b, int bFromIndex, int bToIndex) {
7104 rangeCheck(a.length, aFromIndex, aToIndex);
7105 rangeCheck(b.length, bFromIndex, bToIndex);
7106
7107 int aLength = aToIndex - aFromIndex;
7108 int bLength = bToIndex - bFromIndex;
7109 int i = ArraysSupport.mismatch(a, aFromIndex,
7110 b, bFromIndex,
7111 Math.min(aLength, bLength));
7112 if (i >= 0) {
7113 return Float.compare(a[aFromIndex + i], b[bFromIndex + i]);
7114 }
7115
7116 return aLength - bLength;
7117 }
7118
7119 // Compare double
7120
7121 /**
7122 * Compares two {@code double} arrays lexicographically.
7123 *
7124 * <p>If the two arrays share a common prefix then the lexicographic
7125 * comparison is the result of comparing two elements, as if by
7126 * {@link Double#compare(double, double)}, at an index within the respective
7127 * arrays that is the prefix length.
7128 * Otherwise, one array is a proper prefix of the other and, lexicographic
7129 * comparison is the result of comparing the two array lengths.
7130 * (See {@link #mismatch(double[], double[])} for the definition of a common
7131 * and proper prefix.)
7132 *
7133 * <p>A {@code null} array reference is considered lexicographically less
7134 * than a non-{@code null} array reference. Two {@code null} array
7135 * references are considered equal.
7136 *
7137 * <p>The comparison is consistent with {@link #equals(double[], double[]) equals},
7138 * more specifically the following holds for arrays {@code a} and {@code b}:
7139 * <pre>{@code
7140 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
7141 * }</pre>
7142 *
7143 * @apiNote
7144 * <p>This method behaves as if (for non-{@code null} array references):
7145 * <pre>{@code
7146 * int i = Arrays.mismatch(a, b);
7147 * if (i >= 0 && i < Math.min(a.length, b.length))
7148 * return Double.compare(a[i], b[i]);
7149 * return a.length - b.length;
7150 * }</pre>
7151 *
7152 * @param a the first array to compare
7153 * @param b the second array to compare
7154 * @return the value {@code 0} if the first and second array are equal and
7155 * contain the same elements in the same order;
7156 * a value less than {@code 0} if the first array is
7157 * lexicographically less than the second array; and
7158 * a value greater than {@code 0} if the first array is
7159 * lexicographically greater than the second array
7160 * @since 9
7161 */
7162 public static int compare(double[] a, double[] b) {
7163 if (a == b)
7164 return 0;
7165 if (a == null || b == null)
7166 return a == null ? -1 : 1;
7167
7168 int i = ArraysSupport.mismatch(a, b,
7169 Math.min(a.length, b.length));
7170 if (i >= 0) {
7171 return Double.compare(a[i], b[i]);
7172 }
7173
7174 return a.length - b.length;
7175 }
7176
7177 /**
7178 * Compares two {@code double} arrays lexicographically over the specified
7179 * ranges.
7180 *
7181 * <p>If the two arrays, over the specified ranges, share a common prefix
7182 * then the lexicographic comparison is the result of comparing two
7183 * elements, as if by {@link Double#compare(double, double)}, at a relative
7184 * index within the respective arrays that is the length of the prefix.
7185 * Otherwise, one array is a proper prefix of the other and, lexicographic
7186 * comparison is the result of comparing the two range lengths.
7187 * (See {@link #mismatch(double[], int, int, double[], int, int)} for the
7188 * definition of a common and proper prefix.)
7189 *
7190 * <p>The comparison is consistent with
7191 * {@link #equals(double[], int, int, double[], int, int) equals}, more
7192 * specifically the following holds for arrays {@code a} and {@code b} with
7193 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
7194 * [{@code bFromIndex}, {@code btoIndex}) respectively:
7195 * <pre>{@code
7196 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
7197 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
7198 * }</pre>
7199 *
7200 * @apiNote
7201 * <p>This method behaves as if:
7202 * <pre>{@code
7203 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7204 * b, bFromIndex, bToIndex);
7205 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7206 * return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
7207 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7208 * }</pre>
7209 *
7210 * @param a the first array to compare
7211 * @param aFromIndex the index (inclusive) of the first element in the
7212 * first array to be compared
7213 * @param aToIndex the index (exclusive) of the last element in the
7214 * first array to be compared
7215 * @param b the second array to compare
7216 * @param bFromIndex the index (inclusive) of the first element in the
7217 * second array to be compared
7218 * @param bToIndex the index (exclusive) of the last element in the
7219 * second array to be compared
7220 * @return the value {@code 0} if, over the specified ranges, the first and
7221 * second array are equal and contain the same elements in the same
7222 * order;
7223 * a value less than {@code 0} if, over the specified ranges, the
7224 * first array is lexicographically less than the second array; and
7225 * a value greater than {@code 0} if, over the specified ranges, the
7226 * first array is lexicographically greater than the second array
7227 * @throws IllegalArgumentException
7228 * if {@code aFromIndex > aToIndex} or
7229 * if {@code bFromIndex > bToIndex}
7230 * @throws ArrayIndexOutOfBoundsException
7231 * if {@code aFromIndex < 0 or aToIndex > a.length} or
7232 * if {@code bFromIndex < 0 or bToIndex > b.length}
7233 * @throws NullPointerException
7234 * if either array is {@code null}
7235 * @since 9
7236 */
7237 public static int compare(double[] a, int aFromIndex, int aToIndex,
7238 double[] b, int bFromIndex, int bToIndex) {
7239 rangeCheck(a.length, aFromIndex, aToIndex);
7240 rangeCheck(b.length, bFromIndex, bToIndex);
7241
7242 int aLength = aToIndex - aFromIndex;
7243 int bLength = bToIndex - bFromIndex;
7244 int i = ArraysSupport.mismatch(a, aFromIndex,
7245 b, bFromIndex,
7246 Math.min(aLength, bLength));
7247 if (i >= 0) {
7248 return Double.compare(a[aFromIndex + i], b[bFromIndex + i]);
7249 }
7250
7251 return aLength - bLength;
7252 }
7253
7254 // Compare objects
7255
7256 /**
7257 * Compares two {@code Object} arrays, within comparable elements,
7258 * lexicographically.
7259 *
7260 * <p>If the two arrays share a common prefix then the lexicographic
7261 * comparison is the result of comparing two elements of type {@code T} at
7262 * an index {@code i} within the respective arrays that is the prefix
7263 * length, as if by:
7264 * <pre>{@code
7265 * Comparator.nullsFirst(Comparator.<T>naturalOrder()).
7266 * compare(a[i], b[i])
7267 * }</pre>
7268 * Otherwise, one array is a proper prefix of the other and, lexicographic
7269 * comparison is the result of comparing the two array lengths.
7270 * (See {@link #mismatch(Object[], Object[])} for the definition of a common
7271 * and proper prefix.)
7272 *
7273 * <p>A {@code null} array reference is considered lexicographically less
7274 * than a non-{@code null} array reference. Two {@code null} array
7275 * references are considered equal.
7276 * A {@code null} array element is considered lexicographically than a
7277 * non-{@code null} array element. Two {@code null} array elements are
7278 * considered equal.
7279 *
7280 * <p>The comparison is consistent with {@link #equals(Object[], Object[]) equals},
7281 * more specifically the following holds for arrays {@code a} and {@code b}:
7282 * <pre>{@code
7283 * Arrays.equals(a, b) == (Arrays.compare(a, b) == 0)
7284 * }</pre>
7285 *
7286 * @apiNote
7287 * <p>This method behaves as if (for non-{@code null} array references
7288 * and elements):
7289 * <pre>{@code
7290 * int i = Arrays.mismatch(a, b);
7291 * if (i >= 0 && i < Math.min(a.length, b.length))
7292 * return a[i].compareTo(b[i]);
7293 * return a.length - b.length;
7294 * }</pre>
7295 *
7296 * @param a the first array to compare
7297 * @param b the second array to compare
7298 * @param <T> the type of comparable array elements
7299 * @return the value {@code 0} if the first and second array are equal and
7300 * contain the same elements in the same order;
7301 * a value less than {@code 0} if the first array is
7302 * lexicographically less than the second array; and
7303 * a value greater than {@code 0} if the first array is
7304 * lexicographically greater than the second array
7305 * @since 9
7306 */
7307 public static <T extends Comparable<? super T>> int compare(T[] a, T[] b) {
7308 if (a == b)
7309 return 0;
7310 // A null array is less than a non-null array
7311 if (a == null || b == null)
7312 return a == null ? -1 : 1;
7313
7314 int length = Math.min(a.length, b.length);
7315 for (int i = 0; i < length; i++) {
7316 T oa = a[i];
7317 T ob = b[i];
7318 if (oa != ob) {
7319 // A null element is less than a non-null element
7320 if (oa == null || ob == null)
7321 return oa == null ? -1 : 1;
7322 int v = oa.compareTo(ob);
7323 if (v != 0) {
7324 return v;
7325 }
7326 }
7327 }
7328
7329 return a.length - b.length;
7330 }
7331
7332 /**
7333 * Compares two {@code Object} arrays lexicographically over the specified
7334 * ranges.
7335 *
7336 * <p>If the two arrays, over the specified ranges, share a common prefix
7337 * then the lexicographic comparison is the result of comparing two
7338 * elements of type {@code T} at a relative index {@code i} within the
7339 * respective arrays that is the prefix length, as if by:
7340 * <pre>{@code
7341 * Comparator.nullsFirst(Comparator.<T>naturalOrder()).
7342 * compare(a[aFromIndex + i, b[bFromIndex + i])
7343 * }</pre>
7344 * Otherwise, one array is a proper prefix of the other and, lexicographic
7345 * comparison is the result of comparing the two range lengths.
7346 * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
7347 * definition of a common and proper prefix.)
7348 *
7349 * <p>The comparison is consistent with
7350 * {@link #equals(Object[], int, int, Object[], int, int) equals}, more
7351 * specifically the following holds for arrays {@code a} and {@code b} with
7352 * specified ranges [{@code aFromIndex}, {@code atoIndex}) and
7353 * [{@code bFromIndex}, {@code btoIndex}) respectively:
7354 * <pre>{@code
7355 * Arrays.equals(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) ==
7356 * (Arrays.compare(a, aFromIndex, aToIndex, b, bFromIndex, bToIndex) == 0)
7357 * }</pre>
7358 *
7359 * @apiNote
7360 * <p>This method behaves as if (for non-{@code null} array elements):
7361 * <pre>{@code
7362 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7363 * b, bFromIndex, bToIndex);
7364 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7365 * return a[aFromIndex + i].compareTo(b[bFromIndex + i]);
7366 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7367 * }</pre>
7368 *
7369 * @param a the first array to compare
7370 * @param aFromIndex the index (inclusive) of the first element in the
7371 * first array to be compared
7372 * @param aToIndex the index (exclusive) of the last element in the
7373 * first array to be compared
7374 * @param b the second array to compare
7375 * @param bFromIndex the index (inclusive) of the first element in the
7376 * second array to be compared
7377 * @param bToIndex the index (exclusive) of the last element in the
7378 * second array to be compared
7379 * @param <T> the type of comparable array elements
7380 * @return the value {@code 0} if, over the specified ranges, the first and
7381 * second array are equal and contain the same elements in the same
7382 * order;
7383 * a value less than {@code 0} if, over the specified ranges, the
7384 * first array is lexicographically less than the second array; and
7385 * a value greater than {@code 0} if, over the specified ranges, the
7386 * first array is lexicographically greater than the second array
7387 * @throws IllegalArgumentException
7388 * if {@code aFromIndex > aToIndex} or
7389 * if {@code bFromIndex > bToIndex}
7390 * @throws ArrayIndexOutOfBoundsException
7391 * if {@code aFromIndex < 0 or aToIndex > a.length} or
7392 * if {@code bFromIndex < 0 or bToIndex > b.length}
7393 * @throws NullPointerException
7394 * if either array is {@code null}
7395 * @since 9
7396 */
7397 public static <T extends Comparable<? super T>> int compare(
7398 T[] a, int aFromIndex, int aToIndex,
7399 T[] b, int bFromIndex, int bToIndex) {
7400 rangeCheck(a.length, aFromIndex, aToIndex);
7401 rangeCheck(b.length, bFromIndex, bToIndex);
7402
7403 int aLength = aToIndex - aFromIndex;
7404 int bLength = bToIndex - bFromIndex;
7405 int length = Math.min(aLength, bLength);
7406 for (int i = 0; i < length; i++) {
7407 T oa = a[aFromIndex++];
7408 T ob = b[bFromIndex++];
7409 if (oa != ob) {
7410 if (oa == null || ob == null)
7411 return oa == null ? -1 : 1;
7412 int v = oa.compareTo(ob);
7413 if (v != 0) {
7414 return v;
7415 }
7416 }
7417 }
7418
7419 return aLength - bLength;
7420 }
7421
7422 /**
7423 * Compares two {@code Object} arrays lexicographically using a specified
7424 * comparator.
7425 *
7426 * <p>If the two arrays share a common prefix then the lexicographic
7427 * comparison is the result of comparing with the specified comparator two
7428 * elements at an index within the respective arrays that is the prefix
7429 * length.
7430 * Otherwise, one array is a proper prefix of the other and, lexicographic
7431 * comparison is the result of comparing the two array lengths.
7432 * (See {@link #mismatch(Object[], Object[])} for the definition of a common
7433 * and proper prefix.)
7434 *
7435 * <p>A {@code null} array reference is considered lexicographically less
7436 * than a non-{@code null} array reference. Two {@code null} array
7437 * references are considered equal.
7438 *
7439 * @apiNote
7440 * <p>This method behaves as if (for non-{@code null} array references):
7441 * <pre>{@code
7442 * int i = Arrays.mismatch(a, b, cmp);
7443 * if (i >= 0 && i < Math.min(a.length, b.length))
7444 * return cmp.compare(a[i], b[i]);
7445 * return a.length - b.length;
7446 * }</pre>
7447 *
7448 * @param a the first array to compare
7449 * @param b the second array to compare
7450 * @param cmp the comparator to compare array elements
7451 * @param <T> the type of array elements
7452 * @return the value {@code 0} if the first and second array are equal and
7453 * contain the same elements in the same order;
7454 * a value less than {@code 0} if the first array is
7455 * lexicographically less than the second array; and
7456 * a value greater than {@code 0} if the first array is
7457 * lexicographically greater than the second array
7458 * @throws NullPointerException if the comparator is {@code null}
7459 * @since 9
7460 */
7461 public static <T> int compare(T[] a, T[] b,
7462 Comparator<? super T> cmp) {
7463 Objects.requireNonNull(cmp);
7464 if (a == b)
7465 return 0;
7466 if (a == null || b == null)
7467 return a == null ? -1 : 1;
7468
7469 int length = Math.min(a.length, b.length);
7470 for (int i = 0; i < length; i++) {
7471 T oa = a[i];
7472 T ob = b[i];
7473 if (oa != ob) {
7474 // Null-value comparison is deferred to the comparator
7475 int v = cmp.compare(oa, ob);
7476 if (v != 0) {
7477 return v;
7478 }
7479 }
7480 }
7481
7482 return a.length - b.length;
7483 }
7484
7485 /**
7486 * Compares two {@code Object} arrays lexicographically over the specified
7487 * ranges.
7488 *
7489 * <p>If the two arrays, over the specified ranges, share a common prefix
7490 * then the lexicographic comparison is the result of comparing with the
7491 * specified comparator two elements at a relative index within the
7492 * respective arrays that is the prefix length.
7493 * Otherwise, one array is a proper prefix of the other and, lexicographic
7494 * comparison is the result of comparing the two range lengths.
7495 * (See {@link #mismatch(Object[], int, int, Object[], int, int)} for the
7496 * definition of a common and proper prefix.)
7497 *
7498 * @apiNote
7499 * <p>This method behaves as if (for non-{@code null} array elements):
7500 * <pre>{@code
7501 * int i = Arrays.mismatch(a, aFromIndex, aToIndex,
7502 * b, bFromIndex, bToIndex, cmp);
7503 * if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7504 * return cmp.compare(a[aFromIndex + i], b[bFromIndex + i]);
7505 * return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);
7506 * }</pre>
7507 *
7508 * @param a the first array to compare
7509 * @param aFromIndex the index (inclusive) of the first element in the
7510 * first array to be compared
7511 * @param aToIndex the index (exclusive) of the last element in the
7512 * first array to be compared
7513 * @param b the second array to compare
7514 * @param bFromIndex the index (inclusive) of the first element in the
7515 * second array to be compared
7516 * @param bToIndex the index (exclusive) of the last element in the
7517 * second array to be compared
7518 * @param cmp the comparator to compare array elements
7519 * @param <T> the type of array elements
7520 * @return the value {@code 0} if, over the specified ranges, the first and
7521 * second array are equal and contain the same elements in the same
7522 * order;
7523 * a value less than {@code 0} if, over the specified ranges, the
7524 * first array is lexicographically less than the second array; and
7525 * a value greater than {@code 0} if, over the specified ranges, the
7526 * first array is lexicographically greater than the second array
7527 * @throws IllegalArgumentException
7528 * if {@code aFromIndex > aToIndex} or
7529 * if {@code bFromIndex > bToIndex}
7530 * @throws ArrayIndexOutOfBoundsException
7531 * if {@code aFromIndex < 0 or aToIndex > a.length} or
7532 * if {@code bFromIndex < 0 or bToIndex > b.length}
7533 * @throws NullPointerException
7534 * if either array or the comparator is {@code null}
7535 * @since 9
7536 */
7537 public static <T> int compare(
7538 T[] a, int aFromIndex, int aToIndex,
7539 T[] b, int bFromIndex, int bToIndex,
7540 Comparator<? super T> cmp) {
7541 Objects.requireNonNull(cmp);
7542 rangeCheck(a.length, aFromIndex, aToIndex);
7543 rangeCheck(b.length, bFromIndex, bToIndex);
7544
7545 int aLength = aToIndex - aFromIndex;
7546 int bLength = bToIndex - bFromIndex;
7547 int length = Math.min(aLength, bLength);
7548 for (int i = 0; i < length; i++) {
7549 T oa = a[aFromIndex++];
7550 T ob = b[bFromIndex++];
7551 if (oa != ob) {
7552 // Null-value comparison is deferred to the comparator
7553 int v = cmp.compare(oa, ob);
7554 if (v != 0) {
7555 return v;
7556 }
7557 }
7558 }
7559
7560 return aLength - bLength;
7561 }
7562
7563
7564 // Mismatch methods
7565
7566 // Mismatch boolean
7567
7568 /**
7569 * Finds and returns the index of the first mismatch between two
7570 * {@code boolean} arrays, otherwise return -1 if no mismatch is found. The
7571 * index will be in the range of 0 (inclusive) up to the length (inclusive)
7572 * of the smaller array.
7573 *
7574 * <p>If the two arrays share a common prefix then the returned index is the
7575 * length of the common prefix and it follows that there is a mismatch
7576 * between the two elements at that index within the respective arrays.
7577 * If one array is a proper prefix of the other then the returned index is
7578 * the length of the smaller array and it follows that the index is only
7579 * valid for the larger array.
7580 * Otherwise, there is no mismatch.
7581 *
7582 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7583 * prefix of length {@code pl} if the following expression is true:
7584 * <pre>{@code
7585 * pl >= 0 &&
7586 * pl < Math.min(a.length, b.length) &&
7587 * Arrays.equals(a, 0, pl, b, 0, pl) &&
7588 * a[pl] != b[pl]
7589 * }</pre>
7590 * Note that a common prefix length of {@code 0} indicates that the first
7591 * elements from each array mismatch.
7592 *
7593 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7594 * prefix if the following expression is true:
7595 * <pre>{@code
7596 * a.length != b.length &&
7597 * Arrays.equals(a, 0, Math.min(a.length, b.length),
7598 * b, 0, Math.min(a.length, b.length))
7599 * }</pre>
7600 *
7601 * @param a the first array to be tested for a mismatch
7602 * @param b the second array to be tested for a mismatch
7603 * @return the index of the first mismatch between the two arrays,
7604 * otherwise {@code -1}.
7605 * @throws NullPointerException
7606 * if either array is {@code null}
7607 * @since 9
7608 */
7609 public static int mismatch(boolean[] a, boolean[] b) {
7610 int length = Math.min(a.length, b.length); // Check null array refs
7611 if (a == b)
7612 return -1;
7613
7614 int i = ArraysSupport.mismatch(a, b, length);
7615 return (i < 0 && a.length != b.length) ? length : i;
7616 }
7617
7618 /**
7619 * Finds and returns the relative index of the first mismatch between two
7620 * {@code boolean} arrays over the specified ranges, otherwise return -1 if
7621 * no mismatch is found. The index will be in the range of 0 (inclusive) up
7622 * to the length (inclusive) of the smaller range.
7623 *
7624 * <p>If the two arrays, over the specified ranges, share a common prefix
7625 * then the returned relative index is the length of the common prefix and
7626 * it follows that there is a mismatch between the two elements at that
7627 * relative index within the respective arrays.
7628 * If one array is a proper prefix of the other, over the specified ranges,
7629 * then the returned relative index is the length of the smaller range and
7630 * it follows that the relative index is only valid for the array with the
7631 * larger range.
7632 * Otherwise, there is no mismatch.
7633 *
7634 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7635 * ranges [{@code aFromIndex}, {@code atoIndex}) and
7636 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
7637 * prefix of length {@code pl} if the following expression is true:
7638 * <pre>{@code
7639 * pl >= 0 &&
7640 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
7641 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
7642 * a[aFromIndex + pl] != b[bFromIndex + pl]
7643 * }</pre>
7644 * Note that a common prefix length of {@code 0} indicates that the first
7645 * elements from each array mismatch.
7646 *
7647 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7648 * ranges [{@code aFromIndex}, {@code atoIndex}) and
7649 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
7650 * if the following expression is true:
7651 * <pre>{@code
7652 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
7653 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
7654 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7655 * }</pre>
7656 *
7657 * @param a the first array to be tested for a mismatch
7658 * @param aFromIndex the index (inclusive) of the first element in the
7659 * first array to be tested
7660 * @param aToIndex the index (exclusive) of the last element in the
7661 * first array to be tested
7662 * @param b the second array to be tested for a mismatch
7663 * @param bFromIndex the index (inclusive) of the first element in the
7664 * second array to be tested
7665 * @param bToIndex the index (exclusive) of the last element in the
7666 * second array to be tested
7667 * @return the relative index of the first mismatch between the two arrays
7668 * over the specified ranges, otherwise {@code -1}.
7669 * @throws IllegalArgumentException
7670 * if {@code aFromIndex > aToIndex} or
7671 * if {@code bFromIndex > bToIndex}
7672 * @throws ArrayIndexOutOfBoundsException
7673 * if {@code aFromIndex < 0 or aToIndex > a.length} or
7674 * if {@code bFromIndex < 0 or bToIndex > b.length}
7675 * @throws NullPointerException
7676 * if either array is {@code null}
7677 * @since 9
7678 */
7679 public static int mismatch(boolean[] a, int aFromIndex, int aToIndex,
7680 boolean[] b, int bFromIndex, int bToIndex) {
7681 rangeCheck(a.length, aFromIndex, aToIndex);
7682 rangeCheck(b.length, bFromIndex, bToIndex);
7683
7684 int aLength = aToIndex - aFromIndex;
7685 int bLength = bToIndex - bFromIndex;
7686 int length = Math.min(aLength, bLength);
7687 int i = ArraysSupport.mismatch(a, aFromIndex,
7688 b, bFromIndex,
7689 length);
7690 return (i < 0 && aLength != bLength) ? length : i;
7691 }
7692
7693 // Mismatch byte
7694
7695 /**
7696 * Finds and returns the index of the first mismatch between two {@code byte}
7697 * arrays, otherwise return -1 if no mismatch is found. The index will be
7698 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
7699 * array.
7700 *
7701 * <p>If the two arrays share a common prefix then the returned index is the
7702 * length of the common prefix and it follows that there is a mismatch
7703 * between the two elements at that index within the respective arrays.
7704 * If one array is a proper prefix of the other then the returned index is
7705 * the length of the smaller array and it follows that the index is only
7706 * valid for the larger array.
7707 * Otherwise, there is no mismatch.
7708 *
7709 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7710 * prefix of length {@code pl} if the following expression is true:
7711 * <pre>{@code
7712 * pl >= 0 &&
7713 * pl < Math.min(a.length, b.length) &&
7714 * Arrays.equals(a, 0, pl, b, 0, pl) &&
7715 * a[pl] != b[pl]
7716 * }</pre>
7717 * Note that a common prefix length of {@code 0} indicates that the first
7718 * elements from each array mismatch.
7719 *
7720 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7721 * prefix if the following expression is true:
7722 * <pre>{@code
7723 * a.length != b.length &&
7724 * Arrays.equals(a, 0, Math.min(a.length, b.length),
7725 * b, 0, Math.min(a.length, b.length))
7726 * }</pre>
7727 *
7728 * @param a the first array to be tested for a mismatch
7729 * @param b the second array to be tested for a mismatch
7730 * @return the index of the first mismatch between the two arrays,
7731 * otherwise {@code -1}.
7732 * @throws NullPointerException
7733 * if either array is {@code null}
7734 * @since 9
7735 */
7736 public static int mismatch(byte[] a, byte[] b) {
7737 int length = Math.min(a.length, b.length); // Check null array refs
7738 if (a == b)
7739 return -1;
7740
7741 int i = ArraysSupport.mismatch(a, b, length);
7742 return (i < 0 && a.length != b.length) ? length : i;
7743 }
7744
7745 /**
7746 * Finds and returns the relative index of the first mismatch between two
7747 * {@code byte} arrays over the specified ranges, otherwise return -1 if no
7748 * mismatch is found. The index will be in the range of 0 (inclusive) up to
7749 * the length (inclusive) of the smaller range.
7750 *
7751 * <p>If the two arrays, over the specified ranges, share a common prefix
7752 * then the returned relative index is the length of the common prefix and
7753 * it follows that there is a mismatch between the two elements at that
7754 * relative index within the respective arrays.
7755 * If one array is a proper prefix of the other, over the specified ranges,
7756 * then the returned relative index is the length of the smaller range and
7757 * it follows that the relative index is only valid for the array with the
7758 * larger range.
7759 * Otherwise, there is no mismatch.
7760 *
7761 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7762 * ranges [{@code aFromIndex}, {@code atoIndex}) and
7763 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
7764 * prefix of length {@code pl} if the following expression is true:
7765 * <pre>{@code
7766 * pl >= 0 &&
7767 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
7768 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
7769 * a[aFromIndex + pl] != b[bFromIndex + pl]
7770 * }</pre>
7771 * Note that a common prefix length of {@code 0} indicates that the first
7772 * elements from each array mismatch.
7773 *
7774 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7775 * ranges [{@code aFromIndex}, {@code atoIndex}) and
7776 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
7777 * if the following expression is true:
7778 * <pre>{@code
7779 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
7780 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
7781 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7782 * }</pre>
7783 *
7784 * @param a the first array to be tested for a mismatch
7785 * @param aFromIndex the index (inclusive) of the first element in the
7786 * first array to be tested
7787 * @param aToIndex the index (exclusive) of the last element in the
7788 * first array to be tested
7789 * @param b the second array to be tested for a mismatch
7790 * @param bFromIndex the index (inclusive) of the first element in the
7791 * second array to be tested
7792 * @param bToIndex the index (exclusive) of the last element in the
7793 * second array to be tested
7794 * @return the relative index of the first mismatch between the two arrays
7795 * over the specified ranges, otherwise {@code -1}.
7796 * @throws IllegalArgumentException
7797 * if {@code aFromIndex > aToIndex} or
7798 * if {@code bFromIndex > bToIndex}
7799 * @throws ArrayIndexOutOfBoundsException
7800 * if {@code aFromIndex < 0 or aToIndex > a.length} or
7801 * if {@code bFromIndex < 0 or bToIndex > b.length}
7802 * @throws NullPointerException
7803 * if either array is {@code null}
7804 * @since 9
7805 */
7806 public static int mismatch(byte[] a, int aFromIndex, int aToIndex,
7807 byte[] b, int bFromIndex, int bToIndex) {
7808 rangeCheck(a.length, aFromIndex, aToIndex);
7809 rangeCheck(b.length, bFromIndex, bToIndex);
7810
7811 int aLength = aToIndex - aFromIndex;
7812 int bLength = bToIndex - bFromIndex;
7813 int length = Math.min(aLength, bLength);
7814 int i = ArraysSupport.mismatch(a, aFromIndex,
7815 b, bFromIndex,
7816 length);
7817 return (i < 0 && aLength != bLength) ? length : i;
7818 }
7819
7820 // Mismatch char
7821
7822 /**
7823 * Finds and returns the index of the first mismatch between two {@code char}
7824 * arrays, otherwise return -1 if no mismatch is found. The index will be
7825 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
7826 * array.
7827 *
7828 * <p>If the two arrays share a common prefix then the returned index is the
7829 * length of the common prefix and it follows that there is a mismatch
7830 * between the two elements at that index within the respective arrays.
7831 * If one array is a proper prefix of the other then the returned index is
7832 * the length of the smaller array and it follows that the index is only
7833 * valid for the larger array.
7834 * Otherwise, there is no mismatch.
7835 *
7836 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7837 * prefix of length {@code pl} if the following expression is true:
7838 * <pre>{@code
7839 * pl >= 0 &&
7840 * pl < Math.min(a.length, b.length) &&
7841 * Arrays.equals(a, 0, pl, b, 0, pl) &&
7842 * a[pl] != b[pl]
7843 * }</pre>
7844 * Note that a common prefix length of {@code 0} indicates that the first
7845 * elements from each array mismatch.
7846 *
7847 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7848 * prefix if the following expression is true:
7849 * <pre>{@code
7850 * a.length != b.length &&
7851 * Arrays.equals(a, 0, Math.min(a.length, b.length),
7852 * b, 0, Math.min(a.length, b.length))
7853 * }</pre>
7854 *
7855 * @param a the first array to be tested for a mismatch
7856 * @param b the second array to be tested for a mismatch
7857 * @return the index of the first mismatch between the two arrays,
7858 * otherwise {@code -1}.
7859 * @throws NullPointerException
7860 * if either array is {@code null}
7861 * @since 9
7862 */
7863 public static int mismatch(char[] a, char[] b) {
7864 int length = Math.min(a.length, b.length); // Check null array refs
7865 if (a == b)
7866 return -1;
7867
7868 int i = ArraysSupport.mismatch(a, b, length);
7869 return (i < 0 && a.length != b.length) ? length : i;
7870 }
7871
7872 /**
7873 * Finds and returns the relative index of the first mismatch between two
7874 * {@code char} arrays over the specified ranges, otherwise return -1 if no
7875 * mismatch is found. The index will be in the range of 0 (inclusive) up to
7876 * the length (inclusive) of the smaller range.
7877 *
7878 * <p>If the two arrays, over the specified ranges, share a common prefix
7879 * then the returned relative index is the length of the common prefix and
7880 * it follows that there is a mismatch between the two elements at that
7881 * relative index within the respective arrays.
7882 * If one array is a proper prefix of the other, over the specified ranges,
7883 * then the returned relative index is the length of the smaller range and
7884 * it follows that the relative index is only valid for the array with the
7885 * larger range.
7886 * Otherwise, there is no mismatch.
7887 *
7888 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7889 * ranges [{@code aFromIndex}, {@code atoIndex}) and
7890 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
7891 * prefix of length {@code pl} if the following expression is true:
7892 * <pre>{@code
7893 * pl >= 0 &&
7894 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
7895 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
7896 * a[aFromIndex + pl] != b[bFromIndex + pl]
7897 * }</pre>
7898 * Note that a common prefix length of {@code 0} indicates that the first
7899 * elements from each array mismatch.
7900 *
7901 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
7902 * ranges [{@code aFromIndex}, {@code atoIndex}) and
7903 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
7904 * if the following expression is true:
7905 * <pre>{@code
7906 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
7907 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
7908 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
7909 * }</pre>
7910 *
7911 * @param a the first array to be tested for a mismatch
7912 * @param aFromIndex the index (inclusive) of the first element in the
7913 * first array to be tested
7914 * @param aToIndex the index (exclusive) of the last element in the
7915 * first array to be tested
7916 * @param b the second array to be tested for a mismatch
7917 * @param bFromIndex the index (inclusive) of the first element in the
7918 * second array to be tested
7919 * @param bToIndex the index (exclusive) of the last element in the
7920 * second array to be tested
7921 * @return the relative index of the first mismatch between the two arrays
7922 * over the specified ranges, otherwise {@code -1}.
7923 * @throws IllegalArgumentException
7924 * if {@code aFromIndex > aToIndex} or
7925 * if {@code bFromIndex > bToIndex}
7926 * @throws ArrayIndexOutOfBoundsException
7927 * if {@code aFromIndex < 0 or aToIndex > a.length} or
7928 * if {@code bFromIndex < 0 or bToIndex > b.length}
7929 * @throws NullPointerException
7930 * if either array is {@code null}
7931 * @since 9
7932 */
7933 public static int mismatch(char[] a, int aFromIndex, int aToIndex,
7934 char[] b, int bFromIndex, int bToIndex) {
7935 rangeCheck(a.length, aFromIndex, aToIndex);
7936 rangeCheck(b.length, bFromIndex, bToIndex);
7937
7938 int aLength = aToIndex - aFromIndex;
7939 int bLength = bToIndex - bFromIndex;
7940 int length = Math.min(aLength, bLength);
7941 int i = ArraysSupport.mismatch(a, aFromIndex,
7942 b, bFromIndex,
7943 length);
7944 return (i < 0 && aLength != bLength) ? length : i;
7945 }
7946
7947 // Mismatch short
7948
7949 /**
7950 * Finds and returns the index of the first mismatch between two {@code short}
7951 * arrays, otherwise return -1 if no mismatch is found. The index will be
7952 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
7953 * array.
7954 *
7955 * <p>If the two arrays share a common prefix then the returned index is the
7956 * length of the common prefix and it follows that there is a mismatch
7957 * between the two elements at that index within the respective arrays.
7958 * If one array is a proper prefix of the other then the returned index is
7959 * the length of the smaller array and it follows that the index is only
7960 * valid for the larger array.
7961 * Otherwise, there is no mismatch.
7962 *
7963 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
7964 * prefix of length {@code pl} if the following expression is true:
7965 * <pre>{@code
7966 * pl >= 0 &&
7967 * pl < Math.min(a.length, b.length) &&
7968 * Arrays.equals(a, 0, pl, b, 0, pl) &&
7969 * a[pl] != b[pl]
7970 * }</pre>
7971 * Note that a common prefix length of {@code 0} indicates that the first
7972 * elements from each array mismatch.
7973 *
7974 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
7975 * prefix if the following expression is true:
7976 * <pre>{@code
7977 * a.length != b.length &&
7978 * Arrays.equals(a, 0, Math.min(a.length, b.length),
7979 * b, 0, Math.min(a.length, b.length))
7980 * }</pre>
7981 *
7982 * @param a the first array to be tested for a mismatch
7983 * @param b the second array to be tested for a mismatch
7984 * @return the index of the first mismatch between the two arrays,
7985 * otherwise {@code -1}.
7986 * @throws NullPointerException
7987 * if either array is {@code null}
7988 * @since 9
7989 */
7990 public static int mismatch(short[] a, short[] b) {
7991 int length = Math.min(a.length, b.length); // Check null array refs
7992 if (a == b)
7993 return -1;
7994
7995 int i = ArraysSupport.mismatch(a, b, length);
7996 return (i < 0 && a.length != b.length) ? length : i;
7997 }
7998
7999 /**
8000 * Finds and returns the relative index of the first mismatch between two
8001 * {@code short} arrays over the specified ranges, otherwise return -1 if no
8002 * mismatch is found. The index will be in the range of 0 (inclusive) up to
8003 * the length (inclusive) of the smaller range.
8004 *
8005 * <p>If the two arrays, over the specified ranges, share a common prefix
8006 * then the returned relative index is the length of the common prefix and
8007 * it follows that there is a mismatch between the two elements at that
8008 * relative index within the respective arrays.
8009 * If one array is a proper prefix of the other, over the specified ranges,
8010 * then the returned relative index is the length of the smaller range and
8011 * it follows that the relative index is only valid for the array with the
8012 * larger range.
8013 * Otherwise, there is no mismatch.
8014 *
8015 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8016 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8017 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8018 * prefix of length {@code pl} if the following expression is true:
8019 * <pre>{@code
8020 * pl >= 0 &&
8021 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8022 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8023 * a[aFromIndex + pl] != b[bFromIndex + pl]
8024 * }</pre>
8025 * Note that a common prefix length of {@code 0} indicates that the first
8026 * elements from each array mismatch.
8027 *
8028 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8029 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8030 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8031 * if the following expression is true:
8032 * <pre>{@code
8033 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8034 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8035 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8036 * }</pre>
8037 *
8038 * @param a the first array to be tested for a mismatch
8039 * @param aFromIndex the index (inclusive) of the first element in the
8040 * first array to be tested
8041 * @param aToIndex the index (exclusive) of the last element in the
8042 * first array to be tested
8043 * @param b the second array to be tested for a mismatch
8044 * @param bFromIndex the index (inclusive) of the first element in the
8045 * second array to be tested
8046 * @param bToIndex the index (exclusive) of the last element in the
8047 * second array to be tested
8048 * @return the relative index of the first mismatch between the two arrays
8049 * over the specified ranges, otherwise {@code -1}.
8050 * @throws IllegalArgumentException
8051 * if {@code aFromIndex > aToIndex} or
8052 * if {@code bFromIndex > bToIndex}
8053 * @throws ArrayIndexOutOfBoundsException
8054 * if {@code aFromIndex < 0 or aToIndex > a.length} or
8055 * if {@code bFromIndex < 0 or bToIndex > b.length}
8056 * @throws NullPointerException
8057 * if either array is {@code null}
8058 * @since 9
8059 */
8060 public static int mismatch(short[] a, int aFromIndex, int aToIndex,
8061 short[] b, int bFromIndex, int bToIndex) {
8062 rangeCheck(a.length, aFromIndex, aToIndex);
8063 rangeCheck(b.length, bFromIndex, bToIndex);
8064
8065 int aLength = aToIndex - aFromIndex;
8066 int bLength = bToIndex - bFromIndex;
8067 int length = Math.min(aLength, bLength);
8068 int i = ArraysSupport.mismatch(a, aFromIndex,
8069 b, bFromIndex,
8070 length);
8071 return (i < 0 && aLength != bLength) ? length : i;
8072 }
8073
8074 // Mismatch int
8075
8076 /**
8077 * Finds and returns the index of the first mismatch between two {@code int}
8078 * arrays, otherwise return -1 if no mismatch is found. The index will be
8079 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
8080 * array.
8081 *
8082 * <p>If the two arrays share a common prefix then the returned index is the
8083 * length of the common prefix and it follows that there is a mismatch
8084 * between the two elements at that index within the respective arrays.
8085 * If one array is a proper prefix of the other then the returned index is
8086 * the length of the smaller array and it follows that the index is only
8087 * valid for the larger array.
8088 * Otherwise, there is no mismatch.
8089 *
8090 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8091 * prefix of length {@code pl} if the following expression is true:
8092 * <pre>{@code
8093 * pl >= 0 &&
8094 * pl < Math.min(a.length, b.length) &&
8095 * Arrays.equals(a, 0, pl, b, 0, pl) &&
8096 * a[pl] != b[pl]
8097 * }</pre>
8098 * Note that a common prefix length of {@code 0} indicates that the first
8099 * elements from each array mismatch.
8100 *
8101 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8102 * prefix if the following expression is true:
8103 * <pre>{@code
8104 * a.length != b.length &&
8105 * Arrays.equals(a, 0, Math.min(a.length, b.length),
8106 * b, 0, Math.min(a.length, b.length))
8107 * }</pre>
8108 *
8109 * @param a the first array to be tested for a mismatch
8110 * @param b the second array to be tested for a mismatch
8111 * @return the index of the first mismatch between the two arrays,
8112 * otherwise {@code -1}.
8113 * @throws NullPointerException
8114 * if either array is {@code null}
8115 * @since 9
8116 */
8117 public static int mismatch(int[] a, int[] b) {
8118 int length = Math.min(a.length, b.length); // Check null array refs
8119 if (a == b)
8120 return -1;
8121
8122 int i = ArraysSupport.mismatch(a, b, length);
8123 return (i < 0 && a.length != b.length) ? length : i;
8124 }
8125
8126 /**
8127 * Finds and returns the relative index of the first mismatch between two
8128 * {@code int} arrays over the specified ranges, otherwise return -1 if no
8129 * mismatch is found. The index will be in the range of 0 (inclusive) up to
8130 * the length (inclusive) of the smaller range.
8131 *
8132 * <p>If the two arrays, over the specified ranges, share a common prefix
8133 * then the returned relative index is the length of the common prefix and
8134 * it follows that there is a mismatch between the two elements at that
8135 * relative index within the respective arrays.
8136 * If one array is a proper prefix of the other, over the specified ranges,
8137 * then the returned relative index is the length of the smaller range and
8138 * it follows that the relative index is only valid for the array with the
8139 * larger range.
8140 * Otherwise, there is no mismatch.
8141 *
8142 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8143 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8144 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8145 * prefix of length {@code pl} if the following expression is true:
8146 * <pre>{@code
8147 * pl >= 0 &&
8148 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8149 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8150 * a[aFromIndex + pl] != b[bFromIndex + pl]
8151 * }</pre>
8152 * Note that a common prefix length of {@code 0} indicates that the first
8153 * elements from each array mismatch.
8154 *
8155 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8156 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8157 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8158 * if the following expression is true:
8159 * <pre>{@code
8160 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8161 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8162 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8163 * }</pre>
8164 *
8165 * @param a the first array to be tested for a mismatch
8166 * @param aFromIndex the index (inclusive) of the first element in the
8167 * first array to be tested
8168 * @param aToIndex the index (exclusive) of the last element in the
8169 * first array to be tested
8170 * @param b the second array to be tested for a mismatch
8171 * @param bFromIndex the index (inclusive) of the first element in the
8172 * second array to be tested
8173 * @param bToIndex the index (exclusive) of the last element in the
8174 * second array to be tested
8175 * @return the relative index of the first mismatch between the two arrays
8176 * over the specified ranges, otherwise {@code -1}.
8177 * @throws IllegalArgumentException
8178 * if {@code aFromIndex > aToIndex} or
8179 * if {@code bFromIndex > bToIndex}
8180 * @throws ArrayIndexOutOfBoundsException
8181 * if {@code aFromIndex < 0 or aToIndex > a.length} or
8182 * if {@code bFromIndex < 0 or bToIndex > b.length}
8183 * @throws NullPointerException
8184 * if either array is {@code null}
8185 * @since 9
8186 */
8187 public static int mismatch(int[] a, int aFromIndex, int aToIndex,
8188 int[] b, int bFromIndex, int bToIndex) {
8189 rangeCheck(a.length, aFromIndex, aToIndex);
8190 rangeCheck(b.length, bFromIndex, bToIndex);
8191
8192 int aLength = aToIndex - aFromIndex;
8193 int bLength = bToIndex - bFromIndex;
8194 int length = Math.min(aLength, bLength);
8195 int i = ArraysSupport.mismatch(a, aFromIndex,
8196 b, bFromIndex,
8197 length);
8198 return (i < 0 && aLength != bLength) ? length : i;
8199 }
8200
8201 // Mismatch long
8202
8203 /**
8204 * Finds and returns the index of the first mismatch between two {@code long}
8205 * arrays, otherwise return -1 if no mismatch is found. The index will be
8206 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
8207 * array.
8208 *
8209 * <p>If the two arrays share a common prefix then the returned index is the
8210 * length of the common prefix and it follows that there is a mismatch
8211 * between the two elements at that index within the respective arrays.
8212 * If one array is a proper prefix of the other then the returned index is
8213 * the length of the smaller array and it follows that the index is only
8214 * valid for the larger array.
8215 * Otherwise, there is no mismatch.
8216 *
8217 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8218 * prefix of length {@code pl} if the following expression is true:
8219 * <pre>{@code
8220 * pl >= 0 &&
8221 * pl < Math.min(a.length, b.length) &&
8222 * Arrays.equals(a, 0, pl, b, 0, pl) &&
8223 * a[pl] != b[pl]
8224 * }</pre>
8225 * Note that a common prefix length of {@code 0} indicates that the first
8226 * elements from each array mismatch.
8227 *
8228 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8229 * prefix if the following expression is true:
8230 * <pre>{@code
8231 * a.length != b.length &&
8232 * Arrays.equals(a, 0, Math.min(a.length, b.length),
8233 * b, 0, Math.min(a.length, b.length))
8234 * }</pre>
8235 *
8236 * @param a the first array to be tested for a mismatch
8237 * @param b the second array to be tested for a mismatch
8238 * @return the index of the first mismatch between the two arrays,
8239 * otherwise {@code -1}.
8240 * @throws NullPointerException
8241 * if either array is {@code null}
8242 * @since 9
8243 */
8244 public static int mismatch(long[] a, long[] b) {
8245 int length = Math.min(a.length, b.length); // Check null array refs
8246 if (a == b)
8247 return -1;
8248
8249 int i = ArraysSupport.mismatch(a, b, length);
8250 return (i < 0 && a.length != b.length) ? length : i;
8251 }
8252
8253 /**
8254 * Finds and returns the relative index of the first mismatch between two
8255 * {@code long} arrays over the specified ranges, otherwise return -1 if no
8256 * mismatch is found. The index will be in the range of 0 (inclusive) up to
8257 * the length (inclusive) of the smaller range.
8258 *
8259 * <p>If the two arrays, over the specified ranges, share a common prefix
8260 * then the returned relative index is the length of the common prefix and
8261 * it follows that there is a mismatch between the two elements at that
8262 * relative index within the respective arrays.
8263 * If one array is a proper prefix of the other, over the specified ranges,
8264 * then the returned relative index is the length of the smaller range and
8265 * it follows that the relative index is only valid for the array with the
8266 * larger range.
8267 * Otherwise, there is no mismatch.
8268 *
8269 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8270 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8271 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8272 * prefix of length {@code pl} if the following expression is true:
8273 * <pre>{@code
8274 * pl >= 0 &&
8275 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8276 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8277 * a[aFromIndex + pl] != b[bFromIndex + pl]
8278 * }</pre>
8279 * Note that a common prefix length of {@code 0} indicates that the first
8280 * elements from each array mismatch.
8281 *
8282 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8283 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8284 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8285 * if the following expression is true:
8286 * <pre>{@code
8287 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8288 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8289 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8290 * }</pre>
8291 *
8292 * @param a the first array to be tested for a mismatch
8293 * @param aFromIndex the index (inclusive) of the first element in the
8294 * first array to be tested
8295 * @param aToIndex the index (exclusive) of the last element in the
8296 * first array to be tested
8297 * @param b the second array to be tested for a mismatch
8298 * @param bFromIndex the index (inclusive) of the first element in the
8299 * second array to be tested
8300 * @param bToIndex the index (exclusive) of the last element in the
8301 * second array to be tested
8302 * @return the relative index of the first mismatch between the two arrays
8303 * over the specified ranges, otherwise {@code -1}.
8304 * @throws IllegalArgumentException
8305 * if {@code aFromIndex > aToIndex} or
8306 * if {@code bFromIndex > bToIndex}
8307 * @throws ArrayIndexOutOfBoundsException
8308 * if {@code aFromIndex < 0 or aToIndex > a.length} or
8309 * if {@code bFromIndex < 0 or bToIndex > b.length}
8310 * @throws NullPointerException
8311 * if either array is {@code null}
8312 * @since 9
8313 */
8314 public static int mismatch(long[] a, int aFromIndex, int aToIndex,
8315 long[] b, int bFromIndex, int bToIndex) {
8316 rangeCheck(a.length, aFromIndex, aToIndex);
8317 rangeCheck(b.length, bFromIndex, bToIndex);
8318
8319 int aLength = aToIndex - aFromIndex;
8320 int bLength = bToIndex - bFromIndex;
8321 int length = Math.min(aLength, bLength);
8322 int i = ArraysSupport.mismatch(a, aFromIndex,
8323 b, bFromIndex,
8324 length);
8325 return (i < 0 && aLength != bLength) ? length : i;
8326 }
8327
8328 // Mismatch float
8329
8330 /**
8331 * Finds and returns the index of the first mismatch between two {@code float}
8332 * arrays, otherwise return -1 if no mismatch is found. The index will be
8333 * in the range of 0 (inclusive) up to the length (inclusive) of the smaller
8334 * array.
8335 *
8336 * <p>If the two arrays share a common prefix then the returned index is the
8337 * length of the common prefix and it follows that there is a mismatch
8338 * between the two elements at that index within the respective arrays.
8339 * If one array is a proper prefix of the other then the returned index is
8340 * the length of the smaller array and it follows that the index is only
8341 * valid for the larger array.
8342 * Otherwise, there is no mismatch.
8343 *
8344 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8345 * prefix of length {@code pl} if the following expression is true:
8346 * <pre>{@code
8347 * pl >= 0 &&
8348 * pl < Math.min(a.length, b.length) &&
8349 * Arrays.equals(a, 0, pl, b, 0, pl) &&
8350 * Float.compare(a[pl], b[pl]) != 0
8351 * }</pre>
8352 * Note that a common prefix length of {@code 0} indicates that the first
8353 * elements from each array mismatch.
8354 *
8355 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8356 * prefix if the following expression is true:
8357 * <pre>{@code
8358 * a.length != b.length &&
8359 * Arrays.equals(a, 0, Math.min(a.length, b.length),
8360 * b, 0, Math.min(a.length, b.length))
8361 * }</pre>
8362 *
8363 * @param a the first array to be tested for a mismatch
8364 * @param b the second array to be tested for a mismatch
8365 * @return the index of the first mismatch between the two arrays,
8366 * otherwise {@code -1}.
8367 * @throws NullPointerException
8368 * if either array is {@code null}
8369 * @since 9
8370 */
8371 public static int mismatch(float[] a, float[] b) {
8372 int length = Math.min(a.length, b.length); // Check null array refs
8373 if (a == b)
8374 return -1;
8375
8376 int i = ArraysSupport.mismatch(a, b, length);
8377 return (i < 0 && a.length != b.length) ? length : i;
8378 }
8379
8380 /**
8381 * Finds and returns the relative index of the first mismatch between two
8382 * {@code float} arrays over the specified ranges, otherwise return -1 if no
8383 * mismatch is found. The index will be in the range of 0 (inclusive) up to
8384 * the length (inclusive) of the smaller range.
8385 *
8386 * <p>If the two arrays, over the specified ranges, share a common prefix
8387 * then the returned relative index is the length of the common prefix and
8388 * it follows that there is a mismatch between the two elements at that
8389 * relative index within the respective arrays.
8390 * If one array is a proper prefix of the other, over the specified ranges,
8391 * then the returned relative index is the length of the smaller range and
8392 * it follows that the relative index is only valid for the array with the
8393 * larger range.
8394 * Otherwise, there is no mismatch.
8395 *
8396 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8397 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8398 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8399 * prefix of length {@code pl} if the following expression is true:
8400 * <pre>{@code
8401 * pl >= 0 &&
8402 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8403 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8404 * Float.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
8405 * }</pre>
8406 * Note that a common prefix length of {@code 0} indicates that the first
8407 * elements from each array mismatch.
8408 *
8409 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8410 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8411 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8412 * if the following expression is true:
8413 * <pre>{@code
8414 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8415 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8416 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8417 * }</pre>
8418 *
8419 * @param a the first array to be tested for a mismatch
8420 * @param aFromIndex the index (inclusive) of the first element in the
8421 * first array to be tested
8422 * @param aToIndex the index (exclusive) of the last element in the
8423 * first array to be tested
8424 * @param b the second array to be tested for a mismatch
8425 * @param bFromIndex the index (inclusive) of the first element in the
8426 * second array to be tested
8427 * @param bToIndex the index (exclusive) of the last element in the
8428 * second array to be tested
8429 * @return the relative index of the first mismatch between the two arrays
8430 * over the specified ranges, otherwise {@code -1}.
8431 * @throws IllegalArgumentException
8432 * if {@code aFromIndex > aToIndex} or
8433 * if {@code bFromIndex > bToIndex}
8434 * @throws ArrayIndexOutOfBoundsException
8435 * if {@code aFromIndex < 0 or aToIndex > a.length} or
8436 * if {@code bFromIndex < 0 or bToIndex > b.length}
8437 * @throws NullPointerException
8438 * if either array is {@code null}
8439 * @since 9
8440 */
8441 public static int mismatch(float[] a, int aFromIndex, int aToIndex,
8442 float[] b, int bFromIndex, int bToIndex) {
8443 rangeCheck(a.length, aFromIndex, aToIndex);
8444 rangeCheck(b.length, bFromIndex, bToIndex);
8445
8446 int aLength = aToIndex - aFromIndex;
8447 int bLength = bToIndex - bFromIndex;
8448 int length = Math.min(aLength, bLength);
8449 int i = ArraysSupport.mismatch(a, aFromIndex,
8450 b, bFromIndex,
8451 length);
8452 return (i < 0 && aLength != bLength) ? length : i;
8453 }
8454
8455 // Mismatch double
8456
8457 /**
8458 * Finds and returns the index of the first mismatch between two
8459 * {@code double} arrays, otherwise return -1 if no mismatch is found. The
8460 * index will be in the range of 0 (inclusive) up to the length (inclusive)
8461 * of the smaller array.
8462 *
8463 * <p>If the two arrays share a common prefix then the returned index is the
8464 * length of the common prefix and it follows that there is a mismatch
8465 * between the two elements at that index within the respective arrays.
8466 * If one array is a proper prefix of the other then the returned index is
8467 * the length of the smaller array and it follows that the index is only
8468 * valid for the larger array.
8469 * Otherwise, there is no mismatch.
8470 *
8471 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8472 * prefix of length {@code pl} if the following expression is true:
8473 * <pre>{@code
8474 * pl >= 0 &&
8475 * pl < Math.min(a.length, b.length) &&
8476 * Arrays.equals(a, 0, pl, b, 0, pl) &&
8477 * Double.compare(a[pl], b[pl]) != 0
8478 * }</pre>
8479 * Note that a common prefix length of {@code 0} indicates that the first
8480 * elements from each array mismatch.
8481 *
8482 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8483 * prefix if the following expression is true:
8484 * <pre>{@code
8485 * a.length != b.length &&
8486 * Arrays.equals(a, 0, Math.min(a.length, b.length),
8487 * b, 0, Math.min(a.length, b.length))
8488 * }</pre>
8489 *
8490 * @param a the first array to be tested for a mismatch
8491 * @param b the second array to be tested for a mismatch
8492 * @return the index of the first mismatch between the two arrays,
8493 * otherwise {@code -1}.
8494 * @throws NullPointerException
8495 * if either array is {@code null}
8496 * @since 9
8497 */
8498 public static int mismatch(double[] a, double[] b) {
8499 int length = Math.min(a.length, b.length); // Check null array refs
8500 if (a == b)
8501 return -1;
8502
8503 int i = ArraysSupport.mismatch(a, b, length);
8504 return (i < 0 && a.length != b.length) ? length : i;
8505 }
8506
8507 /**
8508 * Finds and returns the relative index of the first mismatch between two
8509 * {@code double} arrays over the specified ranges, otherwise return -1 if
8510 * no mismatch is found. The index will be in the range of 0 (inclusive) up
8511 * to the length (inclusive) of the smaller range.
8512 *
8513 * <p>If the two arrays, over the specified ranges, share a common prefix
8514 * then the returned relative index is the length of the common prefix and
8515 * it follows that there is a mismatch between the two elements at that
8516 * relative index within the respective arrays.
8517 * If one array is a proper prefix of the other, over the specified ranges,
8518 * then the returned relative index is the length of the smaller range and
8519 * it follows that the relative index is only valid for the array with the
8520 * larger range.
8521 * Otherwise, there is no mismatch.
8522 *
8523 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8524 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8525 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8526 * prefix of length {@code pl} if the following expression is true:
8527 * <pre>{@code
8528 * pl >= 0 &&
8529 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8530 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8531 * Double.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
8532 * }</pre>
8533 * Note that a common prefix length of {@code 0} indicates that the first
8534 * elements from each array mismatch.
8535 *
8536 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8537 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8538 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8539 * if the following expression is true:
8540 * <pre>{@code
8541 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8542 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8543 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8544 * }</pre>
8545 *
8546 * @param a the first array to be tested for a mismatch
8547 * @param aFromIndex the index (inclusive) of the first element in the
8548 * first array to be tested
8549 * @param aToIndex the index (exclusive) of the last element in the
8550 * first array to be tested
8551 * @param b the second array to be tested for a mismatch
8552 * @param bFromIndex the index (inclusive) of the first element in the
8553 * second array to be tested
8554 * @param bToIndex the index (exclusive) of the last element in the
8555 * second array to be tested
8556 * @return the relative index of the first mismatch between the two arrays
8557 * over the specified ranges, otherwise {@code -1}.
8558 * @throws IllegalArgumentException
8559 * if {@code aFromIndex > aToIndex} or
8560 * if {@code bFromIndex > bToIndex}
8561 * @throws ArrayIndexOutOfBoundsException
8562 * if {@code aFromIndex < 0 or aToIndex > a.length} or
8563 * if {@code bFromIndex < 0 or bToIndex > b.length}
8564 * @throws NullPointerException
8565 * if either array is {@code null}
8566 * @since 9
8567 */
8568 public static int mismatch(double[] a, int aFromIndex, int aToIndex,
8569 double[] b, int bFromIndex, int bToIndex) {
8570 rangeCheck(a.length, aFromIndex, aToIndex);
8571 rangeCheck(b.length, bFromIndex, bToIndex);
8572
8573 int aLength = aToIndex - aFromIndex;
8574 int bLength = bToIndex - bFromIndex;
8575 int length = Math.min(aLength, bLength);
8576 int i = ArraysSupport.mismatch(a, aFromIndex,
8577 b, bFromIndex,
8578 length);
8579 return (i < 0 && aLength != bLength) ? length : i;
8580 }
8581
8582 // Mismatch objects
8583
8584 /**
8585 * Finds and returns the index of the first mismatch between two
8586 * {@code Object} arrays, otherwise return -1 if no mismatch is found. The
8587 * index will be in the range of 0 (inclusive) up to the length (inclusive)
8588 * of the smaller array.
8589 *
8590 * <p>If the two arrays share a common prefix then the returned index is the
8591 * length of the common prefix and it follows that there is a mismatch
8592 * between the two elements at that index within the respective arrays.
8593 * If one array is a proper prefix of the other then the returned index is
8594 * the length of the smaller array and it follows that the index is only
8595 * valid for the larger array.
8596 * Otherwise, there is no mismatch.
8597 *
8598 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8599 * prefix of length {@code pl} if the following expression is true:
8600 * <pre>{@code
8601 * pl >= 0 &&
8602 * pl < Math.min(a.length, b.length) &&
8603 * Arrays.equals(a, 0, pl, b, 0, pl) &&
8604 * !Objects.equals(a[pl], b[pl])
8605 * }</pre>
8606 * Note that a common prefix length of {@code 0} indicates that the first
8607 * elements from each array mismatch.
8608 *
8609 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8610 * prefix if the following expression is true:
8611 * <pre>{@code
8612 * a.length != b.length &&
8613 * Arrays.equals(a, 0, Math.min(a.length, b.length),
8614 * b, 0, Math.min(a.length, b.length))
8615 * }</pre>
8616 *
8617 * @param a the first array to be tested for a mismatch
8618 * @param b the second array to be tested for a mismatch
8619 * @return the index of the first mismatch between the two arrays,
8620 * otherwise {@code -1}.
8621 * @throws NullPointerException
8622 * if either array is {@code null}
8623 * @since 9
8624 */
8625 public static int mismatch(Object[] a, Object[] b) {
8626 int length = Math.min(a.length, b.length); // Check null array refs
8627 if (a == b)
8628 return -1;
8629
8630 for (int i = 0; i < length; i++) {
8631 if (!Objects.equals(a[i], b[i]))
8632 return i;
8633 }
8634
8635 return a.length != b.length ? length : -1;
8636 }
8637
8638 /**
8639 * Finds and returns the relative index of the first mismatch between two
8640 * {@code Object} arrays over the specified ranges, otherwise return -1 if
8641 * no mismatch is found. The index will be in the range of 0 (inclusive) up
8642 * to the length (inclusive) of the smaller range.
8643 *
8644 * <p>If the two arrays, over the specified ranges, share a common prefix
8645 * then the returned relative index is the length of the common prefix and
8646 * it follows that there is a mismatch between the two elements at that
8647 * relative index within the respective arrays.
8648 * If one array is a proper prefix of the other, over the specified ranges,
8649 * then the returned relative index is the length of the smaller range and
8650 * it follows that the relative index is only valid for the array with the
8651 * larger range.
8652 * Otherwise, there is no mismatch.
8653 *
8654 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8655 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8656 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8657 * prefix of length {@code pl} if the following expression is true:
8658 * <pre>{@code
8659 * pl >= 0 &&
8660 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8661 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl) &&
8662 * !Objects.equals(a[aFromIndex + pl], b[bFromIndex + pl])
8663 * }</pre>
8664 * Note that a common prefix length of {@code 0} indicates that the first
8665 * elements from each array mismatch.
8666 *
8667 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8668 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8669 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8670 * if the following expression is true:
8671 * <pre>{@code
8672 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8673 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8674 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
8675 * }</pre>
8676 *
8677 * @param a the first array to be tested for a mismatch
8678 * @param aFromIndex the index (inclusive) of the first element in the
8679 * first array to be tested
8680 * @param aToIndex the index (exclusive) of the last element in the
8681 * first array to be tested
8682 * @param b the second array to be tested for a mismatch
8683 * @param bFromIndex the index (inclusive) of the first element in the
8684 * second array to be tested
8685 * @param bToIndex the index (exclusive) of the last element in the
8686 * second array to be tested
8687 * @return the relative index of the first mismatch between the two arrays
8688 * over the specified ranges, otherwise {@code -1}.
8689 * @throws IllegalArgumentException
8690 * if {@code aFromIndex > aToIndex} or
8691 * if {@code bFromIndex > bToIndex}
8692 * @throws ArrayIndexOutOfBoundsException
8693 * if {@code aFromIndex < 0 or aToIndex > a.length} or
8694 * if {@code bFromIndex < 0 or bToIndex > b.length}
8695 * @throws NullPointerException
8696 * if either array is {@code null}
8697 * @since 9
8698 */
8699 public static int mismatch(
8700 Object[] a, int aFromIndex, int aToIndex,
8701 Object[] b, int bFromIndex, int bToIndex) {
8702 rangeCheck(a.length, aFromIndex, aToIndex);
8703 rangeCheck(b.length, bFromIndex, bToIndex);
8704
8705 int aLength = aToIndex - aFromIndex;
8706 int bLength = bToIndex - bFromIndex;
8707 int length = Math.min(aLength, bLength);
8708 for (int i = 0; i < length; i++) {
8709 if (!Objects.equals(a[aFromIndex++], b[bFromIndex++]))
8710 return i;
8711 }
8712
8713 return aLength != bLength ? length : -1;
8714 }
8715
8716 /**
8717 * Finds and returns the index of the first mismatch between two
8718 * {@code Object} arrays, otherwise return -1 if no mismatch is found.
8719 * The index will be in the range of 0 (inclusive) up to the length
8720 * (inclusive) of the smaller array.
8721 *
8722 * <p>The specified comparator is used to determine if two array elements
8723 * from the each array are not equal.
8724 *
8725 * <p>If the two arrays share a common prefix then the returned index is the
8726 * length of the common prefix and it follows that there is a mismatch
8727 * between the two elements at that index within the respective arrays.
8728 * If one array is a proper prefix of the other then the returned index is
8729 * the length of the smaller array and it follows that the index is only
8730 * valid for the larger array.
8731 * Otherwise, there is no mismatch.
8732 *
8733 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a common
8734 * prefix of length {@code pl} if the following expression is true:
8735 * <pre>{@code
8736 * pl >= 0 &&
8737 * pl < Math.min(a.length, b.length) &&
8738 * Arrays.equals(a, 0, pl, b, 0, pl, cmp)
8739 * cmp.compare(a[pl], b[pl]) != 0
8740 * }</pre>
8741 * Note that a common prefix length of {@code 0} indicates that the first
8742 * elements from each array mismatch.
8743 *
8744 * <p>Two non-{@code null} arrays, {@code a} and {@code b}, share a proper
8745 * prefix if the following expression is true:
8746 * <pre>{@code
8747 * a.length != b.length &&
8748 * Arrays.equals(a, 0, Math.min(a.length, b.length),
8749 * b, 0, Math.min(a.length, b.length),
8750 * cmp)
8751 * }</pre>
8752 *
8753 * @param a the first array to be tested for a mismatch
8754 * @param b the second array to be tested for a mismatch
8755 * @param cmp the comparator to compare array elements
8756 * @param <T> the type of array elements
8757 * @return the index of the first mismatch between the two arrays,
8758 * otherwise {@code -1}.
8759 * @throws NullPointerException
8760 * if either array or the comparator is {@code null}
8761 * @since 9
8762 */
8763 public static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp) {
8764 Objects.requireNonNull(cmp);
8765 int length = Math.min(a.length, b.length); // Check null array refs
8766 if (a == b)
8767 return -1;
8768
8769 for (int i = 0; i < length; i++) {
8770 T oa = a[i];
8771 T ob = b[i];
8772 if (oa != ob) {
8773 // Null-value comparison is deferred to the comparator
8774 int v = cmp.compare(oa, ob);
8775 if (v != 0) {
8776 return i;
8777 }
8778 }
8779 }
8780
8781 return a.length != b.length ? length : -1;
8782 }
8783
8784 /**
8785 * Finds and returns the relative index of the first mismatch between two
8786 * {@code Object} arrays over the specified ranges, otherwise return -1 if
8787 * no mismatch is found. The index will be in the range of 0 (inclusive) up
8788 * to the length (inclusive) of the smaller range.
8789 *
8790 * <p>If the two arrays, over the specified ranges, share a common prefix
8791 * then the returned relative index is the length of the common prefix and
8792 * it follows that there is a mismatch between the two elements at that
8793 * relative index within the respective arrays.
8794 * If one array is a proper prefix of the other, over the specified ranges,
8795 * then the returned relative index is the length of the smaller range and
8796 * it follows that the relative index is only valid for the array with the
8797 * larger range.
8798 * Otherwise, there is no mismatch.
8799 *
8800 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8801 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8802 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a common
8803 * prefix of length {@code pl} if the following expression is true:
8804 * <pre>{@code
8805 * pl >= 0 &&
8806 * pl < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex) &&
8807 * Arrays.equals(a, aFromIndex, aFromIndex + pl, b, bFromIndex, bFromIndex + pl, cmp) &&
8808 * cmp.compare(a[aFromIndex + pl], b[bFromIndex + pl]) != 0
8809 * }</pre>
8810 * Note that a common prefix length of {@code 0} indicates that the first
8811 * elements from each array mismatch.
8812 *
8813 * <p>Two non-{@code null} arrays, {@code a} and {@code b} with specified
8814 * ranges [{@code aFromIndex}, {@code atoIndex}) and
8815 * [{@code bFromIndex}, {@code btoIndex}) respectively, share a proper
8816 * if the following expression is true:
8817 * <pre>{@code
8818 * (aToIndex - aFromIndex) != (bToIndex - bFromIndex) &&
8819 * Arrays.equals(a, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8820 * b, 0, Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex),
8821 * cmp)
8822 * }</pre>
8823 *
8824 * @param a the first array to be tested for a mismatch
8825 * @param aFromIndex the index (inclusive) of the first element in the
8826 * first array to be tested
8827 * @param aToIndex the index (exclusive) of the last element in the
8828 * first array to be tested
8829 * @param b the second array to be tested for a mismatch
8830 * @param bFromIndex the index (inclusive) of the first element in the
8831 * second array to be tested
8832 * @param bToIndex the index (exclusive) of the last element in the
8833 * second array to be tested
8834 * @param cmp the comparator to compare array elements
8835 * @param <T> the type of array elements
8836 * @return the relative index of the first mismatch between the two arrays
8837 * over the specified ranges, otherwise {@code -1}.
8838 * @throws IllegalArgumentException
8839 * if {@code aFromIndex > aToIndex} or
8840 * if {@code bFromIndex > bToIndex}
8841 * @throws ArrayIndexOutOfBoundsException
8842 * if {@code aFromIndex < 0 or aToIndex > a.length} or
8843 * if {@code bFromIndex < 0 or bToIndex > b.length}
8844 * @throws NullPointerException
8845 * if either array or the comparator is {@code null}
8846 * @since 9
8847 */
8848 public static <T> int mismatch(
8849 T[] a, int aFromIndex, int aToIndex,
8850 T[] b, int bFromIndex, int bToIndex,
8851 Comparator<? super T> cmp) {
8852 Objects.requireNonNull(cmp);
8853 rangeCheck(a.length, aFromIndex, aToIndex);
8854 rangeCheck(b.length, bFromIndex, bToIndex);
8855
8856 int aLength = aToIndex - aFromIndex;
8857 int bLength = bToIndex - bFromIndex;
8858 int length = Math.min(aLength, bLength);
8859 for (int i = 0; i < length; i++) {
8860 T oa = a[aFromIndex++];
8861 T ob = b[bFromIndex++];
8862 if (oa != ob) {
8863 // Null-value comparison is deferred to the comparator
8864 int v = cmp.compare(oa, ob);
8865 if (v != 0) {
8866 return i;
8867 }
8868 }
8869 }
8870
8871 return aLength != bLength ? length : -1;
8872 }
8873 }