-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashfunc.cpp
159 lines (135 loc) · 3.68 KB
/
hashfunc.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
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
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <ctime>
using namespace std;
vector<char> charset()
{
return vector<char>(
{ '0','1','2','3','4',
'5','6','7','8','9',
'a','b','c','d','e','f',
'g','h','i','j','k',
'l','m','n','o','p',
'q','r','s','t','u',
'v','w','x','y','z'
});
};
void StrRand(string *Str, int arrSize, int strlen)
{
const auto ch_set = charset();
for(int j = 0; j < arrSize; j++)
for (int i = 0; i < strlen; i++)
Str[j] += ch_set[rand() % 36];
}
string IntToHex(int64_t num)
{
int base = 16;
string res = "";
for (int i = 0; num > 0; i++)
{
int rem = num % base;
if (num == base)
{
res = "10" + res;
break;
}
else
{
if (rem > 9)
res = char('A' + rem - 10) + res;
else
res = char(rem + '0') + res;
}
num /= base;
}
return res;
}
template <typename T>
int Compare(const T* Arr, const int Arr_size)
{
T Arr2[200]{};
int k = 0, k1 = 0;
int flag = 0;
for (int i = 0; i < Arr_size; i++)
{
for (int j = 0; j < Arr_size; j++)
{
flag = 0;
if (i != j && Arr[i] == Arr[j])
{
for (k1 = 0; k1 < k; k1++)
if (Arr2[k1] == Arr[j])
flag = 1;
if (flag != 1)
{
Arr2[k++] = Arr[j];
}
}
}
}
return k;
}
uint32_t LojikHash32(string str)
{
const int p = 19;
uint64_t HashOut = 0, p_pow = 1;
while (str.length() != 6)
{
int strlen = (int)str.length();
char add = 0;
while (!(((add <= 122) && (add >= 97)) || ((add <= 57) && (add >= 48))))
{
if (add < 48)
add += 31 * strlen;
else
add -= 37 * strlen;
}
str += add;
}
for (int i = 0; i < str.length(); i++)
{
HashOut += str[i] * p_pow;
p_pow *= p;
}
if (!((HashOut >> 31) & 1))
HashOut |= 1UL << 31;
return HashOut;
}
int main()
{
clock_t start = 0, end = 0;
double seconds = 0;
srand(time(0));
const int N = 2000; //Number of strings/hashs
int str_size = 6; //String max length
for (int str_size_now = 3; str_size_now <= str_size; str_size_now++)
{
string* StringArr = new string[N]{ "" };
int64_t* HashArr = new int64_t[N]{ 0 };
cout << "String size is " << str_size_now << endl;
StrRand(StringArr, N, str_size_now); // Initialising strings array
for (int k = 0; k < N; k++)
{
HashArr[k] = LojikHash32(StringArr[k]);
//cout << "hash: " << IntToHex(HashArr[k]) << " " << HashArr[k] << endl;
}
cout << "Number of collisions: " << Compare(HashArr, N) << endl;
//Time of compare the hashs
start = clock();
Compare(HashArr, N);
end = clock();
seconds = (double)(end - start) / CLOCKS_PER_SEC;
cout << "The time of compare the hash codes: " << seconds << endl;
//Time of compare the strings
start = clock();
Compare(StringArr, N);
end = clock();
seconds = (double)(end - start) / CLOCKS_PER_SEC;
cout << "The time of compare the strings: " << seconds << endl << endl;
delete[] StringArr;
delete[] HashArr;
}
return 32;
}