SGISTL源碼閱讀二十五 hash_map 關聯式容器
前言
前面我們已經分析了底層容器爲紅黑樹的map
和容器hashtable
,而接下來我們要分析的hash_map
,其底層容器爲hashtable
。
深入源碼
hash_map
的定義部分
#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定義別名
typedef hashtable<pair<const Key, T>, Key, HashFcn,
select1st<pair<const Key, T> >, EqualKey, Alloc> ht;
//內部維護一個名爲rep的hashtable
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;
//直接使用了hashtable的迭代器
typedef typename ht::iterator iterator;
typedef typename ht::const_iterator const_iterator;
hash_map
的構造函數
它的構造函數版本非常地多,但是實現卻非常簡單,直接簡單地調用了底層hashtable
提供的方法
public:
//默認構造函數
hash_map() : rep(100, hasher(), key_equal()) {}
//explicit關鍵字,防止隱式轉換
explicit hash_map(size_type n) : rep(n, hasher(), key_equal()) {}
//指定大小n以及哈希函數,其餘取默認值
hash_map(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
//指定大小n及哈希函數和比較key的大小的函數
hash_map(size_type n, const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) {}
#ifdef __STL_MEMBER_TEMPLATES
//我們需要注意到下面所有的插入都是調用的insert_unique
//保證了set中元素的唯一性
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
//以下爲const版本
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 */
hash_map
的相關操作
這些操作也是一樣,直接調用了底層hashtable
所實現的方法。
基本操作
public:
//返回hash_map的大小
size_type size() const { return rep.size(); }
//返回最大長度
size_type max_size() const { return rep.max_size(); }
//判斷該hash_map是否爲空
bool empty() const { return rep.empty(); }
//交換兩個hash_map
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版本
const_iterator begin() const { return rep.begin(); }
const_iterator end() const { return rep.end(); }
//...
//根據key值查找,返回一個迭代器
iterator find(const key_type& key) { return rep.find(key); }
const_iterator find(const key_type& key) const { return rep.find(key); }
//...
//返回key值的個數
size_type count(const key_type& key) const { return rep.count(key); }
//...
public:
//重構
void resize(size_type hint) { rep.resize(hint); }
size_type bucket_count() const { return rep.bucket_count(); }
size_type max_bucket_count() const { return rep.max_bucket_count(); }
size_type elems_in_bucket(size_type n) const
{ return rep.elems_in_bucket(n); }
};
插入操作
我們需要注意的是以下所有的插入操作調用的都是insert_unique
函數,它確定了hash_map
中數據的唯一性。
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值刪除,返回一共刪除了多少個鍵值對
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); }
//將整個hash_map清空
void clear() { rep.clear(); }
操作符重載
//重載了[]
//可以直接通過鍵值訪問實值
T& operator[](const key_type& key) {
return rep.find_or_insert(value_type(key, T())).second;
}
//...
//重載==
//直接比較兩hash_map的內部rep
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;
}
關於hash_multiset
hash_multimap
和 hash_map
的區別等同於multimap
和 map
的區別。
也是由於調用inset
的函數不同而造成的。
深入源碼
下面只貼出了部分代碼
//調用了rep.insert_equal
#ifdef __STL_MEMBER_TEMPLATES
template <class InputIterator>
hash_multimap(InputIterator f, InputIterator l)
: rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
template <class InputIterator>
hash_multimap(InputIterator f, InputIterator l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
template <class InputIterator>
hash_multimap(InputIterator f, InputIterator l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
template <class InputIterator>
hash_multimap(InputIterator f, InputIterator l, size_type n,
const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) { rep.insert_equal(f, l); }
#else
hash_multimap(const value_type* f, const value_type* l)
: rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
hash_multimap(const value_type* f, const value_type* l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
hash_multimap(const value_type* f, const value_type* l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
hash_multimap(const value_type* f, const value_type* l, size_type n,
const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) { rep.insert_equal(f, l); }
hash_multimap(const_iterator f, const_iterator l)
: rep(100, hasher(), key_equal()) { rep.insert_equal(f, l); }
hash_multimap(const_iterator f, const_iterator l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_equal(f, l); }
hash_multimap(const_iterator f, const_iterator l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_equal(f, l); }
hash_multimap(const_iterator f, const_iterator l, size_type n,
const hasher& hf, const key_equal& eql)
//...
public:
iterator insert(const value_type& obj) { return rep.insert_equal(obj); }
#ifdef __STL_MEMBER_TEMPLATES
template <class InputIterator>
void insert(InputIterator f, InputIterator l) { rep.insert_equal(f,l); }
#else
void insert(const value_type* f, const value_type* l) {
rep.insert_equal(f,l);
}
void insert(const_iterator f, const_iterator l) { rep.insert_equal(f, l); }
總結
本次我們分析了hash_map
以及hash_multimap
,可以看到它們底層是由hashtable
實現的,他們大量地調用了hashtable
所實現的方法。
hash_map
支持下標運算,直接通過鍵值去訪問它的實值。並且它的鍵值是不允許改變的。