Class ArrayUtil


  • public final class ArrayUtil
    extends Object
    Methods for manipulating arrays.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_ARRAY_LENGTH
      Maximum length for an array; we set this to "a bit" below Integer.MAX_VALUE because the exact max allowed byte[] is JVM dependent, so we want to avoid a case where a large value worked during indexing on one JVM but failed later at search time with a different JVM.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean equals​(byte[] left, int offsetLeft, byte[] right, int offsetRight, int length)
      See if two array slices are the same.
      static boolean equals​(char[] left, int offsetLeft, char[] right, int offsetRight, int length)
      See if two array slices are the same.
      static boolean equals​(int[] left, int offsetLeft, int[] right, int offsetRight, int length)
      See if two array slices are the same.
      static int getShrinkSize​(int currentSize, int targetSize, int bytesPerElement)  
      static boolean[] grow​(boolean[] array)  
      static boolean[] grow​(boolean[] array, int minSize)  
      static byte[] grow​(byte[] array)  
      static byte[] grow​(byte[] array, int minSize)  
      static char[] grow​(char[] array)  
      static char[] grow​(char[] array, int minSize)  
      static double[] grow​(double[] array)  
      static double[] grow​(double[] array, int minSize)  
      static float[] grow​(float[] array)  
      static float[][] grow​(float[][] array)  
      static float[][] grow​(float[][] array, int minSize)  
      static float[] grow​(float[] array, int minSize)  
      static int[] grow​(int[] array)  
      static int[][] grow​(int[][] array)  
      static int[][] grow​(int[][] array, int minSize)  
      static int[] grow​(int[] array, int minSize)  
      static long[] grow​(long[] array)  
      static long[] grow​(long[] array, int minSize)  
      static short[] grow​(short[] array)  
      static short[] grow​(short[] array, int minSize)  
      static int hashCode​(byte[] array, int start, int end)
      Returns hash of bytes in range start (inclusive) to end (inclusive)
      static int hashCode​(char[] array, int start, int end)
      Returns hash of chars in range start (inclusive) to end (inclusive)
      static <T extends Comparable<? super T>>
      void
      introSort​(T[] a)
      Sorts the given array in natural order.
      static <T extends Comparable<? super T>>
      void
      introSort​(T[] a, int fromIndex, int toIndex)
      Sorts the given array slice in natural order.
      static <T> void introSort​(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
      Sorts the given array slice using the Comparator.
      static <T> void introSort​(T[] a, Comparator<? super T> comp)
      Sorts the given array using the Comparator.
      static <T extends Comparable<? super T>>
      Comparator<T>
      naturalComparator()
      Get the natural Comparator for the provided object class.
      static int oversize​(int minTargetSize, int bytesPerElement)
      Returns an array size >= minTargetSize, generally over-allocating exponentially to achieve amortized linear-time cost as the array grows.
      static int parseInt​(char[] chars)
      Parses the string argument as if it was an int value and returns the result.
      static int parseInt​(char[] chars, int offset, int len)
      Parses a char array into an int.
      static int parseInt​(char[] chars, int offset, int len, int radix)
      Parses the string argument as if it was an int value and returns the result.
      static boolean[] shrink​(boolean[] array, int targetSize)  
      static byte[] shrink​(byte[] array, int targetSize)  
      static char[] shrink​(char[] array, int targetSize)  
      static float[][] shrink​(float[][] array, int targetSize)  
      static int[][] shrink​(int[][] array, int targetSize)  
      static int[] shrink​(int[] array, int targetSize)  
      static long[] shrink​(long[] array, int targetSize)  
      static short[] shrink​(short[] array, int targetSize)  
      static <T> void swap​(T[] arr, int i, int j)
      Swap values stored in slots i and j
      static <T extends Comparable<? super T>>
      void
      timSort​(T[] a)
      Sorts the given array in natural order.
      static <T extends Comparable<? super T>>
      void
      timSort​(T[] a, int fromIndex, int toIndex)
      Sorts the given array slice in natural order.
      static <T> void timSort​(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
      Sorts the given array slice using the Comparator.
      static <T> void timSort​(T[] a, Comparator<? super T> comp)
      Sorts the given array using the Comparator.
      static int[] toIntArray​(Collection<Integer> ints)  
    • Field Detail

      • MAX_ARRAY_LENGTH

        public static final int MAX_ARRAY_LENGTH
        Maximum length for an array; we set this to "a bit" below Integer.MAX_VALUE because the exact max allowed byte[] is JVM dependent, so we want to avoid a case where a large value worked during indexing on one JVM but failed later at search time with a different JVM.
        See Also:
        Constant Field Values
    • Method Detail

      • parseInt

        public static int parseInt​(char[] chars)
                            throws NumberFormatException
        Parses the string argument as if it was an int value and returns the result. Throws NumberFormatException if the string does not represent an int quantity.
        Parameters:
        chars - a string representation of an int quantity.
        Returns:
        int the value represented by the argument
        Throws:
        NumberFormatException - if the argument could not be parsed as an int quantity.
      • parseInt

        public static int parseInt​(char[] chars,
                                   int offset,
                                   int len)
                            throws NumberFormatException
        Parses a char array into an int.
        Parameters:
        chars - the character array
        offset - The offset into the array
        len - The length
        Returns:
        the int
        Throws:
        NumberFormatException - if it can't parse
      • parseInt

        public static int parseInt​(char[] chars,
                                   int offset,
                                   int len,
                                   int radix)
                            throws NumberFormatException
        Parses the string argument as if it was an int value and returns the result. Throws NumberFormatException if the string does not represent an int quantity. The second argument specifies the radix to use when parsing the value.
        Parameters:
        chars - a string representation of an int quantity.
        radix - the base to use for conversion.
        Returns:
        int the value represented by the argument
        Throws:
        NumberFormatException - if the argument could not be parsed as an int quantity.
      • oversize

        public static int oversize​(int minTargetSize,
                                   int bytesPerElement)
        Returns an array size >= minTargetSize, generally over-allocating exponentially to achieve amortized linear-time cost as the array grows. NOTE: this was originally borrowed from Python 2.4.2 listobject.c sources (attribution in LICENSE.txt), but has now been substantially changed based on discussions from java-dev thread with subject "Dynamic array reallocation algorithms", started on Jan 12 2010.
        Parameters:
        minTargetSize - Minimum required value to be returned.
        bytesPerElement - Bytes used by each element of the array. See constants in RamUsageEstimator.
      • getShrinkSize

        public static int getShrinkSize​(int currentSize,
                                        int targetSize,
                                        int bytesPerElement)
      • grow

        public static short[] grow​(short[] array,
                                   int minSize)
      • grow

        public static short[] grow​(short[] array)
      • grow

        public static float[] grow​(float[] array,
                                   int minSize)
      • grow

        public static float[] grow​(float[] array)
      • grow

        public static double[] grow​(double[] array,
                                    int minSize)
      • grow

        public static double[] grow​(double[] array)
      • shrink

        public static short[] shrink​(short[] array,
                                     int targetSize)
      • grow

        public static int[] grow​(int[] array,
                                 int minSize)
      • grow

        public static int[] grow​(int[] array)
      • shrink

        public static int[] shrink​(int[] array,
                                   int targetSize)
      • grow

        public static long[] grow​(long[] array,
                                  int minSize)
      • grow

        public static long[] grow​(long[] array)
      • shrink

        public static long[] shrink​(long[] array,
                                    int targetSize)
      • grow

        public static byte[] grow​(byte[] array,
                                  int minSize)
      • grow

        public static byte[] grow​(byte[] array)
      • shrink

        public static byte[] shrink​(byte[] array,
                                    int targetSize)
      • grow

        public static boolean[] grow​(boolean[] array,
                                     int minSize)
      • grow

        public static boolean[] grow​(boolean[] array)
      • shrink

        public static boolean[] shrink​(boolean[] array,
                                       int targetSize)
      • grow

        public static char[] grow​(char[] array,
                                  int minSize)
      • grow

        public static char[] grow​(char[] array)
      • shrink

        public static char[] shrink​(char[] array,
                                    int targetSize)
      • grow

        public static int[][] grow​(int[][] array,
                                   int minSize)
      • grow

        public static int[][] grow​(int[][] array)
      • shrink

        public static int[][] shrink​(int[][] array,
                                     int targetSize)
      • grow

        public static float[][] grow​(float[][] array,
                                     int minSize)
      • grow

        public static float[][] grow​(float[][] array)
      • shrink

        public static float[][] shrink​(float[][] array,
                                       int targetSize)
      • hashCode

        public static int hashCode​(char[] array,
                                   int start,
                                   int end)
        Returns hash of chars in range start (inclusive) to end (inclusive)
      • hashCode

        public static int hashCode​(byte[] array,
                                   int start,
                                   int end)
        Returns hash of bytes in range start (inclusive) to end (inclusive)
      • equals

        public static boolean equals​(char[] left,
                                     int offsetLeft,
                                     char[] right,
                                     int offsetRight,
                                     int length)
        See if two array slices are the same.
        Parameters:
        left - The left array to compare
        offsetLeft - The offset into the array. Must be positive
        right - The right array to compare
        offsetRight - the offset into the right array. Must be positive
        length - The length of the section of the array to compare
        Returns:
        true if the two arrays, starting at their respective offsets, are equal
        See Also:
        Arrays.equals(char[], char[])
      • equals

        public static boolean equals​(byte[] left,
                                     int offsetLeft,
                                     byte[] right,
                                     int offsetRight,
                                     int length)
        See if two array slices are the same.
        Parameters:
        left - The left array to compare
        offsetLeft - The offset into the array. Must be positive
        right - The right array to compare
        offsetRight - the offset into the right array. Must be positive
        length - The length of the section of the array to compare
        Returns:
        true if the two arrays, starting at their respective offsets, are equal
        See Also:
        Arrays.equals(byte[], byte[])
      • equals

        public static boolean equals​(int[] left,
                                     int offsetLeft,
                                     int[] right,
                                     int offsetRight,
                                     int length)
        See if two array slices are the same.
        Parameters:
        left - The left array to compare
        offsetLeft - The offset into the array. Must be positive
        right - The right array to compare
        offsetRight - the offset into the right array. Must be positive
        length - The length of the section of the array to compare
        Returns:
        true if the two arrays, starting at their respective offsets, are equal
        See Also:
        Arrays.equals(char[], char[])
      • naturalComparator

        public static <T extends Comparable<? super T>> Comparator<T> naturalComparator()
        Get the natural Comparator for the provided object class.
      • swap

        public static <T> void swap​(T[] arr,
                                    int i,
                                    int j)
        Swap values stored in slots i and j
      • introSort

        public static <T> void introSort​(T[] a,
                                         int fromIndex,
                                         int toIndex,
                                         Comparator<? super T> comp)
        Sorts the given array slice using the Comparator. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.
        Parameters:
        fromIndex - start index (inclusive)
        toIndex - end index (exclusive)
      • introSort

        public static <T> void introSort​(T[] a,
                                         Comparator<? super T> comp)
        Sorts the given array using the Comparator. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.
      • introSort

        public static <T extends Comparable<? super T>> void introSort​(T[] a,
                                                                       int fromIndex,
                                                                       int toIndex)
        Sorts the given array slice in natural order. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.
        Parameters:
        fromIndex - start index (inclusive)
        toIndex - end index (exclusive)
      • introSort

        public static <T extends Comparable<? super T>> void introSort​(T[] a)
        Sorts the given array in natural order. This method uses the intro sort algorithm, but falls back to insertion sort for small arrays.
      • timSort

        public static <T> void timSort​(T[] a,
                                       int fromIndex,
                                       int toIndex,
                                       Comparator<? super T> comp)
        Sorts the given array slice using the Comparator. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.
        Parameters:
        fromIndex - start index (inclusive)
        toIndex - end index (exclusive)
      • timSort

        public static <T> void timSort​(T[] a,
                                       Comparator<? super T> comp)
        Sorts the given array using the Comparator. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.
      • timSort

        public static <T extends Comparable<? super T>> void timSort​(T[] a,
                                                                     int fromIndex,
                                                                     int toIndex)
        Sorts the given array slice in natural order. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.
        Parameters:
        fromIndex - start index (inclusive)
        toIndex - end index (exclusive)
      • timSort

        public static <T extends Comparable<? super T>> void timSort​(T[] a)
        Sorts the given array in natural order. This method uses the Tim sort algorithm, but falls back to binary sort for small arrays.