Skip to content

Commit ba60683

Browse files
committed
Correct foreach and count but due to complement issue
1 parent 0c4c5a9 commit ba60683

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
language: php
22

33
php:
4-
- 5.4
54
- 5.5
5+
- 7.0
66

77
before_install:
88
- composer self-update

examples/BitArray.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717

1818
use chdemko\BitArray\BitArray;
1919

20-
$bits = BitArray::fromString('10010');
21-
2220
// Print 10010
21+
$bits = BitArray::fromString('10010');
2322
echo $bits . PHP_EOL;
2423

2524
// Print 01101
2625
$bits->applyComplement();
2726
echo $bits . PHP_EOL;
2827

2928
// Print 11100
30-
$bits->applyXor(BitArray::fromIterable([true, false, false, false, true]));
29+
$bits->applyXor(BitArray::fromTraversable([true, false, false, false, true]));
3130
echo $bits . PHP_EOL;
3231

3332
// Print 11101

src/BitArray/BitArray.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ class BitArray implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSer
4545
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
4646
];
4747

48+
/**
49+
* @var integer[] Mask for restricting complements
50+
*/
51+
private static $restrict = [
52+
1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 7,
53+
1 << 0,
54+
1 << 0 | 1 << 1,
55+
1 << 0 | 1 << 1 | 1 << 2,
56+
1 << 0 | 1 << 1 | 1 << 2 | 1 << 3,
57+
1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4,
58+
1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5,
59+
1 << 0 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5 | 1 << 6,
60+
];
61+
4862
/**
4963
* @var string Underlying data
5064
*
@@ -123,13 +137,10 @@ public function __get($property)
123137
{
124138
case 'size':
125139
return $this->size;
126-
break;
127140
case 'count':
128141
return $this->count();
129-
break;
130142
default:
131143
throw new \RuntimeException('Undefined property');
132-
break;
133144
}
134145
}
135146

@@ -400,6 +411,12 @@ public function applyComplement()
400411
$this->data[$i] = chr(~ ord($this->data[$i]));
401412
}
402413

414+
// Remove useless bits
415+
if ($length > 0)
416+
{
417+
$this->data[$length - 1] = chr(ord($this->data[$length - 1]) & self::$restrict[$this->size % 8]);
418+
}
419+
403420
return $this;
404421
}
405422

src/BitArray/Iterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,6 @@ public function next()
107107
*/
108108
public function valid()
109109
{
110-
return $this->index < count($this->bits);
110+
return $this->index < $this->bits->size;
111111
}
112112
}

tests/BitArray/BitArrayTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,14 @@ public function test_applyComplement()
496496
'01100',
497497
(string) $bits->applyComplement()
498498
);
499+
$this->assertEquals(
500+
2,
501+
count($bits)
502+
);
503+
$this->assertEquals(
504+
5,
505+
$bits->size
506+
);
499507
}
500508

501509
/**

0 commit comments

Comments
 (0)