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