-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency_table.cpp
More file actions
95 lines (85 loc) · 3.01 KB
/
frequency_table.cpp
File metadata and controls
95 lines (85 loc) · 3.01 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "compresskit/frequency_table.hpp"
#include <array>
#include <limits>
namespace compresskit {
namespace {
bool write_u32_le(std::ostream& out, uint32_t value) {
const std::array<char, 4> bytes = {
static_cast<char>(value & 0xFFu),
static_cast<char>((value >> 8) & 0xFFu),
static_cast<char>((value >> 16) & 0xFFu),
static_cast<char>((value >> 24) & 0xFFu),
};
out.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
return static_cast<bool>(out);
}
bool read_u32_le(std::istream& in, uint32_t& value) {
std::array<unsigned char, 4> bytes{};
in.read(reinterpret_cast<char*>(bytes.data()), static_cast<std::streamsize>(bytes.size()));
if (!in) {
return false;
}
value = static_cast<uint32_t>(bytes[0]) | (static_cast<uint32_t>(bytes[1]) << 8) |
(static_cast<uint32_t>(bytes[2]) << 16) | (static_cast<uint32_t>(bytes[3]) << 24);
return true;
}
} // namespace
bool write_frequency_table(std::ostream& out, const std::vector<uint32_t>& freq) {
if (!write_u32_le(out, static_cast<uint32_t>(freq.size()))) {
return false;
}
for (uint32_t value : freq) {
if (!write_u32_le(out, value)) {
return false;
}
}
return true;
}
FrequencyTableReadStatus read_frequency_table(std::istream& in, std::vector<uint32_t>& freq,
uint32_t expected_count, uint32_t* actual_count) {
uint32_t count = 0;
if (!read_u32_le(in, count)) {
freq.clear();
return FrequencyTableReadStatus::TRUNCATED;
}
if (actual_count) {
*actual_count = count;
}
if (expected_count != 0 && count != expected_count) {
freq.clear();
return FrequencyTableReadStatus::BAD_COUNT;
}
freq.assign(count, 0);
for (uint32_t& value : freq) {
if (!read_u32_le(in, value)) {
freq.clear();
return FrequencyTableReadStatus::TRUNCATED;
}
}
return FrequencyTableReadStatus::OK;
}
FrequencyCountStatus accumulate_frequencies(std::istream& in, std::vector<uint32_t>& freq,
uint32_t* overflow_symbol) {
std::array<unsigned char, 32 * 1024> buffer{};
for (;;) {
in.read(reinterpret_cast<char*>(buffer.data()), static_cast<std::streamsize>(buffer.size()));
const std::streamsize read_count = in.gcount();
for (std::streamsize i = 0; i < read_count; ++i) {
const uint32_t symbol = static_cast<uint32_t>(buffer[static_cast<std::size_t>(i)]);
if (freq[symbol] == std::numeric_limits<uint32_t>::max()) {
if (overflow_symbol) {
*overflow_symbol = symbol;
}
return FrequencyCountStatus::OVERFLOW;
}
++freq[symbol];
}
if (in.eof()) {
return FrequencyCountStatus::OK;
}
if (!in) {
return FrequencyCountStatus::IO_ERROR;
}
}
}
} // namespace compresskit