-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.cpp
103 lines (82 loc) · 2.4 KB
/
encode.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
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
/*!
\file
\brief Ôàéë, ñîäåðæàùèé îñíîâíûå ôóíêöèè àðõèâàöèè
*/
#include "Haffman Algorithm.h"
#include <utility>
#include <iostream>
#include <fstream>
#include <queue>
Node* addNode(char ch, int freq, Node* left, Node* right) {
Node* node = new Node();
node->ch = ch;
node->freq = freq;
node->left = left;
node->right = right;
return node;
}
int pack(std::string& name) {
std::string text;
if (!parse_file(name, text)) {
std::cout << "\n Unable to open file ";
return 0;
}
if (text == "") {
std::cout << "\n File is empty ";
return 0;
}
//std::cout << "\n\n Source text:\n" << text << "\n";
std::unordered_map<char, int> freq = find_frequency(text);
std::cout << "\n Found characters frequency: ";
/*for (auto& it : freq) {
std::cout << "\n " << it.first;
std::cout << " " << it.second;
}*/
Node* root = build_tree(freq);
std::unordered_map<char, std::string> huffmanCode;
encode(root, "", huffmanCode);
std::cout << "\n\n Huffman Codes are: ";
/*for (auto pair : huffmanCode)
std::cout << "\n " << pair.first << " " << pair.second;*/
std::string str;
for (char ch : text)
str += huffmanCode[ch];
//std::cout << "\n Binary code: " << str;
std::ofstream outfile(gen_en_filename(name), std::ios::binary);
insert_zeros_counter(outfile, str.size());
std::string tree;
writeBinaryTree(root, tree);
//std::cout << "\n Tree transcription: " << tree << std::endl;
outfile << tree.size();
outfile << "#";
outfile << tree;
writeBinaryString(outfile, str);
outfile.close();
}
std::unordered_map<char, int> find_frequency(std::string& text) {
std::unordered_map<char, int> freq;
for (char character : text)
freq[character]++;
return freq;
}
Node* build_tree(std::unordered_map<char, int> freq) {
std::priority_queue<Node*, std::vector<Node*>, comp> node_queue;
for (auto pair : freq)
node_queue.push(addNode(pair.first, pair.second, nullptr, nullptr));
while (node_queue.size() != 1) {
Node* left = node_queue.top();
node_queue.pop();
Node* right = node_queue.top();
node_queue.pop();
node_queue.push(addNode('\0', (left->freq + right->freq), left, right));
}
return node_queue.top();
}
void encode(Node* root, std::string str, std::unordered_map<char, std::string>& huffmanCode) {
if (root == nullptr)
return;
if (!root->left && !root->right)
huffmanCode[root->ch] = str;
encode(root->left, str + "0", huffmanCode);
encode(root->right, str + "1", huffmanCode);
}