-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_stream.cpp
More file actions
71 lines (62 loc) · 1.52 KB
/
bit_stream.cpp
File metadata and controls
71 lines (62 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "bit_stream.h"
#include <algorithm>
#include <climits>
#include <iostream>
#include <iterator>
BitReader::BitReader(std::istream& is) : is_(is) {
}
void BitReader::BufferFill() {
if (buffer_pos_ != buffer_size_) {
return;
}
if (Eof()) {
buffer_pos_ = 0;
buffer_size_ = 0;
return;
}
is_.read(buffer_, BUFFER_CAPACITY);
buffer_size_ = is_.gcount();
buffer_pos_ = 0;
}
bool BitReader::Eof() const {
return buffer_pos_ == buffer_size_ && is_.eof();
}
bool BitReader::ReadBit() {
if (Eof()) {
throw EndOfFile();
}
if (buffer_pos_ == buffer_size_) {
BufferFill();
}
int bit_position = CHAR_BIT - current_bit_ - 1;
bool bit = (buffer_[buffer_pos_] >> bit_position) & 1;
++current_bit_;
if (current_bit_ == CHAR_BIT) {
current_bit_ = 0;
++buffer_pos_;
}
return bit;
}
BitWriter::BitWriter(std::ostream& os) : os_(os) {
}
BitWriter::~BitWriter() {
BufferFlush();
}
void BitWriter::BufferFlush() {
os_.write(buffer_, buffer_pos_ + (current_bit_ > 0));
buffer_pos_ = 0;
current_bit_ = 0;
std::fill(std::begin(buffer_), std::end(buffer_), 0);
}
void BitWriter::WriteBit(bool val) {
if (buffer_pos_ == BUFFER_SIZE) {
BufferFlush();
}
int8_t bit_position = CHAR_BIT - current_bit_ - 1;
buffer_[buffer_pos_] |= static_cast<char>(val << bit_position); // NOLINT
++current_bit_;
if (current_bit_ == CHAR_BIT) {
current_bit_ = 0;
++buffer_pos_;
}
}