Enum BufferSearch

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<BufferSearch>

    public enum BufferSearch
    extends java.lang.Enum<BufferSearch>
    Provides the primitive array binary search implementations from java.util.Arrays, modified with minimal changes to support nio buffers.

    Also provides methods for finding the first or last index of a sequence of duplicate values.

    Author:
    biteytech@protonmail.com, adapted from java.util.Arrays
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int binaryFindFirst​(java.nio.ByteBuffer b, int minIndex, int keyIndex)
      Searches a range of the specified ByteBuffer for the first occurrence of the value at the given keyIndex.
      static int binaryFindFirst​(java.nio.DoubleBuffer b, int minIndex, int keyIndex)
      Searches a range of the specified DoubleBuffer for the first occurrence of the value at the given keyIndex.
      static int binaryFindFirst​(java.nio.FloatBuffer b, int minIndex, int keyIndex)
      Searches a range of the specified FloatBuffer for the first occurrence of the value at the given keyIndex.
      static int binaryFindFirst​(java.nio.IntBuffer b, int minIndex, int keyIndex)
      Searches a range of the specified IntBuffer for the first occurrence of the value at the given keyIndex.
      static int binaryFindFirst​(java.nio.LongBuffer b, int minIndex, int keyIndex)
      Searches a range of the specified LongBuffer for the first occurrence of the value at the given keyIndex.
      static int binaryFindFirst​(java.nio.ShortBuffer b, int minIndex, int keyIndex)
      Searches a range of the specified ShortBuffer for the first occurrence of the value at the given keyIndex.
      static int binaryFindLast​(java.nio.ByteBuffer b, int maxIndex, int keyIndex)
      Searches a range of the specified ByteBuffer for the last occurrence of the value at the given keyIndex.
      static int binaryFindLast​(java.nio.DoubleBuffer b, int maxIndex, int keyIndex)
      Searches a range of the specified DoubleBuffer for the last occurrence of the value at the given keyIndex.
      static int binaryFindLast​(java.nio.FloatBuffer b, int maxIndex, int keyIndex)
      Searches a range of the specified FloatBuffer for the last occurrence of the value at the given keyIndex.
      static int binaryFindLast​(java.nio.IntBuffer b, int maxIndex, int keyIndex)
      Searches a range of the specified IntBuffer for the last occurrence of the value at the given keyIndex.
      static int binaryFindLast​(java.nio.LongBuffer b, int maxIndex, int keyIndex)
      Searches a range of the specified LongBuffer for the last occurrence of the value at the given keyIndex.
      static int binaryFindLast​(java.nio.ShortBuffer b, int maxIndex, int keyIndex)
      Searches a range of the specified ShortBuffer for the last occurrence of the value at the given keyIndex.
      static int binarySearch​(java.nio.ByteBuffer b, int fromIndex, int toIndex, byte key)
      Searches a range of the specified ByteBuffer for the specified value using the binary search algorithm.
      static int binarySearch​(java.nio.DoubleBuffer b, int fromIndex, int toIndex, double key)
      Searches a range of the specified DoubleBuffer for the specified value using the binary search algorithm.
      static int binarySearch​(java.nio.FloatBuffer b, int fromIndex, int toIndex, float key)
      Searches a range of the specified FloatBuffer for the specified value using the binary search algorithm.
      static int binarySearch​(java.nio.IntBuffer b, int fromIndex, int toIndex, int key)
      Searches a range of the specified IntBuffer for the specified value using the binary search algorithm.
      static int binarySearch​(java.nio.LongBuffer b, int fromIndex, int toIndex, long key)
      Searches a range of the specified LongBuffer for the specified value using the binary search algorithm.
      static int binarySearch​(java.nio.ShortBuffer b, int fromIndex, int toIndex, short key)
      Searches a range of the specified ShortBuffer for the specified value using the binary search algorithm.
      static BufferSearch valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static BufferSearch[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • values

        public static BufferSearch[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (BufferSearch c : BufferSearch.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static BufferSearch valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • binarySearch

        public static int binarySearch​(java.nio.IntBuffer b,
                                       int fromIndex,
                                       int toIndex,
                                       int key)
        Searches a range of the specified IntBuffer for the specified value using the binary search algorithm. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.
        Parameters:
        b - the buffer to be searched
        fromIndex - the index of the first element (inclusive) to be searched
        toIndex - the index of the last element (exclusive) to be searched
        key - the value to be searched for
        Returns:
        index of the search key, if it is contained in the buffer within the specified range; otherwise, (-(<i>insertion point</i>) - 1). The insertion point is defined as the point at which the key would be inserted into the buffer: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • binarySearch

        public static int binarySearch​(java.nio.LongBuffer b,
                                       int fromIndex,
                                       int toIndex,
                                       long key)
        Searches a range of the specified LongBuffer for the specified value using the binary search algorithm. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.
        Parameters:
        b - the buffer to be searched
        fromIndex - the index of the first element (inclusive) to be searched
        toIndex - the index of the last element (exclusive) to be searched
        key - the value to be searched for
        Returns:
        index of the search key, if it is contained in the buffer within the specified range; otherwise, (-(<i>insertion point</i>) - 1). The insertion point is defined as the point at which the key would be inserted into the buffer: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • binarySearch

        public static int binarySearch​(java.nio.ShortBuffer b,
                                       int fromIndex,
                                       int toIndex,
                                       short key)
        Searches a range of the specified ShortBuffer for the specified value using the binary search algorithm. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.
        Parameters:
        b - the buffer to be searched
        fromIndex - the index of the first element (inclusive) to be searched
        toIndex - the index of the last element (exclusive) to be searched
        key - the value to be searched for
        Returns:
        index of the search key, if it is contained in the buffer within the specified range; otherwise, (-(<i>insertion point</i>) - 1). The insertion point is defined as the point at which the key would be inserted into the buffer: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • binarySearch

        public static int binarySearch​(java.nio.ByteBuffer b,
                                       int fromIndex,
                                       int toIndex,
                                       byte key)
        Searches a range of the specified ByteBuffer for the specified value using the binary search algorithm. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.
        Parameters:
        b - the buffer to be searched
        fromIndex - the index of the first element (inclusive) to be searched
        toIndex - the index of the last element (exclusive) to be searched
        key - the value to be searched for
        Returns:
        index of the search key, if it is contained in the buffer within the specified range; otherwise, (-(<i>insertion point</i>) - 1). The insertion point is defined as the point at which the key would be inserted into the buffer: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • binarySearch

        public static int binarySearch​(java.nio.FloatBuffer b,
                                       int fromIndex,
                                       int toIndex,
                                       float key)
        Searches a range of the specified FloatBuffer for the specified value using the binary search algorithm. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found. This method considers all NaN values to be equivalent and equal.
        Parameters:
        b - the buffer to be searched
        fromIndex - the index of the first element (inclusive) to be searched
        toIndex - the index of the last element (exclusive) to be searched
        key - the value to be searched for
        Returns:
        index of the search key, if it is contained in the buffer within the specified range; otherwise, (-(<i>insertion point</i>) - 1). The insertion point is defined as the point at which the key would be inserted into the buffer: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • binarySearch

        public static int binarySearch​(java.nio.DoubleBuffer b,
                                       int fromIndex,
                                       int toIndex,
                                       double key)
        Searches a range of the specified DoubleBuffer for the specified value using the binary search algorithm. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found. This method considers all NaN values to be equivalent and equal.
        Parameters:
        b - the buffer to be searched
        fromIndex - the index of the first element (inclusive) to be searched
        toIndex - the index of the last element (exclusive) to be searched
        key - the value to be searched for
        Returns:
        index of the search key, if it is contained in the buffer within the specified range; otherwise, (-(<i>insertion point</i>) - 1). The insertion point is defined as the point at which the key would be inserted into the buffer: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
        Throws:
        java.lang.IllegalArgumentException - if fromIndex > toIndex
        java.lang.IndexOutOfBoundsException - if fromIndex < 0 or toIndex > b.capacity()
      • binaryFindFirst

        public static int binaryFindFirst​(java.nio.IntBuffer b,
                                          int minIndex,
                                          int keyIndex)
        Searches a range of the specified IntBuffer for the first occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        minIndex - the lowest index to be searched
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the first occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if minIndex > keyIndex
        java.lang.IndexOutOfBoundsException - if minIndex < 0 or keyIndex >= b.capacity()
      • binaryFindLast

        public static int binaryFindLast​(java.nio.IntBuffer b,
                                         int maxIndex,
                                         int keyIndex)
        Searches a range of the specified IntBuffer for the last occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        maxIndex - the highest index to be searched (exclusive)
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the last occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if maxIndex < keyIndex
        java.lang.IndexOutOfBoundsException - if keyIndex < 0 or maxIndex > b.capacity()
      • binaryFindFirst

        public static int binaryFindFirst​(java.nio.LongBuffer b,
                                          int minIndex,
                                          int keyIndex)
        Searches a range of the specified LongBuffer for the first occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        minIndex - the lowest index to be searched
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the first occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if minIndex > keyIndex
        java.lang.IndexOutOfBoundsException - if minIndex < 0 or keyIndex >= b.capacity()
      • binaryFindLast

        public static int binaryFindLast​(java.nio.LongBuffer b,
                                         int maxIndex,
                                         int keyIndex)
        Searches a range of the specified LongBuffer for the last occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        maxIndex - the highest index to be searched (exclusive)
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the last occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if maxIndex < keyIndex
        java.lang.IndexOutOfBoundsException - if keyIndex < 0 or maxIndex > b.capacity()
      • binaryFindFirst

        public static int binaryFindFirst​(java.nio.ShortBuffer b,
                                          int minIndex,
                                          int keyIndex)
        Searches a range of the specified ShortBuffer for the first occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        minIndex - the lowest index to be searched
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the first occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if minIndex > keyIndex
        java.lang.IndexOutOfBoundsException - if minIndex < 0 or keyIndex >= b.capacity()
      • binaryFindLast

        public static int binaryFindLast​(java.nio.ShortBuffer b,
                                         int maxIndex,
                                         int keyIndex)
        Searches a range of the specified ShortBuffer for the last occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        maxIndex - the highest index to be searched (exclusive)
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the last occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if maxIndex < keyIndex
        java.lang.IndexOutOfBoundsException - if keyIndex < 0 or maxIndex > b.capacity()
      • binaryFindFirst

        public static int binaryFindFirst​(java.nio.ByteBuffer b,
                                          int minIndex,
                                          int keyIndex)
        Searches a range of the specified ByteBuffer for the first occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        minIndex - the lowest index to be searched
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the first occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if minIndex > keyIndex
        java.lang.IndexOutOfBoundsException - if minIndex < 0 or keyIndex >= b.capacity()
      • binaryFindLast

        public static int binaryFindLast​(java.nio.ByteBuffer b,
                                         int maxIndex,
                                         int keyIndex)
        Searches a range of the specified ByteBuffer for the last occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        maxIndex - the highest index to be searched (exclusive)
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the last occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if maxIndex < keyIndex
        java.lang.IndexOutOfBoundsException - if keyIndex < 0 or maxIndex > b.capacity()
      • binaryFindFirst

        public static int binaryFindFirst​(java.nio.FloatBuffer b,
                                          int minIndex,
                                          int keyIndex)
        Searches a range of the specified FloatBuffer for the first occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. This method considers all NaN values to be equivalent and equal.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        minIndex - the lowest index to be searched
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the first occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if minIndex > keyIndex
        java.lang.IndexOutOfBoundsException - if minIndex < 0 or keyIndex >= b.capacity()
      • binaryFindLast

        public static int binaryFindLast​(java.nio.FloatBuffer b,
                                         int maxIndex,
                                         int keyIndex)
        Searches a range of the specified FloatBuffer for the last occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. This method considers all NaN values to be equivalent and equal.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        maxIndex - the highest index to be searched (exclusive)
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the last occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if maxIndex < keyIndex
        java.lang.IndexOutOfBoundsException - if keyIndex < 0 or maxIndex > b.capacity()
      • binaryFindFirst

        public static int binaryFindFirst​(java.nio.DoubleBuffer b,
                                          int minIndex,
                                          int keyIndex)
        Searches a range of the specified DoubleBuffer for the first occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. This method considers all NaN values to be equivalent and equal.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        minIndex - the lowest index to be searched
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the first occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if minIndex > keyIndex
        java.lang.IndexOutOfBoundsException - if minIndex < 0 or keyIndex >= b.capacity()
      • binaryFindLast

        public static int binaryFindLast​(java.nio.DoubleBuffer b,
                                         int maxIndex,
                                         int keyIndex)
        Searches a range of the specified DoubleBuffer for the last occurrence of the value at the given keyIndex. The range must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. This method considers all NaN values to be equivalent and equal.

        This method is useful as a post-processing step after a binary search on a buffer which contains duplicate elements.

        Parameters:
        b - the buffer to be searched
        maxIndex - the highest index to be searched (exclusive)
        keyIndex - an index of the value for which to find the first occurrence (inclusive)
        Returns:
        index of the last occurrence of the value at keyIndex
        Throws:
        java.lang.IllegalArgumentException - if maxIndex < keyIndex
        java.lang.IndexOutOfBoundsException - if keyIndex < 0 or maxIndex > b.capacity()