-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashFunctions.java
More file actions
208 lines (159 loc) · 5.33 KB
/
HashFunctions.java
File metadata and controls
208 lines (159 loc) · 5.33 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
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
/*
**************************************************************************
* *
* General Purpose Hash Function Algorithms Library *
* *
* Author: Arash Partow - 2002 *
* URL: http://www.partow.net *
* URL: http://www.partow.net/programming/hashfunctions/index.html *
* *
* Copyright notice: *
* Free use of the General Purpose Hash Function Algorithms Library is *
* permitted under the guidelines and in accordance with the most current *
* version of the Common Public License. *
* http://www.opensource.org/licenses/cpl.php *
* *
**************************************************************************
*/
public class HashFunctions
{
/** RS hash function -- this is the one we should be using in Numerate */
public static long RSHash(String str, int maxlength)
{
int b = 378551;
int a = 63689;
long hash = 0;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = hash * a + str.charAt(i);
a = a * b;
}
return hash;
}
/* End Of RS Hash Function */
public static long JSHash(String str, int maxlength)
{
long hash = 1315423911;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));
}
return hash;
}
/* End Of JS Hash Function */
public static long PJWHash(String str, int maxlength)
{
long BitsInUnsignedInt = (long)(4 * 8);
long ThreeQuarters = (long)((BitsInUnsignedInt * 3) / 4);
long OneEighth = (long)(BitsInUnsignedInt / 8);
long HighBits = (long)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
long hash = 0;
long test = 0;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = (hash << OneEighth) + str.charAt(i);
if((test = hash & HighBits) != 0)
{
hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));
}
}
return hash;
}
/* End Of P. J. Weinberger Hash Function */
public static long ELFHash(String str, int maxlength)
{
long hash = 0;
long x = 0;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = (hash << 4) + str.charAt(i);
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return hash;
}
/* End Of ELF Hash Function */
public static long BKDRHash(String str, int maxlength)
{
long seed = 131; // 31 131 1313 13131 131313 etc..
long hash = 0;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = (hash * seed) + str.charAt(i);
}
return hash;
}
/* End Of BKDR Hash Function */
public static long SDBMHash(String str, int maxlength)
{
long hash = 0;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;
}
return hash;
}
/* End Of SDBM Hash Function */
public static long DJBHash(String str, int maxlength)
{
long hash = 5381;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = ((hash << 5) + hash) + str.charAt(i);
}
return hash;
}
/* End Of DJB Hash Function */
public static long DEKHash(String str, int maxlength)
{
long hash = str.length();
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
}
return hash;
}
/* End Of DEK Hash Function */
public static long BPHash(String str, int maxlength)
{
long hash = 0;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash = hash << 7 ^ str.charAt(i);
}
return hash;
}
/* End Of BP Hash Function */
public static long FNVHash(String str, int maxlength)
{
long fnv_prime = 0x811C9DC5;
long hash = 0;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
hash *= fnv_prime;
hash ^= str.charAt(i);
}
return hash;
}
/* End Of FNV Hash Function */
public static long APHash(String str, int maxlength)
{
long hash = 0xAAAAAAAA;
for(int i = 0; i < str.length() && (i < maxlength || maxlength <= 0); i++)
{
if ((i & 1) == 0)
{
hash ^= ((hash << 7) ^ str.charAt(i) * (hash >> 3));
}
else
{
hash ^= (~((hash << 11) + str.charAt(i) ^ (hash >> 5)));
}
}
return hash;
}
/* End Of AP Hash Function */
}