mirror of
https://github.com/corda/corda.git
synced 2025-05-31 22:50:53 +00:00
Adding cardinality() to BitSet.
This commit is contained in:
parent
3c2adb86f5
commit
31311160c3
@ -182,4 +182,12 @@ public class BitSet implements Serializable, Cloneable {
|
|||||||
return nextBit(fromIndex, true);
|
return nextBit(fromIndex, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int cardinality() {
|
||||||
|
int numSetBits = 0;
|
||||||
|
for (int i = nextSetBit(0); i >= 0; i = nextSetBit(i+1)) {
|
||||||
|
++numSetBits;
|
||||||
|
}
|
||||||
|
|
||||||
|
return numSetBits;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,16 +14,19 @@ public class BitsetTest {
|
|||||||
assertTrue("bit 5 is set", bits.get(5));
|
assertTrue("bit 5 is set", bits.get(5));
|
||||||
assertTrue("bit 0 is not set", !bits.get(0));
|
assertTrue("bit 0 is not set", !bits.get(0));
|
||||||
assertTrue("bit 16 is not set", !bits.get(16));
|
assertTrue("bit 16 is not set", !bits.get(16));
|
||||||
|
assertCardinality(bits, 2);
|
||||||
|
|
||||||
bits.and(other);
|
bits.and(other);
|
||||||
|
|
||||||
assertTrue("bit 5 is set", bits.get(5));
|
assertTrue("bit 5 is set", bits.get(5));
|
||||||
assertTrue("bit 1 is not set", !bits.get(1));
|
assertTrue("bit 1 is not set", !bits.get(1));
|
||||||
|
assertCardinality(bits, 1);
|
||||||
|
|
||||||
bits.set(100);
|
bits.set(100);
|
||||||
|
|
||||||
assertTrue("bit 100 is set", bits.get(100));
|
assertTrue("bit 100 is set", bits.get(100));
|
||||||
assertTrue("bit 101 is not set", !bits.get(101));
|
assertTrue("bit 101 is not set", !bits.get(101));
|
||||||
|
assertCardinality(bits, 2);
|
||||||
|
|
||||||
other.set(101);
|
other.set(101);
|
||||||
|
|
||||||
@ -38,6 +41,7 @@ public class BitsetTest {
|
|||||||
assertEquals("second bit is 100 from 100", 100, bits.nextSetBit(100));
|
assertEquals("second bit is 100 from 100", 100, bits.nextSetBit(100));
|
||||||
assertEquals("third bit is 101", 101, bits.nextSetBit(101));
|
assertEquals("third bit is 101", 101, bits.nextSetBit(101));
|
||||||
assertEquals("there is no 4th bit", -1, bits.nextSetBit(102));
|
assertEquals("there is no 4th bit", -1, bits.nextSetBit(102));
|
||||||
|
assertCardinality(bits, 3);
|
||||||
|
|
||||||
assertEquals("first empty bit is 0", 0, bits.nextClearBit(0));
|
assertEquals("first empty bit is 0", 0, bits.nextClearBit(0));
|
||||||
assertEquals("after 5, 6 is empty", 6, bits.nextClearBit(5));
|
assertEquals("after 5, 6 is empty", 6, bits.nextClearBit(5));
|
||||||
@ -61,4 +65,8 @@ public class BitsetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void assertCardinality(BitSet set, int expectedCardinality) {
|
||||||
|
assertEquals("Checking cardinality", expectedCardinality, set.cardinality());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user