我有一个哈希表,它是成对矢量的向量。
std::vector <std::list<std::pair<K, V> > > bucketVector;
如何为此类内容制作rehash功能?基本上,每当表中元素的数量超过桶向量的大小时,向量需要调整大小
我有这些助手
unsigned long HashTable<K, V>::prime_below (unsigned long n)
{
if (n > max_prime)
{
std::cerr << "** input too large for prime_below()\n";
return 0;
}
if (n == max_prime)
{
return max_prime;
}
if (n <= 1)
{
std::cerr << "** input too small \n";
return 0;
}
// now: 2 <= n < max_prime
std::vector <unsigned long> v (n+1);
setPrimes(v);
while (n > 2)
{
if (v[n] == 1)
return n;
--n;
}
return 2;
}
但是,我不确定如何,或者我是否需要添加rehash功能。
//Sets all prime number indexes to 1. Called by method prime_below(n)
template <typename K, typename V>
void HashTable<K, V>::setPrimes(std::vector<unsigned long>& vprimes)
{
int i = 0;
int j = 0;
vprimes[0] = 0;
vprimes[1] = 0;
int n = vprimes.capacity();
for (i = 2; i < n; ++i)
vprimes[i] = 1;
for( i = 2; i*i < n; ++i)
{
if (vprimes[i] == 1)
for(j = i + i ; j < n; j += i)
vprimes[j] = 0;
}
}