Arrays.java revision ff18b5f136f92154f2e05217e3953d10f459e561
151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 34c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.util; 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.lang.reflect.*; 30471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniakimport java.util.concurrent.ForkJoinPool; 31e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.Consumer; 32e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntFunction; 33e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntToDoubleFunction; 34e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntToLongFunction; 35e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmeraimport java.util.function.IntUnaryOperator; 36ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.DoubleStream; 37ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.IntStream; 38ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.LongStream; 39ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.Stream; 40ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkinimport java.util.stream.StreamSupport; 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This class contains various methods for manipulating arrays (such as 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorting and searching). This class also contains a static factory 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that allows arrays to be viewed as lists. 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The methods in this class all throw a {@code NullPointerException}, 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the specified array reference is null, except where noted. 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The documentation for the methods contained in this class includes 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * briefs description of the <i>implementations</i>. Such descriptions should 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be regarded as <i>implementation notes</i>, rather than parts of the 5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>specification</i>. Implementors should feel free to substitute other 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * algorithms, so long as the specification itself is adhered to. (For 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, the algorithm used by {@code sort(Object[])} does not have to be 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a MergeSort, but it does have to be <i>stable</i>.) 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This class is a member of the 5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <a href="{@docRoot}/../technotes/guides/collections/index.html"> 6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Java Collections Framework</a>. 6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Josh Bloch 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Neal Gafter 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author John Rose 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic class Arrays { 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 69471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 70471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The minimum array length below which a parallel sorting 71471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * algorithm will not further partition the sorting task. Using 72471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * smaller sizes typically results in memory contention across 73471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * tasks that makes parallel speedups unlikely. 74471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @hide 75471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 76471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static final int MIN_ARRAY_SORT_GRAN = 1 << 13; 77471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 78471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 79471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 80471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * A comparator that implements the natural ordering of a group of 81471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * mutually comparable elements. May be used when a supplied 82471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * comparator is null. To simplify code-sharing within underlying 83471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * implementations, the compare method only declares type Object 84471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * for its second argument. 85471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 86471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays class implementor's note: It is an empirical matter 87471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * whether ComparableTimSort offers any performance benefit over 88471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * TimSort used with this comparator. If not, you are better off 89471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * deleting or bypassing ComparableTimSort. There is currently no 90471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * empirical case for separating them for parallel sorting, so all 91471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * public Object parallelSort methods use the same comparator 92471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * based implementation. 93471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 94471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak static final class NaturalOrder implements Comparator<Object> { 95471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 96471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public int compare(Object first, Object second) { 97471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak return ((Comparable<Object>)first).compareTo(second); 98471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 99471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak static final NaturalOrder INSTANCE = new NaturalOrder(); 100471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 101471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Suppresses default constructor, ensuring non-instantiability. 10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Arrays() {} 10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting of primitive type arrays. 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(int[] a) { 121c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(int[] a, int fromIndex, int toIndex) { 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 146c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 15951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(long[] a) { 161c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 16351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 16451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(long[] a, int fromIndex, int toIndex) { 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 186c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(short[] a) { 201c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(short[] a, int fromIndex, int toIndex) { 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 226c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(char[] a) { 241c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 25251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(char[] a, int fromIndex, int toIndex) { 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 266c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 26751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(byte[] a) { 281c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1); 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(byte[] a, int fromIndex, int toIndex) { 30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all float 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0f} and {@code Float.NaN} is considered greater than any 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Float.NaN} values are considered equal. 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(float[] a) { 329c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 33251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all float 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0f} and {@code Float.NaN} is considered greater than any 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Float.NaN} values are considered equal. 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 34851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 35651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 35951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 36051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(float[] a, int fromIndex, int toIndex) { 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 362c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 36451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array into ascending numerical order. 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all double 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 37051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 37151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 37251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0d} and {@code Double.NaN} is considered greater than any 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Double.NaN} values are considered equal. 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 38351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 38451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(double[] a) { 385c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0); 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 38951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the array into ascending order. The range 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to be sorted extends from the index {@code fromIndex}, inclusive, to 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the index {@code toIndex}, exclusive. If {@code fromIndex == toIndex}, 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range to be sorted is empty. 39351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The {@code <} relation does not provide a total order on all double 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 39651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * value compares neither less than, greater than, nor equal to any value, 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * even itself. This method uses the total order imposed by the method 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 39951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code 0.0d} and {@code Double.NaN} is considered greater than any 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other value and all {@code Double.NaN} values are considered equal. 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: The sorting algorithm is a Dual-Pivot Quicksort 40351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers O(n log(n)) performance on many data sets that cause other 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * quicksorts to degrade to quadratic performance, and is typically 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * faster than traditional (one-pivot) Quicksort implementations. 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 40851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 40951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element, inclusive, to be sorted 41051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element, exclusive, to be sorted 41151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0} or {@code toIndex > a.length} 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(double[] a, int fromIndex, int toIndex) { 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 418c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 421471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 422471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 423471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 424471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 425471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 426471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 427471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 428471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 429471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 430471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a 431471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 432471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 433471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 434471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 435471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 436471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 437471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 438471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 439471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(byte[] a) { 440471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 441471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 442471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 443471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1); 444471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 445471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJByte.Sorter 446471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new byte[n], 0, n, 0, 447471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 448471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 449471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 450471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 451471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 452471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 453471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 454471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 455471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 456471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 457471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 458471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 459471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 460471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(byte[]) Arrays.sort} 461471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 462471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 463471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(byte[]) Arrays.sort} method. The algorithm requires a working 464471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 465471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 466471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 467471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 468471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 469471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 470471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 471471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 472471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 473471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 474471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 475471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 476471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 477471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 478471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(byte[] a, int fromIndex, int toIndex) { 479471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 480471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 481471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 482471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 483471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1); 484471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 485471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJByte.Sorter 486471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new byte[n], fromIndex, n, 0, 487471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 488471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 489471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 490471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 491471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 492471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 493471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 494471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 495471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 496471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 497471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 498471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 499471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 500471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a 501471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 502471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 503471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 504471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 505471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 506471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 507471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 508471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 509471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(char[] a) { 510471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 511471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 512471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 513471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 514471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 515471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJChar.Sorter 516471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new char[n], 0, n, 0, 517471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 518471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 519471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 520471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 521471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 522471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 523471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 524471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 525471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 526471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 527471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @implNote The sorting algorithm is a parallel sort-merge that breaks the 528471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 529471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 530471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(char[]) Arrays.sort} 531471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 532471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 533471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(char[]) Arrays.sort} method. The algorithm requires a working 534471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 535471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 536471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 537471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 538471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 539471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 540471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 541471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 542471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 543471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 544471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 545471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 546471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 547471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 548471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(char[] a, int fromIndex, int toIndex) { 549471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 550471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 551471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 552471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 553471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 554471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 555471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJChar.Sorter 556471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new char[n], fromIndex, n, 0, 557471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 558471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 559471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 560471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 561471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 562471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 563471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 564471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 565471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 566471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 567471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 568471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 569471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 570471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a 571471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 572471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 573471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 574471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 575471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 576471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 577471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 578471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 579471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(short[] a) { 580471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 581471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 582471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 583471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 584471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 585471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJShort.Sorter 586471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new short[n], 0, n, 0, 587471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 588471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 589471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 590471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 591471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 592471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 593471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 594471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 595471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 596471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 597471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 598471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 599471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 600471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(short[]) Arrays.sort} 601471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 602471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 603471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(short[]) Arrays.sort} method. The algorithm requires a working 604471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 605471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 606471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 607471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 608471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 609471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 610471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 611471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 612471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 613471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 614471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 615471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 616471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 617471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 618471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(short[] a, int fromIndex, int toIndex) { 619471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 620471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 621471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 622471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 623471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 624471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 625471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJShort.Sorter 626471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new short[n], fromIndex, n, 0, 627471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 628471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 629471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 630471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 631471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 632471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 633471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 634471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 635471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 636471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 637471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 638471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 639471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 640471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a 641471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 642471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 643471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 644471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 645471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 646471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 647471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 648471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 649471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(int[] a) { 650471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 651471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 652471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 653471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 654471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 655471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJInt.Sorter 656471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new int[n], 0, n, 0, 657471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 658471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 659471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 660471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 661471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 662471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 663471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 664471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 665471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 666471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 667471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 668471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 669471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 670471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(int[]) Arrays.sort} 671471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 672471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 673471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(int[]) Arrays.sort} method. The algorithm requires a working 674471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 675471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 676471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 677471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 678471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 679471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 680471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 681471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 682471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 683471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 684471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 685471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 686471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 687471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 688471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(int[] a, int fromIndex, int toIndex) { 689471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 690471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 691471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 692471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 693471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 694471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 695471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJInt.Sorter 696471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new int[n], fromIndex, n, 0, 697471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 698471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 699471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 700471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 701471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 702471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 703471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 704471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 705471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 706471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 707471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 708471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 709471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 710471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a 711471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 712471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 713471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 714471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 715471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 716471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 717471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 718471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 719471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(long[] a) { 720471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 721471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 722471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 723471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 724471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 725471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJLong.Sorter 726471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new long[n], 0, n, 0, 727471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 728471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 729471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 730471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 731471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 732471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 733471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 734471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 735471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 736471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 737471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 738471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 739471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 740471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(long[]) Arrays.sort} 741471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 742471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 743471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(long[]) Arrays.sort} method. The algorithm requires a working 744471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 745471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 746471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 747471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 748471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 749471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 750471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 751471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 752471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 753471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 754471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 755471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 756471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 757471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 758471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(long[] a, int fromIndex, int toIndex) { 759471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 760471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 761471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 762471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 763471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 764471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 765471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJLong.Sorter 766471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new long[n], fromIndex, n, 0, 767471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 768471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 769471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 770471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 771471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 772471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 773471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 774471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all float 775471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 776471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 777471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 778471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 779471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0f} and {@code Float.NaN} is considered greater than any 780471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Float.NaN} values are considered equal. 781471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 782471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 783471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 784471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 785471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 786471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 787471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 788471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a 789471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 790471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 791471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 792471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 793471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 794471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 795471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 796471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 797471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(float[] a) { 798471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 799471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 800471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 801471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 802471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 803471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJFloat.Sorter 804471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new float[n], 0, n, 0, 805471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 806471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 807471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 808471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 809471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 810471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 811471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 812471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 813471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 814471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 815471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all float 816471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0f == 0.0f} is {@code true} and a {@code Float.NaN} 817471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 818471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 819471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Float#compareTo}: {@code -0.0f} is treated as less than value 820471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0f} and {@code Float.NaN} is considered greater than any 821471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Float.NaN} values are considered equal. 822471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 823471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 824471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 825471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 826471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(float[]) Arrays.sort} 827471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 828471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 829471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(float[]) Arrays.sort} method. The algorithm requires a working 830471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 831471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 832471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 833471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 834471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 835471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 836471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 837471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 838471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 839471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 840471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 841471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 842471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 843471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 844471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(float[] a, int fromIndex, int toIndex) { 845471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 846471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 847471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 848471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 849471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 850471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 851471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJFloat.Sorter 852471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new float[n], fromIndex, n, 0, 853471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 854471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 855471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 856471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 857471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 858471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array into ascending numerical order. 859471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 860471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all double 861471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 862471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 863471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 864471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 865471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0d} and {@code Double.NaN} is considered greater than any 866471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Double.NaN} values are considered equal. 867471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 868471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 869471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 870471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 871471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 872471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 873471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 874471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a 875471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 876471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 877471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 878471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 879471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 880471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 881471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 882471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 883471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(double[] a) { 884471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 885471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 886471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 887471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, 0, n - 1, null, 0, 0); 888471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 889471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJDouble.Sorter 890471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new double[n], 0, n, 0, 891471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 892471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 893471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 894471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 895471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 896471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the array into ascending numerical order. 897471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * The range to be sorted extends from the index {@code fromIndex}, 898471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * inclusive, to the index {@code toIndex}, exclusive. If 899471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex == toIndex}, the range to be sorted is empty. 900471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 901471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>The {@code <} relation does not provide a total order on all double 902471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * values: {@code -0.0d == 0.0d} is {@code true} and a {@code Double.NaN} 903471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * value compares neither less than, greater than, nor equal to any value, 904471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * even itself. This method uses the total order imposed by the method 905471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Double#compareTo}: {@code -0.0d} is treated as less than value 906471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code 0.0d} and {@code Double.NaN} is considered greater than any 907471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * other value and all {@code Double.NaN} values are considered equal. 908471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 909471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 910471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 911471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 912471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(double[]) Arrays.sort} 913471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 914471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 915471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(double[]) Arrays.sort} method. The algorithm requires a working 916471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 917471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 918471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 919471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 920471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 921471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element, inclusive, to be sorted 922471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element, exclusive, to be sorted 923471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 924471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} 925471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException 926471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * if {@code fromIndex < 0} or {@code toIndex > a.length} 927471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 928471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 929471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 930471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static void parallelSort(double[] a, int fromIndex, int toIndex) { 931471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 932471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 933471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 934471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 935471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0); 936471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 937471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJDouble.Sorter 938471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, new double[n], fromIndex, n, 0, 939471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 940471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g).invoke(); 941471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 942471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 943471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 944471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array of objects into ascending order, according 945471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * to the {@linkplain Comparable natural ordering} of its elements. 946471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * All elements in the array must implement the {@link Comparable} 947471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * interface. Furthermore, all elements in the array must be 948471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 949471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not throw a {@code ClassCastException} for any elements {@code e1} 950471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * and {@code e2} in the array). 951471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 952471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 953471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 954471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 955471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 956471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 957471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 958471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 959471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 960471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 961471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 962471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 963471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 964471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 965471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 966471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 967471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 968471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 969471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are not 970471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> (for example, strings and integers) 971471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException (optional) if the natural 972471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * ordering of the array elements is found to violate the 973471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link Comparable} contract 974471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 975471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 976471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 977471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 978471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T extends Comparable<? super T>> void parallelSort(T[] a) { 979471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 980471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 981471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 982471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, 0, n, NaturalOrder.INSTANCE, null, 0, 0); 983471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 984471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 985471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 986471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 987471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 988471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 989471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 990471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 991471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 992471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the specified array of objects into 993471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * ascending order, according to the 994471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@linkplain Comparable natural ordering} of its 995471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * elements. The range to be sorted extends from index 996471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 997471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 998471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * elements in this range must implement the {@link Comparable} 999471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * interface. Furthermore, all elements in this range must be <i>mutually 1000471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 1001471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code ClassCastException} for any elements {@code e1} and 1002471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code e2} in the array). 1003471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1004471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1005471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 1006471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1007471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1008471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 1009471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 1010471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1011471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 1012471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 1013471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1014471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 1015471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1016471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 1017471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1018471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 1019471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 1020471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element (inclusive) to be 1021471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted 1022471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element (exclusive) to be sorted 1023471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1024471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * (optional) if the natural ordering of the array elements is 1025471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * found to violate the {@link Comparable} contract 1026471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1027471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code toIndex > a.length} 1028471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are 1029471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not <i>mutually comparable</i> (for example, strings and 1030471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * integers). 1031471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1032471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 1033471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 1034471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 1035471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T extends Comparable<? super T>> 1036471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak void parallelSort(T[] a, int fromIndex, int toIndex) { 1037471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 1038471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 1039471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 1040471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1041471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, fromIndex, toIndex, NaturalOrder.INSTANCE, null, 0, 0); 1042471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 1043471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 1044471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 1045471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 1046471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1047471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, NaturalOrder.INSTANCE).invoke(); 1048471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 1049471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 1050471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 1051471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified array of objects according to the order induced by 1052471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the specified comparator. All elements in the array must be 1053471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> by the specified comparator (that is, 1054471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1055471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * for any elements {@code e1} and {@code e2} in the array). 1056471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1057471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1058471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 1059471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1060471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1061471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 1062471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 1063471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1064471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 1065471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 1066471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a 1067471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * working space no greater than the size of the original array. The 1068471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@link ForkJoinPool#commonPool() ForkJoin common pool} is used to 1069471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * execute any parallel tasks. 1070471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1071471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 1072471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 1073471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param cmp the comparator to determine the order of the array. A 1074471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code null} value indicates that the elements' 1075471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@linkplain Comparable natural ordering} should be used. 1076471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are 1077471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not <i>mutually comparable</i> using the specified comparator 1078471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException (optional) if the comparator is 1079471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * found to violate the {@link java.util.Comparator} contract 1080471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1081471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 1082471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 1083471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 1084471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) { 1085471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (cmp == null) 1086471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak cmp = NaturalOrder.INSTANCE; 1087471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = a.length, p, g; 1088471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 1089471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1090471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, 0, n, cmp, null, 0, 0); 1091471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 1092471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 1093471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 1094471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 1095471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 0, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1096471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1097471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 1098471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 1099471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak /** 1100471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Sorts the specified range of the specified array of objects according 1101471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * to the order induced by the specified comparator. The range to be 1102471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted extends from index {@code fromIndex}, inclusive, to index 1103471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 1104471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * range to be sorted is empty.) All elements in the range must be 1105471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <i>mutually comparable</i> by the specified comparator (that is, 1106471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 1107471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * for any elements {@code e1} and {@code e2} in the range). 1108471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1109471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 1110471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not be reordered as a result of the sort. 1111471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1112471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @implNote The sorting algorithm is a parallel sort-merge that breaks the 1113471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array into sub-arrays that are themselves sorted and then merged. When 1114471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * the sub-array length reaches a minimum granularity, the sub-array is 1115471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted using the appropriate {@link Arrays#sort(Object[]) Arrays.sort} 1116471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * method. If the length of the specified array is less than the minimum 1117471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * granularity, then it is sorted using the appropriate {@link 1118471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * Arrays#sort(Object[]) Arrays.sort} method. The algorithm requires a working 1119471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * space no greater than the size of the specified range of the original 1120471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * array. The {@link ForkJoinPool#commonPool() ForkJoin common pool} is 1121471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * used to execute any parallel tasks. 1122471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1123471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param <T> the class of the objects to be sorted 1124471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param a the array to be sorted 1125471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param fromIndex the index of the first element (inclusive) to be 1126471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * sorted 1127471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param toIndex the index of the last element (exclusive) to be sorted 1128471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @param cmp the comparator to determine the order of the array. A 1129471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code null} value indicates that the elements' 1130471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@linkplain Comparable natural ordering} should be used. 1131471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 1132471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * (optional) if the natural ordering of the array elements is 1133471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * found to violate the {@link Comparable} contract 1134471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 1135471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * {@code toIndex > a.length} 1136471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @throws ClassCastException if the array contains elements that are 1137471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * not <i>mutually comparable</i> (for example, strings and 1138471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * integers). 1139471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * 1140471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak * @since 1.8 1141471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak */ 1142471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak @SuppressWarnings("unchecked") 1143471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak public static <T> void parallelSort(T[] a, int fromIndex, int toIndex, 1144471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak Comparator<? super T> cmp) { 1145471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak rangeCheck(a.length, fromIndex, toIndex); 1146471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (cmp == null) 1147471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak cmp = NaturalOrder.INSTANCE; 1148471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak int n = toIndex - fromIndex, p, g; 1149471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak if (n <= MIN_ARRAY_SORT_GRAN || 1150471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (p = ForkJoinPool.getCommonPoolParallelism()) == 1) 1151471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak TimSort.sort(a, fromIndex, toIndex, cmp, null, 0, 0); 1152471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak else 1153471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak new ArraysParallelSortHelpers.FJObject.Sorter<T> 1154471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (null, a, 1155471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak (T[])Array.newInstance(a.getClass().getComponentType(), n), 1156471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak fromIndex, n, 0, ((g = n / (p << 2)) <= MIN_ARRAY_SORT_GRAN) ? 1157471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak MIN_ARRAY_SORT_GRAN : g, cmp).invoke(); 1158471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak } 1159471945701ad7895acf0dae73f90a5f18cd9ca23aPrzemyslaw Szczepaniak 116051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting of complex type arrays. 116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 116451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Old merge sort implementation can be selected (for 116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * compatibility with broken comparators) using a system property. 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Cannot be a static boolean in the enclosing class due to 116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * circular dependencies. To be removed in a future release. 116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static final class LegacyMergeSort { 11711ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski // Android-changed: Never use circular merge sort. 11721ae149ae8db46adac5723c739d53dd82a1cf2e1aPiotr Jastrzebski private static final boolean userRequested = false; 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If this platform has an optimizing VM, check whether ComparableTimSort 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * offers any performance benefit over TimSort in conjunction with a 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * comparator that returns: 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ((Comparable)first).compareTo(Second)}. 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If not, you are better off deleting ComparableTimSort to 118151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * eliminate the code duplication. In other words, the commented 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * out code below is the preferable implementation for sorting 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays of Comparables if it offers sufficient performance. 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// /** 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * A comparator that implements the natural ordering of a group of 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * mutually comparable elements. Using this comparator saves us 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * from duplicating most of the code in this file (one version for 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// * Comparables, one for explicit Comparators). 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// */ 119251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// private static final Comparator<Object> NATURAL_ORDER = 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// new Comparator<Object>() { 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// @SuppressWarnings("unchecked") 119551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// public int compare(Object first, Object second) { 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// return ((Comparable<Object>)first).compareTo(second); 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// } 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// }; 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// public static void sort(Object[] a) { 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// sort(a, 0, a.length, NATURAL_ORDER); 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// } 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// public static void sort(Object[] a, int fromIndex, int toIndex) { 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// sort(a, fromIndex, toIndex, NATURAL_ORDER); 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski// } 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array of objects into ascending order, according 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the {@linkplain Comparable natural ordering} of its elements. 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * All elements in the array must implement the {@link Comparable} 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. Furthermore, all elements in the array must be 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> (that is, {@code e1.compareTo(e2)} must 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not throw a {@code ClassCastException} for any elements {@code e1} 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and {@code e2} in the array). 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 121951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 123251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 123351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 123451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 123551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 123651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 123751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 123851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 123951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 124051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 124151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 124451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are not 124551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> (for example, strings and integers) 124651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException (optional) if the natural 124751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ordering of the array elements is found to violate the 124851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Comparable} contract 124951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 125051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(Object[] a) { 125151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (LegacyMergeSort.userRequested) 125251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski legacyMergeSort(a); 125351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 1254c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath ComparableTimSort.sort(a, 0, a.length, null, 0, 0); 125551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 125651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 125751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 125851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void legacyMergeSort(Object[] a) { 125951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] aux = a.clone(); 126051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, 0, a.length, 0); 126151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 126251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 126351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 126451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the specified array of objects into 126551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending order, according to the 126651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} of its 126751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements. The range to be sorted extends from index 126851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code fromIndex}, inclusive, to index {@code toIndex}, exclusive. 126951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (If {@code fromIndex==toIndex}, the range to be sorted is empty.) All 127051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in this range must implement the {@link Comparable} 127151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interface. Furthermore, all elements in this range must be <i>mutually 127251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * comparable</i> (that is, {@code e1.compareTo(e2)} must not throw a 127351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code ClassCastException} for any elements {@code e1} and 127451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code e2} in the array). 127551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 127751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 127851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 127951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 128051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 128151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 128251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 128351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 128451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 128551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 128651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 128751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 128851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 128951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 129051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 129151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 129251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 129351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 129451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 129551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 129651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 129751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 129851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 129951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 130051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 130151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 130251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 130351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 130451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorted 130551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be sorted 130651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 130751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (optional) if the natural ordering of the array elements is 130851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * found to violate the {@link Comparable} contract 130951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 131051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code toIndex > a.length} 131151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are 131251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not <i>mutually comparable</i> (for example, strings and 131351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * integers). 131451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 131551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void sort(Object[] a, int fromIndex, int toIndex) { 1316c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath rangeCheck(a.length, fromIndex, toIndex); 131751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (LegacyMergeSort.userRequested) 131851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski legacyMergeSort(a, fromIndex, toIndex); 131951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 1320c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath ComparableTimSort.sort(a, fromIndex, toIndex, null, 0, 0); 132151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 132251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 132351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 132451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void legacyMergeSort(Object[] a, 132551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int fromIndex, int toIndex) { 132651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 132751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] aux = copyOfRange(a, fromIndex, toIndex); 132851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 132951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 133051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 133151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 133251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Tuning parameter: list size at or below which insertion sort will be 133351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * used in preference to mergesort. 133451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To be removed in a future release. 133551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 133651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final int INSERTIONSORT_THRESHOLD = 7; 133751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 133851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 133951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Src is the source array that starts at index 0 134051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Dest is the (possibly larger) array destination with a possible offset 134151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * low is the index in dest to start sorting 134251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * high is the end index in dest to end sorting 134351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * off is the offset to generate corresponding low, high in src 134451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To be removed in a future release. 134551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 134651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void mergeSort(Object[] src, 134751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] dest, 134851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low, 134951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high, 135051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int off) { 135151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = high - low; 135251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 135351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Insertion sort on smallest arrays 135451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (length < INSERTIONSORT_THRESHOLD) { 135551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=low; i<high; i++) 135651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int j=i; j>low && 135751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ((Comparable) dest[j-1]).compareTo(dest[j])>0; j--) 135851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski swap(dest, j, j-1); 135951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 136051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 136151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 136251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Recursively sort halves of dest into src 136351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destLow = low; 136451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destHigh = high; 136551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low += off; 136651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high += off; 136751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 136851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, low, mid, -off); 136951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, mid, high, -off); 137051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 137151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // If list is already sorted, just copy from src to dest. This is an 137251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // optimization that results in faster sorts for nearly ordered lists. 137351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) { 137451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(src, low, dest, destLow, length); 137551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 137651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 137751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 137851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Merge sorted halves (now in src) into dest 137951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 138051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0) 138151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[p++]; 138251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 138351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[q++]; 138451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 138551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 138651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 138751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 138851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Swaps x[a] with x[b]. 138951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 139051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void swap(Object[] x, int a, int b) { 139151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object t = x[a]; 139251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski x[a] = x[b]; 139351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski x[b] = t; 139451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 139551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 139651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 139751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified array of objects according to the order induced by 139851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified comparator. All elements in the array must be 139951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> by the specified comparator (that is, 140051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 140151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for any elements {@code e1} and {@code e2} in the array). 140251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 140451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 140551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 140651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 140751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 140851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 140951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 141051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 141151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 141251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 141351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 141451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 141551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 141651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 141751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 141851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 141951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 142051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 142151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 142351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 142451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 142551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 142651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 142751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 142851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 142951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 143051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator to determine the order of the array. A 143151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} value indicates that the elements' 143251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 143351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are 143451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not <i>mutually comparable</i> using the specified comparator 143551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException (optional) if the comparator is 143651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * found to violate the {@link Comparator} contract 143751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 143851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> void sort(T[] a, Comparator<? super T> c) { 1439c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (c == null) { 1440c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath sort(a); 1441c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } else { 1442c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (LegacyMergeSort.userRequested) 1443c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath legacyMergeSort(a, c); 1444c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath else 1445c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath TimSort.sort(a, 0, a.length, c, null, 0, 0); 1446c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } 144751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 144851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 144951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 145051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static <T> void legacyMergeSort(T[] a, Comparator<? super T> c) { 145151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] aux = a.clone(); 145251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c==null) 145351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, 0, a.length, 0); 145451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 145551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, 0, a.length, 0, c); 145651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 145751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 145851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 145951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorts the specified range of the specified array of objects according 146051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the order induced by the specified comparator. The range to be 146151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorted extends from index {@code fromIndex}, inclusive, to index 146251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code toIndex}, exclusive. (If {@code fromIndex==toIndex}, the 146351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be sorted is empty.) All elements in the range must be 146451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> by the specified comparator (that is, 146551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code c.compare(e1, e2)} must not throw a {@code ClassCastException} 146651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for any elements {@code e1} and {@code e2} in the range). 146751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 146851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This sort is guaranteed to be <i>stable</i>: equal elements will 146951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not be reordered as a result of the sort. 147051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 147151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Implementation note: This implementation is a stable, adaptive, 147251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * iterative mergesort that requires far fewer than n lg(n) comparisons 147351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * when the input array is partially sorted, while offering the 147451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * performance of a traditional mergesort when the input array is 147551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * randomly ordered. If the input array is nearly sorted, the 147651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * implementation requires approximately n comparisons. Temporary 147751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * storage requirements vary from a small constant for nearly sorted 147851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input arrays to n/2 object references for randomly ordered input 147951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 148051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation takes equal advantage of ascending and 148251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * descending order in its input array, and can take advantage of 148351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ascending and descending order in different parts of the the same 148451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input array. It is well-suited to merging two or more sorted arrays: 148551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * simply concatenate the arrays and sort the resulting array. 148651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 148751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The implementation was adapted from Tim Peters's list sort for Python 148851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt"> 148951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * TimSort</a>). It uses techiques from Peter McIlroy's "Optimistic 149051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sorting and Information Theoretic Complexity", in Proceedings of the 149151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474, 149251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * January 1993. 149351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 149451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be sorted 149551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 149651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sorted 149751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be sorted 149851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator to determine the order of the array. A 149951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code null} value indicates that the elements' 150051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 150151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are not 150251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> using the specified comparator. 150351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if {@code fromIndex > toIndex} or 150451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (optional) if the comparator is found to violate the 150551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link Comparator} contract 150651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code fromIndex < 0} or 150751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@code toIndex > a.length} 150851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 150951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> void sort(T[] a, int fromIndex, int toIndex, 151051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparator<? super T> c) { 1511c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (c == null) { 1512c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath sort(a, fromIndex, toIndex); 1513c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } else { 1514c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath rangeCheck(a.length, fromIndex, toIndex); 1515c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath if (LegacyMergeSort.userRequested) 1516c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath legacyMergeSort(a, fromIndex, toIndex, c); 1517c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath else 1518c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath TimSort.sort(a, fromIndex, toIndex, c, null, 0, 0); 1519c353afc367d0bc83ac1c2bf33afb07e9ae0fbdffNarayan Kamath } 152051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 152151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 152251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** To be removed in a future release. */ 152351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static <T> void legacyMergeSort(T[] a, int fromIndex, int toIndex, 152451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparator<? super T> c) { 152551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 152651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] aux = copyOfRange(a, fromIndex, toIndex); 152751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c==null) 152851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, fromIndex, toIndex, -fromIndex); 152951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 153051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(aux, a, fromIndex, toIndex, -fromIndex, c); 153151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 153251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 153351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 153451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Src is the source array that starts at index 0 153551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Dest is the (possibly larger) array destination with a possible offset 153651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * low is the index in dest to start sorting 153751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * high is the end index in dest to end sorting 153851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * off is the offset into src corresponding to low in dest 153951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * To be removed in a future release. 154051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 154151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void mergeSort(Object[] src, 154251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object[] dest, 154351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low, int high, int off, 154451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparator c) { 154551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = high - low; 154651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 154751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Insertion sort on smallest arrays 154851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (length < INSERTIONSORT_THRESHOLD) { 154951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=low; i<high; i++) 155051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--) 155151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski swap(dest, j, j-1); 155251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 155351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 155451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 155551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Recursively sort halves of dest into src 155651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destLow = low; 155751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int destHigh = high; 155851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low += off; 155951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high += off; 156051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 156151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, low, mid, -off, c); 156251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski mergeSort(dest, src, mid, high, -off, c); 156351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 156451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // If list is already sorted, just copy from src to dest. This is an 156551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // optimization that results in faster sorts for nearly ordered lists. 156651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c.compare(src[mid-1], src[mid]) <= 0) { 156751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(src, low, dest, destLow, length); 156851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 156951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 157051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 157151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Merge sorted halves (now in src) into dest 157251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for(int i = destLow, p = low, q = mid; i < destHigh; i++) { 157351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0) 157451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[p++]; 157551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 157651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dest[i] = src[q++]; 157751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 157851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 157951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 158051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 158151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Checks that {@code fromIndex} and {@code toIndex} are in 158251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the range and throws an appropriate exception, if they aren't. 158351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 158451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void rangeCheck(int length, int fromIndex, int toIndex) { 158551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (fromIndex > toIndex) { 158651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException( 158751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); 158851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 158951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (fromIndex < 0) { 159051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ArrayIndexOutOfBoundsException(fromIndex); 159151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 159251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (toIndex > length) { 159351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new ArrayIndexOutOfBoundsException(toIndex); 159451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 159551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 159651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 159751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Searching 159851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 159951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 160051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of longs for the specified value using the 160151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 160251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(long[])} method) prior to making this call. If it 160351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 160451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 160551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 160651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 160751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 160851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 160951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 161051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 161151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 161251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 161351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 161451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 161551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 161651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 161751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 161851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(long[] a, long key) { 161951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 162051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 162151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 162251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 162351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 162451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of longs for the specified value using the 162551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 162651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 162751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(long[], int, int)} method) 162851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 162951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 163051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 163151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 163251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 163351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 163451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 163551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 163651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 163751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 163851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 163951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 164051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 164151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 164251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 164351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 164451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 164551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 164651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 164751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 164851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 164951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 165051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 165151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 165251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 165351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 165451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(long[] a, int fromIndex, int toIndex, 165551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long key) { 165651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 165751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 165851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 165951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 166051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 166151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(long[] a, int fromIndex, int toIndex, 166251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long key) { 166351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 166451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 166551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 166651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 166751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 166851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long midVal = a[mid]; 166951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 167051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 167151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 167251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 167351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 167451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 167551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 167651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 167751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 167851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 167951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 168051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 168151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of ints for the specified value using the 168251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 168351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(int[])} method) prior to making this call. If it 168451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 168551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 168651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 168751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 168851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 168951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 169051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 169151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 169251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 169351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 169451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 169551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 169651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 169751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 169851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 169951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(int[] a, int key) { 170051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 170151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 170251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 170351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 170451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 170551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of ints for the specified value using the 170651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 170751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 170851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(int[], int, int)} method) 170951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 171051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 171151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 171251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 171351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 171451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 171551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 171651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 171751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 171851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 171951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 172051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 172151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 172251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 172351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 172451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 172551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 172651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 172751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 172851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 172951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 173051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 173151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 173251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 173351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 173451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 173551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(int[] a, int fromIndex, int toIndex, 173651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int key) { 173751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 173851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 173951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 174051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 174151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 174251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(int[] a, int fromIndex, int toIndex, 174351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int key) { 174451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 174551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 174651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 174751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 174851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 174951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int midVal = a[mid]; 175051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 175151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 175251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 175351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 175451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 175551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 175651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 175751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 175851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 175951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 176051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 176151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 176251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of shorts for the specified value using 176351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. The array must be sorted 176451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(short[])} method) prior to making this call. If 176551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the array contains 176651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 176751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 176851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 176951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 177051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 177151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 177251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 177351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 177451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 177551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 177651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 177751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 177851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 177951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 178051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(short[] a, short key) { 178151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 178251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 178351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 178451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 178551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 178651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of shorts for the specified value using 178751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. 178851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted 178951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(short[], int, int)} method) 179051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If 179151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the range contains 179251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 179351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 179451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 179551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 179651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 179751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 179851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 179951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 180051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 180151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 180251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 180351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 180451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 180551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 180651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 180751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 180851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 180951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 181051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 181151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 181251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 181351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 181451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 181551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 181651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(short[] a, int fromIndex, int toIndex, 181751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short key) { 181851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 181951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 182051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 182151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 182251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 182351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(short[] a, int fromIndex, int toIndex, 182451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short key) { 182551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 182651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 182751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 182851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 182951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 183051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short midVal = a[mid]; 183151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 183251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 183351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 183451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 183551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 183651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 183751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 183851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 183951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 184051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 184151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 184251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 184351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of chars for the specified value using the 184451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 184551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(char[])} method) prior to making this call. If it 184651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 184751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 184851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 184951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 185051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 185151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 185251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 185351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 185451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 185551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 185651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 185751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 185851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 185951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 186051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 186151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(char[] a, char key) { 186251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 186351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 186451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 186551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 186651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 186751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of chars for the specified value using the 186851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 186951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 187051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(char[], int, int)} method) 187151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 187251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 187351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 187451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 187551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 187651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 187751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 187851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 187951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 188051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 188151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 188251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 188351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 188451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 188551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 188651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 188751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 188851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 188951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 189051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 189151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 189251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 189351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 189451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 189551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 189651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 189751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(char[] a, int fromIndex, int toIndex, 189851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char key) { 189951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 190051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 190151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 190251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 190351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 190451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(char[] a, int fromIndex, int toIndex, 190551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char key) { 190651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 190751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 190851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 190951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 191051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 191151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char midVal = a[mid]; 191251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 191351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 191451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 191551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 191651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 191751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 191851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 191951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 192051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 192151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 192251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 192351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 192451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of bytes for the specified value using the 192551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. The array must be sorted (as 192651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(byte[])} method) prior to making this call. If it 192751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the array contains 192851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 192951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 193051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 193151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 193251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 193351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 193451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 193551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 193651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 193751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 193851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 193951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 194051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 194151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 194251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(byte[] a, byte key) { 194351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 194451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 194551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 194651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 194751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 194851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of bytes for the specified value using the 194951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * binary search algorithm. 195051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted (as 195151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by the {@link #sort(byte[], int, int)} method) 195251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If it 195351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is not sorted, the results are undefined. If the range contains 195451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 195551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 195651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 195751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 195851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 195951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 196051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 196151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 196251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 196351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 196451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 196551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 196651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 196751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 196851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 196951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 197051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 197151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 197251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 197351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 197451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 197551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 197651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 197751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 197851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(byte[] a, int fromIndex, int toIndex, 197951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte key) { 198051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 198151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 198251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 198351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 198451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 198551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(byte[] a, int fromIndex, int toIndex, 198651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte key) { 198751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 198851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 198951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 199051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 199151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 199251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte midVal = a[mid]; 199351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 199451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 199551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 199651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 199751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 199851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 199951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 200051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 200151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 200251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 200351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 200451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 200551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of doubles for the specified value using 200651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. The array must be sorted 200751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(double[])} method) prior to making this call. 200851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. If the array contains 200951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 201051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 201151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 201251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 201351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 201451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 201551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 201651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 201751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 201851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 201951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 202051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 202151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 202251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 202351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 202451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(double[] a, double key) { 202551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 202651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 202751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 202851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 202951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 203051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of doubles for the specified value using 203151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. 203251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted 203351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(double[], int, int)} method) 203451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. 203551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. If the range contains 203651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 203751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 203851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 203951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 204051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 204151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 204251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 204351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 204451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 204551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 204651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 204751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 204851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 204951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 205051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 205151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 205251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 205351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 205451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 205551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 205651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 205751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 205851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 205951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 206051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 206151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(double[] a, int fromIndex, int toIndex, 206251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double key) { 206351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 206451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 206551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 206651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 206751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 206851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(double[] a, int fromIndex, int toIndex, 206951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double key) { 207051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 207151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 207251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 207351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 207451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 207551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double midVal = a[mid]; 207651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 207751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 207851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; // Neither val is NaN, thisVal is smaller 207951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 208051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; // Neither val is NaN, thisVal is larger 208151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else { 208251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long midBits = Double.doubleToLongBits(midVal); 208351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long keyBits = Double.doubleToLongBits(key); 208451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midBits == keyBits) // Values are equal 208551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // Key found 208651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 208751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 208851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else // (0.0, -0.0) or (NaN, !NaN) 208951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 209051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 209151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 209251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 209351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 209451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 209551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 209651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array of floats for the specified value using 209751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. The array must be sorted 209851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(float[])} method) prior to making this call. If 209951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the array contains 210051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 210151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 210251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 210351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 210451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 210551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 210651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 210751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 210851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 210951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 211051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 211151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 211251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 211351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 211451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 211551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(float[] a, float key) { 211651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 211751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 211851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 211951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 212051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 212151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array of floats for the specified value using 212251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the binary search algorithm. 212351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted 212451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (as by the {@link #sort(float[], int, int)} method) 212551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prior to making this call. If 212651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is not sorted, the results are undefined. If the range contains 212751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * multiple elements with the specified value, there is no guarantee which 212851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. This method considers all NaN values to be 212951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equivalent and equal. 213051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 213151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 213251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 213351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 213451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 213551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 213651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 213751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 213851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 213951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 214051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 214151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 214251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 214351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 214451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 214551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 214651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 214751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 214851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 214951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 215051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 215151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 215251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(float[] a, int fromIndex, int toIndex, 215351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float key) { 215451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 215551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 215651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 215751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 215851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 215951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(float[] a, int fromIndex, int toIndex, 216051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float key) { 216151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 216251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 216351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 216451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 216551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 216651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float midVal = a[mid]; 216751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 216851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midVal < key) 216951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; // Neither val is NaN, thisVal is smaller 217051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midVal > key) 217151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; // Neither val is NaN, thisVal is larger 217251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else { 217351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int midBits = Float.floatToIntBits(midVal); 217451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int keyBits = Float.floatToIntBits(key); 217551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (midBits == keyBits) // Values are equal 217651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // Key found 217751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (midBits < keyBits) // (-0.0, 0.0) or (!NaN, NaN) 217851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 217951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else // (0.0, -0.0) or (NaN, !NaN) 218051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 218151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 218251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 218351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 218451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 218551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 218651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 218751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array for the specified object using the binary 218851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * search algorithm. The array must be sorted into ascending order 218951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the 219051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} 219151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of its elements (as by the 219251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[])} method) prior to making this call. 219351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. 219451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (If the array contains elements that are not mutually comparable (for 219551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, strings and integers), it <i>cannot</i> be sorted according 219651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the natural ordering of its elements, hence results are undefined.) 219751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the array contains multiple 219851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements equal to the specified object, there is no guarantee which 219951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 220051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 220151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 220251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 220351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 220451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 220551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 220651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 220751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 220851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 220951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 221051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 221151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the search key is not comparable to the 221251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements of the array. 221351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 221451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(Object[] a, Object key) { 221551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key); 221651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 221751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 221851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 221951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 222051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array for the specified object using the binary 222151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * search algorithm. 222251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted into ascending order 222351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the 222451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} 222551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of its elements (as by the 222651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[], int, int)} method) prior to making this 222751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * call. If it is not sorted, the results are undefined. 222851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (If the range contains elements that are not mutually comparable (for 222951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * example, strings and integers), it <i>cannot</i> be sorted according 223051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to the natural ordering of its elements, hence results are undefined.) 223151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the range contains multiple 223251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements equal to the specified object, there is no guarantee which 223351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one will be found. 223451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 223551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 223651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 223751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 223851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 223951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 224051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 224151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 224251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 224351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 224451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 224551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 224651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 224751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 224851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 224951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 225051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the search key is not comparable to the 225151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements of the array within the specified range. 225251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 225351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 225451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 225551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 225651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 225751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 225851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int binarySearch(Object[] a, int fromIndex, int toIndex, 225951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object key) { 226051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 226151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 226251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 226351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 226451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 226551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static int binarySearch0(Object[] a, int fromIndex, int toIndex, 226651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object key) { 226751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 226851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 226951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 227051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 227151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 227251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Comparable midVal = (Comparable)a[mid]; 227351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int cmp = midVal.compareTo(key); 227451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 227551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cmp < 0) 227651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 227751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (cmp > 0) 227851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 227951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 228051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 228151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 228351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 228451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 228551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 228651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches the specified array for the specified object using the binary 228751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * search algorithm. The array must be sorted into ascending order 228851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the specified comparator (as by the 228951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[], Comparator) sort(T[], Comparator)} 229051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method) prior to making this call. If it is 229151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not sorted, the results are undefined. 229251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the array contains multiple 229351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements equal to the specified object, there is no guarantee which one 229451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will be found. 229551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 229651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 229751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 229851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator by which the array is ordered. A 229951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> value indicates that the elements' 230051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 230151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array; 230251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 230351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 230451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 230551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element greater than the key, or <tt>a.length</tt> if all 230651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the array are less than the specified key. Note 230751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 230851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 230951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the array contains elements that are not 231051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> using the specified comparator, 231151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or the search key is not comparable to the 231251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements of the array using this comparator. 231351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 231451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) { 231551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, 0, a.length, key, c); 231651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 231751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 231851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 231951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Searches a range of 232051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the specified array for the specified object using the binary 232151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * search algorithm. 232251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The range must be sorted into ascending order 232351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * according to the specified comparator (as by the 232451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #sort(Object[], int, int, Comparator) 232551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * sort(T[], int, int, Comparator)} 232651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method) prior to making this call. 232751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If it is not sorted, the results are undefined. 232851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the range contains multiple elements equal to the specified object, 232951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * there is no guarantee which one will be found. 233051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 233151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be searched 233251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 233351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * searched 233451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be searched 233551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param key the value to be searched for 233651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param c the comparator by which the array is ordered. A 233751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> value indicates that the elements' 233851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@linkplain Comparable natural ordering} should be used. 233951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return index of the search key, if it is contained in the array 234051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * within the specified range; 234151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The 234251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>insertion point</i> is defined as the point at which the 234351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * key would be inserted into the array: the index of the first 234451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element in the range greater than the key, 234551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or <tt>toIndex</tt> if all 234651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range are less than the specified key. Note 234751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that this guarantees that the return value will be >= 0 if 234851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and only if the key is found. 234951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ClassCastException if the range contains elements that are not 235051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mutually comparable</i> using the specified comparator, 235151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or the search key is not comparable to the 235251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements in the range using this comparator. 235351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException 235451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex > toIndex} 235551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException 235651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if {@code fromIndex < 0 or toIndex > a.length} 235751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 235851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 235951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, 236051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T key, Comparator<? super T> c) { 236151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 236251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key, c); 236351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 236451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 236551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Like public version, but without range checks. 236651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static <T> int binarySearch0(T[] a, int fromIndex, int toIndex, 236751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T key, Comparator<? super T> c) { 236851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (c == null) { 236951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return binarySearch0(a, fromIndex, toIndex, key); 237051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 237151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int low = fromIndex; 237251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int high = toIndex - 1; 237351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 237451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (low <= high) { 237551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int mid = (low + high) >>> 1; 237651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T midVal = a[mid]; 237751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int cmp = c.compare(midVal, key); 237851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (cmp < 0) 237951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski low = mid + 1; 238051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (cmp > 0) 238151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski high = mid - 1; 238251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 238351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return mid; // key found 238451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 238551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -(low + 1); // key not found. 238651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 238751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 238851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Equality Testing 238951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 239051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 239151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of longs are 239251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 239351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 239451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 239551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 239651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 239751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 239851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 239951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 240051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 240151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 240251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(long[] a, long[] a2) { 240351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 240451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 240551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 240651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 240751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 240851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 240951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 241051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 241151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 241351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 241451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 241551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 241751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 241851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 241951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 242051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of ints are 242151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 242251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 242351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 242451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 242551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 242651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 242751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 242851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 242951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 243051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 243151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(int[] a, int[] a2) { 243251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 243351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 243451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 243551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 243651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 243751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 243851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 243951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 244051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 244151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 244251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 244351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 244451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 244551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 244651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 244751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 244851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 244951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of shorts are 245051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 245151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 245251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 245351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 245451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 245551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 245651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 245751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 245851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 245951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 246051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(short[] a, short a2[]) { 246151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 246251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 246351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 246451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 246551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 246651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 246751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 246851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 246951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 247151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 247251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 247351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 247551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 247651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 247751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 247851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of chars are 247951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 248051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 248151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 248251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 248351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 248451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 248551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 248651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 248751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 248851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 248951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(char[] a, char[] a2) { 249051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 249151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 249251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 249351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 249451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 249551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 249651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 249751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 249851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 249951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 250051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 250151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 250251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 250351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 250451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 250551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 250651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 250751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of bytes are 250851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 250951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 251051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 251151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 251251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 251351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 251451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 251551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 251651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 251751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 251851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(byte[] a, byte[] a2) { 251951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 252051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 252151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 252251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 252351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 252451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 252551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 252651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 252751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 252851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 252951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 253051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 253151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 253251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 253351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 253451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 253551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 253651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of booleans are 253751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 253851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 253951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 254051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 254151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 254251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 254351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 254451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 254551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 254651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 254751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(boolean[] a, boolean[] a2) { 254851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 254951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 255051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 255151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 255251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 255351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 255451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 255551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 255651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 255751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 255851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i] != a2[i]) 255951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 256051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 256151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 256251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 256351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 256451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 256551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of doubles are 256651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 256751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 256851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 256951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 257051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 257151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 257251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Two doubles <tt>d1</tt> and <tt>d2</tt> are considered equal if: 257351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> <tt>new Double(d1).equals(new Double(d2))</tt></pre> 257451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (Unlike the <tt>==</tt> operator, this method considers 257551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>NaN</tt> equals to itself, and 0.0d unequal to -0.0d.) 257651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 257751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 257851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 257951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 258051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Double#equals(Object) 258151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 258251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(double[] a, double[] a2) { 258351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 258451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 258551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 258651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 258751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 258851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 258951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 259051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 259151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 259251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 259351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (Double.doubleToLongBits(a[i])!=Double.doubleToLongBits(a2[i])) 259451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 259551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 259651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 259751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 259851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 259951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 260051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of floats are 260151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. Two arrays are considered equal if both 260251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays contain the same number of elements, and all corresponding pairs 260351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of elements in the two arrays are equal. In other words, two arrays 260451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are equal if they contain the same elements in the same order. Also, 260551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two array references are considered equal if both are <tt>null</tt>.<p> 260651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 260751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Two floats <tt>f1</tt> and <tt>f2</tt> are considered equal if: 260851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> <tt>new Float(f1).equals(new Float(f2))</tt></pre> 260951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (Unlike the <tt>==</tt> operator, this method considers 261051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>NaN</tt> equals to itself, and 0.0f unequal to -0.0f.) 261151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 261251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 261351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 261451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 261551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Float#equals(Object) 261651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 261751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(float[] a, float[] a2) { 261851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 261951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 262051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 262151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 262251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 262351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 262451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 262551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 262651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 262751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) 262851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (Float.floatToIntBits(a[i])!=Float.floatToIntBits(a2[i])) 262951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 263051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 263151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 263251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 263351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 263451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 263551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays of Objects are 263651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>equal</i> to one another. The two arrays are considered equal if 263751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * both arrays contain the same number of elements, and all corresponding 263851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * pairs of elements in the two arrays are equal. Two objects <tt>e1</tt> 263951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>e2</tt> are considered <i>equal</i> if <tt>(e1==null ? e2==null 264051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * : e1.equals(e2))</tt>. In other words, the two arrays are equal if 264151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they contain the same elements in the same order. Also, two array 264251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * references are considered equal if both are <tt>null</tt>.<p> 264351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 264451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a one array to be tested for equality 264551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 264651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 264751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 264851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean equals(Object[] a, Object[] a2) { 264951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==a2) 265051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 265151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a==null || a2==null) 265251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 265351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 265451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a.length; 265551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 265651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 265751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 265851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<length; i++) { 265951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object o1 = a[i]; 266051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object o2 = a2[i]; 266151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!(o1==null ? o2==null : o1.equals(o2))) 266251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 266351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 266451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 266551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 266651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 266751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 266851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Filling 266951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 267051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 267151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified long value to each element of the specified array 267251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of longs. 267351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 267451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 267551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 267651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 267751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(long[] a, long val) { 267851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 267951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 268051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 268151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 268251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 268351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified long value to each element of the specified 268451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of longs. The range to be filled 268551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 268651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 268751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 268851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 268951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 269051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 269151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 269251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 269351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 269451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 269551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 269651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 269751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 269851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 269951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(long[] a, int fromIndex, int toIndex, long val) { 270051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 270151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 270251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 270351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 270451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 270551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 270651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified int value to each element of the specified array 270751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of ints. 270851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 270951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 271051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 271151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 271251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(int[] a, int val) { 271351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 271451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 271551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 271651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 271751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 271851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified int value to each element of the specified 271951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of ints. The range to be filled 272051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 272151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 272251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 272351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 272451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 272551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 272651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 272751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 272851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 272951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 273051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 273151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 273251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 273351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 273451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(int[] a, int fromIndex, int toIndex, int val) { 273551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 273651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 273751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 273851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 273951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 274051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 274151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified short value to each element of the specified array 274251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of shorts. 274351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 274451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 274551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 274651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 274751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(short[] a, short val) { 274851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 274951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 275051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 275151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 275251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 275351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified short value to each element of the specified 275451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of shorts. The range to be filled 275551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 275651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 275751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 275851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 275951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 276051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 276151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 276251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 276351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 276451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 276551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 276651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 276751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 276851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 276951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(short[] a, int fromIndex, int toIndex, short val) { 277051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 277151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 277251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 277351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 277451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 277551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 277651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified char value to each element of the specified array 277751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of chars. 277851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 277951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 278051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 278151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 278251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(char[] a, char val) { 278351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 278451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 278551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 278651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 278751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 278851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified char value to each element of the specified 278951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of chars. The range to be filled 279051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 279151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 279251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 279351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 279451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 279551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 279651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 279751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 279851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 279951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 280051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 280151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 280251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 280351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 280451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(char[] a, int fromIndex, int toIndex, char val) { 280551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 280651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 280751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 280851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 280951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 281051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 281151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified byte value to each element of the specified array 281251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of bytes. 281351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 281451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 281551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 281651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 281751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(byte[] a, byte val) { 281851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 281951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 282051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 282151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 282251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 282351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified byte value to each element of the specified 282451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of bytes. The range to be filled 282551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 282651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 282751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 282851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 282951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 283051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 283151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 283251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 283351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 283451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 283551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 283651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 283751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 283851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 283951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(byte[] a, int fromIndex, int toIndex, byte val) { 284051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 284151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 284251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 284351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 284451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 284551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 284651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified boolean value to each element of the specified 284751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of booleans. 284851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 284951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 285051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 285151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 285251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(boolean[] a, boolean val) { 285351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 285451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 285551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 285651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 285751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 285851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified boolean value to each element of the specified 285951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of booleans. The range to be filled 286051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 286151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 286251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 286351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 286451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 286551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 286651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 286751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 286851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 286951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 287051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 287151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 287251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 287351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 287451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(boolean[] a, int fromIndex, int toIndex, 287551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean val) { 287651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 287751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 287851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 287951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 288051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 288151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 288251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified double value to each element of the specified 288351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of doubles. 288451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 288551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 288651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 288751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 288851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(double[] a, double val) { 288951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 289051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 289151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 289251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 289351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 289451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified double value to each element of the specified 289551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of doubles. The range to be filled 289651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 289751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 289851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 289951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 290051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 290151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 290251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 290351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 290451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 290551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 290651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 290751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 290851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 290951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 291051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(double[] a, int fromIndex, int toIndex,double val){ 291151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 291251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 291351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 291451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 291551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 291651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 291751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified float value to each element of the specified array 291851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of floats. 291951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 292051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 292151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 292251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 292351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(float[] a, float val) { 292451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 292551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 292651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 292751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 292851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 292951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified float value to each element of the specified 293051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of floats. The range to be filled 293151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 293251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 293351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 293451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 293551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 293651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 293751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 293851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 293951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 294051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 294151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 294251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 294351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 294451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 294551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(float[] a, int fromIndex, int toIndex, float val) { 294651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 294751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 294851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 294951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 295051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 295151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 295251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified Object reference to each element of the specified 295351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of Objects. 295451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 295551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 295651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 295751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if the specified value is not of a 295851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime type that can be stored in the specified array 295951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 296051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(Object[] a, Object val) { 296151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, len = a.length; i < len; i++) 296251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 296351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 296451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 296551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 296651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Assigns the specified Object reference to each element of the specified 296751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range of the specified array of Objects. The range to be filled 296851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extends from index <tt>fromIndex</tt>, inclusive, to index 296951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the 297051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * range to be filled is empty.) 297151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 297251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array to be filled 297351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param fromIndex the index of the first element (inclusive) to be 297451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 297551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param toIndex the index of the last element (exclusive) to be 297651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filled with the specified value 297751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param val the value to be stored in all elements of the array 297851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> 297951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or 298051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>toIndex > a.length</tt> 298151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if the specified value is not of a 298251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * runtime type that can be stored in the specified array 298351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 298451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static void fill(Object[] a, int fromIndex, int toIndex, Object val) { 298551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rangeCheck(a.length, fromIndex, toIndex); 298651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = fromIndex; i < toIndex; i++) 298751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[i] = val; 298851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 298951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 299051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Cloning 299151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 299251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 299351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with nulls (if necessary) 299451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 299551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 299651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 299751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>null</tt>. 299851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 299951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 300051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of exactly the same class as the original array. 300151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 300251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 300351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 300451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with nulls 300551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 300651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 300751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 300851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 300951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 301051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> T[] copyOf(T[] original, int newLength) { 301151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (T[]) copyOf(original, newLength, original.getClass()); 301251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 301351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 301451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 301551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with nulls (if necessary) 301651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 301751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 301851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 301951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>null</tt>. 302051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 302151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 302251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of the class <tt>newType</tt>. 302351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 302451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 302551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 302651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newType the class of the copy to be returned 302751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with nulls 302851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 302951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 303051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 303151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if an element copied from 303251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original</tt> is not of a runtime type that can be stored in 303351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of class <tt>newType</tt> 303451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 303551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 303651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { 303751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] copy = ((Object)newType == (Object)Object[].class) 303851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ? (T[]) new Object[newLength] 303951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski : (T[]) Array.newInstance(newType.getComponentType(), newLength); 304051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 304151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 304251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 304351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 304451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 304551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 304651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 304751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 304851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 304951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 305051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>(byte)0</tt>. 305151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 305251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 305351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 305451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 305551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 305651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 305751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 305851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 305951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 306051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 306151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 306251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static byte[] copyOf(byte[] original, int newLength) { 306351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte[] copy = new byte[newLength]; 306451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 306551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 306651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 306751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 306851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 306951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 307051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 307151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 307251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 307351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 307451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>(short)0</tt>. 307551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 307651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 307751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 307851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 307951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 308051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 308151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 308251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 308351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 308451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 308551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 308651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static short[] copyOf(short[] original, int newLength) { 308751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short[] copy = new short[newLength]; 308851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 308951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 309051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 309151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 309251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 309351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 309451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 309551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 309651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 309751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 309851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0</tt>. 309951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 310051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 310151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 310251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 310351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 310451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 310551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 310651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 310751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 310851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 310951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 311051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int[] copyOf(int[] original, int newLength) { 311151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int[] copy = new int[newLength]; 311251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 311351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 311451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 311551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 311651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 311751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 311851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 311951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 312051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 312151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 312251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0L</tt>. 312351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 312451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 312551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 312651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 312751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 312851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 312951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 313051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 313151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 313251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 313351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 313451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static long[] copyOf(long[] original, int newLength) { 313551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long[] copy = new long[newLength]; 313651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 313751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 313851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 313951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 314051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 314151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 314251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with null characters (if necessary) 314351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are valid 314451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in both the original array and the copy, the two arrays will contain 314551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * identical values. For any indices that are valid in the copy but not 314651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the original, the copy will contain <tt>'\\u000'</tt>. Such indices 314751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will exist if and only if the specified length is greater than that of 314851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the original array. 314951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 315051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 315151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 315251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with null characters 315351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 315451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 315551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 315651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 315751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 315851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static char[] copyOf(char[] original, int newLength) { 315951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char[] copy = new char[newLength]; 316051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 316151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 316251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 316351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 316451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 316551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 316651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 316751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 316851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 316951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 317051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0f</tt>. 317151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 317251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 317351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 317451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 317551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 317651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 317751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 317851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 317951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 318051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 318151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 318251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static float[] copyOf(float[] original, int newLength) { 318351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float[] copy = new float[newLength]; 318451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 318551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 318651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 318751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 318851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 318951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 319051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with zeros (if necessary) 319151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 319251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 319351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 319451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>0d</tt>. 319551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 319651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 319751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 319851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 319951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 320051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with zeros 320151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 320251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 320351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 320451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 320551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 320651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static double[] copyOf(double[] original, int newLength) { 320751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double[] copy = new double[newLength]; 320851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 320951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 321051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 321151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 321251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 321351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 321451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified array, truncating or padding with <tt>false</tt> (if necessary) 321551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * so the copy has the specified length. For all indices that are 321651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid in both the original array and the copy, the two arrays will 321751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contain identical values. For any indices that are valid in the 321851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * copy but not the original, the copy will contain <tt>false</tt>. 321951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Such indices will exist if and only if the specified length 322051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is greater than that of the original array. 322151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 322251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array to be copied 322351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength the length of the copy to be returned 322451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a copy of the original array, truncated or padded with false elements 322551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to obtain the specified length 322651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NegativeArraySizeException if <tt>newLength</tt> is negative 322751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 322851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 322951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 323051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean[] copyOf(boolean[] original, int newLength) { 323151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean[] copy = new boolean[newLength]; 323251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, 0, copy, 0, 323351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length, newLength)); 323451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 323551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 323651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 323751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 323851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 323951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 324051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 324151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 324251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 324351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 324451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 324551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 324651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 324751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> is placed in all elements of the copy whose index is 324851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 324951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 325051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 325151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of exactly the same class as the original array. 325251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 325351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 325451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 325551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 325651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 325751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 325851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with nulls to obtain the required length 325951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 326051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 326151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 326251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 326351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 326451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 326551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> T[] copyOfRange(T[] original, int from, int to) { 326651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copyOfRange(original, from, to, (Class<T[]>) original.getClass()); 326751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 326851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 326951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 327051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 327151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 327251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 327351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 327451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 327551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 327651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 327751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 327851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 327951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt> is placed in all elements of the copy whose index is 328051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 328151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 328251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The resulting array is of the class <tt>newType</tt>. 328351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 328451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 328551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 328651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 328751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 328851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newType the class of the copy to be returned 328951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 329051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with nulls to obtain the required length 329151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 329251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 329351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 329451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 329551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayStoreException if an element copied from 329651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original</tt> is not of a runtime type that can be stored in 329751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an array of class <tt>newType</tt>. 329851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 329951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 330051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) { 330151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 330251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 330351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 330451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski T[] copy = ((Object)newType == (Object)Object[].class) 330551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ? (T[]) new Object[newLength] 330651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski : (T[]) Array.newInstance(newType.getComponentType(), newLength); 330751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 330851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 330951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 331051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 331151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 331251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 331351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 331451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 331551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 331651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 331751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 331851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 331951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 332051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 332151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 332251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>(byte)0</tt> is placed in all elements of the copy whose index is 332351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 332451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 332551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 332651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 332751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 332851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 332951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 333051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 333151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 333251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 333351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 333451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 333551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 333651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 333751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 333851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static byte[] copyOfRange(byte[] original, int from, int to) { 333951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 334051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 334151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 334251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte[] copy = new byte[newLength]; 334351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 334451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 334551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 334651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 334751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 334851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 334951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 335051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 335151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 335251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 335351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 335451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 335551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 335651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 335751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 335851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>(short)0</tt> is placed in all elements of the copy whose index is 335951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 336051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 336151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 336251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 336351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 336451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 336551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 336651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 336751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 336851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 336951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 337051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 337151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 337251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 337351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 337451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static short[] copyOfRange(short[] original, int from, int to) { 337551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 337651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 337751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 337851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski short[] copy = new short[newLength]; 337951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 338051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 338151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 338251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 338351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 338451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 338551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 338651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 338751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 338851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 338951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 339051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 339151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 339251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 339351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 339451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0</tt> is placed in all elements of the copy whose index is 339551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 339651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 339751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 339851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 339951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 340051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 340151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 340251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 340351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 340451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 340551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 340651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 340751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 340851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 340951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 341051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int[] copyOfRange(int[] original, int from, int to) { 341151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 341251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 341351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 341451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int[] copy = new int[newLength]; 341551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 341651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 341751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 341851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 341951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 342051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 342151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 342251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 342351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 342451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 342551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 342651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 342751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 342851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 342951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 343051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0L</tt> is placed in all elements of the copy whose index is 343151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 343251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 343351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 343451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 343551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 343651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 343751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 343851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 343951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 344051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 344151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 344251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 344351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 344451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 344551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 344651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static long[] copyOfRange(long[] original, int from, int to) { 344751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 344851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 344951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 345051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long[] copy = new long[newLength]; 345151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 345251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 345351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 345451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 345551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 345651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 345751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 345851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 345951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 346051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 346151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 346251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 346351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 346451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 346551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 346651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>'\\u000'</tt> is placed in all elements of the copy whose index is 346751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 346851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 346951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 347051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 347151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 347251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 347351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 347451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 347551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with null characters to obtain the required length 347651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 347751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 347851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 347951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 348051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 348151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 348251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static char[] copyOfRange(char[] original, int from, int to) { 348351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 348451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 348551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 348651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char[] copy = new char[newLength]; 348751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 348851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 348951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 349051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 349151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 349251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 349351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 349451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 349551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 349651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 349751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 349851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 349951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 350051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 350151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 350251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0f</tt> is placed in all elements of the copy whose index is 350351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 350451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 350551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 350651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 350751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 350851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 350951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 351051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 351151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 351251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 351351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 351451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 351551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 351651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 351751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 351851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static float[] copyOfRange(float[] original, int from, int to) { 351951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 352051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 352151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 352251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski float[] copy = new float[newLength]; 352351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 352451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 352551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 352651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 352751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 352851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 352951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 353051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 353151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 353251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 353351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 353451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 353551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 353651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 353751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 353851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>0d</tt> is placed in all elements of the copy whose index is 353951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 354051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 354151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 354251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 354351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 354451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 354551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 354651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 354751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with zeros to obtain the required length 354851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 354951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 355051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 355151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 355251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 355351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 355451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static double[] copyOfRange(double[] original, int from, int to) { 355551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 355651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 355751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 355851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski double[] copy = new double[newLength]; 355951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 356051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 356151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 356251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 356351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 356451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 356551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copies the specified range of the specified array into a new array. 356651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The initial index of the range (<tt>from</tt>) must lie between zero 356751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and <tt>original.length</tt>, inclusive. The value at 356851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>original[from]</tt> is placed into the initial element of the copy 356951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (unless <tt>from == original.length</tt> or <tt>from == to</tt>). 357051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Values from subsequent elements in the original array are placed into 357151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * subsequent elements in the copy. The final index of the range 357251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>, 357351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * may be greater than <tt>original.length</tt>, in which case 357451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>false</tt> is placed in all elements of the copy whose index is 357551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * greater than or equal to <tt>original.length - from</tt>. The length 357651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the returned array will be <tt>to - from</tt>. 357751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 357851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param original the array from which a range is to be copied 357951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param from the initial index of the range to be copied, inclusive 358051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param to the final index of the range to be copied, exclusive. 358151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (This index may lie outside the array.) 358251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a new array containing the specified range from the original array, 358351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * truncated or padded with false elements to obtain the required length 358451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws ArrayIndexOutOfBoundsException if {@code from < 0} 358551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or {@code from > original.length} 358651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws IllegalArgumentException if <tt>from > to</tt> 358751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @throws NullPointerException if <tt>original</tt> is null 358851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.6 358951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 359051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean[] copyOfRange(boolean[] original, int from, int to) { 359151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int newLength = to - from; 359251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newLength < 0) 359351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException(from + " > " + to); 359451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean[] copy = new boolean[newLength]; 359551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(original, from, copy, 0, 359651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Math.min(original.length - from, newLength)); 359751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return copy; 359851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 359951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 360051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Misc 360151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 360251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 360351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a fixed-size list backed by the specified array. (Changes to 360451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the returned list "write through" to the array.) This method acts 360551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as bridge between array-based and collection-based APIs, in 360651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * combination with {@link Collection#toArray}. The returned list is 360751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * serializable and implements {@link RandomAccess}. 360851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 360951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method also provides a convenient way to create a fixed-size 361051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * list initialized to contain several elements: 361151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <pre> 361251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); 361351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre> 361451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 361551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array by which the list will be backed 361651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a list view of the specified array 361751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 361851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski @SafeVarargs 361951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static <T> List<T> asList(T... a) { 362051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return new ArrayList<>(a); 362151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 362251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 362351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 362451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @serial include 362551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 362651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static class ArrayList<E> extends AbstractList<E> 362751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski implements RandomAccess, java.io.Serializable 362851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 362951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static final long serialVersionUID = -2764017481108945198L; 363051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final E[] a; 363151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 363251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski ArrayList(E[] array) { 36334c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath a = Objects.requireNonNull(array); 363451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 363551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36364c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 363751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int size() { 363851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a.length; 363951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 364051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 364251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public Object[] toArray() { 364351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a.clone(); 364451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 364551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36464c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 36474c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @SuppressWarnings("unchecked") 364851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public <T> T[] toArray(T[] a) { 364951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int size = size(); 365051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a.length < size) 365151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Arrays.copyOf(this.a, size, 365251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski (Class<? extends T[]>) a.getClass()); 365351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski System.arraycopy(this.a, 0, a, 0, size); 365451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a.length > size) 365551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[size] = null; 365651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a; 365751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 365851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36594c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 366051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public E get(int index) { 366151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return a[index]; 366251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 366351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 366551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public E set(int index, E element) { 366651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski E oldValue = a[index]; 366751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski a[index] = element; 366851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return oldValue; 366951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 367051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36714c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 367251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int indexOf(Object o) { 367351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (o==null) { 367451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<a.length; i++) 367551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a[i]==null) 367651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i; 367751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 367851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i=0; i<a.length; i++) 367951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (o.equals(a[i])) 368051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return i; 368151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 368251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return -1; 368351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 368451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 36854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 368651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public boolean contains(Object o) { 368751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return indexOf(o) != -1; 368851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 368902f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak 369002f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak @Override 369102f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak public void forEach(Consumer<? super E> action) { 369202f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak Objects.requireNonNull(action); 369302f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak for (E e : a) { 369402f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak action.accept(e); 369502f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak } 369602f409d949c9d50806809b827ec503765bed34fdPrzemyslaw Szczepaniak } 36974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 36984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath @Override 36994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public Spliterator<E> spliterator() { 37004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(a, Spliterator.ORDERED); 37014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 370251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 370351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 370451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 370551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 370651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>long</tt> arrays <tt>a</tt> and <tt>b</tt> 370751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 370851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 370951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 371051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 371151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 371251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Long} 371351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 371451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 371551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 371651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 371751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 371851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 371951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 372051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(long a[]) { 372151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 372251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 372351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 372451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 372551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (long element : a) { 372651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int elementHash = (int)(element ^ (element >>> 32)); 372751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + elementHash; 372851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 372951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 373051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 373151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 373251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 373351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 373451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 373551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two non-null <tt>int</tt> arrays <tt>a</tt> and <tt>b</tt> 373651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 373751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 373851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 373951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 374051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 374151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Integer} 374251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 374351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 374451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 374551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 374651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 374751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 374851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 374951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(int a[]) { 375051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 375151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 375251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 375351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 375451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int element : a) 375551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 375651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 375751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 375851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 375951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 376051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 376151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 376251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>short</tt> arrays <tt>a</tt> and <tt>b</tt> 376351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 376451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 376551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 376651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 376751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 376851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Short} 376951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 377051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 377151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 377251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 377351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 377451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 377551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 377651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(short a[]) { 377751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 377851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 377951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 378051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 378151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (short element : a) 378251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 378351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 378451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 378551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 378651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 378751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 378851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 378951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>char</tt> arrays <tt>a</tt> and <tt>b</tt> 379051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 379151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 379251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 379351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 379451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 379551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Character} 379651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 379751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 379851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 379951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 380051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 380151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 380251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 380351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(char a[]) { 380451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 380551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 380651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 380751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 380851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (char element : a) 380951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 381051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 381151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 381251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 381351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 381451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 381551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 381651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>byte</tt> arrays <tt>a</tt> and <tt>b</tt> 381751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 381851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 381951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 382051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 382151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 382251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Byte} 382351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 382451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 382551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 382651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 382751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 382851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 382951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 383051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(byte a[]) { 383151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 383251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 383351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 383451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 383551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (byte element : a) 383651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + element; 383751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 383851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 383951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 384051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 384151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 384251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 384351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>boolean</tt> arrays <tt>a</tt> and <tt>b</tt> 384451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 384551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 384651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 384751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 384851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 384951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Boolean} 385051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 385151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 385251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 385351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 385451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 385551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 385651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 385751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(boolean a[]) { 385851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 385951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 386051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 386151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 386251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (boolean element : a) 386351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + (element ? 1231 : 1237); 386451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 386551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 386651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 386751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 386851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 386951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 387051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>float</tt> arrays <tt>a</tt> and <tt>b</tt> 387151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 387251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 387351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 387451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 387551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 387651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Float} 387751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 387851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 387951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 388051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 388151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 388251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 388351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 388451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(float a[]) { 388551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 388651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 388751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 388851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 388951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (float element : a) 389051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + Float.floatToIntBits(element); 389151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 389251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 389351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 389451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 389551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 389651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. 389751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * For any two <tt>double</tt> arrays <tt>a</tt> and <tt>b</tt> 389851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * such that <tt>Arrays.equals(a, b)</tt>, it is also the case that 389951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 390051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 390151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is the same value that would be 390251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * obtained by invoking the {@link List#hashCode() <tt>hashCode</tt>} 390351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method on a {@link List} containing a sequence of {@link Double} 390451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * instances representing the elements of <tt>a</tt> in the same order. 390551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If <tt>a</tt> is <tt>null</tt>, this method returns 0. 390651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 390751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose hash value to compute 390851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 390951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 391051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 391151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(double a[]) { 391251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 391351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 391451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 391551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 391651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (double element : a) { 391751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long bits = Double.doubleToLongBits(element); 391851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + (int)(bits ^ (bits >>> 32)); 391951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 392051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 392151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 392251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 392351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 392451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the contents of the specified array. If 392551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the array contains other arrays as elements, the hash code is based on 392651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * their identities rather than their contents. It is therefore 392751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * acceptable to invoke this method on an array that contains itself as an 392851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * element, either directly or indirectly through one or more levels of 392951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 393051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 393151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that 393251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.equals(a, b)</tt>, it is also the case that 393351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.hashCode(a) == Arrays.hashCode(b)</tt>. 393451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 393551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is equal to the value that would 393651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be returned by <tt>Arrays.asList(a).hashCode()</tt>, unless <tt>a</tt> 393751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>, in which case <tt>0</tt> is returned. 393851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 393951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose content-based hash code to compute 394051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a content-based hash code for <tt>a</tt> 394151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #deepHashCode(Object[]) 394251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 394351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 394451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int hashCode(Object a[]) { 394551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 394651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 394751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 394851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 394951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 395051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (Object element : a) 395151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + (element == null ? 0 : element.hashCode()); 395251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 395351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 395451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 395551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 395651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 395751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a hash code based on the "deep contents" of the specified 395851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If the array contains other arrays as elements, the 395951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * hash code is based on their contents and so on, ad infinitum. 396051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is therefore unacceptable to invoke this method on an array that 396151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * contains itself as an element, either directly or indirectly through 396251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one or more levels of arrays. The behavior of such an invocation is 396351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * undefined. 396451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 396551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>For any two arrays <tt>a</tt> and <tt>b</tt> such that 396651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.deepEquals(a, b)</tt>, it is also the case that 396751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.deepHashCode(a) == Arrays.deepHashCode(b)</tt>. 396851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 396951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The computation of the value returned by this method is similar to 397051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that of the value returned by {@link List#hashCode()} on a list 397151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * containing the same elements as <tt>a</tt> in the same order, with one 397251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * difference: If an element <tt>e</tt> of <tt>a</tt> is itself an array, 397351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its hash code is computed not by calling <tt>e.hashCode()</tt>, but as 397451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by calling the appropriate overloading of <tt>Arrays.hashCode(e)</tt> 397551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if <tt>e</tt> is an array of a primitive type, or as by calling 397651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.deepHashCode(e)</tt> recursively if <tt>e</tt> is an array 397751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of a reference type. If <tt>a</tt> is <tt>null</tt>, this method 397851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * returns 0. 397951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 398051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose deep-content-based hash code to compute 398151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a deep-content-based hash code for <tt>a</tt> 398251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #hashCode(Object[]) 398351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 398451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 398551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static int deepHashCode(Object a[]) { 398651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 398751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 398851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 398951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int result = 1; 399051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 399151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (Object element : a) { 399251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int elementHash = 0; 3993669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (element != null) { 3994669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak Class<?> cl = element.getClass().getComponentType(); 3995669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (cl == null) 3996669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = element.hashCode(); 3997669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (element instanceof Object[]) 3998669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = deepHashCode((Object[]) element); 3999669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == byte.class) 4000669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((byte[]) element); 4001669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == short.class) 4002669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((short[]) element); 4003669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == int.class) 4004669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((int[]) element); 4005669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == long.class) 4006669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((long[]) element); 4007669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == char.class) 4008669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((char[]) element); 4009669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == float.class) 4010669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((float[]) element); 4011669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == double.class) 4012669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((double[]) element); 4013669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl == boolean.class) 4014669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = hashCode((boolean[]) element); 4015669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else 4016669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak elementHash = element.hashCode(); 4017669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak } 401851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski result = 31 * result + elementHash; 401951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 402051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 402151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return result; 402251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 402351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 402451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 402551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns <tt>true</tt> if the two specified arrays are <i>deeply 402651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * equal</i> to one another. Unlike the {@link #equals(Object[],Object[])} 402751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method, this method is appropriate for use with nested arrays of 402851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arbitrary depth. 402951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 403051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Two array references are considered deeply equal if both 403151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are <tt>null</tt>, or if they refer to arrays that contain the same 403251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * number of elements and all corresponding pairs of elements in the two 403351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays are deeply equal. 403451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 403551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>Two possibly <tt>null</tt> elements <tt>e1</tt> and <tt>e2</tt> are 403651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * deeply equal if any of the following conditions hold: 403751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <ul> 403851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1</tt> and <tt>e2</tt> are both arrays of object reference 403951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * types, and <tt>Arrays.deepEquals(e1, e2) would return true</tt> 404051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1</tt> and <tt>e2</tt> are arrays of the same primitive 404151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * type, and the appropriate overloading of 404251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.equals(e1, e2)</tt> would return true. 404351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1 == e2</tt> 404451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <li> <tt>e1.equals(e2)</tt> would return true. 404551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </ul> 404651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Note that this definition permits <tt>null</tt> elements at any depth. 404751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 404851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If either of the specified arrays contain themselves as elements 404951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * either directly or indirectly through one or more levels of arrays, 405051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the behavior of this method is undefined. 405151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 405251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a1 one array to be tested for equality 405351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a2 the other array to be tested for equality 405451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return <tt>true</tt> if the two arrays are equal 405551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #equals(Object[],Object[]) 405651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see Objects#deepEquals(Object, Object) 405751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 405851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 405951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static boolean deepEquals(Object[] a1, Object[] a2) { 406051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a1 == a2) 406151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 406251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a1 == null || a2==null) 406351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 406451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int length = a1.length; 406551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a2.length != length) 406651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 406751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 406851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; i < length; i++) { 406951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object e1 = a1[i]; 407051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object e2 = a2[i]; 407151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 407251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (e1 == e2) 407351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski continue; 4074669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (e1 == null || e2 == null) 407551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 407651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 407751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Figure out whether the two elements are equal 407851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean eq = deepEquals0(e1, e2); 407951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 408051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (!eq) 408151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return false; 408251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 408351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return true; 408451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 408551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 408651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski static boolean deepEquals0(Object e1, Object e2) { 4087669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak Class<?> cl1 = e1.getClass().getComponentType(); 4088669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak Class<?> cl2 = e2.getClass().getComponentType(); 4089669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak 4090669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (cl1 != cl2) { 4091669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return false; 4092669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak } 4093669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak if (e1 instanceof Object[]) 4094669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return deepEquals ((Object[]) e1, (Object[]) e2); 4095669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == byte.class) 4096669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((byte[]) e1, (byte[]) e2); 4097669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == short.class) 4098669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((short[]) e1, (short[]) e2); 4099669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == int.class) 4100669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((int[]) e1, (int[]) e2); 4101669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == long.class) 4102669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((long[]) e1, (long[]) e2); 4103669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == char.class) 4104669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((char[]) e1, (char[]) e2); 4105669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == float.class) 4106669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((float[]) e1, (float[]) e2); 4107669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == double.class) 4108669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((double[]) e1, (double[]) e2); 4109669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak else if (cl1 == boolean.class) 4110669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return equals((boolean[]) e1, (boolean[]) e2); 411151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 4112669c2cc344614fd721cfff2e7a9e80ad6e8b368cPrzemyslaw Szczepaniak return e1.equals(e2); 411351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 411451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 411551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 411651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 411751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 411851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 411951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 412051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 412151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(long)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 412251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 412351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 412451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 412551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 412651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 412751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 412851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(long[] a) { 412951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 413051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 413151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 413251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 413351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 413451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 413551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 413651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 413751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 413851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 413951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 414051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 414151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 414251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 414351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 414451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 414551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 414651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 414751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 414851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 414951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 415051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 415151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(int)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> is 415251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>null</tt>. 415351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 415451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 415551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 415651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 415751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 415851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(int[] a) { 415951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 416051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 416151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 416251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 416351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 416451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 416551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 416651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 416751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 416851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 416951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 417051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 417151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 417251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 417351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 417451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 417551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 417651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 417751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 417851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 417951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 418051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 418151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(short)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 418251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 418351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 418451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 418551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 418651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 418751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 418851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(short[] a) { 418951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 419051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 419151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 419251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 419351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 419451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 419551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 419651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 419751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 419851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 419951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 420051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 420151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 420251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 420351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 420451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 420551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 420651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 420751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 420851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 420951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 421051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 421151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(char)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 421251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 421351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 421451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 421551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 421651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 421751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 421851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(char[] a) { 421951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 422051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 422151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 422251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 422351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 422451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 422551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 422651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 422751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 422851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 422951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 423051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 423151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 423251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 423351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 423451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 423551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 423651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 423751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 423851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements 423951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are separated by the characters <tt>", "</tt> (a comma followed 424051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by a space). Elements are converted to strings as by 424151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(byte)</tt>. Returns <tt>"null"</tt> if 424251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>a</tt> is <tt>null</tt>. 424351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 424451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 424551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 424651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 424751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 424851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(byte[] a) { 424951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 425051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 425151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 425251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 425351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 425451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 425551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 425651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 425751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 425851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 425951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 426051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 426151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 426251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 426351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 426451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 426551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 426651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 426751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 426851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 426951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 427051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 427151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(boolean)</tt>. Returns <tt>"null"</tt> if 427251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>a</tt> is <tt>null</tt>. 427351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 427451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 427551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 427651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 427751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 427851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(boolean[] a) { 427951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 428051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 428151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 428251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 428351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 428451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 428551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 428651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 428751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 428851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 428951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 429051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 429151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 429251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 429351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 429451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 429551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 429651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 429751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 429851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 429951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 430051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 430151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(float)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 430251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 430351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 430451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 430551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 430651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 430751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 430851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(float[] a) { 430951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 431051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 431151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 431251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 431351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 431451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 431551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 431651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 431751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 431851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 431951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 432051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 432151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 432251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 432351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 432451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 432551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 432651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 432751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 432851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The string representation consists of a list of the array's elements, 432951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * enclosed in square brackets (<tt>"[]"</tt>). Adjacent elements are 433051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * separated by the characters <tt>", "</tt> (a comma followed by a 433151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * space). Elements are converted to strings as by 433251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(double)</tt>. Returns <tt>"null"</tt> if <tt>a</tt> 433351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 433451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 433551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 433651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 433751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 433851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 433951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(double[] a) { 434051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 434151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 434251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 434351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 434451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 434551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 434651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 434751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 434851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 434951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(a[i]); 435051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 435151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 435251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 435351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 435451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 435551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 435651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 435751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the contents of the specified array. 435851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the array contains other arrays as elements, they are converted to 435951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * strings by the {@link Object#toString} method inherited from 436051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Object</tt>, which describes their <i>identities</i> rather than 436151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * their contents. 436251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 436351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The value returned by this method is equal to the value that would 436451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * be returned by <tt>Arrays.asList(a).toString()</tt>, unless <tt>a</tt> 436551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>, in which case <tt>"null"</tt> is returned. 436651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 436751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 436851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 436951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #deepToString(Object[]) 437051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 437151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 437251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String toString(Object[] a) { 437351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 437451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 437551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 437651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 437751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) 437851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "[]"; 437951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 438051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder b = new StringBuilder(); 438151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append('['); 438251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 438351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(String.valueOf(a[i])); 438451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 438551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return b.append(']').toString(); 438651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b.append(", "); 438751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 438851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 438951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 439051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 439151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns a string representation of the "deep contents" of the specified 439251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array. If the array contains other arrays as elements, the string 439351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * representation contains their contents and so on. This method is 439451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * designed for converting multidimensional arrays to strings. 439551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 439651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>The string representation consists of a list of the array's 439751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements, enclosed in square brackets (<tt>"[]"</tt>). Adjacent 439851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * elements are separated by the characters <tt>", "</tt> (a comma 439951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * followed by a space). Elements are converted to strings as by 440051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>String.valueOf(Object)</tt>, unless they are themselves 440151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * arrays. 440251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 440351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>If an element <tt>e</tt> is an array of a primitive type, it is 440451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * converted to a string as by invoking the appropriate overloading of 440551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>Arrays.toString(e)</tt>. If an element <tt>e</tt> is an array of a 440651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reference type, it is converted to a string as by invoking 440751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this method recursively. 440851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 440951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>To avoid infinite recursion, if the specified array contains itself 441051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as an element, or contains an indirect reference to itself through one 441151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or more levels of arrays, the self-reference is converted to the string 441251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>"[...]"</tt>. For example, an array containing only a reference 441351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to itself would be rendered as <tt>"[[...]]"</tt>. 441451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 441551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p>This method returns <tt>"null"</tt> if the specified array 441651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is <tt>null</tt>. 441751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 441851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param a the array whose string representation to return 441951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a string representation of <tt>a</tt> 442051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see #toString(Object[]) 442151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.5 442251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 442351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public static String deepToString(Object[] a) { 442451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) 442551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return "null"; 442651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 442751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int bufLen = 20 * a.length; 442851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a.length != 0 && bufLen <= 0) 442951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski bufLen = Integer.MAX_VALUE; 443051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuilder buf = new StringBuilder(bufLen); 443151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski deepToString(a, buf, new HashSet<Object[]>()); 443251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return buf.toString(); 443351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 443451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 443551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private static void deepToString(Object[] a, StringBuilder buf, 443651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Set<Object[]> dejaVu) { 443751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (a == null) { 443851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("null"); 443951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 444051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 444151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int iMax = a.length - 1; 444251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (iMax == -1) { 444351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("[]"); 444451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 444551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 444651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 444751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dejaVu.add(a); 444851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append('['); 444951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0; ; i++) { 445051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 445151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Object element = a[i]; 445251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (element == null) { 445351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("null"); 445451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { 445551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski Class eClass = element.getClass(); 445651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 445751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (eClass.isArray()) { 445851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (eClass == byte[].class) 445951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((byte[]) element)); 446051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == short[].class) 446151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((short[]) element)); 446251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == int[].class) 446351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((int[]) element)); 446451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == long[].class) 446551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((long[]) element)); 446651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == char[].class) 446751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((char[]) element)); 446851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == float[].class) 446951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((float[]) element)); 447051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == double[].class) 447151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((double[]) element)); 447251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else if (eClass == boolean[].class) 447351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(toString((boolean[]) element)); 447451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else { // element is an array of object references 447551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (dejaVu.contains(element)) 447651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append("[...]"); 447751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski else 447851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski deepToString((Object[])element, buf, dejaVu); 447951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 448051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } else { // element is non-null and not an array 448151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(element.toString()); 448251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 448351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 448451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (i == iMax) 448551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski break; 448651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(", "); 448751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 448851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski buf.append(']'); 448951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski dejaVu.remove(a); 449051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 4491983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski 4492983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski /** 4493e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4494e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4495e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4496e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4497e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4498e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4499e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param <T> type of elements of the array 4500e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4501e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4502e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4503e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4504e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4505e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4506e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static <T> void setAll(T[] array, IntFunction<? extends T> generator) { 4507e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4508e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4509e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.apply(i); 4510e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4511e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4512e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4513e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4514e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4515e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4516e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4517e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4518e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4519e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4520e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4521e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4522e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4523e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4524e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4525e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static void setAll(int[] array, IntUnaryOperator generator) { 4526e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4527e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4528e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.applyAsInt(i); 4529e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4530e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4531e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4532e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4533e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4534e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4535e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4536e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4537e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4538e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4539e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4540e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4541e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4542e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4543e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4544e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static void setAll(long[] array, IntToLongFunction generator) { 4545e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4546e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4547e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.applyAsLong(i); 4548e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4549e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4550e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4551e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * Set all elements of the specified array, using the provided 4552e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * generator function to compute each element. 4553e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4554e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * <p>If the generator function throws an exception, it is relayed to 4555e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * the caller and the array is left in an indeterminate state. 4556e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * 4557e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param array array to be initialized 4558e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @param generator a function accepting an index and producing the desired 4559e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * value for that position 4560e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @throws NullPointerException if the generator is null 4561e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera * @since 1.8 4562e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera */ 4563e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera public static void setAll(double[] array, IntToDoubleFunction generator) { 4564e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera Objects.requireNonNull(generator); 4565e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera for (int i = 0; i < array.length; i++) 4566e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera array[i] = generator.applyAsDouble(i); 4567e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera } 4568e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera 4569e1e167444be3b2d2c232f0b6f941b25f3ce4231aShubham Ajmera /** 4570983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * Checks that the range described by {@code offset} and {@code count} doesn't exceed 4571983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * {@code arrayLength}. 4572983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * 4573983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * Android changed. 4574983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski * @hide 4575983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski */ 4576983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski public static void checkOffsetAndCount(int arrayLength, int offset, int count) { 4577983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski if ((offset | count) < 0 || offset > arrayLength || arrayLength - offset < count) { 4578983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski throw new ArrayIndexOutOfBoundsException(arrayLength, offset, 4579983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski count); 4580983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski } 4581983b2c6ff9ea6d35adf7ab6398dccf870b7e180aPiotr Jastrzebski } 45824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 45834c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 45844c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 45854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator} covering all of the specified array. 45864c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 45874c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 45884c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 45894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 45904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 45914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param <T> type of elements 45924c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 45934c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 45944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 45954c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 45964c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static <T> Spliterator<T> spliterator(T[] array) { 45974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 45984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 45994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 46004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 46014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 46024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator} covering the specified range of the 46034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * specified array. 46044c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46054c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 46064c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 46074c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 46084c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46094c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param <T> type of elements 46104c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 46114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 46124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 46134c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 46144c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 46154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 46164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 46174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 46184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 46194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 46204c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) { 46214c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 46224c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 46234c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 46244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 46254c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 46264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfInt} covering all of the specified array. 46274c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46284c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 46294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 46304c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 46314c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46324c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 46334c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 46344c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 46354c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 46364c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfInt spliterator(int[] array) { 46374c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 46384c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 46394c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 46404c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 46414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 46424c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfInt} covering the specified range of the 46434c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * specified array. 46444c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46454c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 46464c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 46474c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 46484c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46494c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 46504c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 46514c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 46524c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 46534c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 46544c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 46554c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 46564c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 46574c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 46584c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 46594c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) { 46604c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 46614c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 46624c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 46634c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 46644c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 46654c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfLong} covering all of the specified array. 46664c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46674c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 46684c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 46694c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 46704c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46714c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 46724c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return the spliterator for the array elements 46734c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 46744c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 46754c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfLong spliterator(long[] array) { 46764c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 46774c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 46784c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 46794c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 46804c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 46814c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfLong} covering the specified range of the 46824c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * specified array. 46834c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46844c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 46854c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 46864c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 46874c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 46884c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 46894c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 46904c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 46914c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 46924c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 46934c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 46944c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 46954c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 46964c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 46974c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 46984c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) { 46994c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 47004c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 47014c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 47024c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 47034c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 47044c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfDouble} covering all of the specified 47054c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * array. 47064c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 47074c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 47084c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 47094c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 47104c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 47114c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 47124c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 47134c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 47144c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 47154c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfDouble spliterator(double[] array) { 47164c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, 47174c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 47184c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 47194c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath 47204c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath /** 47214c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * Returns a {@link Spliterator.OfDouble} covering the specified range of 47224c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the specified array. 47234c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 47244c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * <p>The spliterator reports {@link Spliterator#SIZED}, 47254c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#SUBSIZED}, {@link Spliterator#ORDERED}, and 47264c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@link Spliterator#IMMUTABLE}. 47274c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * 47284c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param array the array, assumed to be unmodified during use 47294c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param startInclusive the first index to cover, inclusive 47304c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @param endExclusive index immediately past the last index to cover 47314c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @return a spliterator for the array elements 47324c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 47334c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * negative, {@code endExclusive} is less than 47344c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * {@code startInclusive}, or {@code endExclusive} is greater than 47354c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * the array size 47364c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath * @since 1.8 47374c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath */ 47384c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath public static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) { 47394c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath return Spliterators.spliterator(array, startInclusive, endExclusive, 47404c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath Spliterator.ORDERED | Spliterator.IMMUTABLE); 47414c89023ef86f29fa9add7db2574f2169fe842577Narayan Kamath } 4742ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4743ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4744ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4745ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link Stream} with the specified array as its 4746ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 4747ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4748ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param <T> The type of the array elements 4749ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array The array, assumed to be unmodified during use 4750ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code Stream} for the array 4751ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4752ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4753ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static <T> Stream<T> stream(T[] array) { 4754ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 4755ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 4756ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4757ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4758ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link Stream} with the specified range of the 4759ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 4760ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4761ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param <T> the type of the array elements 4762ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 4763ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 4764ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 4765ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code Stream} for the array range 4766ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 4767ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 4768ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 4769ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 4770ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4771ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4772ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) { 4773ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.stream(spliterator(array, startInclusive, endExclusive), false); 4774ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 4775ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4776ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4777ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link IntStream} with the specified array as its 4778ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 4779ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4780ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 4781ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return an {@code IntStream} for the array 4782ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4783ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4784ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static IntStream stream(int[] array) { 4785ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 4786ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 4787ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4788ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4789ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link IntStream} with the specified range of the 4790ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 4791ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4792ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 4793ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 4794ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 4795ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return an {@code IntStream} for the array range 4796ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 4797ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 4798ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 4799ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 4800ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4801ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4802ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static IntStream stream(int[] array, int startInclusive, int endExclusive) { 4803ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.intStream(spliterator(array, startInclusive, endExclusive), false); 4804ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 4805ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4806ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4807ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link LongStream} with the specified array as its 4808ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 4809ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4810ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 4811ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code LongStream} for the array 4812ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4813ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4814ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static LongStream stream(long[] array) { 4815ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 4816ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 4817ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4818ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4819ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link LongStream} with the specified range of the 4820ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 4821ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4822ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 4823ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 4824ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 4825ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code LongStream} for the array range 4826ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 4827ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 4828ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 4829ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 4830ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4831ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4832ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static LongStream stream(long[] array, int startInclusive, int endExclusive) { 4833ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.longStream(spliterator(array, startInclusive, endExclusive), false); 4834ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 4835ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4836ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4837ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link DoubleStream} with the specified array as its 4838ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * source. 4839ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4840ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 4841ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code DoubleStream} for the array 4842ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4843ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4844ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static DoubleStream stream(double[] array) { 4845ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return stream(array, 0, array.length); 4846ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 4847ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin 4848ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin /** 4849ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * Returns a sequential {@link DoubleStream} with the specified range of the 4850ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * specified array as its source. 4851ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * 4852ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param array the array, assumed to be unmodified during use 4853ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param startInclusive the first index to cover, inclusive 4854ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @param endExclusive index immediately past the last index to cover 4855ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @return a {@code DoubleStream} for the array range 4856ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @throws ArrayIndexOutOfBoundsException if {@code startInclusive} is 4857ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * negative, {@code endExclusive} is less than 4858ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * {@code startInclusive}, or {@code endExclusive} is greater than 4859ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * the array size 4860ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin * @since 1.8 4861ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin */ 4862ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin public static DoubleStream stream(double[] array, int startInclusive, int endExclusive) { 4863ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin return StreamSupport.doubleStream(spliterator(array, startInclusive, endExclusive), false); 4864ff18b5f136f92154f2e05217e3953d10f459e561Igor Murashkin } 486551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 4866