Class FourBitBinarySearch

java.lang.Object
com.dannemann.stringcompressor.search.BaseBinarySearch
com.dannemann.stringcompressor.search.FourBitBinarySearch

public final class FourBitBinarySearch extends BaseBinarySearch

Performs binary search on data compressed by FourBitAsciiCompressor. The data must have been sorted prior to compression.

If prefixSearch is true, the method returns the first element whose prefix matches the specified key. Otherwise, it looks for an exact match. When multiple elements share the same prefix, the first matching element is returned (just as in the exact‐match search).

Null elements are considered to come after any character, in the same way that Z comes after A. This is because the compressedData array typically has extra space to accommodate new entries, so unused slots (nulls) are placed at the end.

Note that character ordering depends on the sequence defined in your custom charset (via supportedCharset), which is passed to the compressor constructor (see FourBitAsciiCompressor(byte[])). If no custom charset is provided, compressors use a default charset ordered by ASCII.

See Also:
  • Constructor Details

    • FourBitBinarySearch

      public FourBitBinarySearch(byte[][] compressedData, boolean prefixSearch)
      Creates a binary search object for data compressed with the default character set FourBitAsciiCompressor.DEFAULT_4BIT_CHARSET.
      Parameters:
      compressedData - The mass of compressed strings to search through.
      prefixSearch - If true, searches for elements starting with the provided key prefix (must be unique).
      See Also:
    • FourBitBinarySearch

      public FourBitBinarySearch(byte[][] compressedData, boolean prefixSearch, byte[] charset)
      Creates a binary search object.
      Parameters:
      compressedData - The mass of compressed strings to search through.
      prefixSearch - If true, searches for elements starting with the provided key prefix (must be unique).
      charset - Character set used to compress compressedData.
  • Method Details

    • search

      public int search(byte[] key)

      Performs a binary search on the provided compressed data array to locate the specified key.

      The compressed data is expected to be produced by FourBitAsciiCompressor and must be sorted before compression for this search to work correctly. The search is performed directly on the compressed form without decompressing the entire dataset, enabling fast lookups in large in-memory compressed collections.

      The method returns the index of the matching element if found; otherwise, it returns -(insertion point) - 1, following the contract of Arrays.binarySearch(long[], long).

      Specified by:
      search in class BaseBinarySearch
      Parameters:
      key - The uncompressed key to search for, as a byte array.
      Returns:
      The index of the search key if it is found; otherwise, -(insertion point) - 1.