2 * Copyright 2007 ZXing authors
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 namespace com.google.zxing.common
\r
20 /// <summary> <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>
\r
23 /// <author> Sean Owen
\r
25 /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source
\r
27 public sealed class BitArray
\r
38 // TODO: I have changed these members to be public so ProGuard can inline get() and set(). Ideally
\r
39 // they'd be private and we'd use the -allowaccessmodification flag, but Dalvik rejects the
\r
40 // resulting binary at runtime on Android. If we find a solution to this, these should be changed
\r
43 //UPGRADE_NOTE: Final was removed from the declaration of 'size '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"
\r
46 public BitArray(int size)
\r
50 throw new System.ArgumentException("size must be at least 1");
\r
53 this.bits = makeArray(size);
\r
56 /// <param name="i">bit to get
\r
58 /// <returns> true iff bit i is set
\r
60 public bool get_Renamed(int i)
\r
62 return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;
\r
65 /// <summary> Sets bit i.
\r
68 /// <param name="i">bit to set
\r
70 public void set_Renamed(int i)
\r
72 bits[i >> 5] |= 1 << (i & 0x1F);
\r
75 /// <summary> Flips bit i.
\r
78 /// <param name="i">bit to set
\r
80 public void flip(int i)
\r
82 bits[i >> 5] ^= 1 << (i & 0x1F);
\r
85 /// <summary> Sets a block of 32 bits, starting at bit i.
\r
88 /// <param name="i">first bit to set
\r
90 /// <param name="newBits">the new value of the next 32 bits. Note again that the least-significant bit
\r
91 /// corresponds to bit i, the next-least-significant to i+1, and so on.
\r
93 public void setBulk(int i, int newBits)
\r
95 bits[i >> 5] = newBits;
\r
98 /// <summary> Clears all bits (sets to false).</summary>
\r
101 int max = bits.Length;
\r
102 for (int i = 0; i < max; i++)
\r
108 /// <summary> Efficient method to check if a range of bits is set, or not set.
\r
111 /// <param name="start">start of range, inclusive.
\r
113 /// <param name="end">end of range, exclusive
\r
115 /// <param name="value">if true, checks that bits in range are set, otherwise checks that they are not set
\r
117 /// <returns> true iff all bits are set or not set in range, according to value argument
\r
119 /// <throws> IllegalArgumentException if end is less than or equal to start </throws>
\r
120 public bool isRange(int start, int end, bool value_Renamed)
\r
124 throw new System.ArgumentException();
\r
128 return true; // empty range matches
\r
130 end--; // will be easier to treat this as the last actually set bit -- inclusive
\r
131 int firstInt = start >> 5;
\r
132 int lastInt = end >> 5;
\r
133 for (int i = firstInt; i <= lastInt; i++)
\r
135 int firstBit = i > firstInt?0:start & 0x1F;
\r
136 int lastBit = i < lastInt?31:end & 0x1F;
\r
138 if (firstBit == 0 && lastBit == 31)
\r
145 for (int j = firstBit; j <= lastBit; j++)
\r
151 // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
\r
152 // equals the mask, or we're looking for 0s and the masked portion is not all 0s
\r
153 if ((bits[i] & mask) != (value_Renamed?mask:0))
\r
161 /// <returns> underlying array of ints. The first element holds the first 32 bits, and the least
\r
162 /// significant bit is bit 0.
\r
164 public int[] getBitArray()
\r
169 /// <summary> Reverses all bits in the array.</summary>
\r
170 public void reverse()
\r
172 int[] newBits = new int[bits.Length];
\r
173 int size = this.size;
\r
174 for (int i = 0; i < size; i++)
\r
176 if (get_Renamed(size - i - 1))
\r
178 newBits[i >> 5] |= 1 << (i & 0x1F);
\r
184 private static int[] makeArray(int size)
\r
186 int arraySize = size >> 5;
\r
187 if ((size & 0x1F) != 0)
\r
191 return new int[arraySize];
\r
194 public override System.String ToString()
\r
196 System.Text.StringBuilder result = new System.Text.StringBuilder(size);
\r
197 for (int i = 0; i < size; i++)
\r
199 if ((i & 0x07) == 0)
\r
201 result.Append(' ');
\r
203 result.Append(get_Renamed(i)?'X':'.');
\r
205 return result.ToString();
\r