001/* 002 * Copyright 2007 ZXing authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.itextpdf.text.pdf.qrcode; 018 019/** 020 * <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p> 021 * 022 * @author Sean Owen 023 * @since 5.0.2 024 */ 025public final class BitArray { 026 027 // TODO: I have changed these members to be public so ProGuard can inline get() and set(). Ideally 028 // they'd be private and we'd use the -allowaccessmodification flag, but Dalvik rejects the 029 // resulting binary at runtime on Android. If we find a solution to this, these should be changed 030 // back to private. 031 public int[] bits; 032 public final int size; 033 034 public BitArray(int size) { 035 if (size < 1) { 036 throw new IllegalArgumentException("size must be at least 1"); 037 } 038 this.size = size; 039 this.bits = makeArray(size); 040 } 041 042 public int getSize() { 043 return size; 044 } 045 046 /** 047 * @param i bit to get 048 * @return true iff bit i is set 049 */ 050 public boolean get(int i) { 051 return (bits[i >> 5] & (1 << (i & 0x1F))) != 0; 052 } 053 054 /** 055 * Sets bit i. 056 * 057 * @param i bit to set 058 */ 059 public void set(int i) { 060 bits[i >> 5] |= 1 << (i & 0x1F); 061 } 062 063 /** 064 * Flips bit i. 065 * 066 * @param i bit to set 067 */ 068 public void flip(int i) { 069 bits[i >> 5] ^= 1 << (i & 0x1F); 070 } 071 072 /** 073 * Sets a block of 32 bits, starting at bit i. 074 * 075 * @param i first bit to set 076 * @param newBits the new value of the next 32 bits. Note again that the least-significant bit 077 * corresponds to bit i, the next-least-significant to i+1, and so on. 078 */ 079 public void setBulk(int i, int newBits) { 080 bits[i >> 5] = newBits; 081 } 082 083 /** 084 * Clears all bits (sets to false). 085 */ 086 public void clear() { 087 int max = bits.length; 088 for (int i = 0; i < max; i++) { 089 bits[i] = 0; 090 } 091 } 092 093 /** 094 * Efficient method to check if a range of bits is set, or not set. 095 * 096 * @param start start of range, inclusive. 097 * @param end end of range, exclusive 098 * @param value if true, checks that bits in range are set, otherwise checks that they are not set 099 * @return true iff all bits are set or not set in range, according to value argument 100 * @throws IllegalArgumentException if end is less than or equal to start 101 */ 102 public boolean isRange(int start, int end, boolean value) { 103 if (end < start) { 104 throw new IllegalArgumentException(); 105 } 106 if (end == start) { 107 return true; // empty range matches 108 } 109 end--; // will be easier to treat this as the last actually set bit -- inclusive 110 int firstInt = start >> 5; 111 int lastInt = end >> 5; 112 for (int i = firstInt; i <= lastInt; i++) { 113 int firstBit = i > firstInt ? 0 : start & 0x1F; 114 int lastBit = i < lastInt ? 31 : end & 0x1F; 115 int mask; 116 if (firstBit == 0 && lastBit == 31) { 117 mask = -1; 118 } else { 119 mask = 0; 120 for (int j = firstBit; j <= lastBit; j++) { 121 mask |= 1 << j; 122 } 123 } 124 125 // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is, 126 // equals the mask, or we're looking for 0s and the masked portion is not all 0s 127 if ((bits[i] & mask) != (value ? mask : 0)) { 128 return false; 129 } 130 } 131 return true; 132 } 133 134 /** 135 * @return underlying array of ints. The first element holds the first 32 bits, and the least 136 * significant bit is bit 0. 137 */ 138 public int[] getBitArray() { 139 return bits; 140 } 141 142 /** 143 * Reverses all bits in the array. 144 */ 145 public void reverse() { 146 int[] newBits = new int[bits.length]; 147 int size = this.size; 148 for (int i = 0; i < size; i++) { 149 if (get(size - i - 1)) { 150 newBits[i >> 5] |= 1 << (i & 0x1F); 151 } 152 } 153 bits = newBits; 154 } 155 156 private static int[] makeArray(int size) { 157 int arraySize = size >> 5; 158 if ((size & 0x1F) != 0) { 159 arraySize++; 160 } 161 return new int[arraySize]; 162 } 163 164 public String toString() { 165 StringBuffer result = new StringBuffer(size); 166 for (int i = 0; i < size; i++) { 167 if ((i & 0x07) == 0) { 168 result.append(' '); 169 } 170 result.append(get(i) ? 'X' : '.'); 171 } 172 return result.toString(); 173 } 174 175}