版权声明:本节目由WolfOnTheWay独家播出 https://blog.csdn.net/qq_42214953/article/details/88767859
// 计算哈希值的类
template<typename T>
class CHash
{
public:
int operator()(const T &val)
{
// 默认用除留余数法
return val;
}
}; // string User People
// 线性探测法实现的哈希表结构
template<typename T, typename HashType= CHash<T>>
class CHashTable
{
public:
CHashTable(int size = 3, double lf = 0.75)
:_loadFactor(lf), _usedBuckets(0)
{
// 给哈希表开辟数组空间的
_hashVec.resize(size);
}
void put(const T &val)
{
double lf = _usedBuckets * 1.0 / _hashVec.size();
cout << "size:"<< _hashVec.size() << " loadfactor:" << lf << endl;
if (lf >= _loadFactor)
{
resize();
}
int index = _hash(val) % _hashVec.size();
for (int i = index;;i = (i + 1) % _hashVec.size())
{
// STATE_UNUSE STATE_USE STATE_USED
if (_hashVec[i]._state != STATE_USE)
{
_hashVec[i]._data = val;
_hashVec[i]._state = STATE_USE;
_usedBuckets++;
break;
}
}
}
// 删除哈希表中的元素
void remove(const T &val)
{
int index = _hash(val) % _hashVec.size();
int flag = (index - 1 + _hashVec.size()) % _hashVec.size();
for (int i = index; ;i = (i + 1) % _hashVec.size())
{
if (_hashVec[i]._state == STATE_UNUSE)
return;
if (_hashVec[i]._state == STATE_USE
&&_hashVec[i]._data == val)
{
_hashVec[i]._state = STATE_USED;
_usedBuckets--;
break;
}
if (i == flag)
{
return;
}
}
}
// 在哈希表中查找元素
bool query(const T &val)
{
int index = _hash(val) % _hashVec.size();
int flag = (index - 1 + _hashVec.size()) % _hashVec.size();
for (int i = index; ;i = (i + 1) % _hashVec.size())
{
if (_hashVec[i]._state == STATE_UNUSE)
return;
if (_hashVec[i]._state == STATE_USE
&& _hashVec[i]._data == val)
{
return true;
}
if (i == flag)
{
return false;
}
}
}
private:
// STATE_UNUSE 删除 STATE_USED 0
enum STATE{ STATE_UNUSE, STATE_USE, STATE_USED };
struct Node
{
Node(T data = T())
:_data(data), _state(STATE_UNUSE)
{}
T _data;
STATE _state;
};
//vector<Node> _hashVec;
vector<list<T>> _hashVec;
double _loadFactor; // 记录加载因子
int _usedBuckets;
HashType _hash; // 专门计算T类型对象的哈希值的
//static int _prime[] = {3,11,29, }; 枚举素数
//获取素数
int getPrime(int n)
{
for (int i = n + 1;; ++i)
{
int k = sqrt(i);
int j = 2;
for (; j <= k; ++j)
{
if (i % j == 0)
break;
}
if (j > k)
{
return i;
}
else
{
continue;
}
}
}
// 动态扩容
void resize()
{
vector<Node> _oldhash;
_oldhash.swap(_hashVec);
_usedBuckets = 0;
int prime = getPrime(_oldhash.size());
_hashVec.resize(prime);
//vector<Node> _newhash;
//_newhash.resize(get_prime(_hashVec.size()));
for (int i = 0; i < _oldhash.size(); ++i)
{
// STATE_UNUSE STATE_USE STATE_USED
if (_oldhash[i]._state == STATE_USE)
{
put(_oldhash[i]._data);
/*int index = _hashVec[i]._data % _newhash.size();
for (int j = index; ; j = (j + 1) % _newhash.size())
{
if (_newhash[j]._state == STATE_UNUSE ||
_newhash[j]._state == STATE_USED)
{
_newhash[j]._data = _hashVec[i]._data;
_newhash[j]._state = STATE_USE;
_len++;
_loadFactor = (double)_len / _newhash.size();
break;
}
continue;
}*/
}
}
//_hashVec.erase(_hashVec.begin(), _hashVec.end());
//_hashVec = _newhash;
}
};
int main()
{
CHashTable<int> hash;
srand(time(NULL));
for (int i = 0; i < 8; i++)
{
//hash.put(rand() % 100 + 1);
hash.put(i);
}
hash.put(2);
hash.display();
hash.remove(2);
hash.display();
hash.put(2);
hash.display();
cout << hash.query(3) << endl;
cout << hash.query(2) << endl;
return 0;
}