Total Complexity | 8 |
Total Lines | 67 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
20 | final class BitBuffer{ |
||
21 | |||
22 | /** |
||
23 | * The buffer content |
||
24 | * |
||
25 | * @var int[] |
||
26 | */ |
||
27 | protected array $buffer = []; |
||
28 | |||
29 | /** |
||
30 | * Length of the content (bits) |
||
31 | */ |
||
32 | protected int $length = 0; |
||
33 | |||
34 | /** |
||
35 | * clears the buffer |
||
36 | */ |
||
37 | public function clear():BitBuffer{ |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * appends a sequence of bits |
||
46 | */ |
||
47 | public function put(int $num, int $length):BitBuffer{ |
||
48 | |||
49 | for($i = 0; $i < $length; $i++){ |
||
50 | $this->putBit((($num >> ($length - $i - 1)) & 1) === 1); |
||
51 | } |
||
52 | |||
53 | return $this; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * appends a single bit |
||
58 | */ |
||
59 | public function putBit(bool $bit):BitBuffer{ |
||
60 | $bufIndex = floor($this->length / 8); |
||
61 | |||
62 | if(count($this->buffer) <= $bufIndex){ |
||
63 | $this->buffer[] = 0; |
||
64 | } |
||
65 | |||
66 | if($bit === true){ |
||
67 | $this->buffer[(int)$bufIndex] |= (0x80 >> ($this->length % 8)); |
||
68 | } |
||
69 | |||
70 | $this->length++; |
||
71 | |||
72 | return $this; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * returns the current buffer length |
||
77 | */ |
||
78 | public function getLength():int{ |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * returns the buffer content |
||
84 | */ |
||
85 | public function getBuffer():array{ |
||
87 | } |
||
88 | |||
90 |