public class RoaringBitmap extends Object implements Cloneable, Serializable, Iterable<Integer>, Externalizable, ImmutableBitmapDataProvider
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);
Integers are added in unsigned sorted order. That is, they are
treated as unsigned integers (see Java 8's Integer.toUnsignedLong function).
Bitmaps are limited to a maximum of Integer.MAX_VALUE entries. Trying to
create larger bitmaps could result in undefined behaviors.| Constructor and Description |
|---|
RoaringBitmap()
Create an empty bitmap
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(int x)
Add the value to the container (set the value to "true"), whether it already appears or not.
|
void |
add(int rangeStart,
int rangeEnd)
Add to the current bitmap all integers in [rangeStart,rangeEnd).
|
static RoaringBitmap |
add(RoaringBitmap rb,
int rangeStart,
int rangeEnd)
Generate a new bitmap with all integers in [rangeStart,rangeEnd) added.
|
void |
and(RoaringBitmap x2)
In-place bitwise AND (intersection) operation.
|
static RoaringBitmap |
and(RoaringBitmap x1,
RoaringBitmap x2)
Bitwise AND (intersection) operation.
|
void |
andNot(RoaringBitmap x2)
In-place bitwise ANDNOT (difference) operation.
|
static RoaringBitmap |
andNot(RoaringBitmap x1,
RoaringBitmap x2)
Bitwise ANDNOT (difference) operation.
|
static RoaringBitmap |
bitmapOf(int... dat)
Generate a bitmap with the specified values set to true.
|
boolean |
checkedAdd(int x)
Add the value to the container (set the value to "true"), whether it already appears or not.
|
boolean |
checkedRemove(int x)
If present remove the specified integer (effectively, sets its bit value
to false)
|
void |
clear()
reset to an empty bitmap; result occupies as much space a newly
created bitmap.
|
RoaringBitmap |
clone() |
boolean |
contains(int x)
Checks whether the value in included, which is equivalent to checking
if the corresponding bit is set (get in BitSet class).
|
void |
deserialize(DataInput in)
Deserialize (retrieve) this bitmap.
|
boolean |
equals(Object o) |
void |
flip(int x)
Add the value if it is not already present, otherwise remove it.
|
void |
flip(int rangeStart,
int rangeEnd)
Modifies the current bitmap by complementing the bits in the given
range, from rangeStart (inclusive) rangeEnd (exclusive).
|
static RoaringBitmap |
flip(RoaringBitmap bm,
int rangeStart,
int rangeEnd)
Complements the bits in the given range, from rangeStart (inclusive)
rangeEnd (exclusive).
|
int |
getCardinality()
Returns the number of distinct integers added to the bitmap (e.g.,
number of bits set).
|
IntIterator |
getIntIterator() |
IntIterator |
getReverseIntIterator() |
int |
getSizeInBytes()
Estimate of the memory usage of this data structure.
|
int |
hashCode() |
boolean |
hasRunCompression()
Check whether this bitmap has had its runs compressed.
|
static boolean |
intersects(RoaringBitmap x1,
RoaringBitmap x2)
Checks whether the two bitmaps intersect.
|
boolean |
isEmpty()
Checks whether the bitmap is empty.
|
Iterator<Integer> |
iterator()
iterate over the positions of the true values.
|
protected void |
lazyor(RoaringBitmap x2) |
protected static RoaringBitmap |
lazyor(RoaringBitmap x1,
RoaringBitmap x2) |
protected static RoaringBitmap |
lazyorfromlazyinputs(RoaringBitmap x1,
RoaringBitmap x2) |
RoaringBitmap |
limit(int maxcardinality)
Create a new Roaring bitmap containing at most maxcardinality integers.
|
static RoaringBitmap |
or(Iterator<RoaringBitmap> bitmaps)
Compute overall AND between bitmaps.
|
static RoaringBitmap |
or(RoaringBitmap... bitmaps)
Compute overall OR between bitmaps.
|
void |
or(RoaringBitmap x2)
In-place bitwise OR (union) operation.
|
static RoaringBitmap |
or(RoaringBitmap x1,
RoaringBitmap x2)
Bitwise OR (union) operation.
|
int |
rank(int x)
Rank returns the number of integers that are smaller or equal to x (Rank(infinity) would be GetCardinality()).
|
void |
readExternal(ObjectInput in) |
void |
remove(int x)
If present remove the specified integer (effectively, sets its bit
value to false)
|
void |
remove(int rangeStart,
int rangeEnd)
Remove from the current bitmap all integers in [rangeStart,rangeEnd).
|
static RoaringBitmap |
remove(RoaringBitmap rb,
int rangeStart,
int rangeEnd)
Generate a new bitmap with all integers in [rangeStart,rangeEnd) removed.
|
boolean |
removeRunCompression()
Remove run-length encoding even when it is more space efficient
|
protected void |
repairAfterLazy() |
boolean |
runOptimize()
Use a run-length encoding where it is more space efficient
|
int |
select(int j)
Return the jth value stored in this bitmap.
|
void |
serialize(DataOutput out)
Serialize this bitmap.
|
int |
serializedSizeInBytes()
Report the number of bytes required to serialize this bitmap.
|
int[] |
toArray()
Return the set values as an array.
|
String |
toString()
A string describing the bitmap.
|
void |
trim()
Recover allocated but unused memory.
|
void |
writeExternal(ObjectOutput out) |
void |
xor(RoaringBitmap x2)
In-place bitwise XOR (symmetric difference) operation.
|
static RoaringBitmap |
xor(RoaringBitmap x1,
RoaringBitmap x2)
Bitwise XOR (symmetric difference) operation.
|
finalize, getClass, notify, notifyAll, wait, wait, waitforEach, spliteratorpublic static RoaringBitmap and(RoaringBitmap x1, RoaringBitmap x2)
x1 - first bitmapx2 - other bitmapFastAggregation.and(RoaringBitmap...)public static RoaringBitmap andNot(RoaringBitmap x1, RoaringBitmap x2)
x1 - first bitmapx2 - other bitmappublic static RoaringBitmap bitmapOf(int... dat)
dat - set valuespublic static RoaringBitmap flip(RoaringBitmap bm, int rangeStart, int rangeEnd)
bm - bitmap being negatedrangeStart - inclusive beginning of rangerangeEnd - exclusive ending of rangepublic static RoaringBitmap or(RoaringBitmap x1, RoaringBitmap x2)
x1 - first bitmapx2 - other bitmapFastAggregation.or(RoaringBitmap...),
FastAggregation.horizontal_or(RoaringBitmap...)protected static RoaringBitmap lazyor(RoaringBitmap x1, RoaringBitmap x2)
protected static RoaringBitmap lazyorfromlazyinputs(RoaringBitmap x1, RoaringBitmap x2)
public int rank(int x)
rank in interface ImmutableBitmapDataProviderx - upper limitpublic int select(int j)
select in interface ImmutableBitmapDataProviderj - index of the valuepublic static RoaringBitmap xor(RoaringBitmap x1, RoaringBitmap x2)
x1 - first bitmapx2 - other bitmapFastAggregation.xor(RoaringBitmap...),
FastAggregation.horizontal_xor(RoaringBitmap...)public boolean checkedAdd(int x)
x - integer valuepublic void add(int x)
x - integer valuepublic void flip(int x)
x - integer valuepublic void add(int rangeStart,
int rangeEnd)
rangeStart - inclusive beginning of rangerangeEnd - exclusive ending of rangepublic static RoaringBitmap add(RoaringBitmap rb, int rangeStart, int rangeEnd)
rb - initial bitmap (will not be modified)rangeStart - inclusive beginning of rangerangeEnd - exclusive ending of rangepublic void remove(int rangeStart,
int rangeEnd)
rangeStart - inclusive beginning of rangerangeEnd - exclusive ending of rangepublic static RoaringBitmap remove(RoaringBitmap rb, int rangeStart, int rangeEnd)
rb - initial bitmap (will not be modified)rangeStart - inclusive beginning of rangerangeEnd - exclusive ending of rangepublic void and(RoaringBitmap x2)
x2 - other bitmappublic void andNot(RoaringBitmap x2)
x2 - other bitmappublic void clear()
public RoaringBitmap clone()
public boolean contains(int x)
contains in interface ImmutableBitmapDataProviderx - integer valuepublic void deserialize(DataInput in) throws IOException
in - the DataInput streamIOException - Signals that an I/O exception has occurred.public void flip(int rangeStart,
int rangeEnd)
rangeStart - inclusive beginning of rangerangeEnd - exclusive ending of rangepublic int getCardinality()
getCardinality in interface ImmutableBitmapDataProviderpublic IntIterator getIntIterator()
getIntIterator in interface ImmutableBitmapDataProviderpublic IntIterator getReverseIntIterator()
getReverseIntIterator in interface ImmutableBitmapDataProviderpublic int getSizeInBytes()
getSizeInBytes in interface ImmutableBitmapDataProviderpublic static RoaringBitmap or(RoaringBitmap... bitmaps)
FastAggregation.or(org.roaringbitmap.RoaringBitmap...))bitmaps - input bitmapspublic static RoaringBitmap or(Iterator<RoaringBitmap> bitmaps)
FastAggregation.or(org.roaringbitmap.RoaringBitmap...))bitmaps - input bitmapspublic boolean isEmpty()
isEmpty in interface ImmutableBitmapDataProviderpublic void or(RoaringBitmap x2)
x2 - other bitmapprotected void repairAfterLazy()
protected void lazyor(RoaringBitmap x2)
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
readExternal in interface ExternalizableIOExceptionClassNotFoundExceptionpublic boolean checkedRemove(int x)
x - integer value representing the index in a bitmappublic void remove(int x)
x - integer value representing the index in a bitmappublic void serialize(DataOutput out) throws IOException
runOptimize() before serialization to
improve compression.
The current bitmap is not modified.
Advanced example: To serialize your bitmap to a ByteBuffer,
you can do the following.
//r is your bitmap
r.runOptimize(); // might improve compression
// next we create the ByteBuffer where the data will be stored
ByteBuffer outbb = ByteBuffer.allocate(r.serializedSizeInBytes());
// then we can serialize on a custom OutputStream
mrb.serialize(new DataOutputStream(new OutputStream(){
ByteBuffer mBB;
OutputStream init(ByteBuffer mbb) {mBB=mbb; return this;}
public void close() {}
public void flush() {}
public void write(int b) {
mBB.put((byte) b);}
public void write(byte[] b) {mBB.put(b);}
public void write(byte[] b, int off, int l) {mBB.put(b,off,l);}
}.init(outbb)));
// outbuff will now contain a serialized version of your bitmap
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.serialize in interface ImmutableBitmapDataProviderout - the DataOutput streamIOException - Signals that an I/O exception has occurred.public int serializedSizeInBytes()
serializedSizeInBytes in interface ImmutableBitmapDataProviderpublic RoaringBitmap limit(int maxcardinality)
limit in interface ImmutableBitmapDataProvidermaxcardinality - maximal cardinalitypublic int[] toArray()
toArray in interface ImmutableBitmapDataProviderpublic String toString()
public void trim()
public boolean runOptimize()
public boolean removeRunCompression()
public static boolean intersects(RoaringBitmap x1, RoaringBitmap x2)
x1 - first bitmapx2 - other bitmappublic boolean hasRunCompression()
public void writeExternal(ObjectOutput out) throws IOException
writeExternal in interface ExternalizableIOExceptionpublic void xor(RoaringBitmap x2)
x2 - other bitmapCopyright © 2015. All Rights Reserved.