1 // hashtable.h header -*- C++ -*-
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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 * Do not attempt to use it directly. @headername{unordered_map, unordered_set}
31 #define _HASHTABLE_H 1
33 #pragma GCC system_header
35 #include <bits/hashtable_policy.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
41 // Class template _Hashtable, class definition.
43 // Meaning of class template _Hashtable's template parameters
45 // _Key and _Value: arbitrary CopyConstructible types.
47 // _Allocator: an allocator type ([lib.allocator.requirements]) whose
48 // value type is Value. As a conforming extension, we allow for
49 // value type != Value.
51 // _ExtractKey: function object that takes an object of type Value
52 // and returns a value of type _Key.
54 // _Equal: function object that takes two objects of type k and returns
55 // a bool-like value that is true if the two objects are considered equal.
57 // _H1: the hash function. A unary function object with argument type
58 // Key and result type size_t. Return values should be distributed
59 // over the entire range [0, numeric_limits<size_t>:::max()].
61 // _H2: the range-hashing function (in the terminology of Tavori and
62 // Dreizin). A binary function object whose argument types and result
63 // type are all size_t. Given arguments r and N, the return value is
64 // in the range [0, N).
66 // _Hash: the ranged hash function (Tavori and Dreizin). A binary function
67 // whose argument types are _Key and size_t and whose result type is
68 // size_t. Given arguments k and N, the return value is in the range
69 // [0, N). Default: hash(k, N) = h2(h1(k), N). If _Hash is anything other
70 // than the default, _H1 and _H2 are ignored.
72 // _RehashPolicy: Policy class with three members, all of which govern
73 // the bucket count. _M_next_bkt(n) returns a bucket count no smaller
74 // than n. _M_bkt_for_elements(n) returns a bucket count appropriate
75 // for an element count of n. _M_need_rehash(n_bkt, n_elt, n_ins)
76 // determines whether, if the current bucket count is n_bkt and the
77 // current element count is n_elt, we need to increase the bucket
78 // count. If so, returns make_pair(true, n), where n is the new
79 // bucket count. If not, returns make_pair(false, <anything>).
81 // __cache_hash_code: bool. true if we store the value of the hash
82 // function along with the value. This is a time-space tradeoff.
83 // Storing it may improve lookup speed by reducing the number of times
84 // we need to call the Equal function.
86 // __constant_iterators: bool. true if iterator and const_iterator are
87 // both constant iterator types. This is true for unordered_set and
88 // unordered_multiset, false for unordered_map and unordered_multimap.
90 // __unique_keys: bool. true if the return value of _Hashtable::count(k)
91 // is always at most one, false if it may be an arbitrary number. This
92 // true for unordered_set and unordered_map, false for unordered_multiset
93 // and unordered_multimap.
95 * Here's _Hashtable data structure, each _Hashtable has:
96 * - _Bucket[] _M_buckets
97 * - size_type _M_bucket_count
98 * - size_type _M_begin_bucket_index
99 * - size_type _M_element_count
101 * with _Bucket being _Node* and _Node:
104 * - size_t _M_code if cache_hash_code is true
106 * In terms of Standard containers the hastable is like the aggregation of:
107 * - std::forward_list<_Node> containing the elements
108 * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
110 * The first non-empty bucket with index _M_begin_bucket_index contains the
111 * first container node which is also the first bucket node whereas other
112 * non-empty buckets contain the node before the first bucket node. This is so
113 * to implement something like a std::forward_list::erase_after on container
116 * Access to the bucket last element require a check on the hash code to see
117 * if the node is still in the bucket. Such a design impose a quite efficient
118 * hash functor and is one of the reasons it is highly advise to set
119 * __cache_hash_code to true.
121 * The container iterators are simply built from nodes. This way incrementing
122 * the iterator is perfectly efficient no matter how many empty buckets there
123 * are in the container.
125 * On insert we compute element hash code and thanks to it find the bucket
126 * index. If the element is the first one in the bucket we must find the
127 * previous non-empty bucket where the previous node rely. To keep this loop
128 * minimal it is important that the number of bucket is not too high compared
129 * to the number of elements. So the hash policy must be carefully design so
130 * that it computes a bucket count large enough to respect the user defined
131 * load factor but also not too large to limit impact on the insert operation.
133 * On erase, the simple iterator design impose to use the hash functor to get
134 * the index of the bucket to update. For this reason, when __cache_hash_code
135 * is set to false, there is a static assertion that the hash functor cannot
138 * _M_begin_bucket_index is used to offer contant time access to the container
142 template<typename _Key, typename _Value, typename _Allocator,
143 typename _ExtractKey, typename _Equal,
144 typename _H1, typename _H2, typename _Hash,
145 typename _RehashPolicy,
146 bool __cache_hash_code,
147 bool __constant_iterators,
150 : public __detail::_Rehash_base<_RehashPolicy,
151 _Hashtable<_Key, _Value, _Allocator,
153 _Equal, _H1, _H2, _Hash,
156 __constant_iterators,
158 public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
159 _H1, _H2, _Hash, __cache_hash_code>,
160 public __detail::_Map_base<_Key, _Value, _ExtractKey, __unique_keys,
161 _Hashtable<_Key, _Value, _Allocator,
163 _Equal, _H1, _H2, _Hash,
166 __constant_iterators,
168 public __detail::_Equality_base<_ExtractKey, __unique_keys,
169 _Hashtable<_Key, _Value, _Allocator,
171 _Equal, _H1, _H2, _Hash,
174 __constant_iterators,
177 template<typename _Cond>
178 using __if_hash_code_cached
179 = __or_<__not_<integral_constant<bool, __cache_hash_code>>, _Cond>;
181 template<typename _Cond>
182 using __if_hash_code_not_cached
183 = __or_<integral_constant<bool, __cache_hash_code>, _Cond>;
185 static_assert(__if_hash_code_not_cached<__detail::__is_noexcept_hash<_Key,
187 "Cache the hash code or qualify your hash functor with noexcept");
189 // Following two static assertions are necessary to guarantee that
190 // swapping two hashtable instances won't invalidate associated local
193 // When hash codes are cached local iterator only uses H2 which must then
195 static_assert(__if_hash_code_cached<is_empty<_H2>>::value,
196 "Functor used to map hash code to bucket index must be empty");
198 typedef __detail::_Hash_code_base<_Key, _Value, _ExtractKey,
200 __cache_hash_code> _HCBase;
202 // When hash codes are not cached local iterator is going to use _HCBase
203 // above to compute node bucket index so it has to be empty.
204 static_assert(__if_hash_code_not_cached<is_empty<_HCBase>>::value,
205 "Cache the hash code or make functors involved in hash code"
206 " and bucket index computation empty");
209 typedef _Allocator allocator_type;
210 typedef _Value value_type;
211 typedef _Key key_type;
212 typedef _Equal key_equal;
213 // mapped_type, if present, comes from _Map_base.
214 // hasher, if present, comes from _Hash_code_base.
215 typedef typename _Allocator::pointer pointer;
216 typedef typename _Allocator::const_pointer const_pointer;
217 typedef typename _Allocator::reference reference;
218 typedef typename _Allocator::const_reference const_reference;
220 typedef std::size_t size_type;
221 typedef std::ptrdiff_t difference_type;
222 typedef __detail::_Local_iterator<key_type, value_type, _ExtractKey,
224 __constant_iterators,
227 typedef __detail::_Local_const_iterator<key_type, value_type, _ExtractKey,
229 __constant_iterators,
231 const_local_iterator;
232 typedef __detail::_Node_iterator<value_type, __constant_iterators,
235 typedef __detail::_Node_const_iterator<value_type,
236 __constant_iterators,
240 template<typename _Key2, typename _Value2, typename _Ex2, bool __unique2,
241 typename _Hashtable2>
242 friend struct __detail::_Map_base;
245 typedef typename _RehashPolicy::_State _RehashPolicyState;
246 typedef __detail::_Hash_node<_Value, __cache_hash_code> _Node;
247 typedef typename _Allocator::template rebind<_Node>::other
248 _Node_allocator_type;
249 typedef _Node* _Bucket;
250 typedef typename _Allocator::template rebind<_Bucket>::other
251 _Bucket_allocator_type;
253 typedef typename _Allocator::template rebind<_Value>::other
254 _Value_allocator_type;
256 _Node_allocator_type _M_node_allocator;
258 size_type _M_bucket_count;
259 size_type _M_begin_bucket_index; // First non-empty bucket.
260 size_type _M_element_count;
261 _RehashPolicy _M_rehash_policy;
263 template<typename... _Args>
265 _M_allocate_node(_Args&&... __args);
268 _M_deallocate_node(_Node* __n);
270 // Deallocate the linked list of nodes pointed to by __n
272 _M_deallocate_nodes(_Node* __n);
275 _M_allocate_buckets(size_type __n);
278 _M_deallocate_buckets(_Bucket*, size_type __n);
280 // Gets bucket begin dealing with the difference between first non-empty
281 // bucket containing the first container node and the other non-empty
282 // buckets containing the node before the one belonging to the bucket.
284 _M_bucket_begin(size_type __bkt) const;
286 // Gets the bucket last node if any
288 _M_bucket_end(size_type __bkt) const;
291 // Constructor, destructor, assignment, swap
292 _Hashtable(size_type __bucket_hint,
293 const _H1&, const _H2&, const _Hash&,
294 const _Equal&, const _ExtractKey&,
295 const allocator_type&);
297 template<typename _InputIterator>
298 _Hashtable(_InputIterator __first, _InputIterator __last,
299 size_type __bucket_hint,
300 const _H1&, const _H2&, const _Hash&,
301 const _Equal&, const _ExtractKey&,
302 const allocator_type&);
304 _Hashtable(const _Hashtable&);
306 _Hashtable(_Hashtable&&);
309 operator=(const _Hashtable& __ht)
311 _Hashtable __tmp(__ht);
317 operator=(_Hashtable&& __ht)
326 ~_Hashtable() noexcept;
328 void swap(_Hashtable&);
330 // Basic container operations
333 { return iterator(_M_buckets[_M_begin_bucket_index]); }
336 begin() const noexcept
337 { return const_iterator(_M_buckets[_M_begin_bucket_index]); }
341 { return iterator(nullptr); }
345 { return const_iterator(nullptr); }
348 cbegin() const noexcept
349 { return const_iterator(_M_buckets[_M_begin_bucket_index]); }
352 cend() const noexcept
353 { return const_iterator(nullptr); }
356 size() const noexcept
357 { return _M_element_count; }
360 empty() const noexcept
361 { return size() == 0; }
364 get_allocator() const noexcept
365 { return allocator_type(_M_node_allocator); }
368 max_size() const noexcept
369 { return _M_node_allocator.max_size(); }
374 { return this->_M_eq; }
376 // hash_function, if present, comes from _Hash_code_base.
380 bucket_count() const noexcept
381 { return _M_bucket_count; }
384 max_bucket_count() const noexcept
385 { return max_size(); }
388 bucket_size(size_type __n) const
389 { return std::distance(begin(__n), end(__n)); }
392 bucket(const key_type& __k) const
393 { return _M_bucket_index(__k, this->_M_hash_code(__k)); }
397 { return local_iterator(_M_bucket_begin(__n), __n,
402 { return local_iterator(nullptr, __n, _M_bucket_count); }
405 begin(size_type __n) const
406 { return const_local_iterator(_M_bucket_begin(__n), __n,
410 end(size_type __n) const
411 { return const_local_iterator(nullptr, __n, _M_bucket_count); }
415 cbegin(size_type __n) const
416 { return const_local_iterator(_M_bucket_begin(__n), __n,
420 cend(size_type __n) const
421 { return const_local_iterator(nullptr, __n, _M_bucket_count); }
424 load_factor() const noexcept
426 return static_cast<float>(size()) / static_cast<float>(bucket_count());
429 // max_load_factor, if present, comes from _Rehash_base.
431 // Generalization of max_load_factor. Extension, not found in TR1. Only
432 // useful if _RehashPolicy is something other than the default.
434 __rehash_policy() const
435 { return _M_rehash_policy; }
438 __rehash_policy(const _RehashPolicy&);
442 find(const key_type& __k);
445 find(const key_type& __k) const;
448 count(const key_type& __k) const;
450 std::pair<iterator, iterator>
451 equal_range(const key_type& __k);
453 std::pair<const_iterator, const_iterator>
454 equal_range(const key_type& __k) const;
458 _M_bucket_index(_Node* __n) const
459 { return _HCBase::_M_bucket_index(__n, _M_bucket_count); }
462 _M_bucket_index(const key_type& __k,
463 typename _Hashtable::_Hash_code_type __c) const
464 { return _HCBase::_M_bucket_index(__k, __c, _M_bucket_count); }
466 // Find and insert helper functions and types
468 _M_find_node(size_type, const key_type&,
469 typename _Hashtable::_Hash_code_type) const;
471 // Insert a node in an empty bucket
473 _M_insert_bucket_begin(size_type, _Node*);
475 // Insert a node after an other one in a non-empty bucket
477 _M_insert_after(size_type, _Node*, _Node*);
479 // Remove the bucket first node
481 _M_remove_bucket_begin(size_type __bkt, _Node* __next_n,
482 size_type __next_bkt);
484 // Get the node before __n in the bucket __bkt
486 _M_get_previous_node(size_type __bkt, _Node* __n);
488 template<typename _Arg>
490 _M_insert_bucket(_Arg&&, size_type,
491 typename _Hashtable::_Hash_code_type);
493 typedef typename std::conditional<__unique_keys,
494 std::pair<iterator, bool>,
498 typedef typename std::conditional<__unique_keys,
499 std::_Select1st<_Insert_Return_Type>,
500 std::_Identity<_Insert_Return_Type>
505 template<typename... _Args>
506 std::pair<iterator, bool>
507 _M_emplace(std::true_type, _Args&&... __args);
509 template<typename... _Args>
511 _M_emplace(std::false_type, _Args&&... __args);
513 template<typename _Arg>
514 std::pair<iterator, bool>
515 _M_insert(_Arg&&, std::true_type);
517 template<typename _Arg>
519 _M_insert(_Arg&&, std::false_type);
522 // Emplace, insert and erase
523 template<typename... _Args>
525 emplace(_Args&&... __args)
526 { return _M_emplace(integral_constant<bool, __unique_keys>(),
527 std::forward<_Args>(__args)...); }
529 template<typename... _Args>
531 emplace_hint(const_iterator, _Args&&... __args)
532 { return _Insert_Conv_Type()(emplace(std::forward<_Args>(__args)...)); }
535 insert(const value_type& __v)
536 { return _M_insert(__v, integral_constant<bool, __unique_keys>()); }
539 insert(const_iterator, const value_type& __v)
540 { return _Insert_Conv_Type()(insert(__v)); }
542 template<typename _Pair, typename = typename
543 std::enable_if<__and_<integral_constant<bool, !__constant_iterators>,
544 std::is_convertible<_Pair,
545 value_type>>::value>::type>
548 { return _M_insert(std::forward<_Pair>(__v),
549 integral_constant<bool, __unique_keys>()); }
551 template<typename _Pair, typename = typename
552 std::enable_if<__and_<integral_constant<bool, !__constant_iterators>,
553 std::is_convertible<_Pair,
554 value_type>>::value>::type>
556 insert(const_iterator, _Pair&& __v)
557 { return _Insert_Conv_Type()(insert(std::forward<_Pair>(__v))); }
559 template<typename _InputIterator>
561 insert(_InputIterator __first, _InputIterator __last);
564 insert(initializer_list<value_type> __l)
565 { this->insert(__l.begin(), __l.end()); }
568 erase(const_iterator);
573 { return erase(const_iterator(__it)); }
576 erase(const key_type&);
579 erase(const_iterator, const_iterator);
584 // Set number of buckets to be appropriate for container of n element.
585 void rehash(size_type __n);
588 // reserve, if present, comes from _Rehash_base.
591 // Unconditionally change size of bucket array to n, restore hash policy
592 // state to __state on exception.
593 void _M_rehash(size_type __n, const _RehashPolicyState& __state);
597 // Definitions of class template _Hashtable's out-of-line member functions.
598 template<typename _Key, typename _Value,
599 typename _Allocator, typename _ExtractKey, typename _Equal,
600 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
601 bool __chc, bool __cit, bool __uk>
602 template<typename... _Args>
603 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
604 _H1, _H2, _Hash, _RehashPolicy,
605 __chc, __cit, __uk>::_Node*
606 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
607 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
608 _M_allocate_node(_Args&&... __args)
610 _Node* __n = _M_node_allocator.allocate(1);
613 _M_node_allocator.construct(__n, std::forward<_Args>(__args)...);
618 _M_node_allocator.deallocate(__n, 1);
619 __throw_exception_again;
623 template<typename _Key, typename _Value,
624 typename _Allocator, typename _ExtractKey, typename _Equal,
625 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
626 bool __chc, bool __cit, bool __uk>
628 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
629 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
630 _M_deallocate_node(_Node* __n)
632 _M_node_allocator.destroy(__n);
633 _M_node_allocator.deallocate(__n, 1);
636 template<typename _Key, typename _Value,
637 typename _Allocator, typename _ExtractKey, typename _Equal,
638 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
639 bool __chc, bool __cit, bool __uk>
641 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
642 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
643 _M_deallocate_nodes(_Node* __n)
649 _M_deallocate_node(__tmp);
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 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
658 _H1, _H2, _Hash, _RehashPolicy,
659 __chc, __cit, __uk>::_Bucket*
660 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
661 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
662 _M_allocate_buckets(size_type __n)
664 _Bucket_allocator_type __alloc(_M_node_allocator);
666 // We allocate one extra bucket to have _M_begin_bucket_index
667 // point to it as long as container is empty
668 _Bucket* __p = __alloc.allocate(__n + 1);
669 __builtin_memset(__p, 0, (__n + 1) * sizeof(_Bucket));
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>
678 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
679 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
680 _M_deallocate_buckets(_Bucket* __p, size_type __n)
682 _Bucket_allocator_type __alloc(_M_node_allocator);
683 __alloc.deallocate(__p, __n + 1);
686 template<typename _Key, typename _Value,
687 typename _Allocator, typename _ExtractKey, typename _Equal,
688 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
689 bool __chc, bool __cit, bool __uk>
690 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
691 _Equal, _H1, _H2, _Hash, _RehashPolicy,
692 __chc, __cit, __uk>::_Node*
693 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
694 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
695 _M_bucket_begin(size_type __bkt) const
697 if (__bkt == _M_begin_bucket_index)
698 return _M_buckets[__bkt];
699 _Node* __n = _M_buckets[__bkt];
700 return __n ? __n->_M_next : nullptr;
703 template<typename _Key, typename _Value,
704 typename _Allocator, typename _ExtractKey, typename _Equal,
705 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
706 bool __chc, bool __cit, bool __uk>
707 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
708 _Equal, _H1, _H2, _Hash, _RehashPolicy,
709 __chc, __cit, __uk>::_Node*
710 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
711 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
712 _M_bucket_end(size_type __bkt) const
714 _Node* __n = _M_bucket_begin(__bkt);
716 for (;; __n = __n->_M_next)
717 if (!__n->_M_next || _M_bucket_index(__n->_M_next) != __bkt)
722 template<typename _Key, typename _Value,
723 typename _Allocator, typename _ExtractKey, typename _Equal,
724 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
725 bool __chc, bool __cit, bool __uk>
726 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
727 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
728 _Hashtable(size_type __bucket_hint,
729 const _H1& __h1, const _H2& __h2, const _Hash& __h,
730 const _Equal& __eq, const _ExtractKey& __exk,
731 const allocator_type& __a)
732 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
733 __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
734 _H1, _H2, _Hash, __chc>(__exk, __h1, __h2, __h,
736 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
737 _M_node_allocator(__a),
742 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
743 // We don't want the rehash policy to ask for the hashtable to shrink
744 // on the first insertion so we need to reset its previous resize level.
745 _M_rehash_policy._M_prev_resize = 0;
746 _M_buckets = _M_allocate_buckets(_M_bucket_count);
747 _M_begin_bucket_index = _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 template<typename _InputIterator>
755 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
756 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
757 _Hashtable(_InputIterator __f, _InputIterator __l,
758 size_type __bucket_hint,
759 const _H1& __h1, const _H2& __h2, const _Hash& __h,
760 const _Equal& __eq, const _ExtractKey& __exk,
761 const allocator_type& __a)
762 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(),
763 __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
764 _H1, _H2, _Hash, __chc>(__exk, __h1, __h2, __h,
766 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(),
767 _M_node_allocator(__a),
772 _M_bucket_count = std::max(_M_rehash_policy._M_next_bkt(__bucket_hint),
774 _M_bkt_for_elements(__detail::
777 // We don't want the rehash policy to ask for the hashtable to shrink
778 // on the first insertion so we need to reset its previous resize
780 _M_rehash_policy._M_prev_resize = 0;
781 _M_buckets = _M_allocate_buckets(_M_bucket_count);
782 _M_begin_bucket_index = _M_bucket_count;
785 for (; __f != __l; ++__f)
791 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
792 __throw_exception_again;
796 template<typename _Key, typename _Value,
797 typename _Allocator, typename _ExtractKey, typename _Equal,
798 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
799 bool __chc, bool __cit, bool __uk>
800 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
801 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
802 _Hashtable(const _Hashtable& __ht)
803 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
804 __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
805 _H1, _H2, _Hash, __chc>(__ht),
806 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
807 _M_node_allocator(__ht._M_node_allocator),
808 _M_bucket_count(__ht._M_bucket_count),
809 _M_begin_bucket_index(__ht._M_begin_bucket_index),
810 _M_element_count(__ht._M_element_count),
811 _M_rehash_policy(__ht._M_rehash_policy)
813 _M_buckets = _M_allocate_buckets(_M_bucket_count);
816 const _Node* __ht_n = __ht._M_buckets[__ht._M_begin_bucket_index];
820 // Note that the copy constructor do not rely on hash code usage.
821 // First deal with the special first node that is directly store in
822 // the first non-empty bucket
823 _Node* __this_n = _M_allocate_node(__ht_n->_M_v);
824 this->_M_copy_code(__this_n, __ht_n);
825 _M_buckets[_M_begin_bucket_index] = __this_n;
826 __ht_n = __ht_n->_M_next;
827 // Second deal with following non-empty buckets containing previous
829 for (size_type __i = __ht._M_begin_bucket_index + 1;
830 __i != __ht._M_bucket_count; ++__i)
832 if (!__ht._M_buckets[__i])
835 for (; __ht_n != __ht._M_buckets[__i]->_M_next;
836 __ht_n = __ht_n->_M_next)
838 __this_n->_M_next = _M_allocate_node(__ht_n->_M_v);
839 this->_M_copy_code(__this_n->_M_next, __ht_n);
840 __this_n = __this_n->_M_next;
843 _M_buckets[__i] = __this_n;
845 // Last finalize copy of the nodes of the last non-empty bucket
846 for (; __ht_n; __ht_n = __ht_n->_M_next)
848 __this_n->_M_next = _M_allocate_node(__ht_n->_M_v);
849 this->_M_copy_code(__this_n->_M_next, __ht_n);
850 __this_n = __this_n->_M_next;
856 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
857 __throw_exception_again;
861 template<typename _Key, typename _Value,
862 typename _Allocator, typename _ExtractKey, typename _Equal,
863 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
864 bool __chc, bool __cit, bool __uk>
865 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
866 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
867 _Hashtable(_Hashtable&& __ht)
868 : __detail::_Rehash_base<_RehashPolicy, _Hashtable>(__ht),
869 __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
870 _H1, _H2, _Hash, __chc>(__ht),
871 __detail::_Map_base<_Key, _Value, _ExtractKey, __uk, _Hashtable>(__ht),
872 _M_node_allocator(std::move(__ht._M_node_allocator)),
873 _M_buckets(__ht._M_buckets),
874 _M_bucket_count(__ht._M_bucket_count),
875 _M_begin_bucket_index(__ht._M_begin_bucket_index),
876 _M_element_count(__ht._M_element_count),
877 _M_rehash_policy(__ht._M_rehash_policy)
879 __ht._M_rehash_policy = _RehashPolicy();
880 __ht._M_bucket_count = __ht._M_rehash_policy._M_next_bkt(0);
881 __ht._M_buckets = __ht._M_allocate_buckets(__ht._M_bucket_count);
882 __ht._M_begin_bucket_index = __ht._M_bucket_count;
883 __ht._M_element_count = 0;
886 template<typename _Key, typename _Value,
887 typename _Allocator, typename _ExtractKey, typename _Equal,
888 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
889 bool __chc, bool __cit, bool __uk>
890 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
891 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
892 ~_Hashtable() noexcept
895 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
898 template<typename _Key, typename _Value,
899 typename _Allocator, typename _ExtractKey, typename _Equal,
900 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
901 bool __chc, bool __cit, bool __uk>
903 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
904 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
905 swap(_Hashtable& __x)
907 // The only base class with member variables is hash_code_base. We
908 // define _Hash_code_base::_M_swap because different specializations
909 // have different members.
912 // _GLIBCXX_RESOLVE_LIB_DEFECTS
913 // 431. Swapping containers with unequal allocators.
914 std::__alloc_swap<_Node_allocator_type>::_S_do_it(_M_node_allocator,
915 __x._M_node_allocator);
917 std::swap(_M_rehash_policy, __x._M_rehash_policy);
918 std::swap(_M_buckets, __x._M_buckets);
919 std::swap(_M_bucket_count, __x._M_bucket_count);
920 std::swap(_M_begin_bucket_index, __x._M_begin_bucket_index);
921 std::swap(_M_element_count, __x._M_element_count);
924 template<typename _Key, typename _Value,
925 typename _Allocator, typename _ExtractKey, typename _Equal,
926 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
927 bool __chc, bool __cit, bool __uk>
929 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
930 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
931 __rehash_policy(const _RehashPolicy& __pol)
933 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
934 if (__n_bkt != _M_bucket_count)
935 _M_rehash(__n_bkt, _M_rehash_policy._M_state());
936 _M_rehash_policy = __pol;
939 template<typename _Key, typename _Value,
940 typename _Allocator, typename _ExtractKey, typename _Equal,
941 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
942 bool __chc, bool __cit, bool __uk>
943 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
944 _H1, _H2, _Hash, _RehashPolicy,
945 __chc, __cit, __uk>::iterator
946 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
947 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
948 find(const key_type& __k)
950 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
951 std::size_t __n = _M_bucket_index(__k, __code);
952 _Node* __p = _M_find_node(__n, __k, __code);
953 return __p ? iterator(__p) : this->end();
956 template<typename _Key, typename _Value,
957 typename _Allocator, typename _ExtractKey, typename _Equal,
958 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
959 bool __chc, bool __cit, bool __uk>
960 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
961 _H1, _H2, _Hash, _RehashPolicy,
962 __chc, __cit, __uk>::const_iterator
963 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
964 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
965 find(const key_type& __k) const
967 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
968 std::size_t __n = _M_bucket_index(__k, __code);
969 _Node* __p = _M_find_node(__n, __k, __code);
970 return __p ? const_iterator(__p) : this->end();
973 template<typename _Key, typename _Value,
974 typename _Allocator, typename _ExtractKey, typename _Equal,
975 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
976 bool __chc, bool __cit, bool __uk>
977 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
978 _H1, _H2, _Hash, _RehashPolicy,
979 __chc, __cit, __uk>::size_type
980 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
981 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
982 count(const key_type& __k) const
984 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
985 std::size_t __n = _M_bucket_index(__k, __code);
986 _Node* __p = _M_bucket_begin(__n);
990 std::size_t __result = 0;
991 for (;; __p = __p->_M_next)
993 if (this->_M_equals(__k, __code, __p))
996 // All equivalent values are next to each other, if we found a not
997 // equivalent value after an equivalent one it means that we won't
998 // find anymore an equivalent value.
1000 if (!__p->_M_next || _M_bucket_index(__p->_M_next) != __n)
1006 template<typename _Key, typename _Value,
1007 typename _Allocator, typename _ExtractKey, typename _Equal,
1008 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1009 bool __chc, bool __cit, bool __uk>
1010 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
1011 _ExtractKey, _Equal, _H1,
1012 _H2, _Hash, _RehashPolicy,
1013 __chc, __cit, __uk>::iterator,
1014 typename _Hashtable<_Key, _Value, _Allocator,
1015 _ExtractKey, _Equal, _H1,
1016 _H2, _Hash, _RehashPolicy,
1017 __chc, __cit, __uk>::iterator>
1018 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1019 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1020 equal_range(const key_type& __k)
1022 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1023 std::size_t __n = _M_bucket_index(__k, __code);
1024 _Node* __p = _M_find_node(__n, __k, __code);
1028 _Node* __p1 = __p->_M_next;
1029 while (__p1 && _M_bucket_index(__p1) == __n
1030 && this->_M_equals(__k, __code, __p1))
1031 __p1 = __p1->_M_next;
1033 return std::make_pair(iterator(__p), iterator(__p1));
1036 return std::make_pair(this->end(), this->end());
1039 template<typename _Key, typename _Value,
1040 typename _Allocator, typename _ExtractKey, typename _Equal,
1041 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1042 bool __chc, bool __cit, bool __uk>
1043 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
1044 _ExtractKey, _Equal, _H1,
1045 _H2, _Hash, _RehashPolicy,
1046 __chc, __cit, __uk>::const_iterator,
1047 typename _Hashtable<_Key, _Value, _Allocator,
1048 _ExtractKey, _Equal, _H1,
1049 _H2, _Hash, _RehashPolicy,
1050 __chc, __cit, __uk>::const_iterator>
1051 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1052 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1053 equal_range(const key_type& __k) const
1055 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1056 std::size_t __n = _M_bucket_index(__k, __code);
1057 _Node* __p = _M_find_node(__n, __k, __code);
1061 _Node* __p1 = __p->_M_next;
1062 while (__p1 && _M_bucket_index(__p1) == __n
1063 && this->_M_equals(__k, __code, __p1))
1064 __p1 = __p1->_M_next;
1066 return std::make_pair(const_iterator(__p), const_iterator(__p1));
1069 return std::make_pair(this->end(), this->end());
1072 // Find the node whose key compares equal to k in the bucket n. Return nullptr
1073 // if no node is found.
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,
1079 _Equal, _H1, _H2, _Hash, _RehashPolicy,
1080 __chc, __cit, __uk>::_Node*
1081 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1082 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1083 _M_find_node(size_type __n, const key_type& __k,
1084 typename _Hashtable::_Hash_code_type __code) const
1086 _Node* __p = _M_bucket_begin(__n);
1089 for (;; __p = __p->_M_next)
1091 if (this->_M_equals(__k, __code, __p))
1093 if (!(__p->_M_next) || _M_bucket_index(__p->_M_next) != __n)
1099 template<typename _Key, typename _Value,
1100 typename _Allocator, typename _ExtractKey, typename _Equal,
1101 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1102 bool __chc, bool __cit, bool __uk>
1104 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1105 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1106 _M_insert_bucket_begin(size_type __bkt, _Node* __new_node)
1109 if (__bkt < _M_begin_bucket_index)
1111 if (_M_begin_bucket_index != _M_bucket_count)
1113 __new_node->_M_next = _M_buckets[_M_begin_bucket_index];
1114 _M_buckets[_M_begin_bucket_index] = __new_node;
1116 __prev_n = __new_node;
1117 _M_begin_bucket_index = __bkt;
1121 // We need to find previous non-empty bucket to link the new node.
1122 // There are several ways to find this previous bucket:
1123 // 1. Move backward until we find it (the current method)
1124 // 2. Start from the begin bucket index and move forward until we
1125 // cross __n position.
1126 // 3. Move forward until we find a non-empty bucket that will
1127 // contain the previous node.
1128 size_type __prev_bkt;
1129 for (__prev_bkt = __bkt; __prev_bkt-- != 0;)
1130 if (_M_buckets[__prev_bkt])
1132 __prev_n = _M_bucket_end(__prev_bkt);
1133 _M_insert_after(__prev_bkt, __prev_n, __new_node);
1135 _M_buckets[__bkt] = __prev_n;
1138 template<typename _Key, typename _Value,
1139 typename _Allocator, typename _ExtractKey, typename _Equal,
1140 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1141 bool __chc, bool __cit, bool __uk>
1143 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1144 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1145 _M_insert_after(size_type __bkt, _Node* __prev_n, _Node* __new_n)
1147 if (__prev_n->_M_next)
1149 size_type __next_bkt = _M_bucket_index(__prev_n->_M_next);
1150 if (__next_bkt != __bkt)
1151 _M_buckets[__next_bkt] = __new_n;
1153 __new_n->_M_next = __prev_n->_M_next;
1154 __prev_n->_M_next = __new_n;
1157 template<typename _Key, typename _Value,
1158 typename _Allocator, typename _ExtractKey, typename _Equal,
1159 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1160 bool __chc, bool __cit, bool __uk>
1162 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1163 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1164 _M_remove_bucket_begin(size_type __bkt, _Node* __next, size_type __next_bkt)
1166 if (!__next || __next_bkt != __bkt)
1168 // Bucket is now empty
1169 if (__next && __next_bkt != __bkt)
1170 // Update next non-empty bucket before begin node
1171 _M_buckets[__next_bkt] = _M_buckets[__bkt];
1172 _M_buckets[__bkt] = nullptr;
1173 if (__bkt == _M_begin_bucket_index)
1174 // We need to update begin bucket index
1177 _M_begin_bucket_index = __next_bkt;
1178 _M_buckets[_M_begin_bucket_index] = __next;
1181 _M_begin_bucket_index = _M_bucket_count;
1183 else if (__bkt == _M_begin_bucket_index)
1184 _M_buckets[__bkt] = __next;
1187 template<typename _Key, typename _Value,
1188 typename _Allocator, typename _ExtractKey, typename _Equal,
1189 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1190 bool __chc, bool __cit, bool __uk>
1191 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey,
1192 _Equal, _H1, _H2, _Hash, _RehashPolicy,
1193 __chc, __cit, __uk>::_Node*
1194 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1195 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1196 _M_get_previous_node(size_type __bkt, _Node* __n)
1198 _Node* __prev_n = nullptr;
1199 if (__bkt != _M_begin_bucket_index || __n != _M_buckets[__bkt])
1201 __prev_n = _M_buckets[__bkt];
1202 while (__prev_n->_M_next != __n)
1203 __prev_n = __prev_n->_M_next;
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>
1212 template<typename... _Args>
1213 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
1214 _ExtractKey, _Equal, _H1,
1215 _H2, _Hash, _RehashPolicy,
1216 __chc, __cit, __uk>::iterator, bool>
1217 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1218 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1219 _M_emplace(std::true_type, _Args&&... __args)
1221 // First build the node to get access to the hash code
1222 _Node* __new_node = _M_allocate_node(std::forward<_Args>(__args)...);
1225 const key_type& __k = this->_M_extract()(__new_node->_M_v);
1226 typename _Hashtable::_Hash_code_type __code
1227 = this->_M_hash_code(__k);
1228 size_type __bkt = _M_bucket_index(__k, __code);
1230 if (_Node* __p = _M_find_node(__bkt, __k, __code))
1232 // There is already an equivalent node, no insertion
1233 _M_deallocate_node(__new_node);
1234 return std::make_pair(iterator(__p), false);
1237 // We are going to insert this node
1238 this->_M_store_code(__new_node, __code);
1239 const _RehashPolicyState& __saved_state
1240 = _M_rehash_policy._M_state();
1241 std::pair<bool, std::size_t> __do_rehash
1242 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1243 _M_element_count, 1);
1245 if (__do_rehash.first)
1247 _M_rehash(__do_rehash.second, __saved_state);
1248 __bkt = _M_bucket_index(__k, __code);
1251 if (_M_buckets[__bkt])
1252 _M_insert_after(__bkt, _M_buckets[__bkt], __new_node);
1254 _M_insert_bucket_begin(__bkt, __new_node);
1256 return std::make_pair(iterator(__new_node), true);
1260 _M_deallocate_node(__new_node);
1261 __throw_exception_again;
1265 template<typename _Key, typename _Value,
1266 typename _Allocator, typename _ExtractKey, typename _Equal,
1267 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1268 bool __chc, bool __cit, bool __uk>
1269 template<typename... _Args>
1270 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1271 _H1, _H2, _Hash, _RehashPolicy,
1272 __chc, __cit, __uk>::iterator
1273 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1274 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1275 _M_emplace(std::false_type, _Args&&... __args)
1277 const _RehashPolicyState& __saved_state = _M_rehash_policy._M_state();
1278 std::pair<bool, std::size_t> __do_rehash
1279 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1280 _M_element_count, 1);
1282 // First build the node to get its hash code
1283 _Node* __new_node = _M_allocate_node(std::forward<_Args>(__args)...);
1286 const key_type& __k = this->_M_extract()(__new_node->_M_v);
1287 typename _Hashtable::_Hash_code_type __code
1288 = this->_M_hash_code(__k);
1289 this->_M_store_code(__new_node, __code);
1290 size_type __bkt = _M_bucket_index(__k, __code);
1292 // Second find the node, avoid rehash if compare throws.
1293 _Node* __prev = _M_find_node(__bkt, __k, __code);
1295 if (__do_rehash.first)
1297 _M_rehash(__do_rehash.second, __saved_state);
1298 __bkt = _M_bucket_index(__k, __code);
1299 // __prev is still valid because rehash do not invalidate nodes
1303 // Insert after the previous equivalent node
1304 _M_insert_after(__bkt, __prev, __new_node);
1305 else if (_M_buckets[__bkt])
1306 // Bucket is not empty and the inserted node has no equivalent in
1307 // the hashtable. We must insert the new node at the beginning or
1308 // end of the bucket to preserve equivalent elements relative
1310 if (__bkt != _M_begin_bucket_index)
1311 // We insert the new node at the beginning
1312 _M_insert_after(__bkt, _M_buckets[__bkt], __new_node);
1314 // We insert the new node at the end
1315 _M_insert_after(__bkt, _M_bucket_end(__bkt), __new_node);
1317 _M_insert_bucket_begin(__bkt, __new_node);
1319 return iterator(__new_node);
1323 _M_deallocate_node(__new_node);
1324 __throw_exception_again;
1328 // Insert v in bucket n (assumes no element with its key already present).
1329 template<typename _Key, typename _Value,
1330 typename _Allocator, typename _ExtractKey, typename _Equal,
1331 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1332 bool __chc, bool __cit, bool __uk>
1333 template<typename _Arg>
1334 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1335 _H1, _H2, _Hash, _RehashPolicy,
1336 __chc, __cit, __uk>::iterator
1337 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1338 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1339 _M_insert_bucket(_Arg&& __v, size_type __n,
1340 typename _Hashtable::_Hash_code_type __code)
1342 const _RehashPolicyState& __saved_state = _M_rehash_policy._M_state();
1343 std::pair<bool, std::size_t> __do_rehash
1344 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1345 _M_element_count, 1);
1347 if (__do_rehash.first)
1349 const key_type& __k = this->_M_extract()(__v);
1350 __n = _HCBase::_M_bucket_index(__k, __code, __do_rehash.second);
1353 _Node* __new_node = nullptr;
1356 // Allocate the new node before doing the rehash so that we
1357 // don't do a rehash if the allocation throws.
1358 __new_node = _M_allocate_node(std::forward<_Arg>(__v));
1359 this->_M_store_code(__new_node, __code);
1360 if (__do_rehash.first)
1361 _M_rehash(__do_rehash.second, __saved_state);
1363 if (_M_buckets[__n])
1364 _M_insert_after(__n, _M_buckets[__n], __new_node);
1366 _M_insert_bucket_begin(__n, __new_node);
1368 return iterator(__new_node);
1373 _M_rehash_policy._M_reset(__saved_state);
1375 _M_deallocate_node(__new_node);
1376 __throw_exception_again;
1380 // Insert v if no element with its key is already present.
1381 template<typename _Key, typename _Value,
1382 typename _Allocator, typename _ExtractKey, typename _Equal,
1383 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1384 bool __chc, bool __cit, bool __uk>
1385 template<typename _Arg>
1386 std::pair<typename _Hashtable<_Key, _Value, _Allocator,
1387 _ExtractKey, _Equal, _H1,
1388 _H2, _Hash, _RehashPolicy,
1389 __chc, __cit, __uk>::iterator, bool>
1390 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1391 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1392 _M_insert(_Arg&& __v, std::true_type)
1394 const key_type& __k = this->_M_extract()(__v);
1395 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1396 size_type __n = _M_bucket_index(__k, __code);
1398 if (_Node* __p = _M_find_node(__n, __k, __code))
1399 return std::make_pair(iterator(__p), false);
1400 return std::make_pair(_M_insert_bucket(std::forward<_Arg>(__v),
1401 __n, __code), true);
1404 // Insert v unconditionally.
1405 template<typename _Key, typename _Value,
1406 typename _Allocator, typename _ExtractKey, typename _Equal,
1407 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1408 bool __chc, bool __cit, bool __uk>
1409 template<typename _Arg>
1410 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1411 _H1, _H2, _Hash, _RehashPolicy,
1412 __chc, __cit, __uk>::iterator
1413 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1414 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1415 _M_insert(_Arg&& __v, std::false_type)
1417 const _RehashPolicyState& __saved_state = _M_rehash_policy._M_state();
1418 std::pair<bool, std::size_t> __do_rehash
1419 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1420 _M_element_count, 1);
1422 const key_type& __k = this->_M_extract()(__v);
1423 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1424 size_type __n = _M_bucket_index(__k, __code);
1426 // First find the node, avoid leaking new_node if compare throws.
1427 _Node* __prev = _M_find_node(__n, __k, __code);
1428 _Node* __new_node = nullptr;
1431 // Second allocate new node so that we don't rehash if it throws
1432 __new_node = _M_allocate_node(std::forward<_Arg>(__v));
1433 this->_M_store_code(__new_node, __code);
1434 if (__do_rehash.first)
1436 _M_rehash(__do_rehash.second, __saved_state);
1437 __n = _M_bucket_index(__k, __code);
1438 // __prev is still valid because rehash do not invalidate nodes
1442 // Insert after the previous equivalent node
1443 _M_insert_after(__n, __prev, __new_node);
1444 else if (_M_buckets[__n])
1445 // Bucket is not empty and the inserted node has no equivalent in
1446 // the hashtable. We must insert the new node at the beginning or
1447 // end of the bucket to preserve equivalent elements relative
1449 if (__n != _M_begin_bucket_index)
1450 // We insert the new node at the beginning
1451 _M_insert_after(__n, _M_buckets[__n], __new_node);
1453 // We insert the new node at the end
1454 _M_insert_after(__n, _M_bucket_end(__n), __new_node);
1456 _M_insert_bucket_begin(__n, __new_node);
1458 return iterator(__new_node);
1463 _M_rehash_policy._M_reset(__saved_state);
1465 _M_deallocate_node(__new_node);
1466 __throw_exception_again;
1470 template<typename _Key, typename _Value,
1471 typename _Allocator, typename _ExtractKey, typename _Equal,
1472 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1473 bool __chc, bool __cit, bool __uk>
1474 template<typename _InputIterator>
1476 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1477 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1478 insert(_InputIterator __first, _InputIterator __last)
1480 size_type __n_elt = __detail::__distance_fw(__first, __last);
1481 const _RehashPolicyState& __saved_state = _M_rehash_policy._M_state();
1482 std::pair<bool, std::size_t> __do_rehash
1483 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
1484 _M_element_count, __n_elt);
1485 if (__do_rehash.first)
1486 _M_rehash(__do_rehash.second, __saved_state);
1488 for (; __first != __last; ++__first)
1489 this->insert(*__first);
1492 template<typename _Key, typename _Value,
1493 typename _Allocator, typename _ExtractKey, typename _Equal,
1494 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1495 bool __chc, bool __cit, bool __uk>
1496 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1497 _H1, _H2, _Hash, _RehashPolicy,
1498 __chc, __cit, __uk>::iterator
1499 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1500 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1501 erase(const_iterator __it)
1503 _Node* __n = __it._M_cur;
1504 std::size_t __bkt = _M_bucket_index(__n);
1506 // Look for previous node to unlink it from the erased one, this is why
1507 // we need buckets to contain the before begin node of the bucket to make
1508 // this research fast.
1509 _Node* __prev_n = _M_get_previous_node(__bkt, __n);
1510 if (__n == _M_bucket_begin(__bkt))
1511 _M_remove_bucket_begin(__bkt, __n->_M_next,
1512 __n->_M_next ? _M_bucket_index(__n->_M_next) : 0);
1513 else if (__n->_M_next)
1515 size_type __next_bkt = _M_bucket_index(__n->_M_next);
1516 if (__next_bkt != __bkt)
1517 _M_buckets[__next_bkt] = __prev_n;
1521 __prev_n->_M_next = __n->_M_next;
1522 iterator __result(__n->_M_next);
1523 _M_deallocate_node(__n);
1529 template<typename _Key, typename _Value,
1530 typename _Allocator, typename _ExtractKey, typename _Equal,
1531 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1532 bool __chc, bool __cit, bool __uk>
1533 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1534 _H1, _H2, _Hash, _RehashPolicy,
1535 __chc, __cit, __uk>::size_type
1536 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1537 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1538 erase(const key_type& __k)
1540 typename _Hashtable::_Hash_code_type __code = this->_M_hash_code(__k);
1541 std::size_t __bkt = _M_bucket_index(__k, __code);
1542 // Look for the first matching node with its previous node at the same
1544 _Node* __n = _M_buckets[__bkt];
1547 _Node* __prev_n = nullptr;
1548 if (__bkt != _M_begin_bucket_index)
1553 bool __is_bucket_begin = true;
1554 for (;; __prev_n = __n, __n = __n->_M_next)
1556 if (this->_M_equals(__k, __code, __n))
1558 if (!(__n->_M_next) || _M_bucket_index(__n->_M_next) != __bkt)
1560 __is_bucket_begin = false;
1563 // We found a matching node, start deallocation loop from it
1564 std::size_t __next_bkt = __bkt;
1565 _Node* __next_n = __n;
1566 size_type __result = 0;
1567 _Node* __saved_n = nullptr;
1570 _Node* __p = __next_n;
1571 __next_n = __p->_M_next;
1572 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1573 // 526. Is it undefined if a function in the standard changes
1575 if (std::__addressof(this->_M_extract()(__p->_M_v))
1576 != std::__addressof(__k))
1577 _M_deallocate_node(__p);
1584 __next_bkt = _M_bucket_index(__next_n);
1586 while (__next_bkt == __bkt && this->_M_equals(__k, __code, __next_n));
1589 _M_deallocate_node(__saved_n);
1590 if (__is_bucket_begin)
1591 _M_remove_bucket_begin(__bkt, __next_n, __next_bkt);
1592 else if (__next_n && __next_bkt != __bkt)
1593 _M_buckets[__next_bkt] = __prev_n;
1595 __prev_n->_M_next = __next_n;
1599 template<typename _Key, typename _Value,
1600 typename _Allocator, typename _ExtractKey, typename _Equal,
1601 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1602 bool __chc, bool __cit, bool __uk>
1603 typename _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1604 _H1, _H2, _Hash, _RehashPolicy,
1605 __chc, __cit, __uk>::iterator
1606 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1607 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1608 erase(const_iterator __first, const_iterator __last)
1610 _Node* __n = __first._M_cur;
1611 _Node* __last_n = __last._M_cur;
1612 if (__n == __last_n)
1613 return iterator(__n);
1615 std::size_t __bkt = _M_bucket_index(__n);
1617 _Node* __prev_n = _M_get_previous_node(__bkt, __n);
1618 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
1619 std::size_t __n_bkt = __bkt;
1626 _M_deallocate_node(__tmp);
1630 __n_bkt = _M_bucket_index(__n);
1632 while (__n != __last_n && __n_bkt == __bkt);
1633 if (__is_bucket_begin)
1634 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
1635 if (__n == __last_n)
1637 __is_bucket_begin = true;
1641 if (__n && __n_bkt != __bkt)
1642 _M_buckets[__n_bkt] = __prev_n;
1644 __prev_n->_M_next = __n;
1645 return iterator(__n);
1648 template<typename _Key, typename _Value,
1649 typename _Allocator, typename _ExtractKey, typename _Equal,
1650 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1651 bool __chc, bool __cit, bool __uk>
1653 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1654 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1657 _M_deallocate_nodes(_M_buckets[_M_begin_bucket_index]);
1658 __builtin_memset(_M_buckets, 0, _M_bucket_count * sizeof(_Bucket));
1659 _M_element_count = 0;
1660 _M_begin_bucket_index = _M_bucket_count;
1663 template<typename _Key, typename _Value,
1664 typename _Allocator, typename _ExtractKey, typename _Equal,
1665 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1666 bool __chc, bool __cit, bool __uk>
1668 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1669 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1670 rehash(size_type __n)
1672 const _RehashPolicyState& __saved_state = _M_rehash_policy._M_state();
1673 _M_rehash(std::max(_M_rehash_policy._M_next_bkt(__n),
1674 _M_rehash_policy._M_bkt_for_elements(_M_element_count
1679 template<typename _Key, typename _Value,
1680 typename _Allocator, typename _ExtractKey, typename _Equal,
1681 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1682 bool __chc, bool __cit, bool __uk>
1684 _Hashtable<_Key, _Value, _Allocator, _ExtractKey, _Equal,
1685 _H1, _H2, _Hash, _RehashPolicy, __chc, __cit, __uk>::
1686 _M_rehash(size_type __n, const _RehashPolicyState& __state)
1690 _Bucket* __new_buckets = _M_allocate_buckets(__n);
1691 _Node* __p = _M_buckets[_M_begin_bucket_index];
1692 // First loop to store each node in its new bucket
1695 _Node* __next = __p->_M_next;
1696 std::size_t __new_index = _HCBase::_M_bucket_index(__p, __n);
1697 if (!__new_buckets[__new_index])
1698 // Store temporarily bucket end node in _M_buckets if possible.
1699 // This will boost second loop where we need to access bucket
1700 // end node quickly.
1701 if (__new_index < _M_bucket_count)
1702 _M_buckets[__new_index] = __p;
1703 __p->_M_next = __new_buckets[__new_index];
1704 __new_buckets[__new_index] = __p;
1707 _M_begin_bucket_index = __n;
1708 _Node* __prev_node = nullptr;
1709 // Second loop to link all nodes together and to fix bucket values so
1710 // that they contain the before begin node of the bucket.
1711 for (size_type __i = 0; __i != __n; ++__i)
1712 if (__new_buckets[__i])
1716 __prev_node->_M_next = __new_buckets[__i];
1717 __new_buckets[__i] = __prev_node;
1720 _M_begin_bucket_index = __i;
1721 if (__i < _M_bucket_count)
1722 __prev_node = _M_buckets[__i];
1725 __prev_node = __new_buckets[__i];
1726 while (__prev_node->_M_next)
1727 __prev_node = __prev_node->_M_next;
1730 _M_deallocate_buckets(_M_buckets, _M_bucket_count);
1731 _M_bucket_count = __n;
1732 _M_buckets = __new_buckets;
1736 // A failure here means that buckets allocation failed. We only
1737 // have to restore hash policy previous state.
1738 _M_rehash_policy._M_reset(__state);
1739 __throw_exception_again;
1743 _GLIBCXX_END_NAMESPACE_VERSION
1746 #endif // _HASHTABLE_H