
org.roaringbitmap.BitmapBatchIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of RoaringBitmap Show documentation
Show all versions of RoaringBitmap Show documentation
Roaring bitmaps are compressed bitmaps (also called bitsets) which tend to outperform
conventional compressed bitmaps such as WAH or Concise.
The newest version!
package org.roaringbitmap;
import static java.lang.Long.numberOfTrailingZeros;
public final class BitmapBatchIterator implements ContainerBatchIterator {
private int wordIndex = 0;
private long word;
private BitmapContainer bitmap;
public BitmapBatchIterator(BitmapContainer bitmap) {
wrap(bitmap);
}
@Override
public int next(int key, int[] buffer, int offset) {
int consumed = 0;
while ((consumed + offset) < buffer.length) {
while (word == 0) {
++wordIndex;
if (wordIndex == 1024) {
return consumed;
}
word = bitmap.bitmap[wordIndex];
}
buffer[offset + consumed++] = key + (64 * wordIndex) + numberOfTrailingZeros(word);
word &= (word - 1);
}
return consumed;
}
@Override
public boolean hasNext() {
if (wordIndex > 1023) {
return false;
}
while (word == 0) {
++wordIndex;
if (wordIndex == 1024) { // reached end without a non-empty word
return false;
}
word = bitmap.bitmap[wordIndex];
}
return true; // found some non-empty word, so hasNext
}
@Override
public ContainerBatchIterator clone() {
try {
return (ContainerBatchIterator)super.clone();
} catch (CloneNotSupportedException e) {
// won't happen
throw new IllegalStateException(e);
}
}
@Override
public void releaseContainer() {
bitmap = null;
}
@Override
public void advanceIfNeeded(char target) {
wordIndex = target >>> 6;
word = bitmap.bitmap[wordIndex];
word &= -(1L << target);
}
void wrap(BitmapContainer bitmap) {
this.bitmap = bitmap;
word = bitmap.bitmap[0];
this.wordIndex = 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy