-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhash.h
executable file
·66 lines (57 loc) · 1.37 KB
/
hash.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
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
/////////////Hash chart
namespace HASH{
const int HASHSIZE = 30000001;
const int MAXELEMENTS = 6000000;
int hash[HASHSIZE + 10];
int list[MAXELEMENTS][2];/////maximum number of elements kept
int total;
void init() {
memset(hash, -1, sizeof(hash));
total = 0;
}
inline int hashFunction(int u, int v) {
int p = u + (v>>4) + ((u<<16)^v);
if (p < 0) p = -p;
return p % HASHSIZE;
}
inline int insert(int u, int v) {
int p = hashFunction(u, v);
int del = 1; //quadratic probing
while(hash[p] >= 0) {
if(u == list[hash[p]][0] && v == list[hash[p]][1])
return 0;
p = p + del;
del ++;
if (p >= HASHSIZE) p -= HASHSIZE;
if (del >= HASHSIZE) del -= HASHSIZE;
}
if(total == MAXELEMENTS) {
puts("uhoh, hash chart full...");
exit(0);
}
list[total][0] = u;
list[total][1] = v;
hash[p] = total;
total++;
return 1;
}
inline int find(int u, int v) {
if(u>v) swap(u, v);
int p = hashFunction(u, v);
int del = 1; //quadratic probing
while(hash[p] >= 0) {
if(u == list[hash[p]][0] &&
v == list[hash[p]][1])
return 1;
p = p + del;
del ++;
if (p >= HASHSIZE) p -= HASHSIZE;
if (del >= HASHSIZE) del -= HASHSIZE;
}
return 0;
}
}