-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAnchorHashQre.cpp
More file actions
136 lines (92 loc) · 2.17 KB
/
AnchorHashQre.cpp
File metadata and controls
136 lines (92 loc) · 2.17 KB
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
#include "AnchorHashQre.hpp"
#include "./misc/crc32c_sse42_u64.h"
using namespace std;
/** Constructor */
AnchorHashQre::AnchorHashQre (uint32_t a, uint32_t w) {
// Allocate the anchor array
A = new uint32_t [a]();
// Allocate the working array
W = new uint32_t [a]();
// Allocate the last apperance array
L = new uint32_t [a]();
// Allocate the "map diagonal"
K = new uint32_t [a]();
// Initialize "swap" arrays
for(uint32_t i = 0; i < a; ++i) {
L[i] = i;
W[i] = i;
K[i] = i;
}
// We treat initial removals as ordered removals
for(uint32_t i = a - 1; i >= w; --i) {
A[i] = i;
r.push(i);
}
// Set initial set sizes
M = a;
N = w;
}
/** Destructor */
AnchorHashQre::~AnchorHashQre () {
delete [] A;
delete [] W;
delete [] L;
delete [] K;
}
uint32_t AnchorHashQre::ComputeTranslation(uint32_t i , uint32_t j) {
if (i == j) return K[i];
uint32_t b = j;
while (A[i] <= A[b]) {
b = K[b];
}
return b;
}
uint32_t AnchorHashQre::ComputeBucket(uint64_t key1 , uint64_t key2) {
// First hash is uniform on the anchor set
uint32_t bs = crc32c_sse42_u64(key1, key2);
uint32_t b = bs % M;
// Loop until hitting a working bucket
while (A[b] != 0) {
// New candidate (bs - for better balance - avoid patterns)
bs = crc32c_sse42_u64(key1 - bs, key2 + bs);
uint32_t h = bs % A[b];
// h is working or observed by bucket
if ((A[h] == 0) || (A[h] < A[b])) {
b = h;
}
// need translation for (bucket, h)
else {
b = ComputeTranslation(b,h);
}
}
return b;
}
uint32_t AnchorHashQre::UpdateRemoval(uint32_t b) {
// update reserved stack
r.push(b);
// update live set size
N--;
// who is the replacement
W[L[b]] = W[N];
L[W[N]] = L[b];
// Update map diagonal
K[b] = W[N];
// Update removal
A[b] = N;
return 0;
}
uint32_t AnchorHashQre::UpdateNewBucket() {
// Who was removed last?
uint32_t b = r.top();
r.pop();
// Restore in observed_set
L[W[N]] = N;
W[L[b]] = b;
// update live set size
N++;
// Ressurect
A[b] = 0;
// Restore in diagonal
K[b] = b;
return b;
}