-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.cpp
More file actions
280 lines (280 loc) · 5.06 KB
/
encode.cpp
File metadata and controls
280 lines (280 loc) · 5.06 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
#include <bits/stdc++.h>
using namespace std;
struct node
{
char symbol;
int value;
int number;
struct node *left;
struct node *right;
struct node *parent;
};
int original = 0,compressed = 0;
int n = 7,node_number = 0;
struct node *nyt,*root;
map<char,string> prefix;
map<int,struct node* > getnode;
map<char,int> present;
struct node* newnode(char a)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->symbol = a;
temp->value = 0;
temp->number = 0;
temp->left = NULL;
temp->right = NULL;
temp->parent = NULL;
return temp;
}
string getbinary(int i)
{
string code = "";
int j = 6;
while(j>=0)
{
char c = i%2+'0';
code = c + code;
j--;
i = i/2;
}
return code;
}
void setprefixes()
{
for(int i=0;i<126;i++)
{
prefix.insert(make_pair((char)i,getbinary(i)));
}
}
int add(char c)
{
struct node* merge = newnode('*');
merge->value = 0;
merge->number = node_number;
node_number++;
struct node* symbol = newnode(c);
symbol->value = 0;
symbol->number = node_number;
node_number++;
if(root==nyt)
{
root = merge;
}
else
{
merge->parent = nyt->parent;
nyt->parent->left = merge;
}
int temp = merge->number;
merge->number = nyt->number;
nyt->number = temp;
merge->left = nyt;
nyt->parent = merge;
merge->right = symbol;
symbol->parent = merge;
getnode.find(merge->number)->second = merge;
getnode.insert(make_pair(nyt->number,nyt));
getnode.insert(make_pair(symbol->number,symbol));
return symbol->number;
}
string getcode(struct node* curr)
{
if(curr==root)
{
return "";
}
struct node* temp = curr->parent;
if(temp->left==curr)
{
return getcode(temp)+'0';
}
else
{
return getcode(temp)+'1';
}
}
void swap(struct node* curr,struct node* temp)
{
getnode.find(curr->number)->second = temp;
getnode.find(temp->number)->second = curr;
char z1 = curr->symbol;
char z2 = temp->symbol;
int num = curr->number;
curr->number = temp->number;
temp->number = num;
if(z1!='*')
{
present.find(z1)->second = curr->number;
}
if(z2!='*')
{
present.find(z2)->second = temp->number;
}
struct node* temp1 = curr->parent;
struct node* temp2 = temp->parent;
if(temp1==temp2)
{
temp1->left = temp;
temp1->right = curr;
}
else
{
if(temp1->left==curr)
{
temp1->left = temp;
temp->parent = temp1;
}
else
{
temp1->right = temp;
temp->parent = temp1;
}
if(temp2->left==temp)
{
temp2->left = curr;
curr->parent = temp2;
}
else
{
temp2->right = curr;
curr->parent = temp2;
}
}
}
void update(struct node* curr)
{
if(root==curr)
{
curr->value += 1;
return;
}
struct node* temp = NULL;
int lim;
if(curr->parent->left==curr)
{
lim = curr->number+1;
}
else
{
lim = curr->parent->number;
}
for(int i=0;i<=lim;i++)
{
struct node *check = getnode.find(i)->second;
if(check!=curr&&check->value==curr->value)
{
temp = getnode.find(i)->second;
break;
}
}
if(temp==NULL)
{
curr->value += 1;
update(curr->parent);
}
else
{
if(temp==curr->parent)
{
curr->value += 1;
update(curr->parent);
}
else
{
swap(curr,temp);
curr->value += 1;
update(curr->parent);
}
}
}
int main(int argc,char *argv[])
{
if(argc==1)
{
cout << "Please provide input file followed by the output file" << endl;
return 0;
}
present.clear();
prefix.clear();
getnode.clear();
char c;
nyt = newnode('*');
nyt->value = 0;
nyt->number = node_number;
node_number++;
root = nyt;
setprefixes();
ifstream in(argv[1]);
ofstream out(argv[2]);
string line;
int compressed_length = 0;
int bitcount = 1;
char byte = 0b00100000;
clock_t start = clock();
while(getline(in,line))
{
line += '\n';
for(int i=0;i<line.length();i++)
{
c = line[i];
original += 8;
string code;
if(present.find(c)==present.end())
{
code = getcode(nyt);
code += prefix.find(c)->second;
int num = add(c);
present.insert(make_pair(c,num));
struct node* temp = getnode.find(num)->second;
update(temp);
}
else
{
int num = present.find(c)->second;
struct node* temp = getnode.find(num)->second;
code = getcode(temp);
update(temp);
}
compressed_length += 8;
for(int j=0;j<code.length();j++)
{
bool bit;
if(code[j]=='1')
{
bit = true;
}
else
{
bit = false;
}
if(bitcount==2)
{
byte = byte << 1 | true;
bitcount++;
}
byte = byte << 1 | bit;
bitcount++;
if(bitcount==8)
{
out << byte;
compressed += 8;
bitcount = 1;
byte = 0b00100000;
}
}
}
}
out << byte;
out << bitcount;
compressed += 16;
clock_t end = clock();
in.close();
out.close();
double time = double(end-start);
printf("The Runtime is %0.2f\n",time);
printf("The original file size is %d bytes\n",original/8);
printf("The compressed file size is %d bytes\n",compressed/8);
double ratio = (1.0)*compressed/original;
printf("The compression ratio is %lf\n",(1-ratio)*100);
return 0;
}