1 // Internal header for TR1 unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2007 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 _Pair, typename _Hashtable>
172 friend struct __detail::_Map_base;
175 typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
176 typedef typename _Allocator::template rebind<_Node>::other
177 _Node_allocator_type;
178 typedef typename _Allocator::template rebind<_Node*>::other
179 _Bucket_allocator_type;
181 typedef typename _Allocator::template rebind<_Value>::other
182 _Value_allocator_type;
184 _Node_allocator_type _M_node_allocator;
186 size_type _M_bucket_count;
187 size_type _M_element_count;
188 _RehashPolicy _M_rehash_policy;
191 _M_allocate_node(const value_type& __v);
194 _M_deallocate_node(_Node* __n);
197 _M_deallocate_nodes(_Node**, size_type);
200 _M_allocate_buckets(size_type __n);
203 _M_deallocate_buckets(_Node**, size_type __n);
206 // Constructor, destructor, assignment, swap
207 _Hashtable(size_type __bucket_hint,
208 const _H1&, const _H2&, const _Hash&,
209 const _Equal&, const _ExtractKey&,
210 const allocator_type&);
212 template<typename _InputIterator>
213 _Hashtable(_InputIterator __first, _InputIterator __last,
214 size_type __bucket_hint,
215 const _H1&, const _H2&, const _Hash&,
216 const _Equal&, const _ExtractKey&,
217 const allocator_type&);
219 _Hashtable(const _Hashtable&);
221 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
222 _Hashtable(_Hashtable&&);
226 operator=(const _Hashtable&);
230 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
231 void swap(_Hashtable&&);
233 void swap(_Hashtable&);
236 // Basic container operations
240 iterator __i(_M_buckets);
241 if (!__i._M_cur_node)
242 __i._M_incr_bucket();
249 const_iterator __i(_M_buckets);
250 if (!__i._M_cur_node)
251 __i._M_incr_bucket();
257 { return iterator(_M_buckets + _M_bucket_count); }
261 { return const_iterator(_M_buckets + _M_bucket_count); }
263 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
267 const_iterator __i(_M_buckets);
268 if (!__i._M_cur_node)
269 __i._M_incr_bucket();
275 { return const_iterator(_M_buckets + _M_bucket_count); }
280 { return _M_element_count; }
284 { return size() == 0; }
287 get_allocator() const
288 { return allocator_type(_M_node_allocator); }
290 _Value_allocator_type
291 _M_get_Value_allocator() const
292 { return _Value_allocator_type(_M_node_allocator); }
296 { return _M_get_Value_allocator().max_size(); }
301 { return this->_M_eq; }
303 // hash_function, if present, comes from _Hash_code_base.
308 { return _M_bucket_count; }
311 max_bucket_count() const
312 { return max_size(); }
315 bucket_size(size_type __n) const
316 { return std::distance(begin(__n), end(__n)); }
319 bucket(const key_type& __k) const
321 return this->_M_bucket_index(__k, this->_M_hash_code(__k),
327 { return local_iterator(_M_buckets[__n]); }
331 { return local_iterator(0); }
334 begin(size_type __n) const
335 { return const_local_iterator(_M_buckets[__n]); }
339 { return const_local_iterator(0); }
344 return static_cast<float>(size()) / static_cast<float>(bucket_count());
347 // max_load_factor, if present, comes from _Rehash_base.
349 // Generalization of max_load_factor. Extension, not found in TR1. Only
350 // useful if _RehashPolicy is something other than the default.
352 __rehash_policy() const
353 { return _M_rehash_policy; }
356 __rehash_policy(const _RehashPolicy&);
360 find(const key_type& __k);
363 find(const key_type& __k) const;
366 count(const key_type& __k) const;
368 std::pair<iterator, iterator>
369 equal_range(const key_type& __k);
371 std::pair<const_iterator, const_iterator>
372 equal_range(const key_type& __k) const;
374 private: // Find, insert and erase helper functions
375 // ??? This dispatching is a workaround for the fact that we don't
376 // have partial specialization of member templates; it would be
377 // better to just specialize insert on __unique_keys. There may be a
378 // cleaner workaround.
379 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
380 std::pair<iterator, bool>, iterator>::__type
383 typedef typename __gnu_cxx::__conditional_type<__unique_keys,
384 std::_Select1st<_Insert_Return_Type>,
385 std::_Identity<_Insert_Return_Type>
390 _M_find_node(_Node*, const key_type&,
391 typename _Hashtable::_Hash_code_type) const;
394 _M_insert_bucket(const value_type&, size_type,
395 typename _Hashtable::_Hash_code_type);
397 std::pair<iterator, bool>
398 _M_insert(const value_type&, std::_GLIBCXX_TR1 true_type);
401 _M_insert(const value_type&, std::_GLIBCXX_TR1 false_type);
404 _M_erase_node(_Node*, _Node**);
409 insert(const value_type& __v)
410 { return _M_insert(__v, std::_GLIBCXX_TR1 integral_constant<bool,
414 insert(iterator, const value_type& __v)
415 { return iterator(_Insert_Conv_Type()(this->insert(__v))); }
418 insert(const_iterator, const value_type& __v)
419 { return const_iterator(_Insert_Conv_Type()(this->insert(__v))); }
421 template<typename _InputIterator>
423 insert(_InputIterator __first, _InputIterator __last);
429 erase(const_iterator);
432 erase(const key_type&);
435 erase(iterator, iterator);
438 erase(const_iterator, const_iterator);
443 // Set number of buckets to be appropriate for container of n element.
444 void rehash(size_type __n);
447 // Unconditionally change size of bucket array to n.
448 void _M_rehash(size_type __n);
452 // Definitions of class template _Hashtable's out-of-line member functions.
453 template<typename _Key, typename _Value,
454 typename _Allocator, typename _ExtractKey, typename _Equal,
455 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
456 bool __chc, bool __cit, bool __uk>
457 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
458 _H1, _H2, _Hash, _RehashPolicy,
459 __chc, __cit, __uk>::_Node*
460 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
461 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
462 _M_allocate_node(const value_type& __v)
464 _Node* __n = _M_node_allocator.allocate(1);
467 _M_get_Value_allocator().construct(&__n->_M_v, __v);
473 _M_node_allocator.deallocate(__n, 1);
474 __throw_exception_again;
478 template<typename _Key, typename _Value,
479 typename _Allocator, typename _ExtractKey, typename _Equal,
480 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
481 bool __chc, bool __cit, bool __uk>
483 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
484 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
485 _M_deallocate_node(_Node* __n)
487 _M_get_Value_allocator().destroy(&__n->_M_v);
488 _M_node_allocator.deallocate(__n, 1);
491 template<typename _Key, typename _Value,
492 typename _Allocator, typename _ExtractKey, typename _Equal,
493 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
494 bool __chc, bool __cit, bool __uk>
496 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
497 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
498 _M_deallocate_nodes(_Node** __array, size_type __n)
500 for (size_type __i = 0; __i < __n; ++__i)
502 _Node* __p = __array[__i];
507 _M_deallocate_node(__tmp);
513 template<typename _Key, typename _Value,
514 typename _Allocator, typename _ExtractKey, typename _Equal,
515 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
516 bool __chc, bool __cit, bool __uk>
517 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
518 _H1, _H2, _Hash, _RehashPolicy,
519 __chc, __cit, __uk>::_Node**
520 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
521 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
522 _M_allocate_buckets(size_type __n)
524 _Bucket_allocator_type __alloc(_M_node_allocator);
526 // We allocate one extra bucket to hold a sentinel, an arbitrary
527 // non-null pointer. Iterator increment relies on this.
528 _Node** __p = __alloc.allocate(__n + 1);
529 std::fill(__p, __p + __n, (_Node*) 0);
530 __p[__n] = reinterpret_cast<_Node*>(0x1000);
534 template<typename _Key, typename _Value,
535 typename _Allocator, typename _ExtractKey, typename _Equal,
536 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
537 bool __chc, bool __cit, bool __uk>
539 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
540 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
541 _M_deallocate_buckets(_Node** __p, size_type __n)
543 _Bucket_allocator_type __alloc(_M_node_allocator);
544 __alloc.deallocate(__p, __n + 1);
547 template<typename _Key, typename _Value,
548 typename _Allocator, typename _ExtractKey, typename _Equal,
549 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
550 bool __chc, bool __cit, bool __uk>
551 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
552 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
553 _Hashtable(size_type __bucket_hint,
554 const _H1& __h1, const _H2& __h2, const _Hash& __h,
555 const _Equal& __eq, const _ExtractKey& __exk,
556 const allocator_type& __a)
557 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
558 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
559 _H1, _H2, _Hash, __chc>(__exk, __eq,
561 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
562 _M_node_allocator(__a),
567 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
568 _M_buckets = _M_allocate_buckets(_M_bucket_count);
571 template<typename _Key, typename _Value,
572 typename _Allocator, typename _ExtractKey, typename _Equal,
573 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
574 bool __chc, bool __cit, bool __uk>
575 template<typename _InputIterator>
576 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
577 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
578 _Hashtable(_InputIterator __f, _InputIterator __l,
579 size_type __bucket_hint,
580 const _H1& __h1, const _H2& __h2, const _Hash& __h,
581 const _Equal& __eq, const _ExtractKey& __exk,
582 const allocator_type& __a)
583 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
584 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
585 _H1, _H2, _Hash, __chc>(__exk, __eq,
587 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
588 _M_node_allocator(__a),
593 _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
595 _M_bkt_for_elements(__detail::
598 _M_buckets = _M_allocate_buckets(_M_bucket_count);
601 for (; __f != __l; ++__f)
607 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
608 __throw_exception_again;
612 template<typename _Key, typename _Value,
613 typename _Allocator, typename _ExtractKey, typename _Equal,
614 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
615 bool __chc, bool __cit, bool __uk>
616 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
617 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
618 _Hashtable(const _Hashtable& __ht)
619 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
620 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
621 _H1, _H2, _Hash, __chc>(__ht),
622 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
623 _M_node_allocator(__ht._M_node_allocator),
624 _M_bucket_count(__ht._M_bucket_count),
625 _M_element_count(__ht._M_element_count),
626 _M_rehash_policy(__ht._M_rehash_policy)
628 _M_buckets = _M_allocate_buckets(_M_bucket_count);
631 for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
633 _Node* __n = __ht._M_buckets[__i];
634 _Node** __tail = _M_buckets + __i;
637 *__tail = _M_allocate_node(__n->_M_v);
638 this->_M_copy_code(*__tail, __n);
639 __tail = &((*__tail)->_M_next);
647 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
648 __throw_exception_again;
652 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
653 template<typename _Key, typename _Value,
654 typename _Allocator, typename _ExtractKey, typename _Equal,
655 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
656 bool __chc, bool __cit, bool __uk>
657 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
658 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
659 _Hashtable(_Hashtable&& __ht)
660 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
661 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
662 _H1, _H2, _Hash, __chc>(__ht),
663 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
664 _M_node_allocator(__ht._M_node_allocator),
665 _M_bucket_count(__ht._M_bucket_count),
666 _M_element_count(__ht._M_element_count),
667 _M_rehash_policy(__ht._M_rehash_policy),
668 _M_buckets(__ht._M_buckets)
670 size_type __n_bkt = __ht._M_rehash_policy._M_next_bkt(0);
671 __ht._M_buckets = __ht._M_allocate_buckets(__n_bkt);
672 __ht._M_bucket_count = __n_bkt;
673 __ht._M_element_count = 0;
674 __ht._M_rehash_policy = _RehashPolicy();
678 template<typename _Key, typename _Value,
679 typename _Allocator, typename _ExtractKey, typename _Equal,
680 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
681 bool __chc, bool __cit, bool __uk>
682 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
683 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>&
684 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
685 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
686 operator=(const _Hashtable& __ht)
688 _Hashtable __tmp(__ht);
693 template<typename _Key, typename _Value,
694 typename _Allocator, typename _ExtractKey, typename _Equal,
695 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
696 bool __chc, bool __cit, bool __uk>
697 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
698 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
702 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
705 template<typename _Key, typename _Value,
706 typename _Allocator, typename _ExtractKey, typename _Equal,
707 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
708 bool __chc, bool __cit, bool __uk>
710 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
711 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
712 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
713 swap(_Hashtable&& __x)
715 swap(_Hashtable& __x)
718 // The only base class with member variables is hash_code_base. We
719 // define _Hash_code_base::_M_swap because different specializations
720 // have different members.
721 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
722 _H1, _H2, _Hash, __chc>::_M_swap(__x);
724 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725 // 431. Swapping containers with unequal allocators.
726 std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
727 __x._M_node_allocator);
729 std::swap(_M_rehash_policy, __x._M_rehash_policy);
730 std::swap(_M_buckets, __x._M_buckets);
731 std::swap(_M_bucket_count, __x._M_bucket_count);
732 std::swap(_M_element_count, __x._M_element_count);
735 template<typename _Key, typename _Value,
736 typename _Allocator, typename _ExtractKey, typename _Equal,
737 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
738 bool __chc, bool __cit, bool __uk>
740 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
741 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
742 __rehash_policy(const _RehashPolicy& __pol)
744 _M_rehash_policy = __pol;
745 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
746 if (__n_bkt > _M_bucket_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>
754 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
755 _H1, _H2, _Hash, _RehashPolicy,
756 __chc, __cit, __uk>::iterator
757 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
758 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
759 find(const key_type& __k)
761 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
762 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
763 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
764 return __p ? iterator(__p, _M_buckets + __n) : this->end();
767 template<typename _Key, typename _Value,
768 typename _Allocator, typename _ExtractKey, typename _Equal,
769 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
770 bool __chc, bool __cit, bool __uk>
771 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
772 _H1, _H2, _Hash, _RehashPolicy,
773 __chc, __cit, __uk>::const_iterator
774 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
775 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
776 find(const key_type& __k) const
778 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
779 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
780 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
781 return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
784 template<typename _Key, typename _Value,
785 typename _Allocator, typename _ExtractKey, typename _Equal,
786 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
787 bool __chc, bool __cit, bool __uk>
788 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
789 _H1, _H2, _Hash, _RehashPolicy,
790 __chc, __cit, __uk>::size_type
791 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
792 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
793 count(const key_type& __k) const
795 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
796 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
797 std::size_t __result = 0;
798 for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
799 if (this->_M_compare(__k, __code, __p))
804 template<typename _Key, typename _Value,
805 typename _Allocator, typename _ExtractKey, typename _Equal,
806 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
807 bool __chc, bool __cit, bool __uk>
808 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
809 _ExtractKey, _Equal, _H1,
810 _H2, _Hash, _RehashPolicy,
811 __chc, __cit, __uk>::iterator,
812 typename _Hashtable<_Key, _Value, _Allocator,
813 _ExtractKey, _Equal, _H1,
814 _H2, _Hash, _RehashPolicy,
815 __chc, __cit, __uk>::iterator>
816 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
817 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
818 equal_range(const key_type& __k)
820 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
821 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
822 _Node** __head = _M_buckets + __n;
823 _Node* __p = _M_find_node(*__head, __k, __code);
827 _Node* __p1 = __p->_M_next;
828 for (; __p1; __p1 = __p1->_M_next)
829 if (!this->_M_compare(__k, __code, __p1))
832 iterator __first(__p, __head);
833 iterator __last(__p1, __head);
835 __last._M_incr_bucket();
836 return std::make_pair(__first, __last);
839 return std::make_pair(this->end(), this->end());
842 template<typename _Key, typename _Value,
843 typename _Allocator, typename _ExtractKey, typename _Equal,
844 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
845 bool __chc, bool __cit, bool __uk>
846 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
847 _ExtractKey, _Equal, _H1,
848 _H2, _Hash, _RehashPolicy,
849 __chc, __cit, __uk>::const_iterator,
850 typename _Hashtable<_Key, _Value, _Allocator,
851 _ExtractKey, _Equal, _H1,
852 _H2, _Hash, _RehashPolicy,
853 __chc, __cit, __uk>::const_iterator>
854 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
855 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
856 equal_range(const key_type& __k) const
858 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
859 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
860 _Node** __head = _M_buckets + __n;
861 _Node* __p = _M_find_node(*__head, __k, __code);
865 _Node* __p1 = __p->_M_next;
866 for (; __p1; __p1 = __p1->_M_next)
867 if (!this->_M_compare(__k, __code, __p1))
870 const_iterator __first(__p, __head);
871 const_iterator __last(__p1, __head);
873 __last._M_incr_bucket();
874 return std::make_pair(__first, __last);
877 return std::make_pair(this->end(), this->end());
880 // Find the node whose key compares equal to k, beginning the search
881 // at p (usually the head of a bucket). Return nil if no node is found.
882 template<typename _Key, typename _Value,
883 typename _Allocator, typename _ExtractKey, typename _Equal,
884 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
885 bool __chc, bool __cit, bool __uk>
886 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
887 _Equal, _H1, _H2, _Hash, _RehashPolicy,
888 __chc, __cit, __uk>::_Node*
889 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
890 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
891 _M_find_node(_Node* __p, const key_type& __k,
892 typename _Hashtable::_Hash_code_type __code) const
894 for (; __p; __p = __p->_M_next)
895 if (this->_M_compare(__k, __code, __p))
900 // Insert v in bucket n (assumes no element with its key already present).
901 template<typename _Key, typename _Value,
902 typename _Allocator, typename _ExtractKey, typename _Equal,
903 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
904 bool __chc, bool __cit, bool __uk>
905 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
906 _H1, _H2, _Hash, _RehashPolicy,
907 __chc, __cit, __uk>::iterator
908 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
909 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
910 _M_insert_bucket(const value_type& __v, size_type __n,
911 typename _Hashtable::_Hash_code_type __code)
913 std::pair<bool, std::size_t> __do_rehash
914 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
915 _M_element_count, 1);
917 // Allocate the new node before doing the rehash so that we don't
918 // do a rehash if the allocation throws.
919 _Node* __new_node = _M_allocate_node(__v);
923 if (__do_rehash.first)
925 const key_type& __k = this->_M_extract(__v);
926 __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
927 _M_rehash(__do_rehash.second);
930 __new_node->_M_next = _M_buckets[__n];
931 this->_M_store_code(__new_node, __code);
932 _M_buckets[__n] = __new_node;
934 return iterator(__new_node, _M_buckets + __n);
938 _M_deallocate_node(__new_node);
939 __throw_exception_again;
943 // Insert v if no element with its key is already present.
944 template<typename _Key, typename _Value,
945 typename _Allocator, typename _ExtractKey, typename _Equal,
946 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
947 bool __chc, bool __cit, bool __uk>
948 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
949 _ExtractKey, _Equal, _H1,
950 _H2, _Hash, _RehashPolicy,
951 __chc, __cit, __uk>::iterator, bool>
952 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
953 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
954 _M_insert(const value_type& __v, std::_GLIBCXX_TR1 true_type)
956 const key_type& __k = this->_M_extract(__v);
957 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
958 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
960 if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
961 return std::make_pair(iterator(__p, _M_buckets + __n), false);
962 return std::make_pair(_M_insert_bucket(__v, __n, __code), true);
965 // Insert v unconditionally.
966 template<typename _Key, typename _Value,
967 typename _Allocator, typename _ExtractKey, typename _Equal,
968 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
969 bool __chc, bool __cit, bool __uk>
970 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
971 _H1, _H2, _Hash, _RehashPolicy,
972 __chc, __cit, __uk>::iterator
973 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
974 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
975 _M_insert(const value_type& __v, std::_GLIBCXX_TR1 false_type)
977 std::pair<bool, std::size_t> __do_rehash
978 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
979 _M_element_count, 1);
980 if (__do_rehash.first)
981 _M_rehash(__do_rehash.second);
983 const key_type& __k = this->_M_extract(__v);
984 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
985 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
987 // First find the node, avoid leaking new_node if compare throws.
988 _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
989 _Node* __new_node = _M_allocate_node(__v);
993 __new_node->_M_next = __prev->_M_next;
994 __prev->_M_next = __new_node;
998 __new_node->_M_next = _M_buckets[__n];
999 _M_buckets[__n] = __new_node;
1001 this->_M_store_code(__new_node, __code);
1004 return iterator(__new_node, _M_buckets + __n);
1007 // For erase(iterator) and erase(const_iterator).
1008 template<typename _Key, typename _Value,
1009 typename _Allocator, typename _ExtractKey, typename _Equal,
1010 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1011 bool __chc, bool __cit, bool __uk>
1013 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1014 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1015 _M_erase_node(_Node* __p, _Node** __b)
1017 _Node* __cur = *__b;
1019 *__b = __cur->_M_next;
1022 _Node* __next = __cur->_M_next;
1023 while (__next != __p)
1026 __next = __cur->_M_next;
1028 __cur->_M_next = __next->_M_next;
1031 _M_deallocate_node(__p);
1035 template<typename _Key, typename _Value,
1036 typename _Allocator, typename _ExtractKey, typename _Equal,
1037 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1038 bool __chc, bool __cit, bool __uk>
1039 template<typename _InputIterator>
1041 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1042 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1043 insert(_InputIterator __first, _InputIterator __last)
1045 size_type __n_elt = __detail::__distance_fw(__first, __last);
1046 std::pair<bool, std::size_t> __do_rehash
1047 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1048 _M_element_count, __n_elt);
1049 if (__do_rehash.first)
1050 _M_rehash(__do_rehash.second);
1052 for (; __first != __last; ++__first)
1053 this->insert(*__first);
1056 template<typename _Key, typename _Value,
1057 typename _Allocator, typename _ExtractKey, typename _Equal,
1058 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1059 bool __chc, bool __cit, bool __uk>
1060 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1061 _H1, _H2, _Hash, _RehashPolicy,
1062 __chc, __cit, __uk>::iterator
1063 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1064 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1065 erase(iterator __it)
1067 iterator __result = __it;
1069 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1073 template<typename _Key, typename _Value,
1074 typename _Allocator, typename _ExtractKey, typename _Equal,
1075 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1076 bool __chc, bool __cit, bool __uk>
1077 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1078 _H1, _H2, _Hash, _RehashPolicy,
1079 __chc, __cit, __uk>::const_iterator
1080 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1081 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1082 erase(const_iterator __it)
1084 const_iterator __result = __it;
1086 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1090 template<typename _Key, typename _Value,
1091 typename _Allocator, typename _ExtractKey, typename _Equal,
1092 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1093 bool __chc, bool __cit, bool __uk>
1094 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1095 _H1, _H2, _Hash, _RehashPolicy,
1096 __chc, __cit, __uk>::size_type
1097 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1098 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1099 erase(const key_type& __k)
1101 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1102 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1103 size_type __result = 0;
1105 _Node** __slot = _M_buckets + __n;
1106 while (*__slot && !this->_M_compare(__k, __code, *__slot))
1107 __slot = &((*__slot)->_M_next);
1109 _Node** __saved_slot = 0;
1110 while (*__slot && this->_M_compare(__k, __code, *__slot))
1112 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1113 // 526. Is it undefined if a function in the standard changes
1115 if (&this->_M_extract((*__slot)->_M_v) != &__k)
1117 _Node* __p = *__slot;
1118 *__slot = __p->_M_next;
1119 _M_deallocate_node(__p);
1125 __saved_slot = __slot;
1126 __slot = &((*__slot)->_M_next);
1132 _Node* __p = *__saved_slot;
1133 *__saved_slot = __p->_M_next;
1134 _M_deallocate_node(__p);
1142 // ??? This could be optimized by taking advantage of the bucket
1143 // structure, but it's not clear that it's worth doing. It probably
1144 // wouldn't even be an optimization unless the load factor is large.
1145 template<typename _Key, typename _Value,
1146 typename _Allocator, typename _ExtractKey, typename _Equal,
1147 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1148 bool __chc, bool __cit, bool __uk>
1149 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1150 _H1, _H2, _Hash, _RehashPolicy,
1151 __chc, __cit, __uk>::iterator
1152 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1153 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1154 erase(iterator __first, iterator __last)
1156 while (__first != __last)
1157 __first = this->erase(__first);
1161 template<typename _Key, typename _Value,
1162 typename _Allocator, typename _ExtractKey, typename _Equal,
1163 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1164 bool __chc, bool __cit, bool __uk>
1165 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1166 _H1, _H2, _Hash, _RehashPolicy,
1167 __chc, __cit, __uk>::const_iterator
1168 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1169 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1170 erase(const_iterator __first, const_iterator __last)
1172 while (__first != __last)
1173 __first = this->erase(__first);
1177 template<typename _Key, typename _Value,
1178 typename _Allocator, typename _ExtractKey, typename _Equal,
1179 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1180 bool __chc, bool __cit, bool __uk>
1182 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1183 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1186 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1187 _M_element_count = 0;
1190 template<typename _Key, typename _Value,
1191 typename _Allocator, typename _ExtractKey, typename _Equal,
1192 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1193 bool __chc, bool __cit, bool __uk>
1195 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1196 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1197 rehash(size_type __n)
1199 _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1200 _M_rehash_policy._M_bkt_for_elements(_M_element_count
1204 template<typename _Key, typename _Value,
1205 typename _Allocator, typename _ExtractKey, typename _Equal,
1206 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1207 bool __chc, bool __cit, bool __uk>
1209 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1210 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1211 _M_rehash(size_type __n)
1213 _Node** __new_array = _M_allocate_buckets(__n);
1216 for (size_type __i = 0; __i < _M_bucket_count; ++__i)
1217 while (_Node* __p = _M_buckets[__i])
1219 std::size_t __new_index = this->_M_bucket_index(__p, __n);
1220 _M_buckets[__i] = __p->_M_next;
1221 __p->_M_next = __new_array[__new_index];
1222 __new_array[__new_index] = __p;
1224 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1225 _M_bucket_count = __n;
1226 _M_buckets = __new_array;
1230 // A failure here means that a hash function threw an exception.
1231 // We can't restore the previous state without calling the hash
1232 // function again, so the only sensible recovery is to delete
1234 _M_deallocate_nodes(__new_array, __n);
1235 _M_deallocate_buckets(__new_array, __n);
1236 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1237 _M_element_count = 0;
1238 __throw_exception_again;
1242 _GLIBCXX_END_NAMESPACE_TR1