BitArray.java (zxing-zxing-3.4.1) | : | BitArray.java (zxing-zxing-3.5.0) | ||
---|---|---|---|---|
skipping to change at line 28 | skipping to change at line 28 | |||
import java.util.Arrays; | import java.util.Arrays; | |||
/** | /** | |||
* <p>A simple, fast array of bits, represented compactly by an array of ints in ternally.</p> | * <p>A simple, fast array of bits, represented compactly by an array of ints in ternally.</p> | |||
* | * | |||
* @author Sean Owen | * @author Sean Owen | |||
*/ | */ | |||
public final class BitArray implements Cloneable { | public final class BitArray implements Cloneable { | |||
private static final int[] EMPTY_BITS = {}; | ||||
private static final float LOAD_FACTOR = 0.75f; | ||||
private int[] bits; | private int[] bits; | |||
private int size; | private int size; | |||
public BitArray() { | public BitArray() { | |||
this.size = 0; | this.size = 0; | |||
this.bits = new int[1]; | this.bits = EMPTY_BITS; | |||
} | } | |||
public BitArray(int size) { | public BitArray(int size) { | |||
this.size = size; | this.size = size; | |||
this.bits = makeArray(size); | this.bits = makeArray(size); | |||
} | } | |||
// For testing only | // For testing only | |||
BitArray(int[] bits, int size) { | BitArray(int[] bits, int size) { | |||
this.bits = bits; | this.bits = bits; | |||
skipping to change at line 55 | skipping to change at line 58 | |||
} | } | |||
public int getSize() { | public int getSize() { | |||
return size; | return size; | |||
} | } | |||
public int getSizeInBytes() { | public int getSizeInBytes() { | |||
return (size + 7) / 8; | return (size + 7) / 8; | |||
} | } | |||
private void ensureCapacity(int size) { | private void ensureCapacity(int newSize) { | |||
if (size > bits.length * 32) { | if (newSize > bits.length * 32) { | |||
int[] newBits = makeArray(size); | int[] newBits = makeArray((int) Math.ceil(newSize / LOAD_FACTOR)); | |||
System.arraycopy(bits, 0, newBits, 0, bits.length); | System.arraycopy(bits, 0, newBits, 0, bits.length); | |||
this.bits = newBits; | this.bits = newBits; | |||
} | } | |||
} | } | |||
/** | /** | |||
* @param i bit to get | * @param i bit to get | |||
* @return true iff bit i is set | * @return true iff bit i is set | |||
*/ | */ | |||
public boolean get(int i) { | public boolean get(int i) { | |||
skipping to change at line 236 | skipping to change at line 239 | |||
* least-significant. For example, appending 6 bits from 0x000001E will append the bits | * least-significant. For example, appending 6 bits from 0x000001E will append the bits | |||
* 0, 1, 1, 1, 1, 0 in that order. | * 0, 1, 1, 1, 1, 0 in that order. | |||
* | * | |||
* @param value {@code int} containing bits to append | * @param value {@code int} containing bits to append | |||
* @param numBits bits from value to append | * @param numBits bits from value to append | |||
*/ | */ | |||
public void appendBits(int value, int numBits) { | public void appendBits(int value, int numBits) { | |||
if (numBits < 0 || numBits > 32) { | if (numBits < 0 || numBits > 32) { | |||
throw new IllegalArgumentException("Num bits must be between 0 and 32"); | throw new IllegalArgumentException("Num bits must be between 0 and 32"); | |||
} | } | |||
ensureCapacity(size + numBits); | int nextSize = size; | |||
for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) { | ensureCapacity(nextSize + numBits); | |||
appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1); | for (int numBitsLeft = numBits - 1; numBitsLeft >= 0; numBitsLeft--) { | |||
if ((value & (1 << numBitsLeft)) != 0) { | ||||
bits[nextSize / 32] |= 1 << (nextSize & 0x1F); | ||||
} | ||||
nextSize++; | ||||
} | } | |||
size = nextSize; | ||||
} | } | |||
public void appendBitArray(BitArray other) { | public void appendBitArray(BitArray other) { | |||
int otherSize = other.size; | int otherSize = other.size; | |||
ensureCapacity(size + otherSize); | ensureCapacity(size + otherSize); | |||
for (int i = 0; i < otherSize; i++) { | for (int i = 0; i < otherSize; i++) { | |||
appendBit(other.get(i)); | appendBit(other.get(i)); | |||
} | } | |||
} | } | |||
skipping to change at line 299 | skipping to change at line 307 | |||
/** | /** | |||
* Reverses all bits in the array. | * Reverses all bits in the array. | |||
*/ | */ | |||
public void reverse() { | public void reverse() { | |||
int[] newBits = new int[bits.length]; | int[] newBits = new int[bits.length]; | |||
// reverse all int's first | // reverse all int's first | |||
int len = (size - 1) / 32; | int len = (size - 1) / 32; | |||
int oldBitsLen = len + 1; | int oldBitsLen = len + 1; | |||
for (int i = 0; i < oldBitsLen; i++) { | for (int i = 0; i < oldBitsLen; i++) { | |||
long x = bits[i]; | newBits[len - i] = Integer.reverse(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 | // now correct the int's if the bit size isn't a multiple of 32 | |||
if (size != oldBitsLen * 32) { | if (size != oldBitsLen * 32) { | |||
int leftOffset = oldBitsLen * 32 - size; | int leftOffset = oldBitsLen * 32 - size; | |||
int currentInt = newBits[0] >>> leftOffset; | int currentInt = newBits[0] >>> leftOffset; | |||
for (int i = 1; i < oldBitsLen; i++) { | for (int i = 1; i < oldBitsLen; i++) { | |||
int nextInt = newBits[i]; | int nextInt = newBits[i]; | |||
currentInt |= nextInt << (32 - leftOffset); | currentInt |= nextInt << (32 - leftOffset); | |||
newBits[i - 1] = currentInt; | newBits[i - 1] = currentInt; | |||
currentInt = nextInt >>> leftOffset; | currentInt = nextInt >>> leftOffset; | |||
End of changes. 6 change blocks. | ||||
14 lines changed or deleted | 16 lines changed or added |