#ifndef HashTable_cpp
#define HashTable_cpp
#include "HashTable.h"
bool isPrime( int n );
int nextPrime( int n );
template <class Object>
HashTable<Object>::
HashTable()
: array( nextPrime( 101 ) )
{
makeEmpty( );
}
template <class Object>
void HashTable<Object>::
insert( const Object & x )
{
int currentPos = findPos( x );
if( isActive( currentPos ) )
return;
array[ currentPos ] = HashEntry( x, ACTIVE );
if( ++occupied > array.size( ) / 2 )
rehash( );
}
template <class Object>
void HashTable<Object>::
rehash( )
{
vector<HashEntry> oldArray = array;
array.resize( nextPrime( 2 * oldArray.size( ) ) );
for( int j = 0; j < array.size( ); j++ )
array[ j ].info = EMPTY;
makeEmpty( );
for( int i = 0; i < oldArray.size( ); i++ )
if( oldArray[ i ].info == ACTIVE )
insert( oldArray[ i ].element );
}
template <class Object>
unsigned int HashTable<Object>::
hash( const string & key ) const
{
unsigned int hashVal = 0;
for( size_t i = 0; i < key.size(); i++ )
hashVal = ( hashVal << 5 ) ^ key[ i ] ^ hashVal;
return hashVal;
}
template <class Object>
int HashTable<Object>::
findPos( const Object & x )
{
int collisionNum = 0;
int currentPos = hash( x ) % array.size( );
while( array[ currentPos ].info != EMPTY &&
array[ currentPos ].element != x )
{
currentPos += 2 * ++collisionNum - 1; if( currentPos >= array.size( ) )
currentPos -= array.size( );
}
return currentPos;
}
template <class Object>
void HashTable<Object>::
remove( const Object & x )
{
int currentPos = findPos( x );
if( isActive( currentPos ) )
array[ currentPos ].info = DELETED;
}
template <class Object>
Object * HashTable<Object>::
find( const Object & x )
{
int currentPos = findPos( x );
if( isActive( currentPos ) )
return & (array[ currentPos ].element);
else
return 0;
}
template <class Object>
void HashTable<Object>::
makeEmpty( )
{
occupied = 0;
for( int i = 0; i < array.size( ); i++ )
array[ i ].info = EMPTY;
}
template <class Object>
bool HashTable<Object>::
isActive( int currentPos ) const
{
return array[ currentPos ].info == ACTIVE;
}
template <class Object>
bool HashTable<Object>::
isPrime( int n ) const
{
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;
}
template <class Object>
int HashTable<Object>::
nextPrime( int n ) const
{
if( n % 2 == 0 )
n++;
for( ; !isPrime( n ); n += 2 )
;
return n;
}
#endif