Skip to content

Commit

Permalink
replace cardinality checks with emptiness checks (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstartin authored and lemire committed Apr 2, 2018
1 parent a677b01 commit 60048a6
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 52 deletions.
4 changes: 2 additions & 2 deletions roaringbitmap/src/main/java/org/roaringbitmap/Container.java
Expand Up @@ -109,9 +109,9 @@ public Container and(Container x) {
* @return aggregated container
*/
public int andCardinality(Container x) {
if (this.getCardinality() == 0) {
if (this.isEmpty()) {
return 0;
} else if (x.getCardinality() == 0) {
} else if (x.isEmpty()) {
return 0;
} else {
if (x instanceof ArrayContainer) {
Expand Down
38 changes: 19 additions & 19 deletions roaringbitmap/src/main/java/org/roaringbitmap/RoaringBitmap.java
Expand Up @@ -288,7 +288,7 @@ public static RoaringBitmap and(final RoaringBitmap x1, final RoaringBitmap x2)
final Container c1 = x1.highLowContainer.getContainerAtIndex(pos1);
final Container c2 = x2.highLowContainer.getContainerAtIndex(pos2);
final Container c = c1.and(c2);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append(s1, c);
}
++pos1;
Expand Down Expand Up @@ -356,7 +356,7 @@ public static RoaringBitmap andNot(final RoaringBitmap x1, final RoaringBitmap x
final Container c1 = x1.highLowContainer.getContainerAtIndex(pos1);
final Container c2 = x2.highLowContainer.getContainerAtIndex(pos2);
final Container c = c1.andNot(c2);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append(s1, c);
}
++pos1;
Expand Down Expand Up @@ -500,7 +500,7 @@ public static RoaringBitmap flip(RoaringBitmap bm, final long rangeStart, final
if (i >= 0) {
Container c =
bm.highLowContainer.getContainerAtIndex(i).not(containerStart, containerLast + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.insertNewKeyValueAt(-j - 1, (short) hb, c);
}

Expand Down Expand Up @@ -823,7 +823,7 @@ public static RoaringBitmap remove(RoaringBitmap rb, final long rangeStart, fina
final int i = rb.highLowContainer.getIndex((short) hbStart);
if (i >= 0) {
final Container c = rb.highLowContainer.getContainerAtIndex(i).remove(lbStart, lbLast + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append((short) hbStart, c);
}
}
Expand All @@ -835,13 +835,13 @@ public static RoaringBitmap remove(RoaringBitmap rb, final long rangeStart, fina
if ((ifirst >= 0) && (lbStart != 0)) {
final Container c = rb.highLowContainer.getContainerAtIndex(ifirst).remove(lbStart,
Util.maxLowBitAsInteger() + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append((short) hbStart, c);
}
}
if ((ilast >= 0) && (lbLast != Util.maxLowBitAsInteger())) {
final Container c = rb.highLowContainer.getContainerAtIndex(ilast).remove(0, lbLast + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append((short) hbLast, c);
}
}
Expand Down Expand Up @@ -894,7 +894,7 @@ public static RoaringBitmap xor(final RoaringBitmap x1, final RoaringBitmap x2)
if (s1 == s2) {
final Container c = x1.highLowContainer.getContainerAtIndex(pos1)
.xor(x2.highLowContainer.getContainerAtIndex(pos2));
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append(s1, c);
}
pos1++;
Expand Down Expand Up @@ -1085,7 +1085,7 @@ public void and(final RoaringBitmap x2) {
final Container c1 = highLowContainer.getContainerAtIndex(pos1);
final Container c2 = x2.highLowContainer.getContainerAtIndex(pos2);
final Container c = c1.iand(c2);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
highLowContainer.replaceKeyAndContainerAtIndex(intersectionSize++, s1, c);
}
++pos1;
Expand Down Expand Up @@ -1158,7 +1158,7 @@ public void andNot(final RoaringBitmap x2) {
final Container c1 = highLowContainer.getContainerAtIndex(pos1);
final Container c2 = x2.highLowContainer.getContainerAtIndex(pos2);
final Container c = c1.iandNot(c2);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
highLowContainer.replaceKeyAndContainerAtIndex(intersectionSize++, s1, c);
}
++pos1;
Expand Down Expand Up @@ -1430,7 +1430,7 @@ public void flip(final int x) {
final int i = highLowContainer.getIndex(hb);
if (i >= 0) {
Container c = highLowContainer.getContainerAtIndex(i).flip(Util.lowbits(x));
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
highLowContainer.setContainerAtIndex(i, c);
} else {
highLowContainer.removeAtIndex(i);
Expand Down Expand Up @@ -1470,7 +1470,7 @@ public void flip(final long rangeStart, final long rangeEnd) {
if (i >= 0) {
final Container c =
highLowContainer.getContainerAtIndex(i).inot(containerStart, containerLast + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
highLowContainer.setContainerAtIndex(i, c);
} else {
highLowContainer.removeAtIndex(i);
Expand Down Expand Up @@ -1919,7 +1919,7 @@ public void remove(final int x) {
}
highLowContainer.setContainerAtIndex(i,
highLowContainer.getContainerAtIndex(i).remove(Util.lowbits(x)));
if (highLowContainer.getContainerAtIndex(i).getCardinality() == 0) {
if (highLowContainer.getContainerAtIndex(i).isEmpty()) {
highLowContainer.removeAtIndex(i);
}
}
Expand Down Expand Up @@ -1949,7 +1949,7 @@ public void remove(final long rangeStart, final long rangeEnd) {
return;
}
final Container c = highLowContainer.getContainerAtIndex(i).iremove(lbStart, lbLast + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
highLowContainer.setContainerAtIndex(i, c);
} else {
highLowContainer.removeAtIndex(i);
Expand All @@ -1962,7 +1962,7 @@ public void remove(final long rangeStart, final long rangeEnd) {
if (lbStart != 0) {
final Container c = highLowContainer.getContainerAtIndex(ifirst).iremove(lbStart,
Util.maxLowBitAsInteger() + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
highLowContainer.setContainerAtIndex(ifirst, c);
ifirst++;
}
Expand All @@ -1973,7 +1973,7 @@ public void remove(final long rangeStart, final long rangeEnd) {
if (ilast >= 0) {
if (lbLast != Util.maxLowBitAsInteger()) {
final Container c = highLowContainer.getContainerAtIndex(ilast).iremove(0, lbLast + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
highLowContainer.setContainerAtIndex(ilast, c);
} else {
ilast++;
Expand Down Expand Up @@ -2278,7 +2278,7 @@ private static RoaringBitmap selectRangeWithoutCopy(RoaringBitmap rb, final long
if (i >= 0) {
final Container c = rb.highLowContainer.getContainerAtIndex(i).remove(0, lbStart)
.iremove(lbLast + 1, Util.maxLowBitAsInteger() + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append((short) hbStart, c);
}
}
Expand All @@ -2288,7 +2288,7 @@ private static RoaringBitmap selectRangeWithoutCopy(RoaringBitmap rb, final long
int ilast = rb.highLowContainer.getIndex((short) hbLast);
if (ifirst >= 0) {
final Container c = rb.highLowContainer.getContainerAtIndex(ifirst).remove(0, lbStart);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append((short) hbStart, c);
}
}
Expand All @@ -2308,7 +2308,7 @@ private static RoaringBitmap selectRangeWithoutCopy(RoaringBitmap rb, final long
if (ilast >= 0) {
final Container c = rb.highLowContainer.getContainerAtIndex(ilast).remove(lbLast + 1,
Util.maxLowBitAsInteger() + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.highLowContainer.append((short) hbLast, c);
}
}
Expand Down Expand Up @@ -2405,7 +2405,7 @@ public void xor(final RoaringBitmap x2) {
if (s1 == s2) {
final Container c = highLowContainer.getContainerAtIndex(pos1)
.ixor(x2.highLowContainer.getContainerAtIndex(pos2));
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
this.highLowContainer.setContainerAtIndex(pos1, c);
pos1++;
} else {
Expand Down
Expand Up @@ -1727,7 +1727,7 @@ public Container ixor(RunContainer x) {
}

private RunContainer lazyandNot(ArrayContainer x) {
if (x.getCardinality() == 0) {
if (x.isEmpty()) {
return this;
}
RunContainer answer = new RunContainer(new short[2 * (this.nbrruns + x.cardinality)], 0);
Expand Down Expand Up @@ -1835,7 +1835,7 @@ private Container lazyorToRun(ArrayContainer x) {
}

private Container lazyxor(ArrayContainer x) {
if (x.getCardinality() == 0) {
if (x.isEmpty()) {
return this;
}
if (this.nbrruns == 0) {
Expand Down
Expand Up @@ -265,7 +265,7 @@ public static MutableRoaringBitmap and(final ImmutableRoaringBitmap x1,
final MappeableContainer c1 = x1.highLowContainer.getContainerAtIndex(pos1);
final MappeableContainer c2 = x2.highLowContainer.getContainerAtIndex(pos2);
final MappeableContainer c = c1.and(c2);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.getMappeableRoaringArray().append(s1, c);
}
++pos1;
Expand Down Expand Up @@ -406,7 +406,7 @@ public static MutableRoaringBitmap andNot(final ImmutableRoaringBitmap x1,
final MappeableContainer c1 = x1.highLowContainer.getContainerAtIndex(pos1);
final MappeableContainer c2 = x2.highLowContainer.getContainerAtIndex(pos2);
final MappeableContainer c = c1.andNot(c2);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.getMappeableRoaringArray().append(s1, c);
}
++pos1;
Expand Down Expand Up @@ -484,7 +484,7 @@ public static MutableRoaringBitmap flip(ImmutableRoaringBitmap bm, final long ra
if (i >= 0) {
final MappeableContainer c =
bm.highLowContainer.getContainerAtIndex(i).not(containerStart, containerLast + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.getMappeableRoaringArray().insertNewKeyValueAt(-j - 1, hb, c);
}

Expand Down Expand Up @@ -584,7 +584,7 @@ private static MutableRoaringBitmap selectRangeWithoutCopy(ImmutableRoaringBitma
if (i >= 0) {
final MappeableContainer c = rb.highLowContainer.getContainerAtIndex(i).remove(0, lbStart)
.iremove(lbLast + 1, BufferUtil.maxLowBitAsInteger() + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
((MutableRoaringArray) answer.highLowContainer).append((short) hbStart, c);
}
}
Expand All @@ -595,7 +595,7 @@ private static MutableRoaringBitmap selectRangeWithoutCopy(ImmutableRoaringBitma
if (ifirst >= 0) {
final MappeableContainer c =
rb.highLowContainer.getContainerAtIndex(ifirst).remove(0, lbStart);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
((MutableRoaringArray) answer.highLowContainer).append((short) hbStart, c);
}
}
Expand All @@ -614,7 +614,7 @@ private static MutableRoaringBitmap selectRangeWithoutCopy(ImmutableRoaringBitma
if (ilast >= 0) {
final MappeableContainer c = rb.highLowContainer.getContainerAtIndex(ilast).remove(lbLast + 1,
BufferUtil.maxLowBitAsInteger() + 1);
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
((MutableRoaringArray) answer.highLowContainer).append((short) hbLast, c);
}
}
Expand Down Expand Up @@ -895,7 +895,7 @@ public static MutableRoaringBitmap xor(final ImmutableRoaringBitmap x1,
while (true) {
if (i1.key() == i2.key()) {
final MappeableContainer c = i1.getContainer().xor(i2.getContainer());
if (c.getCardinality() > 0) {
if (!c.isEmpty()) {
answer.getMappeableRoaringArray().append(i1.key(), c);
}
i1.advance();
Expand Down
Expand Up @@ -113,9 +113,9 @@ public int xorCardinality(MappeableContainer other) {
* @return aggregated container
*/
public int andCardinality(MappeableContainer x) {
if (this.getCardinality() == 0) {
if (this.isEmpty()) {
return 0;
} else if (x.getCardinality() == 0) {
} else if (x.isEmpty()) {
return 0;
} else {
if (x instanceof MappeableArrayContainer) {
Expand Down
Expand Up @@ -1608,7 +1608,7 @@ public MappeableContainer ixor(MappeableRunContainer x) {
}

private MappeableRunContainer lazyandNot(MappeableArrayContainer x) {
if (x.getCardinality() == 0) {
if (x.isEmpty()) {
return this;
}
MappeableRunContainer answer =
Expand Down Expand Up @@ -1723,7 +1723,7 @@ private MappeableContainer lazyorToRun(MappeableArrayContainer x) {
}

private MappeableContainer lazyxor(MappeableArrayContainer x) {
if (x.getCardinality() == 0) {
if (x.isEmpty()) {
return this;
}
if (this.nbrruns == 0) {
Expand Down

0 comments on commit 60048a6

Please sign in to comment.