前言
在上小节中,我们已经分析了hashtable
的实现,它只是hash_set
和hash_map
的预备知识。在本小节中,我们将进入到关联式容器hash_set
的源码中,一窥究竟。
hash_set的实现
由于hash_set
并没有自己定义的迭代器,所以接下来我们先对它的定义部分以及数据结构进行分析,然后是它的构造函数以及常用操作等。
定义部分及数据结构
#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class Value, class HashFcn = hash<Value>,
class EqualKey = equal_to<Value>,
class Alloc = alloc>
#else
template <class Value, class HashFcn, class EqualKey, class Alloc = alloc>
#endif
class hash_set
{
private:
//hash_set内部的hashtable成员变量rep
typedef hashtable<Value, Value, HashFcn, identity<Value>,
EqualKey, Alloc> ht;
ht rep;
public:
//定义的别名部分
typedef typename ht::key_type key_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::const_pointer pointer;
typedef typename ht::const_pointer const_pointer;
typedef typename ht::const_reference reference;
typedef typename ht::const_reference const_reference;
//迭代器是const的,代表不能通过迭代器修改hash_set的值,这个和set类似
typedef typename ht::const_iterator iterator;
typedef typename ht::const_iterator const_iterator;
//通过hashtable取得哈希函数对象还有比较key的函数对象
hasher hash_funct() const { return rep.hash_funct(); }
key_equal key_eq() const { return rep.key_eq(); }
构造函数
public:
//默认大小为100个bucket,但是传进去后会被调整,所以实际上比100更大
hash_set() : rep(100, hasher(), key_equal()) {}
//禁止隐式转换,传入n,指定大小,其余的取默认值
explicit hash_set(size_type n) : rep(n, hasher(), key_equal()) {}
//指定大小n以及哈希函数,其余取默认值
hash_set(size_type n, const hasher& hf) : rep(n, hf, key_equal()) {}
//指定大小n及哈希函数还有比较key的大小的函数
hash_set(size_type n, const hasher& hf, const key_equal& eql)
: rep(n, hf, eql) {}
/* 下面传入迭代器进行构造
* 重载了很多版本,这里就不一一细讲了
* 因为里面的实现都是调用hashtable中的insert_unique函数进行插入
*/
#ifdef __STL_MEMBER_TEMPLATES
template <class InputIterator>
hash_set(InputIterator f, InputIterator l)
: rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
template <class InputIterator>
hash_set(InputIterator f, InputIterator l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
template <class InputIterator>
hash_set(InputIterator f, InputIterator l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
template <class InputIterator>
hash_set(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_set(const value_type* f, const value_type* l)
: rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_set(const value_type* f, const value_type* l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_set(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_set(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_set(const_iterator f, const_iterator l)
: rep(100, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_set(const_iterator f, const_iterator l, size_type n)
: rep(n, hasher(), key_equal()) { rep.insert_unique(f, l); }
hash_set(const_iterator f, const_iterator l, size_type n,
const hasher& hf)
: rep(n, hf, key_equal()) { rep.insert_unique(f, l); }
hash_set(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:
//返回大小,调用hashtable提供的函数
size_type size() const { return rep.size(); }
size_type max_size() const { return rep.max_size(); }
//判空,若为空返回true,还是调用的hashtable提供的函数
bool empty() const { return rep.empty(); }
//交换
void swap(hash_set& hs) { rep.swap(hs.rep); }
friend bool operator== __STL_NULL_TMPL_ARGS (const hash_set&,
const hash_set&);
//调用hashtable提供的begin和end
iterator begin() const { return rep.begin(); }
iterator end() const { return rep.end(); }
public:
/* 插入元素
* 调用hashtable提供的插入操作
* 然后返回一个pair, 第一个元素指向插入的元素,第二个元素代表插入是否成功
*/
pair<iterator, bool> insert(const value_type& obj)
{
pair<typename ht::iterator, bool> p = rep.insert_unique(obj);
return pair<iterator, bool>(p.first, p.second);
}
/* 范围插入,转而调用hashtable提供的insert_unique */
#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)
{
pair<typename ht::iterator, bool> p = rep.insert_unique_noresize(obj);
return pair<iterator, bool>(p.first, p.second);
}
//查找为key的元素,并返回指向其的迭代器
iterator find(const key_type& key) const { return rep.find(key); }
//计算为key的元素个数
size_type count(const key_type& key) const { return rep.count(key); }
pair<iterator, iterator> equal_range(const key_type& key) const
{ return rep.equal_range(key); }
/* 删除操作,也是调用的hashtable提供的函数 */
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); }
};
//重载==操作符,其实也是调用的hashtable中重载的==操作符
template <class Value, class HashFcn, class EqualKey, class Alloc>
inline bool operator==(const hash_set<Value, HashFcn, EqualKey, Alloc>& hs1,
const hash_set<Value, HashFcn, EqualKey, Alloc>& hs2)
{
return hs1.rep == hs2.rep;
}
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
template <class Val, class HashFcn, class EqualKey, class Alloc>
inline void swap(hash_set<Val, HashFcn, EqualKey, Alloc>& hs1,
hash_set<Val, HashFcn, EqualKey, Alloc>& hs2) {
hs1.swap(hs2);
}
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
小结
本小节我们对hash_set
容器的实现进行了分析,可以发现它和红黑树以及set
之间的关系很类似,都是底层实现了之后,set
或hash_set
实现时,基本上都是转而调用底层提供的接口,并且都不能通过迭代器修改值。要说set
和hash_set
的区别,因为底层的实现不同,所以set
支持自动排序,但是hash_set
不支持自动排序。
在下一节中,我们将对hash_map
的实现进行分析。