Upgrade 1-11.38

This commit is contained in:
xesmyd
2026-03-30 14:10:30 +02:00
parent f2a7e6d1fc
commit ac648ef29d
24665 changed files with 69682 additions and 2205004 deletions
@@ -30,365 +30,396 @@ namespace Zxing\Common;
* @author Sean Owen
*/
final class BitArray {
private $bits;
private $size;
final class BitArray
{
/**
* @var mixed[]|mixed|int[]|null
*/
private $bits;
/**
* @var mixed|null
*/
private $size;
public function __construct($bits = [], $size = 0)
{
if (!$bits && !$size) {
$this->$size = 0;
$this->bits = [];
} elseif ($bits && !$size) {
$this->size = $bits;
$this->bits = self::makeArray($bits);
} else {
$this->bits = $bits;
$this->size = $size;
}
}
public function __construct($bits=array(),$size=0) {
private static function makeArray($size)
{
return [];
}
if(!$bits&&!$size){
$this->$size = 0;
$this->bits = array();
}elseif($bits&&!$size){
$this->size = $bits;
$this->bits = $this->makeArray($bits);
}else{
$this->bits = $bits;
$this->size = $size;
}
public function getSize()
{
return $this->size;
}
}
public function getSizeInBytes()
{
return ($this->size + 7) / 8;
}
/**
* Sets bit i.
*
* @param bit $i to set
*/
public function set($i): void
{
$this->bits[(int)($i / 32)] |= 1 << ($i & 0x1F);
$this->bits[(int)($i / 32)] = ($this->bits[(int)($i / 32)]);
}
/**
* Flips bit i.
*
* @param bit $i to set
*/
public function flip($i): void
{
$this->bits[(int)($i / 32)] ^= 1 << ($i & 0x1F);
$this->bits[(int)($i / 32)] = ($this->bits[(int)($i / 32)]);
}
/**
* @param first $from bit to check
*
* @return index of first bit that is set, starting from the given index, or size if none are set
* at or beyond this given index
* @see #getNextUnset(int)
*/
public function getNextSet($from)
{
if ($from >= $this->size) {
return $this->size;
}
$bitsOffset = (int)($from / 32);
$currentBits = (int)$this->bits[$bitsOffset];
// mask off lesser bits first
$currentBits &= ~((1 << ($from & 0x1F)) - 1);
while ($currentBits == 0) {
if (++$bitsOffset == (is_countable($this->bits) ? count($this->bits) : 0)) {
return $this->size;
}
$currentBits = $this->bits[$bitsOffset];
}
$result = ($bitsOffset * 32) + numberOfTrailingZeros($currentBits); //numberOfTrailingZeros
public function getSize() {
return $this->size;
}
return $result > $this->size ? $this->size : $result;
}
public function getSizeInBytes() {
return ($this->size + 7) / 8;
}
/**
* @param index $from to start looking for unset bit
*
* @return index of next unset bit, or {@code size} if none are unset until the end
* @see #getNextSet(int)
*/
public function getNextUnset($from)
{
if ($from >= $this->size) {
return $this->size;
}
$bitsOffset = (int)($from / 32);
$currentBits = ~$this->bits[$bitsOffset];
// mask off lesser bits first
$currentBits &= ~((1 << ($from & 0x1F)) - 1);
while ($currentBits == 0) {
if (++$bitsOffset == (is_countable($this->bits) ? count($this->bits) : 0)) {
return $this->size;
}
$currentBits = (~$this->bits[$bitsOffset]);
}
$result = ($bitsOffset * 32) + numberOfTrailingZeros($currentBits);
private function ensureCapacity($size) {
if ($size > count($this->bits) * 32) {
$newBits = $this->makeArray($size);
$newBits = arraycopy($this->bits, 0, $newBits, 0, count($this->bits));
$this->bits = $newBits;
}
}
return $result > $this->size ? $this->size : $result;
}
/**
* @param $i; bit to get
* @return true iff bit i is set
*/
public function get($i) {
$key = intval($i / 32);
return intval32bits($this->bits[$key] & (1 << ($i & 0x1F))) != 0;
}
/**
* Sets a block of 32 bits, starting at bit i.
*
* @param first $i bit to set
* @param the $newBits new value of the next 32 bits. Note again that the least-significant bit
* corresponds to bit i, the next-least-significant to i+1, and so on.
*/
public function setBulk($i, $newBits): void
{
$this->bits[(int)($i / 32)] = $newBits;
}
/**
* Sets bit i.
*
* @param i bit to set
*/
public function set($i) {
$this->bits[intval($i / 32)] |= 1 << ($i & 0x1F);
$this->bits[intval($i / 32)] = overflow($this->bits[intval($i / 32)]);
}
/**
* Sets a range of bits.
*
* @param start $start of range, inclusive.
* @param end $end of range, exclusive
*/
public function setRange($start, $end)
{
if ($end < $start) {
throw new \InvalidArgumentException();
}
if ($end == $start) {
return;
}
$end--; // will be easier to treat this as the last actually set bit -- inclusive
$firstInt = (int)($start / 32);
$lastInt = (int)($end / 32);
for ($i = $firstInt; $i <= $lastInt; $i++) {
$firstBit = $i > $firstInt ? 0 : $start & 0x1F;
$lastBit = $i < $lastInt ? 31 : $end & 0x1F;
$mask = 0;
if ($firstBit == 0 && $lastBit == 31) {
$mask = -1;
} else {
$mask = 0;
for ($j = $firstBit; $j <= $lastBit; $j++) {
$mask |= 1 << $j;
}
}
$this->bits[$i] = ($this->bits[$i] | $mask);
}
}
/**
* Flips bit i.
*
* @param i bit to set
*/
public function flip($i) {
$this->bits[intval($i / 32)] ^= 1 << ($i & 0x1F);
$this->bits[intval($i / 32)] = overflow32($this->bits[intval($i / 32)]);
}
/**
* Clears all bits (sets to false).
*/
public function clear(): void
{
$max = is_countable($this->bits) ? count($this->bits) : 0;
for ($i = 0; $i < $max; $i++) {
$this->bits[$i] = 0;
}
}
/**
* @param from first bit to check
* @return index of first bit that is set, starting from the given index, or size if none are set
* at or beyond this given index
* @see #getNextUnset(int)
*/
public function getNextSet($from) {
if ($from >= $this->size) {
return $this->size;
}
$bitsOffset = intval($from / 32);
$currentBits = (int)$this->bits[$bitsOffset];
// mask off lesser bits first
$currentBits &= ~((1 << ($from & 0x1F)) - 1);
while ($currentBits == 0) {
if (++$bitsOffset == count($this->bits)) {
return $this->size;
}
$currentBits = $this->bits[$bitsOffset];
}
$result = ($bitsOffset * 32) + numberOfTrailingZeros($currentBits); //numberOfTrailingZeros
return $result > $this->size ? $this->size : $result;
}
/**
* Efficient method to check if a range of bits is set, or not set.
*
* @param start $start of range, inclusive.
* @param end $end of range, exclusive
* @param if $value true, checks that bits in range are set, otherwise checks that they are not set
*
* @return true iff all bits are set or not set in range, according to value argument
* @throws InvalidArgumentException if end is less than or equal to start
*/
public function isRange($start, $end, $value)
{
if ($end < $start) {
throw new \InvalidArgumentException();
}
if ($end == $start) {
return true; // empty range matches
}
$end--; // will be easier to treat this as the last actually set bit -- inclusive
$firstInt = (int)($start / 32);
$lastInt = (int)($end / 32);
for ($i = $firstInt; $i <= $lastInt; $i++) {
$firstBit = $i > $firstInt ? 0 : $start & 0x1F;
$lastBit = $i < $lastInt ? 31 : $end & 0x1F;
$mask = 0;
if ($firstBit == 0 && $lastBit == 31) {
$mask = -1;
} else {
$mask = 0;
for ($j = $firstBit; $j <= $lastBit; $j++) {
$mask = ($mask | (1 << $j));
}
}
/**
* @param from index to start looking for unset bit
* @return index of next unset bit, or {@code size} if none are unset until the end
* @see #getNextSet(int)
*/
public function getNextUnset($from) {
if ($from >= $this->size) {
return $this->size;
}
$bitsOffset = intval($from / 32);
$currentBits = ~$this->bits[$bitsOffset];
// mask off lesser bits first
$currentBits &= ~((1 << ($from & 0x1F)) - 1);
while ($currentBits == 0) {
if (++$bitsOffset == count($this->bits)) {
return $this->size;
}
$currentBits = overflow32(~$this->bits[$bitsOffset]);
}
$result = ($bitsOffset * 32) + numberOfTrailingZeros($currentBits);
return $result > $this->size ? $this->size : $result;
}
// Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
// equals the mask, or we're looking for 0s and the masked portion is not all 0s
if (($this->bits[$i] & $mask) != ($value ? $mask : 0)) {
return false;
}
}
/**
* Sets a block of 32 bits, starting at bit i.
*
* @param i first bit to set
* @param newBits the new value of the next 32 bits. Note again that the least-significant bit
* corresponds to bit i, the next-least-significant to i+1, and so on.
*/
public function setBulk($i, $newBits) {
$this->bits[intval($i / 32)] = $newBits;
}
return true;
}
/**
* Sets a range of bits.
*
* @param start start of range, inclusive.
* @param end end of range, exclusive
*/
public function setRange($start, $end) {
if ($end < $start) {
throw new \InvalidArgumentException();
}
if ($end == $start) {
return;
}
$end--; // will be easier to treat this as the last actually set bit -- inclusive
$firstInt = intval($start / 32);
$lastInt = intval($end / 32);
for ($i = $firstInt; $i <= $lastInt; $i++) {
$firstBit = $i > $firstInt ? 0 : $start & 0x1F;
$lastBit = $i < $lastInt ? 31 : $end & 0x1F;
$mask = 0;
if ($firstBit == 0 && $lastBit == 31) {
$mask = -1;
} else {
$mask = 0;
for ($j = $firstBit; $j <= $lastBit; $j++) {
$mask |= 1 << $j;
}
}
$this->bits[$i] = overflow32($this->bits[$i]|$mask);
}
}
/**
* Appends the least-significant bits, from value, in order from most-significant to
* least-significant. For example, appending 6 bits from 0x000001E will append the bits
* 0, 1, 1, 1, 1, 0 in that order.
*
* @param $value {@code int} containing bits to append
* @param bits $numBits from value to append
*/
public function appendBits($value, $numBits)
{
if ($numBits < 0 || $numBits > 32) {
throw new \InvalidArgumentException("Num bits must be between 0 and 32");
}
$this->ensureCapacity($this->size + $numBits);
for ($numBitsLeft = $numBits; $numBitsLeft > 0; $numBitsLeft--) {
$this->appendBit((($value >> ($numBitsLeft - 1)) & 0x01) == 1);
}
}
/**
* Clears all bits (sets to false).
*/
public function clear() {
$max = count($this->bits);
for ($i = 0; $i < $max; $i++) {
$this->bits[$i] = 0;
}
}
private function ensureCapacity($size): void
{
if ($size > (is_countable($this->bits) ? count($this->bits) : 0) * 32) {
$newBits = self::makeArray($size);
$newBits = arraycopy($this->bits, 0, $newBits, 0, is_countable($this->bits) ? count($this->bits) : 0);
$this->bits = $newBits;
}
}
/**
* Efficient method to check if a range of bits is set, or not set.
*
* @param start start of range, inclusive.
* @param end end of range, exclusive
* @param value if true, checks that bits in range are set, otherwise checks that they are not set
* @return true iff all bits are set or not set in range, according to value argument
* @throws InvalidArgumentException if end is less than or equal to start
*/
public function isRange($start, $end, $value) {
if ($end < $start) {
throw new \InvalidArgumentException();
}
if ($end == $start) {
return true; // empty range matches
}
$end--; // will be easier to treat this as the last actually set bit -- inclusive
$firstInt = intval($start / 32);
$lastInt = intval($end / 32);
for ($i = $firstInt; $i <= $lastInt; $i++) {
$firstBit = $i > $firstInt ? 0 : $start & 0x1F;
$lastBit = $i < $lastInt ? 31 :$end & 0x1F;
$mask = 0;
if ($firstBit == 0 && $lastBit == 31) {
$mask = -1;
} else {
$mask = 0;
for ($j = $firstBit; $j <= $lastBit; $j++) {
$mask = overflow32($mask|(1 << $j));
}
}
public function appendBit($bit): void
{
$this->ensureCapacity($this->size + 1);
if ($bit) {
$this->bits[(int)($this->size / 32)] |= 1 << ($this->size & 0x1F);
}
$this->size++;
}
// Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
// equals the mask, or we're looking for 0s and the masked portion is not all 0s
if (($this->bits[$i] & $mask) != ($value ? $mask : 0)) {
return false;
}
}
return true;
}
public function appendBitArray($other): void
{
$otherSize = $other->size;
$this->ensureCapacity($this->size + $otherSize);
for ($i = 0; $i < $otherSize; $i++) {
$this->appendBit($other->get($i));
}
}
public function appendBit($bit) {
$this->ensureCapacity($this->size + 1);
if ($bit) {
$this->bits[intval($this->size / 32)] |= 1 << ($this->size & 0x1F);
}
$this->size++;
}
public function _xor($other)
{
if ((is_countable($this->bits) ? count($this->bits) : 0) !== (is_countable($other->bits) ? count($other->bits) : 0)) {
throw new \InvalidArgumentException("Sizes don't match");
}
$count = is_countable($this->bits) ? count($this->bits) : 0;
for ($i = 0; $i < $count; $i++) {
// The last byte could be incomplete (i.e. not have 8 bits in
// it) but there is no problem since 0 XOR 0 == 0.
$this->bits[$i] ^= $other->bits[$i];
}
}
/**
* Appends the least-significant bits, from value, in order from most-significant to
* least-significant. For example, appending 6 bits from 0x000001E will append the bits
* 0, 1, 1, 1, 1, 0 in that order.
*
* @param value {@code int} containing bits to append
* @param numBits bits from value to append
*/
public function appendBits($value, $numBits) {
if ($numBits < 0 || $numBits > 32) {
throw new \InvalidArgumentException("Num bits must be between 0 and 32");
}
$this->ensureCapacity($this->size + $numBits);
for ($numBitsLeft = $numBits; $numBitsLeft > 0; $numBitsLeft--) {
$this->appendBit((($value >> ($numBitsLeft - 1)) & 0x01) == 1);
}
}
/**
*
* @param first $bitOffset bit to start writing
* @param array $array to write into. Bytes are written most-significant byte first. This is the opposite
* of the internal representation, which is exposed by {@link #getBitArray()}
* @param position $offset in array to start writing
* @param how $numBytes many bytes to write
*/
public function toBytes($bitOffset, &$array, $offset, $numBytes): void
{
for ($i = 0; $i < $numBytes; $i++) {
$theByte = 0;
for ($j = 0; $j < 8; $j++) {
if ($this->get($bitOffset)) {
$theByte |= 1 << (7 - $j);
}
$bitOffset++;
}
$array[(int)($offset + $i)] = $theByte;
}
}
public function appendBitArray($other) {
$otherSize = $other->size;
$this->ensureCapacity($this->size + $otherSize);
for ($i = 0; $i < $otherSize; $i++) {
$this->appendBit($other->get($i));
}
}
/**
* @param $i ; bit to get
*
* @return true iff bit i is set
*/
public function get($i)
{
$key = (int)($i / 32);
public function _xor($other) {
if (count($this->bits) != count($other->bits)) {
throw new \InvalidArgumentException("Sizes don't match");
}
for ($i = 0; $i < count($this->bits); $i++) {
// The last byte could be incomplete (i.e. not have 8 bits in
// it) but there is no problem since 0 XOR 0 == 0.
$this->bits[$i] ^= $other->bits[$i];
}
}
return ($this->bits[$key] & (1 << ($i & 0x1F))) != 0;
}
/**
*
* @param bitOffset first bit to start writing
* @param array array to write into. Bytes are written most-significant byte first. This is the opposite
* of the internal representation, which is exposed by {@link #getBitArray()}
* @param offset position in array to start writing
* @param numBytes how many bytes to write
*/
public function toBytes($bitOffset, &$array, $offset, $numBytes) {
for ($i = 0; $i < $numBytes; $i++) {
$theByte = 0;
for ($j = 0; $j < 8; $j++) {
if ($this->get($bitOffset)) {
$theByte |= 1 << (7 - $j);
}
$bitOffset++;
}
$array[(int)($offset + $i)] = $theByte;
}
}
/**
* @return array underlying array of ints. The first element holds the first 32 bits, and the least
* significant bit is bit 0.
*/
public function getBitArray()
{
return $this->bits;
}
/**
* @return underlying array of ints. The first element holds the first 32 bits, and the least
* significant bit is bit 0.
*/
public function getBitArray() {
return $this->bits;
}
/**
* Reverses all bits in the array.
*/
public function reverse(): void
{
$newBits = [];
// reverse all int's first
$len = (($this->size - 1) / 32);
$oldBitsLen = $len + 1;
for ($i = 0; $i < $oldBitsLen; $i++) {
$x = $this->bits[$i];/*
$x = (($x >> 1) & 0x55555555L) | (($x & 0x55555555L) << 1);
$x = (($x >> 2) & 0x33333333L) | (($x & 0x33333333L) << 2);
$x = (($x >> 4) & 0x0f0f0f0fL) | (($x & 0x0f0f0f0fL) << 4);
$x = (($x >> 8) & 0x00ff00ffL) | (($x & 0x00ff00ffL) << 8);
$x = (($x >> 16) & 0x0000ffffL) | (($x & 0x0000ffffL) << 16);*/
$x = (($x >> 1) & 0x55555555) | (($x & 0x55555555) << 1);
$x = (($x >> 2) & 0x33333333) | (($x & 0x33333333) << 2);
$x = (($x >> 4) & 0x0f0f0f0f) | (($x & 0x0f0f0f0f) << 4);
$x = (($x >> 8) & 0x00ff00ff) | (($x & 0x00ff00ff) << 8);
$x = (($x >> 16) & 0x0000ffff) | (($x & 0x0000ffff) << 16);
$newBits[(int)$len - $i] = (int)$x;
}
// now correct the int's if the bit size isn't a multiple of 32
if ($this->size != $oldBitsLen * 32) {
$leftOffset = $oldBitsLen * 32 - $this->size;
$mask = 1;
for ($i = 0; $i < 31 - $leftOffset; $i++) {
$mask = ($mask << 1) | 1;
}
$currentInt = ($newBits[0] >> $leftOffset) & $mask;
for ($i = 1; $i < $oldBitsLen; $i++) {
$nextInt = $newBits[$i];
$currentInt |= $nextInt << (32 - $leftOffset);
$newBits[(int)($i) - 1] = $currentInt;
$currentInt = ($nextInt >> $leftOffset) & $mask;
}
$newBits[(int)($oldBitsLen) - 1] = $currentInt;
}
// $bits = $newBits;
}
/**
* Reverses all bits in the array.
*/
public function reverse() {
$newBits = array();
// reverse all int's first
$len = (($this->size-1) / 32);
$oldBitsLen = $len + 1;
for ($i = 0; $i < $oldBitsLen; $i++) {
$x = $this->bits[$i];/*
$x = (($x >> 1) & 0x55555555L) | (($x & 0x55555555L) << 1);
$x = (($x >> 2) & 0x33333333L) | (($x & 0x33333333L) << 2);
$x = (($x >> 4) & 0x0f0f0f0fL) | (($x & 0x0f0f0f0fL) << 4);
$x = (($x >> 8) & 0x00ff00ffL) | (($x & 0x00ff00ffL) << 8);
$x = (($x >> 16) & 0x0000ffffL) | (($x & 0x0000ffffL) << 16);*/
$x = (($x >> 1) & 0x55555555) | (($x & 0x55555555) << 1);
$x = (($x >> 2) & 0x33333333) | (($x & 0x33333333) << 2);
$x = (($x >> 4) & 0x0f0f0f0f) | (($x & 0x0f0f0f0f) << 4);
$x = (($x >> 8) & 0x00ff00ff) | (($x & 0x00ff00ff) << 8);
$x = (($x >> 16) & 0x0000ffff) | (($x & 0x0000ffff) << 16);
$newBits[(int)$len - $i] = (int) $x;
}
// now correct the int's if the bit size isn't a multiple of 32
if ($this->size != $oldBitsLen * 32) {
$leftOffset = $oldBitsLen * 32 - $this->size;
$mask = 1;
for ($i = 0; $i < 31 - $leftOffset; $i++) {
$mask = ($mask << 1) | 1;
}
$currentInt = ($newBits[0] >> $leftOffset) & $mask;
for ($i = 1; $i < $oldBitsLen; $i++) {
$nextInt = $newBits[$i];
$currentInt |= $nextInt << (32 - $leftOffset);
$newBits[intval($i) - 1] = $currentInt;
$currentInt = ($nextInt >> $leftOffset) & $mask;
}
$newBits[intval($oldBitsLen) - 1] = $currentInt;
}
$bits = $newBits;
}
public function equals($o)
{
if (!($o instanceof BitArray)) {
return false;
}
$other = $o;
private static function makeArray($size) {
return array();
}
return $this->size == $other->size && $this->bits === $other->bits;
}
// @Override
public function equals($o) {
if (!($o instanceof BitArray)) {
return false;
}
$other = $o;
return $this->size == $other->size && $this->bits===$other->bits;
}
public function hashCode()
{
return 31 * $this->size + hashCode($this->bits);
}
//@Override
public function hashCode() {
return 31 * $this->size +hashCode($this->bits);
}
public function toString()
{
$result = '';
for ($i = 0; $i < $this->size; $i++) {
if (($i & 0x07) == 0) {
$result .= ' ';
}
$result .= ($this->get($i) ? 'X' : '.');
}
// @Override
public function toString() {
$result = '';
for ($i = 0; $i < $this->size; $i++) {
if (($i & 0x07) == 0) {
$result.=' ';
}
$result.= ($this->get($i) ? 'X' : '.');
}
return (string) $result;
}
return (string)$result;
}
// @Override
public function _clone() {
return new BitArray($this->bits, $this->size);
}
}
public function _clone(): \Zxing\Common\BitArray
{
return new BitArray($this->bits, $this->size);
}
}