/*\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 final int size;\r
\r
public BitArray(int size) {\r
- int arraySize = size >> 5;\r
- if ((size & 0x1F) != 0) {\r
- arraySize++;\r
+ if (size < 1) {\r
+ throw new IllegalArgumentException("size must be at least 1");\r
}\r
- bits = new int[arraySize];\r
+ this.size = size;\r
+ this.bits = makeArray(size);\r
+ }\r
+\r
+ public int getSize() {\r
+ return size;\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
/**\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
+ int arraySize = size >> 5;\r
+ if ((size & 0x1F) != 0) {\r
+ arraySize++;\r
+ }\r
+ return new int[arraySize];\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