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_before_begin
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
>,
185 private __detail::_Hashtable_alloc
<
186 typename __alloctr_rebind
<_Alloc
,
187 __detail::_Hash_node
<_Value
,
188 _Traits::__hash_cached::value
> >::__type
>
190 using __traits_type
= _Traits
;
191 using __hash_cached
= typename
__traits_type::__hash_cached
;
192 using __node_type
= __detail::_Hash_node
<_Value
, __hash_cached::value
>;
193 using __node_alloc_type
=
194 typename __alloctr_rebind
<_Alloc
, __node_type
>::__type
;
196 using __hashtable_alloc
= __detail::_Hashtable_alloc
<__node_alloc_type
>;
198 using __value_alloc_traits
=
199 typename
__hashtable_alloc::__value_alloc_traits
;
200 using __node_alloc_traits
=
201 typename
__hashtable_alloc::__node_alloc_traits
;
202 using __node_base
= typename
__hashtable_alloc::__node_base
;
203 using __bucket_type
= typename
__hashtable_alloc::__bucket_type
;
206 typedef _Key key_type
;
207 typedef _Value value_type
;
208 typedef _Alloc allocator_type
;
209 typedef _Equal key_equal
;
211 // mapped_type, if present, comes from _Map_base.
212 // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
213 typedef typename
__value_alloc_traits::pointer pointer
;
214 typedef typename
__value_alloc_traits::const_pointer const_pointer
;
215 typedef value_type
& reference
;
216 typedef const value_type
& const_reference
;
219 using __rehash_type
= _RehashPolicy
;
220 using __rehash_state
= typename
__rehash_type::_State
;
222 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
223 using __unique_keys
= typename
__traits_type::__unique_keys
;
225 using __key_extract
= typename
std::conditional
<
226 __constant_iterators::value
,
228 __detail::_Select1st
>::type
;
230 using __hashtable_base
= __detail::
231 _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
232 _Equal
, _H1
, _H2
, _Hash
, _Traits
>;
234 using __hash_code_base
= typename
__hashtable_base::__hash_code_base
;
235 using __hash_code
= typename
__hashtable_base::__hash_code
;
236 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
238 using __map_base
= __detail::_Map_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
239 _Equal
, _H1
, _H2
, _Hash
,
240 _RehashPolicy
, _Traits
>;
242 using __rehash_base
= __detail::_Rehash_base
<_Key
, _Value
, _Alloc
,
245 _RehashPolicy
, _Traits
>;
247 using __eq_base
= __detail::_Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
,
248 _Equal
, _H1
, _H2
, _Hash
,
249 _RehashPolicy
, _Traits
>;
251 using __reuse_or_alloc_node_type
=
252 __detail::_ReuseOrAllocNode
<__node_alloc_type
>;
254 // Metaprogramming for picking apart hash caching.
255 template<typename _Cond
>
256 using __if_hash_cached
= __or_
<__not_
<__hash_cached
>, _Cond
>;
258 template<typename _Cond
>
259 using __if_hash_not_cached
= __or_
<__hash_cached
, _Cond
>;
261 // Compile-time diagnostics.
263 // Getting a bucket index from a node shall not throw because it is used
264 // in methods (erase, swap...) that shall not throw.
265 static_assert(noexcept(declval
<const _Hashtable
&>()
266 ._M_bucket_index((const __node_type
*)nullptr,
268 "Cache the hash code or qualify your functors involved"
269 " in hash code and bucket index computation with noexcept");
271 // Following two static assertions are necessary to guarantee
272 // that local_iterator will be default constructible.
274 // When hash codes are cached local iterator inherits from H2 functor
275 // which must then be default constructible.
276 static_assert(__if_hash_cached
<is_default_constructible
<_H2
>>::value
,
277 "Functor used to map hash code to bucket index"
278 " must be 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
282 // default constructible.
283 static_assert(__if_hash_not_cached
<
284 is_default_constructible
<
285 // We use _Hashtable_ebo_helper to access the protected
286 // default constructor.
287 __detail::_Hashtable_ebo_helper
<0, __hash_code_base
>>>::value
,
288 "Cache the hash code or make functors involved in hash code"
289 " and bucket index computation default constructible");
291 // When hash codes are not cached local iterator inherits from
292 // __hash_code_base above to compute node bucket index so it has to be
294 static_assert(__if_hash_not_cached
<
295 is_copy_assignable
<__hash_code_base
>>::value
,
296 "Cache the hash code or make functors involved in hash code"
297 " and bucket index computation copy assignable");
299 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
300 typename _ExtractKeya
, typename _Equala
,
301 typename _H1a
, typename _H2a
, typename _Hasha
,
302 typename _RehashPolicya
, typename _Traitsa
,
304 friend struct __detail::_Map_base
;
306 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
307 typename _ExtractKeya
, typename _Equala
,
308 typename _H1a
, typename _H2a
, typename _Hasha
,
309 typename _RehashPolicya
, typename _Traitsa
>
310 friend struct __detail::_Insert_base
;
312 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
313 typename _ExtractKeya
, typename _Equala
,
314 typename _H1a
, typename _H2a
, typename _Hasha
,
315 typename _RehashPolicya
, typename _Traitsa
,
316 bool _Constant_iteratorsa
, bool _Unique_keysa
>
317 friend struct __detail::_Insert
;
320 using size_type
= typename
__hashtable_base::size_type
;
321 using difference_type
= typename
__hashtable_base::difference_type
;
323 using iterator
= typename
__hashtable_base::iterator
;
324 using const_iterator
= typename
__hashtable_base::const_iterator
;
326 using local_iterator
= typename
__hashtable_base::local_iterator
;
327 using const_local_iterator
= typename
__hashtable_base::
328 const_local_iterator
;
331 __bucket_type
* _M_buckets
;
332 size_type _M_bucket_count
;
333 __node_base _M_before_begin
;
334 size_type _M_element_count
;
335 _RehashPolicy _M_rehash_policy
;
338 _M_base_alloc() { return *this; }
340 using __hashtable_alloc::_M_deallocate_buckets
;
343 _M_deallocate_buckets()
344 { this->_M_deallocate_buckets(_M_buckets
, _M_bucket_count
); }
346 // Gets bucket begin, deals with the fact that non-empty buckets contain
347 // their before begin node.
349 _M_bucket_begin(size_type __bkt
) const;
353 { return static_cast<__node_type
*>(_M_before_begin
._M_nxt
); }
355 template<typename _NodeGenerator
>
357 _M_assign(const _Hashtable
&, const _NodeGenerator
&);
360 _M_move_assign(_Hashtable
&&, std::true_type
);
363 _M_move_assign(_Hashtable
&&, std::false_type
);
369 // Constructor, destructor, assignment, swap
370 _Hashtable(size_type __bucket_hint
,
371 const _H1
&, const _H2
&, const _Hash
&,
372 const _Equal
&, const _ExtractKey
&,
373 const allocator_type
&);
375 template<typename _InputIterator
>
376 _Hashtable(_InputIterator __first
, _InputIterator __last
,
377 size_type __bucket_hint
,
378 const _H1
&, const _H2
&, const _Hash
&,
379 const _Equal
&, const _ExtractKey
&,
380 const allocator_type
&);
382 _Hashtable(const _Hashtable
&);
384 _Hashtable(_Hashtable
&&) noexcept
;
386 _Hashtable(const _Hashtable
&, const allocator_type
&);
388 _Hashtable(_Hashtable
&&, const allocator_type
&);
390 // Use delegating constructors.
392 _Hashtable(const allocator_type
& __a
)
393 : _Hashtable(10, _H1(), __detail::_Mod_range_hashing(),
394 __detail::_Default_ranged_hash(), key_equal(),
395 __key_extract(), __a
)
399 _Hashtable(size_type __n
= 10,
400 const _H1
& __hf
= _H1(),
401 const key_equal
& __eql
= key_equal(),
402 const allocator_type
& __a
= allocator_type())
403 : _Hashtable(__n
, __hf
, __detail::_Mod_range_hashing(),
404 __detail::_Default_ranged_hash(), __eql
,
405 __key_extract(), __a
)
408 template<typename _InputIterator
>
409 _Hashtable(_InputIterator __f
, _InputIterator __l
,
411 const _H1
& __hf
= _H1(),
412 const key_equal
& __eql
= key_equal(),
413 const allocator_type
& __a
= allocator_type())
414 : _Hashtable(__f
, __l
, __n
, __hf
, __detail::_Mod_range_hashing(),
415 __detail::_Default_ranged_hash(), __eql
,
416 __key_extract(), __a
)
419 _Hashtable(initializer_list
<value_type
> __l
,
421 const _H1
& __hf
= _H1(),
422 const key_equal
& __eql
= key_equal(),
423 const allocator_type
& __a
= allocator_type())
424 : _Hashtable(__l
.begin(), __l
.end(), __n
, __hf
,
425 __detail::_Mod_range_hashing(),
426 __detail::_Default_ranged_hash(), __eql
,
427 __key_extract(), __a
)
431 operator=(const _Hashtable
& __ht
);
434 operator=(_Hashtable
&& __ht
)
435 noexcept(__node_alloc_traits::_S_nothrow_move())
437 constexpr bool __move_storage
=
438 __node_alloc_traits::_S_propagate_on_move_assign()
439 || __node_alloc_traits::_S_always_equal();
440 _M_move_assign(std::move(__ht
),
441 integral_constant
<bool, __move_storage
>());
446 operator=(initializer_list
<value_type
> __l
)
448 __reuse_or_alloc_node_type
__roan(_M_begin(), *this);
449 _M_before_begin
._M_nxt
= nullptr;
451 this->_M_insert_range(__l
.begin(), __l
.end(), __roan
);
455 ~_Hashtable() noexcept
;
459 noexcept(__node_alloc_traits::_S_nothrow_swap());
461 // Basic container operations
464 { return iterator(_M_begin()); }
467 begin() const noexcept
468 { return const_iterator(_M_begin()); }
472 { return iterator(nullptr); }
476 { return const_iterator(nullptr); }
479 cbegin() const noexcept
480 { return const_iterator(_M_begin()); }
483 cend() const noexcept
484 { return const_iterator(nullptr); }
487 size() const noexcept
488 { return _M_element_count
; }
491 empty() const noexcept
492 { return size() == 0; }
495 get_allocator() const noexcept
496 { return allocator_type(this->_M_node_allocator()); }
499 max_size() const noexcept
500 { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
505 { return this->_M_eq(); }
507 // hash_function, if present, comes from _Hash_code_base.
511 bucket_count() const noexcept
512 { return _M_bucket_count
; }
515 max_bucket_count() const noexcept
516 { return max_size(); }
519 bucket_size(size_type __n
) const
520 { return std::distance(begin(__n
), end(__n
)); }
523 bucket(const key_type
& __k
) const
524 { return _M_bucket_index(__k
, this->_M_hash_code(__k
)); }
529 return local_iterator(*this, _M_bucket_begin(__n
),
530 __n
, _M_bucket_count
);
535 { return local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
538 begin(size_type __n
) const
540 return const_local_iterator(*this, _M_bucket_begin(__n
),
541 __n
, _M_bucket_count
);
545 end(size_type __n
) const
546 { return const_local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
550 cbegin(size_type __n
) const
552 return const_local_iterator(*this, _M_bucket_begin(__n
),
553 __n
, _M_bucket_count
);
557 cend(size_type __n
) const
558 { return const_local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
561 load_factor() const noexcept
563 return static_cast<float>(size()) / static_cast<float>(bucket_count());
566 // max_load_factor, if present, comes from _Rehash_base.
568 // Generalization of max_load_factor. Extension, not found in
569 // TR1. Only useful if _RehashPolicy is something other than
572 __rehash_policy() const
573 { return _M_rehash_policy
; }
576 __rehash_policy(const _RehashPolicy
&);
580 find(const key_type
& __k
);
583 find(const key_type
& __k
) const;
586 count(const key_type
& __k
) const;
588 std::pair
<iterator
, iterator
>
589 equal_range(const key_type
& __k
);
591 std::pair
<const_iterator
, const_iterator
>
592 equal_range(const key_type
& __k
) const;
595 // Bucket index computation helpers.
597 _M_bucket_index(__node_type
* __n
) const noexcept
598 { return __hash_code_base::_M_bucket_index(__n
, _M_bucket_count
); }
601 _M_bucket_index(const key_type
& __k
, __hash_code __c
) const
602 { return __hash_code_base::_M_bucket_index(__k
, __c
, _M_bucket_count
); }
604 // Find and insert helper functions and types
605 // Find the node before the one matching the criteria.
607 _M_find_before_node(size_type
, const key_type
&, __hash_code
) const;
610 _M_find_node(size_type __bkt
, const key_type
& __key
,
611 __hash_code __c
) const
613 __node_base
* __before_n
= _M_find_before_node(__bkt
, __key
, __c
);
615 return static_cast<__node_type
*>(__before_n
->_M_nxt
);
619 // Insert a node at the beginning of a bucket.
621 _M_insert_bucket_begin(size_type
, __node_type
*);
623 // Remove the bucket first node
625 _M_remove_bucket_begin(size_type __bkt
, __node_type
* __next_n
,
626 size_type __next_bkt
);
628 // Get the node before __n in the bucket __bkt
630 _M_get_previous_node(size_type __bkt
, __node_base
* __n
);
632 // Insert node with hash code __code, in bucket bkt if no rehash (assumes
633 // no element with its key already present). Take ownership of the node,
634 // deallocate it on exception.
636 _M_insert_unique_node(size_type __bkt
, __hash_code __code
,
639 // Insert node with hash code __code. Take ownership of the node,
640 // deallocate it on exception.
642 _M_insert_multi_node(__node_type
* __hint
,
643 __hash_code __code
, __node_type
* __n
);
645 template<typename
... _Args
>
646 std::pair
<iterator
, bool>
647 _M_emplace(std::true_type
, _Args
&&... __args
);
649 template<typename
... _Args
>
651 _M_emplace(std::false_type __uk
, _Args
&&... __args
)
652 { return _M_emplace(cend(), __uk
, std::forward
<_Args
>(__args
)...); }
654 // Emplace with hint, useless when keys are unique.
655 template<typename
... _Args
>
657 _M_emplace(const_iterator
, std::true_type __uk
, _Args
&&... __args
)
658 { return _M_emplace(__uk
, std::forward
<_Args
>(__args
)...).first
; }
660 template<typename
... _Args
>
662 _M_emplace(const_iterator
, std::false_type
, _Args
&&... __args
);
664 template<typename _Arg
, typename _NodeGenerator
>
665 std::pair
<iterator
, bool>
666 _M_insert(_Arg
&&, const _NodeGenerator
&, std::true_type
);
668 template<typename _Arg
, typename _NodeGenerator
>
670 _M_insert(_Arg
&& __arg
, const _NodeGenerator
& __node_gen
,
671 std::false_type __uk
)
673 return _M_insert(cend(), std::forward
<_Arg
>(__arg
), __node_gen
,
677 // Insert with hint, not used when keys are unique.
678 template<typename _Arg
, typename _NodeGenerator
>
680 _M_insert(const_iterator
, _Arg
&& __arg
, const _NodeGenerator
& __node_gen
,
684 _M_insert(std::forward
<_Arg
>(__arg
), __node_gen
, __uk
).first
;
687 // Insert with hint when keys are not unique.
688 template<typename _Arg
, typename _NodeGenerator
>
690 _M_insert(const_iterator
, _Arg
&&, const _NodeGenerator
&, std::false_type
);
693 _M_erase(std::true_type
, const key_type
&);
696 _M_erase(std::false_type
, const key_type
&);
699 _M_erase(size_type __bkt
, __node_base
* __prev_n
, __node_type
* __n
);
703 template<typename
... _Args
>
705 emplace(_Args
&&... __args
)
706 { return _M_emplace(__unique_keys(), std::forward
<_Args
>(__args
)...); }
708 template<typename
... _Args
>
710 emplace_hint(const_iterator __hint
, _Args
&&... __args
)
712 return _M_emplace(__hint
, __unique_keys(),
713 std::forward
<_Args
>(__args
)...);
716 // Insert member functions via inheritance.
720 erase(const_iterator
);
725 { return erase(const_iterator(__it
)); }
728 erase(const key_type
& __k
)
730 if (__builtin_expect(_M_bucket_count
== 0, false))
732 return _M_erase(__unique_keys(), __k
);
736 erase(const_iterator
, const_iterator
);
741 // Set number of buckets to be appropriate for container of n element.
742 void rehash(size_type __n
);
745 // reserve, if present, comes from _Rehash_base.
748 // Helper rehash method used when keys are unique.
749 void _M_rehash_aux(size_type __n
, std::true_type
);
751 // Helper rehash method used when keys can be non-unique.
752 void _M_rehash_aux(size_type __n
, std::false_type
);
754 // Unconditionally change size of bucket array to n, restore
755 // hash policy state to __state on exception.
756 void _M_rehash(size_type __n
, const __rehash_state
& __state
);
760 // Definitions of class template _Hashtable's out-of-line member functions.
761 template<typename _Key
, typename _Value
,
762 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
763 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
765 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
766 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
767 _Traits
>::__node_type
*
768 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
769 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
770 _M_bucket_begin(size_type __bkt
) const
772 __node_base
* __n
= _M_buckets
[__bkt
];
773 return __n
? static_cast<__node_type
*>(__n
->_M_nxt
) : nullptr;
776 template<typename _Key
, typename _Value
,
777 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
778 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
780 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
781 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
782 _Hashtable(size_type __bucket_hint
,
783 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
784 const _Equal
& __eq
, const _ExtractKey
& __exk
,
785 const allocator_type
& __a
)
786 : __hashtable_base(__exk
, __h1
, __h2
, __h
, __eq
),
789 __hashtable_alloc(__node_alloc_type(__a
)),
793 _M_bucket_count
= _M_rehash_policy
._M_next_bkt(__bucket_hint
);
794 _M_buckets
= this->_M_allocate_buckets(_M_bucket_count
);
797 template<typename _Key
, typename _Value
,
798 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
799 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
801 template<typename _InputIterator
>
802 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
803 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
804 _Hashtable(_InputIterator __f
, _InputIterator __l
,
805 size_type __bucket_hint
,
806 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
807 const _Equal
& __eq
, const _ExtractKey
& __exk
,
808 const allocator_type
& __a
)
809 : __hashtable_base(__exk
, __h1
, __h2
, __h
, __eq
),
812 __hashtable_alloc(__node_alloc_type(__a
)),
816 auto __nb_elems
= __detail::__distance_fw(__f
, __l
);
818 _M_rehash_policy
._M_next_bkt(
819 std::max(_M_rehash_policy
._M_bkt_for_elements(__nb_elems
),
822 _M_buckets
= this->_M_allocate_buckets(_M_bucket_count
);
825 for (; __f
!= __l
; ++__f
)
831 _M_deallocate_buckets();
832 __throw_exception_again
;
836 template<typename _Key
, typename _Value
,
837 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
838 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
840 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
841 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>&
842 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
843 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::operator=(
844 const _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
845 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>& __ht
)
850 if (__node_alloc_traits::_S_propagate_on_copy_assign())
852 auto& __this_alloc
= this->_M_node_allocator();
853 auto& __that_alloc
= __ht
._M_node_allocator();
854 if (!__node_alloc_traits::_S_always_equal()
855 && __this_alloc
!= __that_alloc
)
857 // Replacement allocator cannot free existing storage.
858 this->_M_deallocate_nodes(_M_begin());
859 if (__builtin_expect(_M_bucket_count
!= 0, true))
860 _M_deallocate_buckets();
862 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
863 __hashtable_base::operator=(__ht
);
864 _M_bucket_count
= __ht
._M_bucket_count
;
865 _M_element_count
= __ht
._M_element_count
;
866 _M_rehash_policy
= __ht
._M_rehash_policy
;
870 [this](const __node_type
* __n
)
871 { return this->_M_allocate_node(__n
->_M_v()); });
875 // _M_assign took care of deallocating all memory. Now we
876 // must make sure this instance remains in a usable state.
878 __throw_exception_again
;
882 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
885 // Reuse allocated buckets and nodes.
886 __bucket_type
* __former_buckets
= nullptr;
887 std::size_t __former_bucket_count
= _M_bucket_count
;
888 const __rehash_state
& __former_state
= _M_rehash_policy
._M_state();
890 if (_M_bucket_count
!= __ht
._M_bucket_count
)
892 __former_buckets
= _M_buckets
;
893 _M_buckets
= this->_M_allocate_buckets(__ht
._M_bucket_count
);
894 _M_bucket_count
= __ht
._M_bucket_count
;
897 __builtin_memset(_M_buckets
, 0,
898 _M_bucket_count
* sizeof(__bucket_type
));
902 __hashtable_base::operator=(__ht
);
903 _M_element_count
= __ht
._M_element_count
;
904 _M_rehash_policy
= __ht
._M_rehash_policy
;
905 __reuse_or_alloc_node_type
__roan(_M_begin(), *this);
906 _M_before_begin
._M_nxt
= nullptr;
908 [&__roan
](const __node_type
* __n
)
909 { return __roan(__n
->_M_v()); });
910 if (__former_buckets
)
911 this->_M_deallocate_buckets(__former_buckets
,
912 __former_bucket_count
);
916 if (__former_buckets
)
918 // Restore previous buckets.
919 _M_deallocate_buckets();
920 _M_rehash_policy
._M_reset(__former_state
);
921 _M_buckets
= __former_buckets
;
922 _M_bucket_count
= __former_bucket_count
;
924 __builtin_memset(_M_buckets
, 0,
925 _M_bucket_count
* sizeof(__bucket_type
));
926 __throw_exception_again
;
931 template<typename _Key
, typename _Value
,
932 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
933 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
935 template<typename _NodeGenerator
>
937 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
938 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
939 _M_assign(const _Hashtable
& __ht
, const _NodeGenerator
& __node_gen
)
941 __bucket_type
* __buckets
= nullptr;
943 _M_buckets
= __buckets
= this->_M_allocate_buckets(_M_bucket_count
);
947 if (!__ht
._M_before_begin
._M_nxt
)
950 // First deal with the special first node pointed to by
952 __node_type
* __ht_n
= __ht
._M_begin();
953 __node_type
* __this_n
= __node_gen(__ht_n
);
954 this->_M_copy_code(__this_n
, __ht_n
);
955 _M_before_begin
._M_nxt
= __this_n
;
956 _M_buckets
[_M_bucket_index(__this_n
)] = &_M_before_begin
;
958 // Then deal with other nodes.
959 __node_base
* __prev_n
= __this_n
;
960 for (__ht_n
= __ht_n
->_M_next(); __ht_n
; __ht_n
= __ht_n
->_M_next())
962 __this_n
= __node_gen(__ht_n
);
963 __prev_n
->_M_nxt
= __this_n
;
964 this->_M_copy_code(__this_n
, __ht_n
);
965 size_type __bkt
= _M_bucket_index(__this_n
);
966 if (!_M_buckets
[__bkt
])
967 _M_buckets
[__bkt
] = __prev_n
;
975 _M_deallocate_buckets();
976 __throw_exception_again
;
980 template<typename _Key
, typename _Value
,
981 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
982 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
985 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
986 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
989 _M_rehash_policy
._M_reset();
991 _M_buckets
= nullptr;
992 _M_before_begin
._M_nxt
= nullptr;
993 _M_element_count
= 0;
996 template<typename _Key
, typename _Value
,
997 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
998 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1001 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1002 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1003 _M_move_assign(_Hashtable
&& __ht
, std::true_type
)
1005 this->_M_deallocate_nodes(_M_begin());
1006 if (__builtin_expect(_M_bucket_count
!= 0, true))
1007 _M_deallocate_buckets();
1009 __hashtable_base::operator=(std::move(__ht
));
1010 _M_rehash_policy
= __ht
._M_rehash_policy
;
1011 _M_buckets
= __ht
._M_buckets
;
1012 _M_bucket_count
= __ht
._M_bucket_count
;
1013 _M_before_begin
._M_nxt
= __ht
._M_before_begin
._M_nxt
;
1014 _M_element_count
= __ht
._M_element_count
;
1015 std::__alloc_on_move(this->_M_node_allocator(), __ht
._M_node_allocator());
1017 // Fix buckets containing the _M_before_begin pointers that can't be
1020 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1024 template<typename _Key
, typename _Value
,
1025 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1026 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1029 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1030 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1031 _M_move_assign(_Hashtable
&& __ht
, std::false_type
)
1033 if (__ht
._M_node_allocator() == this->_M_node_allocator())
1034 _M_move_assign(std::move(__ht
), std::true_type());
1037 // Can't move memory, move elements then.
1038 __bucket_type
* __former_buckets
= nullptr;
1039 size_type __former_bucket_count
= _M_bucket_count
;
1040 const __rehash_state
& __former_state
= _M_rehash_policy
._M_state();
1042 if (_M_bucket_count
!= __ht
._M_bucket_count
)
1044 __former_buckets
= _M_buckets
;
1045 _M_buckets
= this->_M_allocate_buckets(__ht
._M_bucket_count
);
1046 _M_bucket_count
= __ht
._M_bucket_count
;
1049 __builtin_memset(_M_buckets
, 0,
1050 _M_bucket_count
* sizeof(__bucket_type
));
1054 __hashtable_base::operator=(std::move(__ht
));
1055 _M_element_count
= __ht
._M_element_count
;
1056 _M_rehash_policy
= __ht
._M_rehash_policy
;
1057 __reuse_or_alloc_node_type
__roan(_M_begin(), *this);
1058 _M_before_begin
._M_nxt
= nullptr;
1060 [&__roan
](__node_type
* __n
)
1061 { return __roan(std::move_if_noexcept(__n
->_M_v())); });
1066 if (__former_buckets
)
1068 _M_deallocate_buckets();
1069 _M_rehash_policy
._M_reset(__former_state
);
1070 _M_buckets
= __former_buckets
;
1071 _M_bucket_count
= __former_bucket_count
;
1073 __builtin_memset(_M_buckets
, 0,
1074 _M_bucket_count
* sizeof(__bucket_type
));
1075 __throw_exception_again
;
1080 template<typename _Key
, typename _Value
,
1081 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1082 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1084 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1085 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1086 _Hashtable(const _Hashtable
& __ht
)
1087 : __hashtable_base(__ht
),
1089 __rehash_base(__ht
),
1091 __node_alloc_traits::_S_select_on_copy(__ht
._M_node_allocator())),
1093 _M_bucket_count(__ht
._M_bucket_count
),
1094 _M_element_count(__ht
._M_element_count
),
1095 _M_rehash_policy(__ht
._M_rehash_policy
)
1098 [this](const __node_type
* __n
)
1099 { return this->_M_allocate_node(__n
->_M_v()); });
1102 template<typename _Key
, typename _Value
,
1103 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1104 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1106 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1107 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1108 _Hashtable(_Hashtable
&& __ht
) noexcept
1109 : __hashtable_base(__ht
),
1111 __rehash_base(__ht
),
1112 __hashtable_alloc(std::move(__ht
._M_base_alloc())),
1113 _M_buckets(__ht
._M_buckets
),
1114 _M_bucket_count(__ht
._M_bucket_count
),
1115 _M_before_begin(__ht
._M_before_begin
._M_nxt
),
1116 _M_element_count(__ht
._M_element_count
),
1117 _M_rehash_policy(__ht
._M_rehash_policy
)
1119 // Update, if necessary, bucket pointing to before begin that hasn't
1122 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1126 template<typename _Key
, typename _Value
,
1127 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1128 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1130 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1131 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1132 _Hashtable(const _Hashtable
& __ht
, const allocator_type
& __a
)
1133 : __hashtable_base(__ht
),
1135 __rehash_base(__ht
),
1136 __hashtable_alloc(__node_alloc_type(__a
)),
1138 _M_bucket_count(__ht
._M_bucket_count
),
1139 _M_element_count(__ht
._M_element_count
),
1140 _M_rehash_policy(__ht
._M_rehash_policy
)
1143 [this](const __node_type
* __n
)
1144 { return this->_M_allocate_node(__n
->_M_v()); });
1147 template<typename _Key
, typename _Value
,
1148 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1149 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1151 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1152 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1153 _Hashtable(_Hashtable
&& __ht
, const allocator_type
& __a
)
1154 : __hashtable_base(__ht
),
1156 __rehash_base(__ht
),
1157 __hashtable_alloc(__node_alloc_type(__a
)),
1159 _M_bucket_count(__ht
._M_bucket_count
),
1160 _M_element_count(__ht
._M_element_count
),
1161 _M_rehash_policy(__ht
._M_rehash_policy
)
1163 if (__ht
._M_node_allocator() == this->_M_node_allocator())
1165 _M_buckets
= __ht
._M_buckets
;
1166 _M_before_begin
._M_nxt
= __ht
._M_before_begin
._M_nxt
;
1167 // Update, if necessary, bucket pointing to before begin that hasn't
1170 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1176 [this](__node_type
* __n
)
1178 return this->_M_allocate_node(
1179 std::move_if_noexcept(__n
->_M_v()));
1185 template<typename _Key
, typename _Value
,
1186 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1187 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1189 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1190 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1191 ~_Hashtable() noexcept
1195 _M_deallocate_buckets();
1198 template<typename _Key
, typename _Value
,
1199 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1200 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1203 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1204 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1205 swap(_Hashtable
& __x
)
1206 noexcept(__node_alloc_traits::_S_nothrow_swap())
1208 // The only base class with member variables is hash_code_base.
1209 // We define _Hash_code_base::_M_swap because different
1210 // specializations have different members.
1213 std::__alloc_on_swap(this->_M_node_allocator(), __x
._M_node_allocator());
1214 std::swap(_M_rehash_policy
, __x
._M_rehash_policy
);
1215 std::swap(_M_buckets
, __x
._M_buckets
);
1216 std::swap(_M_bucket_count
, __x
._M_bucket_count
);
1217 std::swap(_M_before_begin
._M_nxt
, __x
._M_before_begin
._M_nxt
);
1218 std::swap(_M_element_count
, __x
._M_element_count
);
1220 // Fix buckets containing the _M_before_begin pointers that can't be
1223 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1225 __x
._M_buckets
[__x
._M_bucket_index(__x
._M_begin())]
1226 = &__x
._M_before_begin
;
1229 template<typename _Key
, typename _Value
,
1230 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1231 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1234 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1235 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1236 __rehash_policy(const _RehashPolicy
& __pol
)
1238 size_type __n_bkt
= __pol
._M_bkt_for_elements(_M_element_count
);
1239 __n_bkt
= __pol
._M_next_bkt(__n_bkt
);
1240 if (__n_bkt
!= _M_bucket_count
)
1241 _M_rehash(__n_bkt
, _M_rehash_policy
._M_state());
1242 _M_rehash_policy
= __pol
;
1245 template<typename _Key
, typename _Value
,
1246 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1247 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1249 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1250 _H1
, _H2
, _Hash
, _RehashPolicy
,
1252 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1253 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1254 find(const key_type
& __k
)
1256 if (__builtin_expect(_M_bucket_count
== 0, false))
1259 __hash_code __code
= this->_M_hash_code(__k
);
1260 std::size_t __n
= _M_bucket_index(__k
, __code
);
1261 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1262 return __p
? iterator(__p
) : end();
1265 template<typename _Key
, typename _Value
,
1266 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1267 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1269 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1270 _H1
, _H2
, _Hash
, _RehashPolicy
,
1271 _Traits
>::const_iterator
1272 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1273 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1274 find(const key_type
& __k
) const
1276 if (__builtin_expect(_M_bucket_count
== 0, false))
1279 __hash_code __code
= this->_M_hash_code(__k
);
1280 std::size_t __n
= _M_bucket_index(__k
, __code
);
1281 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1282 return __p
? const_iterator(__p
) : end();
1285 template<typename _Key
, typename _Value
,
1286 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1287 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1289 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1290 _H1
, _H2
, _Hash
, _RehashPolicy
,
1292 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1293 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1294 count(const key_type
& __k
) const
1296 if (__builtin_expect(_M_bucket_count
== 0, false))
1299 __hash_code __code
= this->_M_hash_code(__k
);
1300 std::size_t __n
= _M_bucket_index(__k
, __code
);
1301 __node_type
* __p
= _M_bucket_begin(__n
);
1305 std::size_t __result
= 0;
1306 for (;; __p
= __p
->_M_next())
1308 if (this->_M_equals(__k
, __code
, __p
))
1311 // All equivalent values are next to each other, if we
1312 // found a non-equivalent value after an equivalent one it
1313 // means that we won't find any more equivalent values.
1315 if (!__p
->_M_nxt
|| _M_bucket_index(__p
->_M_next()) != __n
)
1321 template<typename _Key
, typename _Value
,
1322 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1323 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1325 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1326 _ExtractKey
, _Equal
, _H1
,
1327 _H2
, _Hash
, _RehashPolicy
,
1329 typename _Hashtable
<_Key
, _Value
, _Alloc
,
1330 _ExtractKey
, _Equal
, _H1
,
1331 _H2
, _Hash
, _RehashPolicy
,
1333 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1334 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1335 equal_range(const key_type
& __k
)
1337 if (__builtin_expect(_M_bucket_count
== 0, false))
1338 return std::make_pair(end(), end());
1340 __hash_code __code
= this->_M_hash_code(__k
);
1341 std::size_t __n
= _M_bucket_index(__k
, __code
);
1342 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1346 __node_type
* __p1
= __p
->_M_next();
1347 while (__p1
&& _M_bucket_index(__p1
) == __n
1348 && this->_M_equals(__k
, __code
, __p1
))
1349 __p1
= __p1
->_M_next();
1351 return std::make_pair(iterator(__p
), iterator(__p1
));
1354 return std::make_pair(end(), end());
1357 template<typename _Key
, typename _Value
,
1358 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1359 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1361 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1362 _ExtractKey
, _Equal
, _H1
,
1363 _H2
, _Hash
, _RehashPolicy
,
1364 _Traits
>::const_iterator
,
1365 typename _Hashtable
<_Key
, _Value
, _Alloc
,
1366 _ExtractKey
, _Equal
, _H1
,
1367 _H2
, _Hash
, _RehashPolicy
,
1368 _Traits
>::const_iterator
>
1369 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1370 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1371 equal_range(const key_type
& __k
) const
1373 if (__builtin_expect(_M_bucket_count
== 0, false))
1374 return std::make_pair(end(), end());
1376 __hash_code __code
= this->_M_hash_code(__k
);
1377 std::size_t __n
= _M_bucket_index(__k
, __code
);
1378 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1382 __node_type
* __p1
= __p
->_M_next();
1383 while (__p1
&& _M_bucket_index(__p1
) == __n
1384 && this->_M_equals(__k
, __code
, __p1
))
1385 __p1
= __p1
->_M_next();
1387 return std::make_pair(const_iterator(__p
), const_iterator(__p1
));
1390 return std::make_pair(end(), end());
1393 // Find the node whose key compares equal to k in the bucket n.
1394 // Return nullptr if no node is found.
1395 template<typename _Key
, typename _Value
,
1396 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1397 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1399 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1400 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
1401 _Traits
>::__node_base
*
1402 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1403 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1404 _M_find_before_node(size_type __n
, const key_type
& __k
,
1405 __hash_code __code
) const
1407 __node_base
* __prev_p
= _M_buckets
[__n
];
1411 for (__node_type
* __p
= static_cast<__node_type
*>(__prev_p
->_M_nxt
);;
1412 __p
= __p
->_M_next())
1414 if (this->_M_equals(__k
, __code
, __p
))
1417 if (!__p
->_M_nxt
|| _M_bucket_index(__p
->_M_next()) != __n
)
1424 template<typename _Key
, typename _Value
,
1425 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1426 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1429 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1430 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1431 _M_insert_bucket_begin(size_type __bkt
, __node_type
* __node
)
1433 if (_M_buckets
[__bkt
])
1435 // Bucket is not empty, we just need to insert the new node
1436 // after the bucket before begin.
1437 __node
->_M_nxt
= _M_buckets
[__bkt
]->_M_nxt
;
1438 _M_buckets
[__bkt
]->_M_nxt
= __node
;
1442 // The bucket is empty, the new node is inserted at the
1443 // beginning of the singly-linked list and the bucket will
1444 // contain _M_before_begin pointer.
1445 __node
->_M_nxt
= _M_before_begin
._M_nxt
;
1446 _M_before_begin
._M_nxt
= __node
;
1448 // We must update former begin bucket that is pointing to
1450 _M_buckets
[_M_bucket_index(__node
->_M_next())] = __node
;
1451 _M_buckets
[__bkt
] = &_M_before_begin
;
1455 template<typename _Key
, typename _Value
,
1456 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1457 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1460 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1461 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1462 _M_remove_bucket_begin(size_type __bkt
, __node_type
* __next
,
1463 size_type __next_bkt
)
1465 if (!__next
|| __next_bkt
!= __bkt
)
1467 // Bucket is now empty
1468 // First update next bucket if any
1470 _M_buckets
[__next_bkt
] = _M_buckets
[__bkt
];
1472 // Second update before begin node if necessary
1473 if (&_M_before_begin
== _M_buckets
[__bkt
])
1474 _M_before_begin
._M_nxt
= __next
;
1475 _M_buckets
[__bkt
] = nullptr;
1479 template<typename _Key
, typename _Value
,
1480 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1481 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1483 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1484 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
1485 _Traits
>::__node_base
*
1486 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1487 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1488 _M_get_previous_node(size_type __bkt
, __node_base
* __n
)
1490 __node_base
* __prev_n
= _M_buckets
[__bkt
];
1491 while (__prev_n
->_M_nxt
!= __n
)
1492 __prev_n
= __prev_n
->_M_nxt
;
1496 template<typename _Key
, typename _Value
,
1497 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1498 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1500 template<typename
... _Args
>
1501 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1502 _ExtractKey
, _Equal
, _H1
,
1503 _H2
, _Hash
, _RehashPolicy
,
1504 _Traits
>::iterator
, bool>
1505 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1506 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1507 _M_emplace(std::true_type
, _Args
&&... __args
)
1509 // First build the node to get access to the hash code
1510 __node_type
* __node
= this->_M_allocate_node(std::forward
<_Args
>(__args
)...);
1511 const key_type
& __k
= this->_M_extract()(__node
->_M_v());
1515 __code
= this->_M_hash_code(__k
);
1519 this->_M_deallocate_node(__node
);
1520 __throw_exception_again
;
1523 size_type __bkt
= _M_bucket_index(__k
, __code
);
1524 if (__node_type
* __p
= _M_find_node(__bkt
, __k
, __code
))
1526 // There is already an equivalent node, no insertion
1527 this->_M_deallocate_node(__node
);
1528 return std::make_pair(iterator(__p
), false);
1532 return std::make_pair(_M_insert_unique_node(__bkt
, __code
, __node
),
1536 template<typename _Key
, typename _Value
,
1537 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1538 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1540 template<typename
... _Args
>
1541 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1542 _H1
, _H2
, _Hash
, _RehashPolicy
,
1544 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1545 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1546 _M_emplace(const_iterator __hint
, std::false_type
, _Args
&&... __args
)
1548 // First build the node to get its hash code.
1549 __node_type
* __node
=
1550 this->_M_allocate_node(std::forward
<_Args
>(__args
)...);
1555 __code
= this->_M_hash_code(this->_M_extract()(__node
->_M_v()));
1559 this->_M_deallocate_node(__node
);
1560 __throw_exception_again
;
1563 return _M_insert_multi_node(__hint
._M_cur
, __code
, __node
);
1566 template<typename _Key
, typename _Value
,
1567 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1568 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1570 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1571 _H1
, _H2
, _Hash
, _RehashPolicy
,
1573 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1574 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1575 _M_insert_unique_node(size_type __bkt
, __hash_code __code
,
1576 __node_type
* __node
)
1578 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1579 std::pair
<bool, std::size_t> __do_rehash
1580 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
, _M_element_count
, 1);
1584 if (__do_rehash
.first
)
1586 _M_rehash(__do_rehash
.second
, __saved_state
);
1587 __bkt
= _M_bucket_index(this->_M_extract()(__node
->_M_v()), __code
);
1590 this->_M_store_code(__node
, __code
);
1592 // Always insert at the beginning of the bucket.
1593 _M_insert_bucket_begin(__bkt
, __node
);
1595 return iterator(__node
);
1599 this->_M_deallocate_node(__node
);
1600 __throw_exception_again
;
1604 // Insert node, in bucket bkt if no rehash (assumes no element with its key
1605 // already present). Take ownership of the node, deallocate it on exception.
1606 template<typename _Key
, typename _Value
,
1607 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1608 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1610 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1611 _H1
, _H2
, _Hash
, _RehashPolicy
,
1613 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1614 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1615 _M_insert_multi_node(__node_type
* __hint
, __hash_code __code
,
1616 __node_type
* __node
)
1618 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1619 std::pair
<bool, std::size_t> __do_rehash
1620 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
, _M_element_count
, 1);
1624 if (__do_rehash
.first
)
1625 _M_rehash(__do_rehash
.second
, __saved_state
);
1627 this->_M_store_code(__node
, __code
);
1628 const key_type
& __k
= this->_M_extract()(__node
->_M_v());
1629 size_type __bkt
= _M_bucket_index(__k
, __code
);
1631 // Find the node before an equivalent one or use hint if it exists and
1632 // if it is equivalent.
1634 = __builtin_expect(__hint
!= nullptr, false)
1635 && this->_M_equals(__k
, __code
, __hint
)
1637 : _M_find_before_node(__bkt
, __k
, __code
);
1640 // Insert after the node before the equivalent one.
1641 __node
->_M_nxt
= __prev
->_M_nxt
;
1642 __prev
->_M_nxt
= __node
;
1643 if (__builtin_expect(__prev
== __hint
, false))
1644 // hint might be the last bucket node, in this case we need to
1645 // update next bucket.
1647 && !this->_M_equals(__k
, __code
, __node
->_M_next()))
1649 size_type __next_bkt
= _M_bucket_index(__node
->_M_next());
1650 if (__next_bkt
!= __bkt
)
1651 _M_buckets
[__next_bkt
] = __node
;
1655 // The inserted node has no equivalent in the
1656 // hashtable. We must insert the new node at the
1657 // beginning of the bucket to preserve equivalent
1658 // elements' relative positions.
1659 _M_insert_bucket_begin(__bkt
, __node
);
1661 return iterator(__node
);
1665 this->_M_deallocate_node(__node
);
1666 __throw_exception_again
;
1670 // Insert v if no element with its key is already present.
1671 template<typename _Key
, typename _Value
,
1672 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1673 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1675 template<typename _Arg
, typename _NodeGenerator
>
1676 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1677 _ExtractKey
, _Equal
, _H1
,
1678 _H2
, _Hash
, _RehashPolicy
,
1679 _Traits
>::iterator
, bool>
1680 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1681 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1682 _M_insert(_Arg
&& __v
, const _NodeGenerator
& __node_gen
, std::true_type
)
1684 const key_type
& __k
= this->_M_extract()(__v
);
1685 __hash_code __code
= this->_M_hash_code(__k
);
1686 size_type __bkt
= _M_bucket_index(__k
, __code
);
1688 __node_type
* __n
= _M_find_node(__bkt
, __k
, __code
);
1690 return std::make_pair(iterator(__n
), false);
1692 __n
= __node_gen(std::forward
<_Arg
>(__v
));
1693 return std::make_pair(_M_insert_unique_node(__bkt
, __code
, __n
), true);
1696 // Insert v unconditionally.
1697 template<typename _Key
, typename _Value
,
1698 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1699 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1701 template<typename _Arg
, typename _NodeGenerator
>
1702 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1703 _H1
, _H2
, _Hash
, _RehashPolicy
,
1705 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1706 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1707 _M_insert(const_iterator __hint
, _Arg
&& __v
,
1708 const _NodeGenerator
& __node_gen
,
1711 // First compute the hash code so that we don't do anything if it
1713 __hash_code __code
= this->_M_hash_code(this->_M_extract()(__v
));
1715 // Second allocate new node so that we don't rehash if it throws.
1716 __node_type
* __node
= __node_gen(std::forward
<_Arg
>(__v
));
1718 return _M_insert_multi_node(__hint
._M_cur
, __code
, __node
);
1721 template<typename _Key
, typename _Value
,
1722 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1723 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1725 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1726 _H1
, _H2
, _Hash
, _RehashPolicy
,
1728 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1729 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1730 erase(const_iterator __it
)
1732 __node_type
* __n
= __it
._M_cur
;
1733 std::size_t __bkt
= _M_bucket_index(__n
);
1735 // Look for previous node to unlink it from the erased one, this
1736 // is why we need buckets to contain the before begin to make
1737 // this search fast.
1738 __node_base
* __prev_n
= _M_get_previous_node(__bkt
, __n
);
1739 return _M_erase(__bkt
, __prev_n
, __n
);
1742 template<typename _Key
, typename _Value
,
1743 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1744 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1746 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1747 _H1
, _H2
, _Hash
, _RehashPolicy
,
1749 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1750 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1751 _M_erase(size_type __bkt
, __node_base
* __prev_n
, __node_type
* __n
)
1753 if (__prev_n
== _M_buckets
[__bkt
])
1754 _M_remove_bucket_begin(__bkt
, __n
->_M_next(),
1755 __n
->_M_nxt
? _M_bucket_index(__n
->_M_next()) : 0);
1756 else if (__n
->_M_nxt
)
1758 size_type __next_bkt
= _M_bucket_index(__n
->_M_next());
1759 if (__next_bkt
!= __bkt
)
1760 _M_buckets
[__next_bkt
] = __prev_n
;
1763 __prev_n
->_M_nxt
= __n
->_M_nxt
;
1764 iterator
__result(__n
->_M_next());
1765 this->_M_deallocate_node(__n
);
1771 template<typename _Key
, typename _Value
,
1772 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1773 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1775 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1776 _H1
, _H2
, _Hash
, _RehashPolicy
,
1778 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1779 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1780 _M_erase(std::true_type
, const key_type
& __k
)
1782 __hash_code __code
= this->_M_hash_code(__k
);
1783 std::size_t __bkt
= _M_bucket_index(__k
, __code
);
1785 // Look for the node before the first matching node.
1786 __node_base
* __prev_n
= _M_find_before_node(__bkt
, __k
, __code
);
1790 // We found a matching node, erase it.
1791 __node_type
* __n
= static_cast<__node_type
*>(__prev_n
->_M_nxt
);
1792 _M_erase(__bkt
, __prev_n
, __n
);
1796 template<typename _Key
, typename _Value
,
1797 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1798 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1800 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1801 _H1
, _H2
, _Hash
, _RehashPolicy
,
1803 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1804 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1805 _M_erase(std::false_type
, const key_type
& __k
)
1807 __hash_code __code
= this->_M_hash_code(__k
);
1808 std::size_t __bkt
= _M_bucket_index(__k
, __code
);
1810 // Look for the node before the first matching node.
1811 __node_base
* __prev_n
= _M_find_before_node(__bkt
, __k
, __code
);
1815 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1816 // 526. Is it undefined if a function in the standard changes
1818 // We use one loop to find all matching nodes and another to deallocate
1819 // them so that the key stays valid during the first loop. It might be
1820 // invalidated indirectly when destroying nodes.
1821 __node_type
* __n
= static_cast<__node_type
*>(__prev_n
->_M_nxt
);
1822 __node_type
* __n_last
= __n
;
1823 std::size_t __n_last_bkt
= __bkt
;
1826 __n_last
= __n_last
->_M_next();
1829 __n_last_bkt
= _M_bucket_index(__n_last
);
1831 while (__n_last_bkt
== __bkt
&& this->_M_equals(__k
, __code
, __n_last
));
1833 // Deallocate nodes.
1834 size_type __result
= 0;
1837 __node_type
* __p
= __n
->_M_next();
1838 this->_M_deallocate_node(__n
);
1843 while (__n
!= __n_last
);
1845 if (__prev_n
== _M_buckets
[__bkt
])
1846 _M_remove_bucket_begin(__bkt
, __n_last
, __n_last_bkt
);
1847 else if (__n_last
&& __n_last_bkt
!= __bkt
)
1848 _M_buckets
[__n_last_bkt
] = __prev_n
;
1849 __prev_n
->_M_nxt
= __n_last
;
1853 template<typename _Key
, typename _Value
,
1854 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1855 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1857 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1858 _H1
, _H2
, _Hash
, _RehashPolicy
,
1860 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1861 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1862 erase(const_iterator __first
, const_iterator __last
)
1864 __node_type
* __n
= __first
._M_cur
;
1865 __node_type
* __last_n
= __last
._M_cur
;
1866 if (__n
== __last_n
)
1867 return iterator(__n
);
1869 std::size_t __bkt
= _M_bucket_index(__n
);
1871 __node_base
* __prev_n
= _M_get_previous_node(__bkt
, __n
);
1872 bool __is_bucket_begin
= __n
== _M_bucket_begin(__bkt
);
1873 std::size_t __n_bkt
= __bkt
;
1878 __node_type
* __tmp
= __n
;
1879 __n
= __n
->_M_next();
1880 this->_M_deallocate_node(__tmp
);
1884 __n_bkt
= _M_bucket_index(__n
);
1886 while (__n
!= __last_n
&& __n_bkt
== __bkt
);
1887 if (__is_bucket_begin
)
1888 _M_remove_bucket_begin(__bkt
, __n
, __n_bkt
);
1889 if (__n
== __last_n
)
1891 __is_bucket_begin
= true;
1895 if (__n
&& (__n_bkt
!= __bkt
|| __is_bucket_begin
))
1896 _M_buckets
[__n_bkt
] = __prev_n
;
1897 __prev_n
->_M_nxt
= __n
;
1898 return iterator(__n
);
1901 template<typename _Key
, typename _Value
,
1902 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1903 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1906 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1907 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1910 this->_M_deallocate_nodes(_M_begin());
1911 __builtin_memset(_M_buckets
, 0, _M_bucket_count
* sizeof(__bucket_type
));
1912 _M_element_count
= 0;
1913 _M_before_begin
._M_nxt
= nullptr;
1916 template<typename _Key
, typename _Value
,
1917 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1918 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1921 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1922 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1923 rehash(size_type __n
)
1925 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1926 std::size_t __buckets
1927 = std::max(_M_rehash_policy
._M_bkt_for_elements(_M_element_count
+ 1),
1929 __buckets
= _M_rehash_policy
._M_next_bkt(__buckets
);
1931 if (__buckets
!= _M_bucket_count
)
1932 _M_rehash(__buckets
, __saved_state
);
1934 // No rehash, restore previous state to keep a consistent state.
1935 _M_rehash_policy
._M_reset(__saved_state
);
1938 template<typename _Key
, typename _Value
,
1939 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1940 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1943 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1944 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1945 _M_rehash(size_type __n
, const __rehash_state
& __state
)
1949 _M_rehash_aux(__n
, __unique_keys());
1953 // A failure here means that buckets allocation failed. We only
1954 // have to restore hash policy previous state.
1955 _M_rehash_policy
._M_reset(__state
);
1956 __throw_exception_again
;
1960 // Rehash when there is no equivalent elements.
1961 template<typename _Key
, typename _Value
,
1962 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1963 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1966 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1967 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1968 _M_rehash_aux(size_type __n
, std::true_type
)
1970 __bucket_type
* __new_buckets
= this->_M_allocate_buckets(__n
);
1971 __node_type
* __p
= _M_begin();
1972 _M_before_begin
._M_nxt
= nullptr;
1973 std::size_t __bbegin_bkt
= 0;
1976 __node_type
* __next
= __p
->_M_next();
1977 std::size_t __bkt
= __hash_code_base::_M_bucket_index(__p
, __n
);
1978 if (!__new_buckets
[__bkt
])
1980 __p
->_M_nxt
= _M_before_begin
._M_nxt
;
1981 _M_before_begin
._M_nxt
= __p
;
1982 __new_buckets
[__bkt
] = &_M_before_begin
;
1984 __new_buckets
[__bbegin_bkt
] = __p
;
1985 __bbegin_bkt
= __bkt
;
1989 __p
->_M_nxt
= __new_buckets
[__bkt
]->_M_nxt
;
1990 __new_buckets
[__bkt
]->_M_nxt
= __p
;
1995 if (__builtin_expect(_M_bucket_count
!= 0, true))
1996 _M_deallocate_buckets();
1997 _M_bucket_count
= __n
;
1998 _M_buckets
= __new_buckets
;
2001 // Rehash when there can be equivalent elements, preserve their relative
2003 template<typename _Key
, typename _Value
,
2004 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
2005 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
2008 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
2009 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
2010 _M_rehash_aux(size_type __n
, std::false_type
)
2012 __bucket_type
* __new_buckets
= this->_M_allocate_buckets(__n
);
2014 __node_type
* __p
= _M_begin();
2015 _M_before_begin
._M_nxt
= nullptr;
2016 std::size_t __bbegin_bkt
= 0;
2017 std::size_t __prev_bkt
= 0;
2018 __node_type
* __prev_p
= nullptr;
2019 bool __check_bucket
= false;
2023 __node_type
* __next
= __p
->_M_next();
2024 std::size_t __bkt
= __hash_code_base::_M_bucket_index(__p
, __n
);
2026 if (__prev_p
&& __prev_bkt
== __bkt
)
2028 // Previous insert was already in this bucket, we insert after
2029 // the previously inserted one to preserve equivalent elements
2031 __p
->_M_nxt
= __prev_p
->_M_nxt
;
2032 __prev_p
->_M_nxt
= __p
;
2034 // Inserting after a node in a bucket require to check that we
2035 // haven't change the bucket last node, in this case next
2036 // bucket containing its before begin node must be updated. We
2037 // schedule a check as soon as we move out of the sequence of
2038 // equivalent nodes to limit the number of checks.
2039 __check_bucket
= true;
2045 // Check if we shall update the next bucket because of
2046 // insertions into __prev_bkt bucket.
2047 if (__prev_p
->_M_nxt
)
2049 std::size_t __next_bkt
2050 = __hash_code_base::_M_bucket_index(__prev_p
->_M_next(),
2052 if (__next_bkt
!= __prev_bkt
)
2053 __new_buckets
[__next_bkt
] = __prev_p
;
2055 __check_bucket
= false;
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
;
2078 if (__check_bucket
&& __prev_p
->_M_nxt
)
2080 std::size_t __next_bkt
2081 = __hash_code_base::_M_bucket_index(__prev_p
->_M_next(), __n
);
2082 if (__next_bkt
!= __prev_bkt
)
2083 __new_buckets
[__next_bkt
] = __prev_p
;
2086 if (__builtin_expect(_M_bucket_count
!= 0, true))
2087 _M_deallocate_buckets();
2088 _M_bucket_count
= __n
;
2089 _M_buckets
= __new_buckets
;
2092 _GLIBCXX_END_NAMESPACE_VERSION
2095 #endif // _HASHTABLE_H