hash table(用乘法散列法实现)
程序员文章站
2024-03-22 16:35:34
...
hash table(用乘法散列法实现)
#ifndef C11LEARN_HASHMULTI_H
#define C11LEARN_HASHMULTI_H
#include "HashDivision.h"
template<typename T>
class HashMulti:public HashDivision<T>
{
private:
long w;
long p;
long long s;
long long two_32;
public:
HashMulti():HashDivision<T>(1<<14){
s = 2654435769;
w = 32;
p = 14;
two_32 = 1;
two_32 = two_32<<32;
}
HashMulti(const HashMulti<T>& multi):HashDivision<T>(1<<14){
s = 2654435769;
w = 32;
p = 14;
two_32 = 1;
two_32 = two_32<<32;
HashDivision<T>::copy(multi);
}
protected:
virtual int hashing(int key);
};
template<typename T>
int HashMulti<T>::hashing(int key){
return ((key*s)%two_32)>>(w-p);
}
#endif //C11LEARN_HASHMULTI_H
测试代码
HashMulti<string> hashMulti;
hashMulti[2] = "hello";
hashMulti[123456] = "world";
cout << hashMulti[2] << endl;
cout << hashMulti[123456] << endl;
乘法哈希和除法哈希的不同点是哈希函数不同,
共同点都是用双向链表解决冲突问题。
推荐阅读