BitArray.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Ashot
  5. * Date: 3/25/15
  6. * Time: 11:51
  7. */
  8. /*
  9. * Copyright 2007 ZXing authors
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. namespace Zxing\Common;
  24. /**
  25. * <p>A simple, fast array of bits, represented compactly by an array of ints internally.</p>
  26. *
  27. * @author Sean Owen
  28. */
  29. final class BitArray
  30. {
  31. private $bits;
  32. private $size;
  33. public function __construct($bits = [], $size = 0)
  34. {
  35. if (!$bits && !$size) {
  36. $this->$size = 0;
  37. $this->bits = [];
  38. } elseif ($bits && !$size) {
  39. $this->size = $bits;
  40. $this->bits = $this->makeArray($bits);
  41. } else {
  42. $this->bits = $bits;
  43. $this->size = $size;
  44. }
  45. }
  46. private static function makeArray($size)
  47. {
  48. return [];
  49. }
  50. public function getSize()
  51. {
  52. return $this->size;
  53. }
  54. public function getSizeInBytes()
  55. {
  56. return ($this->size + 7) / 8;
  57. }
  58. /**
  59. * Sets bit i.
  60. *
  61. * @param i bit to set
  62. */
  63. public function set($i)
  64. {
  65. $this->bits[(int)($i / 32)] |= 1 << ($i & 0x1F);
  66. $this->bits[(int)($i / 32)] = ($this->bits[(int)($i / 32)]);
  67. }
  68. /**
  69. * Flips bit i.
  70. *
  71. * @param i bit to set
  72. */
  73. public function flip($i)
  74. {
  75. $this->bits[(int)($i / 32)] ^= 1 << ($i & 0x1F);
  76. $this->bits[(int)($i / 32)] = ($this->bits[(int)($i / 32)]);
  77. }
  78. /**
  79. * @param from first bit to check
  80. *
  81. * @return index of first bit that is set, starting from the given index, or size if none are set
  82. * at or beyond this given index
  83. * @see #getNextUnset(int)
  84. */
  85. public function getNextSet($from)
  86. {
  87. if ($from >= $this->size) {
  88. return $this->size;
  89. }
  90. $bitsOffset = (int)($from / 32);
  91. $currentBits = (int)$this->bits[$bitsOffset];
  92. // mask off lesser bits first
  93. $currentBits &= ~((1 << ($from & 0x1F)) - 1);
  94. while ($currentBits == 0) {
  95. if (++$bitsOffset == count($this->bits)) {
  96. return $this->size;
  97. }
  98. $currentBits = $this->bits[$bitsOffset];
  99. }
  100. $result = ($bitsOffset * 32) + numberOfTrailingZeros($currentBits); //numberOfTrailingZeros
  101. return $result > $this->size ? $this->size : $result;
  102. }
  103. /**
  104. * @param from index to start looking for unset bit
  105. *
  106. * @return index of next unset bit, or {@code size} if none are unset until the end
  107. * @see #getNextSet(int)
  108. */
  109. public function getNextUnset($from)
  110. {
  111. if ($from >= $this->size) {
  112. return $this->size;
  113. }
  114. $bitsOffset = (int)($from / 32);
  115. $currentBits = ~$this->bits[$bitsOffset];
  116. // mask off lesser bits first
  117. $currentBits &= ~((1 << ($from & 0x1F)) - 1);
  118. while ($currentBits == 0) {
  119. if (++$bitsOffset == count($this->bits)) {
  120. return $this->size;
  121. }
  122. $currentBits = (~$this->bits[$bitsOffset]);
  123. }
  124. $result = ($bitsOffset * 32) + numberOfTrailingZeros($currentBits);
  125. return $result > $this->size ? $this->size : $result;
  126. }
  127. /**
  128. * Sets a block of 32 bits, starting at bit i.
  129. *
  130. * @param i first bit to set
  131. * @param newBits the new value of the next 32 bits. Note again that the least-significant bit
  132. * corresponds to bit i, the next-least-significant to i+1, and so on.
  133. */
  134. public function setBulk($i, $newBits)
  135. {
  136. $this->bits[(int)($i / 32)] = $newBits;
  137. }
  138. /**
  139. * Sets a range of bits.
  140. *
  141. * @param start start of range, inclusive.
  142. * @param end end of range, exclusive
  143. */
  144. public function setRange($start, $end)
  145. {
  146. if ($end < $start) {
  147. throw new \InvalidArgumentException();
  148. }
  149. if ($end == $start) {
  150. return;
  151. }
  152. $end--; // will be easier to treat this as the last actually set bit -- inclusive
  153. $firstInt = (int)($start / 32);
  154. $lastInt = (int)($end / 32);
  155. for ($i = $firstInt; $i <= $lastInt; $i++) {
  156. $firstBit = $i > $firstInt ? 0 : $start & 0x1F;
  157. $lastBit = $i < $lastInt ? 31 : $end & 0x1F;
  158. $mask = 0;
  159. if ($firstBit == 0 && $lastBit == 31) {
  160. $mask = -1;
  161. } else {
  162. $mask = 0;
  163. for ($j = $firstBit; $j <= $lastBit; $j++) {
  164. $mask |= 1 << $j;
  165. }
  166. }
  167. $this->bits[$i] = ($this->bits[$i] | $mask);
  168. }
  169. }
  170. /**
  171. * Clears all bits (sets to false).
  172. */
  173. public function clear()
  174. {
  175. $max = count($this->bits);
  176. for ($i = 0; $i < $max; $i++) {
  177. $this->bits[$i] = 0;
  178. }
  179. }
  180. /**
  181. * Efficient method to check if a range of bits is set, or not set.
  182. *
  183. * @param start start of range, inclusive.
  184. * @param end end of range, exclusive
  185. * @param value if true, checks that bits in range are set, otherwise checks that they are not set
  186. *
  187. * @return true iff all bits are set or not set in range, according to value argument
  188. * @throws InvalidArgumentException if end is less than or equal to start
  189. */
  190. public function isRange($start, $end, $value)
  191. {
  192. if ($end < $start) {
  193. throw new \InvalidArgumentException();
  194. }
  195. if ($end == $start) {
  196. return true; // empty range matches
  197. }
  198. $end--; // will be easier to treat this as the last actually set bit -- inclusive
  199. $firstInt = (int)($start / 32);
  200. $lastInt = (int)($end / 32);
  201. for ($i = $firstInt; $i <= $lastInt; $i++) {
  202. $firstBit = $i > $firstInt ? 0 : $start & 0x1F;
  203. $lastBit = $i < $lastInt ? 31 : $end & 0x1F;
  204. $mask = 0;
  205. if ($firstBit == 0 && $lastBit == 31) {
  206. $mask = -1;
  207. } else {
  208. $mask = 0;
  209. for ($j = $firstBit; $j <= $lastBit; $j++) {
  210. $mask = ($mask | (1 << $j));
  211. }
  212. }
  213. // Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
  214. // equals the mask, or we're looking for 0s and the masked portion is not all 0s
  215. if (($this->bits[$i] & $mask) != ($value ? $mask : 0)) {
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. /**
  222. * Appends the least-significant bits, from value, in order from most-significant to
  223. * least-significant. For example, appending 6 bits from 0x000001E will append the bits
  224. * 0, 1, 1, 1, 1, 0 in that order.
  225. *
  226. * @param value {@code int} containing bits to append
  227. * @param numBits bits from value to append
  228. */
  229. public function appendBits($value, $numBits)
  230. {
  231. if ($numBits < 0 || $numBits > 32) {
  232. throw new \InvalidArgumentException("Num bits must be between 0 and 32");
  233. }
  234. $this->ensureCapacity($this->size + $numBits);
  235. for ($numBitsLeft = $numBits; $numBitsLeft > 0; $numBitsLeft--) {
  236. $this->appendBit((($value >> ($numBitsLeft - 1)) & 0x01) == 1);
  237. }
  238. }
  239. private function ensureCapacity($size)
  240. {
  241. if ($size > count($this->bits) * 32) {
  242. $newBits = $this->makeArray($size);
  243. $newBits = arraycopy($this->bits, 0, $newBits, 0, count($this->bits));
  244. $this->bits = $newBits;
  245. }
  246. }
  247. public function appendBit($bit)
  248. {
  249. $this->ensureCapacity($this->size + 1);
  250. if ($bit) {
  251. $this->bits[(int)($this->size / 32)] |= 1 << ($this->size & 0x1F);
  252. }
  253. $this->size++;
  254. }
  255. public function appendBitArray($other)
  256. {
  257. $otherSize = $other->size;
  258. $this->ensureCapacity($this->size + $otherSize);
  259. for ($i = 0; $i < $otherSize; $i++) {
  260. $this->appendBit($other->get($i));
  261. }
  262. }
  263. public function _xor($other)
  264. {
  265. if (count($this->bits) !== count($other->bits)) {
  266. throw new \InvalidArgumentException("Sizes don't match");
  267. }
  268. $count = count($this->bits);
  269. for ($i = 0; $i < $count; $i++) {
  270. // The last byte could be incomplete (i.e. not have 8 bits in
  271. // it) but there is no problem since 0 XOR 0 == 0.
  272. $this->bits[$i] ^= $other->bits[$i];
  273. }
  274. }
  275. /**
  276. *
  277. * @param bitOffset first bit to start writing
  278. * @param array array to write into. Bytes are written most-significant byte first. This is the opposite
  279. * of the internal representation, which is exposed by {@link #getBitArray()}
  280. * @param offset position in array to start writing
  281. * @param numBytes how many bytes to write
  282. */
  283. public function toBytes($bitOffset, &$array, $offset, $numBytes)
  284. {
  285. for ($i = 0; $i < $numBytes; $i++) {
  286. $theByte = 0;
  287. for ($j = 0; $j < 8; $j++) {
  288. if ($this->get($bitOffset)) {
  289. $theByte |= 1 << (7 - $j);
  290. }
  291. $bitOffset++;
  292. }
  293. $array[(int)($offset + $i)] = $theByte;
  294. }
  295. }
  296. /**
  297. * @param $i ; bit to get
  298. *
  299. * @return true iff bit i is set
  300. */
  301. public function get($i)
  302. {
  303. $key = (int)($i / 32);
  304. return ($this->bits[$key] & (1 << ($i & 0x1F))) != 0;
  305. }
  306. /**
  307. * @return array underlying array of ints. The first element holds the first 32 bits, and the least
  308. * significant bit is bit 0.
  309. */
  310. public function getBitArray()
  311. {
  312. return $this->bits;
  313. }
  314. /**
  315. * Reverses all bits in the array.
  316. */
  317. public function reverse()
  318. {
  319. $newBits = [];
  320. // reverse all int's first
  321. $len = (($this->size - 1) / 32);
  322. $oldBitsLen = $len + 1;
  323. for ($i = 0; $i < $oldBitsLen; $i++) {
  324. $x = $this->bits[$i];/*
  325. $x = (($x >> 1) & 0x55555555L) | (($x & 0x55555555L) << 1);
  326. $x = (($x >> 2) & 0x33333333L) | (($x & 0x33333333L) << 2);
  327. $x = (($x >> 4) & 0x0f0f0f0fL) | (($x & 0x0f0f0f0fL) << 4);
  328. $x = (($x >> 8) & 0x00ff00ffL) | (($x & 0x00ff00ffL) << 8);
  329. $x = (($x >> 16) & 0x0000ffffL) | (($x & 0x0000ffffL) << 16);*/
  330. $x = (($x >> 1) & 0x55555555) | (($x & 0x55555555) << 1);
  331. $x = (($x >> 2) & 0x33333333) | (($x & 0x33333333) << 2);
  332. $x = (($x >> 4) & 0x0f0f0f0f) | (($x & 0x0f0f0f0f) << 4);
  333. $x = (($x >> 8) & 0x00ff00ff) | (($x & 0x00ff00ff) << 8);
  334. $x = (($x >> 16) & 0x0000ffff) | (($x & 0x0000ffff) << 16);
  335. $newBits[(int)$len - $i] = (int)$x;
  336. }
  337. // now correct the int's if the bit size isn't a multiple of 32
  338. if ($this->size != $oldBitsLen * 32) {
  339. $leftOffset = $oldBitsLen * 32 - $this->size;
  340. $mask = 1;
  341. for ($i = 0; $i < 31 - $leftOffset; $i++) {
  342. $mask = ($mask << 1) | 1;
  343. }
  344. $currentInt = ($newBits[0] >> $leftOffset) & $mask;
  345. for ($i = 1; $i < $oldBitsLen; $i++) {
  346. $nextInt = $newBits[$i];
  347. $currentInt |= $nextInt << (32 - $leftOffset);
  348. $newBits[(int)($i) - 1] = $currentInt;
  349. $currentInt = ($nextInt >> $leftOffset) & $mask;
  350. }
  351. $newBits[(int)($oldBitsLen) - 1] = $currentInt;
  352. }
  353. // $bits = $newBits;
  354. }
  355. public function equals($o)
  356. {
  357. if (!($o instanceof BitArray)) {
  358. return false;
  359. }
  360. $other = $o;
  361. return $this->size == $other->size && $this->bits === $other->bits;
  362. }
  363. public function hashCode()
  364. {
  365. return 31 * $this->size + hashCode($this->bits);
  366. }
  367. public function toString()
  368. {
  369. $result = '';
  370. for ($i = 0; $i < $this->size; $i++) {
  371. if (($i & 0x07) == 0) {
  372. $result .= ' ';
  373. }
  374. $result .= ($this->get($i) ? 'X' : '.');
  375. }
  376. return (string)$result;
  377. }
  378. public function _clone()
  379. {
  380. return new BitArray($this->bits, $this->size);
  381. }
  382. }