前言
在上一小节,我们分析了hash_set
的实现,本小节我们将针对hash_map
进行分析。它大部分也是调用的hashtable
,不过它与map
的区别也是因为底层的实现不同,所以map
具有自动排序功能,而hash_map
没有。
接下来我们来看看hash_map
是如何实现的。
hash_map的实现
还是分为定义部分及其数据结构、构造函数、常用操作这三部分。关于hash_map
的迭代器,是使用的hashtable
的迭代器,并且是const
型的,这就说明hash_map
与map
一样,不允许我们修改key值。
定义及其数据结构
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class Key, class T, class HashFcn = hash<Key>,
class EqualKey = equal_to<Key>,
class Alloc = alloc>
#else
template <class Key, class T, class HashFcn, class EqualKey,
class Alloc = alloc>
#endif
class hash_map
{
private:
//这里定义hashtable成员,pair里面的key是常量类型的
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::size_type size_type;
typedef typename ht::difference_type difference_type;
typedef typename ht::pointer pointer;
typedef typename ht::const_pointer const_pointer;
typedef typename ht::reference reference;
typedef typename ht::const_reference const_reference;
typedef typename ht::iterator iterator;
typedef typename ht::const_iterator const_iterator;
//取得哈希函数以及比较key的函数
hasher hash_funct() const { return rep.hash_funct(); }
key_equal key_eq() const { return rep.key_eq(); }
构造函数
public:
//默认大小为100,不过会被hashtable进行调整
hash_map() : rep(100, hasher(), key_equal()) {}
//禁止隐式转换,只指定大小,其他默认
explicit hash_map(size_type n) : rep(n, hasher(), key_equal()) {}
//指定大小以及哈希函数
hash_map(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
//三者都指定
hash_map(size_type n, const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) {}
/* 根据范围进行构造 */
#ifdef __STL_MEMBER_TEMPLATES
template <class InputIterator>
hash_map(InputIterator f, InputIterator l)
: rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
template <class InputIterator>
hash_map(InputIterator f, InputIterator l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
template <class InputIterator>
hash_map(InputIterator f, InputIterator l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
template <class InputIterator>
hash_map(InputIterator f, InputIterator l, size_type n,
const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) { rep.insert_unique(f, l); }
#else
hash_map(const value_type* f, const value_type* l)
: rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_map(const value_type* f, const value_type* l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_map(const value_type* f, const value_type* l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
hash_map(const value_type* f, const value_type* l, size_type n,
const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) { rep.insert_unique(f, l); }
hash_map(const_iterator f, const_iterator l)
: rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_map(const_iterator f, const_iterator l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_map(const_iterator f, const_iterator l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
hash_map(const_iterator f, const_iterator l, size_type n,
const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) { rep.insert_unique(f, l); }
#endif /*__STL_MEMBER_TEMPLATES */
常用操作
public:
//hash_map里面的元素个数
size_type size() const { return rep.size(); }
size_type max_size() const { return rep.max_size(); }
//判空
bool empty() const { return rep.empty(); }
//交换
void swap(hash_map& hs) { rep.swap(hs.rep); }
friend bool
operator== __STL_NULL_TMPL_ARGS (const hash_map&, const hash_map&);
//返回起始/末尾迭代器
iterator begin() { return rep.begin(); }
iterator end() { return rep.end(); }
const_iterator begin() const { return rep.begin(); }
const_iterator end() const { return rep.end(); }
public:
//插入元素
pair<iterator, bool> insert(const value_type& obj)
{ return rep.insert_unique(obj); }
//范围插入
#ifdef __STL_MEMBER_TEMPLATES
template <class InputIterator>
void insert(InputIterator f, InputIterator l) { rep.insert_unique(f,l); }
#else
void insert(const value_type* f, const value_type* l) {
rep.insert_unique(f,l);
}
void insert(const_iterator f, const_iterator l) { rep.insert_unique(f, l); }
#endif /*__STL_MEMBER_TEMPLATES */
pair<iterator, bool> insert_noresize(const value_type& obj)
{ return rep.insert_unique_noresize(obj); }
//查找键值为key的元素
iterator find(const key_type& key) { return rep.find(key); }
const_iterator find(const key_type& key) const { return rep.find(key); }
//重载[]操作符
T& operator[](const key_type& key) {
//这里调用了之前分析hashtable中的那个查找若没有则插入的函数
return rep.find_or_insert(value_type(key, T())).second;
}
//计算键值为key的元素
size_type count(const key_type& key) const { return rep.count(key); }
pair<iterator, iterator> equal_range(const key_type& key)
{ return rep.equal_range(key); }
pair<const_iterator, const_iterator> equal_range(const key_type& key) const
{ return rep.equal_range(key); }
//删除操作
size_type erase(const key_type& key) {return rep.erase(key); }
void erase(iterator it) { rep.erase(it); }
void erase(iterator f, iterator l) { rep.erase(f, l); }
void clear() { rep.clear(); }
public:
//判断是否需要重建表
void resize(size_type hint) { rep.resize(hint); }
//计算bucket个数
size_type bucket_count() const { return rep.bucket_count(); }
size_type max_bucket_count() const { return rep.max_bucket_count(); }
//计算bucket中的节点数
size_type elems_in_bucket(size_type n) const
{ return rep.elems_in_bucket(n); }
};
//重载==操作符
template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
inline bool operator==(const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm1,
const hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm2)
{
return hm1.rep == hm2.rep;
}
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
template <class Key, class T, class HashFcn, class EqualKey, class Alloc>
inline void swap(hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm1,
hash_map<Key, T, HashFcn, EqualKey, Alloc>& hm2)
{
hm1.swap(hm2);
}
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
小结
本小节介绍了hash_map
的实现,它同样也支持下标运算,这个操作全靠hashtable
中提供的find_or_insert
操作实现。并且hash_map
也不允许键值被改变。
在下节中,我们将分别介绍hash_set
和hash_map
的兄弟hash_multiset
和hash_multimap
。