mirror of
https://github.com/corda/corda.git
synced 2025-06-15 05:38:14 +00:00
Made the bulk clear() fast like bulk set() and flip() for BitSet.
This commit is contained in:
@ -86,6 +86,24 @@ public class BitSet implements Serializable, Cloneable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void enlarge(int newPartition) {
|
||||||
|
if (bits == null || bits.length < (newPartition + 1)) {
|
||||||
|
long[] newBits = new long[newPartition + 1];
|
||||||
|
if (bits != null) {
|
||||||
|
System.arraycopy(bits, 0, newBits, 0, bits.length);
|
||||||
|
}
|
||||||
|
bits = newBits;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean get(int index) {
|
||||||
|
int pos = longPosition(index);
|
||||||
|
if (pos < bits.length) {
|
||||||
|
return (bits[pos] & bitPosition(index)) != 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public void flip(int index) {
|
public void flip(int index) {
|
||||||
flip(index, index+1);
|
flip(index, index+1);
|
||||||
}
|
}
|
||||||
@ -103,31 +121,6 @@ public class BitSet implements Serializable, Cloneable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enlarge(int newPartition) {
|
|
||||||
if (bits == null || bits.length < (newPartition + 1)) {
|
|
||||||
long[] newBits = new long[newPartition + 1];
|
|
||||||
if (bits != null) {
|
|
||||||
System.arraycopy(bits, 0, newBits, 0, bits.length);
|
|
||||||
}
|
|
||||||
bits = newBits;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clear(int index) {
|
|
||||||
int pos = longPosition(index);
|
|
||||||
if (pos < bits.length) {
|
|
||||||
bits[pos] &= (MASK ^ bitPosition(index));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean get(int index) {
|
|
||||||
int pos = longPosition(index);
|
|
||||||
if (pos < bits.length) {
|
|
||||||
return (bits[pos] & bitPosition(index)) != 0;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void set(int index) {
|
public void set(int index) {
|
||||||
int pos = longPosition(index);
|
int pos = longPosition(index);
|
||||||
enlarge(pos);
|
enlarge(pos);
|
||||||
@ -143,9 +136,18 @@ public class BitSet implements Serializable, Cloneable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clear(int index) {
|
||||||
|
int pos = longPosition(index);
|
||||||
|
if (pos < bits.length) {
|
||||||
|
bits[pos] &= (MASK ^ bitPosition(index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void clear(int start, int end) {
|
public void clear(int start, int end) {
|
||||||
for (int i = start; i < end; i++) {
|
MaskInfoIterator iter = new MaskInfoIterator(start, end);
|
||||||
clear(i);
|
while (iter.hasNext()) {
|
||||||
|
MaskInfo info = iter.next();
|
||||||
|
bits[info.partitionIndex] &= (MASK ^ info.mask);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,6 +48,7 @@ public class BitsetTest {
|
|||||||
assertEquals("after 100, 102 is empty", 102, bits.nextClearBit(100));
|
assertEquals("after 100, 102 is empty", 102, bits.nextClearBit(100));
|
||||||
|
|
||||||
testFlip();
|
testFlip();
|
||||||
|
testClear();
|
||||||
|
|
||||||
BitSet expandingSet = new BitSet();
|
BitSet expandingSet = new BitSet();
|
||||||
//should force us to have 3 partitions.
|
//should force us to have 3 partitions.
|
||||||
@ -80,6 +81,23 @@ public class BitsetTest {
|
|||||||
assertTrue("70 should be false", !bitset.get(70));
|
assertTrue("70 should be false", !bitset.get(70));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void testClear() {
|
||||||
|
BitSet bitset = new BitSet();
|
||||||
|
bitset.set(0, 20);
|
||||||
|
assertCardinality(bitset, 20);
|
||||||
|
|
||||||
|
bitset.clear(1);
|
||||||
|
assertTrue("bit 1 should be 0", !bitset.get(1));
|
||||||
|
assertCardinality(bitset, 19);
|
||||||
|
|
||||||
|
bitset.clear(0, 3);
|
||||||
|
assertTrue("bit 0 should be 0", !bitset.get(0));
|
||||||
|
assertTrue("bit 1 should be 0", !bitset.get(1));
|
||||||
|
assertTrue("bit 2 should be 0", !bitset.get(2));
|
||||||
|
assertTrue("bit 3 should be 1", bitset.get(3));
|
||||||
|
assertCardinality(bitset, 17);
|
||||||
|
}
|
||||||
|
|
||||||
static void assertTrue(String msg, boolean flag) {
|
static void assertTrue(String msg, boolean flag) {
|
||||||
if (flag) {
|
if (flag) {
|
||||||
System.out.println(msg + " : OK.");
|
System.out.println(msg + " : OK.");
|
||||||
|
Reference in New Issue
Block a user