STL容器:哈希表

// 计算哈希值的类
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;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值