Package org.roaringbitmap
Class RoaringBitmap
- java.lang.Object
-
- org.roaringbitmap.RoaringBitmap
-
- All Implemented Interfaces:
java.io.Externalizable,java.io.Serializable,java.lang.Cloneable,java.lang.Iterable<java.lang.Integer>,AppendableStorage<Container>,BitmapDataProvider,ImmutableBitmapDataProvider
- Direct Known Subclasses:
FastRankRoaringBitmap
public class RoaringBitmap extends java.lang.Object implements java.lang.Cloneable, java.io.Serializable, java.lang.Iterable<java.lang.Integer>, java.io.Externalizable, ImmutableBitmapDataProvider, BitmapDataProvider, AppendableStorage<Container>
RoaringBitmap, a compressed alternative to the BitSet.
Integers are added in unsigned sorted order. That is, they are treated as unsigned integers (see Java 8's Integer.toUnsignedLong function). Up to 4294967296 integers can be stored.import org.roaringbitmap.*; //... RoaringBitmap rr = RoaringBitmap.bitmapOf(1,2,3,1000); RoaringBitmap rr2 = new RoaringBitmap(); for(int k = 4000; k<4255;++k) rr2.add(k); RoaringBitmap rror = RoaringBitmap.or(rr, rr2); //... DataOutputStream wheretoserialize = ... rr.runOptimize(); // can help compression rr.serialize(wheretoserialize);- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description RoaringBitmap()Create an empty bitmapRoaringBitmap(ImmutableRoaringBitmap rb)Create a RoaringBitmap from a MutableRoaringBitmap or ImmutableRoaringBitmap.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(int x)Add the value to the container (set the value to "true"), whether it already appears or not.voidadd(int... dat)Set all the specified values to true.voidadd(int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangevoidadd(long rangeStart, long rangeEnd)Add to the current bitmap all integers in [rangeStart,rangeEnd).static RoaringBitmapadd(RoaringBitmap rb, int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangestatic RoaringBitmapadd(RoaringBitmap rb, long rangeStart, long rangeEnd)Generate a new bitmap with all integers in [rangeStart,rangeEnd) added.voidaddN(int[] dat, int offset, int n)Set the specified values to true, within given boundaries.static RoaringBitmapaddOffset(RoaringBitmap x, long offset)Generate a new bitmap, but with all its values incremented by offset.static RoaringBitmapand(java.util.Iterator<? extends RoaringBitmap> bitmaps, int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the range.static RoaringBitmapand(java.util.Iterator<? extends RoaringBitmap> bitmaps, long rangeStart, long rangeEnd)Computes AND between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)voidand(RoaringBitmap x2)In-place bitwise AND (intersection) operation.static RoaringBitmapand(RoaringBitmap x1, RoaringBitmap x2)Bitwise AND (intersection) operation.static intandCardinality(RoaringBitmap x1, RoaringBitmap x2)Cardinality of Bitwise AND (intersection) operation.voidandNot(RoaringBitmap x2)In-place bitwise ANDNOT (difference) operation.static RoaringBitmapandNot(RoaringBitmap x1, RoaringBitmap x2)Bitwise ANDNOT (difference) operation.static RoaringBitmapandNot(RoaringBitmap x1, RoaringBitmap x2, int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the range.static RoaringBitmapandNot(RoaringBitmap x1, RoaringBitmap x2, long rangeStart, long rangeEnd)Bitwise ANDNOT (difference) operation for the given range, rangeStart (inclusive) and rangeEnd (exclusive).static intandNotCardinality(RoaringBitmap x1, RoaringBitmap x2)Cardinality of the bitwise ANDNOT (left difference) operation.voidappend(char key, Container container)Appends the key and container to the storage, throws if the key is less than the current mark.static RoaringBitmapbitmapOf(int... dat)Generate a bitmap with the specified values set to true.static RoaringBitmapbitmapOfUnordered(int... data)Efficiently builds a RoaringBitmap from unordered databooleancheckedAdd(int x)Add the value to the container (set the value to "true"), whether it already appears or not.booleancheckedRemove(int x)If present remove the specified integer (effectively, sets its bit value to false)voidclear()reset to an empty bitmap; result occupies as much space a newly created bitmap.RoaringBitmapclone()booleancontains(int x)Checks whether the value is included, which is equivalent to checking if the corresponding bit is set (get in BitSet class).booleancontains(long minimum, long supremum)Checks if the bitmap contains the range.booleancontains(RoaringBitmap subset)Checks whether the parameter is a subset of this RoaringBitmap or notvoiddeserialize(java.io.DataInput in)Deserialize (retrieve) this bitmap.voiddeserialize(java.io.DataInput in, byte[] buffer)Deserialize (retrieve) this bitmap.voiddeserialize(java.nio.ByteBuffer bbf)Deserialize (retrieve) this bitmap.booleanequals(java.lang.Object o)intfirst()Get the first (smallest) integer in this RoaringBitmap, that is, returns the minimum of the set.voidflip(int x)Add the value if it is not already present, otherwise remove it.voidflip(int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangevoidflip(long rangeStart, long rangeEnd)Modifies the current bitmap by complementing the bits in the given range, from rangeStart (inclusive) rangeEnd (exclusive).static RoaringBitmapflip(RoaringBitmap rb, int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangestatic RoaringBitmapflip(RoaringBitmap bm, long rangeStart, long rangeEnd)Complements the bits in the given range, from rangeStart (inclusive) rangeEnd (exclusive).voidforEach(IntConsumer ic)Visit all values in the bitmap and pass them to the consumer.RoaringBatchIteratorgetBatchIterator()This iterator may be faster than othersintgetCardinality()Returns the number of distinct integers added to the bitmap (e.g., number of bits set).ContainerPointergetContainerPointer()Return a low-level container pointer that can be used to access the underlying data structure.PeekableIntIteratorgetIntIterator()For better performance, consider the Use theforEachmethod.longgetLongCardinality()Returns the number of distinct integers added to the bitmap (e.g., number of bits set).longgetLongSizeInBytes()Estimate of the memory usage of this data structure.IntIteratorgetReverseIntIterator()intgetSizeInBytes()Estimate of the memory usage of this data structure.inthashCode()booleanhasRunCompression()Check whether this bitmap has had its runs compressed.booleanintersects(long minimum, long supremum)Checks if the range intersects with the bitmap.static booleanintersects(RoaringBitmap x1, RoaringBitmap x2)Checks whether the two bitmaps intersect.booleanisEmpty()Checks whether the bitmap is empty.booleanisHammingSimilar(RoaringBitmap other, int tolerance)Returns true if the other bitmap has no more than tolerance bits differing from this bitmap.java.util.Iterator<java.lang.Integer>iterator()iterate over the positions of the true values.intlast()Get the last (largest) integer in this RoaringBitmap, that is, returns the maximum of the set.protected voidlazyor(RoaringBitmap x2)protected static RoaringBitmaplazyor(RoaringBitmap x1, RoaringBitmap x2)protected static RoaringBitmaplazyorfromlazyinputs(RoaringBitmap x1, RoaringBitmap x2)RoaringBitmaplimit(int maxcardinality)Create a new Roaring bitmap containing at most maxcardinality integers.static longmaximumSerializedSize(long cardinality, long universe_size)Assume that one wants to store "cardinality" integers in [0, universe_size), this function returns an upper bound on the serialized size in bytes.protected voidnaivelazyor(RoaringBitmap x2)longnextAbsentValue(int fromValue)Returns the first absent value equal to or larger than the provided value (interpreted as an unsigned integer).longnextValue(int fromValue)Returns the first value equal to or larger than the provided value (interpreted as an unsigned integer).static RoaringBitmapor(java.util.Iterator<? extends RoaringBitmap> bitmaps)Compute overall OR between bitmaps.static RoaringBitmapor(java.util.Iterator<? extends RoaringBitmap> bitmaps, int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the range.static RoaringBitmapor(java.util.Iterator<? extends RoaringBitmap> bitmaps, long rangeStart, long rangeEnd)Computes OR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)voidor(RoaringBitmap x2)In-place bitwise OR (union) operation.static RoaringBitmapor(RoaringBitmap... bitmaps)Compute overall OR between bitmaps.static RoaringBitmapor(RoaringBitmap x1, RoaringBitmap x2)Bitwise OR (union) operation.static intorCardinality(RoaringBitmap x1, RoaringBitmap x2)Cardinality of the bitwise OR (union) operation.voidorNot(RoaringBitmap other, long rangeEnd)In-place bitwise ORNOT operation.static RoaringBitmaporNot(RoaringBitmap x1, RoaringBitmap x2, long rangeEnd)Bitwise ORNOT operation for the given range, rangeStart (inclusive) and rangeEnd (exclusive).longpreviousAbsentValue(int fromValue)Returns the first absent value less than or equal to the provided value (interpreted as an unsigned integer).longpreviousValue(int fromValue)Returns the first value less than or equal to the provided value (interpreted as an unsigned integer).longrangeCardinality(long start, long end)Computes the number of values in the interval [start,end) where start is included and end excluded.intrank(int x)Rank returns the number of integers that are smaller or equal to x (rank(infinity) would be getCardinality()).longrankLong(int x)Rank returns the number of integers that are smaller or equal to x (Rank(infinity) would be GetCardinality()).voidreadExternal(java.io.ObjectInput in)voidremove(int x)If present remove the specified integer (effectively, sets its bit value to false)voidremove(int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangevoidremove(long rangeStart, long rangeEnd)Remove from the current bitmap all integers in [rangeStart,rangeEnd).static RoaringBitmapremove(RoaringBitmap rb, int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangestatic RoaringBitmapremove(RoaringBitmap rb, long rangeStart, long rangeEnd)Generate a new bitmap with all integers in [rangeStart,rangeEnd) removed.booleanremoveRunCompression()Remove run-length encoding even when it is more space efficientprotected voidrepairAfterLazy()booleanrunOptimize()Use a run-length encoding where it is more space efficientintselect(int j)Return the jth value stored in this bitmap.voidserialize(java.io.DataOutput out)Serialize this bitmap.voidserialize(java.nio.ByteBuffer buffer)Serialize this bitmap to a ByteBuffer.intserializedSizeInBytes()Report the number of bytes required to serialize this bitmap.int[]toArray()Return the set values as an array, if the cardinality is smaller than 2147483648.MutableRoaringBitmaptoMutableRoaringBitmap()Convert (copies) to a mutable roaring bitmap.java.lang.StringtoString()A string describing the bitmap.voidtrim()Recover allocated but unused memory.voidwriteExternal(java.io.ObjectOutput out)static RoaringBitmapxor(java.util.Iterator<? extends RoaringBitmap> bitmaps, int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the range.static RoaringBitmapxor(java.util.Iterator<? extends RoaringBitmap> bitmaps, long rangeStart, long rangeEnd)Computes XOR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)voidxor(RoaringBitmap x2)In-place bitwise XOR (symmetric difference) operation.static RoaringBitmapxor(RoaringBitmap x1, RoaringBitmap x2)Bitwise XOR (symmetric difference) operation.static intxorCardinality(RoaringBitmap x1, RoaringBitmap x2)Cardinality of the bitwise XOR (symmetric difference) operation.
-
-
-
Constructor Detail
-
RoaringBitmap
public RoaringBitmap()
Create an empty bitmap
-
RoaringBitmap
public RoaringBitmap(ImmutableRoaringBitmap rb)
Create a RoaringBitmap from a MutableRoaringBitmap or ImmutableRoaringBitmap. The source is not modified.- Parameters:
rb- the original bitmap
-
-
Method Detail
-
addOffset
public static RoaringBitmap addOffset(RoaringBitmap x, long offset)
Generate a new bitmap, but with all its values incremented by offset. The parameter offset can be negative. Values that would fall outside of the valid 32-bit range are discarded so that the result can have lower cardinality.- Parameters:
x- source bitmapoffset- increment- Returns:
- a new bitmap
-
add
public static RoaringBitmap add(RoaringBitmap rb, long rangeStart, long rangeEnd)
Generate a new bitmap with all integers in [rangeStart,rangeEnd) added.- Parameters:
rb- initial bitmap (will not be modified)rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new bitmap
-
add
@Deprecated public static RoaringBitmap add(RoaringBitmap rb, int rangeStart, int rangeEnd)
Deprecated.use the version where longs specify the rangeGenerate a new bitmap with all integers in [rangeStart,rangeEnd) added.- Parameters:
rb- initial bitmap (will not be modified)rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new bitmap
-
and
public static RoaringBitmap and(RoaringBitmap x1, RoaringBitmap x2)
Bitwise AND (intersection) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged. If you have more than 2 bitmaps, consider using the FastAggregation class.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- result of the operation
- See Also:
FastAggregation.and(RoaringBitmap...)
-
andCardinality
public static int andCardinality(RoaringBitmap x1, RoaringBitmap x2)
Cardinality of Bitwise AND (intersection) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- as if you did and(x2,x2).getCardinality()
- See Also:
FastAggregation.and(RoaringBitmap...)
-
andNot
public static RoaringBitmap andNot(RoaringBitmap x1, RoaringBitmap x2)
Bitwise ANDNOT (difference) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- result of the operation
-
add
public void add(int... dat)
Set all the specified values to true. This can be expected to be slightly faster than calling "add" repeatedly. The provided integers values don't have to be in sorted order, but it may be preferable to sort them from a performance point of view.- Parameters:
dat- set values
-
addN
public void addN(int[] dat, int offset, int n)Set the specified values to true, within given boundaries. This can be expected to be slightly faster than calling "add" repeatedly on the values dat[offset], dat[offset+1],..., dat[offset+n-1]. The provided integers values don't have to be in sorted order, but it may be preferable to sort them from a performance point of view.- Parameters:
dat- set valuesoffset- from which index the values should be set to truen- how many values should be set to true
-
bitmapOf
public static RoaringBitmap bitmapOf(int... dat)
Generate a bitmap with the specified values set to true. The provided integers values don't have to be in sorted order, but it may be preferable to sort them from a performance point of view.- Parameters:
dat- set values- Returns:
- a new bitmap
-
bitmapOfUnordered
public static RoaringBitmap bitmapOfUnordered(int... data)
Efficiently builds a RoaringBitmap from unordered data- Parameters:
data- unsorted data- Returns:
- a new bitmap
-
flip
public static RoaringBitmap flip(RoaringBitmap bm, long rangeStart, long rangeEnd)
Complements the bits in the given range, from rangeStart (inclusive) rangeEnd (exclusive). The given bitmap is unchanged.- Parameters:
bm- bitmap being negatedrangeStart- inclusive beginning of range, in [0, 0xffffffff]rangeEnd- exclusive ending of range, in [0, 0xffffffff + 1]- Returns:
- a new Bitmap
-
flip
@Deprecated public static RoaringBitmap flip(RoaringBitmap rb, int rangeStart, int rangeEnd)
Deprecated.use the version where longs specify the rangeComplements the bits in the given range, from rangeStart (inclusive) rangeEnd (exclusive). The given bitmap is unchanged.- Parameters:
rb- bitmap being negatedrangeStart- inclusive beginning of range, in [0, 0xffffffff]rangeEnd- exclusive ending of range, in [0, 0xffffffff + 1]- Returns:
- a new Bitmap
-
intersects
public static boolean intersects(RoaringBitmap x1, RoaringBitmap x2)
Checks whether the two bitmaps intersect. This can be much faster than calling "and" and checking the cardinality of the result.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- true if they intersect
-
lazyor
protected static RoaringBitmap lazyor(RoaringBitmap x1, RoaringBitmap x2)
-
lazyorfromlazyinputs
protected static RoaringBitmap lazyorfromlazyinputs(RoaringBitmap x1, RoaringBitmap x2)
-
or
public static RoaringBitmap or(java.util.Iterator<? extends RoaringBitmap> bitmaps)
Compute overall OR between bitmaps. (Effectively callsFastAggregation.or(java.util.Iterator<? extends org.roaringbitmap.RoaringBitmap>))- Parameters:
bitmaps- input bitmaps- Returns:
- aggregated bitmap
-
or
public static RoaringBitmap or(RoaringBitmap... bitmaps)
Compute overall OR between bitmaps. (Effectively callsFastAggregation.or(java.util.Iterator<? extends org.roaringbitmap.RoaringBitmap>))- Parameters:
bitmaps- input bitmaps- Returns:
- aggregated bitmap
-
or
public static RoaringBitmap or(RoaringBitmap x1, RoaringBitmap x2)
Bitwise OR (union) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged. If you have more than 2 bitmaps, consider using the FastAggregation class.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- result of the operation
- See Also:
FastAggregation.or(RoaringBitmap...),FastAggregation.horizontal_or(RoaringBitmap...)
-
orCardinality
public static int orCardinality(RoaringBitmap x1, RoaringBitmap x2)
Cardinality of the bitwise OR (union) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged. If you have more than 2 bitmaps, consider using the FastAggregation class.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- cardinality of the union
- See Also:
FastAggregation.or(RoaringBitmap...),FastAggregation.horizontal_or(RoaringBitmap...)
-
xorCardinality
public static int xorCardinality(RoaringBitmap x1, RoaringBitmap x2)
Cardinality of the bitwise XOR (symmetric difference) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- cardinality of the symmetric difference
-
andNotCardinality
public static int andNotCardinality(RoaringBitmap x1, RoaringBitmap x2)
Cardinality of the bitwise ANDNOT (left difference) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- cardinality of the left difference
-
remove
public static RoaringBitmap remove(RoaringBitmap rb, long rangeStart, long rangeEnd)
Generate a new bitmap with all integers in [rangeStart,rangeEnd) removed.- Parameters:
rb- initial bitmap (will not be modified)rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new bitmap
-
remove
@Deprecated public static RoaringBitmap remove(RoaringBitmap rb, int rangeStart, int rangeEnd)
Deprecated.use the version where longs specify the rangeGenerate a new bitmap with all integers in [rangeStart,rangeEnd) removed.- Parameters:
rb- initial bitmap (will not be modified)rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new bitmap
-
xor
public static RoaringBitmap xor(RoaringBitmap x1, RoaringBitmap x2)
Bitwise XOR (symmetric difference) operation. The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged. If you have more than 2 bitmaps, consider using the FastAggregation class.- Parameters:
x1- first bitmapx2- other bitmap- Returns:
- result of the operation
- See Also:
FastAggregation.xor(RoaringBitmap...),FastAggregation.horizontal_xor(RoaringBitmap...)
-
add
public void add(int x)
Add the value to the container (set the value to "true"), whether it already appears or not. Java lacks native unsigned integers but the x argument is considered to be unsigned. Within bitmaps, numbers are ordered according toInteger.compareUnsigned(int, int). We order the numbers like 0, 1, ..., 2147483647, -2147483648, -2147483647,..., -1.- Specified by:
addin interfaceBitmapDataProvider- Parameters:
x- integer value
-
add
public void add(long rangeStart, long rangeEnd)Add to the current bitmap all integers in [rangeStart,rangeEnd).- Specified by:
addin interfaceBitmapDataProvider- Parameters:
rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range
-
add
@Deprecated public void add(int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangeAdd to the current bitmap all integers in [rangeStart,rangeEnd).- Parameters:
rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range
-
intersects
public boolean intersects(long minimum, long supremum)Checks if the range intersects with the bitmap.- Parameters:
minimum- the inclusive unsigned lower bound of the rangesupremum- the exclusive unsigned upper bound of the range- Returns:
- whether the bitmap intersects with the range
-
and
public void and(RoaringBitmap x2)
In-place bitwise AND (intersection) operation. The current bitmap is modified.- Parameters:
x2- other bitmap
-
and
public static RoaringBitmap and(java.util.Iterator<? extends RoaringBitmap> bitmaps, long rangeStart, long rangeEnd)
Computes AND between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)- Parameters:
bitmaps- input bitmaps, these are not modifiedrangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new result bitmap
-
and
@Deprecated public static RoaringBitmap and(java.util.Iterator<? extends RoaringBitmap> bitmaps, int rangeStart, int rangeEnd)
Deprecated.use the version where longs specify the range. Negative range end are illegal.Computes AND between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)- Parameters:
bitmaps- input bitmaps, these are not modifiedrangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new result bitmap
-
andNot
public void andNot(RoaringBitmap x2)
In-place bitwise ANDNOT (difference) operation. The current bitmap is modified.- Parameters:
x2- other bitmap
-
andNot
public static RoaringBitmap andNot(RoaringBitmap x1, RoaringBitmap x2, long rangeStart, long rangeEnd)
Bitwise ANDNOT (difference) operation for the given range, rangeStart (inclusive) and rangeEnd (exclusive). The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged.- Parameters:
x1- first bitmapx2- other bitmaprangeStart- starting point of the range (inclusive)rangeEnd- end point of the range (exclusive)- Returns:
- result of the operation
-
andNot
@Deprecated public static RoaringBitmap andNot(RoaringBitmap x1, RoaringBitmap x2, int rangeStart, int rangeEnd)
Deprecated.use the version where longs specify the range. Negative values for range endpoints are not allowed.Bitwise ANDNOT (difference) operation for the given range, rangeStart (inclusive) and rangeEnd (exclusive). The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged.- Parameters:
x1- first bitmapx2- other bitmaprangeStart- starting point of the range (inclusive)rangeEnd- end point of the range (exclusive)- Returns:
- result of the operation
-
orNot
public void orNot(RoaringBitmap other, long rangeEnd)
In-place bitwise ORNOT operation. The current bitmap is modified.- Parameters:
other- the other bitmaprangeEnd- end point of the range (exclusive).
-
orNot
public static RoaringBitmap orNot(RoaringBitmap x1, RoaringBitmap x2, long rangeEnd)
Bitwise ORNOT operation for the given range, rangeStart (inclusive) and rangeEnd (exclusive). The provided bitmaps are *not* modified. This operation is thread-safe as long as the provided bitmaps remain unchanged.- Parameters:
x1- first bitmapx2- other bitmaprangeEnd- end point of the range (exclusive)- Returns:
- result of the operation
-
checkedAdd
public boolean checkedAdd(int x)
Add the value to the container (set the value to "true"), whether it already appears or not.- Parameters:
x- integer value- Returns:
- true if the added int wasn't already contained in the bitmap. False otherwise.
-
checkedRemove
public boolean checkedRemove(int x)
If present remove the specified integer (effectively, sets its bit value to false)- Parameters:
x- integer value representing the index in a bitmap- Returns:
- true if the unset bit was already in the bitmap
-
clear
public void clear()
reset to an empty bitmap; result occupies as much space a newly created bitmap.
-
clone
public RoaringBitmap clone()
- Overrides:
clonein classjava.lang.Object
-
contains
public boolean contains(int x)
Checks whether the value is included, which is equivalent to checking if the corresponding bit is set (get in BitSet class).- Specified by:
containsin interfaceImmutableBitmapDataProvider- Parameters:
x- integer value- Returns:
- whether the integer value is included.
-
contains
public boolean contains(long minimum, long supremum)Checks if the bitmap contains the range.- Parameters:
minimum- the inclusive lower bound of the rangesupremum- the exclusive upper bound of the range- Returns:
- whether the bitmap contains the range
-
deserialize
public void deserialize(java.io.DataInput in, byte[] buffer) throws java.io.IOExceptionDeserialize (retrieve) this bitmap. See format specification at https://github.com/RoaringBitmap/RoaringFormatSpec The current bitmap is overwritten.- Parameters:
in- the DataInput streambuffer- The buffer gets overwritten with data during deserialization. You can pass a NULL reference as a buffer. A buffer containing at least 8192 bytes might be ideal for performance. It is recommended to reuse the buffer between calls to deserialize (in a single-threaded context) for best performance.- Throws:
java.io.IOException- Signals that an I/O exception has occurred.
-
deserialize
public void deserialize(java.io.DataInput in) throws java.io.IOExceptionDeserialize (retrieve) this bitmap. See format specification at https://github.com/RoaringBitmap/RoaringFormatSpec The current bitmap is overwritten.- Parameters:
in- the DataInput stream- Throws:
java.io.IOException- Signals that an I/O exception has occurred.
-
deserialize
public void deserialize(java.nio.ByteBuffer bbf) throws java.io.IOExceptionDeserialize (retrieve) this bitmap. See format specification at https://github.com/RoaringBitmap/RoaringFormatSpec The current bitmap is overwritten. It is not necessary that limit() on the input ByteBuffer indicates the end of the serialized data. After loading this RoaringBitmap, you can advance to the rest of the data (if there is more) by setting bbf.position(bbf.position() + bitmap.serializedSizeInBytes()); Note that the input ByteBuffer is effectively copied (with the slice operation) so you should expect the provided ByteBuffer to remain unchanged.- Parameters:
bbf- the byte buffer (can be mapped, direct, array backed etc.- Throws:
java.io.IOException- Signals that an I/O exception has occurred.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
isHammingSimilar
public boolean isHammingSimilar(RoaringBitmap other, int tolerance)
Returns true if the other bitmap has no more than tolerance bits differing from this bitmap. The other may be transformed into a bitmap equal to this bitmap in no more than tolerance bit flips if this method returns true.- Parameters:
other- the bitmap to compare totolerance- the maximum number of bits that may differ- Returns:
- true if the number of differing bits is smaller than tolerance
-
flip
public void flip(int x)
Add the value if it is not already present, otherwise remove it.- Parameters:
x- integer value
-
flip
public void flip(long rangeStart, long rangeEnd)Modifies the current bitmap by complementing the bits in the given range, from rangeStart (inclusive) rangeEnd (exclusive).- Parameters:
rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range
-
flip
@Deprecated public void flip(int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangeModifies the current bitmap by complementing the bits in the given range, from rangeStart (inclusive) rangeEnd (exclusive).- Parameters:
rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range
-
getLongCardinality
public long getLongCardinality()
Returns the number of distinct integers added to the bitmap (e.g., number of bits set).- Specified by:
getLongCardinalityin interfaceImmutableBitmapDataProvider- Returns:
- the cardinality
-
getCardinality
public int getCardinality()
Description copied from interface:ImmutableBitmapDataProviderReturns the number of distinct integers added to the bitmap (e.g., number of bits set). Internally, this is computed as a 64-bit number.- Specified by:
getCardinalityin interfaceImmutableBitmapDataProvider- Returns:
- the cardinality
-
forEach
public void forEach(IntConsumer ic)
Description copied from interface:ImmutableBitmapDataProviderVisit all values in the bitmap and pass them to the consumer. * Usage:bitmap.forEach(new IntConsumer() { {@literal @}Override public void accept(int value) { // do something here }});}- Specified by:
forEachin interfaceImmutableBitmapDataProvider- Parameters:
ic- the consumer
-
getContainerPointer
public ContainerPointer getContainerPointer()
Return a low-level container pointer that can be used to access the underlying data structure.- Returns:
- container pointer
-
getIntIterator
public PeekableIntIterator getIntIterator()
For better performance, consider the Use theforEachmethod.- Specified by:
getIntIteratorin interfaceImmutableBitmapDataProvider- Returns:
- a custom iterator over set bits, the bits are traversed in ascending sorted order
-
getReverseIntIterator
public IntIterator getReverseIntIterator()
- Specified by:
getReverseIntIteratorin interfaceImmutableBitmapDataProvider- Returns:
- a custom iterator over set bits, the bits are traversed in descending sorted order
-
getBatchIterator
public RoaringBatchIterator getBatchIterator()
Description copied from interface:ImmutableBitmapDataProviderThis iterator may be faster than others- Specified by:
getBatchIteratorin interfaceImmutableBitmapDataProvider- Returns:
- iterator which works on batches of data.
-
getLongSizeInBytes
public long getLongSizeInBytes()
Estimate of the memory usage of this data structure. This can be expected to be within 1% of the true memory usage in common usage scenarios. If exact measures are needed, we recommend using dedicated libraries such as ehcache-sizeofengine. In adversarial cases, this estimate may be 10x the actual memory usage. For example, if you insert a single random value in a bitmap, then over a 100 bytes may be used by the JVM whereas this function may return an estimate of 32 bytes. The same will be true in the "sparse" scenario where you have a small set of random-looking integers spanning a wide range of values. These are considered adversarial cases because, as a general rule, if your data looks like a set of random integers, Roaring bitmaps are probably not the right data structure. Note that you can serialize your Roaring Bitmaps to disk and then construct ImmutableRoaringBitmap instances from a ByteBuffer. In such cases, the Java heap usage will be significantly less than what is reported. If your main goal is to compress arrays of integers, there are other libraries that are maybe more appropriate such as JavaFastPFOR. Note, however, that in general, random integers (as produced by random number generators or hash functions) are not compressible. Trying to compress random data is an adversarial use case.- Specified by:
getLongSizeInBytesin interfaceImmutableBitmapDataProvider- Returns:
- estimated memory usage.
- See Also:
- JavaFastPFOR
-
getSizeInBytes
public int getSizeInBytes()
Estimate of the memory usage of this data structure. This can be expected to be within 1% of the true memory usage in common usage scenarios. If exact measures are needed, we recommend using dedicated libraries such as ehcache-sizeofengine. In adversarial cases, this estimate may be 10x the actual memory usage. For example, if you insert a single random value in a bitmap, then over a 100 bytes may be used by the JVM whereas this function may return an estimate of 32 bytes. The same will be true in the "sparse" scenario where you have a small set of random-looking integers spanning a wide range of values. These are considered adversarial cases because, as a general rule, if your data looks like a set of random integers, Roaring bitmaps are probably not the right data structure. Note that you can serialize your Roaring Bitmaps to disk and then construct ImmutableRoaringBitmap instances from a ByteBuffer. In such cases, the Java heap usage will be significantly less than what is reported. If your main goal is to compress arrays of integers, there are other libraries that are maybe more appropriate such as JavaFastPFOR. Note, however, that in general, random integers (as produced by random number generators or hash functions) are not compressible. Trying to compress random data is an adversarial use case.- Specified by:
getSizeInBytesin interfaceImmutableBitmapDataProvider- Returns:
- estimated memory usage.
- See Also:
- JavaFastPFOR
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
hasRunCompression
public boolean hasRunCompression()
Check whether this bitmap has had its runs compressed.- Returns:
- whether this bitmap has run compression
-
isEmpty
public boolean isEmpty()
Checks whether the bitmap is empty.- Specified by:
isEmptyin interfaceImmutableBitmapDataProvider- Returns:
- true if this bitmap contains no set bit
-
iterator
public java.util.Iterator<java.lang.Integer> iterator()
iterate over the positions of the true values.- Specified by:
iteratorin interfacejava.lang.Iterable<java.lang.Integer>- Returns:
- the iterator
-
lazyor
protected void lazyor(RoaringBitmap x2)
-
naivelazyor
protected void naivelazyor(RoaringBitmap x2)
-
limit
public RoaringBitmap limit(int maxcardinality)
Create a new Roaring bitmap containing at most maxcardinality integers.- Specified by:
limitin interfaceImmutableBitmapDataProvider- Parameters:
maxcardinality- maximal cardinality- Returns:
- a new bitmap with cardinality no more than maxcardinality
-
or
public void or(RoaringBitmap x2)
In-place bitwise OR (union) operation. The current bitmap is modified.- Parameters:
x2- other bitmap
-
or
public static RoaringBitmap or(java.util.Iterator<? extends RoaringBitmap> bitmaps, long rangeStart, long rangeEnd)
Computes OR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)- Parameters:
bitmaps- input bitmaps, these are not modifiedrangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new result bitmap
-
or
@Deprecated public static RoaringBitmap or(java.util.Iterator<? extends RoaringBitmap> bitmaps, int rangeStart, int rangeEnd)
Deprecated.use the version where longs specify the range. Negative range points are forbidden.Computes OR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)- Parameters:
bitmaps- input bitmaps, these are not modifiedrangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new result bitmap
-
rankLong
public long rankLong(int x)
Rank returns the number of integers that are smaller or equal to x (Rank(infinity) would be GetCardinality()). If you provide the smallest value as a parameter, this function will return 1. If provide a value smaller than the smallest value, it will return 0.- Specified by:
rankLongin interfaceImmutableBitmapDataProvider- Parameters:
x- upper limit- Returns:
- the rank
- See Also:
- Ranking in statistics
-
rangeCardinality
public long rangeCardinality(long start, long end)Description copied from interface:ImmutableBitmapDataProviderComputes the number of values in the interval [start,end) where start is included and end excluded. rangeCardinality(0,0x100000000) provides the total cardinality (getLongCardinality). The answer is a 64-bit value between 1 and 0x100000000.- Specified by:
rangeCardinalityin interfaceImmutableBitmapDataProvider- Parameters:
start- lower limit (included)end- upper limit (excluded)- Returns:
- the number of elements in [start,end), between 0 and 0x100000000.
-
rank
public int rank(int x)
Description copied from interface:ImmutableBitmapDataProviderRank returns the number of integers that are smaller or equal to x (rank(infinity) would be getCardinality()). If you provide the smallest value as a parameter, this function will return 1. If provide a value smaller than the smallest value, it will return 0. The value is internally computed as a 64-bit number.- Specified by:
rankin interfaceImmutableBitmapDataProvider- Parameters:
x- upper limit- Returns:
- the rank
- See Also:
- Ranking in statistics
-
readExternal
public void readExternal(java.io.ObjectInput in) throws java.io.IOException- Specified by:
readExternalin interfacejava.io.Externalizable- Throws:
java.io.IOException
-
remove
public void remove(int x)
If present remove the specified integer (effectively, sets its bit value to false)- Specified by:
removein interfaceBitmapDataProvider- Parameters:
x- integer value representing the index in a bitmap
-
remove
public void remove(long rangeStart, long rangeEnd)Remove from the current bitmap all integers in [rangeStart,rangeEnd).- Parameters:
rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range
-
remove
@Deprecated public void remove(int rangeStart, int rangeEnd)Deprecated.use the version where longs specify the rangeRemove from the current bitmap all integers in [rangeStart,rangeEnd).- Parameters:
rangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range
-
removeRunCompression
public boolean removeRunCompression()
Remove run-length encoding even when it is more space efficient- Returns:
- whether a change was applied
-
repairAfterLazy
protected void repairAfterLazy()
-
runOptimize
public boolean runOptimize()
Use a run-length encoding where it is more space efficient- Returns:
- whether a change was applied
-
contains
public boolean contains(RoaringBitmap subset)
Checks whether the parameter is a subset of this RoaringBitmap or not- Parameters:
subset- the potential subset- Returns:
- true if the parameter is a subset of this RoaringBitmap
-
select
public int select(int j)
Return the jth value stored in this bitmap. The provided value needs to be smaller than the cardinality otherwise an IllegalArgumentException exception is thrown. The smallest value is at index 0. Note that this function differs in convention from the rank function which returns 1 when ranking the smallest value.- Specified by:
selectin interfaceImmutableBitmapDataProvider- Parameters:
j- index of the value- Returns:
- the value
- See Also:
- Selection algorithm
-
nextValue
public long nextValue(int fromValue)
Description copied from interface:ImmutableBitmapDataProviderReturns the first value equal to or larger than the provided value (interpreted as an unsigned integer). If no such bit exists then-1is returned. It is not necessarily a computationally effective way to iterate through the values.- Specified by:
nextValuein interfaceImmutableBitmapDataProvider- Parameters:
fromValue- the lower bound (inclusive)- Returns:
- the smallest value larger than or equal to the specified value,
or
-1if there is no such value
-
previousValue
public long previousValue(int fromValue)
Description copied from interface:ImmutableBitmapDataProviderReturns the first value less than or equal to the provided value (interpreted as an unsigned integer). If no such bit exists then-1is returned. It is not an efficient way to iterate through the values backwards.- Specified by:
previousValuein interfaceImmutableBitmapDataProvider- Parameters:
fromValue- the upper bound (inclusive)- Returns:
- the largest value less than or equal to the specified value,
or
-1if there is no such value
-
nextAbsentValue
public long nextAbsentValue(int fromValue)
Description copied from interface:ImmutableBitmapDataProviderReturns the first absent value equal to or larger than the provided value (interpreted as an unsigned integer). It is not necessarily a computationally effective way to iterate through the values.- Specified by:
nextAbsentValuein interfaceImmutableBitmapDataProvider- Parameters:
fromValue- the lower bound (inclusive)- Returns:
- the smallest absent value larger than or equal to the specified value.
-
previousAbsentValue
public long previousAbsentValue(int fromValue)
Description copied from interface:ImmutableBitmapDataProviderReturns the first absent value less than or equal to the provided value (interpreted as an unsigned integer). It is not necessarily a computationally effective way to iterate through the values.- Specified by:
previousAbsentValuein interfaceImmutableBitmapDataProvider- Parameters:
fromValue- the lower bound (inclusive)- Returns:
- the smallest absent value larger than or equal to the specified value.
-
first
public int first()
Get the first (smallest) integer in this RoaringBitmap, that is, returns the minimum of the set.- Specified by:
firstin interfaceImmutableBitmapDataProvider- Returns:
- the first (smallest) integer
- Throws:
java.util.NoSuchElementException- if empty
-
last
public int last()
Get the last (largest) integer in this RoaringBitmap, that is, returns the maximum of the set.- Specified by:
lastin interfaceImmutableBitmapDataProvider- Returns:
- the last (largest) integer
- Throws:
java.util.NoSuchElementException- if empty
-
serialize
public void serialize(java.io.DataOutput out) throws java.io.IOExceptionSerialize this bitmap. See format specification at https://github.com/RoaringBitmap/RoaringFormatSpec Consider callingrunOptimize()before serialization to improve compression. The current bitmap is not modified. There is a distinct and dedicated method to serialize to a ByteBuffer. Note: Java's data structures are in big endian format. Roaring serializes to a little endian format, so the bytes are flipped by the library during serialization to ensure that what is stored is in little endian---despite Java's big endianness. You can defeat this process by reflipping the bytes again in a custom DataOutput which could lead to serialized Roaring objects with an incorrect byte order.- Specified by:
serializein interfaceImmutableBitmapDataProvider- Parameters:
out- the DataOutput stream- Throws:
java.io.IOException- Signals that an I/O exception has occurred.
-
serialize
public void serialize(java.nio.ByteBuffer buffer)
Description copied from interface:ImmutableBitmapDataProviderSerialize this bitmap to a ByteBuffer. This is the preferred method to serialize to a byte array (byte[]) or to a String (via Base64.getEncoder().encodeToString).. Irrespective of the endianess of the provided buffer, data is written using LITTlE_ENDIAN as per the RoaringBitmap specification. The current bitmap is not modified.byte[] array = new byte[mrb.serializedSizeInBytes()]; mrb.serialize(ByteBuffer.wrap(array));- Specified by:
serializein interfaceImmutableBitmapDataProvider- Parameters:
buffer- the ByteBuffer
-
maximumSerializedSize
public static long maximumSerializedSize(long cardinality, long universe_size)Assume that one wants to store "cardinality" integers in [0, universe_size), this function returns an upper bound on the serialized size in bytes.- Parameters:
cardinality- maximal cardinalityuniverse_size- maximal value- Returns:
- upper bound on the serialized size in bytes of the bitmap
-
serializedSizeInBytes
public int serializedSizeInBytes()
Report the number of bytes required to serialize this bitmap. This is the number of bytes written out when using the serialize method. When using the writeExternal method, the count will be higher due to the overhead of Java serialization.- Specified by:
serializedSizeInBytesin interfaceImmutableBitmapDataProvider- Returns:
- the size in bytes
-
toArray
public int[] toArray()
Return the set values as an array, if the cardinality is smaller than 2147483648. The integer values are in sorted order.- Specified by:
toArrayin interfaceImmutableBitmapDataProvider- Returns:
- array representing the set values.
-
append
public void append(char key, Container container)Description copied from interface:AppendableStorageAppends the key and container to the storage, throws if the key is less than the current mark.- Specified by:
appendin interfaceAppendableStorage<Container>- Parameters:
key- the key to appendcontainer- the data to append
-
toMutableRoaringBitmap
public MutableRoaringBitmap toMutableRoaringBitmap()
Convert (copies) to a mutable roaring bitmap.- Returns:
- a copy of this bitmap as a MutableRoaringBitmap
-
toString
public java.lang.String toString()
A string describing the bitmap.- Overrides:
toStringin classjava.lang.Object- Returns:
- the string
-
trim
public void trim()
Recover allocated but unused memory.- Specified by:
trimin interfaceBitmapDataProvider
-
writeExternal
public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException- Specified by:
writeExternalin interfacejava.io.Externalizable- Throws:
java.io.IOException
-
xor
public void xor(RoaringBitmap x2)
In-place bitwise XOR (symmetric difference) operation. The current bitmap is modified.- Parameters:
x2- other bitmap
-
xor
public static RoaringBitmap xor(java.util.Iterator<? extends RoaringBitmap> bitmaps, long rangeStart, long rangeEnd)
Computes XOR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)- Parameters:
bitmaps- input bitmaps, these are not modifiedrangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new result bitmap
-
xor
@Deprecated public static RoaringBitmap xor(java.util.Iterator<? extends RoaringBitmap> bitmaps, int rangeStart, int rangeEnd)
Deprecated.use the version where longs specify the range. Negative values not allowed for rangeStart and rangeEndComputes XOR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd (exclusive)- Parameters:
bitmaps- input bitmaps, these are not modifiedrangeStart- inclusive beginning of rangerangeEnd- exclusive ending of range- Returns:
- new result bi
-
-