Class BufferUtil


  • public final class BufferUtil
    extends Object
    Various useful methods for roaring bitmaps. This class is similar to org.roaringbitmap.Util but meant to be used with memory mapping.
    • Method Detail

      • addOffset

        public static MappeableContainer[] addOffset​(MappeableContainer source,
                                                     short offsets)
        Add value "offset" to all values in the container, producing two new containers. The existing container remains unchanged. The new container are not converted, so they need to be checked: e.g., we could produce two bitmap containers having low cardinality.
        Parameters:
        source - source container
        offsets - value to add to each value in the container
        Returns:
        return an array made of two containers
      • advanceUntil

        protected static int advanceUntil​(ShortBuffer array,
                                          int pos,
                                          int length,
                                          short min)
        Find the smallest integer larger than pos such that array[pos]>= min. If none can be found, return length. Based on code by O. Kaser.
        Parameters:
        array - container where we search
        pos - initial position
        min - minimal threshold
        length - how big should the array consider to be
        Returns:
        x greater than pos such that array[pos] is at least as large as min, pos is is equal to length if it is not possible.
      • iterateUntil

        public static int iterateUntil​(ShortBuffer array,
                                       int pos,
                                       int length,
                                       int min)
        Find the smallest integer larger than pos such that array[pos]>= min. If none can be found, return length.
        Parameters:
        array - array to search within
        pos - starting position of the search
        length - length of the array to search
        min - minimum value
        Returns:
        x greater than pos such that array[pos] is at least as large as min, pos is is equal to length if it is not possible.
      • arraycopy

        protected static void arraycopy​(ShortBuffer src,
                                        int srcPos,
                                        ShortBuffer dest,
                                        int destPos,
                                        int length)
      • branchyUnsignedBinarySearch

        protected static int branchyUnsignedBinarySearch​(ShortBuffer array,
                                                         int begin,
                                                         int end,
                                                         short k)
      • branchyUnsignedBinarySearch

        protected static int branchyUnsignedBinarySearch​(ByteBuffer array,
                                                         int position,
                                                         int begin,
                                                         int end,
                                                         short k)
      • compareUnsigned

        public static int compareUnsigned​(short a,
                                          short b)
        Compares the two specified short values, treating them as unsigned values between 0 and 2^16 - 1 inclusive.
        Parameters:
        a - the first unsigned short to compare
        b - the second unsigned short to compare
        Returns:
        a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
      • fillArrayAND

        protected static void fillArrayAND​(short[] container,
                                           LongBuffer bitmap1,
                                           LongBuffer bitmap2)
      • fillArrayANDNOT

        protected static void fillArrayANDNOT​(short[] container,
                                              LongBuffer bitmap1,
                                              LongBuffer bitmap2)
      • fillArrayXOR

        protected static void fillArrayXOR​(short[] container,
                                           LongBuffer bitmap1,
                                           LongBuffer bitmap2)
      • flipBitmapRange

        public static void flipBitmapRange​(LongBuffer bitmap,
                                           int start,
                                           int end)
        flip bits at start, start+1,..., end-1
        Parameters:
        bitmap - array of words to be modified
        start - first index to be modified (inclusive)
        end - last index to be modified (exclusive)
      • cardinalityInBitmapRange

        public static int cardinalityInBitmapRange​(LongBuffer bitmap,
                                                   int start,
                                                   int end)
        Hamming weight of the bitset in the range start, start+1,..., end-1
        Parameters:
        bitmap - array of words representing a bitset
        start - first index (inclusive)
        end - last index (exclusive)
        Returns:
        the hamming weight of the corresponding range
      • setBitmapRangeAndCardinalityChange

        @Deprecated
        public static int setBitmapRangeAndCardinalityChange​(LongBuffer bitmap,
                                                             int start,
                                                             int end)
        Deprecated.
        set bits at start, start+1,..., end-1 and report the cardinality change
        Parameters:
        bitmap - array of words to be modified
        start - first index to be modified (inclusive)
        end - last index to be modified (exclusive)
        Returns:
        cardinality change
      • flipBitmapRangeAndCardinalityChange

        @Deprecated
        public static int flipBitmapRangeAndCardinalityChange​(LongBuffer bitmap,
                                                              int start,
                                                              int end)
        Deprecated.
        flip bits at start, start+1,..., end-1 and report the cardinality change
        Parameters:
        bitmap - array of words to be modified
        start - first index to be modified (inclusive)
        end - last index to be modified (exclusive)
        Returns:
        cardinality change
      • resetBitmapRangeAndCardinalityChange

        @Deprecated
        public static int resetBitmapRangeAndCardinalityChange​(LongBuffer bitmap,
                                                               int start,
                                                               int end)
        Deprecated.
        reset bits at start, start+1,..., end-1 and report the cardinality change
        Parameters:
        bitmap - array of words to be modified
        start - first index to be modified (inclusive)
        end - last index to be modified (exclusive)
        Returns:
        cardinality change
      • getSizeInBytesFromCardinalityEtc

        protected static int getSizeInBytesFromCardinalityEtc​(int card,
                                                              int numRuns,
                                                              boolean isRunEncoded)
        From the cardinality of a container, compute the corresponding size in bytes of the container. Additional information is required if the container is run encoded.
        Parameters:
        card - the cardinality if this is not run encoded, otherwise ignored
        numRuns - number of runs if run encoded, othewise ignored
        isRunEncoded - boolean
        Returns:
        the size in bytes
      • highbits

        protected static short highbits​(int x)
      • highbits

        protected static short highbits​(long x)
      • isBackedBySimpleArray

        protected static boolean isBackedBySimpleArray​(Buffer b)
        Checks whether the Buffer is backed by a simple array. In java, a Buffer is an abstraction that can represent various data, from data on disk all the way to native Java arrays. Like all abstractions, a Buffer might carry a performance penalty. Thus, we sometimes check whether the Buffer is simply a wrapper around a Java array. In these instances, it might be best, from a performance point of view, to access the underlying array (using the array()) method.
        Parameters:
        b - the provided Buffer
        Returns:
        whether the Buffer is backed by a simple array
      • lowbits

        protected static short lowbits​(int x)
      • lowbits

        protected static short lowbits​(long x)
      • maxLowBit

        protected static short maxLowBit()
      • maxLowBitAsInteger

        protected static int maxLowBitAsInteger()
      • resetBitmapRange

        public static void resetBitmapRange​(LongBuffer bitmap,
                                            int start,
                                            int end)
        clear bits at start, start+1,..., end-1
        Parameters:
        bitmap - array of words to be modified
        start - first index to be modified (inclusive)
        end - last index to be modified (exclusive)
      • setBitmapRange

        public static void setBitmapRange​(LongBuffer bitmap,
                                          int start,
                                          int end)
        set bits at start, start+1,..., end-1
        Parameters:
        bitmap - array of words to be modified
        start - first index to be modified (inclusive)
        end - last index to be modified (exclusive)
      • toIntUnsigned

        protected static int toIntUnsigned​(short x)
      • unsignedBinarySearch

        public static int unsignedBinarySearch​(ShortBuffer array,
                                               int begin,
                                               int end,
                                               short k)
        Look for value k in buffer in the range [begin,end). If the value is found, return its index. If not, return -(i+1) where i is the index where the value would be inserted. The buffer is assumed to contain sorted values where shorts are interpreted as unsigned integers.
        Parameters:
        array - buffer where we search
        begin - first index (inclusive)
        end - last index (exclusive)
        k - value we search for
        Returns:
        count
      • unsignedBinarySearch

        public static int unsignedBinarySearch​(ByteBuffer array,
                                               int position,
                                               int begin,
                                               int end,
                                               short k)
        Look for value k in buffer in the range [begin,end). If the value is found, return its index. If not, return -(i+1) where i is the index where the value would be inserted. The buffer is assumed to contain sorted values where shorts are interpreted as unsigned integers.
        Parameters:
        array - buffer where we search
        position - starting position of the container in the ByteBuffer
        begin - first index (inclusive)
        end - last index (exclusive)
        k - value we search for
        Returns:
        count
      • unsignedDifference

        protected static int unsignedDifference​(ShortBuffer set1,
                                                int length1,
                                                ShortBuffer set2,
                                                int length2,
                                                short[] buffer)
      • unsignedExclusiveUnion2by2

        protected static int unsignedExclusiveUnion2by2​(ShortBuffer set1,
                                                        int length1,
                                                        ShortBuffer set2,
                                                        int length2,
                                                        short[] buffer)
      • unsignedIntersect2by2

        protected static int unsignedIntersect2by2​(ShortBuffer set1,
                                                   int length1,
                                                   ShortBuffer set2,
                                                   int length2,
                                                   short[] buffer)
      • unsignedIntersects

        public static boolean unsignedIntersects​(ShortBuffer set1,
                                                 int length1,
                                                 ShortBuffer set2,
                                                 int length2)
        Checks if two arrays intersect
        Parameters:
        set1 - first array
        length1 - length of first array
        set2 - second array
        length2 - length of second array
        Returns:
        true if they intersect
      • unsignedLocalIntersect2by2

        protected static int unsignedLocalIntersect2by2​(ShortBuffer set1,
                                                        int length1,
                                                        ShortBuffer set2,
                                                        int length2,
                                                        short[] buffer)
      • unsignedLocalIntersect2by2Cardinality

        protected static int unsignedLocalIntersect2by2Cardinality​(ShortBuffer set1,
                                                                   int length1,
                                                                   ShortBuffer set2,
                                                                   int length2)
      • unsignedOneSidedGallopingIntersect2by2

        protected static int unsignedOneSidedGallopingIntersect2by2​(ShortBuffer smallSet,
                                                                    int smallLength,
                                                                    ShortBuffer largeSet,
                                                                    int largeLength,
                                                                    short[] buffer)
      • unsignedUnion2by2

        protected static int unsignedUnion2by2​(ShortBuffer set1,
                                               int offset1,
                                               int length1,
                                               ShortBuffer set2,
                                               int offset2,
                                               int length2,
                                               short[] buffer)