大家第一次来这里,但我想首先询问我对双重哈希的理解是否正确。
双哈希工作首先实现哈希函数,然后检查该点是否打开。如果当前点未打开,则使用第二个哈希函数确定另一个点,然后将其乘以当前尝试,然后将其添加到由第一个哈希算法确定的索引点。
我目前的代码是:
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;
}
我刚刚意识到我没有包含我的第二个哈希函数
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被正确调用。
答案 0 :(得分:1)
您的if语句不正确:
if ( ( originalPos + iteration ) > array.size( ) )
index = ( originalPos + iteration ) % array.size( );
else
index = originalPos + iteration;
}
应该是:
if ( ( originalPos + iteration ) >= array.size( ) )
index = ( originalPos + iteration ) % array.size( );
else
index = originalPos + iteration;
}
或者更好,因为你通过if做浪费比%op更多,并且答案是相同的,你可以完全摆脱if:
index = ( originalPos + iteration ) % array.size( );
答案 1 :(得分:0)
或者你可以通过说
来完全简化它unsigned int hashkey = myhash1( x );
unsigned int stepSz = myhash2( x );
while( array[ index ].info != EMPTY && array[ index ].element != x )
hashKey = (hashKey + stepSz) % capacity;
return hashkey;
在使while循环变得更小(并且摆脱额外变量)的同时完成同样的事情。我假设你不想允许重复(因此在while循环中的第二个条件?)。