Skip to content

Commit

Permalink
Add RoaringBitmap.add with offset and length arguments (#271)
Browse files Browse the repository at this point in the history
* Add overload of RoaringBitmap.add, to accept offset and length arguments

* Add unit test for RoaringBitmap.addN

* Fix typo

* Add MutableRoaringBitmap.addN method
  • Loading branch information
Rafael Telles authored and lemire committed Aug 28, 2018
1 parent 4148d9c commit 982ef73
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
25 changes: 20 additions & 5 deletions roaringbitmap/src/main/java/org/roaringbitmap/RoaringBitmap.java
Original file line number Diff line number Diff line change
Expand Up @@ -436,21 +436,36 @@ public static RoaringBitmap andNot(final RoaringBitmap x1, final RoaringBitmap x
}
return answer;
}

/**
* Set all the specified values to true. This can be expected to be slightly
* 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.
*
* @param dat set values
*/
public void add(final int... dat) {
this.addN(dat, 0, dat.length);
}

/**
* Set the specified values to true, within given boundaries. 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.
*
* @param dat set values
* @param offset from which index the values should be set to true
* @param n how many values should be set to true
*/
public void addN(final int[] dat, final int offset, final int n) {
Container currentcont = null;
short currenthb = 0;
int currentcontainerindex = 0;
int j = 0;
if(j < dat.length) {
int val = dat[j];
if(j < n) {
int val = dat[j + offset];
currenthb = Util.highbits(val);
currentcontainerindex = highLowContainer.getIndex(currenthb);
if (currentcontainerindex >= 0) {
Expand All @@ -468,8 +483,8 @@ public void add(final int... dat) {
}
j++;
}
for( ; j < dat.length; ++j) {
int val = dat[j];
for( ; j < n; ++j) {
int val = dat[j + offset];
short newhb = Util.highbits(val);
if(currenthb == newhb) {// easy case
// this could be quite frequent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,37 @@ public static MutableRoaringBitmap andNot(final MutableRoaringBitmap x1,
}
return answer;
}

/**
* Set all the specified values to true. This can be expected to be slightly
* 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.
*
* @param dat set values
*/
public void add(final int... dat) {
this.addN(dat, 0, dat.length);
}

/**
* Set the specified values to true, within given boundaries. 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.
*
* @param dat set values
* @param offset from which index the values should be set to true
* @param n how many values should be set to true
*/
public void addN(final int[] dat, final int offset, final int n) {
MutableRoaringArray mra = (MutableRoaringArray) highLowContainer;
MappeableContainer currentcont = null;
short currenthb = 0;
int currentcontainerindex = 0;
int j = 0;
if(j < dat.length) {
int val = dat[j];
if(j < n) {
int val = dat[j + offset];
currenthb = BufferUtil.highbits(val);
currentcontainerindex = highLowContainer.getIndex(currenthb);
if (currentcontainerindex >= 0) {
Expand All @@ -322,8 +337,8 @@ public void add(final int... dat) {
}
j++;
}
for( ; j < dat.length; ++j) {
int val = dat[j];
for( ; j < n; ++j) {
int val = dat[j + offset];
short newhb = BufferUtil.highbits(val);
if(currenthb == newhb) {// easy case
// this could be quite frequent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@
@SuppressWarnings({"static-method"})
public class TestRoaringBitmap {

@Test
public void testMultipleAdd() {
RoaringBitmap bitmap = new RoaringBitmap();
bitmap.add(1);
bitmap.add(1, 2, 3);
bitmap.add(0xFFFFFFFF);
bitmap.add(0xFFFFFFFE,0xFFFFFFFF );
Assert.assertEquals("{1,2,3,4294967294,4294967295}",bitmap.toString());
}
@Test
public void testMultipleAdd() {
RoaringBitmap bitmap = new RoaringBitmap();
bitmap.add(1);
bitmap.add(1, 2, 3);
bitmap.add(0xFFFFFFFF);
bitmap.add(0xFFFFFFFE,0xFFFFFFFF );
Assert.assertEquals("{1,2,3,4294967294,4294967295}",bitmap.toString());
}

@Test
public void testAddN() {
RoaringBitmap bitmap = new RoaringBitmap();
bitmap.addN(new int[]{1, 2, 3, 4, 5}, 1, 3);
Assert.assertEquals("{2,3,4}",bitmap.toString());
}

@Test
public void testStringer() {
Expand Down

0 comments on commit 982ef73

Please sign in to comment.