-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeWriter.cpp
57 lines (44 loc) · 1.09 KB
/
CodeWriter.cpp
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
#include "CodeWriter.h"
CodeWriter::CodeWriter(std::ostream &os): os(os), bits(CHAR_BIT + 1) {
}
CodeWriter::~CodeWriter() {
Write(static_cast<CodeType> (MetaCode::Eof));
// write the incomplete leftover byte as-is
if (lo.used != 0)
os.put(static_cast<char> (lo.data));
}
std::size_t CodeWriter::GetBits() const {
return bits;
}
void CodeWriter::ResetBits() {
bits = CHAR_BIT + 1;
}
void CodeWriter::IncreaseBits() {
++bits;
}
bool CodeWriter::Write(CodeType k) {
std::size_t remaining_bits {bits};
if (lo.used != 0)
{
lo.data |= k << lo.used;
os.put(static_cast<char> (lo.data));
k >>= CHAR_BIT - lo.used;
remaining_bits -= CHAR_BIT - lo.used;
lo.used = 0;
lo.data = 0x00;
}
while (remaining_bits != 0)
if (remaining_bits >= CHAR_BIT)
{
os.put(static_cast<char> (k));
k >>= CHAR_BIT;
remaining_bits -= CHAR_BIT;
}
else
{
lo.used = remaining_bits;
lo.data = k;
break;
}
return true;
}