74 lines
1.5 KiB
C++
74 lines
1.5 KiB
C++
|
#pragma once
|
||
|
|
||
|
#include "HashTable.hpp"
|
||
|
|
||
|
namespace Lenyiin
|
||
|
{
|
||
|
template <class K, class V, class Hash = _Hash<K>>
|
||
|
class Unordered_Map
|
||
|
{
|
||
|
public:
|
||
|
struct MapKOfT
|
||
|
{
|
||
|
const K &operator()(const std::pair<K, V> &kv)
|
||
|
{
|
||
|
return kv.first;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
typedef typename Open_HashTable<K, std::pair<K, V>, MapKOfT, Hash>::iterator iterator;
|
||
|
|
||
|
iterator begin()
|
||
|
{
|
||
|
return _hashTable.begin();
|
||
|
}
|
||
|
|
||
|
iterator end()
|
||
|
{
|
||
|
return _hashTable.end();
|
||
|
}
|
||
|
|
||
|
// 构造函数: 指定哈希表的初始大小
|
||
|
Unordered_Map(size_t bucket_count = 10)
|
||
|
: _hashTable(bucket_count)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
// 析构
|
||
|
~Unordered_Map()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
std::pair<iterator, bool> Insert(const std::pair<K, V> &kv)
|
||
|
{
|
||
|
return _hashTable.Insert(kv);
|
||
|
}
|
||
|
|
||
|
V &operator[](const K &key)
|
||
|
{
|
||
|
std::pair<iterator, bool> ret = _hashTable.Insert(std::make_pair(key, V()));
|
||
|
return ret.first->second;
|
||
|
}
|
||
|
|
||
|
// 查找元素
|
||
|
bool Find(const K &key)
|
||
|
{
|
||
|
return _hashTable.Find(key);
|
||
|
}
|
||
|
|
||
|
// 删除元素
|
||
|
bool Erase(const K &key)
|
||
|
{
|
||
|
return _hashTable.Erase(key);
|
||
|
}
|
||
|
|
||
|
// 打印
|
||
|
void Print() const
|
||
|
{
|
||
|
_hashTable.Print();
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
Open_HashTable<K, std::pair<K, V>, MapKOfT, Hash> _hashTable;
|
||
|
};
|
||
|
}
|