-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash.cpp
132 lines (111 loc) · 2.08 KB
/
Hash.cpp
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
#include <algorithm>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#define all(c) c.begin(), c.end()
#define MAX 1000
typedef unsigned long long ull;
typedef long double ld;
typedef double type;
using namespace std;
class hash
{
public:
int size;
vector <bool> table;
hash(int _sz = 257)
{
table.resize(_sz, false);
size = _sz;
}
int h1(int i)
{
return (i*i) % size;
}
int h2(int i)
{
return (7*i + 64 + i*i) % size;
}
int h3(int i)
{
return i % size;
}
int h4(int i)
{
int k = i;
for (int j = 2; j <= i; j++)
k = (k * i) % size;
return k;
}
int h5(int i)
{
return (i ^ (i >> 4)) % size;
}
int h6(int i)
{
return (i*i ^ i) % size;
}
int h7(int i)
{
return (i << 5 ^ i >> 2) % size;
}
void add(int i)
{
table[h1(i)] = true;
table[h2(i)] = true;
table[h3(i)] = true;
table[h4(i)] = true;
table[h5(i)] = true;
table[h6(i)] = true;
table[h7(i)] = true;
}
bool search(int i)
{
if (table[h1(i)])
if (table[h2(i)])
if (table[h3(i)])
if (table[h4(i)])
if (table[h5(i)])
if (table[h6(i)])
if (table[h7(i)])
return true;
return false;
}
};
int main()
{
// freopen("in.txt", "r+", stdin);
// freopen("out.txt", "w+", stdout);
hash h(10007);
int n, k = 0, q;
set <int> s;
s.clear();
n = h.size / 2;
for (int i = 0; i <= n; i++)
{
do
q = rand();
while (s.find(q) != s.end());
if (h.search(q))
{
k++;
cout << "error on " << q << " iter = " << i <<endl;
}
h.add(q);
s.insert(q);
}
cout << "fails - " << k << endl;
// fclose(stdin);
// fclose(stdout);
return 0;
}