-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
242 lines (208 loc) · 5.31 KB
/
code.cpp
File metadata and controls
242 lines (208 loc) · 5.31 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
#include "bits/stdc++.h"
using namespace std;
class Node{
private:
public:
char data;
int cost;
Node *left;
Node *right;
Node(char val, int freq, Node *lef=nullptr, Node *rit=nullptr){
data = val;
cost = freq;
left = lef;
right = rit;
}
};
struct Comp {
bool operator() ( const Node *n1, const Node *n2){
return n1->cost > n2->cost;
}
};
class Huffman{
private:
Node *decoder_tree;
unordered_map<char,string> encoder;
int frequency[1<<7];
priority_queue<Node*, vector<Node*>, ::Comp> pq;
int edge;
public:
int *get_freq_arr(){
return frequency;
}
void preorder(Node *root,string key=""){
if(root==nullptr)
return;
if(root->left==nullptr and root->right==nullptr){
encoder[root->data]=key;
}
if(root->left){
preorder(root->left,key+"1");
}
if(root->right){
preorder(root->right,key+"0");
}
}
void set_freq(string &s){
memset(frequency,-1,sizeof(frequency));
for (char c:s){
if(frequency[c]==-1)
frequency[c]=0;
frequency[c]++;
}
for (int i=0; i<(1<<7); i++){
if(frequency[i]>0)
pq.push( (new Node((char)i, frequency[i])) );
}
while(pq.size()>1){
Node *one = pq.top();
pq.pop();
Node *two = pq.top();
pq.pop();
pq.push((new Node('\0', one->cost+two->cost, two, one)));
}
Node* tree = pq.top();
preorder(tree);
this->decoder_tree = tree;
/// parse tree to encoder and decoder...
}
Node* get_decoder_tree(){
return decoder_tree;
}
string encode_it(string s){
set_freq(s);
string encoded = "";
for(auto i:s){
encoded.append(encoder[i]);
}
edge = 8- encoded.size()%8 ;
return encoded;
}
string decode_it(string binary_string){
Node *tmp=decoder_tree;
binary_string.erase(binary_string.end()-edge, binary_string.end());
string decrepted = "";
for (int i: binary_string){
if(i-'0'){
tmp = tmp->left;
}
else{
tmp = tmp->right;
}
if(!tmp->left and !tmp->right){
decrepted += tmp->data;
tmp = decoder_tree;
}
}
return decrepted;
}
void print(){
cout<<"encoder : \n";
for (auto cd:encoder){
cout<<cd.first<<" "<<cd.second<<endl;
}
}
};
string read_from_file(string file_name){
string s="";
ifstream input(file_name, ios::binary|ios::in);
char c;
while((c=input.get())!=EOF){
s+=c;
}
input.close();
return s;
}
void write_bits_to_file(string file_name, string &txt){
unsigned byte=0;
ofstream outs( file_name.c_str(), ios::binary|ios::out);
size_t i;
bool term = false;
for (i=0; i<txt.size(); i+=8){
if(i+8>=txt.size()){
term=true;
break;
}
char byte = (char)stoi( txt.substr(i,8),nullptr, 2 );
outs.put(byte);
}
int edge_bits = 0;
if(term){
string terminal = txt.substr(i,string::npos);
edge_bits = 8 - terminal.size();
while(terminal.size()<8)
terminal +='0';
unsigned char edge = stoi(terminal , nullptr, 2);
outs.put(edge);
}
outs.close();
}
string load_bits_from_file(string file_name){
ifstream input(file_name, ios::binary|ios::in);
string bit_stream="";
char c;
while((c=input.get())!=EOF){
bit_stream.append(bitset<8>(c).to_string());
}
return bit_stream;
}
void test_case(){
Huffman engin;
string s ="welcome to the cpp world";
string file_name = "data.bin";
string encoded = engin.encode_it(s);
write_bits_to_file(file_name, encoded);
string loaded_bits = load_bits_from_file(file_name);
string decoded_ouput = engin.decode_it(loaded_bits);
cout<< decoded_ouput << endl ;
}
int main(){
test_case();
return 0;
}
// testing on pending
struct MetaData{
int const size = 1<<7;
int freq[1<<7];
int edge;
priority_queue<Node*,vector<Node*>,::Comp> *pq;
MetaData(){
memset(freq,-1, sizeof(freq));
pq = nullptr;
}
MetaData(int freq_[],int edge_){
copy(freq_, freq_+(size),freq);
edge = edge_;
pq = nullptr;
}
void build_tree(){
for (int i=0;i< size; i++){
if(freq[i]>0)
pq->push( ( new Node((char)i, freq[i]) ) );
}
while (pq->size()>1){
Node* one = pq->top();
pq->pop();
Node* two = pq->top();
pq->pop();
pq->push((new Node('\0', one->cost+two->cost, two, one)));
}
}
string decode(string binary_string){
string decoded_string="";
Node *pointer = pq->top();
for (auto c: binary_string){
if(c-'0'){
pointer = pointer->left;
}
else{
pointer = pointer->right;
}
if(pointer->right==nullptr and pointer->left == nullptr){
decoded_string += pointer->data;
pointer = pq->top();
}
}
return decoded_string;
}
};