Class ArrayUtil


  • public final class ArrayUtil
    extends java.lang.Object

    Since:
    4.0
    Version:
    0.3
    Author:
    Pedro Oliveira
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int[] copyOf​(int[] theArray)  
      static int[] copyOf​(int[] theArray, int theLength)  
      static long[] copyOf​(long[] theArray)  
      static long[] copyOf​(long[] theArray, int theLength)  
      static <T> T[] copyOf​(T[] theArray)  
      static <T> T[] copyOf​(T[] theArray, int theLength)  
      static <T> int deferredEqualityBinarySearch​(T[] theData, T theElement, int theStart, int theEnd, java.util.Comparator<T> theComparator)
      Implementation of a binary search which uses the deferred equality approach.
      static <T> int deferredEqualityBinarySearchAfter​(T[] theData, T theElement, int theStart, int theEnd, java.util.Comparator<T> theComparator)
      static <T> int deferredEqualityBinarySearchBefore​(T[] theData, T theElement, int theStart, int theEnd, java.util.Comparator<T> theComparator)  
      static <T> int deferredEqualityBinarySearchBefore​(T[] theData, T theElement, java.util.Comparator<T> theComparator)  
      static <T> void delete​(long[] data, int size, int pos)  
      static <T> void delete​(T[] data, int size, int pos)  
      static <T> T[] ensureSize​(T[] theArray, int newSize)  
      static <T> T[] increaseSize​(T[] theArray)  
      static long[] insert​(long[] data, int size, int pos, long theElement)  
      static <T> T[] insert​(T[] data, int size, int pos, T theElement)  
      static boolean isSorted​(long[] theArray)  
      static <T> com.google.common.collect.PeekingIterator<T> iterator​(T[] theArray)
      Iterator for the given array
      static <T> com.google.common.collect.PeekingIterator<T> iterator​(T[] theArray, int theStart, int theEnd)
      Iterator for the given array
      static byte[] merge​(byte[] theFirst, byte[] theSecond)  
      static long[] merge​(long[] theFirst, long[] theSecond)
      Merges two sorted arrays and removes duplicates
      static <T> int merge​(T[] theFirstArray, int theFirstSize, T[] theSecondArray, int theSecondSize, T[] theResult, java.util.Comparator<T> theComparator)
      Merges two sorted arrays without duplicates into a new array.
      static <T> T[] newArray​(int theLength)  
      static int normalizeAfter​(int thePosition)  
      static int normalizeBefore​(int thePosition)  
      static <T> T onlyMatch​(T[] array, java.util.function.Predicate<T> predicate)  
      static boolean rangeEquals​(byte[] bytes1, int offset1, byte[] bytes2, int offset2, int length)  
      static <T> int removeDuplicates​(T[] theSortedArray, int theSize)
      Removes duplicates from a sorted array by moving the unique elements to the front of the array and returns the number of unique elements.
      static <T> int removeDuplicates​(T[] theSortedArray, int theSize, java.util.Comparator<T> theComparator)  
      static <T> int search​(T[] theArray, T theObject, int theStart, int theEnd, java.util.Comparator<T> theComparator)  
      static <T> int search​(T[] theArray, T theObject, java.util.Comparator<T> theComparator)  
      static <T> int searchAfter​(T[] theArray, T theObject, int theStart, int theEnd, java.util.Comparator<T> theComparator)  
      static <T> int searchAfter​(T[] theArray, T theObject, java.util.Comparator<T> theComparator)  
      static <T> int searchBefore​(T[] theArray, T theObject, int theStart, int theEnd, java.util.Comparator<T> theComparator)  
      static <T> int searchBefore​(T[] theArray, T theObject, java.util.Comparator<T> theComparator)  
      static int sequentialSearch​(long[] theArray, int theSize, long theElement)  
      static <T> int sequentialSearch​(T[] theArray, int theSize, T theElement)  
      static <T> int sequentialSearch​(T[] theArray, T theElement)  
      static <T> void sort​(T[] theArray, int theStart, int theEnd, java.util.Comparator<? super T> theComparator)  
      static <T> void sort​(T[] theArray, java.util.Comparator<T> theComparator)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
    • Method Detail

      • iterator

        public static <T> com.google.common.collect.PeekingIterator<T> iterator​(T[] theArray,
                                                                                int theStart,
                                                                                int theEnd)
        Iterator for the given array
        Type Parameters:
        T -
        Parameters:
        theArray - The Array to iterate over
        theStart - The starting point (inclusive)
        theEnd - The ending point (exclusive)
        Returns:
      • iterator

        public static <T> com.google.common.collect.PeekingIterator<T> iterator​(T[] theArray)
        Iterator for the given array
        Type Parameters:
        T -
        Parameters:
        theArray - The Array to iterate over
        Returns:
      • search

        public static <T> int search​(T[] theArray,
                                     T theObject,
                                     int theStart,
                                     int theEnd,
                                     java.util.Comparator<T> theComparator)
      • search

        public static <T> int search​(T[] theArray,
                                     T theObject,
                                     java.util.Comparator<T> theComparator)
      • searchAfter

        public static <T> int searchAfter​(T[] theArray,
                                          T theObject,
                                          java.util.Comparator<T> theComparator)
        Type Parameters:
        T -
        Parameters:
        theArray -
        theObject -
        theComparator -
        Returns:
        If found, the position of the element. Otherwise, the position of the next bigger element.
      • searchAfter

        public static <T> int searchAfter​(T[] theArray,
                                          T theObject,
                                          int theStart,
                                          int theEnd,
                                          java.util.Comparator<T> theComparator)
      • deferredEqualityBinarySearch

        public static <T> int deferredEqualityBinarySearch​(T[] theData,
                                                           T theElement,
                                                           int theStart,
                                                           int theEnd,
                                                           java.util.Comparator<T> theComparator)
        Implementation of a binary search which uses the deferred equality approach. This trades some efficiency -- more iterations will be performed -- for the guarantee that the minimum matching element is always selected. This is the preferred search if you have duplicate elements in the array, or the Comparator is lax in that it can return 0 for multiple elements that are not .equals.
        Type Parameters:
        T - the type of elements in the array
        Parameters:
        theData - the array to search
        theElement - the element to search for
        theStart - the start position, inclusive
        theEnd - the end position, exclusive
        theComparator - the comparator
        Returns:
        the position of the element, not found returned as a negative position same as Arrays.binarySearch(long[], long)
        See Also:
        description
      • deferredEqualityBinarySearchAfter

        public static <T> int deferredEqualityBinarySearchAfter​(T[] theData,
                                                                T theElement,
                                                                int theStart,
                                                                int theEnd,
                                                                java.util.Comparator<T> theComparator)
        Type Parameters:
        T - the type of elements in the array
        Parameters:
        theData - the array to search
        theElement - the element to search for
        theStart - the start index of the search
        theEnd - the end index of the search
        theComparator - the comparator
        Returns:
        the position of the element in the array. This will always be a positive value, the index of the actual element in the list, or it will be the location the element whould be in the list if it was present. That is, when the element is not present you can use the return value as the location to insert the element such that the ordering is maintained
      • deferredEqualityBinarySearchBefore

        public static <T> int deferredEqualityBinarySearchBefore​(T[] theData,
                                                                 T theElement,
                                                                 java.util.Comparator<T> theComparator)
      • deferredEqualityBinarySearchBefore

        public static <T> int deferredEqualityBinarySearchBefore​(T[] theData,
                                                                 T theElement,
                                                                 int theStart,
                                                                 int theEnd,
                                                                 java.util.Comparator<T> theComparator)
      • searchBefore

        public static <T> int searchBefore​(T[] theArray,
                                           T theObject,
                                           java.util.Comparator<T> theComparator)
        Type Parameters:
        T -
        Parameters:
        theArray -
        theObject -
        theComparator -
        Returns:
        If found, the position of the element. Otherwise, the position of the next smaller element.
      • searchBefore

        public static <T> int searchBefore​(T[] theArray,
                                           T theObject,
                                           int theStart,
                                           int theEnd,
                                           java.util.Comparator<T> theComparator)
      • copyOf

        public static <T> T[] copyOf​(T[] theArray)
      • normalizeAfter

        public static int normalizeAfter​(int thePosition)
      • normalizeBefore

        public static int normalizeBefore​(int thePosition)
      • increaseSize

        public static <T> T[] increaseSize​(T[] theArray)
      • ensureSize

        public static <T> T[] ensureSize​(T[] theArray,
                                         int newSize)
      • copyOf

        public static <T> T[] copyOf​(T[] theArray,
                                     int theLength)
      • copyOf

        public static long[] copyOf​(long[] theArray)
      • copyOf

        public static long[] copyOf​(long[] theArray,
                                    int theLength)
      • copyOf

        public static int[] copyOf​(int[] theArray)
      • copyOf

        public static int[] copyOf​(int[] theArray,
                                   int theLength)
      • newArray

        public static <T> T[] newArray​(int theLength)
      • sequentialSearch

        public static int sequentialSearch​(long[] theArray,
                                           int theSize,
                                           long theElement)
      • sequentialSearch

        public static <T> int sequentialSearch​(T[] theArray,
                                               T theElement)
      • sequentialSearch

        public static <T> int sequentialSearch​(T[] theArray,
                                               int theSize,
                                               T theElement)
      • sort

        public static <T> void sort​(T[] theArray,
                                    int theStart,
                                    int theEnd,
                                    java.util.Comparator<? super T> theComparator)
      • isSorted

        public static boolean isSorted​(long[] theArray)
      • sort

        public static <T> void sort​(T[] theArray,
                                    java.util.Comparator<T> theComparator)
      • merge

        public static byte[] merge​(byte[] theFirst,
                                   byte[] theSecond)
      • merge

        public static long[] merge​(long[] theFirst,
                                   long[] theSecond)
        Merges two sorted arrays and removes duplicates
        Parameters:
        theFirst - the first array
        theSecond - the second array
        Returns:
        the resulting array
      • removeDuplicates

        public static <T> int removeDuplicates​(T[] theSortedArray,
                                               int theSize)
        Removes duplicates from a sorted array by moving the unique elements to the front of the array and returns the number of unique elements.
        Parameters:
        theSortedArray - a sorted array that possibly contains duplicates
        theSize - the size of the array that contains valid elements, elements beyond this index are completely ignored
        Returns:
        the number of unique elements in the array
      • removeDuplicates

        public static <T> int removeDuplicates​(T[] theSortedArray,
                                               int theSize,
                                               java.util.Comparator<T> theComparator)
      • merge

        public static <T> int merge​(T[] theFirstArray,
                                    int theFirstSize,
                                    T[] theSecondArray,
                                    int theSecondSize,
                                    T[] theResult,
                                    java.util.Comparator<T> theComparator)
        Merges two sorted arrays without duplicates into a new array. Both arrays should already be sorted with the given comparator. Neither array should contain duplicates within itself but there may be common elements that exist in both arrays. The resulting array will be sorted and won't contain duplicates either.
        Type Parameters:
        T -
        Parameters:
        theFirstArray - the first array to be merged
        theFirstSize - the size of valid elements in the first array
        theSecondArray - the second array to be merged
        theSecondSize - the size of valid elements in the second array
        theResult - the result array where elements will be copied
        theComparator - the comparator for the element
        Returns:
        the size of valid elements in the resulting array, typically equal to the sum of first and second size might be less if the arrays have some elements in common
      • delete

        public static <T> void delete​(T[] data,
                                      int size,
                                      int pos)
      • delete

        public static <T> void delete​(long[] data,
                                      int size,
                                      int pos)
      • insert

        public static <T> T[] insert​(T[] data,
                                     int size,
                                     int pos,
                                     T theElement)
      • insert

        public static long[] insert​(long[] data,
                                    int size,
                                    int pos,
                                    long theElement)
      • rangeEquals

        public static boolean rangeEquals​(byte[] bytes1,
                                          int offset1,
                                          byte[] bytes2,
                                          int offset2,
                                          int length)
      • onlyMatch

        public static <T> T onlyMatch​(T[] array,
                                      java.util.function.Predicate<T> predicate)