Note that there are some explanatory texts on larger screens.

plurals
  1. POSTL hash table resize
    primarykey
    data
    text
    <p>SGI STL use separate chaining to implement hash table.It contains a buckets vector in whick each element is the first node link of the bucket list.Sometimes,if the the elements inserted into the hash table are too many,we have to resize the vector.But when should we resize the vector?The SGI STL do like this:compare the new number of elements with the size of old vector.If the former is larger,then resize the vector.<strong>The element of vector is only the first node link of the bucket list</strong>!But the bucket linklist cancontain many elements,why it doesnot think about this?</p> <p>The following is the SGI STL source code(from <a href="http://www.sgi.com/tech/stl/stl_hashtable.h" rel="nofollow">enter link description here</a>):</p> <pre><code>template &lt;class _Val, class _Key, class _HashFcn, class _ExtractKey, class _EqualKey, class _Alloc&gt; 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&amp; reference; typedef const value_type&amp; const_reference; hasher hash_funct() const { return _M_hash; } key_equal key_eq() const { return _M_equals; } private: typedef _Hashtable_node&lt;_Val&gt; _Node; #ifdef __STL_USE_STD_ALLOCATORS public: typedef typename _Alloc_traits&lt;_Val,_Alloc&gt;::allocator_type allocator_type; allocator_type get_allocator() const { return _M_node_allocator; } private: typename _Alloc_traits&lt;_Node, _Alloc&gt;::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&lt;_Node, _Alloc&gt; _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&lt;_Node*,_Alloc&gt; _M_buckets;//Here!!!!! size_type _M_num_elements; </code></pre> <p>Please see the private element <strong>_M_buckets</strong>.</p> <p>And the vector <strong>resize</strong> code of SGI STL:</p> <pre><code>template &lt;class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All&gt; void hashtable&lt;_Val,_Key,_HF,_Ex,_Eq,_All&gt; ::resize(size_type __num_elements_hint) { const size_type __old_n = _M_buckets.size(); if (__num_elements_hint &gt; __old_n) { const size_type __n = _M_next_size(__num_elements_hint); if (__n &gt; __old_n) { vector&lt;_Node*, _All&gt; __tmp(__n, (_Node*)(0), _M_buckets.get_allocator()); __STL_TRY { for (size_type __bucket = 0; __bucket &lt; __old_n; ++__bucket) { _Node* __first = _M_buckets[__bucket];//Here!! while (__first) { size_type __new_bucket = _M_bkt_num(__first-&gt;_M_val, __n); _M_buckets[__bucket] = __first-&gt;_M_next; __first-&gt;_M_next = __tmp[__new_bucket]; __tmp[__new_bucket] = __first; __first = _M_buckets[__bucket]; } } _M_buckets.swap(__tmp); } </code></pre> <p>And the ease of the bucket vector:</p> <pre><code>template &lt;class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All&gt; void hashtable&lt;_Val,_Key,_HF,_Ex,_Eq,_All&gt; ::_M_erase_bucket(const size_type __n, _Node* __first, _Node* __last) { _Node* __cur = _M_buckets[__n]; if (__cur == __first) _M_erase_bucket(__n, __last); else { _Node* __next; for (__next = __cur-&gt;_M_next; __next != __first; __cur = __next, __next = __cur-&gt;_M_next) ; while (__next != __last) { __cur-&gt;_M_next = __next-&gt;_M_next; _M_delete_node(__next); __next = __cur-&gt;_M_next; --_M_num_elements; } } } </code></pre> <p><strong>We could see that the element of the vector is the first node link of the bucket linklist!</strong></p> <p>My question is ,Why SGI STL do like this?Thx!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload