-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringHashFamily.java
More file actions
34 lines (28 loc) · 817 Bytes
/
StringHashFamily.java
File metadata and controls
34 lines (28 loc) · 817 Bytes
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
import java.util.Random;
public class StringHashFamily implements HashFamily<String>
{
private final int [ ] MULTIPLIERS;
private final Random r = new Random( );
public StringHashFamily( int d )
{
MULTIPLIERS = new int [ d ];
generateNewFunctions( );
}
public int getNumberOfFunctions( )
{
return MULTIPLIERS.length;
}
public void generateNewFunctions( )
{
for( int i = 0; i < MULTIPLIERS.length; i++ )
MULTIPLIERS[ i ] = r.nextInt( );
}
public int hash( String x, int which )
{
final int multiplier = MULTIPLIERS[ which ];
int hashVal = 0;
for( int i = 0; i < x.length( ); i++ )
hashVal = multiplier * hashVal + x.charAt( i );
return hashVal;
}
}