forked from mathieuchartier/mcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordModel.hpp
320 lines (274 loc) · 7.39 KB
/
WordModel.hpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* MCM file compressor
Copyright (C) 2016, Google Inc.
Authors: Mathieu Chartier
LICENSE
This file is part of the MCM file compressor.
MCM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MCM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MCM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _WORD_MODEL_HPP_
#define _WORD_MODEL_HPP_
#include "Reorder.hpp"
#include "UTF8.hpp"
class WordModel {
public:
// Hashes.
uint64_t prev;
uint64_t h1, h2;
// UTF decoder.
UTF8Decoder<false> decoder;
// Length of the model.
size_t len;
static const size_t kMaxLen = 31;
// Transform table.
static const uint32_t transform_table_size = 256;
uint32_t transform[transform_table_size];
uint32_t opt_var_ = 0;
size_t* opts_ = nullptr;
void setOpt(uint32_t n) {
opt_var_ = n;
}
void SetOpts(size_t* opts) {
opts_ = opts;
}
ALWAYS_INLINE uint32_t& trans(char c) {
uint32_t index = (uint32_t)(uint8_t)c;
check(index < transform_table_size);
return transform[index];
}
WordModel() {}
void Init(const ReorderMap<uint8_t, 256>& reorder) {
uint32_t index = 0;
for (auto& t : transform) t = transform_table_size;
for (uint32_t i = 'a'; i <= 'z'; ++i) {
transform[reorder[i]] = index++;
}
for (uint32_t i = 'A'; i <= 'Z'; ++i) {
transform[reorder[i]] = transform[reorder[MakeLowerCase(static_cast<int>(i))]];
}
// 6,38,92,3
trans(reorder[6]) = index++;
trans(reorder[38]) = index++;
trans(reorder[92]) = index++;
trans(reorder[3]) = index++;
// trans('"') = index++;
// trans('&') = index++;
// trans('<') = index++;
// trans('{') = index++;
// trans(3) = index++;
// trans(41) = index++;
// trans(33) = index++;
// trans('[') = index++;
// trans(3) = index++;
// trans(38) = index++;
// trans(6) = index++;
// trans(92) = index++;
#if 0
trans('À') = trans('à') = index++;
trans('Á') = trans('á') = index++;
trans('Â') = trans('â') = index++;
trans('Ã') = trans('ã') = index++;
trans('Ä') = trans('ä') = index++;
trans('Å') = trans('å') = index++;
trans('Æ') = trans('æ') = index++;
trans('Ç') = trans('ç') = index++;
trans('È') = trans('è') = index++;
trans('É') = trans('é') = index++;
trans('Ê') = trans('ê') = index++;
trans('Ë') = trans('ë') = index++;
trans('Ì') = trans('ì') = index++;
trans('Í') = trans('í') = index++;
trans('Î') = trans('î') = index++;
trans('Ï') = trans('ï') = index++;
trans('È') = trans('è') = index++;
trans('É') = trans('é') = index++;
trans('Ê') = trans('ê') = index++;
trans('Ë') = trans('ë') = index++;
trans('Ð') = index++;
trans('ð') = index++;
trans('Ñ') = trans('ñ') = index++;
trans('Ò') = trans('ò') = index++;
trans('Ó') = trans('ó') = index++;
trans('Ô') = trans('ô') = index++;
trans('Ö') = trans('ö') = index++;
trans('Õ') = trans('õ') = index++;
trans('Ò') = trans('ò') = index++;
trans('Ó') = trans('ó') = index++;
trans('Ô') = trans('ô') = index++;
trans('Ö') = trans('ö') = index++;
trans('Ù') = trans('ù') = index++;
trans('Ú') = trans('ú') = index++;
trans('Û') = trans('û') = index++;
trans('Ü') = trans('ü') = index++;
#endif
for (size_t i = 128; i < 256; ++i) {
if (transform[reorder[i]] == transform_table_size) {
transform[reorder[i]] = index++;
}
}
len = 0;
prev = 0;
reset();
decoder.init();
}
ALWAYS_INLINE void reset() {
h1 = 0x1F20239A;
h2 = 0xBE5FD47A;
len = 0;
}
ALWAYS_INLINE uint32_t getHash() const {
auto ret = (h1 * 15) ^ (h2 * 41);
ret ^= ret >> 4;
return ret;
}
ALWAYS_INLINE uint32_t getPrevHash() const {
return prev;
}
ALWAYS_INLINE uint32_t getMixedHash() const {
auto ret = getHash();
if (len < 2) {
ret ^= getPrevHash();
}
return ret;
}
ALWAYS_INLINE uint32_t get01Hash() const {
return getHash() ^ getPrevHash();
}
ALWAYS_INLINE size_t getLength() const {
return len;
}
// Return true if just finished word.
bool update(uint8_t c) {
const auto cur = transform[c];
if (LIKELY(cur != transform_table_size)) {
h1 = HashFunc(cur, h1);
h2 = h1 * 24;
len += len < 16;
} else if (len) {
prev = rotate_left(getHash() * 21, 14);
reset();
return true;
}
return false;
}
ALWAYS_INLINE uint64_t HashFunc(uint64_t c, uint64_t h) {
/*
h ^= (h * (1 + 24 * 1)) >> 24;
// h *= 61;
h += c;
h += rotate_left(h, 12);
return h ^ (h >> 8);
*/
return (h * 43) + c;
}
};
class DictXMLModel : public WordModel {
// 40 82 6
public:
void Init(const ReorderMap<uint8_t, 256>& reorder) {
last_char_ = 0;
dict_remain_ = 0;
escape_ = reorder[0x3];
upper1_ = reorder[0x4];
upper2_ = reorder[0x6];
WordModel::Init(reorder);
}
void update(uint8_t c) {
if (c >= 128) {
if (last_char_ == escape_) {
WordModel::update(c);
} else if (dict_remain_ == 0) {
if (c < 128 + 40) {
dict_remain_ = 1;
} else if (c < 128 + 40 + 82) {
dict_remain_ = 2;
} else {
dict_remain_ = 3;
}
} else {
--dict_remain_;
}
} else {
dict_remain_ = 0;
}
last_char_ = c;
WordModel::update(c);
}
uint32_t getMixedHash() {
auto ret = WordModel::getMixedHash();
/* if (dict_remain_) {
ret ^= dict_remain_ * 12931991;
} */
return ret;
}
private:
size_t dict_remain_;
uint8_t last_char_;
uint8_t escape_ = 0;
uint8_t upper1_ = 0;
uint8_t upper2_ = 0;
};
class XMLWordModel : public WordModel {
public:
void init(const ReorderMap<uint8_t, 256>& reorder) {
for (auto& c : stack_) c = 0;
stack_pos_ = 0;
last_symbol_ = 0;
tag_ = kTagNone;
WordModel::Init(reorder);
}
void update(uint8_t c) {
bool update = WordModel::update(c);
if (c == '<') {
last_symbol_ = c;
tag_ = kTagOpen;
} else if (c == '/' && last_symbol_ == '<') {
last_symbol_ = c;
tag_ = kTagClose;
} else if (update) {
if (tag_ == kTagOpen) {
// st_[stack_pos_ & kStackMask] = s;
stack_[stack_pos_++ & kStackMask] = prev;
} else if (tag_ == kTagClose && c == '>') {
if (stack_[--stack_pos_ & kStackMask] != prev) {
stack_pos_ = 0;
} else {
int x = 2;
}
}
// Done word.
tag_ = kTagNone;
// s = "";
}
// s += c;
}
uint32_t getMixedHash() {
auto ret = WordModel::getMixedHash();
if (tag_ == kTagClose) {
ret ^= stack_[(stack_pos_ - 1) & kStackMask] * 3;
}
return ret;
}
private:
enum CurrentTag {
kTagOpen,
kTagClose,
kTagNone,
};
// std::string s;
CurrentTag tag_;
static const uint32_t kStackMask = 255;
uint32_t stack_[kStackMask + 1];
// std::string st_[kStackMask + 1];
uint32_t stack_pos_;
uint8_t last_symbol_ = 0;
};
#endif