-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenaddressing.cpp
More file actions
278 lines (260 loc) · 6.15 KB
/
openaddressing.cpp
File metadata and controls
278 lines (260 loc) · 6.15 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
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
/*
balazs@balazs:~/munka/dgsearch/udbgraph$ ./openaddressing 100 21 20 |less
balazs@balazs:~/munka/dgsearch/udbgraph$ ./openaddressing 100 11 20 |less
used < 80%
deleted sem lehet a maradek
*/
#include<unordered_set>
#include<cmath>
#include<random>
#include<iostream>
#include<cstdlib>
using namespace std;
bool isPrime(unsigned cand) {
for(unsigned i = unsigned(sqrt(cand)); i > 1; i--) {
if(cand % i == 0) {
return false;
}
}
cout << " " << cand << '\n';
return true;
}
void prime(unsigned cand) {
//cout << "--- " << cand << '\n';
for(unsigned i = 0;; i++) {
if(isPrime(cand + i)) {
return;
}
if(i == 0) {
continue;
}
if(isPrime(cand - i)) {
return;
}
}
}
void primes(void) {
double sqrt2 = sqrt(2.0);
double sqrt22 = sqrt(sqrt2);
double now = 4 * sqrt22;
int i;
for(i = 2; i < 32; i++) {
cout << "!-- " << (1u << i) << '\n';
prime(unsigned(round(now)));
prime(unsigned(round(now *= sqrt2)));
now *= sqrt2;
}
cout << (1 << i) << '\n';
}
#define FREE 0
#define DELETED 1
class Hash {
protected:
static uint32_t constexpr primes[] = {
5, 11, 19, 29, 37, 53, 79, 109, 151, 211, 307, 431, 607, 863,
1217, 1723, 2437, 3449, 4871, 6883, 9743, 13781, 19483, 27551, 38971,
55109, 77933, 110221, 155863, 220447, 311743, 440863, 623477, 881743,
1246963, 1763491, 2493949, 3526987, 4987901, 7053971, 9975803, 14107889,
19951579, 28215799, 39903161, 56431601, 79806341, 112863197, 159612679,
225726419, 319225331, 451452823, 638450719, 902905657, 1276901429,
1805811263, 2553802819, 3611622607
};
static uint32_t constexpr primesLen = sizeof(primes) / sizeof(uint32_t);
uint32_t whichSize = 0;
uint32_t buckets;
uint32_t used;
uint32_t deleted;
uint64_t *table;
public:
Hash(uint32_t size = 5) {
for(whichSize = 0; primes[whichSize] != size && whichSize < primesLen; whichSize++) {
if(primes[whichSize] == size) {
break;
}
}
if(whichSize == primesLen) {
// TODO exception
cout << "nem talaltam: " << size << endl;
}
buckets = size;
table = new uint64_t[buckets];
fill();
used = deleted = 0;
}
~Hash() {
delete[] table;
}
void fill() {
for(uint32_t i = 0; i < buckets; i++) {
table[i] = FREE;
}
}
void stat(const char * const cp, uint32_t i) {
(cout << cp).width(4);
(cout << buckets << " u: ").width(4);
(cout << used << " d: ").width(4);
(cout << deleted << " t: ").width(4);
cout << i << '\n';
}
void insert(uint64_t key) {
if(used + deleted >= double(buckets) * 0.89) {
if(whichSize == primesLen) {
// TODO exception
}
cout << "insert: reallocating... " << buckets << '\n';
uint32_t oldLen = buckets;
uint64_t *oldStuff = table;
buckets = primes[++whichSize];
table = new uint64_t[buckets];
fill();
used = deleted = 0;
// copy content into new table
for(uint32_t i = 0; i < oldLen; i++) {
uint64_t key = oldStuff[i];
if(key != FREE && key != DELETED) {
insert(key);
}
}
delete[] oldStuff;
cout << "insert: done. " << buckets << '\n';
}
uint32_t i;
for(i = 0;; i++) {
uint32_t ind = hash(key, i);
if(table[ind] == FREE || table[ind] == DELETED) {
if(table[ind] == DELETED) {
deleted--;
}
table[ind] = key;
used++;
stat("insert: ", i);
break;
}
}
}
bool find(uint64_t key) {
uint32_t i;
for(i = 0; i < buckets; i++) {
uint32_t ind = hash(key, i);
if(table[ind] == key) {
stat(" find: + ", i);
return true;
}
if(table[ind] == FREE) {
break;
}
}
stat(" find: - ", i);
return false;
}
void remove(uint64_t key) {
if(deleted >= used) {
cout << "remove: reallocating... " << buckets << '\n';
uint32_t oldLen = buckets;
uint64_t *oldStuff = table;
for(whichSize = 0; used >= primes[whichSize]; whichSize++);
if(whichSize < primesLen && used / double(primes[whichSize]) > 0.72) {
whichSize++;
}
buckets = primes[whichSize];
if(buckets > oldLen) {
// TODO exception
cout << "remove realloc: new size larger than old\n";
}
table = new uint64_t[buckets];
fill();
used = deleted = 0;
// copy content into new table
for(uint32_t i = 0; i < oldLen; i++) {
uint64_t k = oldStuff[i];
if(k != FREE && k != DELETED && k != key) {
insert(k);
}
}
delete[] oldStuff;
cout << "remove: done. " << buckets << '\n';
}
else {
size_t i;
for(i = 0; i < buckets; i++) {
size_t ind = hash(key, i);
if(table[ind] == key) {
table[ind] = DELETED;
used--;
deleted++;
stat("remove: + ", i);
return;
}
if(table[ind] == FREE) {
break;
}
}
stat("remove: - ", i);
}
}
protected:
uint32_t hash(uint64_t key, uint32_t disp) {
return uint32_t((key % buckets + disp * (1 + key % (buckets - 1))) % buckets);
}
};
uint32_t constexpr Hash::primes[];
uint32_t constexpr Hash::primesLen;
int main(int argc, char **argv) {
// primes();
std::mt19937_64 generator;
std::uniform_int_distribution<int> distribution(2, numeric_limits<uint64_t>::max());
size_t l = atol(argv[1]);
Hash hash(5);
unordered_set<uint64_t> keys;
size_t ir = atol(argv[2]);
size_t j,i;
for(j = 0; j < l; j++) {
size_t n = distribution(generator) % ir;
switch(distribution(generator) % 4) {
case 0:
cout << "INSERT: " << n << endl;
for(i = 0; i < n; i++) {
uint64_t x = distribution(generator);
hash.insert(x);
keys.insert(x);
}
break;
case 1:
cout << "REMOVE: " << n << endl;
{
auto it = keys.begin();
for(i = 0; i < n && it != keys.end(); i++) {
uint64_t key = *it;
it = keys.erase(it);
hash.remove(key);
}
}
break;
case 2:
cout << "- FIND: " << n << endl;
for(size_t j = 0; j < n; j++) {
uint64_t x = distribution(generator);
hash.find(x);
}
break;
case 3:
cout << "+ FIND: " << n << endl;
{
auto it = keys.begin();
for(i = 0; i < n && it != keys.end(); i++) {
uint64_t key = *it++;
hash.find(key);
}
}
break;
}
}
cout << "REMOVE: " << keys.size() << endl;
auto it = keys.begin();
for(i = 0; it != keys.end(); i++) {
uint64_t key = *it;
it = keys.erase(it);
hash.remove(key);
}
return 0;
}