BFGraph
RepHash.hpp
1 #ifndef KALLISTO_REPHASH_H
2 #define KALLISTO_REPHASH_H
3 
4 #include <stdint.h>
5 #include <cassert>
6 #include <time.h>
7 
8 static const unsigned char twin[32] = {
9  0, 20, 2, 7, 4, 5, 6, 3,
10  8, 9, 10, 11, 12, 13, 14, 15,
11  16, 17, 18, 19, 1, 21, 22, 23,
12  24, 25, 26, 27, 28, 29, 30, 31
13 };
14 
15 static const uint64_t hvals[4] = {
16  2053695854357871005ULL, 5073395517033431291ULL,
17  10060236952204337488ULL, 7783083932390163561ULL
18 };
19 
23 class RepHash {
24 public:
25 
26  RepHash(int _k) : k(_k) //, charmask (31)
27  {
28  k = k & 63;
29  seed(0);
30  }
31 
32  RepHash() : k(0) //, charmask(31)
33  {
34  seed(0);
35  }
36 
37  void seed(int s) {}
38 
39 
40  inline void fastleftshiftk(uint64_t& x) {
41  x = (x << k) | (x >> (64-k));
42  }
43 
44  inline void fastrightshiftk(uint64_t& x) {
45  x = (x >> k) | (x << (64-k));
46  }
47 
48  inline void fastleftshift1(uint64_t& x) {
49  x = (x << 1) | (x >> 63);
50  }
51 
52  inline void fastrightshift1(uint64_t& x) {
53  x = (x >> 1) | (x << 63);
54  }
55 
56  inline uint64_t hash() const {
57  return (h ^ ht);
58  }
59 
60  // hash of _s[0:k]
61  void init(const char* _s) {
62  h = uint64_t(0);
63  ht = uint64_t(0);
64  const unsigned char* s = (const unsigned char*) _s;
65  for (size_t i = 0; i < k; i++) {
66  fastleftshift1(h);
67  uint64_t hval = hvals[charmask(s[i])];
68  h ^= hval;
69 
70  fastleftshift1(ht);
71  uint64_t hvalt = hvals[twinmask(s[k-1-i])];
72  ht ^= hvalt;
73  }
74  }
75 
76  // hash of s[1:]+in where s[0] == out
77  inline void update(const unsigned char out, const unsigned char in) {
78  uint64_t z(hvals[charmask(out)]);
79  fastleftshiftk(z);
80  fastleftshift1(h);
81  uint64_t hval(hvals[charmask(in)]);
82  h ^= z;
83  h ^= hval;
84 
85  uint64_t zt(hvals[twinmask(in)]);
86  fastleftshiftk(zt);
87  uint64_t hvalt(hvals[twinmask(out)]);
88  ht ^= hvalt;
89  ht ^= zt;
90  fastrightshift1(ht);
91  }
92 
93  // hash of s+fw
94  inline void extendFW(const unsigned char fw) {
95  // update h, h(a[0.k]) = p(a[k]) + x*h(a[0..k-1])
96  uint64_t hval(hvals[charmask(fw)]);
97  fastleftshift1(h);
98  h ^= hval;
99 
100  // update ht, ht(a[0..k]) = p(~a[k])*x^k + ht(a[0..k-1])
101  uint64_t hvalt(hvals[twinmask(fw)]);
102  fastleftshiftk(hvalt);
103  ht ^= hvalt;
104 
105  // extend k by 1
106  increaseK();
107  }
108 
109  inline void increaseK() {
110  k = (k+1) & 63; // 64 -> 0
111  }
112 
113  void setK(int _k) {
114  h = 0;
115  ht = 0;
116  k = _k;
117 
118  }
119 
120  // hash of bw+s
121  inline void extendBW(const unsigned char bw) {
122  // update h, h(a[-1..k-1]) = p(a[-1])*x^k + h(a[0..k-1])
123  uint64_t hval(hvals[charmask(bw)]);
124  fastleftshiftk(hval);
125  h ^= hval;
126 
127  // update ht, ht(a[-1..k-1]) = p(~a[-1]) + x * ht(a[0..k-1])
128  uint64_t hvalt(hvals[twinmask(bw)]);
129  fastleftshift1(ht);
130  ht ^= hvalt;
131 
132  // extend k by 1
133  increaseK();
134  }
135 
136 
137  inline uint64_t charmask(unsigned char x) {
138  return (x&6)>>1;
139  }
140 
141  inline uint64_t twinmask(unsigned char x) {
142  return ((x^4)&6)>>1;
143  }
144 
145 private:
146  size_t k;
147  //unsigned char charmask;
148  uint64_t h,ht;
149 };
150 
151 
152 
153 
154 #endif // KALLISTO_REPHASH_H
Definition: RepHash.hpp:23