-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgo.cpp
More file actions
175 lines (148 loc) · 5.16 KB
/
Copy pathAlgo.cpp
File metadata and controls
175 lines (148 loc) · 5.16 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
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
struct BinaryTree {
char value;
int frequ;
BinaryTree* left;
BinaryTree* right;
BinaryTree(char val, int freq) : value(val), frequ(freq), left(nullptr), right(nullptr) {}
};
struct Compare {
bool operator()(BinaryTree* a, BinaryTree* b) {
return a->frequ > b->frequ;
}
};
class HuffmanCode {
private:
string path;
unordered_map<char, string> code;
unordered_map<string, char> reverseCode;
priority_queue<BinaryTree*, vector<BinaryTree*>, Compare> minHeap;
unordered_map<char, int> frequencyFromText(const string& text) {
unordered_map<char, int> freq;
for (char ch : text) freq[ch]++;
return freq;
}
void buildHeap(const unordered_map<char, int>& freq) {
for (const auto& pair : freq) {
char ch = pair.first;
int fr = pair.second;
minHeap.push(new BinaryTree(ch, fr));
}
}
void buildBinaryTree() {
while (minHeap.size() > 1) {
BinaryTree* left = minHeap.top(); minHeap.pop();
BinaryTree* right = minHeap.top(); minHeap.pop();
BinaryTree* newNode = new BinaryTree('\0', left->frequ + right->frequ);
newNode->left = left;
newNode->right = right;
minHeap.push(newNode);
}
}
void buildTreeCodeHelper(BinaryTree* root, string currBits) {
if (!root) return;
if (root->value) {
code[root->value] = currBits;
reverseCode[currBits] = root->value;
return;
}
buildTreeCodeHelper(root->left, currBits + "0");
buildTreeCodeHelper(root->right, currBits + "1");
}
void buildTreeCode() {
BinaryTree* root = minHeap.top();
buildTreeCodeHelper(root, "");
}
string buildEncodedText(const string& text) {
string encodedText = "";
for (char ch : text) encodedText += code[ch];
return encodedText;
}
string buildPaddedText(string encodedText) {
int paddingValue = 8 - (encodedText.length() % 8);
encodedText.append(paddingValue, '0');
bitset<8> paddedInfo(paddingValue);
return paddedInfo.to_string() + encodedText;
}
vector<uint8_t> buildByteArray(const string& paddedText) {
vector<uint8_t> byteArray;
for (size_t i = 0; i < paddedText.length(); i += 8) {
byteArray.push_back(stoi(paddedText.substr(i, 8), nullptr, 2));
}
return byteArray;
}
string removePadding(const string& text) {
int extraPadding = stoi(text.substr(0, 8), nullptr, 2);
return text.substr(8, text.length() - 8 - extraPadding);
}
string decompressText(const string& text) {
string decodedText = "", currBits = "";
for (char bit : text) {
currBits += bit;
if (reverseCode.count(currBits)) {
decodedText += reverseCode[currBits];
currBits = "";
}
}
return decodedText;
}
public:
HuffmanCode(string filepath) : path(filepath) {}
string compress() {
ifstream inputFile(path);
if (!inputFile) {
cerr << "Error opening file!" << endl;
return "";
}
string text((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
inputFile.close();
unordered_map<char, int> freq = frequencyFromText(text);
buildHeap(freq);
buildBinaryTree();
buildTreeCode();
string encodedText = buildEncodedText(text);
string paddedText = buildPaddedText(encodedText);
vector<uint8_t> byteArray = buildByteArray(paddedText);
string outputPath = path.substr(0, path.find_last_of('.')) + ".bin";
ofstream outputFile(outputPath, ios::binary);
outputFile.write(reinterpret_cast<char*>(&byteArray[0]), byteArray.size());
outputFile.close();
cout << "Compressed to: " << outputPath << endl;
return outputPath;
}
void decompress(const string& inputPath) {
ifstream inputFile(inputPath, ios::binary);
if (!inputFile) {
cerr << "Error opening file!" << endl;
return;
}
vector<uint8_t> byteArray((istreambuf_iterator<char>(inputFile)), istreambuf_iterator<char>());
inputFile.close();
string bitString = "";
for (uint8_t byte : byteArray) {
bitString += bitset<8>(byte).to_string();
}
string actualText = removePadding(bitString);
string decompressedText = decompressText(actualText);
string outputPath = inputPath.substr(0, inputPath.find_last_of('.')) + "_decompressed.txt";
ofstream outputFile(outputPath);
outputFile << decompressedText;
outputFile.close();
cout << "Decompressed to: " << outputPath << endl;
}
};
int main() {
string path;
cout << "ENTER THE PATH OF YOUR FILE: ";
cin >> path;
HuffmanCode h(path);
string outputPath = h.compress();
h.decompress(outputPath);
return 0;
}