⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _hashtable.h

📁 MONA是为数不多的C++语言编写的一个很小的操作系统
💻 H
📖 第 1 页 / 共 2 页
字号:
/* * * 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_ITERATOR_H#  include <stl/_iterator.h># endif# ifndef _STLP_INTERNAL_FUNCTION_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, and hash_multimap.#ifdef _STLP_DEBUG#  define hashtable __WORKAROUND_DBG_RENAME(hashtable)#endif_STLP_BEGIN_NAMESPACEtemplate <class _Val>struct _Hashtable_node{  typedef _Hashtable_node<_Val> _Self;  _Self* _M_next;  _Val _M_val;  __TRIVIAL_STUFF(_Hashtable_node)};  // some compilers require the names of template parameters to be the sametemplate <class _Val, class _Key, class _HF,          class _ExK, class _EqK, class _All>class hashtable;template <class _Val, class _Key, class _HF,          class _ExK, class _EqK, class _All>struct _Hashtable_iterator{  typedef hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>          _Hashtable;  typedef _Hashtable_node<_Val> _Node;  _Node* _M_cur;  _Hashtable* _M_ht;  _Hashtable_iterator(_Node* __n, _Hashtable* __tab)     : _M_cur(__n), _M_ht(__tab) {}  _Hashtable_iterator() {}  _Node* _M_skip_to_next();};template <class _Val, class _Traits, class _Key, class _HF,          class _ExK, class _EqK, class _All>struct _Ht_iterator : public _Hashtable_iterator< _Val, _Key,_HF, _ExK,_EqK,_All>{    typedef _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All> _Base;  //  typedef _Ht_iterator<_Val, _Nonconst_traits<_Val>,_Key,_HF,_ExK,_EqK,_All> iterator;  //  typedef _Ht_iterator<_Val, _Const_traits<_Val>,_Key,_HF,_ExK,_EqK,_All> const_iterator;  typedef _Ht_iterator<_Val, _Traits,_Key,_HF,_ExK,_EqK,_All> _Self;  typedef hashtable<_Val,_Key,_HF,_ExK,_EqK,_All> _Hashtable;  typedef _Hashtable_node<_Val> _Node;  typedef _Val value_type;  typedef forward_iterator_tag iterator_category;  typedef ptrdiff_t difference_type;  typedef size_t size_type;  typedef typename _Traits::reference reference;  typedef typename _Traits::pointer   pointer;  _Ht_iterator(const _Node* __n, const _Hashtable* __tab) :    _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>((_Node*)__n, (_Hashtable*)__tab) {}  _Ht_iterator() {}  _Ht_iterator(const _Ht_iterator<_Val, _Nonconst_traits<_Val>,_Key,_HF,_ExK,_EqK,_All>& __it) :     _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>(__it) {}  reference operator*() const {       return this->_M_cur->_M_val;   }  _STLP_DEFINE_ARROW_OPERATOR  _Self& operator++() {    _Node* __n = this->_M_cur->_M_next;    this->_M_cur =  (__n !=0 ? __n : this->_M_skip_to_next());    return *this;  }  inline  _Self operator++(int) {     _Self __tmp = *this;    ++*this;    return __tmp;  }};template <class _Val, class _Traits, class _Traits1, class _Key, class _HF,          class _ExK, class _EqK, class _All>inline bool operator==(const _Ht_iterator<_Val, _Traits,_Key,_HF,_ExK,_EqK,_All>& __x, 	   const _Ht_iterator<_Val, _Traits1,_Key,_HF,_ExK,_EqK,_All>& __y) {   return __x._M_cur == __y._M_cur; }#ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACEtemplate <class _Val, class _Key, class _HF,          class _ExK, class _EqK, class _All>inline bool operator!=(const _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>& __x, 	   const _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>& __y) {   return __x._M_cur != __y._M_cur; }#else# if (defined (__GNUC__) && (__GNUC_MINOR__ < 8))template <class _Val, class _Key, class _HF,          class _ExK, class _EqK, class _All>inline booloperator!=(const _Ht_iterator<_Val, _Const_traits<_Val>,_Key,_HF,_ExK,_EqK,_All>& __x,           const _Ht_iterator<_Val, _Nonconst_traits<_Val>,_Key,_HF,_ExK,_EqK,_All>& __y) {  return __x._M_cur != __y._M_cur;}# endiftemplate <class _Val, class _Key, class _HF,          class _ExK, class _EqK, class _All>inline bool operator!=(const _Ht_iterator<_Val, _Nonconst_traits<_Val>,_Key,_HF,_ExK,_EqK,_All>& __x, 	   const _Ht_iterator<_Val, _Const_traits<_Val>,_Key,_HF,_ExK,_EqK,_All>& __y) {   return __x._M_cur != __y._M_cur; }#endif# ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIEStemplate <class _Val, class _Traits, class _Key, class _HF, class _ExK, class _EqK, class _All>inline _Val* value_type(const _Ht_iterator<_Val, _Traits,_Key,_HF,_ExK,_EqK,_All>&) { return (_Val*) 0; }template <class _Val, class _Traits, class _Key, class _HF, class _ExK, class _EqK, class _All>inline forward_iterator_tag iterator_category(const _Ht_iterator<_Val, _Traits,_Key,_HF,_ExK,_EqK,_All>&) { return forward_iterator_tag(); }template <class _Val, class _Traits, class _Key, class _HF, class _ExK, class _EqK, class _All>inline ptrdiff_t* distance_type(const _Ht_iterator<_Val,_Traits,_Key,_HF,_ExK,_EqK,_All>&) { return (ptrdiff_t*) 0; }#endif#define __stl_num_primes  28template <class _Tp>class _Stl_prime {public:  static const size_t _M_list[__stl_num_primes];};# if defined (_STLP_USE_TEMPLATE_EXPORT) _STLP_EXPORT_TEMPLATE_CLASS _Stl_prime<bool>;# endiftypedef _Stl_prime<bool> _Stl_prime_type;// 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 _ExK, class _EqK, class _All>class hashtable {  typedef hashtable<_Val, _Key, _HF, _ExK, _EqK, _All> _Self;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 value_type*       pointer;  typedef const value_type* const_pointer;  typedef value_type&       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:  typedef _Hashtable_node<_Val> _Node;private:  _STLP_FORCE_ALLOCATORS(_Val, _All)  typedef typename _Alloc_traits<_Node, _All>::allocator_type _M_node_allocator_type;  typedef typename _Alloc_traits<void*, _All>::allocator_type _M_node_ptr_allocator_type;  typedef __vector__<void*, _M_node_ptr_allocator_type> _BucketVector;public:  typedef typename _Alloc_traits<_Val,_All>::allocator_type allocator_type;  allocator_type get_allocator() const {     return _STLP_CONVERT_ALLOCATOR((const _M_node_allocator_type&)_M_num_elements, _Val);   }private:  hasher                _M_hash;  key_equal             _M_equals;  _ExK                  _M_get_key;  _BucketVector         _M_buckets;  _STLP_alloc_proxy<size_type, _Node, _M_node_allocator_type>  _M_num_elements;  const _Node* _M_get_bucket(size_t __n) const { return (_Node*)_M_buckets[__n]; }public:  typedef _Const_traits<_Val> __const_val_traits;  typedef _Nonconst_traits<_Val> __nonconst_val_traits;  typedef _Ht_iterator<_Val, __const_val_traits,_Key,_HF,_ExK,_EqK, _All> const_iterator;  typedef _Ht_iterator<_Val, __nonconst_val_traits,_Key,_HF,_ExK,_EqK,_All> iterator;  friend struct _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>;  friend struct _Ht_iterator<_Val, _Nonconst_traits<_Val>,_Key,_HF,_ExK,_EqK,_All>;  friend struct _Ht_iterator<_Val, _Const_traits<_Val>,_Key,_HF,_ExK,_EqK, _All>;public:  hashtable(size_type __n,            const _HF&  __hf,            const _EqK& __eql,            const _ExK& __ext,            const allocator_type& __a = allocator_type())    :      _M_hash(__hf),      _M_equals(__eql),      _M_get_key(__ext),      _M_buckets(_STLP_CONVERT_ALLOCATOR(__a,void*)),      _M_num_elements(_STLP_CONVERT_ALLOCATOR(__a,_Node), (size_type)0)  {    _M_initialize_buckets(__n);  }  hashtable(size_type __n,            const _HF&    __hf,            const _EqK&   __eql,            const allocator_type& __a = allocator_type())    :      _M_hash(__hf),      _M_equals(__eql),      _M_get_key(_ExK()),      _M_buckets(_STLP_CONVERT_ALLOCATOR(__a,void*)),      _M_num_elements(_STLP_CONVERT_ALLOCATOR(__a,_Node), (size_type)0)  {    _M_initialize_buckets(__n);  }  hashtable(const _Self& __ht)    :      _M_hash(__ht._M_hash),      _M_equals(__ht._M_equals),      _M_get_key(__ht._M_get_key),      _M_buckets(_STLP_CONVERT_ALLOCATOR(__ht.get_allocator(),void*)),      _M_num_elements((const _M_node_allocator_type&)__ht._M_num_elements, (size_type)0)  {    _M_copy_from(__ht);  }  _Self& operator= (const _Self& __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;

⌨️ 快捷键说明

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