public class ImmutableRoaringBitmap extends Object implements Iterable<Integer>, Cloneable, ImmutableBitmapDataProvider
import org.roaringbitmap.buffer.*;
//...
MutableRoaringBitmap rr1 = MutableRoaringBitmap.bitmapOf(1, 2, 3, 1000);
MutableRoaringBitmap rr2 = MutableRoaringBitmap.bitmapOf( 2, 3, 1010);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
// could call "rr1.runOptimize()" and "rr2.runOptimize" if there
// there were runs to compress
rr1.serialize(dos);
rr2.serialize(dos);
dos.close();
ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
ImmutableRoaringBitmap rrback1 = new ImmutableRoaringBitmap(bb);
bb.position(bb.position() + rrback1.serializedSizeInBytes());
ImmutableRoaringBitmap rrback2 = new ImmutableRoaringBitmap(bb);
It can also be constructed from a ByteBuffer (useful for memory mapping).
Objects of this class may reside almost entirely in memory-map files.MutableRoaringBitmap
Modifier | Constructor and Description |
---|---|
protected |
ImmutableRoaringBitmap() |
|
ImmutableRoaringBitmap(ByteBuffer b)
Constructs a new ImmutableRoaringBitmap starting at this ByteBuffer's position().
|
Modifier and Type | Method and Description |
---|---|
static MutableRoaringBitmap |
and(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2)
Bitwise AND (intersection) operation.
|
static MutableRoaringBitmap |
and(Iterator bitmaps,
int rangeStart,
int rangeEnd)
Deprecated.
use the version where longs specify the range. Negative range end are illegal.
|
static MutableRoaringBitmap |
and(Iterator bitmaps,
long rangeStart,
long rangeEnd)
Computes AND between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd
(exclusive)
|
static int |
andCardinality(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2)
Cardinality of Bitwise AND (intersection) operation.
|
static MutableRoaringBitmap |
andNot(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2)
Bitwise ANDNOT (difference) operation.
|
static MutableRoaringBitmap |
andNot(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2,
int rangeStart,
int rangeEnd)
Deprecated.
use the version where longs specify the range. Negative values for range
endpoints are not allowed.
|
static MutableRoaringBitmap |
andNot(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2,
long rangeStart,
long rangeEnd)
Bitwise ANDNOT (difference) operation for the given range, rangeStart (inclusive) and rangeEnd
(exclusive).
|
static ImmutableRoaringBitmap |
bitmapOf(int... data)
Generate a bitmap with the specified values set to true.
|
ImmutableRoaringBitmap |
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).
|
boolean |
equals(Object o) |
static MutableRoaringBitmap |
flip(ImmutableRoaringBitmap bm,
int rangeStart,
int rangeEnd)
Deprecated.
use the version where longs specify the range
|
static MutableRoaringBitmap |
flip(ImmutableRoaringBitmap bm,
long rangeStart,
long rangeEnd)
Complements the bits in the given range, from rangeStart (inclusive) rangeEnd (exclusive).
|
void |
forEach(IntConsumer ic)
Visit all values in the bitmap and pass them to the consumer.
|
int |
getCardinality()
Returns the number of distinct integers added to the bitmap (e.g., number of bits set).
|
MappeableContainerPointer |
getContainerPointer()
Return a low-level container pointer that can be used to access the underlying data structure.
|
PeekableIntIterator |
getIntIterator()
For better performance, consider the Use the
forEach method. |
long |
getLongCardinality()
Returns the number of distinct integers added to the bitmap (e.g., number of bits set).
|
long |
getLongSizeInBytes()
Estimate of the memory usage of this data structure.
|
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(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap 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 static MutableRoaringBitmap |
lazyor(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2) |
MutableRoaringBitmap |
limit(int maxcardinality)
Create a new Roaring bitmap containing at most maxcardinality integers.
|
static MutableRoaringBitmap |
or(ImmutableRoaringBitmap... bitmaps)
Compute overall OR between bitmaps.
|
static MutableRoaringBitmap |
or(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2)
Bitwise OR (union) operation.
|
static MutableRoaringBitmap |
or(Iterator bitmaps)
Compute overall OR between bitmaps.
|
static MutableRoaringBitmap |
or(Iterator bitmaps,
int rangeStart,
int rangeEnd)
Deprecated.
use the version where longs specify the range.
Negative range points are forbidden.
|
static MutableRoaringBitmap |
or(Iterator bitmaps,
long rangeStart,
long rangeEnd)
Computes OR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd
(exclusive)
|
static int |
orCardinality(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2)
Cardinality of the 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()).
|
long |
rankLong(int x)
Rank returns the number of integers that are smaller or equal to x (Rank(infinity) would be
GetCardinality()).
|
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 for serialization.
|
int[] |
toArray()
Return the set values as an array if the cardinality is less
than 2147483648.
|
MutableRoaringBitmap |
toMutableRoaringBitmap()
Copies the content of this bitmap to a bitmap that can be modified.
|
RoaringBitmap |
toRoaringBitmap()
Copies this bitmap to a mutable RoaringBitmap.
|
String |
toString()
A string describing the bitmap.
|
static MutableRoaringBitmap |
xor(ImmutableRoaringBitmap x1,
ImmutableRoaringBitmap x2)
Bitwise XOR (symmetric difference) operation.
|
static MutableRoaringBitmap |
xor(Iterator bitmaps,
int rangeStart,
int rangeEnd)
Deprecated.
use the version where longs specify the range.
Negative values not allowed for rangeStart and rangeEnd
|
static MutableRoaringBitmap |
xor(Iterator bitmaps,
long rangeStart,
long rangeEnd)
Computes XOR between input bitmaps in the given range, from rangeStart (inclusive) to rangeEnd
(exclusive)
|
finalize, getClass, notify, notifyAll, wait, wait, wait
forEach, spliterator
protected ImmutableRoaringBitmap()
public ImmutableRoaringBitmap(ByteBuffer b)
b
- data sourcepublic static MutableRoaringBitmap and(Iterator bitmaps, long rangeStart, long rangeEnd)
bitmaps
- input bitmaps, these are not modifiedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of range@Deprecated public static MutableRoaringBitmap and(Iterator bitmaps, int rangeStart, int rangeEnd)
bitmaps
- input bitmaps, these are not modifiedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of rangepublic static MutableRoaringBitmap and(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
x1
- first bitmapx2
- other bitmapBufferFastAggregation.and(ImmutableRoaringBitmap...)
public static int andCardinality(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
x1
- first bitmapx2
- other bitmapBufferFastAggregation.and(ImmutableRoaringBitmap...)
public static MutableRoaringBitmap andNot(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2, long rangeStart, long rangeEnd)
x1
- first bitmapx2
- other bitmaprangeStart
- beginning of the range (inclusive)rangeEnd
- end of range (exclusive)@Deprecated public static MutableRoaringBitmap andNot(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2, int rangeStart, int rangeEnd)
x1
- first bitmapx2
- other bitmaprangeStart
- beginning of the range (inclusive)rangeEnd
- end of range (exclusive)public static MutableRoaringBitmap andNot(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
x1
- first bitmapx2
- other bitmappublic static ImmutableRoaringBitmap bitmapOf(int... data)
(ImmutableRoaringBitmap) MutableRoaringBitmap.bitmapOf(data)
data
- set valuespublic static MutableRoaringBitmap flip(ImmutableRoaringBitmap bm, long rangeStart, long rangeEnd)
bm
- bitmap being negatedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of range@Deprecated public static MutableRoaringBitmap flip(ImmutableRoaringBitmap bm, int rangeStart, int rangeEnd)
bm
- bitmap being negatedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of rangepublic static boolean intersects(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
x1
- first bitmapx2
- other bitmapprotected static MutableRoaringBitmap lazyor(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
public static MutableRoaringBitmap or(ImmutableRoaringBitmap... bitmaps)
BufferFastAggregation.or(org.roaringbitmap.buffer.ImmutableRoaringBitmap...)
)bitmaps
- input bitmapspublic static MutableRoaringBitmap or(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
x1
- first bitmapx2
- other bitmapBufferFastAggregation.or(ImmutableRoaringBitmap...)
,
BufferFastAggregation.horizontal_or(ImmutableRoaringBitmap...)
public static MutableRoaringBitmap or(Iterator bitmaps)
BufferFastAggregation.or(org.roaringbitmap.buffer.ImmutableRoaringBitmap...)
)bitmaps
- input bitmapspublic static MutableRoaringBitmap or(Iterator bitmaps, long rangeStart, long rangeEnd)
bitmaps
- input bitmaps, these are not modifiedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of range@Deprecated public static MutableRoaringBitmap or(Iterator bitmaps, int rangeStart, int rangeEnd)
bitmaps
- input bitmaps, these are not modifiedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of rangepublic static int orCardinality(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
x1
- first bitmapx2
- other bitmapBufferFastAggregation.or(ImmutableRoaringBitmap...)
,
BufferFastAggregation.horizontal_or(ImmutableRoaringBitmap...)
public static MutableRoaringBitmap xor(Iterator bitmaps, long rangeStart, long rangeEnd)
bitmaps
- input bitmaps, these are not modifiedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of range@Deprecated public static MutableRoaringBitmap xor(Iterator bitmaps, int rangeStart, int rangeEnd)
bitmaps
- input bitmaps, these are not modifiedrangeStart
- inclusive beginning of rangerangeEnd
- exclusive ending of rangepublic static MutableRoaringBitmap xor(ImmutableRoaringBitmap x1, ImmutableRoaringBitmap x2)
x1
- first bitmapx2
- other bitmapBufferFastAggregation.xor(ImmutableRoaringBitmap...)
,
BufferFastAggregation.horizontal_xor(ImmutableRoaringBitmap...)
public ImmutableRoaringBitmap clone()
public boolean contains(int x)
contains
in interface ImmutableBitmapDataProvider
x
- integer valuepublic long getLongCardinality()
getLongCardinality
in interface ImmutableBitmapDataProvider
public int getCardinality()
ImmutableBitmapDataProvider
getCardinality
in interface ImmutableBitmapDataProvider
public void forEach(IntConsumer ic)
ImmutableBitmapDataProvider
bitmap.forEach(new IntConsumer() {
{@literal @}Override
public void accept(int value) {
// do something here
}});
}
forEach
in interface ImmutableBitmapDataProvider
ic
- the consumerpublic MappeableContainerPointer getContainerPointer()
public PeekableIntIterator getIntIterator()
forEach
method.getIntIterator
in interface ImmutableBitmapDataProvider
public IntIterator getReverseIntIterator()
getReverseIntIterator
in interface ImmutableBitmapDataProvider
public long getLongSizeInBytes()
getLongSizeInBytes
in interface ImmutableBitmapDataProvider
public int getSizeInBytes()
ImmutableBitmapDataProvider
getSizeInBytes
in interface ImmutableBitmapDataProvider
public boolean hasRunCompression()
public boolean isEmpty()
isEmpty
in interface ImmutableBitmapDataProvider
public MutableRoaringBitmap limit(int maxcardinality)
limit
in interface ImmutableBitmapDataProvider
maxcardinality
- maximal cardinalitypublic long rankLong(int x)
rankLong
in interface ImmutableBitmapDataProvider
x
- upper limitpublic int rank(int x)
ImmutableBitmapDataProvider
rank
in interface ImmutableBitmapDataProvider
x
- upper limitpublic int select(int j)
select
in interface ImmutableBitmapDataProvider
j
- index of the valuepublic void serialize(DataOutput out) throws IOException
MutableRoaringBitmap.runOptimize()
before serialization to improve
compression if this is a MutableRoaringBitmap instance.
The current bitmap is not modified.
Advanced example: To serialize your bitmap to a ByteBuffer, you can do the following.
{ @code // r is your bitmap // r.runOptimize(); // might improve compression, only if you have a // MutableRoaringBitmap instance. // 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 ImmutableBitmapDataProvider
out
- the DataOutput streamIOException
- Signals that an I/O exception has occurred.public int serializedSizeInBytes()
serializedSizeInBytes
in interface ImmutableBitmapDataProvider
public int[] toArray()
toArray
in interface ImmutableBitmapDataProvider
public MutableRoaringBitmap toMutableRoaringBitmap()
public RoaringBitmap toRoaringBitmap()
Copyright © 2016. All Rights Reserved.