-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
231 lines (203 loc) · 6.37 KB
/
main.cpp
File metadata and controls
231 lines (203 loc) · 6.37 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
#include <iostream>
#include <fstream>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;
// Node struct for Huffman tree
struct Node {
char ch;
int freq;
Node* left;
Node* right;
Node(char c, int f) : ch(c), freq(f), left(nullptr), right(nullptr) {}
Node(char c, int f, Node* l, Node* r) : ch(c), freq(f), left(l), right(r) {}
};
// Comparator for priority queue (min-heap)
struct compare {
bool operator()(Node* l, Node* r) {
return l->freq > r->freq;
}
};
// Generate Huffman codes by traversing the tree
void generateCodes(Node* root, string str, unordered_map<char, string>& huffmanCode) {
if (!root) return;
if (!root->left && !root->right) {
huffmanCode[root->ch] = str;
}
generateCodes(root->left, str + "0", huffmanCode);
generateCodes(root->right, str + "1", huffmanCode);
}
// Delete tree to avoid memory leak
void deleteTree(Node* root) {
if (!root) return;
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
// Write frequency table to output file (for decompression later)
void writeFrequencyTable(ofstream& outFile, unordered_map<char, int>& freq) {
size_t size = freq.size();
outFile.write(reinterpret_cast<const char*>(&size), sizeof(size));
for (auto& p : freq) {
outFile.write(reinterpret_cast<const char*>(&p.first), sizeof(p.first));
outFile.write(reinterpret_cast<const char*>(&p.second), sizeof(p.second));
}
}
// Read frequency table from input file (during decompression)
unordered_map<char, int> readFrequencyTable(ifstream& inFile) {
unordered_map<char, int> freq;
size_t size;
inFile.read(reinterpret_cast<char*>(&size), sizeof(size));
for (size_t i = 0; i < size; i++) {
char ch;
int f;
inFile.read(reinterpret_cast<char*>(&ch), sizeof(ch));
inFile.read(reinterpret_cast<char*>(&f), sizeof(f));
freq[ch] = f;
}
return freq;
}
// Write bits to output file, buffering bits into bytes
class BitWriter {
ofstream& outFile;
unsigned char buffer;
int bitCount;
public:
BitWriter(ofstream& out) : outFile(out), buffer(0), bitCount(0) {}
void writeBit(bool bit) {
buffer = (buffer << 1) | bit;
bitCount++;
if (bitCount == 8) {
outFile.put(buffer);
bitCount = 0;
buffer = 0;
}
}
void writeBits(const string& bits) {
for (char c : bits) {
writeBit(c == '1');
}
}
void flush() {
if (bitCount > 0) {
buffer <<= (8 - bitCount); // pad remaining bits with 0s on the right
outFile.put(buffer);
}
}
};
// Read bits from input file, one by one
class BitReader {
ifstream& inFile;
unsigned char buffer;
int bitCount;
public:
BitReader(ifstream& in) : inFile(in), buffer(0), bitCount(0) {}
// Read next bit, returns -1 if EOF
int readBit() {
if (bitCount == 0) {
if (!inFile.get(reinterpret_cast<char&>(buffer))) return -1;
bitCount = 8;
}
int bit = (buffer >> (bitCount - 1)) & 1;
bitCount--;
return bit;
}
};
// Build Huffman Tree from frequency table
Node* buildTree(const unordered_map<char, int>& freq) {
priority_queue<Node*, vector<Node*>, compare> pq;
for (auto& p : freq) {
pq.push(new Node(p.first, p.second));
}
while (pq.size() > 1) {
Node* left = pq.top(); pq.pop();
Node* right = pq.top(); pq.pop();
Node* node = new Node('\0', left->freq + right->freq, left, right);
pq.push(node);
}
return pq.empty() ? nullptr : pq.top();
}
// Compress file: read input, build tree, write freq table + bits
void compressFile(const string& inputFile, const string& outputFile) {
ifstream inFile(inputFile, ios::binary);
if(!inFile) {
cerr << "Can't open input file.\n"; return;
}
ofstream outFile(outputFile, ios::binary);
if(!outFile) {
cerr << "Can't open output file.\n"; return;
}
// Read full input file content
string content((istreambuf_iterator<char>(inFile)), istreambuf_iterator<char>());
// Frequency map
unordered_map<char,int> freq;
for(char c : content) freq[c]++;
// Write frequency table
writeFrequencyTable(outFile, freq);
// Build Huffman tree & codes
Node* root = buildTree(freq);
unordered_map<char,string> huffmanCode;
generateCodes(root, "", huffmanCode);
// Write encoded bits to file
BitWriter writer(outFile);
for(char c : content) {
writer.writeBits(huffmanCode[c]);
}
writer.flush();
deleteTree(root);
cout << "Compression done. Original size: " << content.size()*8
<< " bits, Compressed size: " << outFile.tellp()*8 << " bits.\n";
}
// Decompress file: read freq table, reconstruct tree, decode bitstream
void decompressFile(const string& inputFile, const string& outputFile) {
ifstream inFile(inputFile, ios::binary);
if(!inFile) {
cerr << "Can't open input file.\n"; return;
}
ofstream outFile(outputFile, ios::binary);
if(!outFile) {
cerr << "Can't open output file.\n"; return;
}
// Read frequency table
unordered_map<char,int> freq = readFrequencyTable(inFile);
// Build Huffman tree
Node* root = buildTree(freq);
// Decode bitstream using tree
BitReader reader(inFile);
Node* current = root;
int bit;
while((bit = reader.readBit()) != -1) {
if(bit == 0)
current = current->left;
else
current = current->right;
if(!current->left && !current->right) { // leaf node
outFile.put(current->ch);
current = root;
}
}
deleteTree(root);
cout << "Decompression done.\n";
}
// Command-line interface
int main(int argc, char* argv[]) {
if(argc != 4) {
cout << "Usage:\n";
cout << " " << argv[0] << " compress <inputfile> <outputfile>\n";
cout << " " << argv << " decompress <inputfile> <outputfile>\n";
return 1;
}
string op = argv[1];
string inputFile = argv[2];
string outputFile = argv[3];
if(op == "compress") {
compressFile(inputFile, outputFile);
} else if(op == "decompress") {
decompressFile(inputFile, outputFile);
} else {
cerr << "Unknown operation: " << op << "\n";
return 1;
}
return 0;
}