schemator-php

Форк
0
141 строка · 2.9 Кб
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Smoren\Schemator\Structs;
6

7
use Smoren\Schemator\Interfaces\BitmapInterface;
8

9
class Bitmap implements BitmapInterface
10
{
11
    /**
12
     * @var int bitmap value
13
     */
14
    protected int $value;
15

16
    /**
17
     * {@inheritDoc}
18
     */
19
    public static function create($value): BitmapInterface
20
    {
21
        if (is_array($value)) {
22
            return static::createFromArray($value);
23
        }
24
        if ($value instanceof BitmapInterface) {
25
            return new static($value->getValue());
26
        }
27
        return new static($value);
28
    }
29

30
    /**
31
     * {@inheritDoc}
32
     */
33
    public static function createFromArray(array $bits): BitmapInterface
34
    {
35
        $value = 0;
36
        foreach ($bits as $code) {
37
            $value += 2 ** $code;
38
        }
39
        return new static($value);
40
    }
41

42
    /**
43
     * {@inheritDoc}
44
     */
45
    public static function toBitmap($bitmap): BitmapInterface
46
    {
47
        return ($bitmap instanceof BitmapInterface) ? $bitmap : static::create($bitmap);
48
    }
49

50
    /**
51
     * Bitmap constructor.
52
     *
53
     * @param int $value bitmap value
54
     */
55
    final public function __construct(int $value)
56
    {
57
        $this->value = $value;
58
    }
59

60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function getValue(): int
64
    {
65
        return $this->value;
66
    }
67

68
    /**
69
     * {@inheritDoc}
70
     */
71
    public function intersectsWith($bitmap): bool
72
    {
73
        $bitmap = static::toBitmap($bitmap);
74
        return ($this->getValue() & $bitmap->getValue()) !== 0;
75
    }
76

77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function includes($bitmap): bool
81
    {
82
        $bitmap = static::toBitmap($bitmap);
83
        return ($this->getValue() & $bitmap->getValue()) === $bitmap->getValue();
84
    }
85

86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function isEqualWith($bitmap): bool
90
    {
91
        $bitmap = static::toBitmap($bitmap);
92
        return $this->getValue() === $bitmap->getValue();
93
    }
94

95
    /**
96
     * {@inheritDoc}
97
     */
98
    public function add($bitmap): BitmapInterface
99
    {
100
        $bitmap = static::toBitmap($bitmap);
101
        return new static($this->getValue() | $bitmap->getValue());
102
    }
103

104
    /**
105
     * {@inheritDoc}
106
     */
107
    public function sub($bitmap): BitmapInterface
108
    {
109
        $bitmap = static::toBitmap($bitmap);
110
        return new static($this->getValue() & (~$bitmap->getValue()));
111
    }
112

113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function hasBit(int $bitPosition): bool
117
    {
118
        return $this->intersectsWith(Bitmap::createFromArray([$bitPosition]));
119
    }
120

121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function toArray(): array
125
    {
126
        $bitmap = $this->getValue();
127
        $result = [];
128
        $i = 0;
129

130
        while ($bitmap) {
131
            if ($bitmap % 2) {
132
                $result[] = $i;
133
            }
134

135
            $bitmap = (int)($bitmap / 2);
136
            ++$i;
137
        }
138

139
        return $result;
140
    }
141
}
142

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.