1 // hashtable.h header -*- C++ -*-
3 // Copyright (C) 2007-2013 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 template<typename _Tp
, typename _Hash
>
43 = __not_
<__and_
<// Do not cache for fast hasher.
44 __is_fast_hash
<_Hash
>,
45 // Mandatory to make local_iterator default
46 // constructible and assignable.
47 is_default_constructible
<_Hash
>,
48 is_copy_assignable
<_Hash
>,
49 // Mandatory to have erase not throwing.
50 __detail::__is_noexcept_hash
<_Tp
, _Hash
>>>;
53 * Primary class template _Hashtable.
55 * @ingroup hashtable-detail
57 * @tparam _Value CopyConstructible type.
59 * @tparam _Key CopyConstructible type.
61 * @tparam _Alloc An allocator type
62 * ([lib.allocator.requirements]) whose _Alloc::value_type is
63 * _Value. As a conforming extension, we allow for
64 * _Alloc::value_type != _Value.
66 * @tparam _ExtractKey Function object that takes an object of type
67 * _Value and returns a value of type _Key.
69 * @tparam _Equal Function object that takes two objects of type k
70 * and returns a bool-like value that is true if the two objects
71 * are considered equal.
73 * @tparam _H1 The hash function. A unary function object with
74 * argument type _Key and result type size_t. Return values should
75 * be distributed over the entire range [0, numeric_limits<size_t>:::max()].
77 * @tparam _H2 The range-hashing function (in the terminology of
78 * Tavori and Dreizin). A binary function object whose argument
79 * types and result type are all size_t. Given arguments r and N,
80 * the return value is in the range [0, N).
82 * @tparam _Hash The ranged hash function (Tavori and Dreizin). A
83 * binary function whose argument types are _Key and size_t and
84 * whose result type is size_t. Given arguments k and N, the
85 * return value is in the range [0, N). Default: hash(k, N) =
86 * h2(h1(k), N). If _Hash is anything other than the default, _H1
87 * and _H2 are ignored.
89 * @tparam _RehashPolicy Policy class with three members, all of
90 * which govern the bucket count. _M_next_bkt(n) returns a bucket
91 * count no smaller than n. _M_bkt_for_elements(n) returns a
92 * bucket count appropriate for an element count of n.
93 * _M_need_rehash(n_bkt, n_elt, n_ins) determines whether, if the
94 * current bucket count is n_bkt and the current element count is
95 * n_elt, we need to increase the bucket count. If so, returns
96 * make_pair(true, n), where n is the new bucket count. If not,
97 * returns make_pair(false, <anything>)
99 * @tparam _Traits Compile-time class with three boolean
100 * std::integral_constant members: __cache_hash_code, __constant_iterators,
103 * Each _Hashtable data structure has:
105 * - _Bucket[] _M_buckets
106 * - _Hash_node_base _M_bbegin
107 * - size_type _M_bucket_count
108 * - size_type _M_element_count
110 * with _Bucket being _Hash_node* and _Hash_node containing:
112 * - _Hash_node* _M_next
114 * - size_t _M_hash_code if cache_hash_code is true
116 * In terms of Standard containers the hashtable is like the aggregation of:
118 * - std::forward_list<_Node> containing the elements
119 * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
121 * The non-empty buckets contain the node before the first node in the
122 * bucket. This design makes it possible to implement something like a
123 * std::forward_list::insert_after on container insertion and
124 * std::forward_list::erase_after on container erase
125 * calls. _M_before_begin is equivalent to
126 * std::forward_list::before_begin. Empty buckets contain
127 * nullptr. Note that one of the non-empty buckets contains
128 * &_M_before_begin which is not a dereferenceable node so the
129 * node pointer in a bucket shall never be dereferenced, only its
132 * Walking through a bucket's nodes requires a check on the hash code to
133 * see if each node is still in the bucket. Such a design assumes a
134 * quite efficient hash functor and is one of the reasons it is
135 * highly advisable to set __cache_hash_code to true.
137 * The container iterators are simply built from nodes. This way
138 * incrementing the iterator is perfectly efficient independent of
139 * how many empty buckets there are in the container.
141 * On insert we compute the element's hash code and use it to find the
142 * bucket index. If the element must be inserted in an empty bucket
143 * we add it at the beginning of the singly linked list and make the
144 * bucket point to _M_before_begin. The bucket that used to point to
145 * _M_before_begin, if any, is updated to point to its new before
148 * On erase, the simple iterator design requires using the hash
149 * functor to get the index of the bucket to update. For this
150 * reason, when __cache_hash_code is set to false the hash functor must
151 * not throw and this is enforced by a static assertion.
153 * Functionality is implemented by decomposition into base classes,
154 * where the derived _Hashtable class is used in _Map_base,
155 * _Insert, _Rehash_base, and _Equality base classes to access the
156 * "this" pointer. _Hashtable_base is used in the base classes as a
157 * non-recursive, fully-completed-type so that detailed nested type
158 * information, such as iterator type and node type, can be
159 * used. This is similar to the "Curiously Recurring Template
160 * Pattern" (CRTP) technique, but uses a reconstructed, not
161 * explicitly passed, template pattern.
163 * Base class templates are:
164 * - __detail::_Hashtable_base
165 * - __detail::_Map_base
166 * - __detail::_Insert
167 * - __detail::_Rehash_base
168 * - __detail::_Equality
170 template<typename _Key
, typename _Value
, typename _Alloc
,
171 typename _ExtractKey
, typename _Equal
,
172 typename _H1
, typename _H2
, typename _Hash
,
173 typename _RehashPolicy
, typename _Traits
>
175 : public __detail::_Hashtable_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
176 _H1
, _H2
, _Hash
, _Traits
>,
177 public __detail::_Map_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
178 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>,
179 public __detail::_Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
180 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>,
181 public __detail::_Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
182 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>,
183 public __detail::_Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
184 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
186 typedef std::allocator_traits
<_Alloc
> _Alloc_traits
;
187 typedef typename
_Alloc_traits::template rebind_alloc
<_Value
>
189 typedef __gnu_cxx::__alloc_traits
<_Value_alloc_type
> _Value_alloc_traits
;
192 typedef _Key key_type
;
193 typedef _Value value_type
;
194 typedef _Alloc allocator_type
;
195 typedef _Equal key_equal
;
197 // mapped_type, if present, comes from _Map_base.
198 // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
199 typedef typename
_Value_alloc_traits::pointer pointer
;
200 typedef typename
_Value_alloc_traits::const_pointer const_pointer
;
201 typedef value_type
& reference
;
202 typedef const value_type
& const_reference
;
205 using __rehash_type
= _RehashPolicy
;
206 using __rehash_state
= typename
__rehash_type::_State
;
208 using __traits_type
= _Traits
;
209 using __hash_cached
= typename
__traits_type::__hash_cached
;
210 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
211 using __unique_keys
= typename
__traits_type::__unique_keys
;
213 using __key_extract
= typename
std::conditional
<
214 __constant_iterators::value
,
216 __detail::_Select1st
>::type
;
218 using __hashtable_base
= __detail::
219 _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
220 _Equal
, _H1
, _H2
, _Hash
, _Traits
>;
222 using __hash_code_base
= typename
__hashtable_base::__hash_code_base
;
223 using __hash_code
= typename
__hashtable_base::__hash_code
;
224 using __node_type
= typename
__hashtable_base::__node_type
;
225 using __node_base
= typename
__hashtable_base::__node_base
;
226 using __bucket_type
= typename
__hashtable_base::__bucket_type
;
227 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
228 using __iconv_type
= typename
__hashtable_base::__iconv_type
;
230 using __map_base
= __detail::_Map_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
231 _Equal
, _H1
, _H2
, _Hash
,
232 _RehashPolicy
, _Traits
>;
234 using __rehash_base
= __detail::_Rehash_base
<_Key
, _Value
, _Alloc
,
237 _RehashPolicy
, _Traits
>;
239 using __eq_base
= __detail::_Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
,
240 _Equal
, _H1
, _H2
, _Hash
,
241 _RehashPolicy
, _Traits
>;
243 // Metaprogramming for picking apart hash caching.
244 template<typename _Cond
>
245 using __if_hash_cached
= __or_
<__not_
<__hash_cached
>, _Cond
>;
247 template<typename _Cond
>
248 using __if_hash_not_cached
= __or_
<__hash_cached
, _Cond
>;
250 // Compile-time diagnostics.
252 // Getting a bucket index from a node shall not throw because it is used
253 // in methods (erase, swap...) that shall not throw.
254 static_assert(noexcept(declval
<const _Hashtable
&>()
255 ._M_bucket_index((const __node_type
*)nullptr,
257 "Cache the hash code or qualify your functors involved"
258 " in hash code and bucket index computation with noexcept");
260 // Following two static assertions are necessary to guarantee
261 // that local_iterator will be default constructible.
263 // When hash codes are cached local iterator inherits from H2 functor
264 // which must then be default constructible.
265 static_assert(__if_hash_cached
<is_default_constructible
<_H2
>>::value
,
266 "Functor used to map hash code to bucket index"
267 " must be default constructible");
269 // When hash codes are not cached local iterator inherits from
270 // __hash_code_base above to compute node bucket index so it has to be
271 // default constructible.
272 static_assert(__if_hash_not_cached
<
273 is_default_constructible
<
274 // We use _Hashtable_ebo_helper to access the protected
275 // default constructor.
276 __detail::_Hashtable_ebo_helper
<0, __hash_code_base
>>>::value
,
277 "Cache the hash code or make functors involved in hash code"
278 " and bucket index computation default constructible");
280 // When hash codes are not cached local iterator inherits from
281 // __hash_code_base above to compute node bucket index so it has to be
283 static_assert(__if_hash_not_cached
<
284 is_copy_assignable
<__hash_code_base
>>::value
,
285 "Cache the hash code or make functors involved in hash code"
286 " and bucket index computation copy assignable");
289 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
290 typename _ExtractKeya
, typename _Equala
,
291 typename _H1a
, typename _H2a
, typename _Hasha
,
292 typename _RehashPolicya
, typename _Traitsa
,
294 friend struct __detail::_Map_base
;
296 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
297 typename _ExtractKeya
, typename _Equala
,
298 typename _H1a
, typename _H2a
, typename _Hasha
,
299 typename _RehashPolicya
, typename _Traitsa
>
300 friend struct __detail::_Insert_base
;
302 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
303 typename _ExtractKeya
, typename _Equala
,
304 typename _H1a
, typename _H2a
, typename _Hasha
,
305 typename _RehashPolicya
, typename _Traitsa
,
306 bool _Constant_iteratorsa
, bool _Unique_keysa
>
307 friend struct __detail::_Insert
;
309 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
310 typename _ExtractKeya
, typename _Equala
,
311 typename _H1a
, typename _H2a
, typename _Hasha
,
312 typename _RehashPolicya
, typename _Traitsa
,
313 bool _IsCopyAssignable
>
314 friend struct __detail::_ReuseOrAllocNode
;
316 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
317 typename _ExtractKeya
, typename _Equala
,
318 typename _H1a
, typename _H2a
, typename _Hasha
,
319 typename _RehashPolicya
, typename _Traitsa
,
320 bool _IsMoveAssignable
>
321 friend struct __detail::_MoveReuseOrAllocNode
;
323 using size_type
= typename
__hashtable_base::size_type
;
324 using difference_type
= typename
__hashtable_base::difference_type
;
326 using iterator
= typename
__hashtable_base::iterator
;
327 using const_iterator
= typename
__hashtable_base::const_iterator
;
329 using local_iterator
= typename
__hashtable_base::local_iterator
;
330 using const_local_iterator
= typename
__hashtable_base::
331 const_local_iterator
;
334 typedef typename
_Alloc_traits::template rebind_alloc
<__node_type
>
336 // Use __gnu_cxx to benefit from _S_always_equal and al.
337 typedef __gnu_cxx::__alloc_traits
<_Node_alloc_type
> _Node_alloc_traits
;
340 typename
_Alloc_traits::template rebind_alloc
<__bucket_type
>
342 typedef std::allocator_traits
<_Bucket_alloc_type
> _Bucket_alloc_traits
;
344 using __before_begin
= __detail::_Before_begin
<_Node_alloc_type
>;
346 __bucket_type
* _M_buckets
;
347 size_type _M_bucket_count
;
348 __before_begin _M_bbegin
;
349 size_type _M_element_count
;
350 _RehashPolicy _M_rehash_policy
;
354 { return _M_bbegin
; }
356 const _Node_alloc_type
&
357 _M_node_allocator() const
358 { return _M_bbegin
; }
362 { return _M_bbegin
._M_node
; }
365 _M_before_begin() const
366 { return _M_bbegin
._M_node
; }
368 template<typename
... _Args
>
370 _M_allocate_node(_Args
&&... __args
);
373 _M_deallocate_node(__node_type
* __n
);
375 // Deallocate the linked list of nodes pointed to by __n
377 _M_deallocate_nodes(__node_type
* __n
);
380 _M_allocate_buckets(size_type __n
);
383 _M_deallocate_buckets(__bucket_type
*, size_type __n
);
386 _M_deallocate_buckets()
387 { _M_deallocate_buckets(_M_buckets
, _M_bucket_count
); }
389 // Gets bucket begin, deals with the fact that non-empty buckets contain
390 // their before begin node.
392 _M_bucket_begin(size_type __bkt
) const;
396 { return static_cast<__node_type
*>(_M_before_begin()._M_nxt
); }
398 template<typename _UnaryOp
>
400 _M_assign(const _Hashtable
&, const _UnaryOp
&);
403 _M_move_assign(_Hashtable
&&, std::true_type
);
406 _M_move_assign(_Hashtable
&&, std::false_type
);
412 // Constructor, destructor, assignment, swap
413 _Hashtable(size_type __bucket_hint
,
414 const _H1
&, const _H2
&, const _Hash
&,
415 const _Equal
&, const _ExtractKey
&,
416 const allocator_type
&);
418 template<typename _InputIterator
>
419 _Hashtable(_InputIterator __first
, _InputIterator __last
,
420 size_type __bucket_hint
,
421 const _H1
&, const _H2
&, const _Hash
&,
422 const _Equal
&, const _ExtractKey
&,
423 const allocator_type
&);
425 _Hashtable(const _Hashtable
&);
427 _Hashtable(_Hashtable
&&) noexcept
;
429 _Hashtable(const _Hashtable
&, const allocator_type
&);
431 _Hashtable(_Hashtable
&&, const allocator_type
&);
433 // Use delegating constructors.
435 _Hashtable(const allocator_type
& __a
)
436 : _Hashtable(10, _H1(), __detail::_Mod_range_hashing(),
437 __detail::_Default_ranged_hash(), key_equal(),
438 __key_extract(), __a
)
442 _Hashtable(size_type __n
= 10,
443 const _H1
& __hf
= _H1(),
444 const key_equal
& __eql
= key_equal(),
445 const allocator_type
& __a
= allocator_type())
446 : _Hashtable(__n
, __hf
, __detail::_Mod_range_hashing(),
447 __detail::_Default_ranged_hash(), __eql
,
448 __key_extract(), __a
)
451 template<typename _InputIterator
>
452 _Hashtable(_InputIterator __f
, _InputIterator __l
,
454 const _H1
& __hf
= _H1(),
455 const key_equal
& __eql
= key_equal(),
456 const allocator_type
& __a
= allocator_type())
457 : _Hashtable(__f
, __l
, __n
, __hf
, __detail::_Mod_range_hashing(),
458 __detail::_Default_ranged_hash(), __eql
,
459 __key_extract(), __a
)
462 _Hashtable(initializer_list
<value_type
> __l
,
464 const _H1
& __hf
= _H1(),
465 const key_equal
& __eql
= key_equal(),
466 const allocator_type
& __a
= allocator_type())
467 : _Hashtable(__l
.begin(), __l
.end(), __n
, __hf
,
468 __detail::_Mod_range_hashing(),
469 __detail::_Default_ranged_hash(), __eql
,
470 __key_extract(), __a
)
474 operator=(const _Hashtable
& __ht
);
477 operator=(_Hashtable
&& __ht
)
478 noexcept(_Node_alloc_traits::_S_nothrow_move())
480 constexpr bool __move_storage
=
481 _Node_alloc_traits::_S_propagate_on_move_assign()
482 || _Node_alloc_traits::_S_always_equal();
483 _M_move_assign(std::move(__ht
),
484 integral_constant
<bool, __move_storage
>());
489 operator=(initializer_list
<value_type
> __l
)
492 this->insert(__l
.begin(), __l
.end());
496 ~_Hashtable() noexcept
;
500 noexcept(_Node_alloc_traits::_S_nothrow_swap());
502 // Basic container operations
505 { return iterator(_M_begin()); }
508 begin() const noexcept
509 { return const_iterator(_M_begin()); }
513 { return iterator(nullptr); }
517 { return const_iterator(nullptr); }
520 cbegin() const noexcept
521 { return const_iterator(_M_begin()); }
524 cend() const noexcept
525 { return const_iterator(nullptr); }
528 size() const noexcept
529 { return _M_element_count
; }
532 empty() const noexcept
533 { return size() == 0; }
536 get_allocator() const noexcept
537 { return allocator_type(_M_node_allocator()); }
540 max_size() const noexcept
541 { return _Node_alloc_traits::max_size(_M_node_allocator()); }
546 { return this->_M_eq(); }
548 // hash_function, if present, comes from _Hash_code_base.
552 bucket_count() const noexcept
553 { return _M_bucket_count
; }
556 max_bucket_count() const noexcept
557 { return max_size(); }
560 bucket_size(size_type __n
) const
561 { return std::distance(begin(__n
), end(__n
)); }
564 bucket(const key_type
& __k
) const
565 { return _M_bucket_index(__k
, this->_M_hash_code(__k
)); }
570 return local_iterator(*this, _M_bucket_begin(__n
),
571 __n
, _M_bucket_count
);
576 { return local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
579 begin(size_type __n
) const
581 return const_local_iterator(*this, _M_bucket_begin(__n
),
582 __n
, _M_bucket_count
);
586 end(size_type __n
) const
587 { return const_local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
591 cbegin(size_type __n
) const
593 return const_local_iterator(*this, _M_bucket_begin(__n
),
594 __n
, _M_bucket_count
);
598 cend(size_type __n
) const
599 { return const_local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
602 load_factor() const noexcept
604 return static_cast<float>(size()) / static_cast<float>(bucket_count());
607 // max_load_factor, if present, comes from _Rehash_base.
609 // Generalization of max_load_factor. Extension, not found in
610 // TR1. Only useful if _RehashPolicy is something other than
613 __rehash_policy() const
614 { return _M_rehash_policy
; }
617 __rehash_policy(const _RehashPolicy
&);
621 find(const key_type
& __k
);
624 find(const key_type
& __k
) const;
627 count(const key_type
& __k
) const;
629 std::pair
<iterator
, iterator
>
630 equal_range(const key_type
& __k
);
632 std::pair
<const_iterator
, const_iterator
>
633 equal_range(const key_type
& __k
) const;
636 // Bucket index computation helpers.
638 _M_bucket_index(__node_type
* __n
) const noexcept
639 { return __hash_code_base::_M_bucket_index(__n
, _M_bucket_count
); }
642 _M_bucket_index(const key_type
& __k
, __hash_code __c
) const
643 { return __hash_code_base::_M_bucket_index(__k
, __c
, _M_bucket_count
); }
645 // Find and insert helper functions and types
646 // Find the node before the one matching the criteria.
648 _M_find_before_node(size_type
, const key_type
&, __hash_code
) const;
651 _M_find_node(size_type __bkt
, const key_type
& __key
,
652 __hash_code __c
) const
654 __node_base
* __before_n
= _M_find_before_node(__bkt
, __key
, __c
);
656 return static_cast<__node_type
*>(__before_n
->_M_nxt
);
660 // Insert a node at the beginning of a bucket.
662 _M_insert_bucket_begin(size_type
, __node_type
*);
664 // Remove the bucket first node
666 _M_remove_bucket_begin(size_type __bkt
, __node_type
* __next_n
,
667 size_type __next_bkt
);
669 // Get the node before __n in the bucket __bkt
671 _M_get_previous_node(size_type __bkt
, __node_base
* __n
);
673 // Insert node with hash code __code, in bucket bkt if no rehash (assumes
674 // no element with its key already present). Take ownership of the node,
675 // deallocate it on exception.
677 _M_insert_unique_node(size_type __bkt
, __hash_code __code
,
680 // Insert node with hash code __code. Take ownership of the node,
681 // deallocate it on exception.
683 _M_insert_multi_node(__hash_code __code
, __node_type
* __n
);
685 template<typename
... _Args
>
686 std::pair
<iterator
, bool>
687 _M_emplace(std::true_type
, _Args
&&... __args
);
689 template<typename
... _Args
>
691 _M_emplace(std::false_type
, _Args
&&... __args
);
693 template<typename _Arg
>
694 std::pair
<iterator
, bool>
695 _M_insert(_Arg
&&, std::true_type
);
697 template<typename _Arg
>
699 _M_insert(_Arg
&&, std::false_type
);
702 _M_erase(std::true_type
, const key_type
&);
705 _M_erase(std::false_type
, const key_type
&);
708 _M_erase(size_type __bkt
, __node_base
* __prev_n
, __node_type
* __n
);
712 template<typename
... _Args
>
714 emplace(_Args
&&... __args
)
715 { return _M_emplace(__unique_keys(), std::forward
<_Args
>(__args
)...); }
717 template<typename
... _Args
>
719 emplace_hint(const_iterator
, _Args
&&... __args
)
720 { return __iconv_type()(emplace(std::forward
<_Args
>(__args
)...)); }
722 // Insert member functions via inheritance.
726 erase(const_iterator
);
731 { return erase(const_iterator(__it
)); }
734 erase(const key_type
& __k
)
736 if (__builtin_expect(_M_bucket_count
== 0, false))
738 return _M_erase(__unique_keys(), __k
);
742 erase(const_iterator
, const_iterator
);
747 // Set number of buckets to be appropriate for container of n element.
748 void rehash(size_type __n
);
751 // reserve, if present, comes from _Rehash_base.
754 // Helper rehash method used when keys are unique.
755 void _M_rehash_aux(size_type __n
, std::true_type
);
757 // Helper rehash method used when keys can be non-unique.
758 void _M_rehash_aux(size_type __n
, std::false_type
);
760 // Unconditionally change size of bucket array to n, restore
761 // hash policy state to __state on exception.
762 void _M_rehash(size_type __n
, const __rehash_state
& __state
);
766 // Definitions of class template _Hashtable's out-of-line member functions.
767 template<typename _Key
, typename _Value
,
768 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
769 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
771 template<typename
... _Args
>
772 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
773 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::__node_type
*
774 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
775 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
776 _M_allocate_node(_Args
&&... __args
)
778 auto __nptr
= _Node_alloc_traits::allocate(_M_node_allocator(), 1);
779 __node_type
* __n
= std::__addressof(*__nptr
);
782 _Value_alloc_type
__a(_M_node_allocator());
783 ::new ((void*)__n
) __node_type();
784 _Value_alloc_traits::construct(__a
, __n
->_M_valptr(),
785 std::forward
<_Args
>(__args
)...);
790 _Node_alloc_traits::deallocate(_M_node_allocator(), __nptr
, 1);
791 __throw_exception_again
;
795 template<typename _Key
, typename _Value
,
796 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
797 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
800 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
801 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
802 _M_deallocate_node(__node_type
* __n
)
804 typedef typename
_Node_alloc_traits::pointer _Ptr
;
805 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__n
);
806 _Value_alloc_type
__a(_M_node_allocator());
807 _Value_alloc_traits::destroy(__a
, __n
->_M_valptr());
809 _Node_alloc_traits::deallocate(_M_node_allocator(), __ptr
, 1);
812 template<typename _Key
, typename _Value
,
813 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
814 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
817 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
818 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
819 _M_deallocate_nodes(__node_type
* __n
)
823 __node_type
* __tmp
= __n
;
824 __n
= __n
->_M_next();
825 _M_deallocate_node(__tmp
);
829 template<typename _Key
, typename _Value
,
830 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
831 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
833 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
834 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::__bucket_type
*
835 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
836 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
837 _M_allocate_buckets(size_type __n
)
839 _Bucket_alloc_type
__alloc(_M_node_allocator());
841 auto __ptr
= _Bucket_alloc_traits::allocate(__alloc
, __n
);
842 __bucket_type
* __p
= std::__addressof(*__ptr
);
843 __builtin_memset(__p
, 0, __n
* sizeof(__bucket_type
));
847 template<typename _Key
, typename _Value
,
848 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
849 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
852 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
853 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
854 _M_deallocate_buckets(__bucket_type
* __bkts
, size_type __n
)
856 typedef typename
_Bucket_alloc_traits::pointer _Ptr
;
857 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__bkts
);
858 _Bucket_alloc_type
__alloc(_M_node_allocator());
859 _Bucket_alloc_traits::deallocate(__alloc
, __ptr
, __n
);
862 template<typename _Key
, typename _Value
,
863 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
864 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
866 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
867 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
868 _Traits
>::__node_type
*
869 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
870 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
871 _M_bucket_begin(size_type __bkt
) const
873 __node_base
* __n
= _M_buckets
[__bkt
];
874 return __n
? static_cast<__node_type
*>(__n
->_M_nxt
) : nullptr;
877 template<typename _Key
, typename _Value
,
878 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
879 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
881 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
882 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
883 _Hashtable(size_type __bucket_hint
,
884 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
885 const _Equal
& __eq
, const _ExtractKey
& __exk
,
886 const allocator_type
& __a
)
887 : __hashtable_base(__exk
, __h1
, __h2
, __h
, __eq
),
894 _M_bucket_count
= _M_rehash_policy
._M_next_bkt(__bucket_hint
);
895 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
898 template<typename _Key
, typename _Value
,
899 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
900 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
902 template<typename _InputIterator
>
903 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
904 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
905 _Hashtable(_InputIterator __f
, _InputIterator __l
,
906 size_type __bucket_hint
,
907 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
908 const _Equal
& __eq
, const _ExtractKey
& __exk
,
909 const allocator_type
& __a
)
910 : __hashtable_base(__exk
, __h1
, __h2
, __h
, __eq
),
917 auto __nb_elems
= __detail::__distance_fw(__f
, __l
);
919 _M_rehash_policy
._M_next_bkt(
920 std::max(_M_rehash_policy
._M_bkt_for_elements(__nb_elems
),
923 _M_buckets
= _M_allocate_buckets(_M_bucket_count
);
926 for (; __f
!= __l
; ++__f
)
932 _M_deallocate_buckets();
933 __throw_exception_again
;
937 template<typename _Key
, typename _Value
,
938 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
939 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
941 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
942 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>&
943 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
944 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::operator=(
945 const _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
946 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>& __ht
)
951 if (_Node_alloc_traits::_S_propagate_on_copy_assign())
953 auto& __this_alloc
= this->_M_node_allocator();
954 auto& __that_alloc
= __ht
._M_node_allocator();
955 if (!_Node_alloc_traits::_S_always_equal()
956 && __this_alloc
!= __that_alloc
)
958 // Replacement allocator cannot free existing storage.
959 _M_deallocate_nodes(_M_begin());
960 if (__builtin_expect(_M_bucket_count
!= 0, true))
961 _M_deallocate_buckets();
963 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
964 __hashtable_base::operator=(__ht
);
965 _M_bucket_count
= __ht
._M_bucket_count
;
966 _M_element_count
= __ht
._M_element_count
;
967 _M_rehash_policy
= __ht
._M_rehash_policy
;
971 [this](const __node_type
* __n
)
972 { return _M_allocate_node(__n
->_M_v()); });
976 // _M_assign took care of deallocating all memory. Now we
977 // must make sure this instance remains in a usable state.
979 __throw_exception_again
;
983 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
986 // Reuse allocated buckets and nodes.
987 __bucket_type
* __former_buckets
= nullptr;
988 std::size_t __former_bucket_count
= _M_bucket_count
;
989 const __rehash_state
& __former_state
= _M_rehash_policy
._M_state();
991 if (_M_bucket_count
!= __ht
._M_bucket_count
)
993 __former_buckets
= _M_buckets
;
994 _M_buckets
= _M_allocate_buckets(__ht
._M_bucket_count
);
995 _M_bucket_count
= __ht
._M_bucket_count
;
998 __builtin_memset(_M_buckets
, 0,
999 _M_bucket_count
* sizeof(__bucket_type
));
1003 __hashtable_base::operator=(__ht
);
1004 _M_element_count
= __ht
._M_element_count
;
1005 _M_rehash_policy
= __ht
._M_rehash_policy
;
1006 __detail::_ReuseOrAllocNode
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1007 _Equal
, _H1
, _H2
, _Hash
,
1008 _RehashPolicy
, _Traits
>
1009 __roan(_M_begin(), *this);
1010 _M_before_begin()._M_nxt
= nullptr;
1011 _M_assign(__ht
, __roan
);
1012 if (__former_buckets
)
1013 _M_deallocate_buckets(__former_buckets
, __former_bucket_count
);
1017 if (__former_buckets
)
1019 // Restore previous buckets.
1020 _M_deallocate_buckets();
1021 _M_rehash_policy
._M_reset(__former_state
);
1022 _M_buckets
= __former_buckets
;
1023 _M_bucket_count
= __former_bucket_count
;
1025 __builtin_memset(_M_buckets
, 0,
1026 _M_bucket_count
* sizeof(__bucket_type
));
1027 __throw_exception_again
;
1032 template<typename _Key
, typename _Value
,
1033 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1034 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1036 template<typename _UnaryOp
>
1038 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1039 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1040 _M_assign(const _Hashtable
& __ht
, const _UnaryOp
& __node_getter
)
1042 __bucket_type
* __buckets
= nullptr;
1044 _M_buckets
= __buckets
= _M_allocate_buckets(_M_bucket_count
);
1048 if (!__ht
._M_before_begin()._M_nxt
)
1051 // First deal with the special first node pointed to by
1053 __node_type
* __ht_n
= __ht
._M_begin();
1054 __node_type
* __this_n
= __node_getter(__ht_n
);
1055 this->_M_copy_code(__this_n
, __ht_n
);
1056 _M_before_begin()._M_nxt
= __this_n
;
1057 _M_buckets
[_M_bucket_index(__this_n
)] = &_M_before_begin();
1059 // Then deal with other nodes.
1060 __node_base
* __prev_n
= __this_n
;
1061 for (__ht_n
= __ht_n
->_M_next(); __ht_n
; __ht_n
= __ht_n
->_M_next())
1063 __this_n
= __node_getter(__ht_n
);
1064 __prev_n
->_M_nxt
= __this_n
;
1065 this->_M_copy_code(__this_n
, __ht_n
);
1066 size_type __bkt
= _M_bucket_index(__this_n
);
1067 if (!_M_buckets
[__bkt
])
1068 _M_buckets
[__bkt
] = __prev_n
;
1069 __prev_n
= __this_n
;
1076 _M_deallocate_buckets();
1077 __throw_exception_again
;
1081 template<typename _Key
, typename _Value
,
1082 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1083 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1086 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1087 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1090 _M_rehash_policy
._M_reset();
1091 _M_bucket_count
= 0;
1092 _M_buckets
= nullptr;
1093 _M_before_begin()._M_nxt
= nullptr;
1094 _M_element_count
= 0;
1097 template<typename _Key
, typename _Value
,
1098 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1099 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1102 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1103 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1104 _M_move_assign(_Hashtable
&& __ht
, std::true_type
)
1106 _M_deallocate_nodes(_M_begin());
1107 if (__builtin_expect(_M_bucket_count
!= 0, true))
1108 _M_deallocate_buckets();
1110 __hashtable_base::operator=(std::move(__ht
));
1111 _M_rehash_policy
= __ht
._M_rehash_policy
;
1112 _M_buckets
= __ht
._M_buckets
;
1113 _M_bucket_count
= __ht
._M_bucket_count
;
1114 _M_before_begin()._M_nxt
= __ht
._M_before_begin()._M_nxt
;
1115 _M_element_count
= __ht
._M_element_count
;
1116 std::__alloc_on_move(_M_node_allocator(), __ht
._M_node_allocator());
1118 // Fix buckets containing the _M_before_begin pointers that can't be
1121 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin();
1125 template<typename _Key
, typename _Value
,
1126 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1127 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1130 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1131 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1132 _M_move_assign(_Hashtable
&& __ht
, std::false_type
)
1134 if (__ht
._M_node_allocator() == _M_node_allocator())
1135 _M_move_assign(std::move(__ht
), std::true_type());
1138 // Can't move memory, move elements then.
1139 __bucket_type
* __former_buckets
= nullptr;
1140 size_type __former_bucket_count
= _M_bucket_count
;
1141 const __rehash_state
& __former_state
= _M_rehash_policy
._M_state();
1143 if (_M_bucket_count
!= __ht
._M_bucket_count
)
1145 __former_buckets
= _M_buckets
;
1146 _M_buckets
= _M_allocate_buckets(__ht
._M_bucket_count
);
1147 _M_bucket_count
= __ht
._M_bucket_count
;
1150 __builtin_memset(_M_buckets
, 0,
1151 _M_bucket_count
* sizeof(__bucket_type
));
1155 __hashtable_base::operator=(std::move(__ht
));
1156 _M_element_count
= __ht
._M_element_count
;
1157 _M_rehash_policy
= __ht
._M_rehash_policy
;
1158 __detail::_MoveReuseOrAllocNode
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1159 _Equal
, _H1
, _H2
, _Hash
,
1160 _RehashPolicy
, _Traits
>
1161 __mroan(_M_begin(), *this);
1162 _M_before_begin()._M_nxt
= nullptr;
1163 _M_assign(__ht
, __mroan
);
1168 if (__former_buckets
)
1170 _M_deallocate_buckets();
1171 _M_rehash_policy
._M_reset(__former_state
);
1172 _M_buckets
= __former_buckets
;
1173 _M_bucket_count
= __former_bucket_count
;
1175 __builtin_memset(_M_buckets
, 0,
1176 _M_bucket_count
* sizeof(__bucket_type
));
1177 __throw_exception_again
;
1182 template<typename _Key
, typename _Value
,
1183 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1184 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1186 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1187 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1188 _Hashtable(const _Hashtable
& __ht
)
1189 : __hashtable_base(__ht
),
1191 __rehash_base(__ht
),
1193 _M_bucket_count(__ht
._M_bucket_count
),
1194 _M_bbegin(_Node_alloc_traits::_S_select_on_copy(
1195 __ht
._M_node_allocator())),
1196 _M_element_count(__ht
._M_element_count
),
1197 _M_rehash_policy(__ht
._M_rehash_policy
)
1200 [this](const __node_type
* __n
)
1201 { return _M_allocate_node(__n
->_M_v()); });
1204 template<typename _Key
, typename _Value
,
1205 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1206 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1208 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1209 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1210 _Hashtable(_Hashtable
&& __ht
) noexcept
1211 : __hashtable_base(__ht
),
1213 __rehash_base(__ht
),
1214 _M_buckets(__ht
._M_buckets
),
1215 _M_bucket_count(__ht
._M_bucket_count
),
1216 _M_bbegin(std::move(__ht
._M_bbegin
)),
1217 _M_element_count(__ht
._M_element_count
),
1218 _M_rehash_policy(__ht
._M_rehash_policy
)
1220 // Update, if necessary, bucket pointing to before begin that hasn't
1223 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin();
1227 template<typename _Key
, typename _Value
,
1228 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1229 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1231 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1232 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1233 _Hashtable(const _Hashtable
& __ht
, const allocator_type
& __a
)
1234 : __hashtable_base(__ht
),
1236 __rehash_base(__ht
),
1238 _M_bucket_count(__ht
._M_bucket_count
),
1239 _M_bbegin(_Node_alloc_type(__a
)),
1240 _M_element_count(__ht
._M_element_count
),
1241 _M_rehash_policy(__ht
._M_rehash_policy
)
1244 [this](const __node_type
* __n
)
1245 { return _M_allocate_node(__n
->_M_v()); });
1248 template<typename _Key
, typename _Value
,
1249 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1250 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1252 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1253 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1254 _Hashtable(_Hashtable
&& __ht
, const allocator_type
& __a
)
1255 : __hashtable_base(__ht
),
1257 __rehash_base(__ht
),
1259 _M_bucket_count(__ht
._M_bucket_count
),
1260 _M_bbegin(_Node_alloc_type(__a
)),
1261 _M_element_count(__ht
._M_element_count
),
1262 _M_rehash_policy(__ht
._M_rehash_policy
)
1264 if (__ht
._M_node_allocator() == _M_node_allocator())
1266 _M_buckets
= __ht
._M_buckets
;
1267 _M_before_begin()._M_nxt
= __ht
._M_before_begin()._M_nxt
;
1268 // Update, if necessary, bucket pointing to before begin that hasn't
1271 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin();
1277 [this](__node_type
* __n
)
1279 return _M_allocate_node(
1280 std::move_if_noexcept(__n
->_M_v()));
1286 template<typename _Key
, typename _Value
,
1287 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1288 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1290 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1291 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1292 ~_Hashtable() noexcept
1296 _M_deallocate_buckets();
1299 template<typename _Key
, typename _Value
,
1300 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1301 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1304 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1305 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1306 swap(_Hashtable
& __x
)
1307 noexcept(_Node_alloc_traits::_S_nothrow_swap())
1309 // The only base class with member variables is hash_code_base.
1310 // We define _Hash_code_base::_M_swap because different
1311 // specializations have different members.
1314 std::__alloc_on_swap(_M_node_allocator(), __x
._M_node_allocator());
1315 std::swap(_M_rehash_policy
, __x
._M_rehash_policy
);
1316 std::swap(_M_buckets
, __x
._M_buckets
);
1317 std::swap(_M_bucket_count
, __x
._M_bucket_count
);
1318 std::swap(_M_before_begin()._M_nxt
, __x
._M_before_begin()._M_nxt
);
1319 std::swap(_M_element_count
, __x
._M_element_count
);
1321 // Fix buckets containing the _M_before_begin pointers that can't be
1324 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin();
1326 __x
._M_buckets
[__x
._M_bucket_index(__x
._M_begin())]
1327 = &(__x
._M_before_begin());
1330 template<typename _Key
, typename _Value
,
1331 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1332 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1335 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1336 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1337 __rehash_policy(const _RehashPolicy
& __pol
)
1339 size_type __n_bkt
= __pol
._M_bkt_for_elements(_M_element_count
);
1340 __n_bkt
= __pol
._M_next_bkt(__n_bkt
);
1341 if (__n_bkt
!= _M_bucket_count
)
1342 _M_rehash(__n_bkt
, _M_rehash_policy
._M_state());
1343 _M_rehash_policy
= __pol
;
1346 template<typename _Key
, typename _Value
,
1347 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1348 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1350 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1351 _H1
, _H2
, _Hash
, _RehashPolicy
,
1353 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1354 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1355 find(const key_type
& __k
)
1357 if (__builtin_expect(_M_bucket_count
== 0, false))
1360 __hash_code __code
= this->_M_hash_code(__k
);
1361 std::size_t __n
= _M_bucket_index(__k
, __code
);
1362 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1363 return __p
? iterator(__p
) : end();
1366 template<typename _Key
, typename _Value
,
1367 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1368 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1370 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1371 _H1
, _H2
, _Hash
, _RehashPolicy
,
1372 _Traits
>::const_iterator
1373 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1374 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1375 find(const key_type
& __k
) const
1377 if (__builtin_expect(_M_bucket_count
== 0, false))
1380 __hash_code __code
= this->_M_hash_code(__k
);
1381 std::size_t __n
= _M_bucket_index(__k
, __code
);
1382 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1383 return __p
? const_iterator(__p
) : end();
1386 template<typename _Key
, typename _Value
,
1387 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1388 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1390 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1391 _H1
, _H2
, _Hash
, _RehashPolicy
,
1393 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1394 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1395 count(const key_type
& __k
) const
1397 if (__builtin_expect(_M_bucket_count
== 0, false))
1400 __hash_code __code
= this->_M_hash_code(__k
);
1401 std::size_t __n
= _M_bucket_index(__k
, __code
);
1402 __node_type
* __p
= _M_bucket_begin(__n
);
1406 std::size_t __result
= 0;
1407 for (;; __p
= __p
->_M_next())
1409 if (this->_M_equals(__k
, __code
, __p
))
1412 // All equivalent values are next to each other, if we
1413 // found a non-equivalent value after an equivalent one it
1414 // means that we won't find any more equivalent values.
1416 if (!__p
->_M_nxt
|| _M_bucket_index(__p
->_M_next()) != __n
)
1422 template<typename _Key
, typename _Value
,
1423 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1424 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1426 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1427 _ExtractKey
, _Equal
, _H1
,
1428 _H2
, _Hash
, _RehashPolicy
,
1430 typename _Hashtable
<_Key
, _Value
, _Alloc
,
1431 _ExtractKey
, _Equal
, _H1
,
1432 _H2
, _Hash
, _RehashPolicy
,
1434 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1435 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1436 equal_range(const key_type
& __k
)
1438 if (__builtin_expect(_M_bucket_count
== 0, false))
1439 return std::make_pair(end(), end());
1441 __hash_code __code
= this->_M_hash_code(__k
);
1442 std::size_t __n
= _M_bucket_index(__k
, __code
);
1443 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1447 __node_type
* __p1
= __p
->_M_next();
1448 while (__p1
&& _M_bucket_index(__p1
) == __n
1449 && this->_M_equals(__k
, __code
, __p1
))
1450 __p1
= __p1
->_M_next();
1452 return std::make_pair(iterator(__p
), iterator(__p1
));
1455 return std::make_pair(end(), end());
1458 template<typename _Key
, typename _Value
,
1459 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1460 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1462 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1463 _ExtractKey
, _Equal
, _H1
,
1464 _H2
, _Hash
, _RehashPolicy
,
1465 _Traits
>::const_iterator
,
1466 typename _Hashtable
<_Key
, _Value
, _Alloc
,
1467 _ExtractKey
, _Equal
, _H1
,
1468 _H2
, _Hash
, _RehashPolicy
,
1469 _Traits
>::const_iterator
>
1470 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1471 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1472 equal_range(const key_type
& __k
) const
1474 if (__builtin_expect(_M_bucket_count
== 0, false))
1475 return std::make_pair(end(), end());
1477 __hash_code __code
= this->_M_hash_code(__k
);
1478 std::size_t __n
= _M_bucket_index(__k
, __code
);
1479 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1483 __node_type
* __p1
= __p
->_M_next();
1484 while (__p1
&& _M_bucket_index(__p1
) == __n
1485 && this->_M_equals(__k
, __code
, __p1
))
1486 __p1
= __p1
->_M_next();
1488 return std::make_pair(const_iterator(__p
), const_iterator(__p1
));
1491 return std::make_pair(end(), end());
1494 // Find the node whose key compares equal to k in the bucket n.
1495 // Return nullptr if no node is found.
1496 template<typename _Key
, typename _Value
,
1497 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1498 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1500 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1501 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
1502 _Traits
>::__node_base
*
1503 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1504 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1505 _M_find_before_node(size_type __n
, const key_type
& __k
,
1506 __hash_code __code
) const
1508 __node_base
* __prev_p
= _M_buckets
[__n
];
1511 __node_type
* __p
= static_cast<__node_type
*>(__prev_p
->_M_nxt
);
1512 for (;; __p
= __p
->_M_next())
1514 if (this->_M_equals(__k
, __code
, __p
))
1516 if (!__p
->_M_nxt
|| _M_bucket_index(__p
->_M_next()) != __n
)
1523 template<typename _Key
, typename _Value
,
1524 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1525 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1528 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1529 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1530 _M_insert_bucket_begin(size_type __bkt
, __node_type
* __node
)
1532 if (_M_buckets
[__bkt
])
1534 // Bucket is not empty, we just need to insert the new node
1535 // after the bucket before begin.
1536 __node
->_M_nxt
= _M_buckets
[__bkt
]->_M_nxt
;
1537 _M_buckets
[__bkt
]->_M_nxt
= __node
;
1541 // The bucket is empty, the new node is inserted at the
1542 // beginning of the singly-linked list and the bucket will
1543 // contain _M_before_begin pointer.
1544 __node
->_M_nxt
= _M_before_begin()._M_nxt
;
1545 _M_before_begin()._M_nxt
= __node
;
1547 // We must update former begin bucket that is pointing to
1549 _M_buckets
[_M_bucket_index(__node
->_M_next())] = __node
;
1550 _M_buckets
[__bkt
] = &_M_before_begin();
1554 template<typename _Key
, typename _Value
,
1555 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1556 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1559 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1560 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1561 _M_remove_bucket_begin(size_type __bkt
, __node_type
* __next
,
1562 size_type __next_bkt
)
1564 if (!__next
|| __next_bkt
!= __bkt
)
1566 // Bucket is now empty
1567 // First update next bucket if any
1569 _M_buckets
[__next_bkt
] = _M_buckets
[__bkt
];
1571 // Second update before begin node if necessary
1572 if (&_M_before_begin() == _M_buckets
[__bkt
])
1573 _M_before_begin()._M_nxt
= __next
;
1574 _M_buckets
[__bkt
] = nullptr;
1578 template<typename _Key
, typename _Value
,
1579 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1580 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1582 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1583 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
1584 _Traits
>::__node_base
*
1585 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1586 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1587 _M_get_previous_node(size_type __bkt
, __node_base
* __n
)
1589 __node_base
* __prev_n
= _M_buckets
[__bkt
];
1590 while (__prev_n
->_M_nxt
!= __n
)
1591 __prev_n
= __prev_n
->_M_nxt
;
1595 template<typename _Key
, typename _Value
,
1596 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1597 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1599 template<typename
... _Args
>
1600 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1601 _ExtractKey
, _Equal
, _H1
,
1602 _H2
, _Hash
, _RehashPolicy
,
1603 _Traits
>::iterator
, bool>
1604 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1605 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1606 _M_emplace(std::true_type
, _Args
&&... __args
)
1608 // First build the node to get access to the hash code
1609 __node_type
* __node
= _M_allocate_node(std::forward
<_Args
>(__args
)...);
1610 const key_type
& __k
= this->_M_extract()(__node
->_M_v());
1614 __code
= this->_M_hash_code(__k
);
1618 _M_deallocate_node(__node
);
1619 __throw_exception_again
;
1622 size_type __bkt
= _M_bucket_index(__k
, __code
);
1623 if (__node_type
* __p
= _M_find_node(__bkt
, __k
, __code
))
1625 // There is already an equivalent node, no insertion
1626 _M_deallocate_node(__node
);
1627 return std::make_pair(iterator(__p
), false);
1631 return std::make_pair(_M_insert_unique_node(__bkt
, __code
, __node
),
1635 template<typename _Key
, typename _Value
,
1636 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1637 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1639 template<typename
... _Args
>
1640 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1641 _H1
, _H2
, _Hash
, _RehashPolicy
,
1643 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1644 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1645 _M_emplace(std::false_type
, _Args
&&... __args
)
1647 // First build the node to get its hash code.
1648 __node_type
* __node
= _M_allocate_node(std::forward
<_Args
>(__args
)...);
1653 __code
= this->_M_hash_code(this->_M_extract()(__node
->_M_v()));
1657 _M_deallocate_node(__node
);
1658 __throw_exception_again
;
1661 return _M_insert_multi_node(__code
, __node
);
1664 template<typename _Key
, typename _Value
,
1665 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1666 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1668 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1669 _H1
, _H2
, _Hash
, _RehashPolicy
,
1671 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1672 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1673 _M_insert_unique_node(size_type __bkt
, __hash_code __code
,
1674 __node_type
* __node
)
1676 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1677 std::pair
<bool, std::size_t> __do_rehash
1678 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
, _M_element_count
, 1);
1682 if (__do_rehash
.first
)
1684 _M_rehash(__do_rehash
.second
, __saved_state
);
1685 __bkt
= _M_bucket_index(this->_M_extract()(__node
->_M_v()), __code
);
1688 this->_M_store_code(__node
, __code
);
1690 // Always insert at the begining of the bucket.
1691 _M_insert_bucket_begin(__bkt
, __node
);
1693 return iterator(__node
);
1697 _M_deallocate_node(__node
);
1698 __throw_exception_again
;
1702 // Insert node, in bucket bkt if no rehash (assumes no element with its key
1703 // already present). Take ownership of the node, deallocate it on exception.
1704 template<typename _Key
, typename _Value
,
1705 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1706 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1708 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1709 _H1
, _H2
, _Hash
, _RehashPolicy
,
1711 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1712 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1713 _M_insert_multi_node(__hash_code __code
, __node_type
* __node
)
1715 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1716 std::pair
<bool, std::size_t> __do_rehash
1717 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
, _M_element_count
, 1);
1721 if (__do_rehash
.first
)
1722 _M_rehash(__do_rehash
.second
, __saved_state
);
1724 this->_M_store_code(__node
, __code
);
1725 const key_type
& __k
= this->_M_extract()(__node
->_M_v());
1726 size_type __bkt
= _M_bucket_index(__k
, __code
);
1728 // Find the node before an equivalent one.
1729 __node_base
* __prev
= _M_find_before_node(__bkt
, __k
, __code
);
1732 // Insert after the node before the equivalent one.
1733 __node
->_M_nxt
= __prev
->_M_nxt
;
1734 __prev
->_M_nxt
= __node
;
1737 // The inserted node has no equivalent in the
1738 // hashtable. We must insert the new node at the
1739 // beginning of the bucket to preserve equivalent
1740 // elements' relative positions.
1741 _M_insert_bucket_begin(__bkt
, __node
);
1743 return iterator(__node
);
1747 _M_deallocate_node(__node
);
1748 __throw_exception_again
;
1752 // Insert v if no element with its key is already present.
1753 template<typename _Key
, typename _Value
,
1754 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1755 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1757 template<typename _Arg
>
1758 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1759 _ExtractKey
, _Equal
, _H1
,
1760 _H2
, _Hash
, _RehashPolicy
,
1761 _Traits
>::iterator
, bool>
1762 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1763 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1764 _M_insert(_Arg
&& __v
, std::true_type
)
1766 const key_type
& __k
= this->_M_extract()(__v
);
1767 __hash_code __code
= this->_M_hash_code(__k
);
1768 size_type __bkt
= _M_bucket_index(__k
, __code
);
1770 __node_type
* __n
= _M_find_node(__bkt
, __k
, __code
);
1772 return std::make_pair(iterator(__n
), false);
1774 __n
= _M_allocate_node(std::forward
<_Arg
>(__v
));
1775 return std::make_pair(_M_insert_unique_node(__bkt
, __code
, __n
), true);
1778 // Insert v unconditionally.
1779 template<typename _Key
, typename _Value
,
1780 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1781 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1783 template<typename _Arg
>
1784 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1785 _H1
, _H2
, _Hash
, _RehashPolicy
,
1787 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1788 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1789 _M_insert(_Arg
&& __v
, std::false_type
)
1791 // First compute the hash code so that we don't do anything if it
1793 __hash_code __code
= this->_M_hash_code(this->_M_extract()(__v
));
1795 // Second allocate new node so that we don't rehash if it throws.
1796 __node_type
* __node
= _M_allocate_node(std::forward
<_Arg
>(__v
));
1798 return _M_insert_multi_node(__code
, __node
);
1801 template<typename _Key
, typename _Value
,
1802 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1803 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1805 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1806 _H1
, _H2
, _Hash
, _RehashPolicy
,
1808 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1809 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1810 erase(const_iterator __it
)
1812 __node_type
* __n
= __it
._M_cur
;
1813 std::size_t __bkt
= _M_bucket_index(__n
);
1815 // Look for previous node to unlink it from the erased one, this
1816 // is why we need buckets to contain the before begin to make
1817 // this search fast.
1818 __node_base
* __prev_n
= _M_get_previous_node(__bkt
, __n
);
1819 return _M_erase(__bkt
, __prev_n
, __n
);
1822 template<typename _Key
, typename _Value
,
1823 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1824 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1826 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1827 _H1
, _H2
, _Hash
, _RehashPolicy
,
1829 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1830 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1831 _M_erase(size_type __bkt
, __node_base
* __prev_n
, __node_type
* __n
)
1833 if (__prev_n
== _M_buckets
[__bkt
])
1834 _M_remove_bucket_begin(__bkt
, __n
->_M_next(),
1835 __n
->_M_nxt
? _M_bucket_index(__n
->_M_next()) : 0);
1836 else if (__n
->_M_nxt
)
1838 size_type __next_bkt
= _M_bucket_index(__n
->_M_next());
1839 if (__next_bkt
!= __bkt
)
1840 _M_buckets
[__next_bkt
] = __prev_n
;
1843 __prev_n
->_M_nxt
= __n
->_M_nxt
;
1844 iterator
__result(__n
->_M_next());
1845 _M_deallocate_node(__n
);
1851 template<typename _Key
, typename _Value
,
1852 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1853 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1855 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1856 _H1
, _H2
, _Hash
, _RehashPolicy
,
1858 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1859 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1860 _M_erase(std::true_type
, const key_type
& __k
)
1862 __hash_code __code
= this->_M_hash_code(__k
);
1863 std::size_t __bkt
= _M_bucket_index(__k
, __code
);
1865 // Look for the node before the first matching node.
1866 __node_base
* __prev_n
= _M_find_before_node(__bkt
, __k
, __code
);
1870 // We found a matching node, erase it.
1871 __node_type
* __n
= static_cast<__node_type
*>(__prev_n
->_M_nxt
);
1872 _M_erase(__bkt
, __prev_n
, __n
);
1876 template<typename _Key
, typename _Value
,
1877 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1878 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1880 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1881 _H1
, _H2
, _Hash
, _RehashPolicy
,
1883 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1884 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1885 _M_erase(std::false_type
, const key_type
& __k
)
1887 __hash_code __code
= this->_M_hash_code(__k
);
1888 std::size_t __bkt
= _M_bucket_index(__k
, __code
);
1890 // Look for the node before the first matching node.
1891 __node_base
* __prev_n
= _M_find_before_node(__bkt
, __k
, __code
);
1895 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1896 // 526. Is it undefined if a function in the standard changes
1898 // We use one loop to find all matching nodes and another to deallocate
1899 // them so that the key stays valid during the first loop. It might be
1900 // invalidated indirectly when destroying nodes.
1901 __node_type
* __n
= static_cast<__node_type
*>(__prev_n
->_M_nxt
);
1902 __node_type
* __n_last
= __n
;
1903 std::size_t __n_last_bkt
= __bkt
;
1906 __n_last
= __n_last
->_M_next();
1909 __n_last_bkt
= _M_bucket_index(__n_last
);
1911 while (__n_last_bkt
== __bkt
&& this->_M_equals(__k
, __code
, __n_last
));
1913 // Deallocate nodes.
1914 size_type __result
= 0;
1917 __node_type
* __p
= __n
->_M_next();
1918 _M_deallocate_node(__n
);
1923 while (__n
!= __n_last
);
1925 if (__prev_n
== _M_buckets
[__bkt
])
1926 _M_remove_bucket_begin(__bkt
, __n_last
, __n_last_bkt
);
1927 else if (__n_last
&& __n_last_bkt
!= __bkt
)
1928 _M_buckets
[__n_last_bkt
] = __prev_n
;
1929 __prev_n
->_M_nxt
= __n_last
;
1933 template<typename _Key
, typename _Value
,
1934 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1935 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1937 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1938 _H1
, _H2
, _Hash
, _RehashPolicy
,
1940 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1941 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1942 erase(const_iterator __first
, const_iterator __last
)
1944 __node_type
* __n
= __first
._M_cur
;
1945 __node_type
* __last_n
= __last
._M_cur
;
1946 if (__n
== __last_n
)
1947 return iterator(__n
);
1949 std::size_t __bkt
= _M_bucket_index(__n
);
1951 __node_base
* __prev_n
= _M_get_previous_node(__bkt
, __n
);
1952 bool __is_bucket_begin
= __n
== _M_bucket_begin(__bkt
);
1953 std::size_t __n_bkt
= __bkt
;
1958 __node_type
* __tmp
= __n
;
1959 __n
= __n
->_M_next();
1960 _M_deallocate_node(__tmp
);
1964 __n_bkt
= _M_bucket_index(__n
);
1966 while (__n
!= __last_n
&& __n_bkt
== __bkt
);
1967 if (__is_bucket_begin
)
1968 _M_remove_bucket_begin(__bkt
, __n
, __n_bkt
);
1969 if (__n
== __last_n
)
1971 __is_bucket_begin
= true;
1975 if (__n
&& (__n_bkt
!= __bkt
|| __is_bucket_begin
))
1976 _M_buckets
[__n_bkt
] = __prev_n
;
1977 __prev_n
->_M_nxt
= __n
;
1978 return iterator(__n
);
1981 template<typename _Key
, typename _Value
,
1982 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1983 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1986 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1987 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1990 _M_deallocate_nodes(_M_begin());
1991 __builtin_memset(_M_buckets
, 0, _M_bucket_count
* sizeof(__bucket_type
));
1992 _M_element_count
= 0;
1993 _M_before_begin()._M_nxt
= nullptr;
1996 template<typename _Key
, typename _Value
,
1997 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1998 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
2001 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
2002 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
2003 rehash(size_type __n
)
2005 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
2006 std::size_t __buckets
2007 = std::max(_M_rehash_policy
._M_bkt_for_elements(_M_element_count
+ 1),
2009 __buckets
= _M_rehash_policy
._M_next_bkt(__buckets
);
2011 if (__buckets
!= _M_bucket_count
)
2012 _M_rehash(__buckets
, __saved_state
);
2014 // No rehash, restore previous state to keep a consistent state.
2015 _M_rehash_policy
._M_reset(__saved_state
);
2018 template<typename _Key
, typename _Value
,
2019 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
2020 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
2023 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
2024 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
2025 _M_rehash(size_type __n
, const __rehash_state
& __state
)
2029 _M_rehash_aux(__n
, __unique_keys());
2033 // A failure here means that buckets allocation failed. We only
2034 // have to restore hash policy previous state.
2035 _M_rehash_policy
._M_reset(__state
);
2036 __throw_exception_again
;
2040 // Rehash when there is no equivalent elements.
2041 template<typename _Key
, typename _Value
,
2042 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
2043 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
2046 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
2047 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
2048 _M_rehash_aux(size_type __n
, std::true_type
)
2050 __bucket_type
* __new_buckets
= _M_allocate_buckets(__n
);
2051 __node_type
* __p
= _M_begin();
2052 _M_before_begin()._M_nxt
= nullptr;
2053 std::size_t __bbegin_bkt
= 0;
2056 __node_type
* __next
= __p
->_M_next();
2057 std::size_t __bkt
= __hash_code_base::_M_bucket_index(__p
, __n
);
2058 if (!__new_buckets
[__bkt
])
2060 __p
->_M_nxt
= _M_before_begin()._M_nxt
;
2061 _M_before_begin()._M_nxt
= __p
;
2062 __new_buckets
[__bkt
] = &_M_before_begin();
2064 __new_buckets
[__bbegin_bkt
] = __p
;
2065 __bbegin_bkt
= __bkt
;
2069 __p
->_M_nxt
= __new_buckets
[__bkt
]->_M_nxt
;
2070 __new_buckets
[__bkt
]->_M_nxt
= __p
;
2075 if (__builtin_expect(_M_bucket_count
!= 0, true))
2076 _M_deallocate_buckets();
2077 _M_bucket_count
= __n
;
2078 _M_buckets
= __new_buckets
;
2081 // Rehash when there can be equivalent elements, preserve their relative
2083 template<typename _Key
, typename _Value
,
2084 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
2085 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
2088 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
2089 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
2090 _M_rehash_aux(size_type __n
, std::false_type
)
2092 __bucket_type
* __new_buckets
= _M_allocate_buckets(__n
);
2094 __node_type
* __p
= _M_begin();
2095 _M_before_begin()._M_nxt
= nullptr;
2096 std::size_t __bbegin_bkt
= 0;
2097 std::size_t __prev_bkt
= 0;
2098 __node_type
* __prev_p
= nullptr;
2099 bool __check_bucket
= false;
2103 __node_type
* __next
= __p
->_M_next();
2104 std::size_t __bkt
= __hash_code_base::_M_bucket_index(__p
, __n
);
2106 if (__prev_p
&& __prev_bkt
== __bkt
)
2108 // Previous insert was already in this bucket, we insert after
2109 // the previously inserted one to preserve equivalent elements
2111 __p
->_M_nxt
= __prev_p
->_M_nxt
;
2112 __prev_p
->_M_nxt
= __p
;
2114 // Inserting after a node in a bucket require to check that we
2115 // haven't change the bucket last node, in this case next
2116 // bucket containing its before begin node must be updated. We
2117 // schedule a check as soon as we move out of the sequence of
2118 // equivalent nodes to limit the number of checks.
2119 __check_bucket
= true;
2125 // Check if we shall update the next bucket because of
2126 // insertions into __prev_bkt bucket.
2127 if (__prev_p
->_M_nxt
)
2129 std::size_t __next_bkt
2130 = __hash_code_base::_M_bucket_index(__prev_p
->_M_next(),
2132 if (__next_bkt
!= __prev_bkt
)
2133 __new_buckets
[__next_bkt
] = __prev_p
;
2135 __check_bucket
= false;
2138 if (!__new_buckets
[__bkt
])
2140 __p
->_M_nxt
= _M_before_begin()._M_nxt
;
2141 _M_before_begin()._M_nxt
= __p
;
2142 __new_buckets
[__bkt
] = &_M_before_begin();
2144 __new_buckets
[__bbegin_bkt
] = __p
;
2145 __bbegin_bkt
= __bkt
;
2149 __p
->_M_nxt
= __new_buckets
[__bkt
]->_M_nxt
;
2150 __new_buckets
[__bkt
]->_M_nxt
= __p
;
2158 if (__check_bucket
&& __prev_p
->_M_nxt
)
2160 std::size_t __next_bkt
2161 = __hash_code_base::_M_bucket_index(__prev_p
->_M_next(), __n
);
2162 if (__next_bkt
!= __prev_bkt
)
2163 __new_buckets
[__next_bkt
] = __prev_p
;
2166 if (__builtin_expect(_M_bucket_count
!= 0, true))
2167 _M_deallocate_buckets();
2168 _M_bucket_count
= __n
;
2169 _M_buckets
= __new_buckets
;
2172 _GLIBCXX_END_NAMESPACE_VERSION
2175 #endif // _HASHTABLE_H