-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.cpp
More file actions
315 lines (239 loc) · 9.09 KB
/
Copy pathEncoder.cpp
File metadata and controls
315 lines (239 loc) · 9.09 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
# include "Encoder.h"
Encoder::Encoder(shared_ptr<vector<int>> fq){
letter = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','|'};
if(fq->size() != 26){
throw invalid_argument("Constructor Error: Must have 26 Letter frequenices ");
}
this->frequency = fq;
BuildUpBsTree();
}
shared_ptr<vector<char>> Encoder::Encode(string str){
shared_ptr<vector<char>> result;
//vector<char> save;
vector<char> v;
queue<int> store;
//if string is not Alphabet
if(!isAlphabet(str)){
throw invalid_argument("Encode Error: the string input is not Alphabet");
}
str += '|';
//if the string is empty
if(str.size() == 0){
throw invalid_argument("Encode Error: the string input is empty");
// cerr << "Encode Error: the string input is empty" << endl;
// return result;
}
for(int i=0; i<str.size(); i++){
//if string has lowercase
if(isupper(str[i])){
//str[i]=tolower(str[i]);
throw invalid_argument("Encode Error: the input string is uppercase");
}
// example: str[i] = a; tmp = 10011
string tmp = SavingCode[str[i]];
for(int j =0 ;j <tmp.size(); j++){
//char c = tmp[j];
int s = tmp[j] - '0';
//cout << "S: " << s << endl;
//save.push_back(c);
store.push(s);
}
}
/*
cout << "Enode Output: {";
for(int y = 0; y<save.size(); y++){
cout << "'" << save[y] << "'" << ",";
}
cout << "}";
cout << endl;
*/
// 10011010 10101101 01010111
//while no empty
//push 10011010 for eight times loop
while(!store.empty()){
vector<int> eightbit;
for(int i=0; i<8; i++){
if(store.empty()){
break;
}else{
eightbit.push_back(store.front());
store.pop();
}
}
char res = packInt(eightbit);
v.push_back(res);
}
result = make_shared<vector<char>>(v);
//cout << "Str: " << str << endl;
return result;
}
string Encoder::Decode(shared_ptr<vector<char>> code){
string result;
string store = "";
shared_ptr<vector<char>> unpack = unpackChar(code);
//if the vector is empty
if(code->size() < 1){
throw invalid_argument("Decode Error: It is empty");
}
for(char c : *unpack){
if(!isNumber(c)){
throw invalid_argument("Decode Error: the input character is not either 1 or 0");
}
unordered_map<string,char>::const_iterator got = SavingChar.find(store);
if(got == SavingChar.end()){
//didn't find
store += c;
//cout << "Store: " << store << endl;
}else{
//found
//If found the delimiter |, end the decode function and return the result
if(SavingChar[store] == '|'){
//break
//cout << "\nDecode Output: " << result << endl;
return result;
break;
}else{
result += SavingChar[store];
//cout << "Letter: " << SavingChar[store] << ", Code: " << store << endl;
store.clear();
store += c;
}
}
}
unordered_map<string,char>::const_iterator got = SavingChar.find(store);
if(got == SavingChar.end()){
//If cannot find the direct Letter, throw exception
throw invalid_argument("Decode Error: Cannot find the Letter with your input character vector");
}else{
//found
//If found the delimiter |, end the decode function and return the result
if(SavingChar[store] == '|'){
//break
//cout << "\nDecode Output: " << result << endl;
return result;
}else{
result += SavingChar[store];
}
}
//cout << "\nDecode Output: " << result << endl;
return result;
}
void Encoder::SaveCodes(shared_ptr<LetterNode> curr, string str){
if(curr == nullptr){
return;
}
if(curr->getLetter() != '$'){
SavingCode[curr->getLetter()] = str;
SavingChar[str] = curr->getLetter();
//cout << "Letter: " << curr->getLetter() << ": " << SavingCode[curr->getLetter()] << endl;
}
SaveCodes(curr->getLeft(), str + "0");
SaveCodes(curr->getRight(), str + "1");
}
void Encoder::BuildUpBsTree(){
shared_ptr<LetterNode> left, right, top, left1, right1;
priority_queue<shared_ptr<LetterNode>, vector<shared_ptr<LetterNode>>, greater> pg;
int i = 0;
for(int fq : *frequency){
//pg.push( new LetterNode(letter[i], freq[i]) );
//if the freq is 0, dont create node
if(fq == 0){
// stop create node
throw invalid_argument(" Error: Letter frequenices must not be 0");
}else{
// create node
shared_ptr<LetterNode> node = make_shared<LetterNode>(letter[i],fq);
pg.push(node);
}
i++;
}
//Push the delimiter | node into pq
shared_ptr<LetterNode> node = make_shared<LetterNode>(letter[26],INT_MAX);
pg.push(node);
while(pg.size() != 1){
left = pg.top();
pg.pop();
right = pg.top();
pg.pop();
// top = new LetterNode('$', left->getFreq() + right->getFreq());
top = make_shared<LetterNode>('$',left->getFreq() + right->getFreq());
//cout << tc << endl;
// If the letters have the same freq, go with alphabetic order
// for example: a = 1, h = 1
if(left->getFreq() == right->getFreq()){
// go with alphabetic order
left1 = left;
right1 = right;
while(left1->getLeft() != nullptr){
left1 = left1->getLeft();
}
while(right1->getLeft() != nullptr){
right1 = right1->getLeft();
}
char a = left1->getLetter();
char b = right1->getLetter();
int a1 = a -'0';
int b1 = b -'0';
//cout << "A: " << a1 << " B1: " << b1 << endl;
if(a1 < b1){
// the smaller ascii value go left, larger go right
top->setLeft(left);
top->setRight(right);
}else{
top->setLeft(right);
top->setRight(left);
}
}else{
top->setLeft(right);
top->setRight(left);
}
pg.push(top);
}
SaveCodes(pg.top(), "");
}
bool Encoder::isNumber(const char &c){
if(c != '1' && c != '0'){
// the char c is not either 1 or 0
return false;
}
return true;
}
bool Encoder::isAlphabet(const string &str){
int check;
for(int i=0; i<str.size(); i++){
check = isalpha(str[i]);
if(!check){
return false;
}
}
return true;
}
char Encoder::packInt(vector<int> v){
char result = 0x00; //0000 0000
//string str = "";
for(int i=0; i<v.size(); i++){
//str += to_string(v[i]);
result = result << 1; //shift left
result = result | v[i]; // value is 0b0 or 0b1
}
int shift = 8 -v.size();
//cout << "Shift: " << shift << endl;
result = result << shift;
string binary = bitset<8>(result).to_string();
//cout << "decial output: " << str << endl;
//cout << "Binary output: " << binary << endl;
return result;
}
shared_ptr<vector<char>> Encoder::unpackChar(shared_ptr<vector<char>> v){
shared_ptr<vector<char>> result;
vector<char> store;
for(int i=0; i<v->size(); i++){
char c = v->at(i);
string binary = bitset<8>(c).to_string();
for(int j=0; j<binary.size(); j++){
store.push_back(binary[j]);
}
}
result = make_shared<vector<char>>(store);
return result;
}