-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhuffman.cpp
More file actions
332 lines (283 loc) · 8.7 KB
/
Copy pathhuffman.cpp
File metadata and controls
332 lines (283 loc) · 8.7 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
#include "huffman.hpp"
void huffman::inifreqarr(){
for(int i=0;i<128;i++){
freqarr.push_back(new Node());
freqarr[i]->data=i;
freqarr[i]->freq=0;
}
}
void huffman:: traverse(Node* root,string s){
if(!root->left && !root->right){
root->code=s;
return;
}
traverse(root->left,s+'0');
traverse(root->right,s+'1');
}
int huffman::binToDec(string inStr){
int res=0;
for(auto c:inStr){
res=res*2+c-'0';
}
return res;
}
string huffman::decToBinary(int inNum){
string temp="",res="";
while(inNum>0){
temp+=(inNum%2+'0');
inNum/=2;
}
res.append(8-temp.length(),'0');
for(int i=temp.length()-1;i>=0;i--){
res+=temp[i];
}
return res;
}
void huffman::createMinHeap(){
char id;
infile.open(inFileName,ios::in);
infile.get(id);
while(!infile.eof()){
freqarr[id]->freq++;
infile.get(id);
}
infile.close();
for(int i=0;i<128;i++){
if(freqarr[i]->freq>0){
minHeap.push(freqarr[i]);
}
}
}
void huffman::createTree(){
//creating the huffman tree by minhead
Node*left,*right;
priority_queue<Node*,vector<Node*>,Compare>tempPQ(minHeap);
while(tempPQ.size()!=1){
left=tempPQ.top();
tempPQ.pop();
right=tempPQ.top();
tempPQ.pop();
root=new Node();
root->freq=left->freq+right->freq;
root->left=left;
root->right=right;
tempPQ.push(root);
}
}
void huffman::createCodes(){
traverse(root,"");
}
void huffman::savedEncodedFile(){
//saving encoded (.huf) file
infile.open(inFileName,ios::in);
outfile.open(outFileName,ios::out | ios::binary);
string in="";
string s="";
char currchar;
priority_queue<Node*,vector<Node*>,Compare>pq(minHeap);
in+=(char)minHeap.size();//storing the number of unique characters as the firstt character in encoded string.
//will store the char equivalents of the decimal equivalents of the every 8 bit of the huffman code..
//BUT FIRST WE HAVE TO STORE LIKE THE METADATA FOR THE FILE AS THIS IS REQUIRED FOR DECODING OF THE FILE..
/*
STRUCTURE OF METADATA
1.NUMBER OF UNIQUE CHARACTERS.
2.EACH CODE REPRESENTATION AS THE FIRST UNIQUE CHARACTER WITH LOWEST PRIORITY FOLLOWED BY 16 BYTE LIKE 128bit {
IN WHICH FIRST 0 THEN 1 AS STARTING BIT AND THEN ACTUAL CODE.
}
3.NOW CODE FOR EACH CHARACTER AS THE CHARACTER IS COMMIING IN THE FILE{
HERE CODE MEAND THE CHAR EQUIVALENT OF THE DECIMAL EQUIVALEND
}
4.IF NEED OF APPPENDED ZEORS THEN THE NUMBER OF APPENDED ZEROS.
*/
while(!pq.empty()){
Node* curr=pq.top();
pq.pop();
currchar=curr->data;
in+=currchar;
s.assign(127-curr->code.length(),'0');
s+='1';
s+=curr->code;
//now saving the char eqquivalent of the decimal of each 8 bit of the string.
for(int i=0;i<16;i++){
in+=(char)binToDec(s.substr(0,8));
s=s.substr(8);
}
}
s.clear();
char id;
//now each characters code we have to
// store like the decimal equivalent...
infile.get(id);
while(!infile.eof()) {
s+=freqarr[id]->code;
while(s.length()>8){
in+=(char)binToDec(s.substr(0,8));
s=s.substr(8);
}
infile.get(id);
}
//if bits are less that 8
if(s.length()<8){
s.append(8-s.length(),'0');
}
in+=(char)binToDec(s);
in+=(char)(8-s.length());
outfile.write(in.c_str(),in.size());
infile.close();
outfile.close();
}
void huffman::compress(){
using namespace std;
ifstream infile(inFileName,ios::binary|ios::ate);
streamsize size=infile.tellg();
cout<<size<<endl;
infile.close();
if(size>50*1024*1024){
cout<<"compressing large file"<<endl;
compressLargeFile();
}else{
createMinHeap();
createTree();
createCodes();
savedEncodedFile();
}
}
void huffman::getTree(){
infile.open(inFileName,ios::in| ios::binary);
unsigned char size;
// reading the number of unique characters;
infile.read(reinterpret_cast<char*>(&size),1);
root=new Node();
//now next 1+16 bytes or characters containes the actual char data and the string code
for(int i=0;i<size;i++){
char acode;
unsigned char hCodeC[16];
infile.read(&acode,1);
infile.read(reinterpret_cast<char*>(hCodeC),16);
//converting the decimal equivalent characters into their binary equivalend to obtain the code
string hCodeStr="";
for(int i=0;i<16;i++){
hCodeStr+=decToBinary(hCodeC[i]);
}
// remove padding by ignoring first 127-curr->code.length() 0s and next 1 character
int j=0;
while(hCodeStr[j]=='0'){
j++;
}
hCodeStr=hCodeStr.substr(j+1);
buildTree(acode,hCodeStr);
}
infile.close();
}
void huffman::buildTree(char currchar,string path){
Node* curr=root;
for(int i=0;i<path.length();i++){
if(path[i]=='0'){
if(curr->left==NULL){
curr->left=new Node();
}
curr=curr->left;
}else if(path[i]=='1'){
if(curr->right==NULL){
curr->right=new Node();
}
curr=curr->right;
}
}
curr->data=currchar;
}
void huffman::saveDecodedFile(){
infile.open(inFileName,ios::in|ios::binary);
outfile.open(outFileName,ios::out);
unsigned char size;
infile.read(reinterpret_cast<char*>(&size),1);
// now
// the
// end zeros
infile.seekg(-1,ios::end);
char countOfZeros;
infile.read(&countOfZeros,1);
// ignoring the metadata
infile.seekg(1+(17*size),ios::beg);
vector<unsigned char>text;
unsigned char textseg;
infile.read(reinterpret_cast<char*>(&textseg),1);
while(!infile.eof()){
text.push_back(textseg);
infile.read(reinterpret_cast<char*>(&textseg),1);
}
Node* curr=root;
string path;
for(int i=0;i<text.size()-1;i++){
//converting the decimal number to its equivalent 8 bit binary code
path=decToBinary(text[i]);
if(i==text.size()-2){
path=path.substr(0,8-countOfZeros);
}
// Traversing the huffman tree and apending resultant data to the file
for(int j=0;j<path.size();j++){
if(path[j]=='0'){
curr=curr->left;
}else{
curr=curr->right;
}
if(curr->left ==NULL && curr->right==NULL){
outfile.put(curr->data);
curr=root;
}
}
}
infile.close();
outfile.close();
}
void huffman::decompress(){
getTree();
saveDecodedFile();
}
void huffman::compressLargeFile() {
unsigned int numThreads = std::thread::hardware_concurrency();
if (numThreads == 0) numThreads = 2; // Fallback to 2 threads if hardware_concurrency() returns 0
std::ifstream infile(inFileName, std::ios::in | std::ios::binary);
if (!infile.is_open()) {
throw std::runtime_error("Failed to open input file.");
}
infile.seekg(0, std::ios::end);
std::streampos fileSize = infile.tellg();
infile.seekg(0, std::ios::beg);
std::streamoff partSize = fileSize / numThreads;
std::vector<std::future<std::vector<int>>> futures;
for (unsigned int i = 0; i < numThreads; ++i) {
futures.push_back(std::async(std::launch::async, [&, i]() {
std::ifstream partFile(inFileName, std::ios::in | std::ios::binary);
std::streampos start = i * partSize;
std::streampos end = (i == numThreads - 1) ? fileSize : static_cast<std::streampos>((i + 1) * partSize);
std::vector<int> localFreqArr(128, 0);
processPart(partFile, localFreqArr, start, end);
return localFreqArr;
}));
}
std::vector<int> globalFreqArr(128, 0);
for (auto& future : futures) {
std::vector<int> localFreqArr = future.get();
for (int i = 0; i < 128; ++i) {
globalFreqArr[i] += localFreqArr[i];
}
}
for (int i = 0; i < 128; ++i) {
freqarr[i]->freq = globalFreqArr[i];
if (freqarr[i]->freq > 0) {
minHeap.push(freqarr[i]);
}
}
infile.close();
createTree();
createCodes();
savedEncodedFile();
}
void huffman::processPart(std::ifstream& infile, std::vector<int>& localFreqArr, std::streampos start, std::streampos end) {
infile.seekg(start);
char id;
while (infile.tellg() < end && infile.get(id)) {
localFreqArr[static_cast<unsigned char>(id)]++;
}
}