1 // Internal header for TR1 unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2007, 2008 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file tr1_impl/hashtable
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
35 // This header file defines std::tr1::hashtable, which is used to
36 // implement std::tr1::unordered_set, std::tr1::unordered_map,
37 // std::tr1::unordered_multiset, and std::tr1::unordered_multimap.
38 // hashtable has many template parameters, partly to accommodate
39 // the differences between those four classes and partly to
40 // accommodate policy choices that go beyond TR1 specifications.
42 // Class template hashtable attempts to encapsulate all reasonable
43 // variation among hash tables that use chaining. It does not handle
47 // M. Austern, "A Proposal to Add Hash Tables to the Standard
48 // Library (revision 4)," WG21 Document N1456=03-0039, 2003.
49 // D. E. Knuth, The Art of Computer Programming, v. 3, Sorting and Searching.
50 // A. Tavori and V. Dreizin, "Policy-Based Data Structures", 2004.
51 // http://gcc.gnu.org/onlinedocs/libstdc++/ext/pb_ds/index.html
53 #include <tr1_impl/hashtable_policy.h>
57 _GLIBCXX_BEGIN_NAMESPACE_TR1
59 // Class template _Hashtable, class definition.
61 // Meaning of class template _Hashtable's template parameters
63 // _Key and _Value: arbitrary CopyConstructible types.
65 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
66 // value type is Value. As a conforming extension, we allow for
67 // value type != Value.
69 // _ExtractKey: function object that takes a object of type Value
70 // and returns a value of type _Key.
72 // _Equal: function object that takes two objects of type k and returns
73 // a bool-like value that is true if the two objects are considered equal.
75 // _H1: the hash function. A unary function object with argument type
76 // Key and result type size_t. Return values should be distributed
77 // over the entire range [0, numeric_limits<size_t>:::max()].
79 // _H2: the range-hashing function (in the terminology of Tavori and
80 // Dreizin). A binary function object whose argument types and result
81 // type are all size_t. Given arguments r and N, the return value is
82 // in the range [0, N).
84 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
85 // whose argument types are _Key and size_t and whose result type is
86 // size_t. Given arguments k and N, the return value is in the range
87 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
88 // than the default, _H1 and _H2 are ignored.
90 // _RehashPolicy: Policy class with three members, all of which govern
91 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
92 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
93 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
94 // determines whether, if the current bucket count is n_bkt and the
95 // current element count is n_elt, we need to increase the bucket
96 // count. If so, returns make_pair(true, n), where n is the new
97 // bucket count. If not, returns make_pair(false, <anything>).
99 // ??? Right now it is hard-wired that the number of buckets never
100 // shrinks. Should we allow _RehashPolicy to change that?
102 // __cache_hash_code: bool. true if we store the value of the hash
103 // function along with the value. This is a time-space tradeoff.
104 // Storing it may improve lookup speed by reducing the number of times
105 // we need to call the Equal function.
107 // __constant_iterators: bool. true if iterator and const_iterator are
108 // both constant iterator types. This is true for unordered_set and
109 // unordered_multiset, false for unordered_map and unordered_multimap.
111 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
112 // is always at most one, false if it may be an arbitrary number. This
113 // true for unordered_set and unordered_map, false for unordered_multiset
114 // and unordered_multimap.
116 template<typename _Key, typename _Value, typename _Allocator,
117 typename _ExtractKey, typename _Equal,
118 typename _H1, typename _H2, typename _Hash,
119 typename _RehashPolicy,
120 bool __cache_hash_code,
121 bool __constant_iterators,
124 : public __detail::_Rehash_base<_RehashPolicy,
125 _Hashtable<_Key, _Value, _Allocator,
127 _Equal, _H1, _H2, _Hash,
130 __constant_iterators,
132 public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
133 _H1, _H2, _Hash, __cache_hash_code>,
134 public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
135 _Hashtable<_Key, _Value, _Allocator,
137 _Equal, _H1, _H2, _Hash,
140 __constant_iterators,
144 typedef _Allocator allocator_type;
145 typedef _Value value_type;
146 typedef _Key key_type;
147 typedef _Equal key_equal;
148 // mapped_type, if present, comes from _Map_base.
149 // hasher, if present, comes from _Hash_code_base.
150 typedef typename _Allocator::difference_type difference_type;
151 typedef typename _Allocator::size_type size_type;
152 typedef typename _Allocator::reference reference;
153 typedef typename _Allocator::const_reference const_reference;
155 typedef __detail::_Node_iterator<value_type, __constant_iterators,
158 typedef __detail::_Node_const_iterator<value_type,
159 __constant_iterators,
161 const_local_iterator;
163 typedef __detail::_Hashtable_iterator<value_type, __constant_iterators,
166 typedef __detail::_Hashtable_const_iterator<value_type,
167 __constant_iterators,
171 template<typename _Key2, typename _Value2, typename _Ex2, bool __unique2,
172 typename _Hashtable2>
173 friend struct __detail::_Map_base;
176 typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
177 typedef typename _Allocator::template rebind<_Node>::other
178 _Node_allocator_type;
179 typedef typename _Allocator::template rebind<_Node*>::other
180 _Bucket_allocator_type;
182 typedef typename _Allocator::template rebind<_Value>::other
183 _Value_allocator_type;
185 _Node_allocator_type _M_node_allocator;
187 size_type _M_bucket_count;
188 size_type _M_element_count;
189 _RehashPolicy _M_rehash_policy;
192 _M_allocate_node(const value_type& __v);
195 _M_deallocate_node(_Node* __n);
198 _M_deallocate_nodes(_Node**, size_type);
201 _M_allocate_buckets(size_type __n);
204 _M_deallocate_buckets(_Node**, size_type __n);
207 // Constructor, destructor, assignment, swap
208 _Hashtable(size_type __bucket_hint,
209 const _H1&, const _H2&, const _Hash&,
210 const _Equal&, const _ExtractKey&,
211 const allocator_type&);
213 template<typename _InputIterator>
214 _Hashtable(_InputIterator __first, _InputIterator __last,
215 size_type __bucket_hint,
216 const _H1&, const _H2&, const _Hash&,
217 const _Equal&, const _ExtractKey&,
218 const allocator_type&);
220 _Hashtable(const _Hashtable&);
222 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
223 _Hashtable(_Hashtable&&);
227 operator=(const _Hashtable&);
231 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
232 void swap(_Hashtable&&);
234 void swap(_Hashtable&);
237 // Basic container operations
241 iterator __i(_M_buckets);
242 if (!__i._M_cur_node)
243 __i._M_incr_bucket();
250 const_iterator __i(_M_buckets);
251 if (!__i._M_cur_node)
252 __i._M_incr_bucket();
258 { return iterator(_M_buckets + _M_bucket_count); }
262 { return const_iterator(_M_buckets + _M_bucket_count); }
264 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
268 const_iterator __i(_M_buckets);
269 if (!__i._M_cur_node)
270 __i._M_incr_bucket();
276 { return const_iterator(_M_buckets + _M_bucket_count); }
281 { return _M_element_count; }
285 { return size() == 0; }
288 get_allocator() const
289 { return allocator_type(_M_node_allocator); }
291 _Value_allocator_type
292 _M_get_Value_allocator() const
293 { return _Value_allocator_type(_M_node_allocator); }
297 { return _M_get_Value_allocator().max_size(); }
302 { return this->_M_eq; }
304 // hash_function, if present, comes from _Hash_code_base.
309 { return _M_bucket_count; }
312 max_bucket_count() const
313 { return max_size(); }
316 bucket_size(size_type __n) const
317 { return std::distance(begin(__n), end(__n)); }
320 bucket(const key_type& __k) const
322 return this->_M_bucket_index(__k, this->_M_hash_code(__k),
328 { return local_iterator(_M_buckets[__n]); }
332 { return local_iterator(0); }
335 begin(size_type __n) const
336 { return const_local_iterator(_M_buckets[__n]); }
340 { return const_local_iterator(0); }
342 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
345 cbegin(size_type __n) const
346 { return const_local_iterator(_M_buckets[__n]); }
349 cend(size_type) const
350 { return const_local_iterator(0); }
356 return static_cast<float>(size()) / static_cast<float>(bucket_count());
359 // max_load_factor, if present, comes from _Rehash_base.
361 // Generalization of max_load_factor. Extension, not found in TR1. Only
362 // useful if _RehashPolicy is something other than the default.
364 __rehash_policy() const
365 { return _M_rehash_policy; }
368 __rehash_policy(const _RehashPolicy&);
372 find(const key_type& __k);
375 find(const key_type& __k) const;
378 count(const key_type& __k) const;
380 std::pair<iterator, iterator>
381 equal_range(const key_type& __k);
383 std::pair<const_iterator, const_iterator>
384 equal_range(const key_type& __k) const;
386 private: // Find, insert and erase helper functions
387 // ??? This dispatching is a workaround for the fact that we don't
388 // have partial specialization of member templates; it would be
389 // better to just specialize insert on __unique_keys. There may be a
390 // cleaner workaround.
391 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
392 std::pair<iterator, bool>, iterator>::__type
395 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
396 std::_Select1st<_Insert_Return_Type>,
397 std::_Identity<_Insert_Return_Type>
402 _M_find_node(_Node*, const key_type&,
403 typename _Hashtable::_Hash_code_type) const;
406 _M_insert_bucket(const value_type&, size_type,
407 typename _Hashtable::_Hash_code_type);
409 std::pair<iterator, bool>
410 _M_insert(const value_type&, std::_GLIBCXX_TR1 true_type);
413 _M_insert(const value_type&, std::_GLIBCXX_TR1 false_type);
416 _M_erase_node(_Node*, _Node**);
421 insert(const value_type& __v)
422 { return _M_insert(__v, std::_GLIBCXX_TR1 integral_constant<bool,
426 insert(iterator, const value_type& __v)
427 { return iterator(_Insert_Conv_Type()(this->insert(__v))); }
430 insert(const_iterator, const value_type& __v)
431 { return const_iterator(_Insert_Conv_Type()(this->insert(__v))); }
433 template<typename _InputIterator>
435 insert(_InputIterator __first, _InputIterator __last);
437 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
439 insert(initializer_list<value_type> __l)
440 { this->insert(__l.begin(), __l.end()); }
447 erase(const_iterator);
450 erase(const key_type&);
453 erase(iterator, iterator);
456 erase(const_iterator, const_iterator);
461 // Set number of buckets to be appropriate for container of n element.
462 void rehash(size_type __n);
465 // Unconditionally change size of bucket array to n.
466 void _M_rehash(size_type __n);
470 // Definitions of class template _Hashtable's out-of-line member functions.
471 template<typename _Key, typename _Value,
472 typename _Allocator, typename _ExtractKey, typename _Equal,
473 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
474 bool __chc, bool __cit, bool __uk>
475 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
476 _H1, _H2, _Hash, _RehashPolicy,
477 __chc, __cit, __uk>::_Node*
478 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
479 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
480 _M_allocate_node(const value_type& __v)
482 _Node* __n = _M_node_allocator.allocate(1);
485 _M_get_Value_allocator().construct(&__n->_M_v, __v);
491 _M_node_allocator.deallocate(__n, 1);
492 __throw_exception_again;
496 template<typename _Key, typename _Value,
497 typename _Allocator, typename _ExtractKey, typename _Equal,
498 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
499 bool __chc, bool __cit, bool __uk>
501 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
502 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
503 _M_deallocate_node(_Node* __n)
505 _M_get_Value_allocator().destroy(&__n->_M_v);
506 _M_node_allocator.deallocate(__n, 1);
509 template<typename _Key, typename _Value,
510 typename _Allocator, typename _ExtractKey, typename _Equal,
511 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
512 bool __chc, bool __cit, bool __uk>
514 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
515 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
516 _M_deallocate_nodes(_Node** __array, size_type __n)
518 for (size_type __i = 0; __i < __n; ++__i)
520 _Node* __p = __array[__i];
525 _M_deallocate_node(__tmp);
531 template<typename _Key, typename _Value,
532 typename _Allocator, typename _ExtractKey, typename _Equal,
533 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
534 bool __chc, bool __cit, bool __uk>
535 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
536 _H1, _H2, _Hash, _RehashPolicy,
537 __chc, __cit, __uk>::_Node**
538 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
539 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
540 _M_allocate_buckets(size_type __n)
542 _Bucket_allocator_type __alloc(_M_node_allocator);
544 // We allocate one extra bucket to hold a sentinel, an arbitrary
545 // non-null pointer. Iterator increment relies on this.
546 _Node** __p = __alloc.allocate(__n + 1);
547 std::fill(__p, __p + __n, (_Node*) 0);
548 __p[__n] = reinterpret_cast<_Node*>(0x1000);
552 template<typename _Key, typename _Value,
553 typename _Allocator, typename _ExtractKey, typename _Equal,
554 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
555 bool __chc, bool __cit, bool __uk>
557 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
558 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
559 _M_deallocate_buckets(_Node** __p, size_type __n)
561 _Bucket_allocator_type __alloc(_M_node_allocator);
562 __alloc.deallocate(__p, __n + 1);
565 template<typename _Key, typename _Value,
566 typename _Allocator, typename _ExtractKey, typename _Equal,
567 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
568 bool __chc, bool __cit, bool __uk>
569 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
570 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
571 _Hashtable(size_type __bucket_hint,
572 const _H1& __h1, const _H2& __h2, const _Hash& __h,
573 const _Equal& __eq, const _ExtractKey& __exk,
574 const allocator_type& __a)
575 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
576 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
577 _H1, _H2, _Hash, __chc>(__exk, __eq,
579 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
580 _M_node_allocator(__a),
585 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
586 _M_buckets = _M_allocate_buckets(_M_bucket_count);
589 template<typename _Key, typename _Value,
590 typename _Allocator, typename _ExtractKey, typename _Equal,
591 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
592 bool __chc, bool __cit, bool __uk>
593 template<typename _InputIterator>
594 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
595 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
596 _Hashtable(_InputIterator __f, _InputIterator __l,
597 size_type __bucket_hint,
598 const _H1& __h1, const _H2& __h2, const _Hash& __h,
599 const _Equal& __eq, const _ExtractKey& __exk,
600 const allocator_type& __a)
601 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
602 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
603 _H1, _H2, _Hash, __chc>(__exk, __eq,
605 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
606 _M_node_allocator(__a),
611 _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
613 _M_bkt_for_elements(__detail::
616 _M_buckets = _M_allocate_buckets(_M_bucket_count);
619 for (; __f != __l; ++__f)
625 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
626 __throw_exception_again;
630 template<typename _Key, typename _Value,
631 typename _Allocator, typename _ExtractKey, typename _Equal,
632 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
633 bool __chc, bool __cit, bool __uk>
634 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
635 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
636 _Hashtable(const _Hashtable& __ht)
637 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
638 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
639 _H1, _H2, _Hash, __chc>(__ht),
640 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
641 _M_node_allocator(__ht._M_node_allocator),
642 _M_bucket_count(__ht._M_bucket_count),
643 _M_element_count(__ht._M_element_count),
644 _M_rehash_policy(__ht._M_rehash_policy)
646 _M_buckets = _M_allocate_buckets(_M_bucket_count);
649 for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
651 _Node* __n = __ht._M_buckets[__i];
652 _Node** __tail = _M_buckets + __i;
655 *__tail = _M_allocate_node(__n->_M_v);
656 this->_M_copy_code(*__tail, __n);
657 __tail = &((*__tail)->_M_next);
665 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
666 __throw_exception_again;
670 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
671 template<typename _Key, typename _Value,
672 typename _Allocator, typename _ExtractKey, typename _Equal,
673 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
674 bool __chc, bool __cit, bool __uk>
675 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
676 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
677 _Hashtable(_Hashtable&& __ht)
678 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
679 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
680 _H1, _H2, _Hash, __chc>(__ht),
681 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
682 _M_node_allocator(__ht._M_node_allocator),
683 _M_bucket_count(__ht._M_bucket_count),
684 _M_element_count(__ht._M_element_count),
685 _M_rehash_policy(__ht._M_rehash_policy),
686 _M_buckets(__ht._M_buckets)
688 size_type __n_bkt = __ht._M_rehash_policy._M_next_bkt(0);
689 __ht._M_buckets = __ht._M_allocate_buckets(__n_bkt);
690 __ht._M_bucket_count = __n_bkt;
691 __ht._M_element_count = 0;
692 __ht._M_rehash_policy = _RehashPolicy();
696 template<typename _Key, typename _Value,
697 typename _Allocator, typename _ExtractKey, typename _Equal,
698 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
699 bool __chc, bool __cit, bool __uk>
700 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
701 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>&
702 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
703 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
704 operator=(const _Hashtable& __ht)
706 _Hashtable __tmp(__ht);
711 template<typename _Key, typename _Value,
712 typename _Allocator, typename _ExtractKey, typename _Equal,
713 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
714 bool __chc, bool __cit, bool __uk>
715 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
716 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
720 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
723 template<typename _Key, typename _Value,
724 typename _Allocator, typename _ExtractKey, typename _Equal,
725 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
726 bool __chc, bool __cit, bool __uk>
728 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
729 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
730 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
731 swap(_Hashtable&& __x)
733 swap(_Hashtable& __x)
736 // The only base class with member variables is hash_code_base. We
737 // define _Hash_code_base::_M_swap because different specializations
738 // have different members.
739 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
740 _H1, _H2, _Hash, __chc>::_M_swap(__x);
742 // _GLIBCXX_RESOLVE_LIB_DEFECTS
743 // 431. Swapping containers with unequal allocators.
744 std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
745 __x._M_node_allocator);
747 std::swap(_M_rehash_policy, __x._M_rehash_policy);
748 std::swap(_M_buckets, __x._M_buckets);
749 std::swap(_M_bucket_count, __x._M_bucket_count);
750 std::swap(_M_element_count, __x._M_element_count);
753 template<typename _Key, typename _Value,
754 typename _Allocator, typename _ExtractKey, typename _Equal,
755 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
756 bool __chc, bool __cit, bool __uk>
758 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
759 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
760 __rehash_policy(const _RehashPolicy& __pol)
762 _M_rehash_policy = __pol;
763 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
764 if (__n_bkt > _M_bucket_count)
768 template<typename _Key, typename _Value,
769 typename _Allocator, typename _ExtractKey, typename _Equal,
770 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
771 bool __chc, bool __cit, bool __uk>
772 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
773 _H1, _H2, _Hash, _RehashPolicy,
774 __chc, __cit, __uk>::iterator
775 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
776 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
777 find(const key_type& __k)
779 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
780 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
781 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
782 return __p ? iterator(__p, _M_buckets + __n) : this->end();
785 template<typename _Key, typename _Value,
786 typename _Allocator, typename _ExtractKey, typename _Equal,
787 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
788 bool __chc, bool __cit, bool __uk>
789 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
790 _H1, _H2, _Hash, _RehashPolicy,
791 __chc, __cit, __uk>::const_iterator
792 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
793 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
794 find(const key_type& __k) const
796 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
797 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
798 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
799 return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
802 template<typename _Key, typename _Value,
803 typename _Allocator, typename _ExtractKey, typename _Equal,
804 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
805 bool __chc, bool __cit, bool __uk>
806 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
807 _H1, _H2, _Hash, _RehashPolicy,
808 __chc, __cit, __uk>::size_type
809 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
810 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
811 count(const key_type& __k) const
813 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
814 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
815 std::size_t __result = 0;
816 for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
817 if (this->_M_compare(__k, __code, __p))
822 template<typename _Key, typename _Value,
823 typename _Allocator, typename _ExtractKey, typename _Equal,
824 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
825 bool __chc, bool __cit, bool __uk>
826 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
827 _ExtractKey, _Equal, _H1,
828 _H2, _Hash, _RehashPolicy,
829 __chc, __cit, __uk>::iterator,
830 typename _Hashtable<_Key, _Value, _Allocator,
831 _ExtractKey, _Equal, _H1,
832 _H2, _Hash, _RehashPolicy,
833 __chc, __cit, __uk>::iterator>
834 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
835 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
836 equal_range(const key_type& __k)
838 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
839 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
840 _Node** __head = _M_buckets + __n;
841 _Node* __p = _M_find_node(*__head, __k, __code);
845 _Node* __p1 = __p->_M_next;
846 for (; __p1; __p1 = __p1->_M_next)
847 if (!this->_M_compare(__k, __code, __p1))
850 iterator __first(__p, __head);
851 iterator __last(__p1, __head);
853 __last._M_incr_bucket();
854 return std::make_pair(__first, __last);
857 return std::make_pair(this->end(), this->end());
860 template<typename _Key, typename _Value,
861 typename _Allocator, typename _ExtractKey, typename _Equal,
862 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
863 bool __chc, bool __cit, bool __uk>
864 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
865 _ExtractKey, _Equal, _H1,
866 _H2, _Hash, _RehashPolicy,
867 __chc, __cit, __uk>::const_iterator,
868 typename _Hashtable<_Key, _Value, _Allocator,
869 _ExtractKey, _Equal, _H1,
870 _H2, _Hash, _RehashPolicy,
871 __chc, __cit, __uk>::const_iterator>
872 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
873 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
874 equal_range(const key_type& __k) const
876 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
877 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
878 _Node** __head = _M_buckets + __n;
879 _Node* __p = _M_find_node(*__head, __k, __code);
883 _Node* __p1 = __p->_M_next;
884 for (; __p1; __p1 = __p1->_M_next)
885 if (!this->_M_compare(__k, __code, __p1))
888 const_iterator __first(__p, __head);
889 const_iterator __last(__p1, __head);
891 __last._M_incr_bucket();
892 return std::make_pair(__first, __last);
895 return std::make_pair(this->end(), this->end());
898 // Find the node whose key compares equal to k, beginning the search
899 // at p (usually the head of a bucket). Return nil if no node is found.
900 template<typename _Key, typename _Value,
901 typename _Allocator, typename _ExtractKey, typename _Equal,
902 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
903 bool __chc, bool __cit, bool __uk>
904 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
905 _Equal, _H1, _H2, _Hash, _RehashPolicy,
906 __chc, __cit, __uk>::_Node*
907 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
908 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
909 _M_find_node(_Node* __p, const key_type& __k,
910 typename _Hashtable::_Hash_code_type __code) const
912 for (; __p; __p = __p->_M_next)
913 if (this->_M_compare(__k, __code, __p))
918 // Insert v in bucket n (assumes no element with its key already present).
919 template<typename _Key, typename _Value,
920 typename _Allocator, typename _ExtractKey, typename _Equal,
921 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
922 bool __chc, bool __cit, bool __uk>
923 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
924 _H1, _H2, _Hash, _RehashPolicy,
925 __chc, __cit, __uk>::iterator
926 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
927 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
928 _M_insert_bucket(const value_type& __v, size_type __n,
929 typename _Hashtable::_Hash_code_type __code)
931 std::pair<bool, std::size_t> __do_rehash
932 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
933 _M_element_count, 1);
935 // Allocate the new node before doing the rehash so that we don't
936 // do a rehash if the allocation throws.
937 _Node* __new_node = _M_allocate_node(__v);
941 if (__do_rehash.first)
943 const key_type& __k = this->_M_extract(__v);
944 __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
945 _M_rehash(__do_rehash.second);
948 __new_node->_M_next = _M_buckets[__n];
949 this->_M_store_code(__new_node, __code);
950 _M_buckets[__n] = __new_node;
952 return iterator(__new_node, _M_buckets + __n);
956 _M_deallocate_node(__new_node);
957 __throw_exception_again;
961 // Insert v if no element with its key is already present.
962 template<typename _Key, typename _Value,
963 typename _Allocator, typename _ExtractKey, typename _Equal,
964 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
965 bool __chc, bool __cit, bool __uk>
966 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
967 _ExtractKey, _Equal, _H1,
968 _H2, _Hash, _RehashPolicy,
969 __chc, __cit, __uk>::iterator, bool>
970 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
971 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
972 _M_insert(const value_type& __v, std::_GLIBCXX_TR1 true_type)
974 const key_type& __k = this->_M_extract(__v);
975 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
976 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
978 if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
979 return std::make_pair(iterator(__p, _M_buckets + __n), false);
980 return std::make_pair(_M_insert_bucket(__v, __n, __code), true);
983 // Insert v unconditionally.
984 template<typename _Key, typename _Value,
985 typename _Allocator, typename _ExtractKey, typename _Equal,
986 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
987 bool __chc, bool __cit, bool __uk>
988 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
989 _H1, _H2, _Hash, _RehashPolicy,
990 __chc, __cit, __uk>::iterator
991 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
992 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
993 _M_insert(const value_type& __v, std::_GLIBCXX_TR1 false_type)
995 std::pair<bool, std::size_t> __do_rehash
996 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
997 _M_element_count, 1);
998 if (__do_rehash.first)
999 _M_rehash(__do_rehash.second);
1001 const key_type& __k = this->_M_extract(__v);
1002 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1003 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1005 // First find the node, avoid leaking new_node if compare throws.
1006 _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
1007 _Node* __new_node = _M_allocate_node(__v);
1011 __new_node->_M_next = __prev->_M_next;
1012 __prev->_M_next = __new_node;
1016 __new_node->_M_next = _M_buckets[__n];
1017 _M_buckets[__n] = __new_node;
1019 this->_M_store_code(__new_node, __code);
1022 return iterator(__new_node, _M_buckets + __n);
1025 // For erase(iterator) and erase(const_iterator).
1026 template<typename _Key, typename _Value,
1027 typename _Allocator, typename _ExtractKey, typename _Equal,
1028 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1029 bool __chc, bool __cit, bool __uk>
1031 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1032 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1033 _M_erase_node(_Node* __p, _Node** __b)
1035 _Node* __cur = *__b;
1037 *__b = __cur->_M_next;
1040 _Node* __next = __cur->_M_next;
1041 while (__next != __p)
1044 __next = __cur->_M_next;
1046 __cur->_M_next = __next->_M_next;
1049 _M_deallocate_node(__p);
1053 template<typename _Key, typename _Value,
1054 typename _Allocator, typename _ExtractKey, typename _Equal,
1055 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1056 bool __chc, bool __cit, bool __uk>
1057 template<typename _InputIterator>
1059 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1060 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1061 insert(_InputIterator __first, _InputIterator __last)
1063 size_type __n_elt = __detail::__distance_fw(__first, __last);
1064 std::pair<bool, std::size_t> __do_rehash
1065 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1066 _M_element_count, __n_elt);
1067 if (__do_rehash.first)
1068 _M_rehash(__do_rehash.second);
1070 for (; __first != __last; ++__first)
1071 this->insert(*__first);
1074 template<typename _Key, typename _Value,
1075 typename _Allocator, typename _ExtractKey, typename _Equal,
1076 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1077 bool __chc, bool __cit, bool __uk>
1078 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1079 _H1, _H2, _Hash, _RehashPolicy,
1080 __chc, __cit, __uk>::iterator
1081 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1082 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1083 erase(iterator __it)
1085 iterator __result = __it;
1087 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1091 template<typename _Key, typename _Value,
1092 typename _Allocator, typename _ExtractKey, typename _Equal,
1093 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1094 bool __chc, bool __cit, bool __uk>
1095 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1096 _H1, _H2, _Hash, _RehashPolicy,
1097 __chc, __cit, __uk>::const_iterator
1098 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1099 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1100 erase(const_iterator __it)
1102 const_iterator __result = __it;
1104 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1108 template<typename _Key, typename _Value,
1109 typename _Allocator, typename _ExtractKey, typename _Equal,
1110 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1111 bool __chc, bool __cit, bool __uk>
1112 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1113 _H1, _H2, _Hash, _RehashPolicy,
1114 __chc, __cit, __uk>::size_type
1115 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1116 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1117 erase(const key_type& __k)
1119 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1120 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1121 size_type __result = 0;
1123 _Node** __slot = _M_buckets + __n;
1124 while (*__slot && !this->_M_compare(__k, __code, *__slot))
1125 __slot = &((*__slot)->_M_next);
1127 _Node** __saved_slot = 0;
1128 while (*__slot && this->_M_compare(__k, __code, *__slot))
1130 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1131 // 526. Is it undefined if a function in the standard changes
1133 if (&this->_M_extract((*__slot)->_M_v) != &__k)
1135 _Node* __p = *__slot;
1136 *__slot = __p->_M_next;
1137 _M_deallocate_node(__p);
1143 __saved_slot = __slot;
1144 __slot = &((*__slot)->_M_next);
1150 _Node* __p = *__saved_slot;
1151 *__saved_slot = __p->_M_next;
1152 _M_deallocate_node(__p);
1160 // ??? This could be optimized by taking advantage of the bucket
1161 // structure, but it's not clear that it's worth doing. It probably
1162 // wouldn't even be an optimization unless the load factor is large.
1163 template<typename _Key, typename _Value,
1164 typename _Allocator, typename _ExtractKey, typename _Equal,
1165 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1166 bool __chc, bool __cit, bool __uk>
1167 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1168 _H1, _H2, _Hash, _RehashPolicy,
1169 __chc, __cit, __uk>::iterator
1170 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1171 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1172 erase(iterator __first, iterator __last)
1174 while (__first != __last)
1175 __first = this->erase(__first);
1179 template<typename _Key, typename _Value,
1180 typename _Allocator, typename _ExtractKey, typename _Equal,
1181 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1182 bool __chc, bool __cit, bool __uk>
1183 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1184 _H1, _H2, _Hash, _RehashPolicy,
1185 __chc, __cit, __uk>::const_iterator
1186 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1187 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1188 erase(const_iterator __first, const_iterator __last)
1190 while (__first != __last)
1191 __first = this->erase(__first);
1195 template<typename _Key, typename _Value,
1196 typename _Allocator, typename _ExtractKey, typename _Equal,
1197 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1198 bool __chc, bool __cit, bool __uk>
1200 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1201 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1204 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1205 _M_element_count = 0;
1208 template<typename _Key, typename _Value,
1209 typename _Allocator, typename _ExtractKey, typename _Equal,
1210 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1211 bool __chc, bool __cit, bool __uk>
1213 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1214 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1215 rehash(size_type __n)
1217 _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1218 _M_rehash_policy._M_bkt_for_elements(_M_element_count
1222 template<typename _Key, typename _Value,
1223 typename _Allocator, typename _ExtractKey, typename _Equal,
1224 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1225 bool __chc, bool __cit, bool __uk>
1227 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1228 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1229 _M_rehash(size_type __n)
1231 _Node** __new_array = _M_allocate_buckets(__n);
1234 for (size_type __i = 0; __i < _M_bucket_count; ++__i)
1235 while (_Node* __p = _M_buckets[__i])
1237 std::size_t __new_index = this->_M_bucket_index(__p, __n);
1238 _M_buckets[__i] = __p->_M_next;
1239 __p->_M_next = __new_array[__new_index];
1240 __new_array[__new_index] = __p;
1242 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1243 _M_bucket_count = __n;
1244 _M_buckets = __new_array;
1248 // A failure here means that a hash function threw an exception.
1249 // We can't restore the previous state without calling the hash
1250 // function again, so the only sensible recovery is to delete
1252 _M_deallocate_nodes(__new_array, __n);
1253 _M_deallocate_buckets(__new_array, __n);
1254 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1255 _M_element_count = 0;
1256 __throw_exception_again;
1260 _GLIBCXX_END_NAMESPACE_TR1