forked from floft/sprouts
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompressed_stream.h
294 lines (271 loc) · 7.22 KB
/
compressed_stream.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#ifndef COMPRESSED_STREAM_H_INCLUDED
#define COMPRESSED_STREAM_H_INCLUDED
#include <deque>
#include "stream.h"
#include <iostream>
/**************
*Description:
*Input:
*Output:
**************/
class LZ77FormatException final : public IOException
{
public:
LZ77FormatException()
: IOException("LZ77 format error")
{
}
};
struct LZ77CodeType final
{
static constexpr int lengthBits = 6, offsetBits = 16 - lengthBits;
static constexpr size_t maxLength = (1 << lengthBits) - 1, maxOffset = (1 << offsetBits) - 1;
size_t length;
size_t offset;
uint8_t nextByte;
LZ77CodeType(size_t length, size_t offset, uint8_t nextByte)
: length(length), offset(offset), nextByte(nextByte)
{
}
LZ77CodeType() // initialize with EOF
: length(0), offset(1), nextByte(0)
{
}
LZ77CodeType(uint8_t nextByte)
: length(0), offset(0), nextByte(nextByte)
{
}
bool hasNextByte()
{
return length != 0 || offset == 0;
}
bool eof()
{
return length == 0 && offset != 0;
}
static LZ77CodeType read(Reader &reader)
{
LZ77CodeType retval;
retval.nextByte = reader.readByte();
try
{
uint16_t v = reader.readU16();
retval.length = v >> offsetBits;
retval.offset = v & maxOffset;
}
catch(EOFException &e)
{
throw LZ77FormatException();
}
return retval;
}
void write(Writer &writer)
{
//cout << "Write code : 0x" << hex << (unsigned)nextByte << dec << " : length : " << length << " : offset : " << offset << endl;
writer.writeByte(nextByte);
uint16_t v = (offset & maxOffset) | (length << offsetBits);
writer.writeU16(v);
}
};
class ExpandReader final : public Reader
{
private:
shared_ptr<Reader> reader;
static constexpr size_t bufferSize = LZ77CodeType::maxOffset + 1;
circularDeque<uint8_t, bufferSize + 2> buffer;
LZ77CodeType currentCode;
public:
ExpandReader(shared_ptr<Reader> reader)
: reader(reader)
{
}
ExpandReader(Reader &reader)
: ExpandReader(shared_ptr<Reader>(&reader, [](Reader *) {}))
{
}
virtual ~ExpandReader()
{
}
virtual uint8_t readByte() override
{
while(currentCode.eof())
{
currentCode = LZ77CodeType::read(*reader);
}
uint8_t retval;
if(currentCode.length == 0)
{
retval = currentCode.nextByte;
currentCode = LZ77CodeType();
}
else
{
if(currentCode.offset >= buffer.size())
{
throw LZ77FormatException();
}
retval = buffer.cbegin()[currentCode.offset];
if(--currentCode.length == 0)
{
currentCode = LZ77CodeType(currentCode.nextByte);
}
}
buffer.push_front(retval);
if(buffer.size() > bufferSize)
{
buffer.pop_back();
}
return retval;
}
};
class CompressWriter final : public Writer
{
private:
static constexpr int uint8_max = (1 << 8) - 1;
static constexpr size_t bufferSize = LZ77CodeType::maxOffset + 1;
struct Match
{
size_t location, length;
Match(size_t location, size_t length)
: location(location), length(length)
{
}
Match()
: Match(0, 0)
{
}
};
size_t location;
size_t getActualLocation(size_t l)
{
return location - l;
}
shared_ptr<Writer> writer;
circularDeque<uint_fast8_t, bufferSize + 1> currentInput;
circularDeque<uint_fast8_t, bufferSize + 2> buffer;
list<size_t> nodes[uint8_max + 1];
void addByte(uint_fast8_t v)
{
nodes[v].push_front(++location);
buffer.push_front(v);
if(buffer.size() > bufferSize)
{
buffer.pop_back();
for(list<size_t> & nodeList : nodes)
{
for(auto i = nodeList.begin(); i != nodeList.end();)
{
if(getActualLocation(*i) >= buffer.size())
i = nodeList.erase(i);
else
i++;
}
}
}
}
/*void dumpLocation(size_t l)
{
cout << l << " : ";
l = getActualLocation(l);
if(l >= buffer.size())
cout << "\"\"" << endl;
else
{
cout << "\"";
auto iter = buffer.cbegin() + l;
for(int i = 0; i < 5 && i <= l; i++, iter--)
{
cout << (char)*iter;
}
cout << "\"" << endl;
}
}*/
Match getBiggestMatch()
{
Match retval;
auto ii = currentInput.cbegin();
if(ii == currentInput.cend())
return Match();
const list<size_t> & curNodes = nodes[*ii++];
if(curNodes.empty())
return Match();
auto startII = ii;
for(size_t startPos : curNodes)
{
size_t matchLength = 1;
size_t node = startPos;
for(ii = startII; ii != currentInput.cend(); matchLength++, ii++, node++)
{
size_t pos = getActualLocation(node + 1);
if(pos >= buffer.size())
break;
if(buffer.cbegin()[pos] != *ii)
break;
}
if(matchLength > retval.length)
{
retval.length = matchLength;
retval.location = getActualLocation(startPos);
}
}
if(retval.length > LZ77CodeType::maxLength + 1)
retval.length = LZ77CodeType::maxLength + 1;
return retval;
}
void writeCode()
{
if(currentInput.empty())
return;
if(currentInput.size() == 1)
{
addByte(currentInput.front());
LZ77CodeType(currentInput.front()).write(*writer);
currentInput.pop_front();
return;
}
Match m = getBiggestMatch();
if(m.length <= 1)
{
addByte(currentInput.front());
LZ77CodeType(currentInput.front()).write(*writer);
currentInput.pop_front();
return;
}
m.length--;
for(size_t i = 0; i < m.length; i++)
{
addByte(currentInput.front());
currentInput.pop_front();
}
LZ77CodeType code(m.length, m.location, currentInput.front());
code.write(*writer);
addByte(currentInput.front());
currentInput.pop_front();
}
public:
CompressWriter(shared_ptr<Writer> writer)
: writer(writer)
{
}
CompressWriter(Writer &writer)
: CompressWriter(shared_ptr<Writer>(&writer, [](Writer *) {}))
{
}
virtual ~CompressWriter()
{
}
virtual void flush() override
{
while(!currentInput.empty())
writeCode();
writer->flush();
}
virtual void writeByte(uint8_t v) override
{
currentInput.push_back(v);
if(currentInput.size() < bufferSize)
return;
writeCode();
}
};
#endif // COMPRESSED_STREAM_H_INCLUDED