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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
#include <array>
#include <cstdint>
#include <iostream>
#include <vector>
#include <memory>
#include <string>
#include <algorithm>
#include <bitset>
#include "huffman.hpp"
#include <arpa/inet.h>
huffman_tree::huffman_tree(const std::string& value)
: _value(value), _left(nullptr), _right(nullptr)
{
}
huffman_tree::huffman_tree(std::shared_ptr<huffman_tree> left, std::shared_ptr<huffman_tree> right)
: _value(""), _left(left), _right(right)
{
}
std::uint8_t huffman_tree::value() const
{
return _value.front();
}
std::shared_ptr<huffman_tree> huffman_tree::left() const
{
return _left;
}
std::shared_ptr<huffman_tree> huffman_tree::right() const
{
return _right;
}
bool huffman_tree::is_leaf() const
{
return !(this->left() || this->right());
}
huffman_probability::huffman_probability(const std::string& bytes, double probability)
: bytes(bytes), probability(probability), tree(std::make_shared<huffman_tree>(bytes))
{
}
huffman_probability huffman_probability::operator+(const huffman_probability& that) const
{
huffman_probability result{ this->bytes + that.bytes, this->probability + that.probability };
result.tree = std::make_shared<huffman_tree>(this->tree, that.tree);
return result;
}
bool huffman_probability::operator<(const huffman_probability& that) const
{
return this->probability < that.probability;
}
bool huffman_probability::operator>(const huffman_probability& that) const
{
return this->probability > that.probability;
}
bool huffman_probability::operator<=(const huffman_probability& that) const
{
return this->probability <= that.probability;
}
bool huffman_probability::operator>=(const huffman_probability& that) const
{
return this->probability >= that.probability;
}
std::pair<std::bitset<8>, std::size_t> encode_coding(const coding& input, std::ostream& output,
std::bitset<8> bitset, std::size_t index)
{
for (const bool bit: input)
{
bitset[index] = bit;
if (index == 7)
{
output.put(static_cast<std::ostream::char_type>(bitset.to_ulong()));
index = 0;
bitset = std::bitset<8>();
}
else
{
++index;
}
}
return { bitset, index };
}
void huffman_table::create_table(std::shared_ptr<huffman_tree> tree, coding path)
{
if (tree->is_leaf())
{
insert(tree->value(), std::move(path));
}
else
{
coding new_path;
new_path = path;
new_path.push_back(false);
create_table(tree->left(), std::move(new_path));
new_path = path;
new_path.push_back(true);
create_table(tree->right(), std::move(new_path));
}
}
huffman_table::huffman_table(std::shared_ptr<huffman_tree> tree)
{
create_table(tree, coding());
}
huffman_table::huffman_table(std::vector<std::uint8_t>::const_iterator& coding_stream)
{
std::size_t table_size = *coding_stream;
std::advance(coding_stream, 1);
for (std::uint8_t i = 0; i < table_size; ++i)
{
auto coded_symbol = static_cast<std::byte>(*coding_stream);
std::advance(coding_stream, 1);
std::uint8_t bits_in_coding = *coding_stream;
std::advance(coding_stream, 1);
coding current_coding;
std::size_t current_bit{ 0 };
for (std::uint8_t j = 0; j < bits_in_coding; ++j)
{
std::bitset<8> current_bitset{ *coding_stream };
current_coding.push_back(current_bitset[current_bit]);
if (current_bit == 7)
{
current_bit = 0;
std::advance(coding_stream, 1);
}
else
{
++current_bit;
}
}
if (current_bit != 0)
{
std::advance(coding_stream, 1);
}
this->payload.insert({ coded_symbol, current_coding });
}
}
void huffman_table::insert(std::uint8_t byte, coding&& path)
{
this->payload.insert({ static_cast<std::byte>(byte), std::move(path) });
}
const coding& huffman_table::operator[](std::uint8_t byte) const
{
return this->payload.at(static_cast<std::byte>(byte));
}
std::size_t huffman_table::size() const
{
return this->payload.size();
}
void huffman_table::encode(std::ostream& output) const
{
// The first byte is the table size.
output.put(static_cast<std::ostream::char_type>(size()));
for (const std::pair<std::byte, coding>& entry: this->payload)
{
// For each entry write the byte encoded.
output.put(static_cast<std::ostream::char_type>(entry.first));
output.put(static_cast<std::ostream::char_type>(entry.second.size()));
auto rest = encode_coding(entry.second, output, {}, 0);
if (rest.second > 0)
{
output << static_cast<std::ostream::char_type>(rest.first.to_ulong());
}
}
}
std::unordered_map<std::byte, coding>::iterator huffman_table::begin()
{
return this->payload.begin();
}
std::unordered_map<std::byte, coding>::iterator huffman_table::end()
{
return this->payload.end();
}
std::unordered_map<std::byte, coding>::const_iterator huffman_table::begin() const
{
return this->payload.cbegin();
}
std::unordered_map<std::byte, coding>::const_iterator huffman_table::end() const
{
return this->payload.cend();
}
std::shared_ptr<huffman_tree> create_tree(probability_list& probabilities)
{
while (probabilities.size() > 1)
{
std::sort(std::begin(probabilities), std::end(probabilities), std::greater());
auto last = probabilities.back();
probabilities.pop_back();
probabilities.back() = probabilities.back() + last;
}
return probabilities.front().tree;
}
probability_list adapt_probabilities(const std::vector<std::uint8_t>& input)
{
std::array<std::size_t, 256> counts = {};
probability_list result;
for (auto byte: input)
{
++counts[byte];
}
for (std::array<std::size_t, 256>::const_iterator count = std::cbegin(counts); count != std::cend(counts); ++count)
{
if (*count > 0)
{
result.push_back({
std::string(1, static_cast<char>(std::distance(std::cbegin(counts), count))),
*count / 256.0
});
}
}
return result;
}
void encode(const huffman_table& table, const std::vector<std::uint8_t>& input, std::ostream& output)
{
std::pair<std::bitset<8>, std::size_t> rest{ {}, 0 };
auto input_size = htonl(input.size());
// Write the number of elements.
output.write(reinterpret_cast<const char *>(&input_size), 4);
table.encode(output);
for (const std::uint8_t character: input)
{
rest = encode_coding(table[character], output, rest.first, rest.second);
}
if (rest.second > 0)
{
output << static_cast<std::ostream::char_type>(rest.first.to_ulong());
}
}
void compress(const std::vector<std::uint8_t>& input, std::ostream& output)
{
probability_list probabilities = adapt_probabilities(input);
std::shared_ptr<huffman_tree> tree = create_tree(probabilities);
huffman_table table{ tree };
encode(table, input, output);
}
void decompress(const std::vector<std::uint8_t>& input, std::ostream& output)
{
auto input_size = ntohl(*reinterpret_cast<const std::uint32_t *>(input.data()));
auto table_iterator = std::cbegin(input) + 4;
huffman_table table{ table_iterator };
std::unordered_map<coding, std::byte> reverse_table;
for (auto entry: table)
{
reverse_table.insert({ entry.second, entry.first });
}
std::vector<bool> bit_input;
std::size_t element_count{ 0 };
while (table_iterator != std::cend(input))
{
std::bitset<8> current_bitset{ *table_iterator };
for (auto j = 0; j < current_bitset.size(); ++j)
{
bit_input.push_back(current_bitset[j]);
auto lookup_result = reverse_table.find(bit_input);
if (lookup_result != std::cend(reverse_table))
{
output << static_cast<unsigned char>(lookup_result->second);
bit_input.clear();
if (++element_count == input_size)
{
break;
}
}
}
std::advance(table_iterator, 1);
}
}
|