总结

  1. hashtable使用的拉链法解决冲突
  2. 可以实现 O(1) 级别的查询/插入/删除
  3. hashfunc 只支持内置类型(string不是内置类型),其他类型需要自定义hash函数
  4. 自定义哈希函数的要求:
    1. 使用struct,然后重载operator()
    2. 返回size_t类型
    3. operator()函数的参数是要哈希的参数类型
    4. 函数必须要const类型
      1. struct str_hash{
      2. size_t operator()(const string& str) const
      3. {
      4. unsigned long __h = 0;
      5. for (size_t i = 0 ; i < str.size() ; i ++)
      6. __h = 5*__h + str[i];
      7. return size_t(__h);
      8. }};

hashtable结构

  1. #ifndef __SGI_STL_INTERNAL_HASHTABLE_H
  2. #define __SGI_STL_INTERNAL_HASHTABLE_H
  3. // Hashtable class, used to implement the hashed associative containers// hash_set, hash_map, hash_multiset, and hash_multimap.
  4. //SGI STL的hashtable的实现方法是拉链法.//拉链法可以避免hashtable的冲突问题,即不同数据映射到同一的hash值
  5. //有关hashtable的介绍见前文://http://blog.csdn.net/chenhanzhun/article/details/38091431
  6. #include <stl_algobase.h>
  7. #include <stl_alloc.h>
  8. #include <stl_construct.h>
  9. #include <stl_tempbuf.h>
  10. #include <stl_algo.h>
  11. #include <stl_uninitialized.h>
  12. #include <stl_function.h>
  13. #include <stl_vector.h>
  14. #include <stl_hash_fun.h>
  15. __STL_BEGIN_NAMESPACE
  16. //hashtable中链表的节点结构//类似于单链表的节点结构
  17. template <class _Val>
  18. struct _Hashtable_node
  19. {
  20. _Hashtable_node* _M_next;//指向下一节点
  21. _Val _M_val;//节点元素值};
  22. //这里使用前置声明, 避免在后面交叉引用会导致编译错误
  23. template <class _Val, class _Key, class _HashFcn,
  24. class _ExtractKey, class _EqualKey, class _Alloc = alloc>
  25. class hashtable;
  26. template <class _Val, class _Key, class _HashFcn,
  27. class _ExtractKey, class _EqualKey, class _Alloc>
  28. struct _Hashtable_iterator;
  29. template <class _Val, class _Key, class _HashFcn,
  30. class _ExtractKey, class _EqualKey, class _Alloc>
  31. struct _Hashtable_const_iterator;
  32. //hashtable迭代器定义//注意:hash table迭代器没有提供后退操作operator--//也没用提供逆向迭代器reverse iterator
  33. template <class _Val, class _Key, class _HashFcn,
  34. class _ExtractKey, class _EqualKey, class _Alloc>
  35. struct _Hashtable_iterator {
  36. //内嵌类型别名
  37. typedef hashtable<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>
  38. _Hashtable;
  39. typedef _Hashtable_iterator<_Val, _Key, _HashFcn,
  40. _ExtractKey, _EqualKey, _Alloc>
  41. iterator;
  42. typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn,
  43. _ExtractKey, _EqualKey, _Alloc>
  44. const_iterator;
  45. typedef _Hashtable_node<_Val> _Node;
  46. typedef forward_iterator_tag iterator_category;//采用正向迭代器
  47. typedef _Val value_type;
  48. typedef ptrdiff_t difference_type;
  49. typedef size_t size_type;
  50. typedef _Val& reference;
  51. typedef _Val* pointer;
  52. _Node* _M_cur;//当前迭代器所指位置
  53. _Hashtable* _M_ht;//hashtable中的位置,控制访问桶子连续性
  54. _Hashtable_iterator(_Node* __n, _Hashtable* __tab)
  55. : _M_cur(__n), _M_ht(__tab) {}
  56. _Hashtable_iterator() {}
  57. //返回当前节点元素值的引用
  58. reference operator*() const { return _M_cur->_M_val; }
  59. #ifndef __SGI_STL_NO_ARROW_OPERATOR
  60. pointer operator->() const { return &(operator*()); }
  61. #endif /* __SGI_STL_NO_ARROW_OPERATOR */
  62. //操作符重载定义在后面定义
  63. iterator& operator++();
  64. iterator operator++(int);
  65. //比较两个迭代器是否指向同一个节点
  66. bool operator==(const iterator& __it) const
  67. { return _M_cur == __it._M_cur; }
  68. bool operator!=(const iterator& __it) const
  69. { return _M_cur != __it._M_cur; }};
  70. //下面是const iterator的定义,基本和上面相同
  71. template <class _Val, class _Key, class _HashFcn,
  72. class _ExtractKey, class _EqualKey, class _Alloc>
  73. struct _Hashtable_const_iterator {
  74. typedef hashtable<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>
  75. _Hashtable;
  76. typedef _Hashtable_iterator<_Val,_Key,_HashFcn,
  77. _ExtractKey,_EqualKey,_Alloc>
  78. iterator;
  79. typedef _Hashtable_const_iterator<_Val, _Key, _HashFcn,
  80. _ExtractKey, _EqualKey, _Alloc>
  81. const_iterator;
  82. typedef _Hashtable_node<_Val> _Node;
  83. typedef forward_iterator_tag iterator_category;
  84. typedef _Val value_type;
  85. typedef ptrdiff_t difference_type;
  86. typedef size_t size_type;
  87. typedef const _Val& reference;
  88. typedef const _Val* pointer;
  89. const _Node* _M_cur;
  90. const _Hashtable* _M_ht;
  91. _Hashtable_const_iterator(const _Node* __n, const _Hashtable* __tab)
  92. : _M_cur(__n), _M_ht(__tab) {}
  93. _Hashtable_const_iterator() {}
  94. _Hashtable_const_iterator(const iterator& __it)
  95. : _M_cur(__it._M_cur), _M_ht(__it._M_ht) {}
  96. reference operator*() const { return _M_cur->_M_val; }
  97. #ifndef __SGI_STL_NO_ARROW_OPERATOR
  98. pointer operator->() const { return &(operator*()); }
  99. #endif /* __SGI_STL_NO_ARROW_OPERATOR */
  100. const_iterator& operator++();
  101. const_iterator operator++(int);
  102. bool operator==(const const_iterator& __it) const
  103. { return _M_cur == __it._M_cur; }
  104. bool operator!=(const const_iterator& __it) const
  105. { return _M_cur != __it._M_cur; }};
  106. // Note: assumes long is at least 32 bits.// 注意:假设long至少为32-bits, 可以根据自己需要修改//定义28个素数用作hashtable的大小
  107. enum { __stl_num_primes = 28 };
  108. static const unsigned long __stl_prime_list[__stl_num_primes] ={
  109. 53ul, 97ul, 193ul, 389ul, 769ul,
  110. 1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
  111. 49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
  112. 1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
  113. 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
  114. 1610612741ul, 3221225473ul, 4294967291ul
  115. };
  116. //返回大于n的最小素数
  117. inline unsigned long __stl_next_prime(unsigned long __n){
  118. const unsigned long* __first = __stl_prime_list;
  119. const unsigned long* __last = __stl_prime_list + (int)__stl_num_primes;
  120. const unsigned long* pos = lower_bound(__first, __last, __n);
  121. /*
  122. 上面的lower_bound调用的是STL中的算法lower_bound();
  123. 该算法的功能:
  124. Returns an iterator pointing to the first element in the range [first,last) which does not compare less than val.
  125. The elements in the range shall already be sorted according to this same criterion (operator< or comp)
  126. 即返回在[first,last)范围内第一个不小于val的位置
  127. 注意:调用该算法之前,[first,last)范围里面的元素必须已排序
  128. 该算法的原型如下:
  129. 第一个版本:采用默认比较准则operator<
  130. template <class ForwardIterator, class T>
  131. ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
  132. const T& val);
  133. 第二版本:采用用户指定的比较函数comp
  134. template <class ForwardIterator, class T, class Compare>
  135. ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,
  136. const T& val, Compare comp);
  137. 其实该算法的实现机制使用二分查找法进行查找元素val:
  138. template <class ForwardIterator, class T>
  139. ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
  140. {
  141. ForwardIterator it;
  142. iterator_traits<ForwardIterator>::difference_type count, step;
  143. count = distance(first,last);
  144. while (count>0)
  145. {
  146. it = first; step=count/2; advance (it,step);
  147. if (*it<val) { // or: if (comp(*it,val)), for version (2)
  148. first=++it;
  149. count-=step+1;
  150. }
  151. else count=step;
  152. }
  153. return first;
  154. }
  155. 下面给出例子:lower_bound/upper_bound example
  156. #include <iostream> // std::cout
  157. #include <algorithm> // std::lower_bound, std::upper_bound, std::sort
  158. #include <vector> // std::vector
  159. int main () {
  160. int myints[] = {10,20,30,30,20,10,10,20};
  161. std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
  162. std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
  163. std::vector<int>::iterator low,up;
  164. low=std::lower_bound (v.begin(), v.end(), 20); // ^
  165. up= std::upper_bound (v.begin(), v.end(), 20); // ^
  166. std::cout << "lower_bound at position " << (low- v.begin()) << '\n';
  167. std::cout << "upper_bound at position " << (up - v.begin()) << '\n';
  168. return 0;
  169. }
  170. Output:
  171. lower_bound at position 3
  172. upper_bound at position 6
  173. */
  174. return pos == __last ? *(__last - 1) : *pos;}
  175. // Forward declaration of operator==.
  176. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  177. class hashtable;
  178. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  179. bool operator==(const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht1,
  180. const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht2);
  181. // 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.
  182. //hash table的定义//模板参数定义/*
  183. Value: 节点的实值类型
  184. Key: 节点的键值类型
  185. HashFcn: hash function的类型
  186. ExtractKey:从节点中取出键值的方法(函数或仿函数)
  187. EqualKey:判断键值是否相同的方法(函数或仿函数)
  188. Alloc:空间配置器
  189. */
  190. //hash table的线性表是用vector容器维护
  191. template <class _Val, class _Key, class _HashFcn,
  192. class _ExtractKey, class _EqualKey, class _Alloc>
  193. class hashtable {
  194. public:
  195. typedef _Key key_type;
  196. typedef _Val value_type;
  197. typedef _HashFcn hasher;
  198. typedef _EqualKey key_equal;
  199. typedef size_t size_type;
  200. typedef ptrdiff_t difference_type;
  201. typedef value_type* pointer;
  202. typedef const value_type* const_pointer;
  203. typedef value_type& reference;
  204. typedef const value_type& const_reference;
  205. hasher hash_funct() const { return _M_hash; }
  206. key_equal key_eq() const { return _M_equals; }
  207. private:
  208. typedef _Hashtable_node<_Val> _Node;
  209. #ifdef __STL_USE_STD_ALLOCATORS
  210. public:
  211. typedef typename _Alloc_traits<_Val,_Alloc>::allocator_type allocator_type;
  212. allocator_type get_allocator() const { return _M_node_allocator; }
  213. private:
  214. typename _Alloc_traits<_Node, _Alloc>::allocator_type _M_node_allocator;
  215. _Node* _M_get_node() { return _M_node_allocator.allocate(1); }
  216. void _M_put_node(_Node* __p) { _M_node_allocator.deallocate(__p, 1); }
  217. # define __HASH_ALLOC_INIT(__a) _M_node_allocator(__a),
  218. #else /* __STL_USE_STD_ALLOCATORS */
  219. public:
  220. typedef _Alloc allocator_type;
  221. allocator_type get_allocator() const { return allocator_type(); }
  222. private:
  223. typedef simple_alloc<_Node, _Alloc> _M_node_allocator_type;
  224. _Node* _M_get_node() { return _M_node_allocator_type::allocate(1); }
  225. void _M_put_node(_Node* __p) { _M_node_allocator_type::deallocate(__p, 1); }
  226. # define __HASH_ALLOC_INIT(__a)
  227. #endif /* __STL_USE_STD_ALLOCATORS */
  228. //以下是hash table的成员变量
  229. private:
  230. hasher _M_hash;
  231. key_equal _M_equals;
  232. _ExtractKey _M_get_key;
  233. vector<_Node*,_Alloc> _M_buckets;//用vector维护buckets
  234. size_type _M_num_elements;//hashtable中list节点个数
  235. public:
  236. typedef _Hashtable_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>
  237. iterator;
  238. typedef _Hashtable_const_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,
  239. _Alloc>
  240. const_iterator;
  241. friend struct
  242. _Hashtable_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>;
  243. friend struct
  244. _Hashtable_const_iterator<_Val,_Key,_HashFcn,_ExtractKey,_EqualKey,_Alloc>;
  245. public:
  246. //构造函数
  247. hashtable(size_type __n,
  248. const _HashFcn& __hf,
  249. const _EqualKey& __eql,
  250. const _ExtractKey& __ext,
  251. const allocator_type& __a = allocator_type())
  252. : __HASH_ALLOC_INIT(__a)
  253. _M_hash(__hf),
  254. _M_equals(__eql),
  255. _M_get_key(__ext),
  256. _M_buckets(__a),
  257. _M_num_elements(0)
  258. {
  259. _M_initialize_buckets(__n);//预留空间,并将其初始化为空0
  260. //预留空间大小为大于n的最小素数
  261. }
  262. hashtable(size_type __n,
  263. const _HashFcn& __hf,
  264. const _EqualKey& __eql,
  265. const allocator_type& __a = allocator_type())
  266. : __HASH_ALLOC_INIT(__a)
  267. _M_hash(__hf),
  268. _M_equals(__eql),
  269. _M_get_key(_ExtractKey()),
  270. _M_buckets(__a),
  271. _M_num_elements(0)
  272. {
  273. _M_initialize_buckets(__n);
  274. }
  275. //拷贝构造函数
  276. hashtable(const hashtable& __ht)
  277. : __HASH_ALLOC_INIT(__ht.get_allocator())
  278. _M_hash(__ht._M_hash),
  279. _M_equals(__ht._M_equals),
  280. _M_get_key(__ht._M_get_key),
  281. _M_buckets(__ht.get_allocator()),
  282. _M_num_elements(0)
  283. {
  284. _M_copy_from(__ht);//复制hashtable内容
  285. }
  286. #undef __HASH_ALLOC_INIT
  287. //可以通过operator=初始化hashtable对象
  288. hashtable& operator= (const hashtable& __ht)
  289. {
  290. if (&__ht != this) {
  291. clear();
  292. _M_hash = __ht._M_hash;
  293. _M_equals = __ht._M_equals;
  294. _M_get_key = __ht._M_get_key;
  295. _M_copy_from(__ht);
  296. }
  297. return *this;
  298. }
  299. ~hashtable() { clear(); }
  300. //返回hashtable元素的个数
  301. size_type size() const { return _M_num_elements; }
  302. size_type max_size() const { return size_type(-1); }
  303. bool empty() const { return size() == 0; }
  304. //交换两个hashtable的内容
  305. void swap(hashtable& __ht)
  306. {
  307. __STD::swap(_M_hash, __ht._M_hash);
  308. __STD::swap(_M_equals, __ht._M_equals);
  309. __STD::swap(_M_get_key, __ht._M_get_key);
  310. _M_buckets.swap(__ht._M_buckets);
  311. __STD::swap(_M_num_elements, __ht._M_num_elements);
  312. }
  313. iterator begin()
  314. {
  315. for (size_type __n = 0; __n < _M_buckets.size(); ++__n)
  316. if (_M_buckets[__n])//若hashtable中的桶子_M_buckets有链表list
  317. return iterator(_M_buckets[__n], this);//返回链表的第一个节点位置
  318. return end();//若list链表为空,则返回尾端end(),其实这里尾端和起始端一样
  319. }
  320. iterator end() { return iterator(0, this); }//返回桶子list链表的null指针,即尾端
  321. const_iterator begin() const
  322. {
  323. for (size_type __n = 0; __n < _M_buckets.size(); ++__n)
  324. if (_M_buckets[__n])
  325. return const_iterator(_M_buckets[__n], this);
  326. return end();
  327. }
  328. const_iterator end() const { return const_iterator(0, this); }
  329. #ifdef __STL_MEMBER_TEMPLATES
  330. template <class _Vl, class _Ky, class _HF, class _Ex, class _Eq, class _Al>
  331. friend bool operator== (const hashtable<_Vl, _Ky, _HF, _Ex, _Eq, _Al>&,
  332. const hashtable<_Vl, _Ky, _HF, _Ex, _Eq, _Al>&);
  333. #else /* __STL_MEMBER_TEMPLATES */
  334. friend bool __STD_QUALIFIER
  335. operator== __STL_NULL_TMPL_ARGS (const hashtable&, const hashtable&);
  336. #endif /* __STL_MEMBER_TEMPLATES */
  337. public:
  338. //返回桶子个数,即线性表中节点数
  339. size_type bucket_count() const { return _M_buckets.size(); }
  340. //线性表最多分配节点数
  341. size_type max_bucket_count() const
  342. { return __stl_prime_list[(int)__stl_num_primes - 1]; }
  343. //指定桶子键值key中list链表的元素个数
  344. size_type elems_in_bucket(size_type __bucket) const
  345. {
  346. size_type __result = 0;
  347. for (_Node* __cur = _M_buckets[__bucket]; __cur; __cur = __cur->_M_next)
  348. __result += 1;
  349. return __result;
  350. }
  351. //插入元素节点,不允许存在重复元素
  352. pair<iterator, bool> insert_unique(const value_type& __obj)
  353. {
  354. //判断容量是否够用, 否则就重新配置
  355. resize(_M_num_elements + 1);
  356. //插入元素,不允许存在重复元素
  357. return insert_unique_noresize(__obj);
  358. }
  359. //插入元素节点,允许存在重复元素
  360. iterator insert_equal(const value_type& __obj)
  361. {//判断容量是否够用, 否则就重新配置
  362. resize(_M_num_elements + 1);
  363. //插入元素,允许存在重复元素
  364. return insert_equal_noresize(__obj);
  365. }
  366. //具体定义见hashtable类外后面的剖析
  367. pair<iterator, bool> insert_unique_noresize(const value_type& __obj);
  368. iterator insert_equal_noresize(const value_type& __obj);
  369. #ifdef __STL_MEMBER_TEMPLATES
  370. template <class _InputIterator>
  371. void insert_unique(_InputIterator __f, _InputIterator __l)
  372. {
  373. insert_unique(__f, __l, __ITERATOR_CATEGORY(__f));
  374. }
  375. template <class _InputIterator>
  376. void insert_equal(_InputIterator __f, _InputIterator __l)
  377. {
  378. insert_equal(__f, __l, __ITERATOR_CATEGORY(__f));
  379. }
  380. template <class _InputIterator>
  381. void insert_unique(_InputIterator __f, _InputIterator __l,
  382. input_iterator_tag)
  383. {
  384. for ( ; __f != __l; ++__f)
  385. insert_unique(*__f);
  386. }
  387. template <class _InputIterator>
  388. void insert_equal(_InputIterator __f, _InputIterator __l,
  389. input_iterator_tag)
  390. {
  391. for ( ; __f != __l; ++__f)
  392. insert_equal(*__f);
  393. }
  394. template <class _ForwardIterator>
  395. void insert_unique(_ForwardIterator __f, _ForwardIterator __l,
  396. forward_iterator_tag)
  397. {
  398. size_type __n = 0;
  399. distance(__f, __l, __n);
  400. resize(_M_num_elements + __n);
  401. for ( ; __n > 0; --__n, ++__f)
  402. insert_unique_noresize(*__f);
  403. }
  404. template <class _ForwardIterator>
  405. void insert_equal(_ForwardIterator __f, _ForwardIterator __l,
  406. forward_iterator_tag)
  407. {
  408. size_type __n = 0;
  409. distance(__f, __l, __n);
  410. resize(_M_num_elements + __n);
  411. for ( ; __n > 0; --__n, ++__f)
  412. insert_equal_noresize(*__f);
  413. }
  414. #else /* __STL_MEMBER_TEMPLATES */
  415. void insert_unique(const value_type* __f, const value_type* __l)
  416. {
  417. size_type __n = __l - __f;
  418. resize(_M_num_elements + __n);
  419. for ( ; __n > 0; --__n, ++__f)
  420. insert_unique_noresize(*__f);
  421. }
  422. void insert_equal(const value_type* __f, const value_type* __l)
  423. {
  424. size_type __n = __l - __f;
  425. resize(_M_num_elements + __n);
  426. for ( ; __n > 0; --__n, ++__f)
  427. insert_equal_noresize(*__f);
  428. }
  429. void insert_unique(const_iterator __f, const_iterator __l)
  430. {
  431. size_type __n = 0;
  432. distance(__f, __l, __n);
  433. resize(_M_num_elements + __n);
  434. for ( ; __n > 0; --__n, ++__f)
  435. insert_unique_noresize(*__f);
  436. }
  437. void insert_equal(const_iterator __f, const_iterator __l)
  438. {
  439. size_type __n = 0;
  440. distance(__f, __l, __n);
  441. resize(_M_num_elements + __n);
  442. for ( ; __n > 0; --__n, ++__f)
  443. insert_equal_noresize(*__f);
  444. }
  445. #endif /*__STL_MEMBER_TEMPLATES */
  446. reference find_or_insert(const value_type& __obj);
  447. //查找指定键值的元素
  448. iterator find(const key_type& __key)
  449. {
  450. size_type __n = _M_bkt_num_key(__key);//获取键值
  451. _Node* __first;
  452. for ( __first = _M_buckets[__n];
  453. __first && !_M_equals(_M_get_key(__first->_M_val), __key);
  454. __first = __first->_M_next)
  455. {}
  456. return iterator(__first, this);
  457. }
  458. const_iterator find(const key_type& __key) const
  459. {
  460. size_type __n = _M_bkt_num_key(__key);
  461. const _Node* __first;
  462. for ( __first = _M_buckets[__n];
  463. __first && !_M_equals(_M_get_key(__first->_M_val), __key);
  464. __first = __first->_M_next)
  465. {}
  466. return const_iterator(__first, this);
  467. }
  468. //返回键值为key的元素的个数
  469. size_type count(const key_type& __key) const
  470. {
  471. const size_type __n = _M_bkt_num_key(__key);
  472. size_type __result = 0;
  473. for (const _Node* __cur = _M_buckets[__n]; __cur; __cur = __cur->_M_next)
  474. if (_M_equals(_M_get_key(__cur->_M_val), __key))
  475. ++__result;
  476. return __result;
  477. }
  478. pair<iterator, iterator>
  479. equal_range(const key_type& __key);
  480. pair<const_iterator, const_iterator>
  481. equal_range(const key_type& __key) const;
  482. //擦除元素
  483. size_type erase(const key_type& __key);
  484. void erase(const iterator& __it);
  485. void erase(iterator __first, iterator __last);
  486. void erase(const const_iterator& __it);
  487. void erase(const_iterator __first, const_iterator __last);
  488. void resize(size_type __num_elements_hint);
  489. void clear();
  490. private:
  491. //返回大于n的最小素数
  492. //实际上调用__stl_next_prime(__n);
  493. size_type _M_next_size(size_type __n) const
  494. { return __stl_next_prime(__n); }
  495. //预留空间,并将其初始化为0
  496. void _M_initialize_buckets(size_type __n)
  497. {
  498. //返回大于n的最小素数__n_buckets
  499. const size_type __n_buckets = _M_next_size(__n);
  500. //这里调用vector的成员函数reserve
  501. //reserve该函数功能是改变可用空间的大小
  502. //Requests that the vector capacity be at least enough to contain __n_buckets elements.
  503. _M_buckets.reserve(__n_buckets);
  504. //调用vector的插入函数insert
  505. //在原始end后面连续插入__n_buckets个0
  506. _M_buckets.insert(_M_buckets.end(), __n_buckets, (_Node*) 0);
  507. _M_num_elements = 0;
  508. }
  509. //获取键值key在桶子的位置
  510. size_type _M_bkt_num_key(const key_type& __key) const
  511. {
  512. return _M_bkt_num_key(__key, _M_buckets.size());
  513. }
  514. //获取在桶子的序号,也就是键值
  515. //输入参数是实值value
  516. size_type _M_bkt_num(const value_type& __obj) const
  517. {
  518. return _M_bkt_num_key(_M_get_key(__obj));
  519. }
  520. size_type _M_bkt_num_key(const key_type& __key, size_t __n) const
  521. {
  522. return _M_hash(__key) % __n;//采用除法取余hash函数
  523. }
  524. size_type _M_bkt_num(const value_type& __obj, size_t __n) const
  525. {
  526. return _M_bkt_num_key(_M_get_key(__obj), __n);
  527. }
  528. //分配节点空间,并构造对象
  529. _Node* _M_new_node(const value_type& __obj)
  530. {
  531. _Node* __n = _M_get_node();
  532. __n->_M_next = 0;
  533. __STL_TRY {
  534. construct(&__n->_M_val, __obj);
  535. return __n;
  536. }
  537. __STL_UNWIND(_M_put_node(__n));
  538. }
  539. //析构对象,并释放空间
  540. void _M_delete_node(_Node* __n)
  541. {
  542. destroy(&__n->_M_val);
  543. _M_put_node(__n);
  544. }
  545. void _M_erase_bucket(const size_type __n, _Node* __first, _Node* __last);
  546. void _M_erase_bucket(const size_type __n, _Node* __last);
  547. void _M_copy_from(const hashtable& __ht);
  548. };
  549. //前缀operator++重载,前进一个list节点
  550. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  551. class _All>
  552. _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&
  553. _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++(){
  554. const _Node* __old = _M_cur;
  555. _M_cur = _M_cur->_M_next;//若存在,则返回
  556. //若当前节点为空,则需前进到下一个桶子的节点
  557. if (!_M_cur) {
  558. //根据元素值,定位出下一个bucket的位置,其起始位置就是我们的目的地
  559. size_type __bucket = _M_ht->_M_bkt_num(__old->_M_val);
  560. while (!_M_cur && ++__bucket < _M_ht->_M_buckets.size())
  561. _M_cur = _M_ht->_M_buckets[__bucket];
  562. }
  563. return *this;}
  564. //后缀operator++重载
  565. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  566. class _All>
  567. inline _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>
  568. _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++(int){
  569. iterator __tmp = *this;
  570. ++*this;//调用operator++
  571. return __tmp;}
  572. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  573. class _All>
  574. _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&
  575. _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++(){
  576. const _Node* __old = _M_cur;
  577. _M_cur = _M_cur->_M_next;
  578. if (!_M_cur) {
  579. size_type __bucket = _M_ht->_M_bkt_num(__old->_M_val);
  580. while (!_M_cur && ++__bucket < _M_ht->_M_buckets.size())
  581. _M_cur = _M_ht->_M_buckets[__bucket];
  582. }
  583. return *this;}
  584. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  585. class _All>
  586. inline _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>
  587. _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>::operator++(int){
  588. const_iterator __tmp = *this;
  589. ++*this;
  590. return __tmp;}
  591. #ifndef __STL_CLASS_PARTIAL_SPECIALIZATION
  592. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  593. class _All>
  594. inline forward_iterator_tag
  595. iterator_category(const _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&){
  596. return forward_iterator_tag();}
  597. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  598. class _All>
  599. inline _Val*
  600. value_type(const _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&){
  601. return (_Val*) 0;}
  602. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  603. class _All>
  604. inline hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>::difference_type*distance_type(const _Hashtable_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&){
  605. return (hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>::difference_type*) 0;}
  606. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  607. class _All>
  608. inline forward_iterator_tag
  609. iterator_category(const _Hashtable_const_iterator<_Val,_Key,_HF,
  610. _ExK,_EqK,_All>&){
  611. return forward_iterator_tag();}
  612. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  613. class _All>
  614. inline _Val*
  615. value_type(const _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&){
  616. return (_Val*) 0;}
  617. template <class _Val, class _Key, class _HF, class _ExK, class _EqK,
  618. class _All>
  619. inline hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>::difference_type*distance_type(const _Hashtable_const_iterator<_Val,_Key,_HF,_ExK,_EqK,_All>&){
  620. return (hashtable<_Val,_Key,_HF,_ExK,_EqK,_All>::difference_type*) 0;}
  621. #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
  622. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  623. bool operator==(const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht1,
  624. const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht2){
  625. typedef typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::_Node _Node;
  626. if (__ht1._M_buckets.size() != __ht2._M_buckets.size())
  627. return false;
  628. for (int __n = 0; __n < __ht1._M_buckets.size(); ++__n) {
  629. _Node* __cur1 = __ht1._M_buckets[__n];
  630. _Node* __cur2 = __ht2._M_buckets[__n];
  631. for ( ; __cur1 && __cur2 && __cur1->_M_val == __cur2->_M_val;
  632. __cur1 = __cur1->_M_next, __cur2 = __cur2->_M_next)
  633. {}
  634. if (__cur1 || __cur2)
  635. return false;
  636. }
  637. return true;}
  638. #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
  639. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  640. inline bool operator!=(const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht1,
  641. const hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>& __ht2) {
  642. return !(__ht1 == __ht2);}
  643. template <class _Val, class _Key, class _HF, class _Extract, class _EqKey,
  644. class _All>
  645. inline void swap(hashtable<_Val, _Key, _HF, _Extract, _EqKey, _All>& __ht1,
  646. hashtable<_Val, _Key, _HF, _Extract, _EqKey, _All>& __ht2) {
  647. __ht1.swap(__ht2);}
  648. #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
  649. //插入元素,不需要重新调整内存空间,不允许存在重复元素
  650. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  651. pair<typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator, bool>
  652. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  653. ::insert_unique_noresize(const value_type& __obj){
  654. //获取待插入元素在hashtable中的桶子位置
  655. const size_type __n = _M_bkt_num(__obj);
  656. _Node* __first = _M_buckets[__n];
  657. //判断hashtable中是否存在与之相等的键值元素
  658. //若存在则不插入
  659. //否则插入该元素
  660. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  661. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))
  662. return pair<iterator, bool>(iterator(__cur, this), false);
  663. //把元素插入到第一个节点位置
  664. _Node* __tmp = _M_new_node(__obj);
  665. __tmp->_M_next = __first;
  666. _M_buckets[__n] = __tmp;
  667. ++_M_num_elements;
  668. return pair<iterator, bool>(iterator(__tmp, this), true);}
  669. //插入元素,允许重复,不需要分配新的空间//也就是说有足够的空间
  670. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  671. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator
  672. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  673. ::insert_equal_noresize(const value_type& __obj){
  674. //获取待插入元素在hashtable中的桶子位置
  675. const size_type __n = _M_bkt_num(__obj);
  676. _Node* __first = _M_buckets[__n];
  677. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  678. //若存在键值相同的元素,则插在相同元素下一个位置
  679. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj))) {
  680. _Node* __tmp = _M_new_node(__obj);//创建新节点
  681. __tmp->_M_next = __cur->_M_next;//将新节点插在当前节点之后
  682. __cur->_M_next = __tmp;
  683. ++_M_num_elements;//节点数加1
  684. return iterator(__tmp, this);//返回指向新增节点迭代器
  685. }
  686. //若不存在相同键值的元素,则插在第一个位置
  687. _Node* __tmp = _M_new_node(__obj);//创建新节点
  688. __tmp->_M_next = __first;//插入在链表表头
  689. _M_buckets[__n] = __tmp;
  690. ++_M_num_elements;//节点数加1
  691. return iterator(__tmp, this);//返回指向新增节点的迭代器}
  692. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  693. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::reference
  694. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::find_or_insert(const value_type& __obj){
  695. resize(_M_num_elements + 1);
  696. size_type __n = _M_bkt_num(__obj);
  697. _Node* __first = _M_buckets[__n];
  698. for (_Node* __cur = __first; __cur; __cur = __cur->_M_next)
  699. if (_M_equals(_M_get_key(__cur->_M_val), _M_get_key(__obj)))
  700. return __cur->_M_val;
  701. _Node* __tmp = _M_new_node(__obj);
  702. __tmp->_M_next = __first;
  703. _M_buckets[__n] = __tmp;
  704. ++_M_num_elements;
  705. return __tmp->_M_val;}
  706. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  707. pair<typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator,
  708. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::iterator>
  709. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::equal_range(const key_type& __key){
  710. typedef pair<iterator, iterator> _Pii;
  711. const size_type __n = _M_bkt_num_key(__key);
  712. for (_Node* __first = _M_buckets[__n]; __first; __first = __first->_M_next)
  713. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  714. for (_Node* __cur = __first->_M_next; __cur; __cur = __cur->_M_next)
  715. if (!_M_equals(_M_get_key(__cur->_M_val), __key))
  716. return _Pii(iterator(__first, this), iterator(__cur, this));
  717. for (size_type __m = __n + 1; __m < _M_buckets.size(); ++__m)
  718. if (_M_buckets[__m])
  719. return _Pii(iterator(__first, this),
  720. iterator(_M_buckets[__m], this));
  721. return _Pii(iterator(__first, this), end());
  722. }
  723. return _Pii(end(), end());}
  724. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  725. pair<typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::const_iterator,
  726. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::const_iterator>
  727. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  728. ::equal_range(const key_type& __key) const
  729. {
  730. typedef pair<const_iterator, const_iterator> _Pii;
  731. const size_type __n = _M_bkt_num_key(__key);
  732. for (const _Node* __first = _M_buckets[__n] ;
  733. __first;
  734. __first = __first->_M_next) {
  735. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  736. for (const _Node* __cur = __first->_M_next;
  737. __cur;
  738. __cur = __cur->_M_next)
  739. if (!_M_equals(_M_get_key(__cur->_M_val), __key))
  740. return _Pii(const_iterator(__first, this),
  741. const_iterator(__cur, this));
  742. for (size_type __m = __n + 1; __m < _M_buckets.size(); ++__m)
  743. if (_M_buckets[__m])
  744. return _Pii(const_iterator(__first, this),
  745. const_iterator(_M_buckets[__m], this));
  746. return _Pii(const_iterator(__first, this), end());
  747. }
  748. }
  749. return _Pii(end(), end());}
  750. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  751. typename hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::size_type
  752. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const key_type& __key){
  753. const size_type __n = _M_bkt_num_key(__key);
  754. _Node* __first = _M_buckets[__n];
  755. size_type __erased = 0;
  756. if (__first) {
  757. _Node* __cur = __first;
  758. _Node* __next = __cur->_M_next;
  759. while (__next) {
  760. if (_M_equals(_M_get_key(__next->_M_val), __key)) {
  761. __cur->_M_next = __next->_M_next;
  762. _M_delete_node(__next);
  763. __next = __cur->_M_next;
  764. ++__erased;
  765. --_M_num_elements;
  766. }
  767. else {
  768. __cur = __next;
  769. __next = __cur->_M_next;
  770. }
  771. }
  772. if (_M_equals(_M_get_key(__first->_M_val), __key)) {
  773. _M_buckets[__n] = __first->_M_next;
  774. _M_delete_node(__first);
  775. ++__erased;
  776. --_M_num_elements;
  777. }
  778. }
  779. return __erased;}
  780. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  781. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const iterator& __it){
  782. _Node* __p = __it._M_cur;
  783. if (__p) {
  784. const size_type __n = _M_bkt_num(__p->_M_val);
  785. _Node* __cur = _M_buckets[__n];
  786. if (__cur == __p) {
  787. _M_buckets[__n] = __cur->_M_next;
  788. _M_delete_node(__cur);
  789. --_M_num_elements;
  790. }
  791. else {
  792. _Node* __next = __cur->_M_next;
  793. while (__next) {
  794. if (__next == __p) {
  795. __cur->_M_next = __next->_M_next;
  796. _M_delete_node(__next);
  797. --_M_num_elements;
  798. break;
  799. }
  800. else {
  801. __cur = __next;
  802. __next = __cur->_M_next;
  803. }
  804. }
  805. }
  806. }}
  807. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  808. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  809. ::erase(iterator __first, iterator __last){
  810. size_type __f_bucket = __first._M_cur ?
  811. _M_bkt_num(__first._M_cur->_M_val) : _M_buckets.size();
  812. size_type __l_bucket = __last._M_cur ?
  813. _M_bkt_num(__last._M_cur->_M_val) : _M_buckets.size();
  814. if (__first._M_cur == __last._M_cur)
  815. return;
  816. else if (__f_bucket == __l_bucket)
  817. _M_erase_bucket(__f_bucket, __first._M_cur, __last._M_cur);
  818. else {
  819. _M_erase_bucket(__f_bucket, __first._M_cur, 0);
  820. for (size_type __n = __f_bucket + 1; __n < __l_bucket; ++__n)
  821. _M_erase_bucket(__n, 0);
  822. if (__l_bucket != _M_buckets.size())
  823. _M_erase_bucket(__l_bucket, __last._M_cur);
  824. }}
  825. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  826. inline void
  827. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const_iterator __first,
  828. const_iterator __last){
  829. erase(iterator(const_cast<_Node*>(__first._M_cur),
  830. const_cast<hashtable*>(__first._M_ht)),
  831. iterator(const_cast<_Node*>(__last._M_cur),
  832. const_cast<hashtable*>(__last._M_ht)));}
  833. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  834. inline void
  835. hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::erase(const const_iterator& __it){
  836. erase(iterator(const_cast<_Node*>(__it._M_cur),
  837. const_cast<hashtable*>(__it._M_ht)));}
  838. //调整hashtable的容量//新的容量大小为__num_elements_hint
  839. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  840. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  841. ::resize(size_type __num_elements_hint){
  842. //hashtable原始大小
  843. const size_type __old_n = _M_buckets.size();
  844. if (__num_elements_hint > __old_n) {//若新的容量大小比原始的大
  845. //查找不低于__num_elements_hint的最小素数
  846. const size_type __n = _M_next_size(__num_elements_hint);
  847. if (__n > __old_n) {
  848. //创建新的线性表,容量为__n,只是起到中介作用
  849. vector<_Node*, _All> __tmp(__n, (_Node*)(0),
  850. _M_buckets.get_allocator());
  851. __STL_TRY {//以下是复制数据
  852. for (size_type __bucket = 0; __bucket < __old_n; ++__bucket) {
  853. _Node* __first = _M_buckets[__bucket];
  854. while (__first) {
  855. //获取实值在新桶子的键值位置
  856. size_type __new_bucket = _M_bkt_num(__first->_M_val, __n);
  857. //这个只是为了方便while循环里面__first的迭代
  858. _M_buckets[__bucket] = __first->_M_next;
  859. //将当前节点插入到新的桶子__new_bucket里面,成为list的第一个节点
  860. __first->_M_next = __tmp[__new_bucket];//__first->_M_next指向null指针,因为新桶子是空的
  861. __tmp[__new_bucket] = __first;//新桶子对应键值指向第一个节点
  862. __first = _M_buckets[__bucket];//更新当前指针
  863. }
  864. }
  865. _M_buckets.swap(__tmp);//交换内容
  866. }
  867. # ifdef __STL_USE_EXCEPTIONS
  868. catch(...) {//释放临时hashtable的线性表tmp
  869. for (size_type __bucket = 0; __bucket < __tmp.size(); ++__bucket) {
  870. while (__tmp[__bucket]) {
  871. _Node* __next = __tmp[__bucket]->_M_next;
  872. _M_delete_node(__tmp[__bucket]);
  873. __tmp[__bucket] = __next;
  874. }
  875. }
  876. throw;
  877. }
  878. # endif /* __STL_USE_EXCEPTIONS */
  879. }
  880. }}
  881. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  882. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  883. ::_M_erase_bucket(const size_type __n, _Node* __first, _Node* __last){
  884. _Node* __cur = _M_buckets[__n];
  885. if (__cur == __first)
  886. _M_erase_bucket(__n, __last);
  887. else {
  888. _Node* __next;
  889. for (__next = __cur->_M_next;
  890. __next != __first;
  891. __cur = __next, __next = __cur->_M_next)
  892. ;
  893. while (__next != __last) {
  894. __cur->_M_next = __next->_M_next;
  895. _M_delete_node(__next);
  896. __next = __cur->_M_next;
  897. --_M_num_elements;
  898. }
  899. }}
  900. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  901. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  902. ::_M_erase_bucket(const size_type __n, _Node* __last){
  903. _Node* __cur = _M_buckets[__n];
  904. while (__cur != __last) {
  905. _Node* __next = __cur->_M_next;
  906. _M_delete_node(__cur);
  907. __cur = __next;
  908. _M_buckets[__n] = __cur;
  909. --_M_num_elements;
  910. }}
  911. //清空hashtable,但是没有释放bucket vector空间
  912. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  913. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>::clear(){
  914. for (size_type __i = 0; __i < _M_buckets.size(); ++__i) {//遍历每个桶子
  915. _Node* __cur = _M_buckets[__i];//当前节点为桶子的第一个节点
  916. while (__cur != 0) {//遍历桶子维护的链表,并释放每个链表节点
  917. _Node* __next = __cur->_M_next;
  918. _M_delete_node(__cur);
  919. __cur = __next;
  920. }
  921. _M_buckets[__i] = 0;//桶子链表为空
  922. }
  923. _M_num_elements = 0;//链表节点数为0}
  924. //拷贝hashtable对象//实现机制:首先把原始对象清空,再把空间变成被复制对象__ht一样的大小//最后把__ht的内容复制到目标对象
  925. template <class _Val, class _Key, class _HF, class _Ex, class _Eq, class _All>
  926. void hashtable<_Val,_Key,_HF,_Ex,_Eq,_All>
  927. ::_M_copy_from(const hashtable& __ht){//_M_buckets是vector维护的,可以调用vector的成员函数
  928. _M_buckets.clear();//清空线性表调用vector::clear()
  929. _M_buckets.reserve(__ht._M_buckets.size());//把线性表内存空间变成与__ht一样大
  930. //在_M_buckets vector尾端插入size个元素,初始值为空指针
  931. //注意:此时_M_buckets vector为空,即尾端也是起始端
  932. _M_buckets.insert(_M_buckets.end(), __ht._M_buckets.size(), (_Node*) 0);
  933. __STL_TRY {//开始复制操作
  934. for (size_type __i = 0; __i < __ht._M_buckets.size(); ++__i) {
  935. //复制vector的每一个元素(是指向hashtable节点指针)
  936. const _Node* __cur = __ht._M_buckets[__i];
  937. if (__cur) {
  938. _Node* __copy = _M_new_node(__cur->_M_val);
  939. _M_buckets[__i] = __copy;
  940. //针对每一个hashtable节点对应的list,复制list的每一个节点
  941. for (_Node* __next = __cur->_M_next;
  942. __next;
  943. __cur = __next, __next = __cur->_M_next) {
  944. __copy->_M_next = _M_new_node(__next->_M_val);
  945. __copy = __copy->_M_next;
  946. }
  947. }
  948. }
  949. _M_num_elements = __ht._M_num_elements;//更新节点个数
  950. }
  951. __STL_UNWIND(clear());}
  952. __STL_END_NAMESPACE
  953. #endif /* __SGI_STL_INTERNAL_HASHTABLE_H */
  954. // Local Variables:// mode:C++// End:

hash_fun结构

#ifndef __SGI_STL_HASH_FUN_H
#define __SGI_STL_HASH_FUN_H

#include <stddef.h>

__STL_BEGIN_NAMESPACE

//hash function 是计算元素位置的函数//这些函数可以对hashtable进行取模运算//这是hashtable所提供的散列函数是取模运算决定的

/*
SGI hashtable以下有限的定义类型:
        struct hash<char*>
        struct hash<const char*>
        struct hash<char> 
        struct hash<unsigned char> 
        struct hash<signed char>
        struct hash<short>
        struct hash<unsigned short> 
        struct hash<int> 
        struct hash<unsigned int>
        struct hash<long> 
        struct hash<unsigned long>
不在这里定义的类型,不能使用,若用户想要使用,则必须自己定义。例如:string,double,float
*//*定义自己的哈希函数时要注意以下几点:
        [1]使用struct,然后重载operator().
        [2]返回是size_t
        [3]参数是你要hash的key的类型。
        [4]函数是const类型的。
*/

template <class _Key> struct hash { };

//对const char* 提供字符串转换函数
inline size_t __stl_hash_string(const char* __s){
  unsigned long __h = 0; 
  for ( ; *__s; ++__s)
    __h = 5*__h + *__s;

  return size_t(__h);}

__STL_TEMPLATE_NULL struct hash<char*>{
  size_t operator()(const char* __s) const { return __stl_hash_string(__s); }};

__STL_TEMPLATE_NULL struct hash<const char*>{
  size_t operator()(const char* __s) const { return __stl_hash_string(__s); }};

//下面的hash函数都是直接返回原值
//对于char,unsigned char,signed char,int,unsigned int, 
//short, unsigned short, long,unsigned long都只是返回数值本身
__STL_TEMPLATE_NULL struct hash<char> {
  size_t operator()(char __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<unsigned char> {
  size_t operator()(unsigned char __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<signed char> {
  size_t operator()(unsigned char __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<short> {
  size_t operator()(short __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<unsigned short> {
  size_t operator()(unsigned short __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<int> {
  size_t operator()(int __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<unsigned int> {
  size_t operator()(unsigned int __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<long> {
  size_t operator()(long __x) const { return __x; }};
__STL_TEMPLATE_NULL struct hash<unsigned long> {
  size_t operator()(unsigned long __x) const { return __x; }};

__STL_END_NAMESPACE

#endif /* __SGI_STL_HASH_FUN_H */

// Local Variables:// mode:C++// End: