BitArray.php 13 KB

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