LUCENE-3179: Fix bug in handling of indexes >= size in OpenBitSet.prevSetBit()
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1139431 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lucene/src/java/org/apache/lucene/util/OpenBitSet.java b/lucene/src/java/org/apache/lucene/util/OpenBitSet.java
index 89a40dc..281faaa 100644
--- a/lucene/src/java/org/apache/lucene/util/OpenBitSet.java
+++ b/lucene/src/java/org/apache/lucene/util/OpenBitSet.java
@@ -664,15 +664,19 @@
* -1 is returned if there are no more set bits.
*/
public int prevSetBit(int index) {
- if (index < 0) {
- return -1;
- }
- int i = index>>6;
+ int i = index >> 6;
+ final int subIndex;
+ long word;
if (i >= wlen) {
i = wlen - 1;
+ if (i < 0) return -1;
+ subIndex = 63; // last possible bit
+ word = bits[i];
+ } else {
+ if (i < 0) return -1;
+ subIndex = index & 0x3f; // index within the word
+ word = (bits[i] << (63-subIndex)); // skip all the bits to the left of index
}
- final int subIndex = index & 0x3f; // index within the word
- long word = (bits[i] << (63-subIndex)); // skip all the bits to the left of index
if (word != 0) {
return (i << 6) + subIndex - Long.numberOfLeadingZeros(word); // See LUCENE-3197
diff --git a/lucene/src/test/org/apache/lucene/util/TestOpenBitSet.java b/lucene/src/test/org/apache/lucene/util/TestOpenBitSet.java
index 34f6ba9..61322fe 100644
--- a/lucene/src/test/org/apache/lucene/util/TestOpenBitSet.java
+++ b/lucene/src/test/org/apache/lucene/util/TestOpenBitSet.java
@@ -42,8 +42,8 @@
}
void doPrevSetBit(BitSet a, OpenBitSet b) {
- int aa=a.length();
- int bb=aa;
+ int aa = a.size() + random.nextInt(100);
+ int bb = aa;
do {
// aa = a.prevSetBit(aa-1);
aa--;