Ported over the BitVector bug fix and new unit test from Satoru.
[zxing.git] / core / src / com / google / zxing / qrcode / encoder / BitVector.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 /**
20  * JAVAPORT: This should be combined with BitArray in the future, although that class is not yet
21  * dynamically resizeable. This implementation is reasonable but there is a lot of function calling
22  * in loops I'd like to get rid of.
23  *
24  * @author satorux@google.com (Satoru Takabayashi) - creator
25  * @author dswitkin@google.com (Daniel Switkin) - ported from C++
26  */
27 public final class BitVector {
28
29   private int sizeInBits;
30   private byte[] array;
31
32   // For efficiency, start out with some room to work.
33   private static final int DEFAULT_SIZE_IN_BYTES = 32;
34
35   public BitVector() {
36     sizeInBits = 0;
37     array = new byte[DEFAULT_SIZE_IN_BYTES];
38   }
39
40   // Return the bit value at "index".
41   public int at(final int index) {
42     if (index < 0 || index >= sizeInBits) {
43       throw new IllegalArgumentException("Bad index: " + index);
44     }
45     final int value = array[index >> 3] & 0xff;
46     return (value >> (7 - (index & 0x7))) & 1;
47   }
48
49   // Return the number of bits in the bit vector.
50   public int size() {
51     return sizeInBits;
52   }
53
54   // Return the number of bytes in the bit vector.
55   public int num_bytes() {
56     return (sizeInBits + 7) >> 3;
57   }
58
59   // Append one bit to the bit vector.
60   public void AppendBit(final int bit) {
61     if (!(bit == 0 || bit == 1)) {
62       throw new IllegalArgumentException("Bad bit");
63     }
64     final int num_bits_in_last_byte = sizeInBits & 0x7;
65     // We'll expand array if we don't have bits in the last byte.
66     if (num_bits_in_last_byte == 0) {
67       appendByte(0);
68       sizeInBits -= 8;
69     }
70     // Modify the last byte.
71     array[sizeInBits >> 3] |= (bit << (7 - num_bits_in_last_byte));
72     ++sizeInBits;
73   }
74
75   // Append "num_bits" bits in "value" to the bit vector.
76   // REQUIRES: 0<= num_bits <= 32.
77   //
78   // Examples:
79   // - AppendBits(0x00, 1) adds 0.
80   // - AppendBits(0x00, 4) adds 0000.
81   // - AppendBits(0xff, 8) adds 11111111.
82   public void AppendBits(final int value, final int num_bits) {
83     if (num_bits < 0 || num_bits > 32) {
84       throw new IllegalArgumentException("Num bits must be between 0 and 32");
85     }
86     int num_bits_left = num_bits;
87     while (num_bits_left > 0) {
88       // Optimization for byte-oriented appending.
89       if ((sizeInBits & 0x7) == 0 && num_bits_left >= 8) {
90         final int newByte = (value >> (num_bits_left - 8)) & 0xff;
91         appendByte(newByte);
92         num_bits_left -= 8;
93       } else {
94         final int bit = (value >> (num_bits_left - 1)) & 1;
95         AppendBit(bit);
96         --num_bits_left;
97       }
98     }
99   }
100
101   // Append "bits".
102   public void AppendBitVector(final BitVector bits) {
103     int size = bits.size();
104     for (int i = 0; i < size; ++i) {
105       AppendBit(bits.at(i));
106     }
107   }
108
109   // Modify the bit vector by XOR'ing with "other"
110   public void XOR(final BitVector other) {
111     if (sizeInBits != other.size()) {
112       throw new IllegalArgumentException("BitVector sizes don't match");
113     }
114     int sizeInBytes = (sizeInBits + 7) >> 3;
115     for (int i = 0; i < sizeInBytes; ++i) {
116       // The last byte could be incomplete (i.e. not have 8 bits in
117       // it) but there is no problem since 0 XOR 0 == 0.
118       array[i] ^= other.array[i];
119     }
120   }
121
122   // Return String like "01110111" for debugging.
123   public String toString() {
124     StringBuffer result = new StringBuffer(sizeInBits);
125     for (int i = 0; i < sizeInBits; ++i) {
126       if (at(i) == 0) {
127         result.append("0");
128       } else if (at(i) == 1) {
129         result.append("1");
130       } else {
131         throw new IllegalArgumentException("Byte isn't 0 or 1");
132       }
133     }
134     return result.toString();
135   }
136
137   // Callers should not assume that array.length is the exact number of bytes needed to hold
138   // sizeInBits - it will typically be larger for efficiency.
139   public byte[] getArray() {
140     return array;
141   }
142
143   // Add a new byte to the end, possibly reallocating and doubling the size of the array if we've
144   // run out of room.
145   private void appendByte(int value) {
146     if ((sizeInBits >> 3) == array.length) {
147       byte[] newArray = new byte[array.length * 2];
148       System.arraycopy(array, 0, newArray, 0, array.length);
149       array = newArray;
150     }
151     array[sizeInBits >> 3] = (byte) value;
152     sizeInBits += 8;
153   }
154
155 }