-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathRepHash.hpp
executable file
·320 lines (207 loc) · 8.5 KB
/
RepHash.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
#ifndef BIFROST_REPHASH_HPP
#define BIFROST_REPHASH_HPP
#include <cassert>
#include <stdint.h>
#include "wyhash.h"
static const unsigned char twin[32] = {
0, 20, 2, 7, 4, 5, 6, 3,
8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 1, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31
};
#if MAX_KMER_SIZE <= 64
static const uint64_t hvals[4] = {
2053695854357871005ULL, 5073395517033431291ULL,
10060236952204337488ULL, 7783083932390163561ULL
};
class RepHash {
public:
RepHash(const size_t _k = 0) {
setK(_k);
}
void init(const char* _s) {
h = 0;
ht = 0;
const unsigned char* s = (const unsigned char*) _s;
for (size_t i = 0; i < k; ++i) {
fastleftshift1(h);
fastleftshift1(ht);
h ^= hvals[charmask(s[i])];
ht ^= hvals[twinmask(s[k-1-i])];
}
}
inline void updateFW(const unsigned char out, const unsigned char in) {
uint64_t z(hvals[charmask(out)]);
uint64_t zt(hvals[twinmask(in)]);
fastleftshiftk(z);
fastleftshiftk(zt);
fastleftshift1(h);
h ^= z;
h ^= hvals[charmask(in)];
ht ^= zt;
ht ^= hvals[twinmask(out)];
fastrightshift1(ht);
}
inline void updateBW(const unsigned char out, const unsigned char in) {
uint64_t z(hvals[twinmask(out)]);
uint64_t zt(hvals[charmask(in)]);
fastleftshiftk(z);
fastleftshiftk(zt);
fastleftshift1(ht);
ht ^= z;
ht ^= hvals[twinmask(in)];
h ^= zt;
h ^= hvals[charmask(out)];
fastrightshift1(h);
}
inline void update(const unsigned char out, const unsigned char in){
updateFW(out, in);
}
inline uint64_t hash() const {
uint64_t hashes[2] = {h, ht};
if (hashes[1] < hashes[0]) swap(hashes[0], hashes[1]);
return wyhash(hashes, sizeof(uint64_t) + sizeof(uint64_t), 0, _wyp);
}
inline void setK(const size_t _k) {
k = _k;
h = 0;
ht = 0;
}
private:
inline uint64_t charmask (const unsigned char x) const {
return (x & 6) >> 1;
}
inline uint64_t twinmask (const unsigned char x) const {
return ((x ^ 4) & 6) >> 1;
}
inline void fastleftshiftk(uint64_t& x) const {
x = (x << k) | (x >> (64-k));
}
inline void fastrightshiftk(uint64_t& x) const {
x = (x >> k) | (x << (64-k));
}
inline void fastleftshift1(uint64_t& x) const {
x = (x << 1) | (x >> 63);
}
inline void fastrightshift1(uint64_t& x) const {
x = (x >> 1) | (x << 63);
}
size_t k;
uint64_t h, ht;
string str;
};
#else
struct rep_state_t {
uint64_t hi;
uint64_t lo;
rep_state_t& operator^=(const rep_state_t& o) {
hi ^= o.hi;
lo ^= o.lo;
return *this;
}
rep_state_t() : hi(0), lo(0) {}
rep_state_t(const uint64_t hi_, const uint64_t lo_) : hi(hi_), lo(lo_) {}
};
static const rep_state_t hvals[32] = {
rep_state_t(0x498bf4da68e4a5d2ULL, 0xcd18b2ed2719ae49ULL), rep_state_t(0x87613b9d792b27bfULL, 0x5a9f31e9650d3ac6ULL),
rep_state_t(0x4395a1a049fa76a2ULL, 0x10e4b5bf779adbc8ULL), rep_state_t(0x7544bc518bb54d90ULL, 0x41f3e8de1fe48d6eULL),
rep_state_t(0x8ed640367372ac6cULL, 0xa291bee592c5e1e7ULL), rep_state_t(0x4f1f5965708ec160ULL, 0xe71b6d1444ba099dULL),
rep_state_t(0xc378038bae2f493bULL, 0x70d4008dad7e3aa5ULL), rep_state_t(0x615bf67b70de0cdfULL, 0x208f3adcc8ca1aa4ULL),
rep_state_t(0xff239058a8bedba9ULL, 0x4e06dacb0b57d0d2ULL), rep_state_t(0x6221ac90a2ed270eULL, 0x1485df91c0b2a0efULL),
rep_state_t(0x6aaa09ef06685b06ULL, 0xbddd68547f296a28ULL), rep_state_t(0xba49899319971eb2ULL, 0x105f2a98dd90b3f8ULL),
rep_state_t(0x312a4ac3ddfdc014ULL, 0x412038b51ee959e5ULL), rep_state_t(0xc0cb77a29256270dULL, 0xbe6b7e9d4c6766deULL),
rep_state_t(0xaa74044d51fdf18fULL, 0xa911bddbf8ffa08eULL), rep_state_t(0xcbdc765b04fa21d3ULL, 0xb18ad8b272439eecULL),
rep_state_t(0x82765cedb75bd1c4ULL, 0x9445fa7b930d0503ULL), rep_state_t(0x1179cbc60b28e89cULL, 0x92104ea02ed0534fULL),
rep_state_t(0xaee876b54b4df8daULL, 0xb3eaf5b4a10a08ccULL), rep_state_t(0xc34976b3ef5ee0a4ULL, 0x8210c76de6308193ULL),
rep_state_t(0xee023d391e54a57bULL, 0x96e73dc6786b1f9aULL), rep_state_t(0x9929b98b3762d17fULL, 0xa1594e9916c0a563ULL),
rep_state_t(0x395ecbebcfae31b1ULL, 0x79af8d5843ac918eULL), rep_state_t(0xd50a220a58e47a01ULL, 0x84d8e77ecba6959bULL),
rep_state_t(0x4d710459ed613706ULL, 0x7b2bed1fd86d045eULL), rep_state_t(0xd671765f3784fd94ULL, 0x52d47f000a07bf45ULL),
rep_state_t(0xfbb4a41a674fa47bULL, 0xaf81a75844adead1ULL), rep_state_t(0x55e243ef1134258fULL, 0x5de7beb874b7a97dULL),
rep_state_t(0x85a7787202b8571cULL, 0x50f5fc4dd85517a9ULL), rep_state_t(0xb31bf1fcb1d83e09ULL, 0xa9cbec09c5de31b2ULL),
rep_state_t(0x2a753d679e777b97ULL, 0x29641395bed797e9ULL), rep_state_t(0x1817b8f128494dbcULL, 0xb7fed4eae0bf9afcULL)
};
class RepHash {
public:
RepHash(const size_t _k = 0) {
setK(_k);
}
inline void setK(const size_t _k) {
full_k = _k;
k = _k % 64;
firstkmask = (1ULL << _k) - 1;
lastkmask = firstkmask << (64 - k);
}
uint64_t hash() const {
uint64_t hashes[2] = {h.lo, ht.lo};
if (hashes[1] < hashes[0]) swap(hashes[0], hashes[1]);
return wyhash(hashes, sizeof(uint64_t) + sizeof(uint64_t), 0, _wyp);
}
void init(const char *_s) {
h = rep_state_t();
ht = rep_state_t();
const unsigned char *s = (const unsigned char*) _s;
for (size_t i = 0; i < full_k; ++i) {
fastleftshift1(h);
fastleftshift1(ht);
h ^= hvals[s[i] & charmask];
ht ^= hvals[twin[s[full_k-1-i] & charmask]];
}
}
inline void updateFW(const unsigned char out, const unsigned char in) {
rep_state_t z(hvals[out & charmask]);
rep_state_t zt(hvals[twin[in & charmask]]);
fastleftshift1(h);
fastleftshiftk(z);
fastleftshiftk(zt);
h ^= z;
h ^= hvals[in & charmask];
ht ^= hvals[twin[out & charmask]];
ht ^= zt;
fastrightshift1(ht);
}
inline void updateBW(const unsigned char out, const unsigned char in) {
rep_state_t z(hvals[twin[out & charmask]]);
rep_state_t zt(hvals[in & charmask]);
fastleftshift1(ht);
fastleftshiftk(z);
fastleftshiftk(zt);
ht ^= z;
ht ^= hvals[twin[in & charmask]];
h ^= hvals[out & charmask] ;
h ^= zt;
fastrightshift1(h);
}
inline void update(const unsigned char out, const unsigned char in){
updateFW(out, in);
}
private:
inline void fastleftshiftk(rep_state_t& x) const {
const uint64_t upper = x.hi & lastkmask; // upper k of 64 bits of hi
x.hi = (x.hi << k) | ((x.lo & lastkmask) >> (64 - k));
x.lo = (x.lo << k) | (upper >> (64 - k));
if (full_k & 64) swap(x.hi, x.lo);
}
inline void fastrightshiftk(rep_state_t& x) const {
const uint64_t lower = x.hi & firstkmask; // lower k bits
x.hi = (x.hi >> k) | ((x.lo & firstkmask) << (64 - k));
x.lo = (x.lo >> k) | (lower << (64 - k));
if (full_k & 64) swap(x.hi, x.lo);
}
inline void fastleftshift1(rep_state_t& x) const {
uint64_t last1 = (x.hi & last1mask); // last bit of hi
x.hi = (x.hi << 1) | ((x.lo & last1mask) >> 63);
x.lo = (x.lo << 1) | (last1 >> 63);
}
inline void fastrightshift1(rep_state_t& x) const {
const uint64_t first1 = (x.hi & 1ULL); // first bit of hi
x.hi = (x.hi >> 1) | ((x.lo & 1ULL) << 63);
x.lo = (x.lo >> 1) | (first1 << 63);
}
size_t k, full_k;
uint64_t lastkmask, firstkmask;
static const uint64_t last1mask = 1ULL << 63;
static const unsigned char charmask = 31;
rep_state_t h, ht;
};
#endif
#endif