From 5dba28fc86fb7b0bbc5c60fd618df7b16b9cdb6e Mon Sep 17 00:00:00 2001 From: dswitkin Date: Thu, 13 Nov 2008 21:59:08 +0000 Subject: [PATCH] Wrote a reasonable implementation of BitVector which now compiles. git-svn-id: https://zxing.googlecode.com/svn/trunk@698 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../zxing/qrcode/encoder/BitVector.java | 97 +++++++++++-------- 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/core/src/com/google/zxing/qrcode/encoder/BitVector.java b/core/src/com/google/zxing/qrcode/encoder/BitVector.java index a0885134d..fa4011516 100644 --- a/core/src/com/google/zxing/qrcode/encoder/BitVector.java +++ b/core/src/com/google/zxing/qrcode/encoder/BitVector.java @@ -17,47 +17,57 @@ package com.google.zxing.qrcode.encoder; /** + * JAVAPORT: This should be combined with BitArray in the future, although that class is not yet + * dynamically resizeable. This implementation is reasonable but there is a lot of function calling + * in loops I'd like to get rid of. + * * @author satorux@google.com (Satoru Takabayashi) - creator * @author dswitkin@google.com (Daniel Switkin) - ported from C++ */ public final class BitVector { - private int size_; - private String bytes_; + private int sizeInBits; + private int bytePosition; + private byte[] array; + + // For efficiency, start out with some room to work. + private static final int DEFAULT_SIZE_IN_BITS = 32 * 8; public BitVector() { - size_ = 0; + sizeInBits = DEFAULT_SIZE_IN_BITS; + bytePosition = 0; + array = new byte[DEFAULT_SIZE_IN_BITS / 8]; } // Return the bit value at "index". public int at(final int index) { Debug.DCHECK_LE(0, index); - Debug.DCHECK_LT(index, size_); - final uint8 byte = bytes_.at(index / 8); - return (byte >> (7 - (index % 8))) & 1; + Debug.DCHECK_LT(index, sizeInBits); + final int value = array[index / 8]; + return (value >> (7 - (index % 8))) & 1; } // Return the number of bits in the bit vector. public int size() { - return size_; + return sizeInBits; } // Return the number of bytes in the bit vector. public int num_bytes() { - return size_ / 8; + return sizeInBits / 8; } // Append one bit to the bit vector. public void AppendBit(final int bit) { Debug.DCHECK(bit == 0 || bit == 1); - final int num_bits_in_last_byte = size_ % 8; - // We'll expand bytes_ if we don't have bits in the last byte. + final int num_bits_in_last_byte = sizeInBits % 8; + // We'll expand array if we don't have bits in the last byte. if (num_bits_in_last_byte == 0) { - bytes_.push_back(0); + appendByte(0); } // Modify the last byte. - bytes_[bytes_.size() - 1] |= (bit << (7 - num_bits_in_last_byte)); - ++size_; + array[array.length - 1] |= (bit << (7 - num_bits_in_last_byte)); + ++sizeInBits; } // Append "num_bits" bits in "value" to the bit vector. @@ -67,15 +77,15 @@ public final class BitVector { // - AppendBits(0x00, 1) adds 0. // - AppendBits(0x00, 4) adds 0000. // - AppendBits(0xff, 8) adds 11111111. - public void AppendBits(final uint32 value, final int num_bits) { + public void AppendBits(final int value, final int num_bits) { Debug.DCHECK(num_bits >= 0 && num_bits <= 32); int num_bits_left = num_bits; while (num_bits_left > 0) { // Optimization for byte-oriented appending. - if (size_ % 8 == 0 && num_bits_left >= 8) { - final uint8 byte = (value >> (num_bits_left - 8)) & 0xff; - bytes_.push_back(byte); - size_ += 8; + if (sizeInBits % 8 == 0 && num_bits_left >= 8) { + final int newByte = (value >> (num_bits_left - 8)) & 0xff; + appendByte(newByte); + sizeInBits += 8; num_bits_left -= 8; } else { final int bit = (value >> (num_bits_left - 1)) & 1; @@ -86,39 +96,36 @@ public final class BitVector { } // Append "bytes". - public void AppendBytes(final StringPiece &bytes) { - for (int i = 0; i < bytes.size(); ++i) { - AppendBits(bytes[i], 8); - } - } + // + // JAVAPORT: Uncomment and implement when a substitute for StringPiece is chosen. +// public void AppendBytes(final StringPiece stringPiece) { +// for (int i = 0; i < stringPiece.size(); ++i) { +// AppendBits(stringPiece[i], 8); +// } +// } // Append "bits". - public void AppendBitVector(final BitVector &bits) { - for (int i = 0; i < bits.size(); ++i) { + public void AppendBitVector(final BitVector bits) { + int size = bits.size(); + for (int i = 0; i < size; ++i) { AppendBit(bits.at(i)); } } // Modify the bit vector by XOR'ing with "other" - public void XOR(final BitVector &other) { - Debug.DCHECK_EQ(size_, other.size()); - for (int i = 0; i < bytes_.size(); ++i) { + public void XOR(final BitVector other) { + Debug.DCHECK_EQ(sizeInBits, other.size()); + for (int i = 0; i < array.length; ++i) { // The last byte could be incomplete (i.e. not have 8 bits in // it) but there is no problem since 0 XOR 0 == 0. - bytes_[i] ^= other.ToString()[i]; + array[i] ^= other.array[i]; } } - // Return the content of the bit vector as String. - public final String &ToString() { - return bytes_; - } - // Return String like "01110111" for debugging. - public String ToASCII() { - String result; - result.reserve(size_); - for (int i = 0; i < size_; ++i) { + public String toString() { + StringBuffer result = new StringBuffer(sizeInBits); + for (int i = 0; i < sizeInBits; ++i) { if (at(i) == 0) { result.append("0"); } else if (at(i) == 1) { @@ -127,7 +134,19 @@ public final class BitVector { Debug.DCHECK(false); } } - return result; + return result.toString(); + } + + // Add a new byte to the end, possibly reallocating and doubling the size of the array if we've + // run out of room. + private void appendByte(int value) { + if (bytePosition >= array.length) { + byte[] newArray = new byte[array.length * 2]; + System.arraycopy(array, 0, newArray, 0, array.length); + array = newArray; + } + array[bytePosition] = (byte) value; + bytePosition++; } }