_hashtable.h

来自「stl的源码」· C头文件 代码 · 共 659 行 · 第 1/2 页

H
659
字号
/* * * Copyright (c) 1994 * Hewlett-Packard Company * * Copyright (c) 1996,1997 * Silicon Graphics Computer Systems, Inc. * * Copyright (c) 1997 * Moscow Center for SPARC Technology * * Copyright (c) 1999 * Boris Fomitchev * * This material is provided "as is", with absolutely no warranty expressed * or implied. Any use is at your own risk. * * Permission to use or copy this software for any purpose is hereby granted * without fee, provided the above notices are retained on all copies. * Permission to modify the code and to distribute modified code is granted, * provided the above notices are retained, and a notice that the code was * modified is included with the above copyright notice. * *//* NOTE: This is an internal header file, included by other STL headers. *   You should not attempt to use it directly. */#ifndef _STLP_INTERNAL_HASHTABLE_H#define _STLP_INTERNAL_HASHTABLE_H#ifndef _STLP_INTERNAL_VECTOR_H#  include <stl/_vector.h>#endif#ifndef _STLP_INTERNAL_SLIST_H#  include <stl/_slist.h>#endif#ifndef _STLP_INTERNAL_ITERATOR_H#  include <stl/_iterator.h>#endif#ifndef _STLP_INTERNAL_FUNCTION_BASE_H#  include <stl/_function_base.h>#endif#ifndef _STLP_INTERNAL_ALGOBASE_H#  include <stl/_algobase.h>#endif#ifndef _STLP_HASH_FUN_H#  include <stl/_hash_fun.h>#endif/* * Hashtable class, used to implement the hashed associative containers * hash_set, hash_map, hash_multiset, hash_multimap, * unordered_set, unordered_map, unordered_multiset, unordered_multimap. */_STLP_BEGIN_NAMESPACE#if defined (_STLP_USE_TEMPLATE_EXPORT)//Export of the classes used to represent buckets in the hashtable implementation.#  if !defined (_STLP_USE_PTR_SPECIALIZATIONS)//If pointer specialization is enabled vector<_Slist_node_base*> will use the void*//storage type for which internal classes have already been exported._STLP_EXPORT_TEMPLATE_CLASS allocator<_STLP_PRIV _Slist_node_base*>;_STLP_MOVE_TO_PRIV_NAMESPACE_STLP_EXPORT_TEMPLATE_CLASS _STLP_alloc_proxy<_Slist_node_base**, _Slist_node_base*,                                              allocator<_Slist_node_base*> >;_STLP_EXPORT_TEMPLATE_CLASS _Vector_base<_Slist_node_base*,                                         allocator<_Slist_node_base*> >;_STLP_MOVE_TO_STD_NAMESPACE#  endif#  if defined (_STLP_DEBUG)_STLP_MOVE_TO_PRIV_NAMESPACE#    define _STLP_NON_DBG_VECTOR _STLP_NON_DBG_NAME(vector)_STLP_EXPORT_TEMPLATE_CLASS __construct_checker<_STLP_NON_DBG_VECTOR<_Slist_node_base*, allocator<_Slist_node_base*> > >;_STLP_EXPORT_TEMPLATE_CLASS _STLP_NON_DBG_VECTOR<_Slist_node_base*, allocator<_Slist_node_base*> >;#    undef _STLP_NON_DBG_VECTOR_STLP_MOVE_TO_STD_NAMESPACE#  endif_STLP_EXPORT_TEMPLATE_CLASS vector<_STLP_PRIV _Slist_node_base*,                                   allocator<_STLP_PRIV _Slist_node_base*> >;#endif#if defined (_STLP_DEBUG)#  define hashtable _STLP_NON_DBG_NAME(hashtable)_STLP_MOVE_TO_PRIV_NAMESPACE#endif// some compilers require the names of template parameters to be the sametemplate <class _Val, class _Key, class _HF,          class _Traits, class _ExK, class _EqK, class _All>class hashtable;#if !defined (_STLP_DEBUG)_STLP_MOVE_TO_PRIV_NAMESPACE#endiftemplate <class _BaseIte, class _Traits>struct _Ht_iterator {  typedef typename _Traits::_ConstTraits _ConstTraits;  typedef typename _Traits::_NonConstTraits _NonConstTraits;  typedef _Ht_iterator<_BaseIte,_Traits> _Self;  typedef typename _Traits::value_type value_type;  typedef typename _Traits::pointer pointer;  typedef typename _Traits::reference reference;  typedef forward_iterator_tag iterator_category;  typedef ptrdiff_t difference_type;  typedef size_t size_type;  typedef _Ht_iterator<_BaseIte, _NonConstTraits> iterator;  typedef _Ht_iterator<_BaseIte, _ConstTraits> const_iterator;  _Ht_iterator() {}  //copy constructor for iterator and constructor from iterator for const_iterator  _Ht_iterator(const iterator& __it) : _M_ite(__it._M_ite) {}  _Ht_iterator(_BaseIte __it) : _M_ite(__it) {}  reference operator*() const {    return *_M_ite;  }  _STLP_DEFINE_ARROW_OPERATOR  _Self& operator++() {    ++_M_ite;    return *this;  }  _Self operator++(int) {    _Self __tmp = *this;    ++*this;    return __tmp;  }  bool operator == (const_iterator __rhs) const {    return _M_ite == __rhs._M_ite;  }  bool operator != (const_iterator __rhs) const {    return _M_ite != __rhs._M_ite;  }  _BaseIte _M_ite;};_STLP_MOVE_TO_STD_NAMESPACE#if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)template <class _BaseIte, class _Traits>struct __type_traits<_STLP_PRIV _Ht_iterator<_BaseIte, _Traits> > {  typedef __false_type   has_trivial_default_constructor;  typedef __true_type    has_trivial_copy_constructor;  typedef __true_type    has_trivial_assignment_operator;  typedef __true_type    has_trivial_destructor;  typedef __false_type   is_POD_type;};#endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */#if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)template <class _BaseIte, class _Traits>inline#  if defined (_STLP_NESTED_TYPE_PARAM_BUG)_STLP_TYPENAME_ON_RETURN_TYPE _Traits::value_type *#  else_STLP_TYPENAME_ON_RETURN_TYPE _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>::value_type *#  endifvalue_type(const _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>&) {  typedef _STLP_TYPENAME _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>::value_type _Val;  return (_Val*) 0;}template <class _BaseIte, class _Traits>inline forward_iterator_tag iterator_category(const _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>&){ return forward_iterator_tag(); }template <class _BaseIte, class _Traits>inline ptrdiff_t* distance_type(const _STLP_PRIV _Ht_iterator<_BaseIte,_Traits>&){ return (ptrdiff_t*) 0; }#endif_STLP_MOVE_TO_PRIV_NAMESPACEtemplate <class _Dummy>class _Stl_prime {  // Returns begining of primes list and size by reference.  static const size_t* _S_primes(size_t&);public:  //Returns the maximum number of buckets handled by the hashtable implementation  static size_t _STLP_CALL _S_max_nb_buckets();  //Returns the bucket size next to a required size  static size_t _STLP_CALL _S_next_size(size_t);  // Returns the bucket range containing sorted list of prime numbers <= __hint.  static void _STLP_CALL _S_prev_sizes(size_t __hint, const size_t *&__begin, const size_t *&__end);};#if defined (_STLP_USE_TEMPLATE_EXPORT)_STLP_EXPORT_TEMPLATE_CLASS _Stl_prime<bool>;#endiftypedef _Stl_prime<bool> _Stl_prime_type;#if !defined (_STLP_DEBUG)_STLP_MOVE_TO_STD_NAMESPACE#endif/* * Hashtables handle allocators a bit differently than other containers * do. If we're using standard-conforming allocators, then a hashtable * unconditionally has a member variable to hold its allocator, even if * it so happens that all instances of the allocator type are identical. * This is because, for hashtables, this extra storage is negligible. * Additionally, a base class wouldn't serve any other purposes; it * wouldn't, for example, simplify the exception-handling code. */template <class _Val, class _Key, class _HF,          class _Traits, class _ExK, class _EqK, class _All>class hashtable {  typedef hashtable<_Val, _Key, _HF, _Traits, _ExK, _EqK, _All> _Self;  typedef typename _Traits::_NonConstTraits _NonConstTraits;  typedef typename _Traits::_ConstTraits _ConstTraits;  typedef typename _Traits::_NonConstLocalTraits _NonConstLocalTraits;  typedef typename _Traits::_ConstLocalTraits _ConstLocalTraits;public:  typedef _Key key_type;  typedef _Val value_type;  typedef _HF hasher;  typedef _EqK key_equal;  typedef size_t            size_type;  typedef ptrdiff_t         difference_type;  typedef typename _NonConstTraits::pointer pointer;  typedef const value_type* const_pointer;  typedef typename _NonConstTraits::reference reference;  typedef const value_type& const_reference;  typedef forward_iterator_tag _Iterator_category;  hasher hash_funct() const { return _M_hash; }  key_equal key_eq() const { return _M_equals; }private:  _STLP_FORCE_ALLOCATORS(_Val, _All)#if defined (_STLP_DEBUG)  typedef _STLP_PRIV _STLP_NON_DBG_NAME(slist)<value_type, _All> _ElemsCont;#else  typedef slist<value_type, _All> _ElemsCont;#endif  typedef typename _ElemsCont::iterator _ElemsIte;  typedef typename _ElemsCont::const_iterator _ElemsConstIte;  typedef _STLP_PRIV _Slist_node_base _BucketType;  typedef typename _Alloc_traits<_BucketType*, _All>::allocator_type _BucketAllocType;  /*   * We are going to use vector of _Slist_node_base pointers for 2 reasons:   *  - limit code bloat, all hashtable instanciation use the same buckets representation.   *  - avoid _STLP_DEBUG performance trouble: with a vector of iterator on slist the resize   *    method would be too slow because the slist::splice_after method become linear on   *    the number of iterators in the buckets rather than constant in time as the iterator   *    has to be move from a slist to the other.   */#if defined (_STLP_DEBUG)  typedef _STLP_PRIV _STLP_NON_DBG_NAME(vector)<_BucketType*, _BucketAllocType> _BucketVector;#else  typedef vector<_BucketType*, _BucketAllocType> _BucketVector;#endif  hasher                _M_hash;  key_equal             _M_equals;  _ElemsCont            _M_elems;  _BucketVector         _M_buckets;  size_type             _M_num_elements;  float                 _M_max_load_factor;  _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)  static const key_type& _M_get_key(const value_type& __val) {    _ExK k;    return k(__val);  }public:  typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _NonConstTraits> iterator;  typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _ConstTraits> const_iterator;  //TODO: Avoids this debug check and make the local_iterator different from  //iterator in debug mode too.#if !defined (_STLP_DEBUG)  typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _NonConstLocalTraits> local_iterator;  typedef _STLP_PRIV _Ht_iterator<_ElemsIte, _ConstLocalTraits> const_local_iterator;#else  typedef iterator       local_iterator;  typedef const_iterator const_local_iterator;#endif  typedef _All allocator_type;  allocator_type get_allocator() const { return _M_elems.get_allocator(); }#if !defined (_STLP_DONT_SUP_DFLT_PARAM)  hashtable(size_type __n,            const _HF&    __hf,            const _EqK&   __eql,            const allocator_type& __a = allocator_type())#else  hashtable(size_type __n,            const _HF&    __hf,            const _EqK&   __eql)    : _M_hash(__hf),      _M_equals(__eql),      _M_elems(allocator_type()),      _M_buckets(_STLP_CONVERT_ALLOCATOR(__a, _BucketType*)),      _M_num_elements(0),      _M_max_load_factor(1.0f)  { _M_initialize_buckets(__n); }  hashtable(size_type __n,            const _HF&    __hf,            const _EqK&   __eql,            const allocator_type& __a)#endif    : _M_hash(__hf),      _M_equals(__eql),      _M_elems(__a),      _M_buckets(_STLP_CONVERT_ALLOCATOR(__a, _BucketType*)),      _M_num_elements(0),      _M_max_load_factor(1.0f)  { _M_initialize_buckets(__n); }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?