Program: 100602
Project: 100602
Buggy source:
private void fixAfterSparseInsert(){
  if (sparseInsertCount++ > SPARSE_INSERT_THRESHOLD) {
    sparseInsertCount=0;
    IntIterator sparseBits=sparsePart.intIterator();
    int thisBit=sparseBits.next();
    if (densePart == null) {
      int maxOffset=-1;
      int maxCount=-1;
      int maxMax=-1;
      int offset=thisBit;
      int bits=32;
      int count=1;
      while (sparseBits.hasNext()) {
        int nextBit=sparseBits.next();
        int newBits=bits + (nextBit - thisBit);
        int newCount=count + 1;
        if (newBits > (32 * newCount)) {
          count=newCount;
          bits=newBits;
        }
 else         if (bits > 32 * count) {
          if (count > maxCount) {
            maxOffset=offset;
            maxMax=thisBit;
            maxCount=count;
          }
          offset=nextBit;
          count=1;
          bits=32;
        }
        thisBit=nextBit;
      }
      if (maxOffset != -1) {
        densePart=new OffsetBitVector(maxOffset,maxMax - maxOffset);
        sparseBits=sparsePart.intIterator();
        while ((sparseBits.next()) != maxOffset)         ;
        for (int i=0; i < maxCount; i++) {
          densePart.set(sparseBits.next());
        }
        for (int bit1=densePart.nextSetBit(0); bit1 != -1; bit1=densePart.nextSetBit(bit1 + 1)) {
          sparsePart.remove(bit1);
        }
      }
    }
 else {
      int moveCount=0;
      int newOffset=-1;
      int newLength=-1;
      if (thisBit < densePart.getOffset()) {
        newOffset=thisBit;
        int bits=32;
        int count=1;
        while (sparseBits.hasNext()) {
          int nextBit=sparseBits.next();
          if (nextBit >= densePart.getOffset()) {
            if (bits > (32 * count)) {
              moveCount+=count;
              break;
            }
 else {
              newOffset=-1;
            }
          }
 else {
            bits+=(nextBit - thisBit);
            count++;
            if (bits > (32 * count)) {
              newOffset=nextBit;
              count=1;
              bits=32;
            }
          }
        }
      }
      if (thisBit >= densePart.length()) {
        int count=1;
        int bits=(thisBit + 1 - densePart.length());
        if (32 * count > bits) {
          newLength=thisBit;
        }
        while (sparseBits.hasNext()) {
          thisBit=sparseBits.next();
          count++;
          bits=(thisBit + 1 - densePart.length());
          newLength=(32 * count > bits) ? thisBit : newLength;
        }
        if (newLength > -1) {
          moveCount+=count;
        }
      }
      if (newOffset != -1 || newLength != -1) {
        int index=0;
        int[] bits=new int[moveCount];
        for (sparseBits=sparsePart.intIterator(); sparseBits.hasNext(); ) {
          int bit=sparseBits.next();
          if (newOffset != -1 && bit >= newOffset && bit <= densePart.getOffset()) {
            bits[index++]=bit;
          }
          if (newLength != -1 && bit >= densePart.length() && bit <= newLength) {
            bits[index++]=bit;
          }
        }
        for (int i=0; i < moveCount; i++) {
          sparsePart.remove(bits[i]);
          densePart.set(bits[i]);
        }
      }
    }
  }
}

Fixed version (ground truth):
private void fixAfterSparseInsert(){
  if (sparseInsertCount++ > SPARSE_INSERT_THRESHOLD) {
    sparseInsertCount=0;
    IntIterator sparseBits=sparsePart.intIterator();
    int thisBit=sparseBits.next();
    if (densePart == null) {
      int maxOffset=-1;
      int maxCount=-1;
      int maxMax=-1;
      int offset=thisBit;
      int bits=32;
      int count=1;
      while (sparseBits.hasNext()) {
        int nextBit=sparseBits.next();
        int newBits=bits + (nextBit - thisBit);
        int newCount=count + 1;
        if (newBits < (32 * newCount)) {
          count=newCount;
          bits=newBits;
          if (count > maxCount) {
            maxOffset=offset;
            maxMax=nextBit;
            maxCount=count;
          }
        }
 else         if (bits < 32 * count) {
          offset=nextBit;
          count=1;
          bits=32;
        }
        thisBit=nextBit;
      }
      if (maxOffset != -1) {
        densePart=new OffsetBitVector(maxOffset,maxMax - maxOffset);
        sparseBits=sparsePart.intIterator();
        int bit;
        while ((bit=sparseBits.next()) != maxOffset)         ;
        densePart.set(bit);
        for (int i=1; i < maxCount; i++) {
          densePart.set(sparseBits.next());
        }
        for (int bit1=densePart.nextSetBit(0); bit1 != -1; bit1=densePart.nextSetBit(bit1 + 1)) {
          sparsePart.remove(bit1);
        }
      }
    }
 else {
      int moveCount=0;
      int newOffset=-1;
      int newCount=-1;
      int newLength=-1;
      if (thisBit < densePart.getOffset()) {
        newOffset=thisBit;
        int bits=32;
        int count=1;
        while (sparseBits.hasNext()) {
          int nextBit=sparseBits.next();
          if (nextBit >= densePart.getOffset() || !sparseBits.hasNext()) {
            if (!sparseBits.hasNext())             count++;
            if (densePart.getOffset() - newOffset < (32 * count)) {
              moveCount+=count;
            }
 else {
              newOffset=-1;
            }
            thisBit=nextBit;
            break;
          }
 else {
            bits+=(nextBit - thisBit);
            count++;
            if (bits > (32 * count)) {
              newOffset=nextBit;
              count=1;
              bits=32;
            }
            thisBit=nextBit;
          }
        }
      }
      while (thisBit < densePart.length() && sparseBits.hasNext()) {
        thisBit=sparseBits.next();
      }
      if (thisBit >= densePart.length()) {
        int count=1;
        int bits=(thisBit + 1 - densePart.length());
        if (32 * count > bits) {
          newLength=thisBit;
          newCount=1;
        }
        while (sparseBits.hasNext()) {
          thisBit=sparseBits.next();
          count++;
          bits=(thisBit + 1 - densePart.length());
          newLength=((32 * count) > bits) ? thisBit : newLength;
          newCount=((32 * count) > bits) ? count : newCount;
        }
        if (newLength > -1) {
          moveCount+=newCount;
        }
      }
      if (newOffset != -1 || newLength != -1) {
        int index=0;
        int[] bits=new int[moveCount];
        for (sparseBits=sparsePart.intIterator(); sparseBits.hasNext(); ) {
          int bit=sparseBits.next();
          if (newOffset != -1 && bit >= newOffset && bit < densePart.getOffset()) {
            bits[index++]=bit;
          }
          if (newLength != -1 && bit >= densePart.length() && bit <= newLength) {
            bits[index++]=bit;
          }
        }
        for (int i=0; i < moveCount; i++) {
          sparsePart.remove(bits[i]);
          densePart.set(bits[i]);
        }
      }
    }
  }
}