unordered_map和unordered_set的封装

多看代码,经常复盘

#pragma once
#include<vector>

//仿函数 使能强转成size_t类型的数据强转成size_t类型
template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};
//模板的特化 如果是string类型,通过字符串转化算法,转换成size_t类型
template<>
struct HashFunc<string>
{
	size_t operator()(const string& key)
	{
		size_t hash = 0;
		for (auto ch : key)
		{
			hash *= 131;
			hash += ch;
		}
		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
	{
		typedef HashNode<T> Node;
	public:
		//迭代器作为内部类实现
		template<class Ptr, class Ref>
		struct _HTIterator
		{
			typedef HashNode<T> Node;
			typedef _HTIterator Self;

			Node* _node;//结点指针
			const HashTable* _pht;//哈希表指针

			_HTIterator(Node* node, const HashTable* pht)
				:_node(node)
				, _pht(pht)
			{}

			Ref operator*()
			{
				return _node->_data;
			}
			Ptr 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())
					{
						_node = nullptr;
					}
					else
					{
						_node = _pht->_tables[i];
					}
				}
				return *this;
			}

			bool operator!=(const Self& s)
			{
				return _node != s._node;
			}

		};

		typedef _HTIterator<T*, T&> iterator;
		typedef _HTIterator<const T*, const T&> const_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);
		}


		//const迭代器
		const_iterator begin()const
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
				{
					return const_iterator(cur, this);
				}
			}
			return end();
		}
		const_iterator end() const
		{
			return const_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[i] = cur;
						cur = next;
					}
				}
				_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), true);
		}

		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* prev = nullptr;
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					//删除的是第一个
					if (prev == nullptr)
					{
						_tables[hashi] = cur->_next;
					}
					else
					{
						prev->_next = cur->_next;
					}
					delete cur;
					return true;
				}
				else
				{
					prev = cur;
					cur = cur->_next;
				}
			}
			return false;
		}


	private:
		vector<HashNode<T>*> _tables;
		size_t _n = 0;
	};


}
#pragma once
namespace ljp
{
	template<class K, class V, class Hash = HashFunc<K>>
	class unordered_map
	{
		struct unordered_mapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename hash_bucket::HashTable<K, pair<const K, V>, unordered_mapKeyOfT, Hash>::iterator iterator;
		iterator begin()
		{
			return _ht.begin();
		}
		iterator end()
		{
			return _ht.end();
		}

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _ht.Insert(kv);
		}
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));

			return ret.first->second;
		}


	private:
		hash_bucket::HashTable<K, pair<const K, V>, unordered_mapKeyOfT, Hash> _ht;

	};

	void test_unordered_map()
	{
		string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜",
	"苹果", "香蕉", "苹果", "香蕉","苹果","草莓", "苹果","草莓" };
		unordered_map<string, int> countMap;
		for (auto& e : arr)
		{
			++countMap[e];
		}

		unordered_map<string, int>::iterator it = countMap.begin();
		while (it != countMap.end())
		{
			//it->first += 'x'; // key不能修改
			it->second += 1;  // value可以修改
			cout << it->first << ":" << it->second << endl;
			++it;
		}
		cout << endl;

		for (auto& kv : countMap)
		{
			cout << kv.first << ":" << kv.second << endl;
		}
		cout << endl;
	}
}
#pragma once
namespace ljp
{
	template<class K, class Hash = HashFunc<K>>
	class unordered_set
	{
		struct unordered_setKeyOfT
		{
			const K& operator()(const K& key)
			{
				return	key;
			}
		};
	public:
		typedef typename hash_bucket::HashTable<K, const K, unordered_setKeyOfT, Hash>::iterator iterator;
		typedef typename hash_bucket::HashTable<K, const K, unordered_setKeyOfT, Hash>::const_iterator 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, unordered_setKeyOfT, Hash> _ht;

	};

	void test_unordered_set()
	{
		unordered_set<int> s;
		s.insert(31);
		s.insert(11);
		s.insert(5);
		s.insert(15);
		s.insert(25);

		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;
	}
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

#include"HashTable.h"
#include"Myunordered_map.h"
#include"Myunordered_set.h"
int main()
{
	//ljp::test_unordered_map();
	ljp::test_unordered_set();
	return 0;
}

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值