1 // hashtable.h header -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010 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 bits/hashtable.h
26 * This is an internal header file, included by other library headers.
27 * You should not attempt to use it directly.
31 #define _HASHTABLE_H 1
33 #pragma GCC system_header
35 #include <bits/hashtable_policy.h>
39 // Class template _Hashtable, class definition.
41 // Meaning of class template _Hashtable's template parameters
43 // _Key and _Value: arbitrary CopyConstructible types.
45 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
46 // value type is Value. As a conforming extension, we allow for
47 // value type != Value.
49 // _ExtractKey: function object that takes a object of type Value
50 // and returns a value of type _Key.
52 // _Equal: function object that takes two objects of type k and returns
53 // a bool-like value that is true if the two objects are considered equal.
55 // _H1: the hash function. A unary function object with argument type
56 // Key and result type size_t. Return values should be distributed
57 // over the entire range [0, numeric_limits<size_t>:::max()].
59 // _H2: the range-hashing function (in the terminology of Tavori and
60 // Dreizin). A binary function object whose argument types and result
61 // type are all size_t. Given arguments r and N, the return value is
62 // in the range [0, N).
64 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
65 // whose argument types are _Key and size_t and whose result type is
66 // size_t. Given arguments k and N, the return value is in the range
67 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
68 // than the default, _H1 and _H2 are ignored.
70 // _RehashPolicy: Policy class with three members, all of which govern
71 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
72 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
73 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
74 // determines whether, if the current bucket count is n_bkt and the
75 // current element count is n_elt, we need to increase the bucket
76 // count. If so, returns make_pair(true, n), where n is the new
77 // bucket count. If not, returns make_pair(false, <anything>).
79 // ??? Right now it is hard-wired that the number of buckets never
80 // shrinks. Should we allow _RehashPolicy to change that?
82 // __cache_hash_code: bool. true if we store the value of the hash
83 // function along with the value. This is a time-space tradeoff.
84 // Storing it may improve lookup speed by reducing the number of times
85 // we need to call the Equal function.
87 // __constant_iterators: bool. true if iterator and const_iterator are
88 // both constant iterator types. This is true for unordered_set and
89 // unordered_multiset, false for unordered_map and unordered_multimap.
91 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
92 // is always at most one, false if it may be an arbitrary number. This
93 // true for unordered_set and unordered_map, false for unordered_multiset
94 // and unordered_multimap.
96 template<typename _Key, typename _Value, typename _Allocator,
97 typename _ExtractKey, typename _Equal,
98 typename _H1, typename _H2, typename _Hash,
99 typename _RehashPolicy,
100 bool __cache_hash_code,
101 bool __constant_iterators,
104 : public __detail::_Rehash_base<_RehashPolicy,
105 _Hashtable<_Key, _Value, _Allocator,
107 _Equal, _H1, _H2, _Hash,
110 __constant_iterators,
112 public __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
113 _H1, _H2, _Hash, __cache_hash_code>,
114 public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
115 _Hashtable<_Key, _Value, _Allocator,
117 _Equal, _H1, _H2, _Hash,
120 __constant_iterators,
124 typedef _Allocator allocator_type;
125 typedef _Value value_type;
126 typedef _Key key_type;
127 typedef _Equal key_equal;
128 // mapped_type, if present, comes from _Map_base.
129 // hasher, if present, comes from _Hash_code_base.
130 typedef typename _Allocator::pointer pointer;
131 typedef typename _Allocator::const_pointer const_pointer;
132 typedef typename _Allocator::reference reference;
133 typedef typename _Allocator::const_reference const_reference;
135 typedef std::size_t size_type;
136 typedef std::ptrdiff_t difference_type;
137 typedef __detail::_Node_iterator<value_type, __constant_iterators,
140 typedef __detail::_Node_const_iterator<value_type,
141 __constant_iterators,
143 const_local_iterator;
145 typedef __detail::_Hashtable_iterator<value_type, __constant_iterators,
148 typedef __detail::_Hashtable_const_iterator<value_type,
149 __constant_iterators,
153 template<typename _Key2, typename _Value2, typename _Ex2, bool __unique2,
154 typename _Hashtable2>
155 friend struct __detail::_Map_base;
158 typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
159 typedef typename _Allocator::template rebind<_Node>::other
160 _Node_allocator_type;
161 typedef typename _Allocator::template rebind<_Node*>::other
162 _Bucket_allocator_type;
164 typedef typename _Allocator::template rebind<_Value>::other
165 _Value_allocator_type;
167 _Node_allocator_type _M_node_allocator;
169 size_type _M_bucket_count;
170 size_type _M_element_count;
171 _RehashPolicy _M_rehash_policy;
174 _M_allocate_node(const value_type& __v);
177 _M_deallocate_node(_Node* __n);
180 _M_deallocate_nodes(_Node**, size_type);
183 _M_allocate_buckets(size_type __n);
186 _M_deallocate_buckets(_Node**, size_type __n);
189 // Constructor, destructor, assignment, swap
190 _Hashtable(size_type __bucket_hint,
191 const _H1&, const _H2&, const _Hash&,
192 const _Equal&, const _ExtractKey&,
193 const allocator_type&);
195 template<typename _InputIterator>
196 _Hashtable(_InputIterator __first, _InputIterator __last,
197 size_type __bucket_hint,
198 const _H1&, const _H2&, const _Hash&,
199 const _Equal&, const _ExtractKey&,
200 const allocator_type&);
202 _Hashtable(const _Hashtable&);
204 _Hashtable(_Hashtable&&);
207 operator=(const _Hashtable&);
211 void swap(_Hashtable&);
213 // Basic container operations
217 iterator __i(_M_buckets);
218 if (!__i._M_cur_node)
219 __i._M_incr_bucket();
226 const_iterator __i(_M_buckets);
227 if (!__i._M_cur_node)
228 __i._M_incr_bucket();
234 { return iterator(_M_buckets + _M_bucket_count); }
238 { return const_iterator(_M_buckets + _M_bucket_count); }
243 const_iterator __i(_M_buckets);
244 if (!__i._M_cur_node)
245 __i._M_incr_bucket();
251 { return const_iterator(_M_buckets + _M_bucket_count); }
255 { return _M_element_count; }
259 { return size() == 0; }
262 get_allocator() const
263 { return allocator_type(_M_node_allocator); }
265 _Value_allocator_type
266 _M_get_Value_allocator() const
267 { return _Value_allocator_type(_M_node_allocator); }
271 { return _M_node_allocator.max_size(); }
276 { return this->_M_eq; }
278 // hash_function, if present, comes from _Hash_code_base.
283 { return _M_bucket_count; }
286 max_bucket_count() const
287 { return max_size(); }
290 bucket_size(size_type __n) const
291 { return std::distance(begin(__n), end(__n)); }
294 bucket(const key_type& __k) const
296 return this->_M_bucket_index(__k, this->_M_hash_code(__k),
302 { return local_iterator(_M_buckets[__n]); }
306 { return local_iterator(0); }
309 begin(size_type __n) const
310 { return const_local_iterator(_M_buckets[__n]); }
314 { return const_local_iterator(0); }
318 cbegin(size_type __n) const
319 { return const_local_iterator(_M_buckets[__n]); }
322 cend(size_type) const
323 { return const_local_iterator(0); }
328 return static_cast<float>(size()) / static_cast<float>(bucket_count());
331 // max_load_factor, if present, comes from _Rehash_base.
333 // Generalization of max_load_factor. Extension, not found in TR1. Only
334 // useful if _RehashPolicy is something other than the default.
336 __rehash_policy() const
337 { return _M_rehash_policy; }
340 __rehash_policy(const _RehashPolicy&);
344 find(const key_type& __k);
347 find(const key_type& __k) const;
350 count(const key_type& __k) const;
352 std::pair<iterator, iterator>
353 equal_range(const key_type& __k);
355 std::pair<const_iterator, const_iterator>
356 equal_range(const key_type& __k) const;
358 private: // Find, insert and erase helper functions
359 // ??? This dispatching is a workaround for the fact that we don't
360 // have partial specialization of member templates; it would be
361 // better to just specialize insert on __unique_keys. There may be a
362 // cleaner workaround.
363 typedef typename std::conditional<__unique_keys,
364 std::pair<iterator, bool>,
368 typedef typename std::conditional<__unique_keys,
369 std::_Select1st<_Insert_Return_Type>,
370 std::_Identity<_Insert_Return_Type>
375 _M_find_node(_Node*, const key_type&,
376 typename _Hashtable::_Hash_code_type) const;
379 _M_insert_bucket(const value_type&, size_type,
380 typename _Hashtable::_Hash_code_type);
382 std::pair<iterator, bool>
383 _M_insert(const value_type&, std::true_type);
386 _M_insert(const value_type&, std::false_type);
389 _M_erase_node(_Node*, _Node**);
394 insert(const value_type& __v)
395 { return _M_insert(__v, std::integral_constant<bool,
399 insert(const_iterator, const value_type& __v)
400 { return iterator(_Insert_Conv_Type()(this->insert(__v))); }
402 template<typename _InputIterator>
404 insert(_InputIterator __first, _InputIterator __last);
407 insert(initializer_list<value_type> __l)
408 { this->insert(__l.begin(), __l.end()); }
411 erase(const_iterator);
414 erase(const key_type&);
417 erase(const_iterator, const_iterator);
422 // Set number of buckets to be appropriate for container of n element.
423 void rehash(size_type __n);
426 // reserve, if present, comes from _Rehash_base.
429 // Unconditionally change size of bucket array to n.
430 void _M_rehash(size_type __n);
434 // Definitions of class template _Hashtable's out-of-line member functions.
435 template<typename _Key, typename _Value,
436 typename _Allocator, typename _ExtractKey, typename _Equal,
437 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
438 bool __chc, bool __cit, bool __uk>
439 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
440 _H1, _H2, _Hash, _RehashPolicy,
441 __chc, __cit, __uk>::_Node*
442 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
443 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
444 _M_allocate_node(const value_type& __v)
446 _Node* __n = _M_node_allocator.allocate(1);
449 _M_node_allocator.construct(__n, __v);
455 _M_node_allocator.deallocate(__n, 1);
456 __throw_exception_again;
460 template<typename _Key, typename _Value,
461 typename _Allocator, typename _ExtractKey, typename _Equal,
462 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
463 bool __chc, bool __cit, bool __uk>
465 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
466 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
467 _M_deallocate_node(_Node* __n)
469 _M_node_allocator.destroy(__n);
470 _M_node_allocator.deallocate(__n, 1);
473 template<typename _Key, typename _Value,
474 typename _Allocator, typename _ExtractKey, typename _Equal,
475 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
476 bool __chc, bool __cit, bool __uk>
478 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
479 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
480 _M_deallocate_nodes(_Node** __array, size_type __n)
482 for (size_type __i = 0; __i < __n; ++__i)
484 _Node* __p = __array[__i];
489 _M_deallocate_node(__tmp);
495 template<typename _Key, typename _Value,
496 typename _Allocator, typename _ExtractKey, typename _Equal,
497 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
498 bool __chc, bool __cit, bool __uk>
499 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
500 _H1, _H2, _Hash, _RehashPolicy,
501 __chc, __cit, __uk>::_Node**
502 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
503 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
504 _M_allocate_buckets(size_type __n)
506 _Bucket_allocator_type __alloc(_M_node_allocator);
508 // We allocate one extra bucket to hold a sentinel, an arbitrary
509 // non-null pointer. Iterator increment relies on this.
510 _Node** __p = __alloc.allocate(__n + 1);
511 std::fill(__p, __p + __n, (_Node*) 0);
512 __p[__n] = reinterpret_cast<_Node*>(0x1000);
516 template<typename _Key, typename _Value,
517 typename _Allocator, typename _ExtractKey, typename _Equal,
518 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
519 bool __chc, bool __cit, bool __uk>
521 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
522 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
523 _M_deallocate_buckets(_Node** __p, size_type __n)
525 _Bucket_allocator_type __alloc(_M_node_allocator);
526 __alloc.deallocate(__p, __n + 1);
529 template<typename _Key, typename _Value,
530 typename _Allocator, typename _ExtractKey, typename _Equal,
531 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
532 bool __chc, bool __cit, bool __uk>
533 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
534 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
535 _Hashtable(size_type __bucket_hint,
536 const _H1& __h1, const _H2& __h2, const _Hash& __h,
537 const _Equal& __eq, const _ExtractKey& __exk,
538 const allocator_type& __a)
539 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
540 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
541 _H1, _H2, _Hash, __chc>(__exk, __eq,
543 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
544 _M_node_allocator(__a),
549 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
550 _M_buckets = _M_allocate_buckets(_M_bucket_count);
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>
557 template<typename _InputIterator>
558 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
559 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
560 _Hashtable(_InputIterator __f, _InputIterator __l,
561 size_type __bucket_hint,
562 const _H1& __h1, const _H2& __h2, const _Hash& __h,
563 const _Equal& __eq, const _ExtractKey& __exk,
564 const allocator_type& __a)
565 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
566 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
567 _H1, _H2, _Hash, __chc>(__exk, __eq,
569 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
570 _M_node_allocator(__a),
575 _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
577 _M_bkt_for_elements(__detail::
580 _M_buckets = _M_allocate_buckets(_M_bucket_count);
583 for (; __f != __l; ++__f)
589 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
590 __throw_exception_again;
594 template<typename _Key, typename _Value,
595 typename _Allocator, typename _ExtractKey, typename _Equal,
596 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
597 bool __chc, bool __cit, bool __uk>
598 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
599 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
600 _Hashtable(const _Hashtable& __ht)
601 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
602 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
603 _H1, _H2, _Hash, __chc>(__ht),
604 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
605 _M_node_allocator(__ht._M_node_allocator),
606 _M_bucket_count(__ht._M_bucket_count),
607 _M_element_count(__ht._M_element_count),
608 _M_rehash_policy(__ht._M_rehash_policy)
610 _M_buckets = _M_allocate_buckets(_M_bucket_count);
613 for (size_type __i = 0; __i < __ht._M_bucket_count; ++__i)
615 _Node* __n = __ht._M_buckets[__i];
616 _Node** __tail = _M_buckets + __i;
619 *__tail = _M_allocate_node(__n->_M_v);
620 this->_M_copy_code(*__tail, __n);
621 __tail = &((*__tail)->_M_next);
629 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
630 __throw_exception_again;
634 template<typename _Key, typename _Value,
635 typename _Allocator, typename _ExtractKey, typename _Equal,
636 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
637 bool __chc, bool __cit, bool __uk>
638 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
639 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
640 _Hashtable(_Hashtable&& __ht)
641 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
642 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
643 _H1, _H2, _Hash, __chc>(__ht),
644 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
645 _M_node_allocator(__ht._M_node_allocator),
646 _M_bucket_count(__ht._M_bucket_count),
647 _M_element_count(__ht._M_element_count),
648 _M_rehash_policy(__ht._M_rehash_policy),
649 _M_buckets(__ht._M_buckets)
651 size_type __n_bkt = __ht._M_rehash_policy._M_next_bkt(0);
652 __ht._M_buckets = __ht._M_allocate_buckets(__n_bkt);
653 __ht._M_bucket_count = __n_bkt;
654 __ht._M_element_count = 0;
655 __ht._M_rehash_policy = _RehashPolicy();
658 template<typename _Key, typename _Value,
659 typename _Allocator, typename _ExtractKey, typename _Equal,
660 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
661 bool __chc, bool __cit, bool __uk>
662 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
663 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>&
664 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
665 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
666 operator=(const _Hashtable& __ht)
668 _Hashtable __tmp(__ht);
673 template<typename _Key, typename _Value,
674 typename _Allocator, typename _ExtractKey, typename _Equal,
675 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
676 bool __chc, bool __cit, bool __uk>
677 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
678 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
682 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
685 template<typename _Key, typename _Value,
686 typename _Allocator, typename _ExtractKey, typename _Equal,
687 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
688 bool __chc, bool __cit, bool __uk>
690 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
691 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
692 swap(_Hashtable& __x)
694 // The only base class with member variables is hash_code_base. We
695 // define _Hash_code_base::_M_swap because different specializations
696 // have different members.
697 __detail::_Hash_code_base<_Key, _Value, _ExtractKey, _Equal,
698 _H1, _H2, _Hash, __chc>::_M_swap(__x);
700 // _GLIBCXX_RESOLVE_LIB_DEFECTS
701 // 431. Swapping containers with unequal allocators.
702 std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
703 __x._M_node_allocator);
705 std::swap(_M_rehash_policy, __x._M_rehash_policy);
706 std::swap(_M_buckets, __x._M_buckets);
707 std::swap(_M_bucket_count, __x._M_bucket_count);
708 std::swap(_M_element_count, __x._M_element_count);
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>
716 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
717 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
718 __rehash_policy(const _RehashPolicy& __pol)
720 _M_rehash_policy = __pol;
721 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
722 if (__n_bkt > _M_bucket_count)
726 template<typename _Key, typename _Value,
727 typename _Allocator, typename _ExtractKey, typename _Equal,
728 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
729 bool __chc, bool __cit, bool __uk>
730 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
731 _H1, _H2, _Hash, _RehashPolicy,
732 __chc, __cit, __uk>::iterator
733 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
734 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
735 find(const key_type& __k)
737 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
738 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
739 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
740 return __p ? iterator(__p, _M_buckets + __n) : this->end();
743 template<typename _Key, typename _Value,
744 typename _Allocator, typename _ExtractKey, typename _Equal,
745 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
746 bool __chc, bool __cit, bool __uk>
747 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
748 _H1, _H2, _Hash, _RehashPolicy,
749 __chc, __cit, __uk>::const_iterator
750 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
751 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
752 find(const key_type& __k) const
754 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
755 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
756 _Node* __p = _M_find_node(_M_buckets[__n], __k, __code);
757 return __p ? const_iterator(__p, _M_buckets + __n) : this->end();
760 template<typename _Key, typename _Value,
761 typename _Allocator, typename _ExtractKey, typename _Equal,
762 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
763 bool __chc, bool __cit, bool __uk>
764 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
765 _H1, _H2, _Hash, _RehashPolicy,
766 __chc, __cit, __uk>::size_type
767 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
768 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
769 count(const key_type& __k) const
771 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
772 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
773 std::size_t __result = 0;
774 for (_Node* __p = _M_buckets[__n]; __p; __p = __p->_M_next)
775 if (this->_M_compare(__k, __code, __p))
780 template<typename _Key, typename _Value,
781 typename _Allocator, typename _ExtractKey, typename _Equal,
782 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
783 bool __chc, bool __cit, bool __uk>
784 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
785 _ExtractKey, _Equal, _H1,
786 _H2, _Hash, _RehashPolicy,
787 __chc, __cit, __uk>::iterator,
788 typename _Hashtable<_Key, _Value, _Allocator,
789 _ExtractKey, _Equal, _H1,
790 _H2, _Hash, _RehashPolicy,
791 __chc, __cit, __uk>::iterator>
792 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
793 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
794 equal_range(const key_type& __k)
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** __head = _M_buckets + __n;
799 _Node* __p = _M_find_node(*__head, __k, __code);
803 _Node* __p1 = __p->_M_next;
804 for (; __p1; __p1 = __p1->_M_next)
805 if (!this->_M_compare(__k, __code, __p1))
808 iterator __first(__p, __head);
809 iterator __last(__p1, __head);
811 __last._M_incr_bucket();
812 return std::make_pair(__first, __last);
815 return std::make_pair(this->end(), this->end());
818 template<typename _Key, typename _Value,
819 typename _Allocator, typename _ExtractKey, typename _Equal,
820 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
821 bool __chc, bool __cit, bool __uk>
822 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
823 _ExtractKey, _Equal, _H1,
824 _H2, _Hash, _RehashPolicy,
825 __chc, __cit, __uk>::const_iterator,
826 typename _Hashtable<_Key, _Value, _Allocator,
827 _ExtractKey, _Equal, _H1,
828 _H2, _Hash, _RehashPolicy,
829 __chc, __cit, __uk>::const_iterator>
830 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
831 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
832 equal_range(const key_type& __k) const
834 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
835 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
836 _Node** __head = _M_buckets + __n;
837 _Node* __p = _M_find_node(*__head, __k, __code);
841 _Node* __p1 = __p->_M_next;
842 for (; __p1; __p1 = __p1->_M_next)
843 if (!this->_M_compare(__k, __code, __p1))
846 const_iterator __first(__p, __head);
847 const_iterator __last(__p1, __head);
849 __last._M_incr_bucket();
850 return std::make_pair(__first, __last);
853 return std::make_pair(this->end(), this->end());
856 // Find the node whose key compares equal to k, beginning the search
857 // at p (usually the head of a bucket). Return nil if no node is found.
858 template<typename _Key, typename _Value,
859 typename _Allocator, typename _ExtractKey, typename _Equal,
860 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
861 bool __chc, bool __cit, bool __uk>
862 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
863 _Equal, _H1, _H2, _Hash, _RehashPolicy,
864 __chc, __cit, __uk>::_Node*
865 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
866 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
867 _M_find_node(_Node* __p, const key_type& __k,
868 typename _Hashtable::_Hash_code_type __code) const
870 for (; __p; __p = __p->_M_next)
871 if (this->_M_compare(__k, __code, __p))
876 // Insert v in bucket n (assumes no element with its key already present).
877 template<typename _Key, typename _Value,
878 typename _Allocator, typename _ExtractKey, typename _Equal,
879 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
880 bool __chc, bool __cit, bool __uk>
881 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
882 _H1, _H2, _Hash, _RehashPolicy,
883 __chc, __cit, __uk>::iterator
884 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
885 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
886 _M_insert_bucket(const value_type& __v, size_type __n,
887 typename _Hashtable::_Hash_code_type __code)
889 std::pair<bool, std::size_t> __do_rehash
890 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
891 _M_element_count, 1);
893 // Allocate the new node before doing the rehash so that we don't
894 // do a rehash if the allocation throws.
895 _Node* __new_node = _M_allocate_node(__v);
899 if (__do_rehash.first)
901 const key_type& __k = this->_M_extract(__v);
902 __n = this->_M_bucket_index(__k, __code, __do_rehash.second);
903 _M_rehash(__do_rehash.second);
906 __new_node->_M_next = _M_buckets[__n];
907 this->_M_store_code(__new_node, __code);
908 _M_buckets[__n] = __new_node;
910 return iterator(__new_node, _M_buckets + __n);
914 _M_deallocate_node(__new_node);
915 __throw_exception_again;
919 // Insert v if no element with its key is already present.
920 template<typename _Key, typename _Value,
921 typename _Allocator, typename _ExtractKey, typename _Equal,
922 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
923 bool __chc, bool __cit, bool __uk>
924 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
925 _ExtractKey, _Equal, _H1,
926 _H2, _Hash, _RehashPolicy,
927 __chc, __cit, __uk>::iterator, bool>
928 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
929 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
930 _M_insert(const value_type& __v, std::true_type)
932 const key_type& __k = this->_M_extract(__v);
933 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
934 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
936 if (_Node* __p = _M_find_node(_M_buckets[__n], __k, __code))
937 return std::make_pair(iterator(__p, _M_buckets + __n), false);
938 return std::make_pair(_M_insert_bucket(__v, __n, __code), true);
941 // Insert v unconditionally.
942 template<typename _Key, typename _Value,
943 typename _Allocator, typename _ExtractKey, typename _Equal,
944 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
945 bool __chc, bool __cit, bool __uk>
946 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
947 _H1, _H2, _Hash, _RehashPolicy,
948 __chc, __cit, __uk>::iterator
949 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
950 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
951 _M_insert(const value_type& __v, std::false_type)
953 std::pair<bool, std::size_t> __do_rehash
954 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
955 _M_element_count, 1);
956 if (__do_rehash.first)
957 _M_rehash(__do_rehash.second);
959 const key_type& __k = this->_M_extract(__v);
960 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
961 size_type __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
963 // First find the node, avoid leaking new_node if compare throws.
964 _Node* __prev = _M_find_node(_M_buckets[__n], __k, __code);
965 _Node* __new_node = _M_allocate_node(__v);
969 __new_node->_M_next = __prev->_M_next;
970 __prev->_M_next = __new_node;
974 __new_node->_M_next = _M_buckets[__n];
975 _M_buckets[__n] = __new_node;
977 this->_M_store_code(__new_node, __code);
980 return iterator(__new_node, _M_buckets + __n);
983 // For erase(iterator) and erase(const_iterator).
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>
989 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
990 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
991 _M_erase_node(_Node* __p, _Node** __b)
995 *__b = __cur->_M_next;
998 _Node* __next = __cur->_M_next;
999 while (__next != __p)
1002 __next = __cur->_M_next;
1004 __cur->_M_next = __next->_M_next;
1007 _M_deallocate_node(__p);
1011 template<typename _Key, typename _Value,
1012 typename _Allocator, typename _ExtractKey, typename _Equal,
1013 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1014 bool __chc, bool __cit, bool __uk>
1015 template<typename _InputIterator>
1017 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1018 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1019 insert(_InputIterator __first, _InputIterator __last)
1021 size_type __n_elt = __detail::__distance_fw(__first, __last);
1022 std::pair<bool, std::size_t> __do_rehash
1023 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1024 _M_element_count, __n_elt);
1025 if (__do_rehash.first)
1026 _M_rehash(__do_rehash.second);
1028 for (; __first != __last; ++__first)
1029 this->insert(*__first);
1032 template<typename _Key, typename _Value,
1033 typename _Allocator, typename _ExtractKey, typename _Equal,
1034 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1035 bool __chc, bool __cit, bool __uk>
1036 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1037 _H1, _H2, _Hash, _RehashPolicy,
1038 __chc, __cit, __uk>::iterator
1039 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1040 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1041 erase(const_iterator __it)
1043 iterator __result(__it._M_cur_node, __it._M_cur_bucket);
1045 _M_erase_node(__it._M_cur_node, __it._M_cur_bucket);
1049 template<typename _Key, typename _Value,
1050 typename _Allocator, typename _ExtractKey, typename _Equal,
1051 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1052 bool __chc, bool __cit, bool __uk>
1053 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1054 _H1, _H2, _Hash, _RehashPolicy,
1055 __chc, __cit, __uk>::size_type
1056 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1057 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1058 erase(const key_type& __k)
1060 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1061 std::size_t __n = this->_M_bucket_index(__k, __code, _M_bucket_count);
1062 size_type __result = 0;
1064 _Node** __slot = _M_buckets + __n;
1065 while (*__slot && !this->_M_compare(__k, __code, *__slot))
1066 __slot = &((*__slot)->_M_next);
1068 _Node** __saved_slot = 0;
1069 while (*__slot && this->_M_compare(__k, __code, *__slot))
1071 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1072 // 526. Is it undefined if a function in the standard changes
1074 if (&this->_M_extract((*__slot)->_M_v) != &__k)
1076 _Node* __p = *__slot;
1077 *__slot = __p->_M_next;
1078 _M_deallocate_node(__p);
1084 __saved_slot = __slot;
1085 __slot = &((*__slot)->_M_next);
1091 _Node* __p = *__saved_slot;
1092 *__saved_slot = __p->_M_next;
1093 _M_deallocate_node(__p);
1101 // ??? This could be optimized by taking advantage of the bucket
1102 // structure, but it's not clear that it's worth doing. It probably
1103 // wouldn't even be an optimization unless the load factor is large.
1104 template<typename _Key, typename _Value,
1105 typename _Allocator, typename _ExtractKey, typename _Equal,
1106 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1107 bool __chc, bool __cit, bool __uk>
1108 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1109 _H1, _H2, _Hash, _RehashPolicy,
1110 __chc, __cit, __uk>::iterator
1111 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1112 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1113 erase(const_iterator __first, const_iterator __last)
1115 while (__first != __last)
1116 __first = this->erase(__first);
1117 return iterator(__last._M_cur_node, __last._M_cur_bucket);
1120 template<typename _Key, typename _Value,
1121 typename _Allocator, typename _ExtractKey, typename _Equal,
1122 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1123 bool __chc, bool __cit, bool __uk>
1125 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1126 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1129 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1130 _M_element_count = 0;
1133 template<typename _Key, typename _Value,
1134 typename _Allocator, typename _ExtractKey, typename _Equal,
1135 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1136 bool __chc, bool __cit, bool __uk>
1138 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1139 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1140 rehash(size_type __n)
1142 _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1143 _M_rehash_policy._M_bkt_for_elements(_M_element_count
1147 template<typename _Key, typename _Value,
1148 typename _Allocator, typename _ExtractKey, typename _Equal,
1149 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1150 bool __chc, bool __cit, bool __uk>
1152 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1153 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1154 _M_rehash(size_type __n)
1156 _Node** __new_array = _M_allocate_buckets(__n);
1159 for (size_type __i = 0; __i < _M_bucket_count; ++__i)
1160 while (_Node* __p = _M_buckets[__i])
1162 std::size_t __new_index = this->_M_bucket_index(__p, __n);
1163 _M_buckets[__i] = __p->_M_next;
1164 __p->_M_next = __new_array[__new_index];
1165 __new_array[__new_index] = __p;
1167 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1168 _M_bucket_count = __n;
1169 _M_buckets = __new_array;
1173 // A failure here means that a hash function threw an exception.
1174 // We can't restore the previous state without calling the hash
1175 // function again, so the only sensible recovery is to delete
1177 _M_deallocate_nodes(__new_array, __n);
1178 _M_deallocate_buckets(__new_array, __n);
1179 _M_deallocate_nodes(_M_buckets, _M_bucket_count);
1180 _M_element_count = 0;
1181 __throw_exception_again;
1186 #endif // _HASHTABLE_H