-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLZWenc.cpp
More file actions
253 lines (211 loc) · 5.95 KB
/
Copy pathLZWenc.cpp
File metadata and controls
253 lines (211 loc) · 5.95 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <algorithm>
#include <array>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <fstream>
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
#include <memory>
#include <ostream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
using CodeType = std::uint32_t;
/// Dictionary Maximum Size
// Used as a BreakPoint instead of NULL for BST
const CodeType DictSizemax = 13000 * 1024;
// character to control decoding
const CodeType Eof_enc = 1u << CHAR_BIT; ///< End-of-file.
// Encoder Class
class EncoderDictionary
{
// Node for BST
struct Node
{
explicit Node(char c) : first(DictSizemax), c(c), left(DictSizemax), right(DictSizemax) {}
CodeType first; // Code of first child string.
char c;
CodeType left; // Code of child node with byte < c.
CodeType right; // Code of child node with byte > c.
};
public:
// Fill the Dictionary with all possible chars
EncoderDictionary()
{
const int minc = std::numeric_limits<char>::min();
const int maxc = std::numeric_limits<char>::max();
CodeType k{0};
vn.reserve(DictSizemax);
vn.clear();
for (int c = minc; c <= maxc; ++c) {
initials[static_cast<unsigned char>(c)] = k++;
vn.push_back(Node(c));
}
// add node for metacode
vn.push_back(Node('\x00'));
}
// Searches for a pair (i, c) and inserts the pair if it wasn't found.
// if found returns index , else return DictSizemax
CodeType search_and_insert(CodeType i, char c)
{
if (i == DictSizemax)
return search_initials(c);
const CodeType vn_size = vn.size();
CodeType ci{vn[i].first}; // Current Index
// Searching the BST
if (ci != DictSizemax)
{
while (true)
if (c < vn[ci].c)
{
if (vn[ci].left == DictSizemax)
{
vn[ci].left = vn_size;
break;
}
else
ci = vn[ci].left;
}
else if (c > vn[ci].c)
{
if (vn[ci].right == DictSizemax)
{
vn[ci].right = vn_size;
break;
}
else
ci = vn[ci].right;
}
else // c == vn[ci].c
return ci;
}
else
vn[i].first = vn_size;
vn.push_back(Node(c));
return DictSizemax;
}
// Returns Code of char c from the initial one-byte part of the dictionary
CodeType search_initials(char c) const
{
return initials[static_cast<unsigned char>(c)];
}
// Return size of Dictionary
std::vector<Node>::size_type size() const
{
return vn.size();
}
private:
// Vector of nodes for the BST.
std::vector<Node> vn;
// maps one-byte strings to their codes.
std::array<CodeType, 1u << CHAR_BIT> initials;
};
// helper class to store leftover bits
struct ByteCache_enc
{
ByteCache_enc() : used(0), data(0x00) {}
std::size_t used; ///< Bits currently in use.
unsigned char data; ///< The bits of the cached byte.
};
// class to write bits to file
class CodeWriter
{
public:
explicit CodeWriter(std::ostream &os) : os(os), bits(CHAR_BIT + 1)
{
}
// Destructor to write the leftover bits when finished
~CodeWriter()
{
write(static_cast<CodeType>(Eof_enc));
// write the incomplete leftover byte
if (lo.used != 0)
os.put(static_cast<char>(lo.data));
}
// getter for number of bits
std::size_t get_bits() const
{
return bits;
}
// increases bits number
void increase_bits()
{
++bits;
}
// Writes the code k with the current bit width.
bool write(CodeType k)
{
std::size_t remaining_bits{bits};
if (lo.used != 0)
{
lo.data |= k << lo.used;
os.put(static_cast<char>(lo.data));
k >>= CHAR_BIT - lo.used;
remaining_bits -= CHAR_BIT - lo.used;
lo.used = 0;
lo.data = 0x00;
}
while (remaining_bits != 0)
if (remaining_bits >= CHAR_BIT)
{
os.put(static_cast<char>(k));
k >>= CHAR_BIT;
remaining_bits -= CHAR_BIT;
}
else
{
lo.used = remaining_bits;
lo.data = k;
break;
}
return true;
}
private:
ByteCache_enc lo; ///< LeftOvers.
std::ostream &os; ///< Output Stream.
std::size_t bits; ///< Binary width of codes.
};
// Computes the number of bits required to store n.
std::size_t required_bits_enc(unsigned long int n)
{
std::size_t r{1};
while ((n >>= 1) != 0)
r++;
return r;
}
//Compresses the contents of "is" and writes the result to "os".
void compress(std::istream &is, std::ostream &os)
{
EncoderDictionary ed;
CodeWriter cw(os);
CodeType i{DictSizemax}; // Index
char c;
while (is.get(c))
{
const CodeType temp{i};
if ((i = ed.search_and_insert(temp, c)) == DictSizemax)
{
cw.write(temp);
i = ed.search_initials(c);
if (required_bits_enc(ed.size() - 1) > cw.get_bits())
cw.increase_bits();
}
}
if (i != DictSizemax)
cw.write(i);
}
int LZWenc(std::string inputFileName, std::string outputFileName)
{
std::ifstream input_file;
std::ofstream output_file;
input_file.open(inputFileName, std::ios_base::binary);
output_file.open(outputFileName, std::ios_base::binary);
// main compress function
compress(input_file, output_file);
return EXIT_SUCCESS;
}