BitArray.php 11 KB

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