-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuffman_compressor_internal.cpp
170 lines (130 loc) · 3.38 KB
/
huffman_compressor_internal.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
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
#include "includes.hpp"
bool CalculateCharactersFrequency(std::priority_queue<CHAR_FREQ_PAIR*, std::vector<CHAR_FREQ_PAIR*>, CharAndFreqPairComparator>& char_and_frequency_tree, BYTE* file_raw, size_t file_size)
{
if (!file_raw || !file_size)
{
return false;
}
std::unordered_map<char, int> char_freq;
for (size_t i = 0; i < file_size; ++i)
{
if (char_freq.find(file_raw[i]) == char_freq.end())
{
char_freq[file_raw[i]] = 1;
}
else
{
++char_freq[file_raw[i]];
}
}
for (const auto& pair : char_freq)
{
char_and_frequency_tree.push(new CHAR_FREQ_PAIR( pair.first, pair.second ));
}
return true;
}
bool CharAndFreqPairComparator::operator()(const CHAR_FREQ_PAIR* left, const CHAR_FREQ_PAIR* right)
{
return left->frequency > right->frequency;
}
CHAR_FREQ_PAIR::CHAR_FREQ_PAIR(char ch, int frequency)
{
this->ch = ch;
this->frequency = frequency;
}
CHAR_FREQ_PAIR::CHAR_FREQ_PAIR()
{
}
CHAR_FREQ_PAIR* BuildHuffmanTree(std::priority_queue<CHAR_FREQ_PAIR*, std::vector<CHAR_FREQ_PAIR*>, CharAndFreqPairComparator>& char_and_frequency_tree)
{
CHAR_FREQ_PAIR* head = nullptr;
while (char_and_frequency_tree.size() != 1)
{
auto* left = char_and_frequency_tree.top();
char_and_frequency_tree.pop();
auto* right = char_and_frequency_tree.top();
char_and_frequency_tree.pop();
auto* new_node = new CHAR_FREQ_PAIR;
new_node->left = left;
new_node->right = right;
new_node->ch = '%';
new_node->frequency = left->frequency + right->frequency;
head = new_node;
char_and_frequency_tree.push(head);
}
return head;
}
void TraverseTree(CHAR_FREQ_PAIR* head, std::map<char, std::string>& key_char_map, std::string binary_path_to_char)
{
if (head->left == nullptr && head->right == nullptr)
{
key_char_map[head->ch] = binary_path_to_char;
return;
}
TraverseTree(head->left, key_char_map, binary_path_to_char + '0');
TraverseTree(head->right, key_char_map, binary_path_to_char + '1');
}
void WriteTree(CHAR_FREQ_PAIR* head, std::vector<BYTE>& compressed_file_bytes)
{
if (head->left == nullptr && head->right == nullptr)
{
compressed_file_bytes.push_back('1');
compressed_file_bytes.push_back(head->ch);
return;
}
compressed_file_bytes.push_back('0');
WriteTree(head->left, compressed_file_bytes);
WriteTree(head->right, compressed_file_bytes);
}
size_t WriteCompressedBytes(std::map<char, std::string>& key_char_map, std::vector<BYTE>& compressed_file_bytes, BYTE* file_raw, size_t file_size)
{
if (!file_raw || !file_size)
{
return 0;
}
size_t out_compressed_bytes_count = 0;
uint16_t bits_remain = BYTE_SIZE_IN_BITS;
BYTE out_byte = 0;
for (size_t i = 0; i < file_size; ++i)
{
BYTE in_byte = file_raw[i];
std::string key = key_char_map[in_byte];
for (size_t j = 0; j < key.size(); ++j)
{
if (key[j] == '0')
{
out_byte <<= 1;
}
else
{
out_byte = (out_byte << 1) | 0b1;
}
--bits_remain;
if (bits_remain == 0)
{
compressed_file_bytes.push_back(out_byte);
++out_compressed_bytes_count;
out_byte = 0;
bits_remain = 8;
}
}
}
if (bits_remain)
{
out_byte <<= bits_remain;
compressed_file_bytes.push_back(out_byte);
++out_compressed_bytes_count;
}
return out_compressed_bytes_count;
}
void DeleteTree(CHAR_FREQ_PAIR* head)
{
if (head->left == nullptr && head->right == nullptr)
{
delete head;
return;
}
DeleteTree(head->left);
DeleteTree(head->right);
delete head;
}