-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
176 lines (150 loc) · 5.96 KB
/
main.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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include "Huffman.cpp"
const size_t MAX_WINDOW_SIZE = 1024; // Maximum window size
struct LZ77Token {
size_t offset; // Offset from the start of the window
size_t length; // Match length
char nextChar; // Next character after the match
LZ77Token(size_t offset, size_t length, char nextChar)
: offset(offset), length(length), nextChar(nextChar) {}
};
/**
* @brief Compresses the given text using the LZ77 algorithm.
*
* @param text The input text to be compressed.
* @return A vector of LZ77Token representing the compressed text.
*/
std::vector<LZ77Token> lz77Compress(const std::string& text) {
std::vector<LZ77Token> tokens;
size_t i = 0;
while (i < text.size()) {
size_t maxMatchLength = 0;
size_t maxMatchDistance = 0;
size_t startWindow = (i < MAX_WINDOW_SIZE) ? 0 : i - MAX_WINDOW_SIZE;
for (size_t j = startWindow; j < i; ++j) {
size_t matchLength = 0;
while (text[i + matchLength] == text[j + matchLength]) {
matchLength++;
if (i + matchLength >= text.size() || j + matchLength >= i) {
break;
}
}
if (matchLength > maxMatchLength) {
maxMatchLength = matchLength;
maxMatchDistance = i - j;
}
}
if (maxMatchLength == 0) {
maxMatchDistance = 0;
maxMatchLength = 0;
}
tokens.emplace_back(maxMatchDistance, maxMatchLength, text[i + maxMatchLength]);
i += maxMatchLength + 1;
}
return tokens;
}
std::vector<LZ77Token> parseTokensFromString(const std::string& tokenStr) {
std::vector<LZ77Token> tokens;
std::istringstream tokenStream(tokenStr);
char delimiter;
// Expected format: "(offset,length,nextChar)"
size_t offset, length;
char nextChar;
while (tokenStream >> delimiter) {
if (delimiter != '(') {
throw std::runtime_error("Invalid token format");
}
if (!(tokenStream >> offset >> delimiter) || delimiter != ',') {
throw std::runtime_error("Invalid token format");
}
if (!(tokenStream >> length >> delimiter) || delimiter != ',') {
throw std::runtime_error("Invalid token format");
}
if (!(tokenStream >> nextChar >> delimiter) || delimiter != ')') {
throw std::runtime_error("Invalid token format");
}
tokens.emplace_back(offset, length, nextChar);
}
return tokens;
}
/**
* @brief Decompresses a sequence of LZ77 tokens and returns the decompressed string.
*
* This function takes a vector of LZ77 tokens as input and performs the decompression
* process to reconstruct the original string. The LZ77 algorithm is a lossless data
* compression algorithm that replaces repeated occurrences of data with references to
* a dictionary window.
*
* @param tokens The vector of LZ77 tokens representing the compressed data.
* @return The decompressed string.
*/
std::string lz77Decompress(const std::vector<LZ77Token>& tokens) {
std::string decompressedText;
for (const LZ77Token& token : tokens) {
if (token.length > 0) {
// Copy the matching string from the window
size_t startPos = decompressedText.size() - token.offset;
for (size_t i = 0; i < token.length; ++i) {
decompressedText += decompressedText[startPos + i];
}
}
// Append the next character
decompressedText += token.nextChar;
}
return decompressedText;
}
std::string lz77DecompressFromString(const std::string& tokenStr) {
std::vector<LZ77Token> tokens = parseTokensFromString(tokenStr);
return lz77Decompress(tokens);
}
std::string compressTokensToString(const std::vector<LZ77Token>& tokens) {
std::stringstream ss;
for (const auto& token : tokens) {
ss << "(" << token.offset << "," << token.length << "," << token.nextChar << ")";
}
return ss.str();
}
void writeCompressedToFile(const std::string& compressedData, const std::string& filename) {
std::ofstream outFile(filename);
outFile << compressedData;
outFile.close();
}
int main() {
std::ifstream sourceFile("source.txt");
if (!sourceFile.is_open()) {
std::cerr << "Error opening source.txt" << std::endl;
return 1;
}
std::string sourceText((std::istreambuf_iterator<char>(sourceFile)), std::istreambuf_iterator<char>());
sourceFile.close();
std::vector<LZ77Token> compressedTokens = lz77Compress(sourceText);
std::string compressedData = compressTokensToString(compressedTokens);
writeCompressedToFile(compressedData, "LZ77_output.txt");
std::cout << "Compressed data has been written to 'LZ77_output.txt'" << std::endl;
std::cout << "Compressed Data with LZ77:\n " << compressedData << std::endl;
HuffmanNode* root = buildHuffmanTree(compressedData);
std::string encodedData = HuffmanCodes(compressedData);
std::cout << "Encoded Data with Huffman:\n" << encodedData << std::endl;
// Open a file in write mode.
std::ofstream outFile("DEFLATE_output");
std::cout << "Writing encoded data to 'DEFLATE_output'..." << std::endl;
// Check if the file stream is good to go before writing to it.
if (outFile.good()) {
outFile << encodedData;
outFile.close();
std::cout << "Encoded Data has been written to 'DEFLATE_output'." << std::endl;
} else {
std::cerr << "Error: Could not open file 'DEFLATE_output' for writing." << std::endl;
}
// Decompress using the Huffman tree
std::string decodedData = HuffmanDecompress(encodedData, root);
std::cout << "Decoded Data with Huffman:\n" << decodedData << std::endl;
std::string decodedLZ77 = lz77DecompressFromString(decodedData);
std::cout << "Decompressed Data with LZ77:" << decodedLZ77 << std::endl;
return 0;
}