Package pixy.util

Class ArrayUtils

java.lang.Object
pixy.util.ArrayUtils

public class ArrayUtils
extends java.lang.Object
Array utility class
Version:
1.0 09/18/2012
Author:
Wen Yu, yuwen_66@yahoo.com
  • Method Summary

    Modifier and Type Method Description
    static java.util.List<java.lang.Integer> asList​(int[] a)  
    static void bubbleSort​(int[] array)  
    static <T extends java.lang.Comparable<? super T>>
    void
    bubbleSort​(T[] array)  
    static <T> boolean checkDuplicate​(T[] input)
    Since Set doesn't allow duplicates add() return false if we try to add duplicates into Set and this property can be used to check if array contains duplicates.
    static byte[] concat​(byte[] first, byte[]... rest)  
    static <T> T[] concat​(java.lang.Class<T> type, T[]... arrays)
    Type safe concatenation of arrays with upper type bound T
    static <T> T[] concat​(T[]... arrays)
    Type safe concatenation of arrays with upper type bound T
    static int findEqualOrLess​(int[] a, int key)  
    static int findEqualOrLess​(int[] a, int fromIndex, int toIndex, int key)
    Find the index of the element which is equal or less than the key.
    static <T> int findEqualOrLess​(T[] a, int fromIndex, int toIndex, T key, java.util.Comparator<? super T> c)  
    static <T> int findEqualOrLess​(T[] a, T key, java.util.Comparator<? super T> c)  
    static byte[] flipEndian​(byte[] input, int offset, int len, int bits, int scanLineStride, boolean bigEndian)
    Flip the endian of the input byte-compacted array
    static int fromFloat​(float fval)  
    static byte[] getBitReverseTable()
    Since nonzero-length array is always mutable, we should return a clone of the underlying array as BIT_REVERSE_TABLE.clone().
    static void insertionsort​(int[] array)  
    static void insertionsort​(int[] array, int start, int end)  
    static <T extends java.lang.Comparable<? super T>>
    void
    insertionsort​(T[] array)  
    static <T extends java.lang.Comparable<? super T>>
    void
    insertionsort​(T[] array, int start, int end)  
    static void mergesort​(int[] array)  
    static void mergesort​(int[] array, int left, int right)  
    static <T extends java.lang.Comparable<? super T>>
    void
    mergesort​(T[] array)  
    static <T extends java.lang.Comparable<? super T>>
    void
    mergesort​(T[] array, int left, int right)  
    static byte[] packByteArray​(byte[] input, int start, int bits, int len)
    Packs all or part of the input byte array which uses "bits" bits to use all 8 bits.
    static byte[] packByteArray​(byte[] input, int stride, int start, int bits, int len)
    Packs all or part of the input byte array which uses "bits" bits to use all 8 bits.
    static void quicksort​(int[] array)  
    static void quicksort​(int[] array, int start, int end)  
    static <T extends java.lang.Comparable<? super T>>
    void
    quicksort​(T[] array)  
    static <T extends java.lang.Comparable<? super T>>
    void
    quicksort​(T[] array, int low, int high)  
    static int[] removeDuplicates​(int[] input)
    Remove duplicate elements from an int array
    static byte[] reverse​(byte[] array)  
    static <T> void reverse​(T[] data)  
    static void reverseBits​(byte[] input)  
    static void shellsort​(int[] array)  
    static void shellsort​(int[] array, int start, int end)  
    static <T extends java.lang.Comparable<? super T>>
    void
    shellsort​(T[] array)  
    static <T extends java.lang.Comparable<? super T>>
    void
    shellsort​(T[] array, int start, int end)  
    static byte[] subArray​(byte[] src, int offset, int len)  
    static float[] to16BitFloatArray​(byte[] data, boolean bigEndian)  
    static float[] to24BitFloatArray​(byte[] data, boolean bigEndian)  
    static int[] to32BitsLongArray​(byte[] data, boolean bigEndian)  
    static byte[] toByteArray​(int value)  
    static byte[] toByteArray​(int[] data, boolean bigEndian)  
    static byte[] toByteArray​(long[] data, boolean bigEndian)  
    static byte[] toByteArray​(short value)  
    static byte[] toByteArray​(short[] data, boolean bigEndian)  
    static byte[] toByteArrayMM​(int value)  
    static byte[] toByteArrayMM​(short value)  
    static double[] toDoubleArray​(byte[] data, boolean bigEndian)  
    static double[] toDoubleArray​(byte[] data, int offset, int len, boolean bigEndian)  
    static float toFloat​(int lbits)  
    static float[] toFloatArray​(byte[] data, boolean bigEndian)  
    static float[] toFloatArray​(byte[] data, int offset, int len, boolean bigEndian)  
    static int[] toIntArray​(byte[] data, boolean bigEndian)  
    static int[] toIntArray​(byte[] data, int offset, int len, boolean bigEndian)  
    static long[] toLongArray​(byte[] data, boolean bigEndian)  
    static long[] toLongArray​(byte[] data, int offset, int len, boolean bigEndian)  
    static java.lang.Object toNBits​(int nBits, byte[] input, int stride, boolean bigEndian)
    Converts an input byte array to nBits data array using the smallest data type which can hold the nBits data.
    static double[] toPrimitive​(java.lang.Double[] doubles)  
    static float[] toPrimitive​(java.lang.Float[] floats)  
    static int[] toPrimitive​(java.lang.Integer[] integers)  
    static long[] toPrimitive​(java.lang.Long[] longs)  
    static short[] toPrimitive​(java.lang.Short[] shorts)  
    static short[] toShortArray​(byte[] data, boolean bigEndian)  
    static short[] toShortArray​(byte[] data, int offset, int len, boolean bigEndian)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • asList

      public static java.util.List<java.lang.Integer> asList​(int[] a)
    • bubbleSort

      public static void bubbleSort​(int[] array)
    • bubbleSort

      public static <T extends java.lang.Comparable<? super T>> void bubbleSort​(T[] array)
    • checkDuplicate

      public static <T> boolean checkDuplicate​(T[] input)
      Since Set doesn't allow duplicates add() return false if we try to add duplicates into Set and this property can be used to check if array contains duplicates.
      Parameters:
      input - input array
      Returns:
      true if input array contains duplicates, otherwise false.
    • concat

      public static byte[] concat​(byte[] first, byte[]... rest)
    • concat

      public static <T> T[] concat​(T[]... arrays)
      Type safe concatenation of arrays with upper type bound T

      Note: if type parameter is not explicitly supplied, it will be inferred as the upper bound for the two parameters.

      Parameters:
      arrays - the arrays to be concatenated
      Returns:
      a concatenation of the input arrays
      Throws:
      java.lang.NullPointerException - if any of the input array is null
    • concat

      public static <T> T[] concat​(java.lang.Class<T> type, T[]... arrays)
      Type safe concatenation of arrays with upper type bound T
      Parameters:
      type - type bound for the concatenated array
      arrays - arrays to be concatenated
      Returns:
      a concatenation of the arrays.
      Throws:
      java.lang.NullPointerException - if any of the arrays to be concatenated is null.
    • findEqualOrLess

      public static int findEqualOrLess​(int[] a, int key)
    • findEqualOrLess

      public static int findEqualOrLess​(int[] a, int fromIndex, int toIndex, int key)
      Find the index of the element which is equal or less than the key. The array must be sorted in ascending order.
      Parameters:
      a - the array to be searched
      key - the value to be searched for
      Returns:
      index of the search key or index of the element which is closest to but less than the search key or -1 is the search key is less than the first element of the array.
    • findEqualOrLess

      public static <T> int findEqualOrLess​(T[] a, int fromIndex, int toIndex, T key, java.util.Comparator<? super T> c)
    • findEqualOrLess

      public static <T> int findEqualOrLess​(T[] a, T key, java.util.Comparator<? super T> c)
    • flipEndian

      public static byte[] flipEndian​(byte[] input, int offset, int len, int bits, int scanLineStride, boolean bigEndian)
      Flip the endian of the input byte-compacted array
      Parameters:
      input - the input byte array which is byte compacted
      offset - the offset to start the reading input
      len - number of bytes to read
      bits - number of bits for the data before compaction
      scanLineStride - scan line stride to skip bits
      bigEndian - whether or not the input data is in big endian order
      Returns:
      a byte array with the endian flipped
    • fromFloat

      public static int fromFloat​(float fval)
    • getBitReverseTable

      public static byte[] getBitReverseTable()
      Since nonzero-length array is always mutable, we should return a clone of the underlying array as BIT_REVERSE_TABLE.clone().
      Returns:
      the byte reverse table.
    • insertionsort

      public static void insertionsort​(int[] array)
    • insertionsort

      public static void insertionsort​(int[] array, int start, int end)
    • insertionsort

      public static <T extends java.lang.Comparable<? super T>> void insertionsort​(T[] array)
    • insertionsort

      public static <T extends java.lang.Comparable<? super T>> void insertionsort​(T[] array, int start, int end)
    • mergesort

      public static void mergesort​(int[] array)
    • mergesort

      public static void mergesort​(int[] array, int left, int right)
    • mergesort

      public static <T extends java.lang.Comparable<? super T>> void mergesort​(T[] array)
    • mergesort

      public static <T extends java.lang.Comparable<? super T>> void mergesort​(T[] array, int left, int right)
    • packByteArray

      public static byte[] packByteArray​(byte[] input, int start, int bits, int len)
      Packs all or part of the input byte array which uses "bits" bits to use all 8 bits.
      Parameters:
      input - input byte array
      start - offset of the input array to start packing
      bits - number of bits used by the input array
      len - number of bytes from the input to be packed
      Returns:
      the packed byte array
    • packByteArray

      public static byte[] packByteArray​(byte[] input, int stride, int start, int bits, int len)
      Packs all or part of the input byte array which uses "bits" bits to use all 8 bits.

      We assume len is a multiplication of stride. The parameter stride controls the packing unit length and different units DO NOT share same byte. This happens when packing image data where each scan line MUST start at byte boundary like TIFF.

      Parameters:
      input - input byte array to be packed
      stride - length of packing unit
      start - offset of the input array to start packing
      bits - number of bits used in each byte of the input
      len - number of input bytes to be packed
      Returns:
      the packed byte array
    • quicksort

      public static void quicksort​(int[] array)
    • quicksort

      public static void quicksort​(int[] array, int start, int end)
    • quicksort

      public static <T extends java.lang.Comparable<? super T>> void quicksort​(T[] array)
    • quicksort

      public static <T extends java.lang.Comparable<? super T>> void quicksort​(T[] array, int low, int high)
    • removeDuplicates

      public static int[] removeDuplicates​(int[] input)
      Remove duplicate elements from an int array
      Parameters:
      input - input unsorted int array
      Returns:
      a sorted int array with unique elements
    • reverseBits

      public static void reverseBits​(byte[] input)
    • reverse

      public static byte[] reverse​(byte[] array)
    • reverse

      public static <T> void reverse​(T[] data)
    • shellsort

      public static void shellsort​(int[] array)
    • shellsort

      public static void shellsort​(int[] array, int start, int end)
    • shellsort

      public static <T extends java.lang.Comparable<? super T>> void shellsort​(T[] array)
    • shellsort

      public static <T extends java.lang.Comparable<? super T>> void shellsort​(T[] array, int start, int end)
    • subArray

      public static byte[] subArray​(byte[] src, int offset, int len)
    • to16BitFloatArray

      public static float[] to16BitFloatArray​(byte[] data, boolean bigEndian)
    • to24BitFloatArray

      public static float[] to24BitFloatArray​(byte[] data, boolean bigEndian)
    • to32BitsLongArray

      public static int[] to32BitsLongArray​(byte[] data, boolean bigEndian)
    • toByteArray

      public static byte[] toByteArray​(int value)
    • toByteArray

      public static byte[] toByteArray​(int[] data, boolean bigEndian)
    • toByteArray

      public static byte[] toByteArray​(long[] data, boolean bigEndian)
    • toByteArray

      public static byte[] toByteArray​(short value)
    • toByteArray

      public static byte[] toByteArray​(short[] data, boolean bigEndian)
    • toByteArrayMM

      public static byte[] toByteArrayMM​(int value)
    • toByteArrayMM

      public static byte[] toByteArrayMM​(short value)
    • toDoubleArray

      public static double[] toDoubleArray​(byte[] data, boolean bigEndian)
    • toDoubleArray

      public static double[] toDoubleArray​(byte[] data, int offset, int len, boolean bigEndian)
    • toFloat

      public static float toFloat​(int lbits)
    • toFloatArray

      public static float[] toFloatArray​(byte[] data, boolean bigEndian)
    • toFloatArray

      public static float[] toFloatArray​(byte[] data, int offset, int len, boolean bigEndian)
    • toIntArray

      public static int[] toIntArray​(byte[] data, boolean bigEndian)
    • toIntArray

      public static int[] toIntArray​(byte[] data, int offset, int len, boolean bigEndian)
    • toLongArray

      public static long[] toLongArray​(byte[] data, boolean bigEndian)
    • toLongArray

      public static long[] toLongArray​(byte[] data, int offset, int len, boolean bigEndian)
    • toNBits

      public static java.lang.Object toNBits​(int nBits, byte[] input, int stride, boolean bigEndian)
      Converts an input byte array to nBits data array using the smallest data type which can hold the nBits data. Each data type contains only one data element.
      Parameters:
      nBits - number of bits for the data element
      input - the input array for the data elements
      stride - scan line stride used to discard remaining bits
      bigEndian - the packing order of the bits. True if bigEndian, otherwise false.
      Returns:
      an array of the smallest data type which can hold the nBits data
    • toPrimitive

      public static double[] toPrimitive​(java.lang.Double[] doubles)
    • toPrimitive

      public static float[] toPrimitive​(java.lang.Float[] floats)
    • toPrimitive

      public static int[] toPrimitive​(java.lang.Integer[] integers)
    • toPrimitive

      public static long[] toPrimitive​(java.lang.Long[] longs)
    • toPrimitive

      public static short[] toPrimitive​(java.lang.Short[] shorts)
    • toShortArray

      public static short[] toShortArray​(byte[] data, boolean bigEndian)
    • toShortArray

      public static short[] toShortArray​(byte[] data, int offset, int len, boolean bigEndian)