You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitArray.cs 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright 2007 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using System;
  17. namespace ZXing.Common
  18. {
  19. /// <summary>
  20. /// A simple, fast array of bits, represented compactly by an array of ints internally.
  21. /// </summary>
  22. /// <author>Sean Owen</author>
  23. public sealed class BitArray
  24. {
  25. private int[] bits;
  26. private int size;
  27. public int Size
  28. {
  29. get
  30. {
  31. return size;
  32. }
  33. }
  34. public int SizeInBytes
  35. {
  36. get
  37. {
  38. return (size + 7) >> 3;
  39. }
  40. }
  41. public bool this[int i]
  42. {
  43. get
  44. {
  45. return (bits[i >> 5] & (1 << (i & 0x1F))) != 0;
  46. }
  47. set
  48. {
  49. if (value)
  50. bits[i >> 5] |= 1 << (i & 0x1F);
  51. }
  52. }
  53. public BitArray()
  54. {
  55. this.size = 0;
  56. this.bits = new int[1];
  57. }
  58. public BitArray(int size)
  59. {
  60. if (size < 1)
  61. {
  62. throw new ArgumentException("size must be at least 1");
  63. }
  64. this.size = size;
  65. this.bits = makeArray(size);
  66. }
  67. // For testing only
  68. private BitArray(int[] bits, int size)
  69. {
  70. this.bits = bits;
  71. this.size = size;
  72. }
  73. private void ensureCapacity(int size)
  74. {
  75. if (size > bits.Length << 5)
  76. {
  77. int[] newBits = makeArray(size);
  78. System.Array.Copy(bits, 0, newBits, 0, bits.Length);
  79. bits = newBits;
  80. }
  81. }
  82. /// <summary> Flips bit i.
  83. ///
  84. /// </summary>
  85. /// <param name="i">bit to set
  86. /// </param>
  87. public void flip(int i)
  88. {
  89. bits[i >> 5] ^= 1 << (i & 0x1F);
  90. }
  91. private static int numberOfTrailingZeros(int num)
  92. {
  93. var index = (-num & num)%37;
  94. if (index < 0)
  95. index *= -1;
  96. return _lookup[index];
  97. }
  98. private static readonly int[] _lookup =
  99. {
  100. 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17,
  101. 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
  102. };
  103. /// <summary>
  104. /// Gets the next set.
  105. /// </summary>
  106. /// <param name="from">first bit to check</param>
  107. /// <returns>index of first bit that is set, starting from the given index, or size if none are set
  108. /// at or beyond this given index</returns>
  109. public int getNextSet(int from)
  110. {
  111. if (from >= size)
  112. {
  113. return size;
  114. }
  115. int bitsOffset = from >> 5;
  116. int currentBits = bits[bitsOffset];
  117. // mask off lesser bits first
  118. currentBits &= ~((1 << (from & 0x1F)) - 1);
  119. while (currentBits == 0)
  120. {
  121. if (++bitsOffset == bits.Length)
  122. {
  123. return size;
  124. }
  125. currentBits = bits[bitsOffset];
  126. }
  127. int result = (bitsOffset << 5) + numberOfTrailingZeros(currentBits);
  128. return result > size ? size : result;
  129. }
  130. /// <summary>
  131. /// see getNextSet(int)
  132. /// </summary>
  133. /// <param name="from"></param>
  134. /// <returns></returns>
  135. public int getNextUnset(int from)
  136. {
  137. if (from >= size)
  138. {
  139. return size;
  140. }
  141. int bitsOffset = from >> 5;
  142. int currentBits = ~bits[bitsOffset];
  143. // mask off lesser bits first
  144. currentBits &= ~((1 << (from & 0x1F)) - 1);
  145. while (currentBits == 0)
  146. {
  147. if (++bitsOffset == bits.Length)
  148. {
  149. return size;
  150. }
  151. currentBits = ~bits[bitsOffset];
  152. }
  153. int result = (bitsOffset << 5) + numberOfTrailingZeros(currentBits);
  154. return result > size ? size : result;
  155. }
  156. /// <summary> Sets a block of 32 bits, starting at bit i.
  157. ///
  158. /// </summary>
  159. /// <param name="i">first bit to set
  160. /// </param>
  161. /// <param name="newBits">the new value of the next 32 bits. Note again that the least-significant bit
  162. /// corresponds to bit i, the next-least-significant to i+1, and so on.
  163. /// </param>
  164. public void setBulk(int i, int newBits)
  165. {
  166. bits[i >> 5] = newBits;
  167. }
  168. /// <summary>
  169. /// Sets a range of bits.
  170. /// </summary>
  171. /// <param name="start">start of range, inclusive.</param>
  172. /// <param name="end">end of range, exclusive</param>
  173. public void setRange(int start, int end)
  174. {
  175. if (end < start)
  176. {
  177. throw new ArgumentException();
  178. }
  179. if (end == start)
  180. {
  181. return;
  182. }
  183. end--; // will be easier to treat this as the last actually set bit -- inclusive
  184. int firstInt = start >> 5;
  185. int lastInt = end >> 5;
  186. for (int i = firstInt; i <= lastInt; i++)
  187. {
  188. int firstBit = i > firstInt ? 0 : start & 0x1F;
  189. int lastBit = i < lastInt ? 31 : end & 0x1F;
  190. int mask;
  191. if (firstBit == 0 && lastBit == 31)
  192. {
  193. mask = -1;
  194. }
  195. else
  196. {
  197. mask = 0;
  198. for (int j = firstBit; j <= lastBit; j++)
  199. {
  200. mask |= 1 << j;
  201. }
  202. }
  203. bits[i] |= mask;
  204. }
  205. }
  206. /// <summary> Clears all bits (sets to false).</summary>
  207. public void clear()
  208. {
  209. int max = bits.Length;
  210. for (int i = 0; i < max; i++)
  211. {
  212. bits[i] = 0;
  213. }
  214. }
  215. /// <summary> Efficient method to check if a range of bits is set, or not set.
  216. ///
  217. /// </summary>
  218. /// <param name="start">start of range, inclusive.
  219. /// </param>
  220. /// <param name="end">end of range, exclusive
  221. /// </param>
  222. /// <param name="value">if true, checks that bits in range are set, otherwise checks that they are not set
  223. /// </param>
  224. /// <returns> true iff all bits are set or not set in range, according to value argument
  225. /// </returns>
  226. /// <throws> IllegalArgumentException if end is less than or equal to start </throws>
  227. public bool isRange(int start, int end, bool value)
  228. {
  229. if (end < start)
  230. {
  231. throw new System.ArgumentException();
  232. }
  233. if (end == start)
  234. {
  235. return true; // empty range matches
  236. }
  237. end--; // will be easier to treat this as the last actually set bit -- inclusive
  238. int firstInt = start >> 5;
  239. int lastInt = end >> 5;
  240. for (int i = firstInt; i <= lastInt; i++)
  241. {
  242. int firstBit = i > firstInt ? 0 : start & 0x1F;
  243. int lastBit = i < lastInt ? 31 : end & 0x1F;
  244. int mask;
  245. if (firstBit == 0 && lastBit == 31)
  246. {
  247. mask = -1;
  248. }
  249. else
  250. {
  251. mask = 0;
  252. for (int j = firstBit; j <= lastBit; j++)
  253. {
  254. mask |= 1 << j;
  255. }
  256. }
  257. // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
  258. // equals the mask, or we're looking for 0s and the masked portion is not all 0s
  259. if ((bits[i] & mask) != (value ? mask : 0))
  260. {
  261. return false;
  262. }
  263. }
  264. return true;
  265. }
  266. /// <summary>
  267. /// Appends the bit.
  268. /// </summary>
  269. /// <param name="bit">The bit.</param>
  270. public void appendBit(bool bit)
  271. {
  272. ensureCapacity(size + 1);
  273. if (bit)
  274. {
  275. bits[size >> 5] |= 1 << (size & 0x1F);
  276. }
  277. size++;
  278. }
  279. /// <returns> underlying array of ints. The first element holds the first 32 bits, and the least
  280. /// significant bit is bit 0.
  281. /// </returns>
  282. public int[] Array
  283. {
  284. get { return bits; }
  285. }
  286. /// <summary>
  287. /// Appends the least-significant bits, from value, in order from most-significant to
  288. /// least-significant. For example, appending 6 bits from 0x000001E will append the bits
  289. /// 0, 1, 1, 1, 1, 0 in that order.
  290. /// </summary>
  291. /// <param name="value">The value.</param>
  292. /// <param name="numBits">The num bits.</param>
  293. public void appendBits(int value, int numBits)
  294. {
  295. if (numBits < 0 || numBits > 32)
  296. {
  297. throw new ArgumentException("Num bits must be between 0 and 32");
  298. }
  299. ensureCapacity(size + numBits);
  300. for (int numBitsLeft = numBits; numBitsLeft > 0; numBitsLeft--)
  301. {
  302. appendBit(((value >> (numBitsLeft - 1)) & 0x01) == 1);
  303. }
  304. }
  305. public void appendBitArray(BitArray other)
  306. {
  307. int otherSize = other.size;
  308. ensureCapacity(size + otherSize);
  309. for (int i = 0; i < otherSize; i++)
  310. {
  311. appendBit(other[i]);
  312. }
  313. }
  314. public void xor(BitArray other)
  315. {
  316. if (bits.Length != other.bits.Length)
  317. {
  318. throw new ArgumentException("Sizes don't match");
  319. }
  320. for (int i = 0; i < bits.Length; i++)
  321. {
  322. // The last byte could be incomplete (i.e. not have 8 bits in
  323. // it) but there is no problem since 0 XOR 0 == 0.
  324. bits[i] ^= other.bits[i];
  325. }
  326. }
  327. /// <summary>
  328. /// Toes the bytes.
  329. /// </summary>
  330. /// <param name="bitOffset">first bit to start writing</param>
  331. /// <param name="array">array to write into. Bytes are written most-significant byte first. This is the opposite
  332. /// of the internal representation, which is exposed by BitArray</param>
  333. /// <param name="offset">position in array to start writing</param>
  334. /// <param name="numBytes">how many bytes to write</param>
  335. public void toBytes(int bitOffset, byte[] array, int offset, int numBytes)
  336. {
  337. for (int i = 0; i < numBytes; i++)
  338. {
  339. int theByte = 0;
  340. for (int j = 0; j < 8; j++)
  341. {
  342. if (this[bitOffset])
  343. {
  344. theByte |= 1 << (7 - j);
  345. }
  346. bitOffset++;
  347. }
  348. array[offset + i] = (byte)theByte;
  349. }
  350. }
  351. /// <summary> Reverses all bits in the array.</summary>
  352. public void reverse()
  353. {
  354. var newBits = new int[bits.Length];
  355. // reverse all int's first
  356. var len = ((size - 1) >> 5);
  357. var oldBitsLen = len + 1;
  358. for (var i = 0; i < oldBitsLen; i++)
  359. {
  360. var x = (long)bits[i];
  361. x = ((x >> 1) & 0x55555555u) | ((x & 0x55555555u) << 1);
  362. x = ((x >> 2) & 0x33333333u) | ((x & 0x33333333u) << 2);
  363. x = ((x >> 4) & 0x0f0f0f0fu) | ((x & 0x0f0f0f0fu) << 4);
  364. x = ((x >> 8) & 0x00ff00ffu) | ((x & 0x00ff00ffu) << 8);
  365. x = ((x >> 16) & 0x0000ffffu) | ((x & 0x0000ffffu) << 16);
  366. newBits[len - i] = (int)x;
  367. }
  368. // now correct the int's if the bit size isn't a multiple of 32
  369. if (size != oldBitsLen * 32)
  370. {
  371. var leftOffset = oldBitsLen * 32 - size;
  372. var mask = 1;
  373. for (var i = 0; i < 31 - leftOffset; i++)
  374. mask = (mask << 1) | 1;
  375. var currentInt = (newBits[0] >> leftOffset) & mask;
  376. for (var i = 1; i < oldBitsLen; i++)
  377. {
  378. var nextInt = newBits[i];
  379. currentInt |= nextInt << (32 - leftOffset);
  380. newBits[i - 1] = currentInt;
  381. currentInt = (nextInt >> leftOffset) & mask;
  382. }
  383. newBits[oldBitsLen - 1] = currentInt;
  384. }
  385. bits = newBits;
  386. }
  387. private static int[] makeArray(int size)
  388. {
  389. return new int[(size + 31) >> 5];
  390. }
  391. /// <summary>
  392. /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
  393. /// </summary>
  394. /// <param name="o">The <see cref="System.Object"/> to compare with this instance.</param>
  395. /// <returns>
  396. /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
  397. /// </returns>
  398. public override bool Equals(Object o)
  399. {
  400. var other = o as BitArray;
  401. if (other == null)
  402. return false;
  403. if (size != other.size)
  404. return false;
  405. for (var index = 0; index < size; index++)
  406. {
  407. if (bits[index] != other.bits[index])
  408. return false;
  409. }
  410. return true;
  411. }
  412. /// <summary>
  413. /// Returns a hash code for this instance.
  414. /// </summary>
  415. /// <returns>
  416. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  417. /// </returns>
  418. public override int GetHashCode()
  419. {
  420. var hash = size;
  421. foreach (var bit in bits)
  422. {
  423. hash = 31 * hash + bit.GetHashCode();
  424. }
  425. return hash;
  426. }
  427. /// <summary>
  428. /// Returns a <see cref="System.String"/> that represents this instance.
  429. /// </summary>
  430. /// <returns>
  431. /// A <see cref="System.String"/> that represents this instance.
  432. /// </returns>
  433. public override String ToString()
  434. {
  435. var result = new System.Text.StringBuilder(size);
  436. for (int i = 0; i < size; i++)
  437. {
  438. if ((i & 0x07) == 0)
  439. {
  440. result.Append(' ');
  441. }
  442. result.Append(this[i] ? 'X' : '.');
  443. }
  444. return result.ToString();
  445. }
  446. /// <summary>
  447. /// Erstellt ein neues Objekt, das eine Kopie der aktuellen Instanz darstellt.
  448. /// </summary>
  449. /// <returns>
  450. /// Ein neues Objekt, das eine Kopie dieser Instanz darstellt.
  451. /// </returns>
  452. public object Clone()
  453. {
  454. return new BitArray((int[])bits.Clone(), size);
  455. }
  456. }
  457. }