Made Java naming changes to BitVector.

git-svn-id: https://zxing.googlecode.com/svn/trunk@757 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
dswitkin 2008-11-21 17:06:52 +00:00
parent afdc4508b7
commit 98b34ae187
6 changed files with 84 additions and 84 deletions

View file

@ -52,12 +52,12 @@ public final class BitVector {
}
// Return the number of bytes in the bit vector.
public int num_bytes() {
public int sizeInBytes() {
return (sizeInBits + 7) >> 3;
}
// Append one bit to the bit vector.
public void AppendBit(final int bit) {
public void appendBit(final int bit) {
if (!(bit == 0 || bit == 1)) {
throw new IllegalArgumentException("Bad bit");
}
@ -76,10 +76,10 @@ public final class BitVector {
// REQUIRES: 0<= num_bits <= 32.
//
// Examples:
// - AppendBits(0x00, 1) adds 0.
// - AppendBits(0x00, 4) adds 0000.
// - AppendBits(0xff, 8) adds 11111111.
public void AppendBits(final int value, final int num_bits) {
// - appendBits(0x00, 1) adds 0.
// - appendBits(0x00, 4) adds 0000.
// - appendBits(0xff, 8) adds 11111111.
public void appendBits(final int value, final int num_bits) {
if (num_bits < 0 || num_bits > 32) {
throw new IllegalArgumentException("Num bits must be between 0 and 32");
}
@ -92,22 +92,22 @@ public final class BitVector {
num_bits_left -= 8;
} else {
final int bit = (value >> (num_bits_left - 1)) & 1;
AppendBit(bit);
appendBit(bit);
--num_bits_left;
}
}
}
// Append "bits".
public void AppendBitVector(final BitVector bits) {
public void appendBitVector(final BitVector bits) {
int size = bits.size();
for (int i = 0; i < size; ++i) {
AppendBit(bits.at(i));
appendBit(bits.at(i));
}
}
// Modify the bit vector by XOR'ing with "other"
public void XOR(final BitVector other) {
public void xor(final BitVector other) {
if (sizeInBits != other.size()) {
throw new IllegalArgumentException("BitVector sizes don't match");
}

View file

@ -134,14 +134,14 @@ public final class Encoder {
BitVector data_bits = new BitVector();
AppendBytes(bytes, mode, data_bits);
// Step 3: Initialize QR code that can contain "data_bits".
final int num_input_bytes = data_bits.num_bytes();
final int num_input_bytes = data_bits.sizeInBytes();
InitQRCode(num_input_bytes, ec_level, mode, qr_code);
// Step 4: Build another bit vector that contains header and data.
BitVector header_and_data_bits = new BitVector();
AppendModeInfo(qr_code.mode(), header_and_data_bits);
AppendLengthInfo(bytes.size(), qr_code.version(), qr_code.mode(), header_and_data_bits);
header_and_data_bits.AppendBitVector(data_bits);
header_and_data_bits.appendBitVector(data_bits);
// Step 5: Terminate the bits properly.
TerminateBits(qr_code.num_data_bytes(), header_and_data_bits);
@ -280,14 +280,14 @@ public final class Encoder {
}
// Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details.
for (int i = 0; i < 4 && bits.size() < capacity; ++i) {
bits.AppendBit(0);
bits.appendBit(0);
}
final int num_bits_in_last_byte = bits.size() % 8;
// If the last byte isn't 8-bit aligned, we'll add padding bits.
if (num_bits_in_last_byte > 0) {
final int num_padding_bits = 8 - num_bits_in_last_byte;
for (int i = 0; i < num_padding_bits; ++i) {
bits.AppendBit(0);
bits.appendBit(0);
}
}
// Should be 8-bit aligned here.
@ -295,12 +295,12 @@ public final class Encoder {
throw new WriterException("Number of bits is not a multiple of 8");
}
// If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24).
final int num_padding_bytes = num_data_bytes - bits.num_bytes();
final int num_padding_bytes = num_data_bytes - bits.sizeInBytes();
for (int i = 0; i < num_padding_bytes; ++i) {
if (i % 2 == 0) {
bits.AppendBits(0xec, 8);
bits.appendBits(0xec, 8);
} else {
bits.AppendBits(0x11, 8);
bits.appendBits(0x11, 8);
}
}
if (bits.size() != capacity) {
@ -369,7 +369,7 @@ public final class Encoder {
int num_data_bytes, int num_rs_blocks, BitVector result) throws WriterException {
// "bits" must have "num_data_bytes" bytes of data.
if (bits.num_bytes() != num_data_bytes) {
if (bits.sizeInBytes() != num_data_bytes) {
throw new WriterException("Number of bits and data bytes does not match");
}
@ -407,7 +407,7 @@ public final class Encoder {
for (int j = 0; j < blocks.size(); ++j) {
final ByteArray data_bytes = ((BlockPair) blocks.elementAt(j)).getDataBytes();
if (i < data_bytes.size()) {
result.AppendBits(data_bytes.at(i), 8);
result.appendBits(data_bytes.at(i), 8);
}
}
}
@ -416,12 +416,12 @@ public final class Encoder {
for (int j = 0; j < blocks.size(); ++j) {
final ByteArray ec_bytes = ((BlockPair) blocks.elementAt(j)).getErrorCorrectionBytes();
if (i < ec_bytes.size()) {
result.AppendBits(ec_bytes.at(i), 8);
result.appendBits(ec_bytes.at(i), 8);
}
}
}
if (num_total_bytes != result.num_bytes()) { // Should be same.
throw new WriterException("Interleaving error: " + num_total_bytes + " and " + result.num_bytes() +
if (num_total_bytes != result.sizeInBytes()) { // Should be same.
throw new WriterException("Interleaving error: " + num_total_bytes + " and " + result.sizeInBytes() +
" differ.");
}
}
@ -445,7 +445,7 @@ public final class Encoder {
// false.
static void AppendModeInfo(int mode, BitVector bits) throws WriterException {
final int code = QRCode.GetModeCode(mode);
bits.AppendBits(code, 4);
bits.appendBits(code, 4);
}
@ -465,7 +465,7 @@ public final class Encoder {
if (num_letters > ((1 << num_bits) - 1)) {
throw new WriterException(num_letters + "is bigger than" + ((1 << num_bits) - 1));
}
bits.AppendBits(num_letters, num_bits);
bits.appendBits(num_letters, num_bits);
}
// Append "bytes" in "mode" mode (encoding) into "bits". On success, store the result in "bits"
@ -505,16 +505,16 @@ public final class Encoder {
// Encode three numeric letters in ten bits.
final int num2 = bytes.at(i + 1) - '0';
final int num3 = bytes.at(i + 2) - '0';
bits.AppendBits(num1 * 100 + num2 * 10 + num3, 10);
bits.appendBits(num1 * 100 + num2 * 10 + num3, 10);
i += 3;
} else if (i + 1 < bytes.size()) {
// Encode two numeric letters in seven bits.
final int num2 = bytes.at(i + 1) - '0';
bits.AppendBits(num1 * 10 + num2, 7);
bits.appendBits(num1 * 10 + num2, 7);
i += 2;
} else {
// Encode one numeric letter in four bits.
bits.AppendBits(num1, 4);
bits.appendBits(num1, 4);
++i;
}
}
@ -534,11 +534,11 @@ public final class Encoder {
throw new WriterException();
}
// Encode two alphanumeric letters in 11 bits.
bits.AppendBits(code1 * 45 + code2, 11);
bits.appendBits(code1 * 45 + code2, 11);
i += 2;
} else {
// Encode one alphanumeric letter in six bits.
bits.AppendBits(code1, 6);
bits.appendBits(code1, 6);
++i;
}
}
@ -548,7 +548,7 @@ public final class Encoder {
// "bits" and return true. On error, return false.
static void Append8BitBytes(final ByteArray bytes, BitVector bits) {
for (int i = 0; i < bytes.size(); ++i) {
bits.AppendBits(bytes.at(i), 8);
bits.appendBits(bytes.at(i), 8);
}
}
@ -574,7 +574,7 @@ public final class Encoder {
throw new WriterException("Invalid byte sequence: " + bytes);
}
final int encoded = ((subtracted >> 8) * 0xc0) + (subtracted & 0xff);
bits.AppendBits(encoded, 13);
bits.appendBits(encoded, 13);
}
}

View file

@ -326,14 +326,14 @@ public final class MatrixUtil {
throw new WriterException("Invalid mask pattern");
}
final int type_info = (ec_code << 3) | mask_pattern;
bits.AppendBits(type_info, 5);
bits.appendBits(type_info, 5);
final int bch_code = MatrixUtil.CalculateBCHCode(type_info, kTypeInfoPoly);
bits.AppendBits(bch_code, 10);
bits.appendBits(bch_code, 10);
BitVector mask_bits = new BitVector();
mask_bits.AppendBits(kTypeInfoMaskPattern, 15);
bits.XOR(mask_bits);
mask_bits.appendBits(kTypeInfoMaskPattern, 15);
bits.xor(mask_bits);
if (bits.size() != 15) { // Just in case.
throw new WriterException("should not happen but we got: " + bits.size());
@ -343,9 +343,9 @@ public final class MatrixUtil {
// Make bit vector of version information. On success, store the result in "bits" and return true.
// On error, return false. See 8.10 of JISX0510:2004 (p.45) for details.
public static void MakeVersionInfoBits(int version, BitVector bits) throws WriterException {
bits.AppendBits(version, 6);
bits.appendBits(version, 6);
final int bch_code = MatrixUtil.CalculateBCHCode(version, kVersionInfoPoly);
bits.AppendBits(bch_code, 12);
bits.appendBits(bch_code, 12);
if (bits.size() != 18) { // Just in case.
throw new WriterException("should not happen but we got: " + bits.size());

View file

@ -30,46 +30,46 @@ public class BitVectorTestCase extends TestCase {
public void testAppendBit() {
BitVector v = new BitVector();
assertEquals(0, v.num_bytes());
assertEquals(0, v.sizeInBytes());
// 1
v.AppendBit(1);
v.appendBit(1);
assertEquals(1, v.size());
assertEquals(0x80, getUnsignedByte(v, 0));
// 10
v.AppendBit(0);
v.appendBit(0);
assertEquals(2, v.size());
assertEquals(0x80, getUnsignedByte(v, 0));
// 101
v.AppendBit(1);
v.appendBit(1);
assertEquals(3, v.size());
assertEquals(0xa0, getUnsignedByte(v, 0));
// 1010
v.AppendBit(0);
v.appendBit(0);
assertEquals(4, v.size());
assertEquals(0xa0, getUnsignedByte(v, 0));
// 10101
v.AppendBit(1);
v.appendBit(1);
assertEquals(5, v.size());
assertEquals(0xa8, getUnsignedByte(v, 0));
// 101010
v.AppendBit(0);
v.appendBit(0);
assertEquals(6, v.size());
assertEquals(0xa8, getUnsignedByte(v, 0));
// 1010101
v.AppendBit(1);
v.appendBit(1);
assertEquals(7, v.size());
assertEquals(0xaa, getUnsignedByte(v, 0));
// 10101010
v.AppendBit(0);
v.appendBit(0);
assertEquals(8, v.size());
assertEquals(0xaa, getUnsignedByte(v, 0));
// 10101010 1
v.AppendBit(1);
v.appendBit(1);
assertEquals(9, v.size());
assertEquals(0xaa, getUnsignedByte(v, 0));
assertEquals(0x80, getUnsignedByte(v, 1));
// 10101010 10
v.AppendBit(0);
v.appendBit(0);
assertEquals(10, v.size());
assertEquals(0xaa, getUnsignedByte(v, 0));
assertEquals(0x80, getUnsignedByte(v, 1));
@ -78,19 +78,19 @@ public class BitVectorTestCase extends TestCase {
public void testAppendBits() {
{
BitVector v = new BitVector();
v.AppendBits(0x1, 1);
v.appendBits(0x1, 1);
assertEquals(1, v.size());
assertEquals(0x80, getUnsignedByte(v, 0));
}
{
BitVector v = new BitVector();
v.AppendBits(0xff, 8);
v.appendBits(0xff, 8);
assertEquals(8, v.size());
assertEquals(0xff, getUnsignedByte(v, 0));
}
{
BitVector v = new BitVector();
v.AppendBits(0xff7, 12);
v.appendBits(0xff7, 12);
assertEquals(12, v.size());
assertEquals(0xff, getUnsignedByte(v, 0));
assertEquals(0x70, getUnsignedByte(v, 1));
@ -99,25 +99,25 @@ public class BitVectorTestCase extends TestCase {
public void testNumBytes() {
BitVector v = new BitVector();
assertEquals(0, v.num_bytes());
v.AppendBit(0);
assertEquals(0, v.sizeInBytes());
v.appendBit(0);
// 1 bit was added in the vector, so 1 byte should be consumed.
assertEquals(1, v.num_bytes());
v.AppendBits(0, 7);
assertEquals(1, v.num_bytes());
v.AppendBits(0, 8);
assertEquals(2, v.num_bytes());
v.AppendBits(0, 1);
assertEquals(1, v.sizeInBytes());
v.appendBits(0, 7);
assertEquals(1, v.sizeInBytes());
v.appendBits(0, 8);
assertEquals(2, v.sizeInBytes());
v.appendBits(0, 1);
// We now have 17 bits, so 3 bytes should be consumed.
assertEquals(3, v.num_bytes());
assertEquals(3, v.sizeInBytes());
}
public void testAppendBitVector() {
BitVector v1 = new BitVector();
v1.AppendBits(0xbe, 8);
v1.appendBits(0xbe, 8);
BitVector v2 = new BitVector();
v2.AppendBits(0xef, 8);
v1.AppendBitVector(v2);
v2.appendBits(0xef, 8);
v1.appendBitVector(v2);
// beef = 1011 1110 1110 1111
assertEquals("1011111011101111", v1.toString());
}
@ -125,10 +125,10 @@ public class BitVectorTestCase extends TestCase {
public void testXOR() {
{
BitVector v1 = new BitVector();
v1.AppendBits(0x5555aaaa, 32);
v1.appendBits(0x5555aaaa, 32);
BitVector v2 = new BitVector();
v2.AppendBits(0xaaaa5555, 32);
v1.XOR(v2);
v2.appendBits(0xaaaa5555, 32);
v1.xor(v2);
assertEquals(0xff, getUnsignedByte(v1, 0));
assertEquals(0xff, getUnsignedByte(v1, 1));
assertEquals(0xff, getUnsignedByte(v1, 2));
@ -136,17 +136,17 @@ public class BitVectorTestCase extends TestCase {
}
{
BitVector v1 = new BitVector();
v1.AppendBits(0x2a, 7); // 010 1010
v1.appendBits(0x2a, 7); // 010 1010
BitVector v2 = new BitVector();
v2.AppendBits(0x55, 7); // 101 0101
v1.XOR(v2);
v2.appendBits(0x55, 7); // 101 0101
v1.xor(v2);
assertEquals(0xfe, getUnsignedByte(v1, 0)); // 1111 1110
}
}
public void testAt() {
BitVector v = new BitVector();
v.AppendBits(0xdead, 16); // 1101 1110 1010 1101
v.appendBits(0xdead, 16); // 1101 1110 1010 1101
assertEquals(1, v.at(0));
assertEquals(1, v.at(1));
assertEquals(0, v.at(2));
@ -170,7 +170,7 @@ public class BitVectorTestCase extends TestCase {
public void testToString() {
BitVector v = new BitVector();
v.AppendBits(0xdead, 16); // 1101 1110 1010 1101
v.appendBits(0xdead, 16); // 1101 1110 1010 1101
assertEquals("1101111010101101", v.toString());
}

View file

@ -237,19 +237,19 @@ public final class EncoderTestCase extends TestCase {
}
{
BitVector v = new BitVector();
v.AppendBits(0, 3); // Append 000
v.appendBits(0, 3); // Append 000
Encoder.TerminateBits(1, v);
assertEquals("00000000", v.toString());
}
{
BitVector v = new BitVector();
v.AppendBits(0, 5); // Append 00000
v.appendBits(0, 5); // Append 00000
Encoder.TerminateBits(1, v);
assertEquals("00000000", v.toString());
}
{
BitVector v = new BitVector();
v.AppendBits(0, 8); // Append 00000000
v.appendBits(0, 8); // Append 00000000
Encoder.TerminateBits(1, v);
assertEquals("00000000", v.toString());
}
@ -260,7 +260,7 @@ public final class EncoderTestCase extends TestCase {
}
{
BitVector v = new BitVector();
v.AppendBits(0, 1); // Append 0
v.appendBits(0, 1); // Append 0
Encoder.TerminateBits(3, v);
assertEquals("000000001110110000010001", v.toString());
}
@ -307,7 +307,7 @@ public final class EncoderTestCase extends TestCase {
final byte[] data_bytes = {32, 65, (byte)205, 69, 41, (byte)220, 46, (byte)128, (byte)236};
BitVector in = new BitVector();
for (byte data_byte: data_bytes) {
in.AppendBits(data_byte, 8);
in.appendBits(data_byte, 8);
}
BitVector out = new BitVector();
Encoder.InterleaveWithECBytes(in, 26, 9, 1, out);
@ -318,9 +318,9 @@ public final class EncoderTestCase extends TestCase {
42, (byte)159, 74, (byte)221, (byte)244, (byte)169, (byte)239, (byte)150, (byte)138, 70,
(byte)237, 85, (byte)224, 96, 74, (byte)219, 61,
};
assertEquals(expected.length, out.num_bytes());
assertEquals(expected.length, out.sizeInBytes());
final byte[] out_array = out.getArray();
// Can't use Arrays.equals(), because out_array may be longer than out.num_bytes()
// Can't use Arrays.equals(), because out_array may be longer than out.sizeInBytes()
for (int x = 0; x < expected.length; x++) {
assertEquals(expected[x], out_array[x]);
}
@ -337,7 +337,7 @@ public final class EncoderTestCase extends TestCase {
};
BitVector in = new BitVector();
for (byte data_byte: data_bytes) {
in.AppendBits(data_byte, 8);
in.appendBits(data_byte, 8);
}
BitVector out = new BitVector();
Encoder.InterleaveWithECBytes(in, 134, 62, 4, out);
@ -358,7 +358,7 @@ public final class EncoderTestCase extends TestCase {
(byte)140, 61, (byte)179, (byte)154, (byte)214, (byte)138, (byte)147, 87, 27, 96, 77, 47,
(byte)187, 49, (byte)156, (byte)214,
};
assertEquals(expected.length, out.num_bytes());
assertEquals(expected.length, out.sizeInBytes());
final byte[] out_array = out.getArray();
for (int x = 0; x < expected.length; x++) {
assertEquals(expected[x], out_array[x]);
@ -609,10 +609,10 @@ public final class EncoderTestCase extends TestCase {
}
public void testBugInBitVectorNumBytes() throws WriterException {
// There was a bug in BitVector::num_bytes() that caused it to return a
// There was a bug in BitVector::sizeInBytes() that caused it to return a
// smaller-by-one value (ex. 1465 instead of 1466) if the number of bits
// in the vector is not 8-bit aligned. In QRCodeEncoder::InitQRCode(),
// BitVector::num_bytes() is used for finding the smallest QR Code
// BitVector::sizeInBytes() is used for finding the smallest QR Code
// version that can fit the given data. Hence there were corner cases
// where we chose a wrong QR Code version that cannot fit the given
// data. Note that the issue did not occur with MODE_8BIT_BYTE, as the
@ -631,7 +631,7 @@ public final class EncoderTestCase extends TestCase {
// - 1828 - 360 = 1468
// - In InitQRCode(), 3 bytes are reserved for a header. Hence 1465 bytes
// (1468 -3) are left for data.
// - Because of the bug in BitVector::num_bytes(), InitQRCode() determines
// - Because of the bug in BitVector::sizeInBytes(), InitQRCode() determines
// the given data can fit in 1465 bytes, despite it needs 1466 bytes.
// - Hence QRCodeEncoder::Encode() failed and returned false.
// - To be precise, it needs 11727 + 4 (mode info) + 14 (length info) =

View file

@ -238,7 +238,7 @@ public final class MatrixUtilTestCase extends TestCase {
70, 237, 85, 224, 96, 74, 219 , 61};
BitVector bits = new BitVector();
for (char c: bytes) {
bits.AppendBits(c, 8);
bits.appendBits(c, 8);
}
ByteMatrix matrix = new ByteMatrix(21, 21);
MatrixUtil.BuildMatrix(bits,