SGISTL源码探究-关联式容器:hash_map
来源:互联网 发布:sap采购订单数据库 编辑:程序博客网 时间:2024/06/05 23:00
前言
在上一小节,我们分析了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_TEMPLATEStemplate <class Key, class T, class HashFcn = hash<Key>, class EqualKey = equal_to<Key>, class Alloc = alloc>#elsetemplate <class Key, class T, class HashFcn, class EqualKey, class Alloc = alloc>#endifclass 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_ORDERtemplate <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
。
阅读全文
0 0
- SGISTL源码探究-关联式容器:hash_map
- SGISTL源码探究-关联式容器:set
- SGISTL源码探究-关联式容器:map
- SGISTL源码探究-关联式容器:multiset
- SGISTL源码探究-关联式容器:multimap
- SGISTL源码探究-关联式容器:hash_set
- SGISTL源码探究-关联式容器:hash_multiset
- SGISTL源码探究-关联式容器:hash_multimap
- SGISTL源码探究-vector容器(上)
- SGISTL源码探究-vector容器(下)
- SGISTL源码探究-list容器(上)
- SGISTL源码探究-list容器(下)
- SGISTL源码探究-deque容器(上)
- SGISTL源码探究-deque容器(下)
- SGISTL源码探究-配接器
- STL源码剖析 - 第5章 关联式容器 - hash_map
- SGISTL源码探究-内存池
- SGISTL源码探究-traits技法
- Android studio百度地图 获取release版和debug版的签名
- 第十章 跟踪与运动 opecv cvGoodFeaturesToTrack
- 微信小程序开发经验总结(四)
- Faster-RCNN_TF代码解读16:roi_data_layer/roidb.py
- 五大步骤快速搭建个人网站
- SGISTL源码探究-关联式容器:hash_map
- 解决"taglib definition not consistent with specification version"
- tablayout+底部底部按钮
- 为什么构造函数不能声明为虚函数,析构函数可以
- LeetCode 7 Reverse Integer
- photoshop msvcp120.dll 丢失
- Java数组及其内存分配
- 不用设置路由器就可以让本地电脑当服务器(亲测可用)
- 笔试题