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