Made the bulk clear() fast like bulk set() and flip() for BitSet.

This commit is contained in:
Mike Keesey
2012-07-05 22:16:19 -06:00
parent 990f4fd154
commit 9c9ee5c26d
2 changed files with 46 additions and 26 deletions

View File

@ -85,23 +85,6 @@ public class BitSet implements Serializable, Cloneable {
bits[i] ^= otherBits.bits[i];
}
}
public void flip(int index) {
flip(index, index+1);
}
public void flip(int fromIndex, int toIndex) {
if (fromIndex > toIndex || fromIndex < 0 || toIndex < 0) {
throw new IndexOutOfBoundsException();
} else if (fromIndex != toIndex) {
MaskInfoIterator iter = new MaskInfoIterator(fromIndex, toIndex);
enlarge(iter.getLastPartition());
while (iter.hasNext()) {
MaskInfo info = iter.next();
bits[info.partitionIndex] ^= info.mask;
}
}
}
private void enlarge(int newPartition) {
if (bits == null || bits.length < (newPartition + 1)) {
@ -113,13 +96,6 @@ 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 boolean get(int index) {
int pos = longPosition(index);
if (pos < bits.length) {
@ -128,6 +104,23 @@ public class BitSet implements Serializable, Cloneable {
return false;
}
public void flip(int index) {
flip(index, index+1);
}
public void flip(int fromIndex, int toIndex) {
if (fromIndex > toIndex || fromIndex < 0 || toIndex < 0) {
throw new IndexOutOfBoundsException();
} else if (fromIndex != toIndex) {
MaskInfoIterator iter = new MaskInfoIterator(fromIndex, toIndex);
enlarge(iter.getLastPartition());
while (iter.hasNext()) {
MaskInfo info = iter.next();
bits[info.partitionIndex] ^= info.mask;
}
}
}
public void set(int index) {
int pos = longPosition(index);
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) {
for (int i = start; i < end; i++) {
clear(i);
MaskInfoIterator iter = new MaskInfoIterator(start, end);
while (iter.hasNext()) {
MaskInfo info = iter.next();
bits[info.partitionIndex] &= (MASK ^ info.mask);
}
}