forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.java
More file actions
249 lines (228 loc) · 8.03 KB
/
Hash.java
File metadata and controls
249 lines (228 loc) · 8.03 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Hash.java
/**
* Copyright (C) 2008 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.util;
public final class Hash {
/** Creates a hash for a string.
* @param s String to hash
* @return the hash code
*/
public static final int hashBackward( String s ) {
int hash = 0;
for ( int i = s.length()-1; i >= 0; i-- )
hash = hash * 31 + s.charAt( i );
return hash;
}
/** Creates a long hash for a string.
* @param s the string to hash
* @return the hash code
*/
public static final long hashBackwardLong( String s ) {
long hash = 0;
for ( int i = s.length()-1; i >= 0; i-- )
hash = hash * 63 + s.charAt( i );
return hash;
}
/** @unexpose */
static final long _longHashConstant = 4095;
/**
* 64-bit hash, using longs, in stead of ints, for less collisions, for when it matters.
* Calls longHash( s , 0 , s.length() )
* @param s The String to hash.
* @return the hash code
*/
public static final long longHash( String s ) {
return longHash( s , 0 , s.length() );
}
/**
* 64-bit hash using longs, starting on index 'start' and including everything before 'end'.
* @param s The string to hash.
* @param start Where to start the hash.
* @param end Where to end the hash.
* @return the hash code
*/
public static final long longHash( String s , int start , int end ) {
long hash = 0;
for ( ; start < end; start++ )
hash = _longHashConstant * hash + s.charAt( start );
return hash;
}
/**
* Same as longHash(String), using only lower-case values of letters.
* Calls longhash( s , 0 , s.length() ).
* @param s The string to Hash.
* @return the hash code
*/
public static final long longLowerHash( String s ) {
return longLowerHash( s , 0 , s.length() );
}
/**
* Long (64-bit) hash, lower-cased, from [start-end)
* @param s The string to hash.
* @param start where to start hashing.
* @param end Where to stop hashing.
* @return the hash code
*/
public static final long longLowerHash( String s , int start , int end ) {
long hash = 0;
for ( ; start < end; start++ )
hash = _longHashConstant * hash + Character.toLowerCase( s.charAt( start ) );
return hash;
}
/**
* Long (64-bit) hash, lower-cased, from [start-end)
* @param s The string to hash.
* @param start where to start hashing.
* @param end Where to stop hashing.
* @return the hash code
*/
public static final long longLowerHash( String s , int start , int end , long hash ) {
for ( ; start < end; start++ )
hash = _longHashConstant * hash + Character.toLowerCase( s.charAt( start ) );
return hash;
}
/** Adds the lower-case equivalent of a character to an existing hash code.
* @param hash the existing hash code
* @param c the character to add
* @return the hash code
*/
public static final long longLowerHashAppend( long hash , char c ) {
return hash * _longHashConstant + Character.toLowerCase( c );
}
/** Adds a character to an existing hash code.
* @param hash the existing hash code
* @param c the character to add
* @return the hash code
*/
public static final long longHashAppend( long hash , char c ) {
return hash * _longHashConstant + c;
}
/**
* This is an exact copy of the String <code>hashCode()</code> function, aside from the lowercasing.
* @param s string to be hashed
* @return the hash code
*/
public static final int lowerCaseHash( String s ) {
int h = 0;
final int len = s.length();
for ( int i = 0; i < len; i++ )
h = 31*h + Character.toLowerCase( s.charAt( i ) );
return h;
}
/**
* Creates a hash code of a lowercase string from [start-end)
* @param s string to be hashed
* @param start the starting index
* @param end the ending index
* @return the hash code
*/
public static final int lowerCaseHash( String s , int start , int end ) {
int h = 0;
final int len = s.length();
for ( int i = start; i < len && i < end; i++ )
h = 31*h + Character.toLowerCase( s.charAt( i ) );
return h;
}
/**
* Creates a hash code of a string from [start-end)
* @param s string to be hashed
* @param start the starting index
* @param end the ending index
* @return the hash code
*/
public static final int hashCode( CharSequence s , int start , int end ) {
int h = 0;
final int len = s.length();
for ( int i = start; i < len && i < end; i++ )
h = 31*h + s.charAt( i );
return h;
}
/**
* Creates a hash code of a lowercase string with whitespace removed from [start-end)
* @param s string to be hashed
* @param start the starting index
* @param end the ending index
* @return the hash code
*/
public static final int nospaceLowerHash( String s , int start , int end ) {
int h = 0;
final int len = s.length();
for ( int i = start; i < len && i < end; i++ ) {
char c = s.charAt( i );
if ( Character.isWhitespace( c ) )
continue;
h = 31*h + Character.toLowerCase( c );
}
return h;
}
/**
* This is an exact copy of the String <code>hashCode()</code> function, aside from the lowercasing.
* No, it's not. It also ignores consecutive whitespace.
* @param s string to be hashed
* @return the hash code
*/
public static final int lowerCaseSpaceTrimHash( String s ) {
int h = 0;
int len = s.length();
while ( len > 1 && Character.isWhitespace( s.charAt( len-1 ) ) )
len--;
boolean lastWasSpace = true;
for ( int i = 0; i < len; i++ ) {
boolean isSpace = Character.isWhitespace( s.charAt( i ) );
if ( isSpace && lastWasSpace )
continue;
lastWasSpace = isSpace;
h = 31*h + Character.toLowerCase( s.charAt( i ) );
}
return h;
}
/**
* Creates a hash code of a lowercase string from [start-end) ignoring whitespace
* @param s string to be hashed
* @param start the starting index
* @param end the ending index
* @return the hash code
*/
public static final int lowerCaseSpaceTrimHash( String s , int start , int end ) {
int h = 0;
int len = s.length();
while ( len > 1 && Character.isWhitespace( s.charAt( len-1 ) ) )
len--;
boolean lastWasSpace = true;
for ( int i = start; i < len && i < end; i++ ) {
boolean isSpace = Character.isWhitespace( s.charAt( i ) );
if ( isSpace && lastWasSpace )
continue;
lastWasSpace = isSpace;
h = 31*h + Character.toLowerCase( s.charAt( i ) );
}
return h;
}
/**
* Calculate the hashcode for a series of strings combined as one.
* @param strings Varargs array of Strings.
* @return A hashcode.
*/
public static final int hashCode( String ... strings ) {
int h = 0;
for ( String s : strings ) {
int len = s.length();
for ( int i = 0; i < len; i++ )
h = 31*h + s.charAt( i );
}
return h;
}
}