-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeparateChainingHashTable.java
More file actions
217 lines (178 loc) · 5.36 KB
/
SeparateChainingHashTable.java
File metadata and controls
217 lines (178 loc) · 5.36 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
import java.util.LinkedList;
import java.util.List;
// SeparateChaining Hash table class
//
// CONSTRUCTION: an approximate initial size or default of 101
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// boolean contains( x ) --> Return true if x is present
// void makeEmpty( ) --> Remove all items
/**
* Separate chaining table implementation of hash tables.
* Note that all "matching" is based on the equals method.
* @author Mark Allen Weiss
*/
public class SeparateChainingHashTable<AnyType>
{
/**
* Construct the hash table.
*/
public SeparateChainingHashTable( )
{
this( DEFAULT_TABLE_SIZE );
}
/**
* Construct the hash table.
* @param size approximate table size.
*/
public SeparateChainingHashTable( int size )
{
theLists = new LinkedList[ nextPrime( size ) ];
for( int i = 0; i < theLists.length; i++ )
theLists[ i ] = new LinkedList<>( );
}
/**
* Insert into the hash table. If the item is
* already present, then do nothing.
* @param x the item to insert.
*/
public void insert( AnyType x )
{
List<AnyType> whichList = theLists[ myhash( x ) ];
if( !whichList.contains( x ) )
{
whichList.add( x );
// Rehash; see Section 5.5
if( ++currentSize > theLists.length )
rehash( );
}
}
/**
* Remove from the hash table.
* @param x the item to remove.
*/
public void remove( AnyType x )
{
List<AnyType> whichList = theLists[ myhash( x ) ];
if( whichList.contains( x ) )
{
whichList.remove( x );
currentSize--;
}
}
/**
* Find an item in the hash table.
* @param x the item to search for.
* @return true if x isnot found.
*/
public boolean contains( AnyType x )
{
List<AnyType> whichList = theLists[ myhash( x ) ];
return whichList.contains( x );
}
/**
* Make the hash table logically empty.
*/
public void makeEmpty( )
{
for( int i = 0; i < theLists.length; i++ )
theLists[ i ].clear( );
currentSize = 0;
}
/**
* A hash routine for String objects.
* @param key the String to hash.
* @param tableSize the size of the hash table.
* @return the hash value.
*/
public static int hash( String key, int tableSize )
{
int hashVal = 0;
for( int i = 0; i < key.length( ); i++ )
hashVal = 37 * hashVal + key.charAt( i );
hashVal %= tableSize;
if( hashVal < 0 )
hashVal += tableSize;
return hashVal;
}
private void rehash( )
{
List<AnyType> [ ] oldLists = theLists;
// Create new double-sized, empty table
theLists = new List[ nextPrime( 2 * theLists.length ) ];
for( int j = 0; j < theLists.length; j++ )
theLists[ j ] = new LinkedList<>( );
// Copy table over
currentSize = 0;
for( List<AnyType> list : oldLists )
for( AnyType item : list )
insert( item );
}
private int myhash( AnyType x )
{
int hashVal = x.hashCode( );
hashVal %= theLists.length;
if( hashVal < 0 )
hashVal += theLists.length;
return hashVal;
}
private static final int DEFAULT_TABLE_SIZE = 101;
/** The array of Lists. */
private List<AnyType> [ ] theLists;
private int currentSize;
/**
* Internal method to find a prime number at least as large as n.
* @param n the starting number (must be positive).
* @return a prime number larger than or equal to n.
*/
private static int nextPrime( int n )
{
if( n % 2 == 0 )
n++;
for( ; !isPrime( n ); n += 2 )
;
return n;
}
/**
* Internal method to test if a number is prime.
* Not an efficient algorithm.
* @param n the number to test.
* @return the result of the test.
*/
private static boolean isPrime( int n )
{
if( n == 2 || n == 3 )
return true;
if( n == 1 || n % 2 == 0 )
return false;
for( int i = 3; i * i <= n; i += 2 )
if( n % i == 0 )
return false;
return true;
}
// Simple main
public static void main( String [ ] args )
{
SeparateChainingHashTable<Integer> H = new SeparateChainingHashTable<>( );
long startTime = System.currentTimeMillis( );
final int NUMS = 2000000;
final int GAP = 37;
System.out.println( "Checking... (no more output means success)" );
for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS )
H.insert( i );
for( int i = 1; i < NUMS; i+= 2 )
H.remove( i );
for( int i = 2; i < NUMS; i+=2 )
if( !H.contains( i ) )
System.out.println( "Find fails " + i );
for( int i = 1; i < NUMS; i+=2 )
{
if( H.contains( i ) )
System.out.println( "OOPS!!! " + i );
}
long endTime = System.currentTimeMillis( );
System.out.println( "Elapsed time: " + (endTime - startTime) );
}
}