-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
executable file
·293 lines (247 loc) · 8.68 KB
/
main.cc
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
#include <fstream>
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <map>
using namespace std;
#include "node.h"
#include "leafnode.h"
#include "rootnode.h"
#include "compare.h"
// traverseTree(Node* root, string code) traverses the Huffman tree for the leafnodes and adds the
// char-string pairs to the map, where the string is the route from the root to the leaf
void traverseTree(Node* node, string code, map<char,string>& codeList) {
if (dynamic_cast<leafNode*>(node)) { // if node is a leafNode
codeList[dynamic_cast<leafNode*>(node)->getChar()] = code;
}
if (dynamic_cast<rootNode*>(node)) { // if node is a rootNode
traverseTree(dynamic_cast<rootNode*>(node)->left, code + "0", codeList);
traverseTree(dynamic_cast<rootNode*>(node)->right, code + "1", codeList);
}
}
// createTree takes a priority queue and returns a Huffman Tree made from the elements of the queue
Node* createTree(priority_queue<Node*, vector<Node*>, Compare> pq){
while (pq.size() >= 2) {
int freq1 = pq.top()->getFrequency();
Node* l = pq.top();
pq.pop();
int freq2 = pq.top()->getFrequency();
Node* r = pq.top();
pq.pop();
int depth;
if (dynamic_cast<rootNode*>(l) && dynamic_cast<rootNode*>(r)) {
if (l->getTimestamp() > r->getTimestamp()) {
depth = l->getTimestamp();
} else {
depth = r->getTimestamp();
}
} else if (dynamic_cast<rootNode*>(r)) {
depth = r->getTimestamp();
} else if (dynamic_cast<rootNode*>(l)) {
depth = l->getTimestamp();
} else {
depth = 0;
}
Node* node = new rootNode(freq1+freq2, l, r, depth + 1);
pq.push(node);
}
return pq.top();
}
// packBytes takes a stream of bits (0's and 1's) and packs them in groups of 8 into unsigned chars (a byte)
// and returns the vector of the bytes. The bytes are written to the ofstream output file.
vector<unsigned char> packBytes(vector<bool> bitstream, ofstream& compressedfile) {
vector<unsigned char> packedbytes;
for (int j = 0; j < bitstream.size(); j = j + 8) {
unsigned char byte = 0;
if (bitstream.size() - j >= 8) {
for (int k = 0; k < 8; ++k) {
if (bitstream[j+k] == true) {
byte |= 1 << k;
} else {
byte |= 0 << k;
}
}
packedbytes.push_back(byte);
compressedfile << byte;
} else { // if less than 8 bits left
int rem = bitstream.size() - j;
int bitsneeded = 8 - rem;
for (int k = 0; k < rem; ++k) {
if (bitstream[j+k] == true) {
byte |= 1 << k;
} else {
byte |= 0 << k;
}
}
for (int n = rem; n < 8; ++n) {
if (bitstream[j+n] == true) {
byte |= 1 << n;
} else {
byte |= 0 << n;
}
}
packedbytes.push_back(byte);
compressedfile << byte;
}
}
return packedbytes;
}
// createPriorityQueue takes an int array and returns a priority queue of the non zero entries of the array.
// The elements of the queue are Nodes, and the priority is by Node frequency and first-in-first-out convention.
priority_queue<Node*, vector<Node*>, Compare> createPriorityQueue(int* freqVector) {
priority_queue<Node*, vector<Node*>, Compare> pq;
for (int i = 0; i < 256; ++i) {
if (freqVector[i] != 0) {
char ch = i;
Node* newnode = new leafNode(i, freqVector[i]);
pq.push(newnode);
}
}
return pq;
}
// unpackByte takes in an unsigned char (byte) and pushes its 8 bit composition of 0's and 1's into a bool vector
void unpackByte(unsigned char character, vector<bool>& bitstream) {
for(int k=0; k < 8; k++) {
int thebit = (character & (1 << k)) >> k;
bitstream.push_back(thebit);
}
}
// decode takes in a bool vector and decodes the variable-length bit sequences with a Huffman tree.
// The corresponding characters are pushed to an ofstream output file.
void decode(Node* root, ofstream& o, vector<bool> decodedBitstream, int charactersToDecode) {
Node* current = root;
int charactersDecoded = 0;
for (int j = 0; j < decodedBitstream.size(); ++j) { // iterate through all the bits
if (decodedBitstream[j] == 0) { // if bit is a 0
current = dynamic_cast<rootNode*>(current)->left;
if (dynamic_cast<leafNode*>(current)) { // if a leafNode is reached
o << dynamic_cast<leafNode*>(current)->getChar();
++charactersDecoded;
current = root; // current Node points at root Node again
if (charactersDecoded == charactersToDecode) {
return;
}
}
} else { // if bit is a 1
current = dynamic_cast<rootNode*>(current)->right;
if (dynamic_cast<leafNode*>(current)) { // if a leafNode is reached
o << dynamic_cast<leafNode*>(current)->getChar();
++charactersDecoded;
current = root; // current Node points at root Node again
if (charactersDecoded == charactersToDecode) {
return;
}
}
}
}
}
/************************* MAIN *************************/
// ./exe [file] [-c/-d] [outputfile name]
// -c to compress, -d to decompress
int main(int argc, char* argv[]) {
if (argc != 4) { // check for correct number of arguments
cout << "Invalid number of command line arguments" << endl;
return 0;
}
// COMPRESSION
if (string(argv[2]) == "-c") {
// open file to compress
ifstream textfile;
textfile.open(argv[1]);
// open empty output file
ofstream compressedfile;
compressedfile.open(string(argv[3]) + ".txt");
int freqVector[256] = {0};
int numberofuniquechars = 0;
int numberofchars = 0;
char c;
// count frequency of ASCII characters
while (textfile >> noskipws >> c) {
freqVector[c]++;
++numberofchars;
}
// if empty file, return empty file
if (numberofchars == 0) {
return 0;
}
compressedfile << numberofchars << endl;
// count number of unique ASCII characters
for (int i = 0; i < 256; ++i) {
if (freqVector[i] != 0) {
++numberofuniquechars;
}
}
compressedfile << numberofuniquechars << endl;
// store character and frequency in output file
for (int i = 0; i < 256; ++i) {
if (freqVector[i] != 0) {
char c = i;
compressedfile << c << " " << freqVector[i] << endl;
}
}
// insert freqVector in to a priority queue, with low frequency as high priority
priority_queue<Node*, vector<Node*>, Compare> pq = createPriorityQueue(freqVector);
// priority queue is passed to createTree function to create Huffman tree
Node* head = createTree(pq);
// traverse Huffman tree to find bit sequences for every unique character. Store sequences in a map
map<char, string> codeList;
traverseTree(head, "", codeList);
// reset textfile to be read in again
textfile.clear();
textfile.seekg(0, textfile.beg);
// reread characters as their bit sequence into bool vector
vector<bool> bitstream;
while (textfile >> noskipws >> c) {
for (int i = 0; i < codeList[c].length(); ++i) {
if (codeList[c][i] == '0') {
bitstream.push_back(false);
} else {
bitstream.push_back(true);
}
}
}
// pack bit sequence into bytes (8 bits per byte) and write to output file
vector<unsigned char> packedbytes = packBytes(bitstream, compressedfile);
return 0;
}
// DECOMPRESSION
else if (string(argv[2]) == "-d") {
int charactersToDecode;
int uniqueCharacters;
// open file to decompress
ifstream textfile;
textfile.open(argv[1]);
// open empty output file
ofstream compressedfile;
compressedfile.open(string(argv[3]) + ".txt");
textfile >> charactersToDecode >> uniqueCharacters;
textfile.ignore(1, '\n'); // ignore newline character after end of character-frequency lines
int frequencyVector[256] = {0};
// read in character frequencies
for (int i = 0; i < uniqueCharacters; ++i) {
char character;
int frequency;
textfile >> noskipws >> character;
textfile.ignore(1, ' ');
textfile >> noskipws >> frequency;
textfile.ignore(1, '\n');
int index = character;
frequencyVector[index] = frequency;
}
// create priority queue of Node* containing frequencies of characters
priority_queue<Node*, vector<Node*>, Compare> pq = createPriorityQueue(frequencyVector);
// create Huffman tree from priority queue
Node* root = createTree(pq);
vector<bool> decodedBitstream;
unsigned char packedbyte;
// read in characters from textfile and unpack them into their 8 bit compositions.
// read the bits into a bool vector
while (textfile >> noskipws >> packedbyte) {
unpackByte(packedbyte, decodedBitstream);
}
// decode the variable-length bit sequences to original uncompressed text and write to output file
decode(root, compressedfile, decodedBitstream, charactersToDecode);
return 0;
}
} // main