-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompression.cpp
138 lines (118 loc) · 3.54 KB
/
compression.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
#include <iostream>
#include "compression.h"
#include "lib/BinaryTrie.h"
std::vector<uint64_t> buildCodes(std::vector<unsigned char> const &lens) {
std::vector<std::pair<unsigned char, unsigned char> > length(256);
unsigned char i = 0;
do {
length[i] = { lens[i], i };
} while (++i != 0);
std::sort(length.begin(), length.end());
std::vector<uint64_t> codes(256);
uint64_t cur_code = 0;
codes[length[0].second] = 0;
for (unsigned char i = 1; i != 0; ++i) {
++cur_code;
cur_code <<= (length[i].first - length[i - 1].first);
codes[length[i].second] = cur_code;
}
return codes;
}
void countLens(size_t v, unsigned char d, std::vector<node> const &tree, std::vector<unsigned char> &lens) {
if (tree[v].left == node::nullpointer && tree[v].right == node::nullpointer) {
lens[v] = d;
return;
}
if (tree[v].left != node::nullpointer) {
countLens(tree[v].left, d + 1, tree, lens);
}
if (tree[v].right != node::nullpointer) {
countLens(tree[v].right, d + 1, tree, lens);
}
}
void encode(std::istream &input, std::ostream &output) {
BufferedReader in(input);
BufferedWriter out(output);
uint64_t freq[256] = {};
unsigned char c;
while (in.read(c)) {
freq[c]++;
}
in.reset();
std::vector<node> tree(511);
std::priority_queue<std::pair<uint64_t, uint16_t>, std::vector<std::pair<uint64_t, uint16_t> >, std::greater<std::pair<uint64_t, uint16_t>>> queue; //второе - индекс в tree
uint16_t cnt_nodes = 0;
for (; cnt_nodes < 256; ++cnt_nodes) {
queue.push({ freq[cnt_nodes], cnt_nodes });
}
while (cnt_nodes < 511) {
std::pair<uint64_t, uint16_t> node1 = queue.top();
queue.pop();
std::pair<uint64_t, uint16_t> node2 = queue.top();
queue.pop();
tree[cnt_nodes] = { node1.second, node2.second };
queue.push({ node1.first + node2.first, cnt_nodes });
++cnt_nodes;
}
std::vector<unsigned char> lens(256);
countLens(510, 0, tree, lens);
std::vector<uint64_t> codes = buildCodes(lens);
unsigned char special = 0;
unsigned char i = 0;
do {
special = (special + (lens[i] * freq[i]) % 8 ) % 8;
} while (++i != 0);
out.write(special);
for (auto code : lens) {
out.write(code);
}
unsigned char tmp = 0;
int tmp_size = 0;
while (in.read(c)) {
for (unsigned char j = lens[c]; j-- > 0;) {
if (tmp_size == 8) {
out.write(tmp);
tmp_size = 0;
tmp = 0;
}
bool bit = codes[c] & (1ULL << j);
tmp |= bit << (7 - tmp_size);
++tmp_size;
}
}
out.write(tmp);
}
void decode(std::istream &input, std::ostream &output) {
BufferedReader in(input);
BufferedWriter out(output);
unsigned char special;
in.read(special);
std::vector<unsigned char> lens(256);
for (auto &l : lens) {
in.read(l);
}
std::vector<uint64_t> codes = buildCodes(lens);
BinaryTrie trie(codes, lens);
unsigned char cur, nxt = '!';
in.read(cur);
bool last_time = true, succ;
int CONDITION = -1;
while ((succ = in.read(nxt)) || last_time) {
if (!succ) {
if (special == 0 && nxt != '!')
special = 8;
last_time = false;
CONDITION = 7 - special;
}
for (int j = 7; j > CONDITION; --j) {
bool direction = cur & (1 << j);
trie.step(direction);
if (trie.isTerminal()) {
out.write(trie.getChar());
trie.reset();
}
}
cur = nxt;
nxt = '?';
}
}