fix OpenBitSet.flip bug when indicies outside current size

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@549948 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2007-06-22 20:27:28 +00:00
parent aa705c5f14
commit 7b4db83305
2 changed files with 26 additions and 36 deletions

View File

@ -330,6 +330,7 @@ public class OpenBitSet implements Cloneable, Serializable {
if (endIndex <= startIndex) return;
int startWord = (int)(startIndex>>6);
if (startWord >= wlen) return;
// since endIndex is one past the end, this is index of the last
// word to be changed.
@ -439,7 +440,6 @@ public class OpenBitSet implements Cloneable, Serializable {
*/
public void flip(long startIndex, long endIndex) {
if (endIndex <= startIndex) return;
int oldlen = wlen;
int startWord = (int)(startIndex>>6);
@ -455,7 +455,7 @@ public class OpenBitSet implements Cloneable, Serializable {
***/
long startmask = -1L << startIndex;
long endmask = -1L >>> -endIndex; // 64-endIndex is the same as -endIndex due to wrap
long endmask = -1L >>> -endIndex; // 64-(endIndex&0x3f) is the same as -endIndex due to wrap
if (startWord == endWord) {
bits[startWord] ^= (startmask & endmask);
@ -464,15 +464,10 @@ public class OpenBitSet implements Cloneable, Serializable {
bits[startWord] ^= startmask;
int middle = Math.min(oldlen, endWord);
for (int i=startWord+1; i<middle; i++) {
for (int i=startWord+1; i<endWord; i++) {
bits[i] = ~bits[i];
}
if (endWord>middle) {
Arrays.fill(bits,middle,endWord,-1L);
}
bits[endWord] ^= endmask;
}

View File

@ -76,26 +76,6 @@ public class TestOpenBitSet extends TestCase {
int nOper = rand.nextInt(sz);
for (int j=0; j<nOper; j++) {
int idx;
int idx1,idx2;
idx1 = rand.nextInt(sz);
idx2 = rand.nextInt(sz);
if (idx1>idx2) { idx=idx1; idx1=idx2; idx2=idx; }
a.set(idx1,idx2);
b.set(idx1,idx2);
idx1 = rand.nextInt(sz);
idx2 = rand.nextInt(sz);
if (idx1>idx2) { idx=idx1; idx1=idx2; idx2=idx; }
a.clear(idx1,idx2);
b.clear(idx1,idx2);
idx1 = rand.nextInt(sz);
idx2 = rand.nextInt(sz);
if (idx1>idx2) { idx=idx1; idx1=idx2; idx2=idx; }
a.flip(idx1,idx2);
b.flip(idx1,idx2);
idx = rand.nextInt(sz);
a.set(idx);
@ -122,15 +102,30 @@ public class TestOpenBitSet extends TestCase {
// test that the various ways of accessing the bits are equivalent
doGet(a,b);
doNextSetBit(a,b);
doIterate(a,b);
// test negation
int fromIndex = rand.nextInt(sz+80);
int toIndex = fromIndex + rand.nextInt((sz>>1)+1);
BitSet a_not = (BitSet)a.clone(); a_not.flip(fromIndex,toIndex);
OpenBitSet b_not = (OpenBitSet)b.clone(); b_not.flip(fromIndex,toIndex);
doIterate(a,b);
// test ranges, including possible extension
int fromIndex, toIndex;
fromIndex = rand.nextInt(sz+80);
toIndex = fromIndex + rand.nextInt((sz>>1)+1);
BitSet aa = (BitSet)a.clone(); aa.flip(fromIndex,toIndex);
OpenBitSet bb = (OpenBitSet)b.clone(); bb.flip(fromIndex,toIndex);
doIterate(aa,bb); // a problem here is from flip or doIterate
fromIndex = rand.nextInt(sz+80);
toIndex = fromIndex + rand.nextInt((sz>>1)+1);
aa = (BitSet)a.clone(); aa.clear(fromIndex,toIndex);
bb = (OpenBitSet)b.clone(); bb.clear(fromIndex,toIndex);
doNextSetBit(aa,bb); // a problem here is from clear() or nextSetBit
fromIndex = rand.nextInt(sz+80);
toIndex = fromIndex + rand.nextInt((sz>>1)+1);
aa = (BitSet)a.clone(); aa.set(fromIndex,toIndex);
bb = (OpenBitSet)b.clone(); bb.set(fromIndex,toIndex);
doNextSetBit(aa,bb); // a problem here is from set() or nextSetBit
if (a0 != null) {
assertEquals( a.equals(a0), b.equals(b0));