-
Notifications
You must be signed in to change notification settings - Fork 0
/
huffman2.cpp
268 lines (215 loc) · 5.75 KB
/
huffman2.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
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
#include <bitset>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
struct FileHeader {
char tableSize;
int lenght;
char eofCounter;
} fileHeader;
typedef struct Node {
int frequency;
Node *left, *right;
char value;
} Node;
struct Compare {
bool operator()(const Node* lhs, const Node* rhs) const {
return lhs->frequency > rhs->frequency;
}
};
priority_queue<Node*, vector<Node*>, Compare> nodeQueue;
map<char, int> charFrequency;
map<char, char> codingTable;
fstream compressedFile;
fstream rawFile;
string filename;
void countFrequency() {
char c;
charFrequency.clear();
while (rawFile.get(c)) {
auto it = charFrequency.find(c);
if (it != charFrequency.end())
charFrequency[c] += 1;
else
charFrequency[c] = 1;
}
// clear
rawFile.clear();
rawFile.seekg(0, ios::beg);
}
Node* newNode(Node* r, Node* l, int frequency, char value) {
Node* node = new Node;
node->left = l;
node->right = r;
node->frequency = frequency;
node->value = value;
return node;
}
map<char, string> bitString;
void genBitString(Node* root, string code) {
if (root == nullptr) return;
if (root->value != -1) {
bitString[root->value] = code;
} else {
genBitString(root->left, code + "0");
genBitString(root->right, code + "1");
}
}
void clearTree(Node* root) {
if (root == nullptr) return;
clearTree(root->left);
clearTree(root->right);
delete root;
}
void buildTree() {
nodeQueue = priority_queue<Node*, vector<Node*>, Compare>();
cout << "build tree" << endl;
// make char node
for (const auto& kv : charFrequency)
nodeQueue.push(newNode(nullptr, nullptr, kv.second, kv.first));
// build tree
while (nodeQueue.size() > 1) {
Node *node1, *node2;
node1 = nodeQueue.top();
nodeQueue.pop();
node2 = nodeQueue.top();
nodeQueue.pop();
nodeQueue.push(
newNode(node1, node2, node1->frequency + node2->frequency, -1));
}
}
Node* curStep;
void step(bool right) {
if (curStep == nullptr) {
cout << "curStep error" << endl;
curStep = nodeQueue.top();
}
if (right) curStep = curStep->right;
if (!right) curStep = curStep->left;
if (curStep->value != -1) {
rawFile.put(curStep->value);
curStep = nodeQueue.top();
}
}
void compress() {
// load file
do {
cout << "input file: ";
cin >> filename;
cout << "load file \"" << filename << "\"" << endl;
rawFile.open(filename, ios::in);
} while (!rawFile && cin);
countFrequency();
compressedFile.open(filename + ".output", ios::out);
// build table
buildTree();
if (nodeQueue.size() == 0) {
cout << "empty file?" << endl;
} else {
bitString.clear();
genBitString(nodeQueue.top(), "");
}
// Allocate space for header table
compressedFile.seekp(sizeof(int) + sizeof(char) * 2);
fileHeader = {0};
// save mapping table
for (const auto& kv : charFrequency) {
compressedFile.write((char*)&kv.first, sizeof(char));
compressedFile.write((char*)&kv.second, sizeof(int));
fileHeader.tableSize++;
}
// save data
char c, writer = 0;
char bitCounter = 0;
while (rawFile.get(c)) {
auto it = bitString.find(c);
for (auto bit : it->second) {
if (bit == '1') writer |= (0b10000000 >> bitCounter);
bitCounter++;
if (bitCounter >= 8) {
compressedFile.write((char*)&writer, sizeof(char));
bitCounter = 0;
writer = 0;
fileHeader.lenght++;
}
}
}
if (bitCounter != 0) {
compressedFile.write((char*)&writer, sizeof(char));
fileHeader.eofCounter = bitCounter;
fileHeader.lenght++;
}
// save header
compressedFile.seekp(ios::beg);
compressedFile.write((char*)&fileHeader.tableSize, sizeof(char));
compressedFile.write((char*)&fileHeader.lenght, sizeof(int));
compressedFile.write((char*)&fileHeader.eofCounter, sizeof(char));
// deconstruction
if (nodeQueue.size() > 0) clearTree(nodeQueue.top());
compressedFile.close();
rawFile.close();
}
void decompress() {
// load file
do {
cout << "input file: ";
cin >> filename;
cout << "load file \"" << filename << "\"" << endl;
compressedFile.open(filename, ios::in);
} while (!compressedFile && cin);
rawFile.open(filename + ".raw", ios::out);
// load header
compressedFile.read((char*)&fileHeader.tableSize, sizeof(char));
compressedFile.read((char*)&fileHeader.lenght, sizeof(int));
compressedFile.read((char*)&fileHeader.eofCounter, sizeof(char));
// load table
charFrequency.clear();
while (fileHeader.tableSize > 0) {
char c;
int fq;
fileHeader.tableSize--;
compressedFile.read((char*)&c, sizeof(char));
compressedFile.read((char*)&fq, sizeof(int));
charFrequency[c] = fq;
}
// build tree
buildTree();
if (nodeQueue.size() == 0) {
cout << "empty file?" << endl;
} else {
bitString.clear();
genBitString(nodeQueue.top(), "");
}
// write data
curStep = nodeQueue.top();
char reader;
while (fileHeader.lenght > 1) {
compressedFile.read((char*)&reader, sizeof(char));
fileHeader.lenght--;
for (int i = 0; i < 8; i++) step(reader & (0b10000000 >> i));
}
// last data
if (fileHeader.lenght == 1) {
compressedFile.read((char*)&reader, sizeof(char));
for (int i = 0; i < fileHeader.eofCounter; i++)
step(reader & (0b10000000 >> i));
}
// deconstruction
if (nodeQueue.size() > 0) clearTree(nodeQueue.top());
compressedFile.close();
rawFile.close();
}
int main() {
cout << "type \"1\" compress file" << endl;
cout << "type \"2\" decompress file" << endl;
string mode;
while (cin >> mode) {
if (mode == "1") compress();
if (mode == "2") decompress();
cout << "type \"1\" compress file" << endl;
cout << "type \"2\" decompress file" << endl;
}
return 0;
}