mirror of
https://github.com/zxing/zxing.git
synced 2024-11-10 04:54:04 -08:00
Issue 1764 bit reverse speedup
git-svn-id: https://zxing.googlecode.com/svn/trunk@2875 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
5ce090956d
commit
e23507a825
|
@ -36,16 +36,22 @@ public final class BitArray {
|
||||||
this.bits = makeArray(size);
|
this.bits = makeArray(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For testing only
|
||||||
|
BitArray(int[] bits, int size) {
|
||||||
|
this.bits = bits;
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSizeInBytes() {
|
public int getSizeInBytes() {
|
||||||
return (size + 7) >> 3;
|
return (size + 7) / 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureCapacity(int size) {
|
private void ensureCapacity(int size) {
|
||||||
if (size > bits.length << 5) {
|
if (size > bits.length * 32) {
|
||||||
int[] newBits = makeArray(size);
|
int[] newBits = makeArray(size);
|
||||||
System.arraycopy(bits, 0, newBits, 0, bits.length);
|
System.arraycopy(bits, 0, newBits, 0, bits.length);
|
||||||
this.bits = newBits;
|
this.bits = newBits;
|
||||||
|
@ -57,7 +63,7 @@ public final class BitArray {
|
||||||
* @return true iff bit i is set
|
* @return true iff bit i is set
|
||||||
*/
|
*/
|
||||||
public boolean get(int i) {
|
public boolean get(int i) {
|
||||||
return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;
|
return (bits[i / 32] & (1 << (i & 0x1F))) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,7 +72,7 @@ public final class BitArray {
|
||||||
* @param i bit to set
|
* @param i bit to set
|
||||||
*/
|
*/
|
||||||
public void set(int i) {
|
public void set(int i) {
|
||||||
bits[i >> 5] |= 1 << (i & 0x1F);
|
bits[i / 32] |= 1 << (i & 0x1F);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -75,7 +81,7 @@ public final class BitArray {
|
||||||
* @param i bit to set
|
* @param i bit to set
|
||||||
*/
|
*/
|
||||||
public void flip(int i) {
|
public void flip(int i) {
|
||||||
bits[i >> 5] ^= 1 << (i & 0x1F);
|
bits[i / 32] ^= 1 << (i & 0x1F);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,7 +94,7 @@ public final class BitArray {
|
||||||
if (from >= size) {
|
if (from >= size) {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
int bitsOffset = from >> 5;
|
int bitsOffset = from / 32;
|
||||||
int currentBits = bits[bitsOffset];
|
int currentBits = bits[bitsOffset];
|
||||||
// mask off lesser bits first
|
// mask off lesser bits first
|
||||||
currentBits &= ~((1 << (from & 0x1F)) - 1);
|
currentBits &= ~((1 << (from & 0x1F)) - 1);
|
||||||
|
@ -98,7 +104,7 @@ public final class BitArray {
|
||||||
}
|
}
|
||||||
currentBits = bits[bitsOffset];
|
currentBits = bits[bitsOffset];
|
||||||
}
|
}
|
||||||
int result = (bitsOffset << 5) + Integer.numberOfTrailingZeros(currentBits);
|
int result = (bitsOffset * 32) + Integer.numberOfTrailingZeros(currentBits);
|
||||||
return result > size ? size : result;
|
return result > size ? size : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,7 +115,7 @@ public final class BitArray {
|
||||||
if (from >= size) {
|
if (from >= size) {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
int bitsOffset = from >> 5;
|
int bitsOffset = from / 32;
|
||||||
int currentBits = ~bits[bitsOffset];
|
int currentBits = ~bits[bitsOffset];
|
||||||
// mask off lesser bits first
|
// mask off lesser bits first
|
||||||
currentBits &= ~((1 << (from & 0x1F)) - 1);
|
currentBits &= ~((1 << (from & 0x1F)) - 1);
|
||||||
|
@ -119,7 +125,7 @@ public final class BitArray {
|
||||||
}
|
}
|
||||||
currentBits = ~bits[bitsOffset];
|
currentBits = ~bits[bitsOffset];
|
||||||
}
|
}
|
||||||
int result = (bitsOffset << 5) + Integer.numberOfTrailingZeros(currentBits);
|
int result = (bitsOffset * 32) + Integer.numberOfTrailingZeros(currentBits);
|
||||||
return result > size ? size : result;
|
return result > size ? size : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +137,7 @@ public final class BitArray {
|
||||||
* corresponds to bit i, the next-least-significant to i+1, and so on.
|
* corresponds to bit i, the next-least-significant to i+1, and so on.
|
||||||
*/
|
*/
|
||||||
public void setBulk(int i, int newBits) {
|
public void setBulk(int i, int newBits) {
|
||||||
bits[i >> 5] = newBits;
|
bits[i / 32] = newBits;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -148,8 +154,8 @@ public final class BitArray {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
end--; // will be easier to treat this as the last actually set bit -- inclusive
|
end--; // will be easier to treat this as the last actually set bit -- inclusive
|
||||||
int firstInt = start >> 5;
|
int firstInt = start / 32;
|
||||||
int lastInt = end >> 5;
|
int lastInt = end / 32;
|
||||||
for (int i = firstInt; i <= lastInt; i++) {
|
for (int i = firstInt; i <= lastInt; i++) {
|
||||||
int firstBit = i > firstInt ? 0 : start & 0x1F;
|
int firstBit = i > firstInt ? 0 : start & 0x1F;
|
||||||
int lastBit = i < lastInt ? 31 : end & 0x1F;
|
int lastBit = i < lastInt ? 31 : end & 0x1F;
|
||||||
|
@ -193,8 +199,8 @@ public final class BitArray {
|
||||||
return true; // empty range matches
|
return true; // empty range matches
|
||||||
}
|
}
|
||||||
end--; // will be easier to treat this as the last actually set bit -- inclusive
|
end--; // will be easier to treat this as the last actually set bit -- inclusive
|
||||||
int firstInt = start >> 5;
|
int firstInt = start / 32;
|
||||||
int lastInt = end >> 5;
|
int lastInt = end / 32;
|
||||||
for (int i = firstInt; i <= lastInt; i++) {
|
for (int i = firstInt; i <= lastInt; i++) {
|
||||||
int firstBit = i > firstInt ? 0 : start & 0x1F;
|
int firstBit = i > firstInt ? 0 : start & 0x1F;
|
||||||
int lastBit = i < lastInt ? 31 : end & 0x1F;
|
int lastBit = i < lastInt ? 31 : end & 0x1F;
|
||||||
|
@ -220,7 +226,7 @@ public final class BitArray {
|
||||||
public void appendBit(boolean bit) {
|
public void appendBit(boolean bit) {
|
||||||
ensureCapacity(size + 1);
|
ensureCapacity(size + 1);
|
||||||
if (bit) {
|
if (bit) {
|
||||||
bits[size >> 5] |= 1 << (size & 0x1F);
|
bits[size / 32] |= 1 << (size & 0x1F);
|
||||||
}
|
}
|
||||||
size++;
|
size++;
|
||||||
}
|
}
|
||||||
|
@ -293,17 +299,39 @@ public final class BitArray {
|
||||||
*/
|
*/
|
||||||
public void reverse() {
|
public void reverse() {
|
||||||
int[] newBits = new int[bits.length];
|
int[] newBits = new int[bits.length];
|
||||||
int size = this.size;
|
// reverse all int's first
|
||||||
for (int i = 0; i < size; i++) {
|
int len = ((size-1) / 32);
|
||||||
if (get(size - i - 1)) {
|
int oldBitsLen = len + 1;
|
||||||
newBits[i >> 5] |= 1 << (i & 0x1F);
|
for (int i = 0; i < oldBitsLen; i++) {
|
||||||
|
long x = (long) bits[i];
|
||||||
|
x = ((x >> 1) & 0x55555555L) | ((x & 0x55555555L) << 1);
|
||||||
|
x = ((x >> 2) & 0x33333333L) | ((x & 0x33333333L) << 2);
|
||||||
|
x = ((x >> 4) & 0x0f0f0f0fL) | ((x & 0x0f0f0f0fL) << 4);
|
||||||
|
x = ((x >> 8) & 0x00ff00ffL) | ((x & 0x00ff00ffL) << 8);
|
||||||
|
x = ((x >> 16) & 0x0000ffffL) | ((x & 0x0000ffffL) << 16);
|
||||||
|
newBits[len - i] = (int) x;
|
||||||
|
}
|
||||||
|
// now correct the int's if the bit size isn't a multiple of 32
|
||||||
|
if (size != oldBitsLen * 32) {
|
||||||
|
int leftOffset = oldBitsLen * 32 - size;
|
||||||
|
int mask = 1;
|
||||||
|
for (int i = 0; i < 31 - leftOffset; i++) {
|
||||||
|
mask = (mask << 1) | 1;
|
||||||
}
|
}
|
||||||
|
int currentInt = (newBits[0] >> leftOffset) & mask;
|
||||||
|
for (int i = 1; i < oldBitsLen; i++) {
|
||||||
|
int nextInt = newBits[i];
|
||||||
|
currentInt |= nextInt << (32 - leftOffset);
|
||||||
|
newBits[i - 1] = currentInt;
|
||||||
|
currentInt = (nextInt >> leftOffset) & mask;
|
||||||
|
}
|
||||||
|
newBits[oldBitsLen - 1] = currentInt;
|
||||||
}
|
}
|
||||||
bits = newBits;
|
bits = newBits;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int[] makeArray(int size) {
|
private static int[] makeArray(int size) {
|
||||||
return new int[(size + 31) >> 5];
|
return new int[(size + 31) / 32];
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -181,4 +181,39 @@ public final class BitArrayTestCase extends Assert {
|
||||||
assertFalse(array.isRange(0, 64, false));
|
assertFalse(array.isRange(0, 64, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void reverseAlgorithmTest() {
|
||||||
|
int[] oldBits = {128, 256, 512, 6453324, 50934953};
|
||||||
|
for (int size = 1; size < 160; size++) {
|
||||||
|
int[] newBitsOriginal = reverseOriginal(oldBits.clone(), size);
|
||||||
|
BitArray newBitArray = new BitArray(oldBits.clone(), size);
|
||||||
|
newBitArray.reverse();
|
||||||
|
int[] newBitsNew = newBitArray.getBitArray();
|
||||||
|
assertTrue(arraysAreEqual(newBitsOriginal, newBitsNew, size / 32 + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int[] reverseOriginal(int[] oldBits, int size) {
|
||||||
|
int[] newBits = new int[oldBits.length];
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
if (bitSet(oldBits, size - i - 1)) {
|
||||||
|
newBits[i / 32] |= 1 << (i & 0x1F);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newBits;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean bitSet(int[] bits, int i) {
|
||||||
|
return (bits[i / 32] & (1 << (i & 0x1F))) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean arraysAreEqual(int[] left, int[] right, int size) {
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
if (left[i] != right[i]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue