+++ /dev/null
-/*\r
-* Copyright 2007 ZXing authors\r
-*\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
-using System;\r
-using System.Text;\r
-using com.google.zxing;\r
-using com.google.zxing.common;\r
-\r
-namespace com.google.zxing.qrcode.encoder\r
-{\r
- public sealed class BitVector { \r
- \r
- private int sizeInBits;\r
- private sbyte[] array;\r
-\r
- // For efficiency, start out with some room to work.\r
- private static int DEFAULT_SIZE_IN_BYTES = 32;\r
-\r
- public BitVector() {\r
- sizeInBits = 0;\r
- array = new sbyte[DEFAULT_SIZE_IN_BYTES];\r
- }\r
-\r
- // Return the bit value at "index".\r
- public int at(int index) {\r
- if (index < 0 || index >= sizeInBits) {\r
- throw new ArgumentException("Bad index: " + index);\r
- }\r
- int value = array[index >> 3] & 0xff;\r
- return (value >> (7 - (index & 0x7))) & 1;\r
- }\r
-\r
- // Return the number of bits in the bit vector.\r
- public int size() {\r
- return sizeInBits;\r
- }\r
-\r
- // Return the number of bytes in the bit vector.\r
- public int sizeInBytes() {\r
- return (sizeInBits + 7) >> 3;\r
- }\r
-\r
- // Append one bit to the bit vector.\r
- public void appendBit(int bit) {\r
- if (!(bit == 0 || bit == 1)) {\r
- throw new ArgumentException("Bad bit");\r
- }\r
- int numBitsInLastByte = sizeInBits & 0x7;\r
- // We'll expand array if we don't have bits in the last byte.\r
- if (numBitsInLastByte == 0) {\r
- appendByte(0);\r
- sizeInBits -= 8;\r
- }\r
- // Modify the last byte.\r
- array[sizeInBits >> 3] |= (sbyte)(bit << (7 - numBitsInLastByte));\r
- ++sizeInBits;\r
- }\r
-\r
- // Append "numBits" bits in "value" to the bit vector.\r
- // REQUIRES: 0<= numBits <= 32.\r
- //\r
- // Examples:\r
- // - appendBits(0x00, 1) adds 0.\r
- // - appendBits(0x00, 4) adds 0000.\r
- // - appendBits(0xff, 8) adds 11111111.\r
- public void appendBits(int value, int numBits) {\r
- if (numBits < 0 || numBits > 32) {\r
- throw new ArgumentException("Num bits must be between 0 and 32");\r
- }\r
- int numBitsLeft = numBits;\r
- while (numBitsLeft > 0) {\r
- // Optimization for byte-oriented appending.\r
- if ((sizeInBits & 0x7) == 0 && numBitsLeft >= 8) {\r
- int newByte = (value >> (numBitsLeft - 8)) & 0xff;\r
- appendByte(newByte);\r
- numBitsLeft -= 8;\r
- } else {\r
- int bit = (value >> (numBitsLeft - 1)) & 1;\r
- appendBit(bit);\r
- --numBitsLeft;\r
- }\r
- }\r
- }\r
-\r
- // Append "bits".\r
- public void appendBitVector(BitVector bits) {\r
- int size = bits.size();\r
- for (int i = 0; i < size; ++i) {\r
- appendBit(bits.at(i));\r
- }\r
- }\r
-\r
- // Modify the bit vector by XOR'ing with "other"\r
- public void xor(BitVector other) {\r
- if (sizeInBits != other.size()) {\r
- throw new ArgumentException("BitVector sizes don't match");\r
- }\r
- int sizeInBytes = (sizeInBits + 7) >> 3;\r
- for (int i = 0; i < sizeInBytes; ++i) {\r
- // The last byte could be incomplete (i.e. not have 8 bits in\r
- // it) but there is no problem since 0 XOR 0 == 0.\r
- array[i] ^= other.array[i];\r
- }\r
- }\r
-\r
- // Return String like "01110111" for debugging.\r
- public String toString() {\r
- StringBuilder result = new StringBuilder(sizeInBits);\r
- for (int i = 0; i < sizeInBits; ++i) {\r
- if (at(i) == 0) {\r
- result.Append('0');\r
- } else if (at(i) == 1) {\r
- result.Append('1');\r
- } else {\r
- throw new ArgumentException("Byte isn't 0 or 1");\r
- }\r
- }\r
- return result.ToString();\r
- }\r
-\r
- // Callers should not assume that array.length is the exact number of bytes needed to hold\r
- // sizeInBits - it will typically be larger for efficiency.\r
- public sbyte[] getArray() {\r
- return array;\r
- }\r
-\r
- // Add a new byte to the end, possibly reallocating and doubling the size of the array if we've\r
- // run out of room.\r
- private void appendByte(int value) {\r
- if ((sizeInBits >> 3) == array.Length) {\r
- sbyte[] newArray = new sbyte[(array.Length << 1)];\r
- System.Array.Copy (array, 0, newArray, 0, array.Length);\r
- array = newArray;\r
- }\r
- array[sizeInBits >> 3] = (sbyte) value;\r
- sizeInBits += 8;\r
- }\r
- }\r
-}
\ No newline at end of file