-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLZ.cpp
More file actions
218 lines (187 loc) · 5.92 KB
/
Copy pathLZ.cpp
File metadata and controls
218 lines (187 loc) · 5.92 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
#include <bitset>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<char, string> symbolTable;
unordered_map<string, char> reverseSymbolTable;
int symbolBits = 0;
int segBits = 0;
double culculateTime(clock_t start, clock_t end) {
// 返回以ms计算的时间
return (double)((end - start) * 1000) / CLOCKS_PER_SEC;
}
void buildSymbolTable(const string &input) {
int dictSize = 0;
unordered_map<char, int> tempSymbolTable;
// 统计所有的字符,得到符号编码表
for (char c : input) {
if (tempSymbolTable.find(c) ==
tempSymbolTable.end()) { // 如果字符不在符号表中
tempSymbolTable[c] = dictSize++;
}
}
// 计算编码所需的位数
dictSize = tempSymbolTable.size();
while ((1 << symbolBits) < dictSize) {
symbolBits++;
}
// 将int值转换为二进制字符串,存入符号表
for (const auto &pair : tempSymbolTable) {
string binaryString =
bitset<8>(pair.second).to_string().substr(8 - symbolBits);
symbolTable[pair.first] = binaryString;
}
}
void buildReverseSymbolTable() {
for (const auto &pair : symbolTable) {
reverseSymbolTable[pair.second] = pair.first;
}
}
string lz78Encode(const string &input) {
unordered_map<string, int> dictionary;
vector<pair<int, string>> encodedData;
string encodedBits;
int dictSize = 1;
string currentString = "";
// 分段,得到字典,并进行初步编码
for (char c : input) {
currentString += c;
if (dictionary.find(currentString) == dictionary.end()) {
// 如果当前字符串不在字典中
int index =
currentString.length() > 1
? dictionary[currentString.substr(0, currentString.length() - 1)]
: 0;
char lastChar = currentString.back();
string lastCharBits = symbolTable[lastChar];
encodedData.push_back(make_pair(index, lastCharBits));
dictionary[currentString] = dictSize++;
currentString = "";
}
}
// 处理最后一个字符
if (!currentString.empty()) {
int index =
currentString.length() > 1
? dictionary[currentString.substr(0, currentString.length() - 1)]
: 0;
char lastChar = currentString.back();
string lastCharBits = symbolTable[lastChar];
encodedData.push_back(make_pair(index, lastCharBits));
}
// 计算段号所需的位数
dictSize = dictionary.size();
while ((1 << segBits) < dictSize) {
segBits++;
}
// 在初步编码的基础上完成编码
for (const auto &pair : encodedData) {
// 将索引转换为二进制字符串
int index = pair.first;
string indexBits = bitset<16>(index).to_string().substr(16 - segBits);
// 拼接编码结果
encodedBits += indexBits + pair.second;
}
// 输出字典的内容到文件
ofstream dictionaryFile("dictionary.txt");
for (const auto &pair : dictionary) {
dictionaryFile << pair.first << " -> " << pair.second << endl;
}
dictionaryFile.close();
return encodedBits;
}
string lz78Decode(const string &encodedBits) {
string decodedText;
unordered_map<int, string> dictionary;
int dictSize = 1;
// 解码
size_t pos = 0;
while (pos < encodedBits.length()) {
// 提取出段号
if (pos + segBits > encodedBits.length())
break;
string indexBitSequence = encodedBits.substr(pos, segBits);
pos += segBits;
// 提取出符号
if (pos + symbolBits > encodedBits.length())
break;
string symbolBitSequence = encodedBits.substr(pos, symbolBits);
pos += symbolBits;
// 将索引转换为整数
int index = bitset<64>(indexBitSequence).to_ulong();
// 解码
char nextChar = reverseSymbolTable[symbolBitSequence];
string decodedString =
(index > 0) ? dictionary[index] + nextChar : string(1, nextChar);
decodedText += decodedString;
dictionary[dictSize++] = decodedString;
}
return decodedText;
}
int main() {
// 读取整个文件内容
ifstream file("input.txt");
if (!file.is_open()) {
cerr << "Failed to open file." << endl;
return 1;
}
string text((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
file.close();
buildSymbolTable(text); // 构建符号表
buildReverseSymbolTable(); // 构建化反向符号表
// 编码
string encodedText = lz78Encode(text);
// 解码
string decodedText = lz78Decode(encodedText);
// 比较编码和解码结果
if (text == decodedText) {
cout << "Encoding and Decoding Successful!" << endl;
} else {
cout << "Encoding and Decoding Failed!" << endl;
}
// 计算编码效率
double entropy = 4.42954;
double avgLength = encodedText.size() / (double)text.size();
cout << "Entropy: " << entropy << endl; // 输出信源熵
cout << "Average Length: " << avgLength << endl; // 输出平均长度
cout << "Compression Ratio: " << (entropy / avgLength) * 100 << "%"
<< endl; // 输出编码效率
// 统计编码时间消耗
clock_t start = clock();
for (int i = 0; i < 100; i++) {
lz78Encode(text);
}
clock_t end = clock();
cout << "Encoding Time: " << culculateTime(start, end) / 100.0 << " ms"
<< endl;
// 统计解码时间消耗
start = clock();
for (int i = 0; i < 100; i++) {
lz78Decode(encodedText);
}
end = clock();
cout << "Decoding Time: " << culculateTime(start, end) / 100.0 << " ms"
<< endl;
// 输出符号表到文件
ofstream outputFile("symbolTable.txt");
if (!outputFile.is_open()) {
cerr << "Failed to open output file." << endl;
return 1;
}
for (const auto &pair : symbolTable) {
outputFile << pair.first << " -> " << pair.second << endl;
}
outputFile.close();
// 输出编码结果到文件
ofstream encodedFile("encodedText.txt");
if (!encodedFile.is_open()) {
cerr << "Failed to open encoded file." << endl;
return 1;
}
encodedFile << encodedText << endl;
encodedFile.close();
return 0;
}