-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress.cpp
More file actions
240 lines (212 loc) · 6.53 KB
/
Copy pathcompress.cpp
File metadata and controls
240 lines (212 loc) · 6.53 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
#include<bits/stdc++.h>
using namespace std;
struct Node{
char ch;
int f;
Node* left;
Node* right;
};
class compare{
public:
bool operator()(Node* n1, Node* n2){
return n1->f > n2->f;
//note the the highest priority element will have low freq
}
};
///////////////////////////////////////////////////////////////////
void inOrder(Node* root, unordered_map<char, string> &code, string s){
if(root == NULL){
return;
}
if(root->left == NULL && root->right == NULL){
code[root->ch] = s;
return;
}
inOrder(root->left, code, s+"0");
inOrder(root->right, code, s+"1");
}
//////////////////////////////////////////////////////////////////
Node* createNode(char c, int f){
Node* nn = new Node();
nn->ch = c;
nn->f = f;
nn->left = nn->right = NULL;
return nn;
}
//////////////////////////////////////////////////////////////////
Node* buildHTree(vector<Node*> &v){
priority_queue<Node*, vector<Node*>, compare> pq;
for(auto it : v){
pq.push(it);
}
while(pq.size() != 1){
Node* nn = new Node();
Node* min1 = pq.top();pq.pop();
Node* min2 = pq.top();pq.pop();
nn->left = min1;
nn->right = min2;
nn->ch = '~';
nn->f = min1->f + min2->f;
// cout<<min1->ch <<" "<<min2->ch <<endl;
pq.push(nn);
}
Node* root = pq.top();
return root;
}
//////////////////////////////////////////////////////////////////
string encode(string s, unordered_map<char, string> &code){
string ans;
for(int i=0; i<s.size(); i++){
ans += code[s[i]];
}
return ans;
}
///////////////////////////////////////////////////////////////////
void flush(char &buf, int &tBits){
buf = 0;
buf = buf&0;
tBits = 0;
}
///////////////////////////////////////////////////////////////////
void putInbuf(char c, char &buf, int &tBits){
unsigned char mask = 1;
if(c == '1'){
mask = mask << (7 - tBits);
buf = buf | mask;
}
if(c == '0'){
mask = mask << (7 - tBits);
mask = ~mask;
buf = buf & mask;
}
tBits++;
}
///////////////////////////////////////////////////////////////////
bool checkFileName(string inputFileName){
int n = inputFileName.size();
if(inputFileName[n-1] != 't' || inputFileName[n-2] != 'x' || inputFileName[n-3] != 't' || inputFileName[n-4] != '.'){
cout<<"Input file MUST be a text(.txt) file!" <<endl;
return false;
}
return true;
}
void print(char c){
unsigned char mask = 1;
for(int j=7; j>=0; j--){
if((c & (mask << j)) != 0){
cout<<"1";
}else{
cout<<"0";
}
}
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
//--------------------FILE COMPRESSOR------------------------------
//-----------------------------------------------------------------
//-----------------------------------------------------------------
int main(){
/////////////////////////////accept file name as input
ifstream in;
string inputFileName = "input.txt";
cout<< "Enter the name of text file to be compressed( try sample: input.txt ) : " <<endl;
cin>>inputFileName;
if(!checkFileName(inputFileName)){
return 0;
}
//////////////////////check if file is not present in folder
in.open(inputFileName);
if(in.fail()){
cout << "File Not Found :(" <<endl;
return 0;
}
////////////////////read input file and construct as string
string inputString;
while(!in.eof()){
string temp;
getline(in, temp);
inputString += temp;
inputString += '\n';
}
inputString.pop_back();
// cout<< inputString <<endl;
in.close();
//////////////////////////////////////////////////////////
double inputFileSize = inputString.size();
int compressedFileSize1 = 0, compressedFileSize2 = 0;
//////////////////////////////create frequency map & a node for HM tree
unordered_map<char, int> freq;
for(int i=0; i<inputString.size(); i++){
freq[inputString[i]]++;
}
vector<Node*> v;
for(auto it : freq){
Node* nn = createNode(it.first, it.second);
v.push_back(nn);
}
////////////////////////////////build the HM tree with the nodes array
Node* root = buildHTree(v);
////////////////////////////////create the map of char and its HM codes
unordered_map<char, string> codes;
inOrder(root, codes, "");
// for(auto pair : codes){
// cout<< pair.first <<" - "<< pair.second <<endl;
// }
///////////////////encode the inputString using above map
string encodedString = encode(inputString, codes);
cout<<encodedString<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
compressedFileSize1 += encodedString.size();
///write the codes map and the size of encoded string to the file first
ofstream out("compressed.txt");
out<<encodedString.size();
out<<"\n";
for(auto pair : codes){
string str;
if(pair.first == ' '){
str += '~';
str += '~';
}
else if(pair.first == '\n'){
str += '!';
str += '!';
}else{
str += pair.first;
}
str += pair.second;
str += "\n";
compressedFileSize2 += str.size();
out<<str;
}
out<<"1757051\n";
compressedFileSize2 += 7;
//create the characters from encodedString and put in compressed file
int tBits = 0;
char buf = 0;
buf = buf&0;
for(int i=0; i<encodedString.size(); i++){
putInbuf(encodedString[i], buf, tBits);
if(tBits == 8){
out<<buf;
//print(buf);
flush(buf, tBits);
// out.flush();
}
}
out<<buf;
//print(buf);
flush(buf, tBits);
// out.flush();
out.close();
cout << "\nFile compressed successfully and saved as compressed.txt !!!" <<endl;
///////////////////////////compute the % compression//////////////
// cout << "Input File size : " << inputFileSize << " bytes" <<endl;
// cout << "Compressed File size : " << compressedFileSize1/8 + compressedFileSize2 << " bytes" <<endl;
double compressedFileSize = compressedFileSize1/8 + compressedFileSize2;
double percentage = 100 - (compressedFileSize/inputFileSize)*100;
cout << (int)percentage <<"%" << " compression Achieved!!!\n" <<endl;
////////////////////////////////////////////////////////////////////
return 0;
}