-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperfecthash.h
298 lines (288 loc) · 7.76 KB
/
perfecthash.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
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
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <climits>
#include "testlib.h"
const unsigned long long _SUM_SQR_SIZE = 3ULL;
unsigned long long mysqr(const unsigned long long &x)
{
return x * x;
}
class IHashSet
{
public:
virtual bool isPossibleKey(unsigned long long) const = 0;
virtual bool has(unsigned long long) const = 0;
virtual void insert(unsigned long long) = 0;
virtual void erase(unsigned long long) = 0;
virtual size_t size() const = 0;
virtual void init(const std::vector <unsigned long long> &) = 0;
virtual ~IHashSet(){};
};
class Hash
{
const unsigned long long PRIME = 4294967311LLU;
unsigned long long firstCoefficient, secondCoefficient;
unsigned int setSize;
public:
void generateCoefficients()
{
firstCoefficient = rnd.next(1LLU, PRIME - 1LLU);
secondCoefficient = rnd.next(0LLU, PRIME - 1LLU);
}
void setCoefficients(unsigned long long first, unsigned long long second)
{
firstCoefficient = first;
secondCoefficient = second;
}
void setSetSize(unsigned int size)
{
setSize = size;
}
unsigned long long operator()(unsigned long long curr) const
{
return (((((firstCoefficient >> 32LLU) << 32LLU) * curr) % PRIME + ((firstCoefficient & UINT_MAX) * curr) % PRIME + secondCoefficient) % PRIME) % setSize;
}
};
class EqualElements : public std::exception
{
std::string message;
public:
explicit EqualElements(unsigned long long element)
{
message = "There are two or more " + std::to_string(element) + " elements\n";
}
virtual const char* what() const throw()
{
return message.c_str();
}
};
class ImpossibleElement : public std::exception
{
std::string message;
public:
explicit ImpossibleElement(unsigned long long element)
{
message = "There are no " + std::to_string(element) + " element in basic array\n";
}
virtual const char* what() const throw()
{
return message.c_str();
}
};
void checkEqualElements(unsigned long long a, unsigned long long b)
{
if (a == b)
throw EqualElements(a);
}
class PerfectHashSetLevel2 : public IHashSet
{
Hash hash;
std::vector <unsigned long long> body, hashBody;
std::vector<bool> used, hashBodyUsed;
size_t _size;
void stupidHasEqualKeys()
{
if (body.size() > 1)
{
for (size_t i = 0; i + 1 < body.size(); ++i)
{
for (size_t j = i + 1; j < body.size(); ++j)
{
checkEqualElements(body[i], body[j]);
}
}
}
}
bool goodInsertErase(unsigned long long key, bool isHas)
{
if (has(key) == isHas)
{
used[hash(key)] = (!isHas);
return true;
}
return false;
}
public:
PerfectHashSetLevel2()
{
body.clear();
hashBody.clear();
used.clear();
}
const unsigned long long & operator[](const unsigned long long &index) const
{
return body[index];
}
void push(const unsigned long long &key)
{
body.push_back(key);
}
bool hasCollide()
{
used.assign(mysqr(body.size()), false);
for (std::vector <unsigned long long>::iterator it = body.begin(); it != body.end(); it++)
{
unsigned long long curr = hash(*it);
if (used[curr])
return true;
used[curr] = true;
}
return false;
}
void init(const std::vector<unsigned long long> &arr)
{
_size = 0;
hash.setSetSize(mysqr(arr.size()));
do
hash.generateCoefficients();
while (hasCollide());
hashBody.resize(mysqr(arr.size()));
hashBodyUsed.resize(mysqr(arr.size()), false);
used.assign(mysqr(arr.size()), false);
for (size_t i = 0; i < arr.size(); i++)
{
hashBody[hash(arr[i])] = arr[i];
hashBodyUsed[hash(arr[i])] = true;
}
}
void createHashSet()
{
stupidHasEqualKeys();
init(body);
}
bool isPossibleKey(unsigned long long curr) const
{
return (!hashBody.empty() && hashBodyUsed[hash(curr)] && hashBody[hash(curr)] == curr);
}
bool has(unsigned long long curr) const
{
if (isPossibleKey(curr))
return (used[hash(curr)]);
throw ImpossibleElement(curr);
}
void insert(unsigned long long key)
{
if (goodInsertErase(key, false))
_size++;
}
void erase(unsigned long long key)
{
if (goodInsertErase(key, true))
_size--;
}
size_t tableSize() const
{
return body.size();
}
size_t size() const
{
return _size;
}
void out()
{
for (std::vector <unsigned long long>::iterator it = body.begin(); it != body.end(); it++)
std::cout << *it << " ";
}
};
class PerfectHashSet : public IHashSet
{
private:
Hash hash;
unsigned long long numberOfElements, a, b;
size_t _size;
std::vector < PerfectHashSetLevel2 > firstLevel;
unsigned long long firstA, firstB;
bool goodFirstLevel()
{
if (firstLevel.empty())
return false;
unsigned long long sum = 0ULL;
for (std::vector < PerfectHashSetLevel2 >::iterator it = firstLevel.begin(); it != firstLevel.end(); it++)
sum += mysqr(it->tableSize());
return sum <= (8 * numberOfElements);
}
bool hasEqualKeys() const
{
for (std::vector < PerfectHashSetLevel2 >::const_iterator it = firstLevel.begin(); it != firstLevel.end(); it++)
if (it->tableSize() > 1u)
for (size_t i = 0; i < it->tableSize(); i++)
checkEqualElements((*it)[i], (*it)[(i + 1) % it->tableSize()]);
return false;
}
void out()
{
for (std::vector < PerfectHashSetLevel2 >::iterator it = firstLevel.begin(); it != firstLevel.end(); it++)
{
it->out();
std::cout << std::endl;
}
}
bool goodInsertErase(unsigned long long key, bool isHas)
{
if (!isPossibleKey(key))
throw ImpossibleElement(key);
return has(key) == isHas;
}
public:
void init(const std::vector <unsigned long long> &arr)
{
_size = 0;
if (arr.empty())
{
firstLevel.clear();
return;
}
numberOfElements = arr.size();
hash.setSetSize(numberOfElements);
while (!goodFirstLevel())
{
firstLevel.clear();
firstLevel.resize(numberOfElements);
hash.generateCoefficients();
for (size_t i = 0; i < arr.size(); i++)
firstLevel[hash(arr[i])].push(arr[i]);
hasEqualKeys();
}
for (std::vector < PerfectHashSetLevel2 >::iterator it = firstLevel.begin(); it != firstLevel.end(); it++)
it->createHashSet();
}
bool isPossibleKey(unsigned long long curr) const
{
return firstLevel[hash(curr)].isPossibleKey(curr);
}
bool has(unsigned long long curr) const
{
if (isPossibleKey(curr))
return firstLevel[hash(curr)].has(curr);
throw ImpossibleElement(curr);
}
void insert(unsigned long long curr)
{
if (goodInsertErase(curr, false))
{
firstLevel[hash(curr)].insert(curr);
_size++;
}
}
void erase(unsigned long long curr)
{
if (goodInsertErase(curr, true))
{
firstLevel[hash(curr)].erase(curr);
_size--;
}
}
size_t size() const
{
return _size;
}
void write()
{
out();
}
};