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