1 // Internal header for TR1 unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr1_impl/hashtable
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
30 // This header file defines std::tr1::hashtable, which is used to
31 // implement std::tr1::unordered_set, std::tr1::unordered_map,
32 // std::tr1::unordered_multiset, and std::tr1::unordered_multimap.
33 // hashtable has many template parameters, partly to accommodate
34 // the differences between those four classes and partly to
35 // accommodate policy choices that go beyond TR1 specifications.
37 // Class template hashtable attempts to encapsulate all reasonable
38 // variation among hash tables that use chaining. It does not handle
42 // M. Austern, "A Proposal to Add Hash Tables to the Standard
43 // Library (revision 4)," WG21 Document N1456=03-0039, 2003.
44 // D. E. Knuth, The Art of Computer Programming, v. 3, Sorting and Searching.
45 // A. Tavori and V. Dreizin, "Policy-Based Data Structures", 2004.
46 // http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/index.html
48 #include <tr1_impl/hashtable_policy.h>
52 _GLIBCXX_BEGIN_NAMESPACE_TR1
54 // Class template _Hashtable, class definition.
56 // Meaning of class template _Hashtable's template parameters
58 // _Key and _Value: arbitrary CopyConstructible types.
60 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
61 // value type is Value. As a conforming extension, we allow for
62 // value type != Value.
64 // _ExtractKey: function object that takes a object of type Value
65 // and returns a value of type _Key.
67 // _Equal: function object that takes two objects of type k and returns
68 // a bool-like value that is true if the two objects are considered equal.
70 // _H1: the hash function. A unary function object with argument type
71 // Key and result type size_t. Return values should be distributed
72 // over the entire range [0, numeric_limits<size_t>:::max()].
74 // _H2: the range-hashing function (in the terminology of Tavori and
75 // Dreizin). A binary function object whose argument types and result
76 // type are all size_t. Given arguments r and N, the return value is
77 // in the range [0, N).
79 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
80 // whose argument types are _Key and size_t and whose result type is
81 // size_t. Given arguments k and N, the return value is in the range
82 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
83 // than the default, _H1 and _H2 are ignored.
85 // _RehashPolicy: Policy class with three members, all of which govern
86 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
87 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
88 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
89 // determines whether, if the current bucket count is n_bkt and the
90 // current element count is n_elt, we need to increase the bucket
91 // count. If so, returns make_pair(true, n), where n is the new
92 // bucket count. If not, returns make_pair(false, <anything>).
94 // ??? Right now it is hard-wired that the number of buckets never
95 // shrinks. Should we allow _RehashPolicy to change that?
97 // __cache_hash_code: bool. true if we store the value of the hash
98 // function along with the value. This is a time-space tradeoff.
99 // Storing it may improve lookup speed by reducing the number of times
100 // we need to call the Equal function.
102 // __constant_iterators: bool. true if iterator and const_iterator are
103 // both constant iterator types. This is true for unordered_set and
104 // unordered_multiset, false for unordered_map and unordered_multimap.
106 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
107 // is always at most one, false if it may be an arbitrary number. This
108 // true for unordered_set and unordered_map, false for unordered_multiset
109 // and unordered_multimap.
111 template<typename _Key, typename _Value, typename _Allocator,
112 typename _ExtractKey, typename _Equal,
113 typename _H1, typename _H2, typename _Hash,
114 typename _RehashPolicy,
115 bool __cache_hash_code,
116 bool __constant_iterators,
119 : public __detail::_Rehash_base<_RehashPolicy,
120 _Hashtable<_Key, _Value, _Allocator,
122 _Equal, _H1, _H2, _Hash,
125 __constant_iterators,
127 public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
128 _H1, _H2, _Hash, __cache_hash_code>,
129 public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
130 _Hashtable<_Key, _Value, _Allocator,
132 _Equal, _H1, _H2, _Hash,
135 __constant_iterators,
139 typedef _Allocator allocator_type;
140 typedef _Value value_type;
141 typedef _Key key_type;
142 typedef _Equal key_equal;
143 // mapped_type, if present, comes from _Map_base.
144 // hasher, if present, comes from _Hash_code_base.
145 typedef typename _Allocator::difference_type difference_type;
146 typedef typename _Allocator::size_type size_type;
147 typedef typename _Allocator::pointer pointer;
148 typedef typename _Allocator::const_pointer const_pointer;
149 typedef typename _Allocator::reference reference;
150 typedef typename _Allocator::const_reference const_reference;
152 typedef __detail::_Node_iterator<value_type, __constant_iterators,
155 typedef __detail::_Node_const_iterator<value_type,
156 __constant_iterators,
158 const_local_iterator;
160 typedef __detail::_Hashtable_iterator<value_type, __constant_iterators,
163 typedef __detail::_Hashtable_const_iterator<value_type,
164 __constant_iterators,
168 template<typename _Key2, typename _Value2, typename _Ex2, bool __unique2,
169 typename _Hashtable2>
170 friend struct __detail::_Map_base;
173 typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
174 typedef typename _Allocator::template rebind<_Node>::other
175 _Node_allocator_type;
176 typedef typename _Allocator::template rebind<_Node*>::other
177 _Bucket_allocator_type;
179 typedef typename _Allocator::template rebind<_Value>::other
180 _Value_allocator_type;
182 _Node_allocator_type _M_node_allocator;
184 size_type _M_bucket_count;
185 size_type _M_element_count;
186 _RehashPolicy _M_rehash_policy;
189 _M_allocate_node(const value_type& __v);
192 _M_deallocate_node(_Node* __n);
195 _M_deallocate_nodes(_Node**, size_type);
198 _M_allocate_buckets(size_type __n);
201 _M_deallocate_buckets(_Node**, size_type __n);
204 // Constructor, destructor, assignment, swap
205 _Hashtable(size_type __bucket_hint,
206 const _H1&, const _H2&, const _Hash&,
207 const _Equal&, const _ExtractKey&,
208 const allocator_type&);
210 template<typename _InputIterator>
211 _Hashtable(_InputIterator __first, _InputIterator __last,
212 size_type __bucket_hint,
213 const _H1&, const _H2&, const _Hash&,
214 const _Equal&, const _ExtractKey&,
215 const allocator_type&);
217 _Hashtable(const _Hashtable&);
219 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
220 _Hashtable(_Hashtable&&);
224 operator=(const _Hashtable&);
228 void swap(_Hashtable&);
230 // Basic container operations
234 iterator __i(_M_buckets);
235 if (!__i._M_cur_node)
236 __i._M_incr_bucket();
243 const_iterator __i(_M_buckets);
244 if (!__i._M_cur_node)
245 __i._M_incr_bucket();
251 { return iterator(_M_buckets + _M_bucket_count); }
255 { return const_iterator(_M_buckets + _M_bucket_count); }
257 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
261 const_iterator __i(_M_buckets);
262 if (!__i._M_cur_node)
263 __i._M_incr_bucket();
269 { return const_iterator(_M_buckets + _M_bucket_count); }
274 { return _M_element_count; }
278 { return size() == 0; }
281 get_allocator() const
282 { return allocator_type(_M_node_allocator); }
284 _Value_allocator_type
285 _M_get_Value_allocator() const
286 { return _Value_allocator_type(_M_node_allocator); }
290 { return _M_node_allocator.max_size(); }
295 { return this->_M_eq; }
297 // hash_function, if present, comes from _Hash_code_base.
302 { return _M_bucket_count; }
305 max_bucket_count() const
306 { return max_size(); }
309 bucket_size(size_type __n) const
310 { return std::distance(begin(__n), end(__n)); }
313 bucket(const key_type& __k) const
315 return this->_M_bucket_index(__k, this->_M_hash_code(__k),
321 { return local_iterator(_M_buckets[__n]); }
325 { return local_iterator(0); }
328 begin(size_type __n) const
329 { return const_local_iterator(_M_buckets[__n]); }
333 { return const_local_iterator(0); }
335 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
338 cbegin(size_type __n) const
339 { return const_local_iterator(_M_buckets[__n]); }
342 cend(size_type) const
343 { return const_local_iterator(0); }
349 return static_cast<float>(size()) / static_cast<float>(bucket_count());
352 // max_load_factor, if present, comes from _Rehash_base.
354 // Generalization of max_load_factor. Extension, not found in TR1. Only
355 // useful if _RehashPolicy is something other than the default.
357 __rehash_policy() const
358 { return _M_rehash_policy; }
361 __rehash_policy(const _RehashPolicy&);
365 find(const key_type& __k);
368 find(const key_type& __k) const;
371 count(const key_type& __k) const;
373 std::pair<iterator, iterator>
374 equal_range(const key_type& __k);
376 std::pair<const_iterator, const_iterator>
377 equal_range(const key_type& __k) const;
379 private: // Find, insert and erase helper functions
380 // ??? This dispatching is a workaround for the fact that we don't
381 // have partial specialization of member templates; it would be
382 // better to just specialize insert on __unique_keys. There may be a
383 // cleaner workaround.
384 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
385 std::pair<iterator, bool>, iterator>::__type
388 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
389 std::_Select1st<_Insert_Return_Type>,
390 std::_Identity<_Insert_Return_Type>
395 _M_find_node(_Node*, const key_type&,
396 typename _Hashtable::_Hash_code_type) const;
399 _M_insert_bucket(const value_type&, size_type,
400 typename _Hashtable::_Hash_code_type);
402 std::pair<iterator, bool>
403 _M_insert(const value_type&, std::_GLIBCXX_TR1 true_type);
406 _M_insert(const value_type&, std::_GLIBCXX_TR1 false_type);
409 _M_erase_node(_Node*, _Node**);
414 insert(const value_type& __v)
415 { return _M_insert(__v, std::_GLIBCXX_TR1 integral_constant<bool,
419 insert(iterator, const value_type& __v)
420 { return iterator(_Insert_Conv_Type()(this->insert(__v))); }
423 insert(const_iterator, const value_type& __v)
424 { return const_iterator(_Insert_Conv_Type()(this->insert(__v))); }
426 template<typename _InputIterator>
428 insert(_InputIterator __first, _InputIterator __last);
430 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
432 insert(initializer_list<value_type> __l)
433 { this->insert(__l.begin(), __l.end()); }
440 erase(const_iterator);
443 erase(const key_type&);
446 erase(iterator, iterator);
449 erase(const_iterator, const_iterator);
454 // Set number of buckets to be appropriate for container of n element.
455 void rehash(size_type __n);
458 // Unconditionally change size of bucket array to n.
459 void _M_rehash(size_type __n);
463 // Definitions of class template _Hashtable's out-of-line member functions.
464 template<typename _Key, typename _Value,
465 typename _Allocator, typename _ExtractKey, typename _Equal,
466 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
467 bool __chc, bool __cit, bool __uk>
468 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
469 _H1, _H2, _Hash, _RehashPolicy,
470 __chc, __cit, __uk>::_Node*
471 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
472 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
473 _M_allocate_node(const value_type& __v)
475 _Node* __n = _M_node_allocator.allocate(1);
478 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
479 _M_node_allocator.construct(__n, __v);
481 _M_get_Value_allocator().construct(&__n->_M_v, __v);
488 _M_node_allocator.deallocate(__n, 1);
489 __throw_exception_again;
493 template<typename _Key, typename _Value,
494 typename _Allocator, typename _ExtractKey, typename _Equal,
495 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
496 bool __chc, bool __cit, bool __uk>
498 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
499 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
500 _M_deallocate_node(_Node* __n)
502 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
503 _M_node_allocator.destroy(__n);
505 _M_get_Value_allocator().destroy(&__n->_M_v);
507 _M_node_allocator.deallocate(__n, 1);
510 template<typename _Key, typename _Value,
511 typename _Allocator, typename _ExtractKey, typename _Equal,
512 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
513 bool __chc, bool __cit, bool __uk>
515 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
516 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
517 _M_deallocate_nodes(_Node** __array, size_type __n)
519 for (size_type __i = 0; __i < __n; ++__i)
521 _Node* __p = __array[__i];
526 _M_deallocate_node(__tmp);
532 template<typename _Key, typename _Value,
533 typename _Allocator, typename _ExtractKey, typename _Equal,
534 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
535 bool __chc, bool __cit, bool __uk>
536 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
537 _H1, _H2, _Hash, _RehashPolicy,
538 __chc, __cit, __uk>::_Node**
539 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
540 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
541 _M_allocate_buckets(size_type __n)
543 _Bucket_allocator_type __alloc(_M_node_allocator);
545 // We allocate one extra bucket to hold a sentinel, an arbitrary
546 // non-null pointer. Iterator increment relies on this.
547 _Node** __p = __alloc.allocate(__n + 1);
548 std::fill(__p, __p + __n, (_Node*) 0);
549 __p[__n] = reinterpret_cast<_Node*>(0x1000);
553 template<typename _Key, typename _Value,
554 typename _Allocator, typename _ExtractKey, typename _Equal,
555 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
556 bool __chc, bool __cit, bool __uk>
558 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
559 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
560 _M_deallocate_buckets(_Node** __p, size_type __n)
562 _Bucket_allocator_type __alloc(_M_node_allocator);
563 __alloc.deallocate(__p, __n + 1);
566 template<typename _Key, typename _Value,
567 typename _Allocator, typename _ExtractKey, typename _Equal,
568 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
569 bool __chc, bool __cit, bool __uk>
570 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
571 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
572 _Hashtable(size_type __bucket_hint,
573 const _H1& __h1, const _H2& __h2, const _Hash& __h,
574 const _Equal& __eq, const _ExtractKey& __exk,
575 const allocator_type& __a)
576 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
577 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
578 _H1, _H2, _Hash, __chc>(__exk, __eq,
580 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
581 _M_node_allocator(__a),
586 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
587 _M_buckets = _M_allocate_buckets(_M_bucket_count);
590 template<typename _Key, typename _Value,
591 typename _Allocator, typename _ExtractKey, typename _Equal,
592 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
593 bool __chc, bool __cit, bool __uk>
594 template<typename _InputIterator>
595 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
596 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
597 _Hashtable(_InputIterator __f, _InputIterator __l,
598 size_type __bucket_hint,
599 const _H1& __h1, const _H2& __h2, const _Hash& __h,
600 const _Equal& __eq, const _ExtractKey& __exk,
601 const allocator_type& __a)
602 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
603 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
604 _H1, _H2, _Hash, __chc>(__exk, __eq,
606 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
607 _M_node_allocator(__a),
612 _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
614 _M_bkt_for_elements(__detail::
617 _M_buckets = _M_allocate_buckets(_M_bucket_count);
620 for (; __f != __l; ++__f)
626 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
627 __throw_exception_again;
631 template<typename _Key, typename _Value,
632 typename _Allocator, typename _ExtractKey, typename _Equal,
633 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
634 bool __chc, bool __cit, bool __uk>
635 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
636 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
637 _Hashtable(const _Hashtable& __ht)
638 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
639 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
640 _H1, _H2, _Hash, __chc>(__ht),
641 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
642 _M_node_allocator(__ht._M_node_allocator),
643 _M_bucket_count(__ht._M_bucket_count),
644 _M_element_count(__ht._M_element_count),
645 _M_rehash_policy(__ht._M_rehash_policy)
647 _M_buckets = _M_allocate_buckets(_M_bucket_count);
650 for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
652 _Node* __n = __ht._M_buckets[__i];
653 _Node** __tail = _M_buckets + __i;
656 *__tail = _M_allocate_node(__n->_M_v);
657 this->_M_copy_code(*__tail, __n);
658 __tail = &((*__tail)->_M_next);
666 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
667 __throw_exception_again;
671 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
672 template<typename _Key, typename _Value,
673 typename _Allocator, typename _ExtractKey, typename _Equal,
674 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
675 bool __chc, bool __cit, bool __uk>
676 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
677 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
678 _Hashtable(_Hashtable&& __ht)
679 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
680 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
681 _H1, _H2, _Hash, __chc>(__ht),
682 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
683 _M_node_allocator(__ht._M_node_allocator),
684 _M_bucket_count(__ht._M_bucket_count),
685 _M_element_count(__ht._M_element_count),
686 _M_rehash_policy(__ht._M_rehash_policy),
687 _M_buckets(__ht._M_buckets)
689 size_type __n_bkt = __ht._M_rehash_policy._M_next_bkt(0);
690 __ht._M_buckets = __ht._M_allocate_buckets(__n_bkt);
691 __ht._M_bucket_count = __n_bkt;
692 __ht._M_element_count = 0;
693 __ht._M_rehash_policy = _RehashPolicy();
697 template<typename _Key, typename _Value,
698 typename _Allocator, typename _ExtractKey, typename _Equal,
699 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
700 bool __chc, bool __cit, bool __uk>
701 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
702 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>&
703 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
704 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
705 operator=(const _Hashtable& __ht)
707 _Hashtable __tmp(__ht);
712 template<typename _Key, typename _Value,
713 typename _Allocator, typename _ExtractKey, typename _Equal,
714 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
715 bool __chc, bool __cit, bool __uk>
716 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
717 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
721 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
724 template<typename _Key, typename _Value,
725 typename _Allocator, typename _ExtractKey, typename _Equal,
726 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
727 bool __chc, bool __cit, bool __uk>
729 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
730 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
731 swap(_Hashtable& __x)
733 // The only base class with member variables is hash_code_base. We
734 // define _Hash_code_base::_M_swap because different specializations
735 // have different members.
736 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
737 _H1, _H2, _Hash, __chc>::_M_swap(__x);
739 // _GLIBCXX_RESOLVE_LIB_DEFECTS
740 // 431. Swapping containers with unequal allocators.
741 std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
742 __x._M_node_allocator);
744 std::swap(_M_rehash_policy, __x._M_rehash_policy);
745 std::swap(_M_buckets, __x._M_buckets);
746 std::swap(_M_bucket_count, __x._M_bucket_count);
747 std::swap(_M_element_count, __x._M_element_count);
750 template<typename _Key, typename _Value,
751 typename _Allocator, typename _ExtractKey, typename _Equal,
752 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
753 bool __chc, bool __cit, bool __uk>
755 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
756 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
757 __rehash_policy(const _RehashPolicy& __pol)
759 _M_rehash_policy = __pol;
760 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
761 if (__n_bkt > _M_bucket_count)
765 template<typename _Key, typename _Value,
766 typename _Allocator, typename _ExtractKey, typename _Equal,
767 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
768 bool __chc, bool __cit, bool __uk>
769 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
770 _H1, _H2, _Hash, _RehashPolicy,
771 __chc, __cit, __uk>::iterator
772 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
773 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
774 find(const key_type& __k)
776 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
777 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
778 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
779 return __p ? iterator(__p, _M_buckets + __n) : this->end();
782 template<typename _Key, typename _Value,
783 typename _Allocator, typename _ExtractKey, typename _Equal,
784 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
785 bool __chc, bool __cit, bool __uk>
786 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
787 _H1, _H2, _Hash, _RehashPolicy,
788 __chc, __cit, __uk>::const_iterator
789 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
790 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
791 find(const key_type& __k) const
793 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
794 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
795 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
796 return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
799 template<typename _Key, typename _Value,
800 typename _Allocator, typename _ExtractKey, typename _Equal,
801 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
802 bool __chc, bool __cit, bool __uk>
803 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
804 _H1, _H2, _Hash, _RehashPolicy,
805 __chc, __cit, __uk>::size_type
806 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
807 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
808 count(const key_type& __k) const
810 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
811 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
812 std::size_t __result = 0;
813 for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
814 if (this->_M_compare(__k, __code, __p))
819 template<typename _Key, typename _Value,
820 typename _Allocator, typename _ExtractKey, typename _Equal,
821 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
822 bool __chc, bool __cit, bool __uk>
823 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
824 _ExtractKey, _Equal, _H1,
825 _H2, _Hash, _RehashPolicy,
826 __chc, __cit, __uk>::iterator,
827 typename _Hashtable<_Key, _Value, _Allocator,
828 _ExtractKey, _Equal, _H1,
829 _H2, _Hash, _RehashPolicy,
830 __chc, __cit, __uk>::iterator>
831 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
832 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
833 equal_range(const key_type& __k)
835 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
836 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
837 _Node** __head = _M_buckets + __n;
838 _Node* __p = _M_find_node(*__head, __k, __code);
842 _Node* __p1 = __p->_M_next;
843 for (; __p1; __p1 = __p1->_M_next)
844 if (!this->_M_compare(__k, __code, __p1))
847 iterator __first(__p, __head);
848 iterator __last(__p1, __head);
850 __last._M_incr_bucket();
851 return std::make_pair(__first, __last);
854 return std::make_pair(this->end(), this->end());
857 template<typename _Key, typename _Value,
858 typename _Allocator, typename _ExtractKey, typename _Equal,
859 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
860 bool __chc, bool __cit, bool __uk>
861 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
862 _ExtractKey, _Equal, _H1,
863 _H2, _Hash, _RehashPolicy,
864 __chc, __cit, __uk>::const_iterator,
865 typename _Hashtable<_Key, _Value, _Allocator,
866 _ExtractKey, _Equal, _H1,
867 _H2, _Hash, _RehashPolicy,
868 __chc, __cit, __uk>::const_iterator>
869 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
870 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
871 equal_range(const key_type& __k) const
873 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
874 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
875 _Node** __head = _M_buckets + __n;
876 _Node* __p = _M_find_node(*__head, __k, __code);
880 _Node* __p1 = __p->_M_next;
881 for (; __p1; __p1 = __p1->_M_next)
882 if (!this->_M_compare(__k, __code, __p1))
885 const_iterator __first(__p, __head);
886 const_iterator __last(__p1, __head);
888 __last._M_incr_bucket();
889 return std::make_pair(__first, __last);
892 return std::make_pair(this->end(), this->end());
895 // Find the node whose key compares equal to k, beginning the search
896 // at p (usually the head of a bucket). Return nil if no node is found.
897 template<typename _Key, typename _Value,
898 typename _Allocator, typename _ExtractKey, typename _Equal,
899 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
900 bool __chc, bool __cit, bool __uk>
901 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
902 _Equal, _H1, _H2, _Hash, _RehashPolicy,
903 __chc, __cit, __uk>::_Node*
904 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
905 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
906 _M_find_node(_Node* __p, const key_type& __k,
907 typename _Hashtable::_Hash_code_type __code) const
909 for (; __p; __p = __p->_M_next)
910 if (this->_M_compare(__k, __code, __p))
915 // Insert v in bucket n (assumes no element with its key already present).
916 template<typename _Key, typename _Value,
917 typename _Allocator, typename _ExtractKey, typename _Equal,
918 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
919 bool __chc, bool __cit, bool __uk>
920 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
921 _H1, _H2, _Hash, _RehashPolicy,
922 __chc, __cit, __uk>::iterator
923 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
924 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
925 _M_insert_bucket(const value_type& __v, size_type __n,
926 typename _Hashtable::_Hash_code_type __code)
928 std::pair<bool, std::size_t> __do_rehash
929 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
930 _M_element_count, 1);
932 // Allocate the new node before doing the rehash so that we don't
933 // do a rehash if the allocation throws.
934 _Node* __new_node = _M_allocate_node(__v);
938 if (__do_rehash.first)
940 const key_type& __k = this->_M_extract(__v);
941 __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
942 _M_rehash(__do_rehash.second);
945 __new_node->_M_next = _M_buckets[__n];
946 this->_M_store_code(__new_node, __code);
947 _M_buckets[__n] = __new_node;
949 return iterator(__new_node, _M_buckets + __n);
953 _M_deallocate_node(__new_node);
954 __throw_exception_again;
958 // Insert v if no element with its key is already present.
959 template<typename _Key, typename _Value,
960 typename _Allocator, typename _ExtractKey, typename _Equal,
961 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
962 bool __chc, bool __cit, bool __uk>
963 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
964 _ExtractKey, _Equal, _H1,
965 _H2, _Hash, _RehashPolicy,
966 __chc, __cit, __uk>::iterator, bool>
967 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
968 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
969 _M_insert(const value_type& __v, std::_GLIBCXX_TR1 true_type)
971 const key_type& __k = this->_M_extract(__v);
972 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
973 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
975 if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
976 return std::make_pair(iterator(__p, _M_buckets + __n), false);
977 return std::make_pair(_M_insert_bucket(__v, __n, __code), true);
980 // Insert v unconditionally.
981 template<typename _Key, typename _Value,
982 typename _Allocator, typename _ExtractKey, typename _Equal,
983 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
984 bool __chc, bool __cit, bool __uk>
985 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
986 _H1, _H2, _Hash, _RehashPolicy,
987 __chc, __cit, __uk>::iterator
988 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
989 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
990 _M_insert(const value_type& __v, std::_GLIBCXX_TR1 false_type)
992 std::pair<bool, std::size_t> __do_rehash
993 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
994 _M_element_count, 1);
995 if (__do_rehash.first)
996 _M_rehash(__do_rehash.second);
998 const key_type& __k = this->_M_extract(__v);
999 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1000 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1002 // First find the node, avoid leaking new_node if compare throws.
1003 _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
1004 _Node* __new_node = _M_allocate_node(__v);
1008 __new_node->_M_next = __prev->_M_next;
1009 __prev->_M_next = __new_node;
1013 __new_node->_M_next = _M_buckets[__n];
1014 _M_buckets[__n] = __new_node;
1016 this->_M_store_code(__new_node, __code);
1019 return iterator(__new_node, _M_buckets + __n);
1022 // For erase(iterator) and erase(const_iterator).
1023 template<typename _Key, typename _Value,
1024 typename _Allocator, typename _ExtractKey, typename _Equal,
1025 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1026 bool __chc, bool __cit, bool __uk>
1028 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1029 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1030 _M_erase_node(_Node* __p, _Node** __b)
1032 _Node* __cur = *__b;
1034 *__b = __cur->_M_next;
1037 _Node* __next = __cur->_M_next;
1038 while (__next != __p)
1041 __next = __cur->_M_next;
1043 __cur->_M_next = __next->_M_next;
1046 _M_deallocate_node(__p);
1050 template<typename _Key, typename _Value,
1051 typename _Allocator, typename _ExtractKey, typename _Equal,
1052 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1053 bool __chc, bool __cit, bool __uk>
1054 template<typename _InputIterator>
1056 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1057 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1058 insert(_InputIterator __first, _InputIterator __last)
1060 size_type __n_elt = __detail::__distance_fw(__first, __last);
1061 std::pair<bool, std::size_t> __do_rehash
1062 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1063 _M_element_count, __n_elt);
1064 if (__do_rehash.first)
1065 _M_rehash(__do_rehash.second);
1067 for (; __first != __last; ++__first)
1068 this->insert(*__first);
1071 template<typename _Key, typename _Value,
1072 typename _Allocator, typename _ExtractKey, typename _Equal,
1073 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1074 bool __chc, bool __cit, bool __uk>
1075 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1076 _H1, _H2, _Hash, _RehashPolicy,
1077 __chc, __cit, __uk>::iterator
1078 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1079 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1080 erase(iterator __it)
1082 iterator __result = __it;
1084 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1088 template<typename _Key, typename _Value,
1089 typename _Allocator, typename _ExtractKey, typename _Equal,
1090 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1091 bool __chc, bool __cit, bool __uk>
1092 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1093 _H1, _H2, _Hash, _RehashPolicy,
1094 __chc, __cit, __uk>::const_iterator
1095 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1096 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1097 erase(const_iterator __it)
1099 const_iterator __result = __it;
1101 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1105 template<typename _Key, typename _Value,
1106 typename _Allocator, typename _ExtractKey, typename _Equal,
1107 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1108 bool __chc, bool __cit, bool __uk>
1109 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1110 _H1, _H2, _Hash, _RehashPolicy,
1111 __chc, __cit, __uk>::size_type
1112 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1113 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1114 erase(const key_type& __k)
1116 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1117 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1118 size_type __result = 0;
1120 _Node** __slot = _M_buckets + __n;
1121 while (*__slot && !this->_M_compare(__k, __code, *__slot))
1122 __slot = &((*__slot)->_M_next);
1124 _Node** __saved_slot = 0;
1125 while (*__slot && this->_M_compare(__k, __code, *__slot))
1127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1128 // 526. Is it undefined if a function in the standard changes
1130 if (&this->_M_extract((*__slot)->_M_v) != &__k)
1132 _Node* __p = *__slot;
1133 *__slot = __p->_M_next;
1134 _M_deallocate_node(__p);
1140 __saved_slot = __slot;
1141 __slot = &((*__slot)->_M_next);
1147 _Node* __p = *__saved_slot;
1148 *__saved_slot = __p->_M_next;
1149 _M_deallocate_node(__p);
1157 // ??? This could be optimized by taking advantage of the bucket
1158 // structure, but it's not clear that it's worth doing. It probably
1159 // wouldn't even be an optimization unless the load factor is large.
1160 template<typename _Key, typename _Value,
1161 typename _Allocator, typename _ExtractKey, typename _Equal,
1162 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1163 bool __chc, bool __cit, bool __uk>
1164 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1165 _H1, _H2, _Hash, _RehashPolicy,
1166 __chc, __cit, __uk>::iterator
1167 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1168 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1169 erase(iterator __first, iterator __last)
1171 while (__first != __last)
1172 __first = this->erase(__first);
1176 template<typename _Key, typename _Value,
1177 typename _Allocator, typename _ExtractKey, typename _Equal,
1178 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1179 bool __chc, bool __cit, bool __uk>
1180 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1181 _H1, _H2, _Hash, _RehashPolicy,
1182 __chc, __cit, __uk>::const_iterator
1183 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1184 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1185 erase(const_iterator __first, const_iterator __last)
1187 while (__first != __last)
1188 __first = this->erase(__first);
1192 template<typename _Key, typename _Value,
1193 typename _Allocator, typename _ExtractKey, typename _Equal,
1194 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1195 bool __chc, bool __cit, bool __uk>
1197 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1198 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1201 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1202 _M_element_count = 0;
1205 template<typename _Key, typename _Value,
1206 typename _Allocator, typename _ExtractKey, typename _Equal,
1207 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1208 bool __chc, bool __cit, bool __uk>
1210 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1211 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1212 rehash(size_type __n)
1214 _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1215 _M_rehash_policy._M_bkt_for_elements(_M_element_count
1219 template<typename _Key, typename _Value,
1220 typename _Allocator, typename _ExtractKey, typename _Equal,
1221 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1222 bool __chc, bool __cit, bool __uk>
1224 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1225 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1226 _M_rehash(size_type __n)
1228 _Node** __new_array = _M_allocate_buckets(__n);
1231 for (size_type __i = 0; __i < _M_bucket_count; ++__i)
1232 while (_Node* __p = _M_buckets[__i])
1234 std::size_t __new_index = this->_M_bucket_index(__p, __n);
1235 _M_buckets[__i] = __p->_M_next;
1236 __p->_M_next = __new_array[__new_index];
1237 __new_array[__new_index] = __p;
1239 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1240 _M_bucket_count = __n;
1241 _M_buckets = __new_array;
1245 // A failure here means that a hash function threw an exception.
1246 // We can't restore the previous state without calling the hash
1247 // function again, so the only sensible recovery is to delete
1249 _M_deallocate_nodes(__new_array, __n);
1250 _M_deallocate_buckets(__new_array, __n);
1251 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1252 _M_element_count = 0;
1253 __throw_exception_again;
1257 _GLIBCXX_END_NAMESPACE_TR1