STL源码阅读-hashtable

来源:互联网 发布:在手机上如何开淘宝店 编辑:程序博客网 时间:2024/05/10 04:17

hashtable能够提供常数时间的基本操作,和vector以及queue差不多。hashtable通过hash函数,将变量映射到存储空间中对应位置。hashtable存在最大的问题是容易引起碰撞,对于碰撞的处理就显得尤为重要了。解决hash冲突一般有以下几种方法:

1.开放地址。以发生冲突的哈希地址为自变量,通过某种hash冲突函数得到一个新的哈希地址的方法。常见的开放地址法有:

线性探查法,d0 = h(k), di= (di+1)%m,线性探查法容易出现堆积,也就是当出现若干个相同的同义词之后,会连续的占用空间,从而引起性能下降;

平方映射法,di = (d0+i*i)%m,能够很好地处理堆积问题,但是不能探查到线性表上面的所有空间。

2.拉链法,将具有相同的hash值得链表连接起来,形成串,这样hash表就不记录键值本身了,而是相同词义的单链表头指针。


解析STL中的哈市table,和之前的思路一样,先从hashtable的部分定义开始:

template <class _Val, class _Key, class _HashFcn,          class _ExtractKey, class _EqualKey, class _Alloc>class hashtable {public:  typedef _Key key_type;  typedef _Val value_type;  typedef _HashFcn hasher;  typedef _EqualKey key_equal;  typedef size_t            size_type;  typedef ptrdiff_t         difference_type;  typedef value_type*       pointer;  typedef const value_type* const_pointer;  typedef value_type&       reference;  typedef const value_type& const_reference;  hasher hash_funct() const { return _M_hash; }  key_equal key_eq() const { return _M_equals; }private:  typedef _Hashtable_node<_Val> _Node;#ifdef __STL_USE_STD_ALLOCATORSpublic:  typedef typename _Alloc_traits<_Val,_Alloc>::allocator_type allocator_type;  allocator_type get_allocator() const { return _M_node_allocator; }private:  typename _Alloc_traits<_Node, _Alloc>::allocator_type _M_node_allocator;  _Node* _M_get_node() { return _M_node_allocator.allocate(1); }  void _M_put_node(_Node* __p) { _M_node_allocator.deallocate(__p, 1); }# define __HASH_ALLOC_INIT(__a) _M_node_allocator(__a), #else /* __STL_USE_STD_ALLOCATORS */public:  typedef _Alloc allocator_type;  allocator_type get_allocator() const { return allocator_type(); }private:  typedef simple_alloc<_Node, _Alloc> _M_node_allocator_type;  _Node* _M_get_node() { return _M_node_allocator_type::allocate(1); }  void _M_put_node(_Node* __p) { _M_node_allocator_type::deallocate(__p, 1); }# define __HASH_ALLOC_INIT(__a)#endif /* __STL_USE_STD_ALLOCATORS */private:  hasher                _M_hash;  key_equal             _M_equals;  _ExtractKey           _M_get_key;  vector<_Node*,_Alloc> _M_buckets;  size_type             _M_num_elements;public:  typedef _Hashtable_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>          iterator;  typedef _Hashtable_const_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,                                    _Alloc>          const_iterator;  friend struct  _Hashtable_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>;  friend struct  _Hashtable_const_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>;public:  hashtable(size_type __n,            const _HashFcn&    __hf,            const _EqualKey&   __eql,            const _ExtractKey& __ext,            const allocator_type& __a = allocator_type())    : __HASH_ALLOC_INIT(__a)      _M_hash(__hf),      _M_equals(__eql),      _M_get_key(__ext),      _M_buckets(__a),      _M_num_elements(0)  {    _M_initialize_buckets(__n);  }  hashtable(size_type __n,            const _HashFcn&    __hf,            const _EqualKey&   __eql,            const allocator_type& __a = allocator_type())    : __HASH_ALLOC_INIT(__a)      _M_hash(__hf),      _M_equals(__eql),      _M_get_key(_ExtractKey()),      _M_buckets(__a),      _M_num_elements(0)  {    _M_initialize_buckets(__n);  }  hashtable(const hashtable& __ht)    : __HASH_ALLOC_INIT(__ht.get_allocator())      _M_hash(__ht._M_hash),      _M_equals(__ht._M_equals),      _M_get_key(__ht._M_get_key),      _M_buckets(__ht.get_allocator()),      _M_num_elements(0)  {    _M_copy_from(__ht);  }#undef __HASH_ALLOC_INIT  hashtable& operator= (const hashtable& __ht)  {    if (&__ht != this) {      clear();      _M_hash = __ht._M_hash;      _M_equals = __ht._M_equals;      _M_get_key = __ht._M_get_key;      _M_copy_from(__ht);    }    return *this;  }  ~hashtable() { clear(); }  size_type size() const { return _M_num_elements; }  size_type max_size() const { return size_type(-1); }  bool empty() const { return size() == 0; }  void swap(hashtable& __ht)  {    __STD::swap(_M_hash, __ht._M_hash);    __STD::swap(_M_equals, __ht._M_equals);    __STD::swap(_M_get_key, __ht._M_get_key);    _M_buckets.swap(__ht._M_buckets);    __STD::swap(_M_num_elements, __ht._M_num_elements);  }  iterator begin()  {     for (size_type __n = 0; __n < _M_buckets.size(); ++__n)      if (_M_buckets[__n])        return iterator(_M_buckets[__n], this);    return end();  }  iterator end() { return iterator(0, this); }  const_iterator begin() const  {    for (size_type __n = 0; __n < _M_buckets.size(); ++__n)      if (_M_buckets[__n])        return const_iterator(_M_buckets[__n], this);    return end();  }  const_iterator end() const { return const_iterator(0, this); }

从上面的定义可以看出,hashtable在初始化的时候,调用了函数_M_initialize_buckets,该函数的定义如下:

 void _M_initialize_buckets(size_type __n)  {    const size_type __n_buckets = _M_next_size(__n);//获得比当前n大的第一个质数    _M_buckets.reserve(__n_buckets);//获得hash词义的存储空间    _M_buckets.insert(_M_buckets.end(), __n_buckets, (_Node*) 0);初始化每一个hash表的链表头    _M_num_elements = 0;  }

_M_next_size函数用来获得大于n的第一个质数,该函数的原型如下:

 size_type _M_next_size(size_type __n) const    { return __stl_next_prime(__n); }inline unsigned long __stl_next_prime(unsigned long __n){  const unsigned long* __first = __stl_prime_list;  const unsigned long* __last = __stl_prime_list + (int)__stl_num_primes;  const unsigned long* pos = lower_bound(__first, __last, __n);  return pos == __last ? *(__last - 1) : *pos;}
这个函数从__stl_prime_list这个数组中获得大于n的第一个质数,该数组内容如下:

enum { __stl_num_primes = 28 };static const unsigned long __stl_prime_list[__stl_num_primes] ={  53ul,         97ul,         193ul,       389ul,       769ul,  1543ul,       3079ul,       6151ul,      12289ul,     24593ul,  49157ul,      98317ul,      196613ul,    393241ul,    786433ul,  1572869ul,    3145739ul,    6291469ul,   12582917ul,  25165843ul,  50331653ul,   100663319ul,  201326611ul, 402653189ul, 805306457ul,   1610612741ul, 3221225473ul, 4294967291ul};
可以明显看到,__stl_prime_list总共有28个质数,其中__stl_prime_list[i+1]  大约是__stl_prime_list[i]的两倍,最大的质数是 4294967291ul。

到上面为止,hashtable的初始化就基本完成了。

hashtable的插入有两种函数,insert_equal和insert_unique,如下:

  pair<iterator, bool> insert_unique(const value_type& __obj)  {    resize(_M_num_elements + 1);    return insert_unique_noresize(__obj);  }  iterator insert_equal(const value_type& __obj)  {    resize(_M_num_elements + 1);    return insert_equal_noresize(__obj);  }
这两个函数在执行之前,首先都会调用resize函数,其原型如下:

<pre name="code" class="cpp">template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>  ::resize(size_type __num_elements_hint){  const size_type __old_n = _M_buckets.size();  if (__num_elements_hint > __old_n) {//判断当前hash表的容量是否小于元素的数量,保证装载量a<1    const size_type __n = _M_next_size(__num_elements_hint);//获得下一个nsize,如果没有下一个,就不会进行任何处理。     if (__n > __old_n) {      vector<_Node*, _All> __tmp(__n, (_Node*)(0),                                 _M_buckets.get_allocator());//分配新的空间      __STL_TRY {        for (size_type __bucket = 0; __bucket < __old_n; ++__bucket) {          _Node* __first = _M_buckets[__bucket];          while (__first) {            size_type __new_bucket = _M_bkt_num(__first->_M_val, __n);//计算新的地址            _M_buckets[__bucket] = __first->_M_next;//将元素移过去            __first->_M_next = __tmp[__new_bucket];            __tmp[__new_bucket] = __first;            __first = _M_buckets[__bucket];                    }        }        _M_buckets.swap(__tmp);      }#         ifdef __STL_USE_EXCEPTIONS      catch(...) {        for (size_type __bucket = 0; __bucket < __tmp.size(); ++__bucket) {          while (__tmp[__bucket]) {            _Node* __next = __tmp[__bucket]->_M_next;            _M_delete_node(__tmp[__bucket]);            __tmp[__bucket] = __next;          }        }        throw;      }#         endif /* __STL_USE_EXCEPTIONS */    }  }}

上面的函数说明了,如果hash表的装载因子a>=1的话,就会重新分配空间,并进行新的hash,这也是为什么相邻的质数之间的差距是2倍左右,这样可以在性能之间获得比较好的折中。下面是insert_unique_noresize两个不同版本

template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>pair<typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator, bool> hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>  ::insert_unique_noresize(const value_type& __obj){  const size_type __n = _M_bkt_num(__obj);//获得hash值  _Node* __first = _M_buckets[__n];//获得链表的第一个位置  for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)//确保hash插入的数据不重复     if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))      return pair<iterator, bool>(iterator(__cur, this), false);  _Node* __tmp = _M_new_node(__obj);//倒插法  __tmp->_M_next = __first;  _M_buckets[__n] = __tmp;  ++_M_num_elements;  return pair<iterator, bool>(iterator(__tmp, this), true);}template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>  ::insert_equal_noresize(const value_type& __obj){  const size_type __n = _M_bkt_num(__obj);  _Node* __first = _M_buckets[__n];  for (_Node* __cur = __first; __cur; __cur = __cur->_M_next) //允许有重复。。。    if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj))) {      _Node* __tmp = _M_new_node(__obj);      __tmp->_M_next = __cur->_M_next;      __cur->_M_next = __tmp;      ++_M_num_elements;      return iterator(__tmp, this);    }  _Node* __tmp = _M_new_node(__obj);  __tmp->_M_next = __first;  _M_buckets[__n] = __tmp;  ++_M_num_elements;  return iterator(__tmp, this);}

到这里,hashtable的插入基本就结束了。。。可以看出,hashtable的是实现和标准的hash表的实现还是比较相似的。


到此为止,STL的最基本的部分基本上就完结了,剩下的map、set都是在红黑树和hashtable上面进行封装之后的效果,就不列出来了。。。 令行禁止是件很重要的事情。。




0 0