Skip to content

Commit

Permalink
[brettwooldridge#15] previousSetBit resets word length after miss
Browse files Browse the repository at this point in the history
Currently it sets the length to the position of the needle in the first
word it looks up and only searches indices below that. This is incorrect
after the initial word where we need to search from the top.
  • Loading branch information
BrentDouglas committed Nov 10, 2018
1 parent 07e4074 commit 478d408
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/com/zaxxer/sparsebits/SparseBitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ public int previousClearBit(int i)
if (w1 > aLength - 1)
return i;
w1 = Math.min(w1, aLength - 1);
final int w4 = i % LENGTH4;
int w4 = i % LENGTH4;

long word;
long[][] a2;
Expand Down Expand Up @@ -1074,6 +1074,7 @@ public int previousClearBit(int i)
if ((word & (1L << bitIdx)) == 0)
return (((w1 << SHIFT1) + (w2 << SHIFT2) + w3) << SHIFT3) + bitIdx;
}
w4 = LENGTH4 - 1;
}
w3 = LENGTH3 - 1;
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/zaxxer/sparsebits/PreviousClearBitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,14 @@ public void randomMultiEntry() throws Exception {
values.clear();
}
}

@Test
public void bug15() throws Exception {
set.set(1);
set.set(64);
assertEquals(63, set.previousClearBit(64));
set.clear(0);
set.set(1);
assertEquals(63, set.previousClearBit(64));
}
}

0 comments on commit 478d408

Please sign in to comment.