-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash.cpp
More file actions
83 lines (68 loc) · 1.45 KB
/
hash.cpp
File metadata and controls
83 lines (68 loc) · 1.45 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
//
// Created by user on 12.03.19.
//
#include <iostream>
#define PRIME 65536
unsigned hash(char *string)
{
if (nullptr == string)
return static_cast<unsigned >(-1);
unsigned result = 0;
unsigned mask = static_cast<unsigned >(-1);
char s;
while ((s = *string))
{
result <<= 5;
result ^= s;
mask <<= 2;
mask ^= s;
++string;
}
// return 0xFFFF & (result ^ (result >> 16));
//return 0xFFFF & ((((result) % 3) << 15) | ((result) % 7) << 12 | (((result) % 31) << 7) | (((result) % 127) << 0));
result%=65521;
return (result);
}
int prime();
int main()
{
// prime();
// return 0;
unsigned max = 0;
unsigned probes = 0;
unsigned hash_table[PRIME] = {0,};
char s[33] = "BAzhfdn+ denj shdj, cdd dsiud.))";
for (char k = 'a'; k <= 'z'; ++k)
{
s[28] = k;
for (char n = 'A'; n <= 'Z'; ++n)
{
s[29] = n;
for (char m = 'a'; m <= 'z'; ++m)
{
s[30] = m;
for (char l = '5'; l <= '7'; ++l)
{
probes++;
s[31] = l;
const auto h = hash(s);
hash_table[h]++;
if (max < hash_table[h])
max = hash_table[h];
//std::cout << s << ":" << hash(s) << std::endl;
}
}
}
}
unsigned filled = 0;
for (unsigned n = 0; n < PRIME; ++n)
{
std::cout << n << ":" << hash_table[n] << std::endl;
if (hash_table[n])
filled++;
}
std::cout << "probes:" << probes << std::endl;
std::cout << "max:" << max << std::endl;
std::cout << filled / static_cast<double>(PRIME) << std::endl;
return 0;
}