-
Notifications
You must be signed in to change notification settings - Fork 3
/
compress.cpp
173 lines (151 loc) · 4.71 KB
/
compress.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
#include<iostream>
#include "headers/mapOfCharacters.cpp"
#include "headers/classes.cpp"
#include <fstream>
string bin(long n){
long i;
string s = "";
s+="0";
for (i = 1 << 30; i > 0; i = i / 2){
if((n & i) != 0){
s+="1";
}
else{
s+="0";
}
}
return s;
}
string fixedCode(int e, int r, int index){
string output = "";
//if index lies in [1,2r]
if(index >=1 && index<=2*r){
//represent index-1 in (e+1) bits
string s = bin(index-1);
for(int i = s.size()-e-1; i<s.size(); i++){
output+=s[i];
}
}
//else if index lies in [2r+1, infinity]
else{
//represent (index-r-1) in e bits
string s = bin(index-r-1);
for(int i = s.size()-e; i<s.size(); i++){
output+=s[i];
}
}
return output;
}
AdaptiveHuffmanTreeNode* reqNode;
void update(AdaptiveHuffmanTreeNode* head, string element, string &ans, string temp){
if(head == NULL){
return;
}
if(head->character == element){
ans = temp;
reqNode = head;
return;
}
update(head->left, element, ans, temp+"0");
update(head->right, element, ans, temp+"1");
}
void updateTree(AdaptiveHuffmanTreeNode* head){
head->weight++;
head=head->parent;
while(head!=NULL){
if(head->left->weight > head->right->weight){
AdaptiveHuffmanTreeNode* temp = head->left;
head->left = head->right;
head->right = temp;
}
head->weight = head->left->weight + head->right->weight;
head=head->parent;
}
}
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");
}
string encode(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<string, int> mapOfChars = mapOfCharacters();
map<string, int> existsInTree;
string s(1, input[0]);
existsInTree[s] = 1;
//setting up tree with input[0]
output += fixedCode(e, r, mapOfChars[s]);
AdaptiveHuffmanTreeNode* head = new AdaptiveHuffmanTreeNode(1);
tree->head = head;
tree->head->left = tree->nyt;
tree->nyt->parent = tree->head;
tree->head->right = new AdaptiveHuffmanTreeNode(1, s);
tree->head->right->parent = tree->head;
tree->nytCode = "0";
for(int i = 1; i < input.size(); i++){
string s(1, input[i]);
//if s is in tree -> update tree
if(existsInTree[s]){
string ans = "";
update(tree->head, s, ans, "");
updateTree(reqNode);
// cout << s << " " << ans << endl;
output+=ans;
}
//else if s is not in tree -> add s to tree
else{
output = output + tree->nytCode + fixedCode(e, r, mapOfChars[s]);
existsInTree[s] = 1;
AdaptiveHuffmanTreeNode* newNode = new AdaptiveHuffmanTreeNode(1);
newNode->parent = tree->nyt->parent;
newNode->left = tree->nyt;
newNode->right = new AdaptiveHuffmanTreeNode(1, s);
newNode->right->parent = newNode;
tree->nyt->parent->left = newNode;
tree->nyt->parent = newNode;
newNode = newNode->parent;
while(newNode!=NULL){
if(newNode->left->weight > newNode->right->weight){
AdaptiveHuffmanTreeNode* temp = newNode->left;
newNode->left = newNode->right;
newNode->right = temp;
}
newNode->weight = newNode->left->weight + newNode->right->weight;
newNode=newNode->parent;
}
string nytCode = "";
findNytCode(tree->head,tree->nyt, nytCode, "");
tree->nytCode = nytCode;
}
}
return output;
}
string mainCompressFunction(string s){
return encode(s);
}
int main(){
// string input = "My name is Aryaman Gupta. I am a student at JIIT{[]]]} Noida!";
string input = "";
string myText;
// Read from the text file
ifstream MyReadFile("./uploads/file.txt");
// 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 << encode(input) << endl;
return 0;
}