当前位置: 代码网 > it编程>软件设计>数据结构 > unordered_map和set

unordered_map和set

2024年07月31日 数据结构 我要评论
数据结构,unordered_map和set

前言:本篇文章继续分享新的容器unordered_map和set。前边我们分享过map和set,其底层为红黑树,而unordered_map和set的底层则为哈希表,因此在unordered_map和set的实现中,我们可以效仿许多在map和set的中就分享过的一些知识,所以在本篇文章中,就不对分享过的知识进行重复讲解。

而unordered_map和set与map和set的区别,即为红黑树和哈希表的区别


目录

一.修改hash

二.迭代器

三.完整代码

1.unordered_map

2.unordered_set

四.总结


一.修改hash

我们所实现的普通哈希表肯定是无法直接拿来作为unordered_map和set的底层代码的,所以我们需要对其进行优化修改,完整代码如下:

template<class k>
struct hashfunc
{
	size_t operator()(const k& key)
	{
		return (size_t)key;
	}
};

struct hashstringfunc
{
	size_t operator()(const string& s)
	{
		size_t hash = 0;
		for (auto e : s)
		{
			hash += e;
		}
		return hash;
	}
};

namespace hash_bucket
{
	template<class t>
	struct hashnode
	{
		t _data;
		hashnode<t>* _next;

		hashnode(const t& data)
			:_data(data)
			, _next(nullptr)
		{}
	};
	//前置声明
	template<class k, class t, class keyoft, class hash>
	class hashtable;

	template<class k, class t, class keyoft, class hash>
	struct __iterator
	{
		typedef hashnode<t> node;
		typedef __iterator<k, t, keyoft, hash> self;
		node* _node;
		hashtable<k, t, keyoft, hash>* _pht;
		__iterator(node* node, hashtable<k, t, keyoft, hash>* pht)
			:_node(node)
			,_pht(pht)
		{}

		t& operator*()
		{
			return _node->_data;
		}
		t* operator->()
		{
			return &_node->_data;
		}
		self& operator++()
		{
			if (_node->_next)
				//当前桶没走完,找到下一个节点
				_node = _node->_next;
			else
			{
				//当前桶走完了,找下一个不为空的桶的第一个节点
				keyoft kot;
				hash hs;
				size_t i = hs(kot(_node->_data)) % _pht->_tables.size();
				i++;
				for (; i < _pht->_tables.size(); i++)
				{
					if (_pht->_tables[i])
						break;
				}
				if (i == _pht->_tables.size())
					//所有桶都走完了,置nullptr
					_node = nullptr;
				else
					_node = _pht->_tables[i];
			}
			return *this;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
	};

	template<class k, class t, class keyoft, class hash>
	class hashtable
	{
		typedef hashnode<t> node;
	public:
		//友元
		template<class k, class t, class keyoft, class hash>
		friend struct __iterator;

		typedef __iterator<k, t, keyoft, hash> iterator;
		iterator begin()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				node* cur = _tables[i];
				if (cur)
				{
					return iterator(cur, this);
				}
			}
			return end();
		}
		iterator end()
		{
			return iterator(nullptr, this);
		}
		hashtable()
		{
			_tables.resize(10, nullptr);
			_n = 0;
		}
		~hashtable()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				node* cur = _tables[i];
				while (cur)
				{
					node* next = cur->_next;
					delete cur;

					cur = next;
				}
				_tables[i] = nullptr;
			}
		}
		//插入
		pair<iterator,bool> insert(const t& data)
		{
			keyoft kot;
			hash hs;
			//判断是否存在
			iterator it = find(kot(data));
			if (it != end())
				return make_pair(it,false);
			//扩容
			if (_n == _tables.size())
			{
				vector<node*> newtables(_tables.size() * 2, nullptr);
				for (size_t i = 0; i < _tables.size(); i++)
				{
					node* cur = _tables[i];
					while (cur)
					{
						node* next = cur->_next;
						size_t hashi = hs(kot(cur->_data)) % newtables.size();
						cur->_next = newtables[hashi];
						newtables[hashi] = cur;
						cur = next;
					}
					_tables[i] = nullptr;
				}
				_tables.swap(newtables);
			}

			size_t hashi = hs(kot(data)) % _tables.size();
			node* newnode = new node(data);
			//头插
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			++_n;
			return make_pair(iterator(newnode,this), false);
		}
		//寻找
		iterator find(const k& key)
		{
			keyoft kot;
			hash hs;
			size_t hashi = hs(key) % _tables.size();
			node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
					return iterator(cur,this);
				cur = cur->_next;
			}
			return end();
		}
		bool erase(const k& key)
		{
			keyoft kot;
			hash hs;
			size_t hashi = hs(key) % _tables.size();
			node* cur = _tables[hashi];
			node* prev = nullptr;
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					//删除的是第一个节点
					if (prev == nullptr)
					{
						_tables[hashi] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}
					delete cur;
				}
				else
				{
					prev = cur;
					cur = cur->_next;
				}
			}
			return false;
		}
	private:
		vector<node*> _tables;//指针数组
		size_t _n;
	};

}

这其中包括只对key进行操作的修改,以及当插入的元素为string型时对其进行的特殊处理。都是我们之前所分享过的知识。下面我们重点来看迭代器


二.迭代器

先来看整体结构:

template<class k, class t, class keyoft, class hash>
	struct __iterator
	{
		typedef hashnode<t> node;
		typedef __iterator<k, t, keyoft, hash> self;
		node* _node;
		hashtable<k, t, keyoft, hash>* _pht;
		__iterator(node* node, hashtable<k, t, keyoft, hash>* pht)
			:_node(node)
			,_pht(pht)
		{}

		t& operator*()
		{
			return _node->_data;
		}
		t* operator->()
		{
			return &_node->_data;
		}
		self& operator++()
		{
			if (_node->_next)
				//当前桶没走完,找到下一个节点
				_node = _node->_next;
			else
			{
				//当前桶走完了,找下一个不为空的桶的第一个节点
				keyoft kot;
				hash hs;
				size_t i = hs(kot(_node->_data)) % _pht->_tables.size();
				i++;
				for (; i < _pht->_tables.size(); i++)
				{
					if (_pht->_tables[i])
						break;
				}
				if (i == _pht->_tables.size())
					//所有桶都走完了,置nullptr
					_node = nullptr;
				else
					_node = _pht->_tables[i];
			}
			return *this;
		}
		bool operator!=(const self& s)
		{
			return _node != s._node;
		}
	};

其中最为重要的莫过于++运算符的重载,因为我们的哈希表是闭散列的哈希桶,所以是以指针数组作为底层。

在执行++操作时,需要先判断当前节点的next是否存在,存在就直接为next节点,不存在就说明当前节点已经是其所属的桶里的最后一个节点,那么接下来的操作就是去寻找下一个不为空的桶的第一个节点

此时我们需要先计算出当前节点在数组中的位置,然后通过循环判断其后边的位置是否存在桶如果存在,就返回新桶的第一个节点,不存在,就说明所有的桶都走完了,此时返回空指针nullptr


三.完整代码

1.unordered_map

#include"hash.h"
namespace myunordered_map
{
	template<class k,class v, class hash = hashfunc<k>>
	class unordered_map
	{
		struct mapkeyoft
		{
			const k& operator()(const pair<k, v>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename hash_bucket::hashtable<k, pair<const k, v>, mapkeyoft, hash>::iterator iterator;
		iterator begin()
		{
			return _ht.begin();
		}
		iterator end()
		{
			return _ht.end();
		}
		v& operator[](const k& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, v()));
			return ret.first->second;
		}
		pair<iterator, bool> insert(const pair<k, v>& kv)
		{
			return _ht.insert(kv);
		}
	private:
		hash_bucket::hashtable<k, pair<const k, v>, mapkeyoft, hash> _ht;
	};
}

2.unordered_set

#include"hash.h"
namespace myunordered_set
{
	template<class k, class hash = hashfunc<k>>
	class unordered_set
	{
		struct setkeyoft
		{
			const k& operator()(const k& key)
			{
				return key;
			}
		};
	public:
		typedef typename hash_bucket::hashtable<k, const k, setkeyoft, hash>::iterator iterator;
		iterator begin()
		{
			return _ht.begin();
		}
		iterator end()
		{
			return _ht.end();
		}
		pair<iterator, bool> insert(const k& key)
		{
			return _ht.insert(key);
		}
	private:
		hash_bucket::hashtable<k, const k, setkeyoft, hash> _ht;
	};
}

四.总结

关于unordered_map和set就分享这么多,通过前边的知识的分享和掌握,unordered_map和set的实现也是如鱼得水。

喜欢本篇文章的小伙伴记得一键三连,我们下期再见!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com