/*\r
- * Copyright 2007 Google Inc.\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
/**\r
* <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>\r
*\r
- * @author srowen@google.com (Sean Owen)\r
+ * @author Sean Owen\r
*/\r
public final class BitArray {\r
\r
- private final int[] bits;\r
+ // TODO: I have changed these members to be public so ProGuard can inline get() and set(). Ideally\r
+ // they'd be private and we'd use the -allowaccessmodification flag, but Dalvik rejects the\r
+ // resulting binary at runtime on Android. If we find a solution to this, these should be changed\r
+ // back to private.\r
+ public int[] bits;\r
+ public int size;\r
+\r
+ public BitArray() {\r
+ this.size = 0;\r
+ this.bits = new int[1];\r
+ }\r
\r
public BitArray(int size) {\r
- int arraySize = size >> 5;\r
- if ((size & 0x1F) != 0) {\r
- arraySize++;\r
+ this.size = size;\r
+ this.bits = makeArray(size);\r
+ }\r
+\r
+ public int getSize() {\r
+ return size;\r
+ }\r
+\r
+ public int getSizeInBytes() {\r
+ return (size + 7) >> 3;\r
+ }\r
+\r
+ private void ensureCapacity(int size) {\r
+ if (size > bits.length << 5) {\r
+ int[] newBits = makeArray(size);\r
+ System.arraycopy(bits, 0, newBits, 0, bits.length);\r
+ this.bits = newBits;\r
}\r
- bits = new int[arraySize];\r
}\r
\r
/**\r
bits[i >> 5] |= 1 << (i & 0x1F);\r
}\r
\r
+ /**\r
+ * Flips bit i.\r
+ *\r
+ * @param i bit to set\r
+ */\r
+ public void flip(int i) {\r
+ bits[i >> 5] ^= 1 << (i & 0x1F);\r
+ }\r
+\r
/**\r
* Sets a block of 32 bits, starting at bit i.\r
*\r
* @param i first bit to set\r
* @param newBits the new value of the next 32 bits. Note again that the least-significant bit\r
- * correponds to bit i, the next-least-significant to i+1, and so on.\r
+ * corresponds to bit i, the next-least-significant to i+1, and so on.\r
*/\r
public void setBulk(int i, int newBits) {\r
bits[i >> 5] = newBits;\r
}\r
}\r
\r
+ /**\r
+ * Efficient method to check if a range of bits is set, or not set.\r
+ *\r
+ * @param start start of range, inclusive.\r
+ * @param end end of range, exclusive\r
+ * @param value if true, checks that bits in range are set, otherwise checks that they are not set\r
+ * @return true iff all bits are set or not set in range, according to value argument\r
+ * @throws IllegalArgumentException if end is less than or equal to start\r
+ */\r
+ public boolean isRange(int start, int end, boolean value) {\r
+ if (end < start) {\r
+ throw new IllegalArgumentException();\r
+ }\r
+ if (end == start) {\r
+ return true; // empty range matches\r
+ }\r
+ end--; // will be easier to treat this as the last actually set bit -- inclusive \r
+ int firstInt = start >> 5;\r
+ int lastInt = end >> 5;\r
+ for (int i = firstInt; i <= lastInt; i++) {\r
+ int firstBit = i > firstInt ? 0 : start & 0x1F;\r
+ int lastBit = i < lastInt ? 31 : end & 0x1F;\r
+ int mask;\r
+ if (firstBit == 0 && lastBit == 31) {\r
+ mask = -1;\r
+ } else {\r
+ mask = 0;\r
+ for (int j = firstBit; j <= lastBit; j++) {\r
+ mask |= 1 << j;\r
+ }\r
+ }\r
+\r
+ // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,\r
+ // equals the mask, or we're looking for 0s and the masked portion is not all 0s\r
+ if ((bits[i] & mask) != (value ? mask : 0)) {\r
+ return false;\r
+ }\r
+ }\r
+ return true;\r
+ }\r
+\r
+ public void appendBit(boolean bit) {\r
+ ensureCapacity(size + 1);\r
+ if (bit) {\r
+ bits[size >> 5] |= (1 << (size & 0x1F));\r
+ }\r
+ size++;\r
+ }\r
+\r
+ /**\r
+ * Appends the least-significant bits, from value, in order from most-significant to\r
+ * least-significant. For example, appending 6 bits from 0x000001E will append the bits\r
+ * 0, 1, 1, 1, 1, 0 in that order.\r
+ */\r
+ public void appendBits(int value, int numBits) {\r
+ if (numBits < 0 || numBits > 32) {\r
+ throw new IllegalArgumentException("Num bits must be between 0 and 32");\r
+ }\r
+ ensureCapacity(size + numBits);\r
+ for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--) {\r
+ appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1);\r
+ }\r
+ }\r
+\r
+ public void appendBitArray(BitArray other) {\r
+ int otherSize = other.getSize();\r
+ ensureCapacity(size + otherSize);\r
+ for (int i = 0; i < otherSize; i++) {\r
+ appendBit(other.get(i));\r
+ }\r
+ }\r
+\r
+ public void xor(BitArray other) {\r
+ if (bits.length != other.bits.length) {\r
+ throw new IllegalArgumentException("Sizes don't match");\r
+ }\r
+ for (int i = 0; i < bits.length; 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
+ bits[i] ^= other.bits[i];\r
+ }\r
+ }\r
+\r
+ /**\r
+ *\r
+ * @param bitOffset first bit to start writing\r
+ * @param array array to write into. Bytes are written most-significant byte first. This is the opposite\r
+ * of the internal representation, which is exposed by {@link #getBitArray()}\r
+ * @param offset position in array to start writing\r
+ * @param numBytes how many bytes to write\r
+ */\r
+ public void toBytes(int bitOffset, byte[] array, int offset, int numBytes) {\r
+ for (int i = 0; i < numBytes; i++) {\r
+ int theByte = 0;\r
+ for (int j = 0; j < 8; j++) {\r
+ if (get(bitOffset)) {\r
+ theByte |= 1 << (7 - j);\r
+ }\r
+ bitOffset++;\r
+ }\r
+ array[offset + i] = (byte) theByte;\r
+ }\r
+ }\r
+\r
/**\r
* @return underlying array of ints. The first element holds the first 32 bits, and the least\r
- * significant bit is bit 0.\r
+ * significant bit is bit 0.\r
*/\r
public int[] getBitArray() {\r
return bits;\r
}\r
\r
+ /**\r
+ * Reverses all bits in the array.\r
+ */\r
+ public void reverse() {\r
+ int[] newBits = new int[bits.length];\r
+ int size = this.size;\r
+ for (int i = 0; i < size; i++) {\r
+ if (get(size - i - 1)) {\r
+ newBits[i >> 5] |= 1 << (i & 0x1F);\r
+ }\r
+ }\r
+ bits = newBits;\r
+ }\r
+\r
+ private static int[] makeArray(int size) {\r
+ return new int[(size + 31) >> 5];\r
+ }\r
+ \r
+ public String toString() {\r
+ StringBuffer result = new StringBuffer(size);\r
+ for (int i = 0; i < size; i++) {\r
+ if ((i & 0x07) == 0) {\r
+ result.append(' ');\r
+ }\r
+ result.append(get(i) ? 'X' : '.');\r
+ }\r
+ return result.toString();\r
+ }\r
+\r
}
\ No newline at end of file