-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlidingWindow.h
107 lines (68 loc) · 2.58 KB
/
SlidingWindow.h
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
//
// Created by ale on 01-07-20.
//
#ifndef RRPAIR_SLIDINGWINDOW_H
#define RRPAIR_SLIDINGWINDOW_H
#include <cstdint>
namespace big_repair{
template < typename HashFunction, typename hash_type = uint64_t>
class SlidingWindow{
public:
SlidingWindow() = default;
~SlidingWindow() = default;
virtual const uint32_t sizeWindow() = 0;
virtual const hash_type hashAddCharToWindow(const int& c ) = 0;
virtual void reset() = 0;
};
template <typename hash_type = uint64_t>
class KRPSlindingWindow {
protected:
int wsize;
int *window;
int asize;
const hash_type prime = 27162335252586509;
// const hash_type prime = 1999999973;
hash_type hash;
hash_type tot_char;
hash_type asize_pot; // asize^(wsize-1) mod prime
public:
KRPSlindingWindow() {
wsize = 0;
window = nullptr;
asize = 0;
prime = 27162335252586509;
// prime = 1999999973;
hash = 0;
tot_char = 0;
asize_pot = 0;
}
KRPSlindingWindow(const int& ws ) : wsize(ws){
asize = 256;
asize_pot = 1;
for (int i = 1; i < wsize; i++)
asize_pot = (asize_pot * asize) % prime; // ugly linear-time power algorithm
// alloc and clear window
window = new int[wsize];
for (int i = 0; i < wsize; i++) window[i] = 0;
// init hash value and related values
hash = tot_char = 0;
}
~KRPSlindingWindow() { delete[] window; }
virtual uint32_t sizeWindow() const { return wsize;}
virtual hash_type hashAddCharToWindow(const int& c ) {
int k = tot_char++ % wsize;
// complex expression to avoid negative numbers
hash += (prime - (window[k] * asize_pot) % prime); // remove window[k] contribution
hash = (asize * hash + c) % prime; // add char i
window[k] = c;
// cerr << get_window() << " ~~ " << window << " --> " << hash << endl;
return hash;
}
virtual void reset() {
for (int i = 0; i < wsize; i++) window[i] = 0;
// init hash value and related values
hash = tot_char = 0;
}
};
}
#endif //RRPAIR_SLIDINGWINDOW_H