-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp5.cpp
More file actions
223 lines (169 loc) · 4.34 KB
/
Copy pathp5.cpp
File metadata and controls
223 lines (169 loc) · 4.34 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
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<queue>
using namespace std;
template<typename T>
struct BiNode{
T elem;
BiNode *left,*right;
BiNode():
left(nullptr),right(nullptr){}
BiNode(const T& elem):
elem(elem),left(nullptr),right(nullptr){}
~BiNode(){
delete left,right;
}
};
map<char,string> HuffmanCoding(const map<char,int>& cnt){
// build a Huffman tree
priority_queue<pair<int,BiNode<char>*>>q;
for(auto [ch,freq]:cnt)
q.emplace(-freq,new BiNode<char>(ch));
while(q.size()>1){
auto [f1,ht1]=q.top(); q.pop();
auto [f2,ht2]=q.top(); q.pop();
auto ht=new BiNode<char>(char(-1));
ht->left=ht1; ht->right=ht2;
q.emplace(f1+f2,ht);
}
// get Huffman coding from tree
auto root=q.top().second; q.pop();
map<char,string>res;
string path;
auto dfs=[&](auto &&self,BiNode<char>* rt){
if(rt->elem>=0){
res[rt->elem]=path;
return;
}
path+='0';
self(self,rt->left);
path.pop_back();
path+='1';
self(self,rt->right);
path.pop_back();
};
dfs(dfs,root);
delete root;
return res;
}
bool encode(){
ifstream fin("input/source.txt",ios::in);
if(!fin.is_open()){
cerr<<"Cannot open input/source.txt!\n";
return false;
}
// calculate the frequency of each characters
map<char,int>cnt{{'\0',1}};
for(char ch;fin.get(ch);)
++cnt[ch];
// save the coding
auto ch2code=HuffmanCoding(cnt);
ofstream fout1("Huffman.txt",ios::out);
for(auto [ch,freq]:cnt){
string s{ch};
if(ch=='\0')
s="EOF";
else if(ch==' ')
s="space";
else if(ch=='\n')
s="\\n";
fout1<<s<<" "<<freq<<" "<<ch2code[ch]<<endl;
}
fout1.close();
// calculate the size of code
int len=ch2code['\0'].length();
fin.clear(); fin.seekg(0);
for(char ch;fin.get(ch);)
len+=ch2code[ch].length();
// encode
int siz=(len+7)>>3;
char* buffer=new char[siz]();
fin.clear(); fin.seekg(0);
int i=0;
for(char ch;fin.get(ch);){
for(auto bit:ch2code[ch]){
if(bit=='1')
buffer[i>>3]|=1<<(i&7);
++i;
}
}
fin.close();
// add EOF
for(auto bit:ch2code['\0']){
if(bit=='1')
buffer[i>>3]|=1<<(i&7);
++i;
}
// write into "code.dat"
ofstream fout2("code.dat",ios::out|ios::binary);
fout2.write(buffer,siz);
fout2.close();
delete [] buffer;
return true;
}
bool decode(){
// build tree from "Huffman.txt"
ifstream fin1("Huffman.txt",ios::in);
if(!fin1.is_open()){
cerr<<"Cannot open Huffman.txt!\n";
return false;
}
auto root=new BiNode<char>(-1);
for(string s,code,freq;fin1>>s>>freq>>code;){
char ch=s[0];
if(s=="EOF")
ch='\0';
else if(s=="\\n")
ch='\n';
else if(s=="space")
ch=' ';
auto cur=root;
for(auto bit:code)
if(bit=='0'){
if(cur->left==nullptr)
cur->left=new BiNode<char>(-1);
cur=cur->left;
}else{
if(cur->right==nullptr)
cur->right=new BiNode<char>(-1);
cur=cur->right;
}
cur->elem=ch;
}
fin1.close();
// decode
ifstream fin2("code.dat",ios::in|ios::binary);
if(!fin2.is_open()){
cerr<<"Cannot open code.dat!\n";
return false;
}
ofstream fout("recode.txt",ios::out);
auto cur=root; bool end=0;
for(char buffer;fin2.read(&buffer,1) && !end;){
for(int i=0;i<8;i++){
if(cur->elem>0){
fout<<(cur->elem);
cur=root;
}else if(cur->elem=='\0'){
end=1; break;
}
if((buffer>>i)&1)
cur=cur->right;
else
cur=cur->left;
}
}
fin2.close();
fout.close();
delete root;
return true;
}
int main(){
if(encode())
cout<<"Encode succeed!\n";
if(decode())
cout<<"Decode succeed!\n";
return 0;
}