前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >用哈希表封装myunordered_map和myunordered_set

用哈希表封装myunordered_map和myunordered_set

作者头像
用户11375356
发布2024-12-24 09:57:54
发布2024-12-24 09:57:54
5300
代码可运行
举报
文章被收录于专栏:学习学习
运行总次数:0
代码可运行

1. 源码及框架分析

SGI-STL30版本源代码中没有unordered_map和unordered_set,SGI-STL30版本是C++11之前的STL 版本,这两个容器是C++11之后才更新的。但是SGI-STL30实现了哈希表,只容器的名字是hash_map 和hash_set,他是作为⾮标准的容器出现的,⾮标准是指⾮C++标准规定必须实现的,源代码在 hash_map/hash_set/stl_hash_map/stl_hash_set/stl_hashtable.h中

hash_map和hash_set的实现结构框架核⼼部分截取出来如下:

代码语言:javascript
代码运行次数:0
运行
复制
// stl_hash_set
template <class Value, class HashFcn = hash<Value>,
	class EqualKey = equal_to<Value>,
	class Alloc = alloc>
class hash_set
{
private:
	typedef hashtable<Value, Value, HashFcn, identity<Value>,
		EqualKey, Alloc> ht;
	ht rep;
public:
	typedef typename ht::key_type key_type;
	typedef typename ht::value_type value_type;
	typedef typename ht::hasher hasher;
	typedef typename ht::key_equal key_equal;
	typedef typename ht::const_iterator iterator;
	typedef typename ht::const_iterator const_iterator;
	hasher hash_funct() const { return rep.hash_funct(); }
	key_equal key_eq() const { return rep.key_eq(); }
};
代码语言:javascript
代码运行次数:0
运行
复制
// stl_hash_map
template <class Key, class T, class HashFcn = hash<Key>,
	class EqualKey = equal_to<Key>,
	class Alloc = alloc>
class hash_map
{
private:
	typedef hashtable<pair<const Key, T>, Key, HashFcn,
		select1st<pair<const Key, T> >, EqualKey, Alloc> ht;
	ht rep;
public:
	typedef typename ht::key_type key_type;
	typedef T data_type;
	typedef T mapped_type;
	typedef typename ht::value_type value_type;
	typedef typename ht::hasher hasher;
	typedef typename ht::key_equal key_equal;
	typedef typename ht::iterator iterator;
	typedef typename ht::const_iterator const_iterator;
};
代码语言:javascript
代码运行次数:0
运行
复制
// stl_hashtable.h
template <class Value, class Key, class HashFcn,
	class ExtractKey, class EqualKey,
	class Alloc>
class hashtable {
public:
	typedef Key key_type;
	typedef Value value_type;
	typedef HashFcn hasher;
	typedef EqualKey key_equal;
private:
	hasher hash;
	key_equal equals;
	ExtractKey get_key;
	typedef __hashtable_node<Value> node;
	vector<node*, Alloc> buckets;
	size_type num_elements;
public:
	typedef __hashtable_iterator<Value, Key, HashFcn, ExtractKey, EqualKey,
		Alloc> iterator;
	pair<iterator, bool> insert_unique(const value_type& obj);
	const_iterator find(const key_type& key) const;
};
template <class Value>
struct __hashtable_node
{
	__hashtable_node* next;
	Value val;
};

这⾥我们就不再画图分析了,通过源码可以看到,结构上hash_map和hash_set跟map和set的完 全类似,复⽤同⼀个hashtable实现key和key/value结构,hash_set传给hash_table的是两个 key,hash_map传给hash_table的是pair<const key,value>

需要注意的是源码⾥⾯跟map/set源码类似,命名⻛格⽐较乱,这⾥⽐map和set还乱,hash_set 模板参数居然⽤的Value命名,hash_map⽤的是Key和T命名,可⻅⼤佬有时写代码也不规范,乱 弹琴。下⾯我们模拟⼀份⾃⼰的出来,就按⾃⼰的⻛格⾛了。

2. 模拟实现unordered_map和unordered_set

2.1 实现出复用哈希表的框架,并⽀持insert

参考源码框架,unordered_map和unordered_set复⽤之前我们实现的哈希表。

我们这⾥相⽐源码调整⼀下,key参数就⽤K,value参数就⽤V,哈希表中的数据类型,我们使⽤ T。

其次跟map和set相⽐⽽⾔unordered_map和unordered_set的模拟实现类结构更复杂⼀点,但是 ⼤框架和思路是完全类似的。因为HashTable实现了泛型不知道T参数导致是K,还是pair, 那么insert内部进⾏插⼊时要⽤K对象转换成整形取模和K⽐较相等,因为pair的value不参与计算取 模,且默认⽀持的是key和value⼀起⽐较相等,我们需要时的任何时候只需要⽐较K对象,所以我 们在unordered_map和unordered_set层分别实现⼀个MapKeyOfT和SetKeyOfT的仿函数传给 HashTable的KeyOfT,然后HashTable中通过KeyOfT仿函数取出T类型对象中的K对象,再转换成 整形取模和K⽐较相等,具体细节参考如下代码实现。

代码语言:javascript
代码运行次数:0
运行
复制
// MyUnorderedSet.h
namespace xc
{
	template<class K,class Hash = HashFunc<K>>
	class unordered_set
	{
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};
	public:
		bool insert(const K& key)
		{
			return _ht.Insert(key);
		}
	private:
		hash_bucket::HashTable<K, const K, SetKeyOfT, Hash> _ht;
	};
}
代码语言:javascript
代码运行次数:0
运行
复制
// MyUnorderedMap.h
namespace xc
{
	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:
		bool insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}
	private:
		hash_bucket::HashTable<K, pair<K, V>, MapKeyOfT, Hash> _ht;
	};
}
代码语言:javascript
代码运行次数:0
运行
复制
// HashTable.h
template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};
namespace hash_bucket
{
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;
		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{}
	};
}

实现步骤: 1、实现哈希表 2、封装unordered_map和unordered_set的框架 解决KeyOfT 3、iterator 4、const_iterator 5、key不⽀持修改的问题 6、operator[]

代码语言:javascript
代码运行次数:0
运行
复制
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
	{
		typedef HashNode<T> Node;
		inline unsigned long __stl_next_prime(unsigned long n)
		{
			static const int __stl_num_primes = 28;
			static const unsigned long __stl_prime_list[__stl_num_primes] =
			{
			53, 97, 193, 389, 769,
			1543, 3079, 6151, 12289, 24593,
			49157, 98317, 196613, 393241, 786433,
			1572869, 3145739, 6291469, 12582917, 25165843,
			50331653, 100663319, 201326611, 402653189, 805306457,
			1610612741, 3221225473, 4294967291
			};
			const unsigned long* first = __stl_prime_list;
			const unsigned long* last = __stl_prime_list + __stl_num_primes;
			const unsigned long* pos = lower_bound(first, last, n);
			return pos == last ? *(last - 1) : *pos;
		}
	public:
		HashTable()
		{
			_tables.resize(__stl_next_prime(_tables.size()), nullptr);
		}
		~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;
			}
		}
		bool Insert(const T& data)
		{
			KeyOfT kot;
			if (Find(kot(data)))
				return false;
			Hash hs;
			size_t hashi = hs(kot(data)) % _tables.size();
			// 负载因⼦==1扩容
			if (_n == _tables.size())
			{
				vector<Node*> newtables(__stl_next_prime(_tables.size()),
					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);
			}
			// 头插
			Node* newnode = new Node(data);
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			++_n;
			return true;
		}
	private:
		vector<Node*> _tables; // 指针数组
		size_t _n = 0; // 表中存储数据个数
	};
}

2.2 ⽀持iterator的实现

iterator核⼼源代码

template <class Value, class Key, class HashFcn,     class ExtractKey, class EqualKey, class Alloc> struct __hashtable_iterator {     typedef hashtable<Value, Key, HashFcn, ExtractKey, EqualKey, Alloc>         hashtable;     typedef __hashtable_iterator<Value, Key, HashFcn,         ExtractKey, EqualKey, Alloc>         iterator;     typedef __hashtable_const_iterator<Value, Key, HashFcn,         ExtractKey, EqualKey, Alloc>         const_iterator;     typedef __hashtable_node<Value> node;     typedef forward_iterator_tag iterator_category;     typedef Value value_type;     node* cur;     hashtable* ht;     __hashtable_iterator(node* n, hashtable* tab) : cur(n), ht(tab) {}     __hashtable_iterator() {}     reference operator*() const { return cur->val; } #ifndef __SGI_STL_NO_ARROW_OPERATOR     pointer operator->() const { return &(operator*()); } #endif /* __SGI_STL_NO_ARROW_OPERATOR */     iterator& operator++();     iterator operator++(int);     bool operator==(const iterator& it) const { return cur == it.cur; }     bool operator!=(const iterator& it) const { return cur != it.cur; } }; template <class V, class K, class HF, class ExK, class EqK, class A> __hashtable_iterator<V, K, HF, ExK, EqK, A>& __hashtable_iterator<V, K, HF, ExK, EqK, A>::operator++() {     const node* old = cur;     cur = cur->next;     if (!cur) {         size_type bucket = ht->bkt_num(old->val);         while (!cur && ++bucket < ht->buckets.size())             cur = ht->buckets[bucket];     }     return *this; }

iterator实现思路分析

iterator实现的⼤框架跟list的iterator思路是⼀致的,⽤⼀个类型封装结点的指针,再通过重载运算 符实现,迭代器像指针⼀样访问的⾏为,要注意的是哈希表的迭代器是单向迭代器。

这⾥的难点是operator++的实现。iterator中有⼀个指向结点的指针,如果当前桶下⾯还有结点, 则结点的指针指向下⼀个结点即可。如果当前桶⾛完了,则需要想办法计算找到下⼀个桶。这⾥的 难点是反⽽是结构设计的问题,参考上⾯的源码,我们可以看到iterator中除了有结点的指针,还 有哈希表对象的指针,这样当前桶⾛完了,要计算下⼀个桶就相对容易多了,⽤key值计算出当前 桶位置,依次往后找下⼀个不为空的桶即可。

begin()返回第⼀个桶中第⼀个节点指针构造的迭代器,这⾥end()返回迭代器可以⽤空表⽰。

unordered_set的iterator也不⽀持修改,我们把unordered_set的第⼆个模板参数改成const K即 可,HashTable<K,const K,SetKeyOfT,Hash>_ht;

unordered_map的iterator不⽀持修改key但是可以修改value,我们把unordered_map的第⼆个 模板参数pair的第⼀个参数改成const K即可, HashTable<K,pair<const K,V>,MapKeyofT,Hash>_ht;

⽀持完整的迭代器还有很多细节需要修改,具体参考下⾯题的代码。

代码语言:javascript
代码运行次数:0
运行
复制
//前置声明,因为我们要使用HashTable所以需要声明,因为它定义在后面
template<class K,class T,class KeyOfT,class Hash>
class HashTable;

//迭代器
//第二个为类型
template<class K, class T,class Ref,class Ptr,class KeyOfT ,class Hash>
struct HTIterator
{
	typedef HashNode<T> Node;
	typedef HashTable<K, T, KeyOfT, Hash> HT;
	typedef HTIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;
	Node* _node;
	const HT* _ht;

	HTIterator(Node* node, const HT* ht)
		:_node(node)
		,_ht(ht)
	{}
	
	Ref operator*()
	{
		return _node->_data;
	}
	
	Ptr operator->()
	{
		return &_node->_data;
	}
	
	bool operator !=(const Self& s)
	{
		return _node != s._node;
	}

	Self& operator++()
	{
		if (_node->_next)
		{
			// 当前桶还有数据,走到当前桶下一个节点
			_node = _node->_next;
		}
		else
		{
			// 当前桶走完了,找下一个不为空的桶
			KeyOfT kot;//取key
			Hash hash;//类型转换
			size_t hashi = hash(kot(_node->_data)) % _ht->_tables.size();
			++hashi;//从下一个位置开始找
			while (hashi < _ht->_tables.size())
			{
				_node = _ht->_tables[hashi];
				if (_node)
					//找到了
					break;
				else
					++hashi;
			}
			//所有桶都走完了,end()给的空标识_node
			if (hashi == _ht->_tables.size())
			{
				_node = nullptr;
			}
		}
		return *this;
	}
};
代码语言:javascript
代码运行次数:0
运行
复制
template<class K, class T, class KeyOfT, class Hash>
class HashTable
{
	//友元函数声明
	template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
	friend struct HTIterator;

	typedef HashNode<T>Node;
public:
	typedef HTIterator<K, T, T&, T*, KeyOfT, Hash> Iterator;
	typedef HTIterator<K, T, const T&, const T*, KeyOfT, Hash> ConstIterator;

	Iterator Begin()
	{
		if (_n == 0)
		{
			return End();
		}
		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);
	}

	ConstIterator Begin() const
	{
		if (_n == 0)
		{
			return End();
		}
		for (size_t i = 0; i < _tables.size(); i++)
		{
			Node* cur = _tables[i];
			if (cur)
			{
				return ConstIterator(cur, this);
			}
		}
		return End();
	}
	ConstIterator End() const
	{
		return ConstIterator(nullptr, this);
	}

	HashTable()
		:_tables(__stl_next_prime(0))
		, _n(0)
	{}

	//拷贝构造
	HashTable(const HashTable<K, T, KeyOfT, Hash>& ht)
	{
		_tables.resize(ht._tables.size(), nullptr);
		for (size_t i = 0; i < ht._tables.size(); i++)
		{
			Node* cur = ht._tables[i];
			while (cur)
			{
				Node* newnode = new Node(cur->_data);
				if (_tables[i] == nullptr)
				{
					_tables[i] = newnode;
					++_n;
				}
				else
				{
					newnode->_next = _tables[i];
					_tables[i] = newnode;
					++_n;
				}
				cur = cur->_next;
			}
		}
	}

	//赋值重载
	HashTable<K, T, KeyOfT, Hash> operator=(HashTable<K, T, KeyOfT, Hash> ht)
	{
		_tables.swap(ht._tables);
		swap(_n, ht._n);
		return *this;
	}

	//析构
	~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;
		Iterator it = Find(kot(data));
		//如果为真说明数据已存在
		if (it != End())
			return{ it,false };

		Hash hash;//转为整型

		// 负载因子 == 1时扩容
		if (_n == _tables.size())
		{
			vector<Node*> newtable(__stl_next_prime(_tables.size() + 1));//+1是因为好取素数表的值,如果值为53那么取大于等于53一直加自己
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					//头插到新表
					size_t hashi = hash(kot(cur->_data)) % newtable.size();
					cur->_next = newtable[hashi];
					newtable[hashi] = cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}
			_tables.swap(newtable);
		}
		size_t hashi = hash(kot(data)) % _tables.size();
		//头插
		Node* newnode = new Node(data);
		newnode->_next = _tables[hashi];
		_tables[hashi] = newnode;
		++_n;
		return { Iterator(newnode,this),true };
	}
	Iterator Find(const K& key)
	{
		KeyOfT kot;
		Hash hash;
		size_t hashi = hash(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)
	{
		Hash hash;
		size_t hashi = hash(key) % _tables.size();
		Node* prev = nullptr;
		Node* cur = _tables[hashi];
		while (cur)
		{
			if (cur->_kv.first == key)
			{
				if (prev == nullptr)
				{
					//头删
					_tables[hashi] = cur->_next;//让它指向下一个
				}
				else
				{
					//中间节点
					prev->_next = cur->_next;
				}
				delete cur;
				--_n;
				return true;
			}
			else
			{
				prev = cur;
				cur = cur->_next;
			}
		}
		return false;
	}
private:
	vector<Node*> _tables;// 指针数组
	size_t _n; // 表中存储数据个数
};

2.3 map⽀持[] 

unordered_map要⽀持[]主要需要修改insert返回值⽀持,修改HashTable中的insert返回值为pair<Iterator,bool>Insert(const T& data)

2.4 bit::unordered_map和bit::unordered_set代码实现

MyUnorderedMap.h 
代码语言:javascript
代码运行次数:0
运行
复制
// MyUnorderedMap.h
#pragma once
#include "HashTable.h"
// MyUnorderedMap.h
namespace xc
{
	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;//返回key
			}
		};
	public:
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::Iterator iterator;
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash>::ConstIterator const_iterator;

		iterator begin()
		{
			return _ht.Begin();
		}
		iterator end()
		{
			return _ht.End();
		}
		const_iterator begin() const
		{
			return _ht.Begin();
		}
		const_iterator end() const
		{
			return _ht.End();
		}
		pair<iterator, bool>insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}
		iterator Find(const K& key)
		{
			return _ht.Find(key);
		}
		bool Erase(const K& key)
		{
			return _ht.Erase(key);
		}

	private:

		hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT, Hash> _ht;//这里普通迭代器也不能修改key所以要加上const
	};
	void test_map1()
	{
		unordered_map<string, string> dict;
		dict.insert({ "sort", "排序" });
		dict.insert({ "字符串", "string" });

		dict.insert({ "sort", "排序" });
		dict.insert({ "left", "左边" });
		dict.insert({ "right", "右边" });

		for (auto& kv : dict)
		{
			cout << kv.first << ":" << kv.second << endl;
		}
		cout << endl;

		unordered_map<string, string>::iterator it = dict.begin();
		while (it != dict.end())
		{
			// 不能修改first,可以修改second
			//it->first += 'x';
			it->second += 'x';
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;
	}
}
MyUnorderedSet.h 
代码语言:javascript
代码运行次数:0
运行
复制
#pragma once
#include"HashTable.h"
namespace xc
{
	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;
		typedef typename hash_bucket::HashTable < K, const K, SetKeyOfT, Hash>::ConstIterator const_iterator;

		iterator begin()
		{
			return _ht.Begin();
		}
		iterator end()
		{
			return _ht.End();
		}
		const_iterator begin() const
		{
			return _ht.Begin();
		}
		const_iterator end() const
		{
			return _ht.End();
		}
		pair<iterator, bool>insert(const K& key)
		{
			return _ht.Insert(key);
		}
		iterator Find(const K& key)
		{
			return _ht.Find(key);
		}
		bool Erase(const K& key)
		{
			return _ht.Erase(key);
		}
	private:
		hash_bucket::HashTable<K, const K, SetKeyOfT, Hash> _ht;
	};


	void print(const unordered_set<int>& s)
	{
		unordered_set<int>::const_iterator it = s.begin();
		//while (it != s.end())
		//{
		//	//*it = 1;
		//	cout << *it << " ";
		//	++it;
		//}
		//cout << endl;

		for (auto e : s)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_set1()
	{
		int a[] = { 3,11,86,7,88,82,1,81,5,6,7,6 };
		unordered_set<int> s;
		for (auto e : a)
		{
			s.insert(e);
		}
		unordered_set<int>::iterator it = s.begin();
		//while (it != s.end())
		//{
		//	//*it = 1;
		//	cout << *it << " ";
		//	++it;
		//}
		//cout << endl;

		//for (auto e : s)
		//{
		//	cout << e << " ";
		//}
		//cout << endl;

		print(s);
		//拷贝构造
		unordered_set<int> s1(s);
		print(s1);

	}
}

2.5HashTable所有代码

代码语言:javascript
代码运行次数:0
运行
复制
//仿函数(把类型强转)
template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};

// 特化
template<>
struct HashFunc<string>
{
	size_t operator()(const string& s)
	{
		//BKDR
		size_t hash = 0;
		for (auto ch : s)
		{
			hash += ch;
			hash *= 131;//乘以131可以减少相同数
		}
		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 Ref,class Ptr,class KeyOfT ,class Hash>
	struct HTIterator
	{
		typedef HashNode<T> Node;
		typedef HashTable<K, T, KeyOfT, Hash> HT;
		typedef HTIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;
		Node* _node;
		const HT* _ht;

		HTIterator(Node* node, const HT* ht)
			:_node(node)
			,_ht(ht)
		{}
		
		Ref operator*()
		{
			return _node->_data;
		}
		
		Ptr operator->()
		{
			return &_node->_data;
		}
		
		bool operator !=(const Self& s)
		{
			return _node != s._node;
		}

		Self& operator++()
		{
			if (_node->_next)
			{
				// 当前桶还有数据,走到当前桶下一个节点
				_node = _node->_next;
			}
			else
			{
				// 当前桶走完了,找下一个不为空的桶
				KeyOfT kot;//取key
				Hash hash;//类型转换
				size_t hashi = hash(kot(_node->_data)) % _ht->_tables.size();
				++hashi;//从下一个位置开始找
				while (hashi < _ht->_tables.size())
				{
					_node = _ht->_tables[hashi];
					if (_node)
						//找到了
						break;
					else
						++hashi;
				}
				//所有桶都走完了,end()给的空标识_node
				if (hashi == _ht->_tables.size())
				{
					_node = nullptr;
				}
			}
			return *this;
		}
	};
	template<class K, class T, class KeyOfT, class Hash>
	class HashTable
	{
		//友元函数声明
		template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
		friend struct HTIterator;

		typedef HashNode<T>Node;
	public:
		typedef HTIterator<K, T, T&, T*, KeyOfT, Hash> Iterator;
		typedef HTIterator<K, T, const T&, const T*, KeyOfT, Hash> ConstIterator;

		Iterator Begin()
		{
			if (_n == 0)
			{
				return End();
			}
			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);
		}

		ConstIterator Begin() const
		{
			if (_n == 0)
			{
				return End();
			}
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
				{
					return ConstIterator(cur, this);
				}
			}
			return End();
		}
		ConstIterator End() const
		{
			return ConstIterator(nullptr, this);
		}

		HashTable()
			:_tables(__stl_next_prime(0))
			, _n(0)
		{}

		//拷贝构造
		HashTable(const HashTable<K, T, KeyOfT, Hash>& ht)
		{
			_tables.resize(ht._tables.size(), nullptr);
			for (size_t i = 0; i < ht._tables.size(); i++)
			{
				Node* cur = ht._tables[i];
				while (cur)
				{
					Node* newnode = new Node(cur->_data);
					if (_tables[i] == nullptr)
					{
						_tables[i] = newnode;
						++_n;
					}
					else
					{
						newnode->_next = _tables[i];
						_tables[i] = newnode;
						++_n;
					}
					cur = cur->_next;
				}
			}
		}

		//赋值重载
		HashTable<K, T, KeyOfT, Hash> operator=(HashTable<K, T, KeyOfT, Hash> ht)
		{
			_tables.swap(ht._tables);
			swap(_n, ht._n);
			return *this;
		}

		//析构
		~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;
			Iterator it = Find(kot(data));
			//如果为真说明数据已存在
			if (it != End())
				return{ it,false };

			Hash hash;//转为整型

			// 负载因子 == 1时扩容
			if (_n == _tables.size())
			{
				vector<Node*> newtable(__stl_next_prime(_tables.size()+1));//+1是因为好取素数表的值,如果值为53那么取大于等于53一直加自己
				for (size_t i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						//头插到新表
						size_t hashi = hash(kot(cur->_data)) % newtable.size();
						cur->_next = newtable[hashi];
						newtable[hashi] = cur;
						cur = next;
					}
					_tables[i] = nullptr;
				}
				_tables.swap(newtable);
			}
			size_t hashi = hash(kot(data)) % _tables.size();
			//头插
			Node* newnode = new Node(data);
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			++_n;
			return { Iterator(newnode,this),true };
		}
		Iterator Find(const K& key)
		{
			KeyOfT kot;
			Hash hash;
			size_t hashi = hash(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)
		{
			Hash hash;
			size_t hashi = hash(key) % _tables.size();
			Node* prev = nullptr;
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (cur->_kv.first == key)
				{
					if (prev == nullptr)
					{
						//头删
						_tables[hashi] = cur->_next;//让它指向下一个
					}
					else
					{
						//中间节点
						prev->_next = cur->_next;
					}
					delete cur;
					--_n;
					return true;
				}
				else
				{
					prev = cur;
					cur = cur->_next;
				}
			}
			return false;
		}
	private:
		vector<Node*> _tables;// 指针数组
		size_t _n; // 表中存储数据个数
	};
}

结束语

通过封装哈希表实现 myunorderedSet和 myunorderedmap我们不仅加深了对哈希表工作原理的理解,还掌握了设计和实现复杂数据结构的方法。这些自定义的数据结构不仅提供了高效的性能,还让我们对底层细节有了更深入的控制和优化空间。 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-12-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 源码及框架分析
  • 2. 模拟实现unordered_map和unordered_set
    • 2.1 实现出复用哈希表的框架,并⽀持insert
    • 2.2 ⽀持iterator的实现
      • iterator核⼼源代码
      • iterator实现思路分析
    • 2.3 map⽀持[] 
    • 2.4 bit::unordered_map和bit::unordered_set代码实现
      • MyUnorderedMap.h 
      • MyUnorderedSet.h 
    • 2.5HashTable所有代码
  • 结束语
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档