-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLZ77.cpp
106 lines (84 loc) · 3 KB
/
LZ77.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
#include "LZ77.h"
/* Class call
LZ77 lz77(text,1);
lz77.encode();
lz77.decode();
*/
LZ77::LZ77(string raw_string, int debug){
text = raw_string;
debug_mode = debug;
}
void printDeque(deque<char> d){
auto it = d.begin();
cout<<endl;
while(it!=d.end()){
cout<<*it<<" ";
it++;
}
cout<<endl;
}
void LZ77::encode(){
int textPointer;
// insert the first elements in lookAheadBuffer
for(int i=0;i<lookAheadBufferLength;i++){
lookAheadBuffer.push_back(text[i]);
}
// After pushing the first few characters in the look ahead buffer
// the text pointer will now have a value of
textPointer = lookAheadBufferLength-1;
deque<char>::iterator searchIt,lookAheadIt,tempIt;
while(!lookAheadBuffer.empty()){
int length=0, tempLength,offset=0;
//search for the longest pattern
for(searchIt=searchBuffer.begin();searchIt!=searchBuffer.end();searchIt++){
tempLength = 0;
lookAheadIt = lookAheadBuffer.begin();
tempIt = searchIt;
while(tempIt!=searchBuffer.end() && lookAheadIt!=lookAheadBuffer.end() && *tempIt==*lookAheadIt){
tempIt++;
lookAheadIt++;
tempLength++;
}
if(tempLength > length){
length = tempLength;
offset = searchBuffer.end() - searchIt;
}
}
if(length>0){
for(int i=0;i<=length;i++){
if(i==length) {
cout << "(" << offset << "," << length << "," << *lookAheadBuffer.begin() << ") ";
encodedData.push_back({offset,length,*lookAheadBuffer.begin()});
}
if(searchBuffer.size()==searchBufferLength)
searchBuffer.pop_front();
searchBuffer.push_back(*lookAheadBuffer.begin());
lookAheadBuffer.pop_front();
if(textPointer<text.size())
lookAheadBuffer.push_back(text[++textPointer]);
}
}
else{
cout<<"("<<offset<<","<<length<<","<<*lookAheadBuffer.begin()<<") ";
encodedData.push_back({offset,length,*lookAheadBuffer.begin()});
if(searchBuffer.size()==searchBufferLength)
searchBuffer.pop_front();
searchBuffer.push_back(*lookAheadBuffer.begin());
lookAheadBuffer.pop_front();
if(textPointer<text.size())
lookAheadBuffer.push_back(text[++textPointer]);
}
}
}
void LZ77::decode() {
string decodedMessage="";
for(int i=0;i<encodedData.size()-1;i++){
int index=decodedMessage.size()-encodedData[i].offset;
//cout<<"\n"<<encodedData[i].offset<<","<<encodedData[i].length<<","<<encodedData[i].symbol<<endl;
for(int j=0;j<encodedData[i].length;j++){
decodedMessage+=decodedMessage[index++];
}
decodedMessage+=encodedData[i].symbol;
}
cout<<decodedMessage<<endl;
}