-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdecompress.cpp
182 lines (157 loc) · 5.07 KB
/
decompress.cpp
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
#include "headers/classes.cpp"
#include "headers/mapOfNumbers.cpp"
#include<iostream>
#include <fstream>
int binaryToDecimal(string str)
{
int dec_num = 0;
int power = 0 ;
int n = str.length() ;
for(int i = n-1 ; i>=0 ; i--){
if(str[i] == '1'){
dec_num += (1<<power) ;
}
power++ ;
}
return dec_num;
}
void findNytCode(AdaptiveHuffmanTreeNode* head, AdaptiveHuffmanTreeNode* nyt, string &nytCode, string temp){
if(head == NULL){
return;
}
if(head->left == NULL && head->right == NULL && head==nyt){
nytCode = temp;
return;
}
findNytCode(head->left,nyt, nytCode, temp+"0");
findNytCode(head->right,nyt, nytCode, temp+"1");
}
void updateTree(string current_char, AdaptiveHuffmanTree* tree){
AdaptiveHuffmanTreeNode* newNode = new AdaptiveHuffmanTreeNode(1);
newNode->parent = tree->nyt->parent;
newNode->left = tree->nyt;
newNode->right = new AdaptiveHuffmanTreeNode(1, current_char);
newNode->right->parent = newNode;
tree->nyt->parent->left = newNode;
tree->nyt->parent = newNode;
newNode = newNode->parent;
while(newNode!=NULL){
newNode->weight = newNode->left->weight + newNode->right->weight;
if(newNode->left->weight > newNode->right->weight){
AdaptiveHuffmanTreeNode* temp = newNode->left;
newNode->left = newNode->right;
newNode->right = temp;
}
newNode=newNode->parent;
}
string nytCode = "";
findNytCode(tree->head,tree->nyt, nytCode, "");
tree->nytCode = nytCode;
}
string decode(string input){
//2 parameters e and r
// m = 94 = 2^e + r
//e = 6, r = 30
int e = 6;
int r = 30;
AdaptiveHuffmanTree* tree = new AdaptiveHuffmanTree();
string output = "";
map<int, string> mp = mapOfNumbers();
int i;
string temp = "";
string current_char = "";
for(i = 0; i < e; i++){
temp += input[i];
}
//is bit value < r?
if(binaryToDecimal(temp)<r){
// i = i+1;
temp += input[i];
output += mp[binaryToDecimal(temp)+1];
current_char = mp[binaryToDecimal(temp)+1];
i = i+1;
}
else{
output += mp[binaryToDecimal(temp) + r + 1];
current_char = mp[binaryToDecimal(temp) + r + 1];
}
AdaptiveHuffmanTreeNode* head = new AdaptiveHuffmanTreeNode(1);
tree->head = head;
tree->head->left = tree->nyt;
tree->nyt->parent = tree->head;
tree->head->right = new AdaptiveHuffmanTreeNode(1, current_char);
tree->head->right->parent = tree->head;
tree->nytCode = "0";
while(i<input.size()){
temp = "";
string tempNytCode = "";
if(i + tree->nytCode.size() < input.size()){
for(int j = i; j < i + tree->nytCode.size(); j++){
tempNytCode += input[j];
}
}
//if nyt code, i.e. element does not exist in tree
if(tempNytCode == tree->nytCode){
i = i+tree->nytCode.size();
int j;
for(j= i; j < i + e; j++){
temp += input[j];
}
if(binaryToDecimal(temp) < r){
temp += input[j];
output += mp[binaryToDecimal(temp) + 1];
current_char = mp[binaryToDecimal(temp) + 1];
i = j+1;
//add new node to tree + update the tree
updateTree(current_char, tree);
}
else{
output += mp[binaryToDecimal(temp) + r + 1];
current_char = mp[binaryToDecimal(temp) + r + 1];
i = j;
//add new node to tree + update the tree
updateTree(current_char, tree);
}
}
//if not nyt code, i.e. element already exists in tree
else{
AdaptiveHuffmanTreeNode* curr = tree->head;
while(curr->character==""){
if(input[i] == '0'){
curr = curr->left;
}
else{
curr = curr->right;
}
temp += input[i];
i++;
}
output += curr->character;
curr->weight+=1;
curr = curr->parent;
while(curr!=NULL){
curr->weight = curr->left->weight + curr->right->weight;
if(curr->left->weight > curr->right->weight){
AdaptiveHuffmanTreeNode* temp = curr->left;
curr->left = curr->right;
curr->right = temp;
}
curr=curr->parent;
}
}
}
return output;
}
int main(){
string input = "";
string myText;
// Read from the text file
ifstream MyReadFile("./uploads/file-compressed.bin");
// Use a while loop together with the getline() function to read the file line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
input+=myText;
}
cout << decode(input) << endl;
return 0;
}