こんにちは、初めてここにいますが、最初に二重ハッシュの理解が正しいかどうかを尋ねることから始めたいと思います。
ダブルハッシュは、最初にハッシュ関数を実装し、次にそのスポットが開いているかどうかを確認することで機能します。現在のスポットが開いていない場合は、2 番目のハッシュ関数を使用して別のスポットを決定し、それを現在の試行で乗算してから、最初のハッシュ アルゴリズムによって決定されたインデックス スポットに追加します。
私が持っている現在のコードは次のとおりです。
unsigned int findPos(hashedObj& x)
{
int offset = 1;
int iteration = 0;
unsigned int originalPos = myhash1( x );
unsigned int index = originalPos;
unsigned int secondPos = myhash2( x );
while( array[ index ].info != EMPTY && array[ index ].element != x )
{
iteration = offset++ * secondPos;
if ( ( originalPos + iteration ) > array.size( ) )
index = ( originalPos + iteration ) % array.size( );
else
index = originalPos + iteration;
}
return ( index );
}
unsigned int hash1( const string& key, const int Tsize )
{
//start the hashvalue at 0
unsigned int hashVal = 0;
//cout<<" the size of the table is: "<< Tsize <<endl;
//add the ascii value for every word to hashval, multiply by 37 each time
for ( int i = 0; i < key.length(); i++ )
hashVal = 37 * hashVal + key[ i ];
//mod hashval so it remains smaller than the table size
hashVal %= Tsize;
//return the itemes index value
return hashVal;
}
2 番目のハッシュ関数が含まれていないことに気付きました
unsigned int hash2( const string& key, const int Tsize )
{
//store the sum of ascii numerical values
int hashVal = 0;
//add the values of all chars while multiplying each one with a prime number
for ( int i = 0; i < key.length(); i++ )
hashVal = 29 * hashVal + key[ i ];
//mod the hashed value with a prime smaller than the table size, subtract that number
//with the prime just used and return that value
unsigned int index = 44497 - ( hashVal % 44497 );
return index;
}
そのようには見えないかもしれませんが、実際には tsize は正しく呼び出されています。