Passed
Push — master ( 8c9cfe...19df46 )
by Smoren
01:38
created

Bitmap::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Smoren\BitmapTools\Models;
4
5
use Smoren\BitmapTools\Interfaces\BitmapInterface;
6
7
/**
8
 * Bitmap class
9
 * @author <[email protected]> Smoren
10
 */
11
class Bitmap implements BitmapInterface
12
{
13
    /**
14
     * @var int bitmap value
15
     */
16
    protected int $value;
17
18
    /**
19
     * @inheritDoc
20
     */
21
    public static function create($value): BitmapInterface
22
    {
23
        if(is_array($value)) {
24
            return static::createFromArray($value);
25
        }
26
        if($value instanceof BitmapInterface) {
27
            return new static($value->getValue());
28
        }
29
        return new static($value);
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35
    public static function createFromArray(array $bits): BitmapInterface
36
    {
37
        $value = 0;
38
        foreach($bits as $code) {
39
            $value += 2**$code;
40
        }
41
        return new static($value);
42
    }
43
44
    /**
45
     * Bitmap constructor.
46
     * @param int $value bitmap value
47
     */
48
    final public function __construct(int $value)
49
    {
50
        $this->value = $value;
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function getValue(): int
57
    {
58
        return $this->value;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function intersectsWith(BitmapInterface $bitmap): bool
65
    {
66
        return ($this->getValue() & $bitmap->getValue()) !== 0;
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function includes(BitmapInterface $bitmap): bool
73
    {
74
        return ($this->getValue() & $bitmap->getValue()) === $bitmap->getValue();
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function hasBit(int $bitPosition): bool
81
    {
82
        return $this->intersectsWith(Bitmap::createFromArray([$bitPosition]));
83
    }
84
85
    /**
86
     * @inheritDoc
87
     */
88
    public function isEqualWith(BitmapInterface $bitmap): bool
89
    {
90
        return $this->getValue() === $bitmap->getValue();
91
    }
92
93
    /**
94
     * @inheritDoc
95
     */
96
    public function add(BitmapInterface $bitmap): BitmapInterface
97
    {
98
        return new static($this->getValue() | $bitmap->getValue());
99
    }
100
101
    /**
102
     * @inheritDoc
103
     */
104
    public function sub(BitmapInterface $bitmap): BitmapInterface
105
    {
106
        return new static($this->getValue() & (~$bitmap->getValue()));
107
    }
108
109
    /**
110
     * @inheritDoc
111
     */
112
    public function toArray(): array
113
    {
114
        $bitmap = $this->getValue();
115
        $result = [];
116
        $i = 0;
117
118
        while($bitmap) {
119
            if($bitmap % 2) {
120
                $result[] = $i;
121
            }
122
123
            $bitmap = (int)($bitmap / 2);
124
            ++$i;
125
        }
126
127
        return $result;
128
    }
129
}
130