Skip to content

Commit

Permalink
clone smallest bitmap, adjust naive/workshy and thresholds
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstartin committed Jan 2, 2023
1 parent 7056910 commit cc67655
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
19 changes: 14 additions & 5 deletions RoaringBitmap/src/main/java/org/roaringbitmap/FastAggregation.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static RoaringBitmap and(Iterator<? extends RoaringBitmap> bitmaps) {
* @return aggregated bitmap
*/
public static RoaringBitmap and(RoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
return workShyAnd(new long[1024], bitmaps);
}
return naive_and(bitmaps);
Expand All @@ -49,7 +49,7 @@ public static RoaringBitmap and(RoaringBitmap... bitmaps) {
* @return aggregated bitmap
*/
public static RoaringBitmap and(long[] aggregationBuffer, RoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
if(aggregationBuffer.length < 1024) {
throw new IllegalArgumentException("buffer should have at least 1024 elements.");
}
Expand Down Expand Up @@ -329,9 +329,18 @@ public static RoaringBitmap naive_and(RoaringBitmap... bitmaps) {
if (bitmaps.length == 0) {
return new RoaringBitmap();
}
RoaringBitmap answer = bitmaps[0].clone();
for (int k = 1; k < bitmaps.length && !answer.isEmpty(); ++k) {
answer.and(bitmaps[k]);
RoaringBitmap smallest = bitmaps[0];
for (int i = 1; i < bitmaps.length; i++) {
RoaringBitmap bitmap = bitmaps[i];
if (bitmap.highLowContainer.size() < smallest.highLowContainer.size()) {
smallest = bitmap;
}
}
RoaringBitmap answer = smallest.clone();
for (int k = 0; k < bitmaps.length && !answer.isEmpty(); ++k) {
if (bitmaps[k] != smallest) {
answer.and(bitmaps[k]);
}
}
return answer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class BufferFastAggregation {
* @return aggregated bitmap
*/
public static MutableRoaringBitmap and(ImmutableRoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
return workShyAnd(new long[1024], bitmaps);
}
return naive_and(bitmaps);
Expand All @@ -42,7 +42,7 @@ public static MutableRoaringBitmap and(ImmutableRoaringBitmap... bitmaps) {
*/
public static MutableRoaringBitmap and(long[] aggregationBuffer,
ImmutableRoaringBitmap... bitmaps) {
if (bitmaps.length > 2) {
if (bitmaps.length > 10) {
if(aggregationBuffer.length < 1024) {
throw new IllegalArgumentException("buffer should have at least 1024 elements.");
}
Expand Down Expand Up @@ -348,9 +348,18 @@ public static MutableRoaringBitmap naive_and(ImmutableRoaringBitmap... bitmaps)
MutableRoaringBitmap answer;

if (bitmaps.length > 0) {
answer = (bitmaps[0]).toMutableRoaringBitmap();
for (int k = 1; k < bitmaps.length; ++k) {
answer = ImmutableRoaringBitmap.and(answer, bitmaps[k]);
ImmutableRoaringBitmap smallest = bitmaps[0];
for (int i = 1; i < bitmaps.length; i++) {
ImmutableRoaringBitmap bitmap = bitmaps[i];
if (bitmap.highLowContainer.size() < smallest.highLowContainer.size()) {
smallest = bitmap;
}
}
answer = (smallest).toMutableRoaringBitmap();
for (ImmutableRoaringBitmap bitmap : bitmaps) {
if (bitmap != smallest) {
answer.and(bitmap);
}
}
} else {
answer = new MutableRoaringBitmap();
Expand Down

0 comments on commit cc67655

Please sign in to comment.