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
|
|
|
* @inheritDoc |
46
|
|
|
*/ |
47
|
|
|
public static function toBitmap($bitmap): BitmapInterface |
48
|
|
|
{ |
49
|
|
|
return ($bitmap instanceof BitmapInterface) ? $bitmap : static::create($bitmap); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Bitmap constructor. |
54
|
|
|
* @param int $value bitmap value |
55
|
|
|
*/ |
56
|
|
|
final public function __construct(int $value) |
57
|
|
|
{ |
58
|
|
|
$this->value = $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
|
|
public function getValue(): int |
65
|
|
|
{ |
66
|
|
|
return $this->value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
|
|
public function intersectsWith($bitmap): bool |
73
|
|
|
{ |
74
|
|
|
$bitmap = static::toBitmap($bitmap); |
75
|
|
|
return ($this->getValue() & $bitmap->getValue()) !== 0; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function includes($bitmap): bool |
82
|
|
|
{ |
83
|
|
|
$bitmap = static::toBitmap($bitmap); |
84
|
|
|
return ($this->getValue() & $bitmap->getValue()) === $bitmap->getValue(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
|
|
public function isEqualWith($bitmap): bool |
91
|
|
|
{ |
92
|
|
|
$bitmap = static::toBitmap($bitmap); |
93
|
|
|
return $this->getValue() === $bitmap->getValue(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function add($bitmap): BitmapInterface |
100
|
|
|
{ |
101
|
|
|
$bitmap = static::toBitmap($bitmap); |
102
|
|
|
return new static($this->getValue() | $bitmap->getValue()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public function sub($bitmap): BitmapInterface |
109
|
|
|
{ |
110
|
|
|
$bitmap = static::toBitmap($bitmap); |
111
|
|
|
return new static($this->getValue() & (~$bitmap->getValue())); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritDoc |
116
|
|
|
*/ |
117
|
|
|
public function hasBit(int $bitPosition): bool |
118
|
|
|
{ |
119
|
|
|
return $this->intersectsWith(Bitmap::createFromArray([$bitPosition])); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritDoc |
124
|
|
|
*/ |
125
|
|
|
public function toArray(): array |
126
|
|
|
{ |
127
|
|
|
$bitmap = $this->getValue(); |
128
|
|
|
$result = []; |
129
|
|
|
$i = 0; |
130
|
|
|
|
131
|
|
|
while($bitmap) { |
132
|
|
|
if($bitmap % 2) { |
133
|
|
|
$result[] = $i; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$bitmap = (int)($bitmap / 2); |
137
|
|
|
++$i; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $result; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|