资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

【干货】C++哈希桶(开链法解决哈希冲突)类的实现-创新互联

开链法(哈希桶)是解决哈希冲突的常用手法,结构如下:

创新互联的客户来自各行各业,为了共同目标,我们在工作上密切配合,从创业型小企业到企事业单位,感谢他们对我们的要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。专业领域包括做网站、网站建设、电商网站开发、微信营销、系统平台开发。

【干货】C++哈希桶(开链法解决哈希冲突)类的实现

    数据结构的设计思路是这样的,定义一个K—V的链式节点(Node),以数组方式存储节点指针

    实现代码如下:

#include
#include"HashTable.h"
size_t GetSize()
{
	static size_t index = 0;
	const int _PrimeSize = 28;
	static const unsigned long _PrimeList[_PrimeSize] =
	{
		53ul, 97ul, 193ul, 389ul, 769ul,
		1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
		49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
		1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
		50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
		1610612741ul, 3221225473ul, 4294967291ul
	};
	return _PrimeList[index++];
}
template
struct HashBucketNode
{
	HashBucketNode(const K& key, const V& value)
	:_key(key)
	, _value(value)
	, _next(NULL)
	{}
	K _key;
	V _value;
	HashBucketNode* _next;
};
template >
class HashBucket
{
public:
	typedef HashBucketNode Node;
	HashBucket()
		:_size(0)
	{
			_tables.resize(0);
	}
	bool Push(const K& key, const V& value)
	{
		_CheckCapacity();
		size_t index = HashFunc()(key) % _tables.size();
		Node*cur = _tables[index];
		while (cur)
		{
			if (cur->_key == key&&cur->_value == value)
				return false;
			cur = cur->_next;
		}
		Node*tmp = new Node(key, value);
		if (_tables[index])
		{
			_tables[index]->_next = tmp->_next;
		}
		_tables[index] = tmp;
		++_size;
		return true;
	}
	void Swap(HashBucket & h)
	{
		swap(_size, h._size);
		_tables.swap(h._tables);
	}
	Node* find(const K& key, const V& value)
	{
		size_t index = HashFunc()(key) % _tables.size();
		Node*cur = _tables[index];
		while (cur)
		{
			if (cur->_key == key&&cur->_value == value)
				return cur;
			cur = cur->_next;
		}
		return NULL;
	}
	bool Remove(const K& key)
	{
		size_t index = HashFunc()(key) % _tables.size();
		if (_tables[index])
		{
			if (_tables[index]->key == key)
			{
				Node*tmp = _tables[index];
				_tables[index] = tmp->_next;
				delete tmp;
				return true;
			}
			else
			{
				Node*cur = _tables[index];
				while (cur->_next)
				{
					if (cur->_next->_key == key)
					{
						Node*tmp = cur->_next;
						cur->_next = cur->_next->_next;
						delete tmp;
						return true;
					}
					cur = cur->_next;
				}
			}
		}
		return false;
	}
protected:
	void _CheckCapacity()
	{
		if (_size >= _tables.size())
		{
			HashBucket tmp;
			tmp._tables.resize(GetSize());
			for (size_t i = 0; i < tmp._tables.size(); ++i)
			{
				Node*cur = tmp._tables[i];
				while (cur)
				{
					tmp.Push(cur->_key, cur->_value);
					cur = cur->_next;
				}
			}
			Swap(tmp);
		}
	}
protected:
	vector _tables;
	size_t _size;
};

    如有不足希望指正,有疑问希望提出

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页标题:【干货】C++哈希桶(开链法解决哈希冲突)类的实现-创新互联
浏览路径:http://cdkjz.cn/article/gohhh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220