-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman.cpp
More file actions
379 lines (277 loc) · 12.1 KB
/
huffman.cpp
File metadata and controls
379 lines (277 loc) · 12.1 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "huffman.h"
const size_t Huffman::NUMBER_OF_BITS_IN_BYTE = 8;
const size_t Huffman::SYMBOLS_COUNT = (1 << 9);
const size_t Huffman::ALPHABET_SIZE = 9;
const int Huffman::FILENAME_END = 256;
const int Huffman::ONE_MORE_FILE = 257;
const int Huffman::ARCHIVE_END = 258;
Huffman::Huffman(const std::vector<size_t>& frequencies_of_alphabet) {
code_of_symbol = std::vector<std::vector<bool>>(SYMBOLS_COUNT);
Vertex* root = RunHuffman(frequencies_of_alphabet);
std::vector<std::vector<bool>> new_code_of_symbol = GetNewCodeOfSymbols(root);
size_t number_of_symbols = GetNumberOfSymbols(frequencies_of_alphabet);
order_of_symbols = std::vector<int>(SYMBOLS_COUNT);
std::iota(order_of_symbols.begin(), order_of_symbols.end(), 0);
std::sort(order_of_symbols.begin(), order_of_symbols.end(), [&](int a, int b) {
if (frequencies_of_alphabet[b] == 0) {
return frequencies_of_alphabet[a] != 0;
}
if (frequencies_of_alphabet[a] == 0) {
return false;
}
if (new_code_of_symbol[a].size() != new_code_of_symbol[b].size()) {
return new_code_of_symbol[a].size() < new_code_of_symbol[b].size();
}
return new_code_of_symbol[a] < new_code_of_symbol[b];
});
NormalizeCodeOfSymbols(frequencies_of_alphabet, new_code_of_symbol, number_of_symbols);
}
Huffman::Huffman(const char* file_name) {
Reader reader(file_name);
std::deque<bool>& bits_of_file = reader.bits_of_file_;
while (NUMBER_OF_BITS_IN_BYTE < bits_of_file.size()) {
size_t number_of_symbols = GetValueOfNextLengthBits(reader, bits_of_file, ALPHABET_SIZE);
reader.DeleteUselessBitsAtTheBeginning(ALPHABET_SIZE);
GetOrderOfSymbols(reader, bits_of_file, number_of_symbols);
GetCodeOfSymbols(reader, bits_of_file, number_of_symbols);
std::string next_file_name;
while (true) {
size_t next_length = FindLengthOfNextCode(reader, bits_of_file);
int next_value = FindValueOfNextCode(reader, bits_of_file);
if (next_value == FILENAME_END) {
break;
}
next_file_name += TransformIntToChar(next_value);
reader.DeleteUselessBitsAtTheBeginning(next_length);
}
{
size_t next_length = FindLengthOfNextCode(reader, bits_of_file);
int next_value = FindValueOfNextCode(reader, bits_of_file);
if (next_value != FILENAME_END) {
throw std::runtime_error("error - wrong data in archive file");
}
reader.DeleteUselessBitsAtTheBeginning(next_length);
}
Writer writer(next_file_name);
while (true) {
size_t next_length = FindLengthOfNextCode(reader, bits_of_file);
int next_value = FindValueOfNextCode(reader, bits_of_file);
if (next_value == ARCHIVE_END || next_value == ONE_MORE_FILE) {
break;
}
writer.WriteCharacter(TransformIntToChar(next_value));
reader.DeleteUselessBitsAtTheBeginning(next_length);
}
size_t next_length = FindLengthOfNextCode(reader, bits_of_file);
int next_value = FindValueOfNextCode(reader, bits_of_file);
if (next_value != ARCHIVE_END && next_value != ONE_MORE_FILE) {
throw std::runtime_error("error - wrong data in archive file");
}
if (next_length + NUMBER_OF_BITS_IN_BYTE >= bits_of_file.size()) {
if (next_value != ARCHIVE_END) {
throw std::runtime_error("error - wrong data in archive file");
}
} else {
if (next_value != ONE_MORE_FILE) {
throw std::runtime_error("error - wrong data in archive file");
}
}
reader.DeleteUselessBitsAtTheBeginning(next_length);
}
if (NUMBER_OF_BITS_IN_BYTE < bits_of_file.size()) {
throw std::runtime_error("error - wrong data in archive file");
}
}
Vertex* Huffman::RunHuffman(const std::vector<size_t>& frequencies_of_alphabet) const {
auto comparator_for_vertexes = [](const Vertex* a, const Vertex* b) {
return a->frequency_of_vertex > b->frequency_of_vertex;
};
std::priority_queue<Vertex*, std::vector<Vertex*>, decltype(comparator_for_vertexes)> queue(
comparator_for_vertexes);
for (size_t index = 0; index < SYMBOLS_COUNT; ++index) {
if (frequencies_of_alphabet[index] == 0) {
continue;
}
Vertex* ha = new Vertex();
ha->frequency_of_vertex = frequencies_of_alphabet[index];
ha->symbol_of_vertex = index;
queue.push(ha);
}
while (queue.size() > 1) {
Vertex* left_child = queue.top();
queue.pop();
Vertex* right_child = queue.top();
queue.pop();
Vertex* parent = new Vertex();
parent->frequency_of_vertex =
left_child->frequency_of_vertex + right_child->frequency_of_vertex;
parent->left_child = left_child;
parent->right_child = right_child;
queue.push(parent);
}
assert(queue.size() == 1);
return queue.top();
}
size_t Huffman::GetNumberOfSymbols(const std::vector<size_t>& frequencies_of_alphabet) const {
size_t number_of_symbols = 0;
for (auto frequency : frequencies_of_alphabet) {
if (frequency > 0) {
number_of_symbols++;
}
}
return number_of_symbols;
}
std::vector<std::vector<bool>> Huffman::GetNewCodeOfSymbols(Vertex* root) const {
std::vector<std::vector<bool>> new_code_of_symbol(SYMBOLS_COUNT);
std::function<void(Vertex*, std::vector<bool>&)> dfs = [&](Vertex* current,
std::vector<bool>& current_code) {
if (current->symbol_of_vertex != -1) {
new_code_of_symbol[current->symbol_of_vertex] = current_code;
return;
}
if (current->left_child != nullptr) {
current_code.push_back(false);
dfs(current->left_child, current_code);
current_code.pop_back();
}
if (current->right_child != nullptr) {
current_code.push_back(false);
dfs(current->right_child, current_code);
current_code.pop_back();
}
};
std::vector<bool> current_code;
dfs(root, current_code);
return new_code_of_symbol;
}
void Huffman::NormalizeCodeOfSymbols(const std::vector<size_t>& frequencies_of_alphabet,
const std::vector<std::vector<bool>>& new_code_of_symbol,
size_t number_of_symbols) {
std::vector<bool> now_code(new_code_of_symbol[order_of_symbols[0]].size());
code_of_symbol[order_of_symbols[0]] = now_code;
for (size_t index = 1; index < number_of_symbols; ++index) {
IncrementByOne(now_code);
while (now_code.size() < new_code_of_symbol[order_of_symbols[index]].size()) {
now_code.push_back(false);
}
code_of_symbol[order_of_symbols[index]] = now_code;
}
number_of_codes_with_size = std::vector<int>(now_code.size() + 1);
for (size_t symbol = 0; symbol < SYMBOLS_COUNT; ++symbol) {
if (frequencies_of_alphabet[symbol] == 0) {
continue;
}
number_of_codes_with_size[new_code_of_symbol[symbol].size()]++;
}
}
void Huffman::GetOrderOfSymbols(Reader& reader, std::deque<bool>& bits_of_file,
size_t number_of_symbols) {
order_of_symbols = std::vector<int>(number_of_symbols);
for (size_t index = 0; index < number_of_symbols; ++index) {
order_of_symbols[index] = GetValueOfNextLengthBits(reader, bits_of_file, ALPHABET_SIZE);
reader.DeleteUselessBitsAtTheBeginning(ALPHABET_SIZE);
}
}
void Huffman::GetCodeOfSymbols(Reader& reader, std::deque<bool>& bits_of_file,
size_t number_of_symbols) {
size_t total_number_of_symbols = 0;
number_of_codes_with_size.clear();
number_of_codes_with_size.push_back(0);
while (!bits_of_file.empty() && total_number_of_symbols < number_of_symbols) {
number_of_codes_with_size.push_back(
GetValueOfNextLengthBits(reader, bits_of_file, ALPHABET_SIZE));
reader.DeleteUselessBitsAtTheBeginning(ALPHABET_SIZE);
total_number_of_symbols += number_of_codes_with_size.back();
}
if (total_number_of_symbols < number_of_symbols) {
throw std::runtime_error("error - too few arguments in archive file");
}
if (total_number_of_symbols > number_of_symbols) {
throw std::runtime_error("error - too much arguments in archive file");
}
size_t current_size_of_file = 1;
while (number_of_codes_with_size[current_size_of_file] == 0) {
current_size_of_file++;
}
what_symbol_have_this_code.clear();
std::vector<bool> now_code(current_size_of_file);
code_of_symbol = std::vector<std::vector<bool>>(SYMBOLS_COUNT);
size_t current_symbol = 0;
max_symbol_code_size = number_of_codes_with_size.size() - 1;
if (order_of_symbols.empty()) {
throw std::runtime_error("error - wrong data in archive file");
}
code_of_symbol[order_of_symbols[current_symbol++]] = now_code;
number_of_codes_with_size[current_size_of_file]--;
while (current_symbol < number_of_symbols) {
IncrementByOne(now_code);
while (now_code.size() <= max_symbol_code_size &&
number_of_codes_with_size[current_size_of_file] == 0) {
now_code.push_back(false);
current_size_of_file++;
}
if (now_code.size() > max_symbol_code_size) {
throw std::runtime_error("error - wrong data in archive file");
}
code_of_symbol[order_of_symbols[current_symbol++]] = now_code;
number_of_codes_with_size[current_size_of_file]--;
}
for (size_t index = 0; index < number_of_symbols; ++index) {
what_symbol_have_this_code[code_of_symbol[order_of_symbols[index]]] =
order_of_symbols[index];
}
}
int Huffman::FindValueOfNextCode(Reader& reader, const std::deque<bool>& bits) {
reader.ReadNextBits();
std::vector<bool> current_code;
for (size_t length = 1; length <= bits.size() && length <= max_symbol_code_size; length++) {
current_code.push_back(bits[length - 1]);
if (what_symbol_have_this_code.find(current_code) != what_symbol_have_this_code.end()) {
return what_symbol_have_this_code[current_code];
}
}
throw std::runtime_error("error - wrong data in archive file");
}
size_t Huffman::FindLengthOfNextCode(Reader& reader, const std::deque<bool>& bits) {
reader.ReadNextBits();
std::vector<bool> current_code;
for (size_t length = 1; length <= bits.size() && length <= max_symbol_code_size; length++) {
current_code.push_back(bits[length - 1]);
if (what_symbol_have_this_code.find(current_code) != what_symbol_have_this_code.end()) {
return length;
}
}
throw std::runtime_error("error - wrong data in archive file");
}
int Huffman::GetValueOfNextLengthBits(Reader& reader, std::deque<bool>& bits, size_t length) const {
reader.ReadNextBits();
if (length <= bits.size()) {
int value = 0;
int power_of_two = 1;
for (size_t delta_index = 0; delta_index < length; ++delta_index) {
if (bits[delta_index]) {
value += power_of_two;
}
power_of_two *= 2;
}
return value;
}
throw std::runtime_error("error - wrong data in archive file");
}
char Huffman::TransformIntToChar(int value) const {
if (value == ONE_MORE_FILE || value == ARCHIVE_END || value == FILENAME_END) {
throw std::runtime_error("error - wrong data in archive file");
}
return static_cast<char>(value);
}
void Huffman::IncrementByOne(std::vector<bool>& vec) const {
for (size_t index = 0; index < vec.size(); index++) {
if (vec[vec.size() - 1 - index]) {
vec[vec.size() - 1 - index] = false;
} else {
vec[vec.size() - 1 - index] = true;
return;
}
}
throw std::runtime_error("error - wrong data in archive file");
}