1 // hashtable.h header -*- C++ -*-
3 // Copyright (C) 2007-2014 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 have erase not throwing.
46 __detail::__is_noexcept_hash
<_Tp
, _Hash
>>>;
49 * Primary class template _Hashtable.
51 * @ingroup hashtable-detail
53 * @tparam _Value CopyConstructible type.
55 * @tparam _Key CopyConstructible type.
57 * @tparam _Alloc An allocator type
58 * ([lib.allocator.requirements]) whose _Alloc::value_type is
59 * _Value. As a conforming extension, we allow for
60 * _Alloc::value_type != _Value.
62 * @tparam _ExtractKey Function object that takes an object of type
63 * _Value and returns a value of type _Key.
65 * @tparam _Equal Function object that takes two objects of type k
66 * and returns a bool-like value that is true if the two objects
67 * are considered equal.
69 * @tparam _H1 The hash function. A unary function object with
70 * argument type _Key and result type size_t. Return values should
71 * be distributed over the entire range [0, numeric_limits<size_t>:::max()].
73 * @tparam _H2 The range-hashing function (in the terminology of
74 * Tavori and Dreizin). A binary function object whose argument
75 * types and result type are all size_t. Given arguments r and N,
76 * the return value is in the range [0, N).
78 * @tparam _Hash The ranged hash function (Tavori and Dreizin). A
79 * binary function whose argument types are _Key and size_t and
80 * whose result type is size_t. Given arguments k and N, the
81 * return value is in the range [0, N). Default: hash(k, N) =
82 * h2(h1(k), N). If _Hash is anything other than the default, _H1
83 * and _H2 are ignored.
85 * @tparam _RehashPolicy Policy class with three members, all of
86 * which govern the bucket count. _M_next_bkt(n) returns a bucket
87 * count no smaller than n. _M_bkt_for_elements(n) returns a
88 * bucket count appropriate for an element count of n.
89 * _M_need_rehash(n_bkt, n_elt, n_ins) determines whether, if the
90 * current bucket count is n_bkt and the current element count is
91 * n_elt, we need to increase the bucket count. If so, returns
92 * make_pair(true, n), where n is the new bucket count. If not,
93 * returns make_pair(false, <anything>)
95 * @tparam _Traits Compile-time class with three boolean
96 * std::integral_constant members: __cache_hash_code, __constant_iterators,
99 * Each _Hashtable data structure has:
101 * - _Bucket[] _M_buckets
102 * - _Hash_node_base _M_before_begin
103 * - size_type _M_bucket_count
104 * - size_type _M_element_count
106 * with _Bucket being _Hash_node* and _Hash_node containing:
108 * - _Hash_node* _M_next
110 * - size_t _M_hash_code if cache_hash_code is true
112 * In terms of Standard containers the hashtable is like the aggregation of:
114 * - std::forward_list<_Node> containing the elements
115 * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
117 * The non-empty buckets contain the node before the first node in the
118 * bucket. This design makes it possible to implement something like a
119 * std::forward_list::insert_after on container insertion and
120 * std::forward_list::erase_after on container erase
121 * calls. _M_before_begin is equivalent to
122 * std::forward_list::before_begin. Empty buckets contain
123 * nullptr. Note that one of the non-empty buckets contains
124 * &_M_before_begin which is not a dereferenceable node so the
125 * node pointer in a bucket shall never be dereferenced, only its
128 * Walking through a bucket's nodes requires a check on the hash code to
129 * see if each node is still in the bucket. Such a design assumes a
130 * quite efficient hash functor and is one of the reasons it is
131 * highly advisable to set __cache_hash_code to true.
133 * The container iterators are simply built from nodes. This way
134 * incrementing the iterator is perfectly efficient independent of
135 * how many empty buckets there are in the container.
137 * On insert we compute the element's hash code and use it to find the
138 * bucket index. If the element must be inserted in an empty bucket
139 * we add it at the beginning of the singly linked list and make the
140 * bucket point to _M_before_begin. The bucket that used to point to
141 * _M_before_begin, if any, is updated to point to its new before
144 * On erase, the simple iterator design requires using the hash
145 * functor to get the index of the bucket to update. For this
146 * reason, when __cache_hash_code is set to false the hash functor must
147 * not throw and this is enforced by a static assertion.
149 * Functionality is implemented by decomposition into base classes,
150 * where the derived _Hashtable class is used in _Map_base,
151 * _Insert, _Rehash_base, and _Equality base classes to access the
152 * "this" pointer. _Hashtable_base is used in the base classes as a
153 * non-recursive, fully-completed-type so that detailed nested type
154 * information, such as iterator type and node type, can be
155 * used. This is similar to the "Curiously Recurring Template
156 * Pattern" (CRTP) technique, but uses a reconstructed, not
157 * explicitly passed, template pattern.
159 * Base class templates are:
160 * - __detail::_Hashtable_base
161 * - __detail::_Map_base
162 * - __detail::_Insert
163 * - __detail::_Rehash_base
164 * - __detail::_Equality
166 template<typename _Key
, typename _Value
, typename _Alloc
,
167 typename _ExtractKey
, typename _Equal
,
168 typename _H1
, typename _H2
, typename _Hash
,
169 typename _RehashPolicy
, typename _Traits
>
171 : public __detail::_Hashtable_base
<_Key
, _Value
, _ExtractKey
, _Equal
,
172 _H1
, _H2
, _Hash
, _Traits
>,
173 public __detail::_Map_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
174 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>,
175 public __detail::_Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
176 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>,
177 public __detail::_Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
178 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>,
179 public __detail::_Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
180 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>,
181 private __detail::_Hashtable_alloc
<
182 typename __alloctr_rebind
<_Alloc
,
183 __detail::_Hash_node
<_Value
,
184 _Traits::__hash_cached::value
> >::__type
>
186 using __traits_type
= _Traits
;
187 using __hash_cached
= typename
__traits_type::__hash_cached
;
188 using __node_type
= __detail::_Hash_node
<_Value
, __hash_cached::value
>;
189 using __node_alloc_type
=
190 typename __alloctr_rebind
<_Alloc
, __node_type
>::__type
;
192 using __hashtable_alloc
= __detail::_Hashtable_alloc
<__node_alloc_type
>;
194 using __value_alloc_traits
=
195 typename
__hashtable_alloc::__value_alloc_traits
;
196 using __node_alloc_traits
=
197 typename
__hashtable_alloc::__node_alloc_traits
;
198 using __node_base
= typename
__hashtable_alloc::__node_base
;
199 using __bucket_type
= typename
__hashtable_alloc::__bucket_type
;
202 typedef _Key key_type
;
203 typedef _Value value_type
;
204 typedef _Alloc allocator_type
;
205 typedef _Equal key_equal
;
207 // mapped_type, if present, comes from _Map_base.
208 // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
209 typedef typename
__value_alloc_traits::pointer pointer
;
210 typedef typename
__value_alloc_traits::const_pointer const_pointer
;
211 typedef value_type
& reference
;
212 typedef const value_type
& const_reference
;
215 using __rehash_type
= _RehashPolicy
;
216 using __rehash_state
= typename
__rehash_type::_State
;
218 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
219 using __unique_keys
= typename
__traits_type::__unique_keys
;
221 using __key_extract
= typename
std::conditional
<
222 __constant_iterators::value
,
224 __detail::_Select1st
>::type
;
226 using __hashtable_base
= __detail::
227 _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
228 _Equal
, _H1
, _H2
, _Hash
, _Traits
>;
230 using __hash_code_base
= typename
__hashtable_base::__hash_code_base
;
231 using __hash_code
= typename
__hashtable_base::__hash_code
;
232 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
234 using __map_base
= __detail::_Map_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
235 _Equal
, _H1
, _H2
, _Hash
,
236 _RehashPolicy
, _Traits
>;
238 using __rehash_base
= __detail::_Rehash_base
<_Key
, _Value
, _Alloc
,
241 _RehashPolicy
, _Traits
>;
243 using __eq_base
= __detail::_Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
,
244 _Equal
, _H1
, _H2
, _Hash
,
245 _RehashPolicy
, _Traits
>;
247 using __reuse_or_alloc_node_type
=
248 __detail::_ReuseOrAllocNode
<__node_alloc_type
>;
250 // Metaprogramming for picking apart hash caching.
251 template<typename _Cond
>
252 using __if_hash_cached
= __or_
<__not_
<__hash_cached
>, _Cond
>;
254 template<typename _Cond
>
255 using __if_hash_not_cached
= __or_
<__hash_cached
, _Cond
>;
257 // Compile-time diagnostics.
259 // _Hash_code_base has everything protected, so use this derived type to
261 struct __hash_code_base_access
: __hash_code_base
262 { using __hash_code_base::_M_bucket_index
; };
264 // Getting a bucket index from a node shall not throw because it is used
265 // in methods (erase, swap...) that shall not throw.
266 static_assert(noexcept(declval
<const __hash_code_base_access
&>()
267 ._M_bucket_index((const __node_type
*)nullptr,
269 "Cache the hash code or qualify your functors involved"
270 " in hash code and bucket index computation with noexcept");
272 // Following two static assertions are necessary to guarantee
273 // that local_iterator will be default constructible.
275 // When hash codes are cached local iterator inherits from H2 functor
276 // which must then be default constructible.
277 static_assert(__if_hash_cached
<is_default_constructible
<_H2
>>::value
,
278 "Functor used to map hash code to bucket index"
279 " must be default constructible");
281 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
282 typename _ExtractKeya
, typename _Equala
,
283 typename _H1a
, typename _H2a
, typename _Hasha
,
284 typename _RehashPolicya
, typename _Traitsa
,
286 friend struct __detail::_Map_base
;
288 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
289 typename _ExtractKeya
, typename _Equala
,
290 typename _H1a
, typename _H2a
, typename _Hasha
,
291 typename _RehashPolicya
, typename _Traitsa
>
292 friend struct __detail::_Insert_base
;
294 template<typename _Keya
, typename _Valuea
, typename _Alloca
,
295 typename _ExtractKeya
, typename _Equala
,
296 typename _H1a
, typename _H2a
, typename _Hasha
,
297 typename _RehashPolicya
, typename _Traitsa
,
298 bool _Constant_iteratorsa
, bool _Unique_keysa
>
299 friend struct __detail::_Insert
;
302 using size_type
= typename
__hashtable_base::size_type
;
303 using difference_type
= typename
__hashtable_base::difference_type
;
305 using iterator
= typename
__hashtable_base::iterator
;
306 using const_iterator
= typename
__hashtable_base::const_iterator
;
308 using local_iterator
= typename
__hashtable_base::local_iterator
;
309 using const_local_iterator
= typename
__hashtable_base::
310 const_local_iterator
;
313 __bucket_type
* _M_buckets
= &_M_single_bucket
;
314 size_type _M_bucket_count
= 1;
315 __node_base _M_before_begin
;
316 size_type _M_element_count
= 0;
317 _RehashPolicy _M_rehash_policy
;
319 // A single bucket used when only need for 1 bucket. Especially
320 // interesting in move semantic to leave hashtable with only 1 buckets
321 // which is not allocated so that we can have those operations noexcept
323 // Note that we can't leave hashtable with 0 bucket without adding
324 // numerous checks in the code to avoid 0 modulus.
325 __bucket_type _M_single_bucket
= nullptr;
328 _M_uses_single_bucket(__bucket_type
* __bkts
) const
329 { return __builtin_expect(_M_buckets
== &_M_single_bucket
, false); }
332 _M_uses_single_bucket() const
333 { return _M_uses_single_bucket(_M_buckets
); }
336 _M_base_alloc() { return *this; }
339 _M_allocate_buckets(size_type __n
)
341 if (__builtin_expect(__n
== 1, false))
343 _M_single_bucket
= nullptr;
344 return &_M_single_bucket
;
347 return __hashtable_alloc::_M_allocate_buckets(__n
);
351 _M_deallocate_buckets(__bucket_type
* __bkts
, size_type __n
)
353 if (_M_uses_single_bucket(__bkts
))
356 __hashtable_alloc::_M_deallocate_buckets(__bkts
, __n
);
360 _M_deallocate_buckets()
361 { _M_deallocate_buckets(_M_buckets
, _M_bucket_count
); }
363 // Gets bucket begin, deals with the fact that non-empty buckets contain
364 // their before begin node.
366 _M_bucket_begin(size_type __bkt
) const;
370 { return static_cast<__node_type
*>(_M_before_begin
._M_nxt
); }
372 template<typename _NodeGenerator
>
374 _M_assign(const _Hashtable
&, const _NodeGenerator
&);
377 _M_move_assign(_Hashtable
&&, std::true_type
);
380 _M_move_assign(_Hashtable
&&, std::false_type
);
385 _Hashtable(const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
386 const _Equal
& __eq
, const _ExtractKey
& __exk
,
387 const allocator_type
& __a
)
388 : __hashtable_base(__exk
, __h1
, __h2
, __h
, __eq
),
389 __hashtable_alloc(__node_alloc_type(__a
))
393 // Constructor, destructor, assignment, swap
394 _Hashtable() = default;
395 _Hashtable(size_type __bucket_hint
,
396 const _H1
&, const _H2
&, const _Hash
&,
397 const _Equal
&, const _ExtractKey
&,
398 const allocator_type
&);
400 template<typename _InputIterator
>
401 _Hashtable(_InputIterator __first
, _InputIterator __last
,
402 size_type __bucket_hint
,
403 const _H1
&, const _H2
&, const _Hash
&,
404 const _Equal
&, const _ExtractKey
&,
405 const allocator_type
&);
407 _Hashtable(const _Hashtable
&);
409 _Hashtable(_Hashtable
&&) noexcept
;
411 _Hashtable(const _Hashtable
&, const allocator_type
&);
413 _Hashtable(_Hashtable
&&, const allocator_type
&);
415 // Use delegating constructors.
417 _Hashtable(const allocator_type
& __a
)
418 : __hashtable_alloc(__node_alloc_type(__a
))
422 _Hashtable(size_type __n
,
423 const _H1
& __hf
= _H1(),
424 const key_equal
& __eql
= key_equal(),
425 const allocator_type
& __a
= allocator_type())
426 : _Hashtable(__n
, __hf
, _H2(), _Hash(), __eql
,
427 __key_extract(), __a
)
430 template<typename _InputIterator
>
431 _Hashtable(_InputIterator __f
, _InputIterator __l
,
433 const _H1
& __hf
= _H1(),
434 const key_equal
& __eql
= key_equal(),
435 const allocator_type
& __a
= allocator_type())
436 : _Hashtable(__f
, __l
, __n
, __hf
, _H2(), _Hash(), __eql
,
437 __key_extract(), __a
)
440 _Hashtable(initializer_list
<value_type
> __l
,
442 const _H1
& __hf
= _H1(),
443 const key_equal
& __eql
= key_equal(),
444 const allocator_type
& __a
= allocator_type())
445 : _Hashtable(__l
.begin(), __l
.end(), __n
, __hf
, _H2(), _Hash(), __eql
,
446 __key_extract(), __a
)
450 operator=(const _Hashtable
& __ht
);
453 operator=(_Hashtable
&& __ht
)
454 noexcept(__node_alloc_traits::_S_nothrow_move())
456 constexpr bool __move_storage
=
457 __node_alloc_traits::_S_propagate_on_move_assign()
458 || __node_alloc_traits::_S_always_equal();
459 _M_move_assign(std::move(__ht
),
460 integral_constant
<bool, __move_storage
>());
465 operator=(initializer_list
<value_type
> __l
)
467 __reuse_or_alloc_node_type
__roan(_M_begin(), *this);
468 _M_before_begin
._M_nxt
= nullptr;
470 this->_M_insert_range(__l
.begin(), __l
.end(), __roan
);
474 ~_Hashtable() noexcept
;
478 noexcept(__node_alloc_traits::_S_nothrow_swap());
480 // Basic container operations
483 { return iterator(_M_begin()); }
486 begin() const noexcept
487 { return const_iterator(_M_begin()); }
491 { return iterator(nullptr); }
495 { return const_iterator(nullptr); }
498 cbegin() const noexcept
499 { return const_iterator(_M_begin()); }
502 cend() const noexcept
503 { return const_iterator(nullptr); }
506 size() const noexcept
507 { return _M_element_count
; }
510 empty() const noexcept
511 { return size() == 0; }
514 get_allocator() const noexcept
515 { return allocator_type(this->_M_node_allocator()); }
518 max_size() const noexcept
519 { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
524 { return this->_M_eq(); }
526 // hash_function, if present, comes from _Hash_code_base.
530 bucket_count() const noexcept
531 { return _M_bucket_count
; }
534 max_bucket_count() const noexcept
535 { return max_size(); }
538 bucket_size(size_type __n
) const
539 { return std::distance(begin(__n
), end(__n
)); }
542 bucket(const key_type
& __k
) const
543 { return _M_bucket_index(__k
, this->_M_hash_code(__k
)); }
548 return local_iterator(*this, _M_bucket_begin(__n
),
549 __n
, _M_bucket_count
);
554 { return local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
557 begin(size_type __n
) const
559 return const_local_iterator(*this, _M_bucket_begin(__n
),
560 __n
, _M_bucket_count
);
564 end(size_type __n
) const
565 { return const_local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
569 cbegin(size_type __n
) const
571 return const_local_iterator(*this, _M_bucket_begin(__n
),
572 __n
, _M_bucket_count
);
576 cend(size_type __n
) const
577 { return const_local_iterator(*this, nullptr, __n
, _M_bucket_count
); }
580 load_factor() const noexcept
582 return static_cast<float>(size()) / static_cast<float>(bucket_count());
585 // max_load_factor, if present, comes from _Rehash_base.
587 // Generalization of max_load_factor. Extension, not found in
588 // TR1. Only useful if _RehashPolicy is something other than
591 __rehash_policy() const
592 { return _M_rehash_policy
; }
595 __rehash_policy(const _RehashPolicy
&);
599 find(const key_type
& __k
);
602 find(const key_type
& __k
) const;
605 count(const key_type
& __k
) const;
607 std::pair
<iterator
, iterator
>
608 equal_range(const key_type
& __k
);
610 std::pair
<const_iterator
, const_iterator
>
611 equal_range(const key_type
& __k
) const;
614 // Bucket index computation helpers.
616 _M_bucket_index(__node_type
* __n
) const noexcept
617 { return __hash_code_base::_M_bucket_index(__n
, _M_bucket_count
); }
620 _M_bucket_index(const key_type
& __k
, __hash_code __c
) const
621 { return __hash_code_base::_M_bucket_index(__k
, __c
, _M_bucket_count
); }
623 // Find and insert helper functions and types
624 // Find the node before the one matching the criteria.
626 _M_find_before_node(size_type
, const key_type
&, __hash_code
) const;
629 _M_find_node(size_type __bkt
, const key_type
& __key
,
630 __hash_code __c
) const
632 __node_base
* __before_n
= _M_find_before_node(__bkt
, __key
, __c
);
634 return static_cast<__node_type
*>(__before_n
->_M_nxt
);
638 // Insert a node at the beginning of a bucket.
640 _M_insert_bucket_begin(size_type
, __node_type
*);
642 // Remove the bucket first node
644 _M_remove_bucket_begin(size_type __bkt
, __node_type
* __next_n
,
645 size_type __next_bkt
);
647 // Get the node before __n in the bucket __bkt
649 _M_get_previous_node(size_type __bkt
, __node_base
* __n
);
651 // Insert node with hash code __code, in bucket bkt if no rehash (assumes
652 // no element with its key already present). Take ownership of the node,
653 // deallocate it on exception.
655 _M_insert_unique_node(size_type __bkt
, __hash_code __code
,
658 // Insert node with hash code __code. Take ownership of the node,
659 // deallocate it on exception.
661 _M_insert_multi_node(__node_type
* __hint
,
662 __hash_code __code
, __node_type
* __n
);
664 template<typename
... _Args
>
665 std::pair
<iterator
, bool>
666 _M_emplace(std::true_type
, _Args
&&... __args
);
668 template<typename
... _Args
>
670 _M_emplace(std::false_type __uk
, _Args
&&... __args
)
671 { return _M_emplace(cend(), __uk
, std::forward
<_Args
>(__args
)...); }
673 // Emplace with hint, useless when keys are unique.
674 template<typename
... _Args
>
676 _M_emplace(const_iterator
, std::true_type __uk
, _Args
&&... __args
)
677 { return _M_emplace(__uk
, std::forward
<_Args
>(__args
)...).first
; }
679 template<typename
... _Args
>
681 _M_emplace(const_iterator
, std::false_type
, _Args
&&... __args
);
683 template<typename _Arg
, typename _NodeGenerator
>
684 std::pair
<iterator
, bool>
685 _M_insert(_Arg
&&, const _NodeGenerator
&, std::true_type
);
687 template<typename _Arg
, typename _NodeGenerator
>
689 _M_insert(_Arg
&& __arg
, const _NodeGenerator
& __node_gen
,
690 std::false_type __uk
)
692 return _M_insert(cend(), std::forward
<_Arg
>(__arg
), __node_gen
,
696 // Insert with hint, not used when keys are unique.
697 template<typename _Arg
, typename _NodeGenerator
>
699 _M_insert(const_iterator
, _Arg
&& __arg
, const _NodeGenerator
& __node_gen
,
703 _M_insert(std::forward
<_Arg
>(__arg
), __node_gen
, __uk
).first
;
706 // Insert with hint when keys are not unique.
707 template<typename _Arg
, typename _NodeGenerator
>
709 _M_insert(const_iterator
, _Arg
&&, const _NodeGenerator
&, std::false_type
);
712 _M_erase(std::true_type
, const key_type
&);
715 _M_erase(std::false_type
, const key_type
&);
718 _M_erase(size_type __bkt
, __node_base
* __prev_n
, __node_type
* __n
);
722 template<typename
... _Args
>
724 emplace(_Args
&&... __args
)
725 { return _M_emplace(__unique_keys(), std::forward
<_Args
>(__args
)...); }
727 template<typename
... _Args
>
729 emplace_hint(const_iterator __hint
, _Args
&&... __args
)
731 return _M_emplace(__hint
, __unique_keys(),
732 std::forward
<_Args
>(__args
)...);
735 // Insert member functions via inheritance.
739 erase(const_iterator
);
744 { return erase(const_iterator(__it
)); }
747 erase(const key_type
& __k
)
748 { return _M_erase(__unique_keys(), __k
); }
751 erase(const_iterator
, const_iterator
);
756 // Set number of buckets to be appropriate for container of n element.
757 void rehash(size_type __n
);
760 // reserve, if present, comes from _Rehash_base.
763 // Helper rehash method used when keys are unique.
764 void _M_rehash_aux(size_type __n
, std::true_type
);
766 // Helper rehash method used when keys can be non-unique.
767 void _M_rehash_aux(size_type __n
, std::false_type
);
769 // Unconditionally change size of bucket array to n, restore
770 // hash policy state to __state on exception.
771 void _M_rehash(size_type __n
, const __rehash_state
& __state
);
775 // Definitions of class template _Hashtable's out-of-line member functions.
776 template<typename _Key
, typename _Value
,
777 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
778 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
780 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
781 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
782 _Traits
>::__node_type
*
783 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
784 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
785 _M_bucket_begin(size_type __bkt
) const
787 __node_base
* __n
= _M_buckets
[__bkt
];
788 return __n
? static_cast<__node_type
*>(__n
->_M_nxt
) : nullptr;
791 template<typename _Key
, typename _Value
,
792 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
793 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
795 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
796 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
797 _Hashtable(size_type __bucket_hint
,
798 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
799 const _Equal
& __eq
, const _ExtractKey
& __exk
,
800 const allocator_type
& __a
)
801 : _Hashtable(__h1
, __h2
, __h
, __eq
, __exk
, __a
)
803 auto __bkt
= _M_rehash_policy
._M_next_bkt(__bucket_hint
);
804 if (__bkt
> _M_bucket_count
)
806 _M_buckets
= _M_allocate_buckets(__bkt
);
807 _M_bucket_count
= __bkt
;
811 template<typename _Key
, typename _Value
,
812 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
813 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
815 template<typename _InputIterator
>
816 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
817 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
818 _Hashtable(_InputIterator __f
, _InputIterator __l
,
819 size_type __bucket_hint
,
820 const _H1
& __h1
, const _H2
& __h2
, const _Hash
& __h
,
821 const _Equal
& __eq
, const _ExtractKey
& __exk
,
822 const allocator_type
& __a
)
823 : _Hashtable(__h1
, __h2
, __h
, __eq
, __exk
, __a
)
825 auto __nb_elems
= __detail::__distance_fw(__f
, __l
);
827 _M_rehash_policy
._M_next_bkt(
828 std::max(_M_rehash_policy
._M_bkt_for_elements(__nb_elems
),
831 if (__bkt_count
> _M_bucket_count
)
833 _M_buckets
= _M_allocate_buckets(__bkt_count
);
834 _M_bucket_count
= __bkt_count
;
839 for (; __f
!= __l
; ++__f
)
845 _M_deallocate_buckets();
846 __throw_exception_again
;
850 template<typename _Key
, typename _Value
,
851 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
852 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
854 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
855 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>&
856 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
857 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::operator=(
858 const _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
859 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>& __ht
)
864 if (__node_alloc_traits::_S_propagate_on_copy_assign())
866 auto& __this_alloc
= this->_M_node_allocator();
867 auto& __that_alloc
= __ht
._M_node_allocator();
868 if (!__node_alloc_traits::_S_always_equal()
869 && __this_alloc
!= __that_alloc
)
871 // Replacement allocator cannot free existing storage.
872 this->_M_deallocate_nodes(_M_begin());
873 _M_before_begin
._M_nxt
= nullptr;
874 _M_deallocate_buckets();
875 _M_buckets
= nullptr;
876 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
877 __hashtable_base::operator=(__ht
);
878 _M_bucket_count
= __ht
._M_bucket_count
;
879 _M_element_count
= __ht
._M_element_count
;
880 _M_rehash_policy
= __ht
._M_rehash_policy
;
884 [this](const __node_type
* __n
)
885 { return this->_M_allocate_node(__n
->_M_v()); });
889 // _M_assign took care of deallocating all memory. Now we
890 // must make sure this instance remains in a usable state.
892 __throw_exception_again
;
896 std::__alloc_on_copy(__this_alloc
, __that_alloc
);
899 // Reuse allocated buckets and nodes.
900 __bucket_type
* __former_buckets
= nullptr;
901 std::size_t __former_bucket_count
= _M_bucket_count
;
902 const __rehash_state
& __former_state
= _M_rehash_policy
._M_state();
904 if (_M_bucket_count
!= __ht
._M_bucket_count
)
906 __former_buckets
= _M_buckets
;
907 _M_buckets
= _M_allocate_buckets(__ht
._M_bucket_count
);
908 _M_bucket_count
= __ht
._M_bucket_count
;
911 __builtin_memset(_M_buckets
, 0,
912 _M_bucket_count
* sizeof(__bucket_type
));
916 __hashtable_base::operator=(__ht
);
917 _M_element_count
= __ht
._M_element_count
;
918 _M_rehash_policy
= __ht
._M_rehash_policy
;
919 __reuse_or_alloc_node_type
__roan(_M_begin(), *this);
920 _M_before_begin
._M_nxt
= nullptr;
922 [&__roan
](const __node_type
* __n
)
923 { return __roan(__n
->_M_v()); });
924 if (__former_buckets
)
925 _M_deallocate_buckets(__former_buckets
, __former_bucket_count
);
929 if (__former_buckets
)
931 // Restore previous buckets.
932 _M_deallocate_buckets();
933 _M_rehash_policy
._M_reset(__former_state
);
934 _M_buckets
= __former_buckets
;
935 _M_bucket_count
= __former_bucket_count
;
937 __builtin_memset(_M_buckets
, 0,
938 _M_bucket_count
* sizeof(__bucket_type
));
939 __throw_exception_again
;
944 template<typename _Key
, typename _Value
,
945 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
946 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
948 template<typename _NodeGenerator
>
950 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
951 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
952 _M_assign(const _Hashtable
& __ht
, const _NodeGenerator
& __node_gen
)
954 __bucket_type
* __buckets
= nullptr;
956 _M_buckets
= __buckets
= _M_allocate_buckets(_M_bucket_count
);
960 if (!__ht
._M_before_begin
._M_nxt
)
963 // First deal with the special first node pointed to by
965 __node_type
* __ht_n
= __ht
._M_begin();
966 __node_type
* __this_n
= __node_gen(__ht_n
);
967 this->_M_copy_code(__this_n
, __ht_n
);
968 _M_before_begin
._M_nxt
= __this_n
;
969 _M_buckets
[_M_bucket_index(__this_n
)] = &_M_before_begin
;
971 // Then deal with other nodes.
972 __node_base
* __prev_n
= __this_n
;
973 for (__ht_n
= __ht_n
->_M_next(); __ht_n
; __ht_n
= __ht_n
->_M_next())
975 __this_n
= __node_gen(__ht_n
);
976 __prev_n
->_M_nxt
= __this_n
;
977 this->_M_copy_code(__this_n
, __ht_n
);
978 size_type __bkt
= _M_bucket_index(__this_n
);
979 if (!_M_buckets
[__bkt
])
980 _M_buckets
[__bkt
] = __prev_n
;
988 _M_deallocate_buckets();
989 __throw_exception_again
;
993 template<typename _Key
, typename _Value
,
994 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
995 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
998 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
999 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1002 _M_rehash_policy
._M_reset();
1003 _M_bucket_count
= 1;
1004 _M_single_bucket
= nullptr;
1005 _M_buckets
= &_M_single_bucket
;
1006 _M_before_begin
._M_nxt
= nullptr;
1007 _M_element_count
= 0;
1010 template<typename _Key
, typename _Value
,
1011 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1012 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1015 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1016 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1017 _M_move_assign(_Hashtable
&& __ht
, std::true_type
)
1019 this->_M_deallocate_nodes(_M_begin());
1020 _M_deallocate_buckets();
1021 __hashtable_base::operator=(std::move(__ht
));
1022 _M_rehash_policy
= __ht
._M_rehash_policy
;
1023 if (!__ht
._M_uses_single_bucket())
1024 _M_buckets
= __ht
._M_buckets
;
1027 _M_buckets
= &_M_single_bucket
;
1028 _M_single_bucket
= __ht
._M_single_bucket
;
1030 _M_bucket_count
= __ht
._M_bucket_count
;
1031 _M_before_begin
._M_nxt
= __ht
._M_before_begin
._M_nxt
;
1032 _M_element_count
= __ht
._M_element_count
;
1033 std::__alloc_on_move(this->_M_node_allocator(), __ht
._M_node_allocator());
1035 // Fix buckets containing the _M_before_begin pointers that can't be
1038 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1042 template<typename _Key
, typename _Value
,
1043 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1044 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1047 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1048 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1049 _M_move_assign(_Hashtable
&& __ht
, std::false_type
)
1051 if (__ht
._M_node_allocator() == this->_M_node_allocator())
1052 _M_move_assign(std::move(__ht
), std::true_type());
1055 // Can't move memory, move elements then.
1056 __bucket_type
* __former_buckets
= nullptr;
1057 size_type __former_bucket_count
= _M_bucket_count
;
1058 const __rehash_state
& __former_state
= _M_rehash_policy
._M_state();
1060 if (_M_bucket_count
!= __ht
._M_bucket_count
)
1062 __former_buckets
= _M_buckets
;
1063 _M_buckets
= _M_allocate_buckets(__ht
._M_bucket_count
);
1064 _M_bucket_count
= __ht
._M_bucket_count
;
1067 __builtin_memset(_M_buckets
, 0,
1068 _M_bucket_count
* sizeof(__bucket_type
));
1072 __hashtable_base::operator=(std::move(__ht
));
1073 _M_element_count
= __ht
._M_element_count
;
1074 _M_rehash_policy
= __ht
._M_rehash_policy
;
1075 __reuse_or_alloc_node_type
__roan(_M_begin(), *this);
1076 _M_before_begin
._M_nxt
= nullptr;
1078 [&__roan
](__node_type
* __n
)
1079 { return __roan(std::move_if_noexcept(__n
->_M_v())); });
1084 if (__former_buckets
)
1086 _M_deallocate_buckets();
1087 _M_rehash_policy
._M_reset(__former_state
);
1088 _M_buckets
= __former_buckets
;
1089 _M_bucket_count
= __former_bucket_count
;
1091 __builtin_memset(_M_buckets
, 0,
1092 _M_bucket_count
* sizeof(__bucket_type
));
1093 __throw_exception_again
;
1098 template<typename _Key
, typename _Value
,
1099 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1100 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1102 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1103 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1104 _Hashtable(const _Hashtable
& __ht
)
1105 : __hashtable_base(__ht
),
1107 __rehash_base(__ht
),
1109 __node_alloc_traits::_S_select_on_copy(__ht
._M_node_allocator())),
1110 _M_buckets(nullptr),
1111 _M_bucket_count(__ht
._M_bucket_count
),
1112 _M_element_count(__ht
._M_element_count
),
1113 _M_rehash_policy(__ht
._M_rehash_policy
)
1116 [this](const __node_type
* __n
)
1117 { return this->_M_allocate_node(__n
->_M_v()); });
1120 template<typename _Key
, typename _Value
,
1121 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1122 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1124 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1125 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1126 _Hashtable(_Hashtable
&& __ht
) noexcept
1127 : __hashtable_base(__ht
),
1129 __rehash_base(__ht
),
1130 __hashtable_alloc(std::move(__ht
._M_base_alloc())),
1131 _M_buckets(__ht
._M_buckets
),
1132 _M_bucket_count(__ht
._M_bucket_count
),
1133 _M_before_begin(__ht
._M_before_begin
._M_nxt
),
1134 _M_element_count(__ht
._M_element_count
),
1135 _M_rehash_policy(__ht
._M_rehash_policy
)
1137 // Update, if necessary, buckets if __ht is using its single bucket.
1138 if (__ht
._M_uses_single_bucket())
1140 _M_buckets
= &_M_single_bucket
;
1141 _M_single_bucket
= __ht
._M_single_bucket
;
1144 // Update, if necessary, bucket pointing to before begin that hasn't
1147 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1152 template<typename _Key
, typename _Value
,
1153 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1154 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1156 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1157 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1158 _Hashtable(const _Hashtable
& __ht
, const allocator_type
& __a
)
1159 : __hashtable_base(__ht
),
1161 __rehash_base(__ht
),
1162 __hashtable_alloc(__node_alloc_type(__a
)),
1164 _M_bucket_count(__ht
._M_bucket_count
),
1165 _M_element_count(__ht
._M_element_count
),
1166 _M_rehash_policy(__ht
._M_rehash_policy
)
1169 [this](const __node_type
* __n
)
1170 { return this->_M_allocate_node(__n
->_M_v()); });
1173 template<typename _Key
, typename _Value
,
1174 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1175 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1177 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1178 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1179 _Hashtable(_Hashtable
&& __ht
, const allocator_type
& __a
)
1180 : __hashtable_base(__ht
),
1182 __rehash_base(__ht
),
1183 __hashtable_alloc(__node_alloc_type(__a
)),
1184 _M_buckets(nullptr),
1185 _M_bucket_count(__ht
._M_bucket_count
),
1186 _M_element_count(__ht
._M_element_count
),
1187 _M_rehash_policy(__ht
._M_rehash_policy
)
1189 if (__ht
._M_node_allocator() == this->_M_node_allocator())
1191 if (__ht
._M_uses_single_bucket())
1193 _M_buckets
= &_M_single_bucket
;
1194 _M_single_bucket
= __ht
._M_single_bucket
;
1197 _M_buckets
= __ht
._M_buckets
;
1199 _M_before_begin
._M_nxt
= __ht
._M_before_begin
._M_nxt
;
1200 // Update, if necessary, bucket pointing to before begin that hasn't
1203 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1209 [this](__node_type
* __n
)
1211 return this->_M_allocate_node(
1212 std::move_if_noexcept(__n
->_M_v()));
1218 template<typename _Key
, typename _Value
,
1219 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1220 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1222 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1223 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1224 ~_Hashtable() noexcept
1227 _M_deallocate_buckets();
1230 template<typename _Key
, typename _Value
,
1231 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1232 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1235 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1236 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1237 swap(_Hashtable
& __x
)
1238 noexcept(__node_alloc_traits::_S_nothrow_swap())
1240 // The only base class with member variables is hash_code_base.
1241 // We define _Hash_code_base::_M_swap because different
1242 // specializations have different members.
1245 std::__alloc_on_swap(this->_M_node_allocator(), __x
._M_node_allocator());
1246 std::swap(_M_rehash_policy
, __x
._M_rehash_policy
);
1248 // Deal properly with potentially moved instances.
1249 if (this->_M_uses_single_bucket())
1251 if (!__x
._M_uses_single_bucket())
1253 _M_buckets
= __x
._M_buckets
;
1254 __x
._M_buckets
= &__x
._M_single_bucket
;
1257 else if (__x
._M_uses_single_bucket())
1259 __x
._M_buckets
= _M_buckets
;
1260 _M_buckets
= &_M_single_bucket
;
1263 std::swap(_M_buckets
, __x
._M_buckets
);
1265 std::swap(_M_bucket_count
, __x
._M_bucket_count
);
1266 std::swap(_M_before_begin
._M_nxt
, __x
._M_before_begin
._M_nxt
);
1267 std::swap(_M_element_count
, __x
._M_element_count
);
1268 std::swap(_M_single_bucket
, __x
._M_single_bucket
);
1270 // Fix buckets containing the _M_before_begin pointers that can't be
1273 _M_buckets
[_M_bucket_index(_M_begin())] = &_M_before_begin
;
1276 __x
._M_buckets
[__x
._M_bucket_index(__x
._M_begin())]
1277 = &__x
._M_before_begin
;
1280 template<typename _Key
, typename _Value
,
1281 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1282 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1285 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1286 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1287 __rehash_policy(const _RehashPolicy
& __pol
)
1290 __pol
._M_need_rehash(_M_bucket_count
, _M_element_count
, 0);
1291 if (__do_rehash
.first
)
1292 _M_rehash(__do_rehash
.second
, _M_rehash_policy
._M_state());
1293 _M_rehash_policy
= __pol
;
1296 template<typename _Key
, typename _Value
,
1297 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1298 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1300 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1301 _H1
, _H2
, _Hash
, _RehashPolicy
,
1303 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1304 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1305 find(const key_type
& __k
)
1307 __hash_code __code
= this->_M_hash_code(__k
);
1308 std::size_t __n
= _M_bucket_index(__k
, __code
);
1309 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1310 return __p
? iterator(__p
) : end();
1313 template<typename _Key
, typename _Value
,
1314 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1315 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1317 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1318 _H1
, _H2
, _Hash
, _RehashPolicy
,
1319 _Traits
>::const_iterator
1320 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1321 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1322 find(const key_type
& __k
) const
1324 __hash_code __code
= this->_M_hash_code(__k
);
1325 std::size_t __n
= _M_bucket_index(__k
, __code
);
1326 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1327 return __p
? const_iterator(__p
) : end();
1330 template<typename _Key
, typename _Value
,
1331 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1332 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1334 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1335 _H1
, _H2
, _Hash
, _RehashPolicy
,
1337 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1338 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1339 count(const key_type
& __k
) const
1341 __hash_code __code
= this->_M_hash_code(__k
);
1342 std::size_t __n
= _M_bucket_index(__k
, __code
);
1343 __node_type
* __p
= _M_bucket_begin(__n
);
1347 std::size_t __result
= 0;
1348 for (;; __p
= __p
->_M_next())
1350 if (this->_M_equals(__k
, __code
, __p
))
1353 // All equivalent values are next to each other, if we
1354 // found a non-equivalent value after an equivalent one it
1355 // means that we won't find any new equivalent value.
1357 if (!__p
->_M_nxt
|| _M_bucket_index(__p
->_M_next()) != __n
)
1363 template<typename _Key
, typename _Value
,
1364 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1365 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1367 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1368 _ExtractKey
, _Equal
, _H1
,
1369 _H2
, _Hash
, _RehashPolicy
,
1371 typename _Hashtable
<_Key
, _Value
, _Alloc
,
1372 _ExtractKey
, _Equal
, _H1
,
1373 _H2
, _Hash
, _RehashPolicy
,
1375 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1376 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1377 equal_range(const key_type
& __k
)
1379 __hash_code __code
= this->_M_hash_code(__k
);
1380 std::size_t __n
= _M_bucket_index(__k
, __code
);
1381 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1385 __node_type
* __p1
= __p
->_M_next();
1386 while (__p1
&& _M_bucket_index(__p1
) == __n
1387 && this->_M_equals(__k
, __code
, __p1
))
1388 __p1
= __p1
->_M_next();
1390 return std::make_pair(iterator(__p
), iterator(__p1
));
1393 return std::make_pair(end(), end());
1396 template<typename _Key
, typename _Value
,
1397 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1398 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1400 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1401 _ExtractKey
, _Equal
, _H1
,
1402 _H2
, _Hash
, _RehashPolicy
,
1403 _Traits
>::const_iterator
,
1404 typename _Hashtable
<_Key
, _Value
, _Alloc
,
1405 _ExtractKey
, _Equal
, _H1
,
1406 _H2
, _Hash
, _RehashPolicy
,
1407 _Traits
>::const_iterator
>
1408 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1409 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1410 equal_range(const key_type
& __k
) const
1412 __hash_code __code
= this->_M_hash_code(__k
);
1413 std::size_t __n
= _M_bucket_index(__k
, __code
);
1414 __node_type
* __p
= _M_find_node(__n
, __k
, __code
);
1418 __node_type
* __p1
= __p
->_M_next();
1419 while (__p1
&& _M_bucket_index(__p1
) == __n
1420 && this->_M_equals(__k
, __code
, __p1
))
1421 __p1
= __p1
->_M_next();
1423 return std::make_pair(const_iterator(__p
), const_iterator(__p1
));
1426 return std::make_pair(end(), end());
1429 // Find the node whose key compares equal to k in the bucket n.
1430 // Return nullptr if no node is found.
1431 template<typename _Key
, typename _Value
,
1432 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1433 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1435 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1436 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
1437 _Traits
>::__node_base
*
1438 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1439 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1440 _M_find_before_node(size_type __n
, const key_type
& __k
,
1441 __hash_code __code
) const
1443 __node_base
* __prev_p
= _M_buckets
[__n
];
1447 for (__node_type
* __p
= static_cast<__node_type
*>(__prev_p
->_M_nxt
);;
1448 __p
= __p
->_M_next())
1450 if (this->_M_equals(__k
, __code
, __p
))
1453 if (!__p
->_M_nxt
|| _M_bucket_index(__p
->_M_next()) != __n
)
1460 template<typename _Key
, typename _Value
,
1461 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1462 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1465 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1466 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1467 _M_insert_bucket_begin(size_type __bkt
, __node_type
* __node
)
1469 if (_M_buckets
[__bkt
])
1471 // Bucket is not empty, we just need to insert the new node
1472 // after the bucket before begin.
1473 __node
->_M_nxt
= _M_buckets
[__bkt
]->_M_nxt
;
1474 _M_buckets
[__bkt
]->_M_nxt
= __node
;
1478 // The bucket is empty, the new node is inserted at the
1479 // beginning of the singly-linked list and the bucket will
1480 // contain _M_before_begin pointer.
1481 __node
->_M_nxt
= _M_before_begin
._M_nxt
;
1482 _M_before_begin
._M_nxt
= __node
;
1484 // We must update former begin bucket that is pointing to
1486 _M_buckets
[_M_bucket_index(__node
->_M_next())] = __node
;
1487 _M_buckets
[__bkt
] = &_M_before_begin
;
1491 template<typename _Key
, typename _Value
,
1492 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1493 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1496 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1497 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1498 _M_remove_bucket_begin(size_type __bkt
, __node_type
* __next
,
1499 size_type __next_bkt
)
1501 if (!__next
|| __next_bkt
!= __bkt
)
1503 // Bucket is now empty
1504 // First update next bucket if any
1506 _M_buckets
[__next_bkt
] = _M_buckets
[__bkt
];
1508 // Second update before begin node if necessary
1509 if (&_M_before_begin
== _M_buckets
[__bkt
])
1510 _M_before_begin
._M_nxt
= __next
;
1511 _M_buckets
[__bkt
] = nullptr;
1515 template<typename _Key
, typename _Value
,
1516 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1517 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1519 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1520 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
1521 _Traits
>::__node_base
*
1522 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1523 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1524 _M_get_previous_node(size_type __bkt
, __node_base
* __n
)
1526 __node_base
* __prev_n
= _M_buckets
[__bkt
];
1527 while (__prev_n
->_M_nxt
!= __n
)
1528 __prev_n
= __prev_n
->_M_nxt
;
1532 template<typename _Key
, typename _Value
,
1533 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1534 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1536 template<typename
... _Args
>
1537 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1538 _ExtractKey
, _Equal
, _H1
,
1539 _H2
, _Hash
, _RehashPolicy
,
1540 _Traits
>::iterator
, bool>
1541 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1542 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1543 _M_emplace(std::true_type
, _Args
&&... __args
)
1545 // First build the node to get access to the hash code
1546 __node_type
* __node
= this->_M_allocate_node(std::forward
<_Args
>(__args
)...);
1547 const key_type
& __k
= this->_M_extract()(__node
->_M_v());
1551 __code
= this->_M_hash_code(__k
);
1555 this->_M_deallocate_node(__node
);
1556 __throw_exception_again
;
1559 size_type __bkt
= _M_bucket_index(__k
, __code
);
1560 if (__node_type
* __p
= _M_find_node(__bkt
, __k
, __code
))
1562 // There is already an equivalent node, no insertion
1563 this->_M_deallocate_node(__node
);
1564 return std::make_pair(iterator(__p
), false);
1568 return std::make_pair(_M_insert_unique_node(__bkt
, __code
, __node
),
1572 template<typename _Key
, typename _Value
,
1573 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1574 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1576 template<typename
... _Args
>
1577 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1578 _H1
, _H2
, _Hash
, _RehashPolicy
,
1580 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1581 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1582 _M_emplace(const_iterator __hint
, std::false_type
, _Args
&&... __args
)
1584 // First build the node to get its hash code.
1585 __node_type
* __node
=
1586 this->_M_allocate_node(std::forward
<_Args
>(__args
)...);
1591 __code
= this->_M_hash_code(this->_M_extract()(__node
->_M_v()));
1595 this->_M_deallocate_node(__node
);
1596 __throw_exception_again
;
1599 return _M_insert_multi_node(__hint
._M_cur
, __code
, __node
);
1602 template<typename _Key
, typename _Value
,
1603 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1604 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1606 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1607 _H1
, _H2
, _Hash
, _RehashPolicy
,
1609 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1610 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1611 _M_insert_unique_node(size_type __bkt
, __hash_code __code
,
1612 __node_type
* __node
)
1614 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1615 std::pair
<bool, std::size_t> __do_rehash
1616 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
, _M_element_count
, 1);
1620 if (__do_rehash
.first
)
1622 _M_rehash(__do_rehash
.second
, __saved_state
);
1623 __bkt
= _M_bucket_index(this->_M_extract()(__node
->_M_v()), __code
);
1626 this->_M_store_code(__node
, __code
);
1628 // Always insert at the beginning of the bucket.
1629 _M_insert_bucket_begin(__bkt
, __node
);
1631 return iterator(__node
);
1635 this->_M_deallocate_node(__node
);
1636 __throw_exception_again
;
1640 // Insert node, in bucket bkt if no rehash (assumes no element with its key
1641 // already present). Take ownership of the node, deallocate it on exception.
1642 template<typename _Key
, typename _Value
,
1643 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1644 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1646 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1647 _H1
, _H2
, _Hash
, _RehashPolicy
,
1649 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1650 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1651 _M_insert_multi_node(__node_type
* __hint
, __hash_code __code
,
1652 __node_type
* __node
)
1654 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1655 std::pair
<bool, std::size_t> __do_rehash
1656 = _M_rehash_policy
._M_need_rehash(_M_bucket_count
, _M_element_count
, 1);
1660 if (__do_rehash
.first
)
1661 _M_rehash(__do_rehash
.second
, __saved_state
);
1663 this->_M_store_code(__node
, __code
);
1664 const key_type
& __k
= this->_M_extract()(__node
->_M_v());
1665 size_type __bkt
= _M_bucket_index(__k
, __code
);
1667 // Find the node before an equivalent one or use hint if it exists and
1668 // if it is equivalent.
1670 = __builtin_expect(__hint
!= nullptr, false)
1671 && this->_M_equals(__k
, __code
, __hint
)
1673 : _M_find_before_node(__bkt
, __k
, __code
);
1676 // Insert after the node before the equivalent one.
1677 __node
->_M_nxt
= __prev
->_M_nxt
;
1678 __prev
->_M_nxt
= __node
;
1679 if (__builtin_expect(__prev
== __hint
, false))
1680 // hint might be the last bucket node, in this case we need to
1681 // update next bucket.
1683 && !this->_M_equals(__k
, __code
, __node
->_M_next()))
1685 size_type __next_bkt
= _M_bucket_index(__node
->_M_next());
1686 if (__next_bkt
!= __bkt
)
1687 _M_buckets
[__next_bkt
] = __node
;
1691 // The inserted node has no equivalent in the
1692 // hashtable. We must insert the new node at the
1693 // beginning of the bucket to preserve equivalent
1694 // elements' relative positions.
1695 _M_insert_bucket_begin(__bkt
, __node
);
1697 return iterator(__node
);
1701 this->_M_deallocate_node(__node
);
1702 __throw_exception_again
;
1706 // Insert v if no element with its key is already present.
1707 template<typename _Key
, typename _Value
,
1708 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1709 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1711 template<typename _Arg
, typename _NodeGenerator
>
1712 std::pair
<typename _Hashtable
<_Key
, _Value
, _Alloc
,
1713 _ExtractKey
, _Equal
, _H1
,
1714 _H2
, _Hash
, _RehashPolicy
,
1715 _Traits
>::iterator
, bool>
1716 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1717 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1718 _M_insert(_Arg
&& __v
, const _NodeGenerator
& __node_gen
, std::true_type
)
1720 const key_type
& __k
= this->_M_extract()(__v
);
1721 __hash_code __code
= this->_M_hash_code(__k
);
1722 size_type __bkt
= _M_bucket_index(__k
, __code
);
1724 __node_type
* __n
= _M_find_node(__bkt
, __k
, __code
);
1726 return std::make_pair(iterator(__n
), false);
1728 __n
= __node_gen(std::forward
<_Arg
>(__v
));
1729 return std::make_pair(_M_insert_unique_node(__bkt
, __code
, __n
), true);
1732 // Insert v unconditionally.
1733 template<typename _Key
, typename _Value
,
1734 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1735 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1737 template<typename _Arg
, typename _NodeGenerator
>
1738 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1739 _H1
, _H2
, _Hash
, _RehashPolicy
,
1741 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1742 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1743 _M_insert(const_iterator __hint
, _Arg
&& __v
,
1744 const _NodeGenerator
& __node_gen
,
1747 // First compute the hash code so that we don't do anything if it
1749 __hash_code __code
= this->_M_hash_code(this->_M_extract()(__v
));
1751 // Second allocate new node so that we don't rehash if it throws.
1752 __node_type
* __node
= __node_gen(std::forward
<_Arg
>(__v
));
1754 return _M_insert_multi_node(__hint
._M_cur
, __code
, __node
);
1757 template<typename _Key
, typename _Value
,
1758 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1759 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1761 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1762 _H1
, _H2
, _Hash
, _RehashPolicy
,
1764 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1765 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1766 erase(const_iterator __it
)
1768 __node_type
* __n
= __it
._M_cur
;
1769 std::size_t __bkt
= _M_bucket_index(__n
);
1771 // Look for previous node to unlink it from the erased one, this
1772 // is why we need buckets to contain the before begin to make
1773 // this search fast.
1774 __node_base
* __prev_n
= _M_get_previous_node(__bkt
, __n
);
1775 return _M_erase(__bkt
, __prev_n
, __n
);
1778 template<typename _Key
, typename _Value
,
1779 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1780 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1782 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1783 _H1
, _H2
, _Hash
, _RehashPolicy
,
1785 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1786 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1787 _M_erase(size_type __bkt
, __node_base
* __prev_n
, __node_type
* __n
)
1789 if (__prev_n
== _M_buckets
[__bkt
])
1790 _M_remove_bucket_begin(__bkt
, __n
->_M_next(),
1791 __n
->_M_nxt
? _M_bucket_index(__n
->_M_next()) : 0);
1792 else if (__n
->_M_nxt
)
1794 size_type __next_bkt
= _M_bucket_index(__n
->_M_next());
1795 if (__next_bkt
!= __bkt
)
1796 _M_buckets
[__next_bkt
] = __prev_n
;
1799 __prev_n
->_M_nxt
= __n
->_M_nxt
;
1800 iterator
__result(__n
->_M_next());
1801 this->_M_deallocate_node(__n
);
1807 template<typename _Key
, typename _Value
,
1808 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1809 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1811 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1812 _H1
, _H2
, _Hash
, _RehashPolicy
,
1814 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1815 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1816 _M_erase(std::true_type
, const key_type
& __k
)
1818 __hash_code __code
= this->_M_hash_code(__k
);
1819 std::size_t __bkt
= _M_bucket_index(__k
, __code
);
1821 // Look for the node before the first matching node.
1822 __node_base
* __prev_n
= _M_find_before_node(__bkt
, __k
, __code
);
1826 // We found a matching node, erase it.
1827 __node_type
* __n
= static_cast<__node_type
*>(__prev_n
->_M_nxt
);
1828 _M_erase(__bkt
, __prev_n
, __n
);
1832 template<typename _Key
, typename _Value
,
1833 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1834 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1836 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1837 _H1
, _H2
, _Hash
, _RehashPolicy
,
1839 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1840 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1841 _M_erase(std::false_type
, const key_type
& __k
)
1843 __hash_code __code
= this->_M_hash_code(__k
);
1844 std::size_t __bkt
= _M_bucket_index(__k
, __code
);
1846 // Look for the node before the first matching node.
1847 __node_base
* __prev_n
= _M_find_before_node(__bkt
, __k
, __code
);
1851 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1852 // 526. Is it undefined if a function in the standard changes
1854 // We use one loop to find all matching nodes and another to deallocate
1855 // them so that the key stays valid during the first loop. It might be
1856 // invalidated indirectly when destroying nodes.
1857 __node_type
* __n
= static_cast<__node_type
*>(__prev_n
->_M_nxt
);
1858 __node_type
* __n_last
= __n
;
1859 std::size_t __n_last_bkt
= __bkt
;
1862 __n_last
= __n_last
->_M_next();
1865 __n_last_bkt
= _M_bucket_index(__n_last
);
1867 while (__n_last_bkt
== __bkt
&& this->_M_equals(__k
, __code
, __n_last
));
1869 // Deallocate nodes.
1870 size_type __result
= 0;
1873 __node_type
* __p
= __n
->_M_next();
1874 this->_M_deallocate_node(__n
);
1879 while (__n
!= __n_last
);
1881 if (__prev_n
== _M_buckets
[__bkt
])
1882 _M_remove_bucket_begin(__bkt
, __n_last
, __n_last_bkt
);
1883 else if (__n_last
&& __n_last_bkt
!= __bkt
)
1884 _M_buckets
[__n_last_bkt
] = __prev_n
;
1885 __prev_n
->_M_nxt
= __n_last
;
1889 template<typename _Key
, typename _Value
,
1890 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1891 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1893 typename _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1894 _H1
, _H2
, _Hash
, _RehashPolicy
,
1896 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1897 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1898 erase(const_iterator __first
, const_iterator __last
)
1900 __node_type
* __n
= __first
._M_cur
;
1901 __node_type
* __last_n
= __last
._M_cur
;
1902 if (__n
== __last_n
)
1903 return iterator(__n
);
1905 std::size_t __bkt
= _M_bucket_index(__n
);
1907 __node_base
* __prev_n
= _M_get_previous_node(__bkt
, __n
);
1908 bool __is_bucket_begin
= __n
== _M_bucket_begin(__bkt
);
1909 std::size_t __n_bkt
= __bkt
;
1914 __node_type
* __tmp
= __n
;
1915 __n
= __n
->_M_next();
1916 this->_M_deallocate_node(__tmp
);
1920 __n_bkt
= _M_bucket_index(__n
);
1922 while (__n
!= __last_n
&& __n_bkt
== __bkt
);
1923 if (__is_bucket_begin
)
1924 _M_remove_bucket_begin(__bkt
, __n
, __n_bkt
);
1925 if (__n
== __last_n
)
1927 __is_bucket_begin
= true;
1931 if (__n
&& (__n_bkt
!= __bkt
|| __is_bucket_begin
))
1932 _M_buckets
[__n_bkt
] = __prev_n
;
1933 __prev_n
->_M_nxt
= __n
;
1934 return iterator(__n
);
1937 template<typename _Key
, typename _Value
,
1938 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1939 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1942 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1943 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1946 this->_M_deallocate_nodes(_M_begin());
1947 __builtin_memset(_M_buckets
, 0, _M_bucket_count
* sizeof(__bucket_type
));
1948 _M_element_count
= 0;
1949 _M_before_begin
._M_nxt
= nullptr;
1952 template<typename _Key
, typename _Value
,
1953 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1954 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1957 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1958 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1959 rehash(size_type __n
)
1961 const __rehash_state
& __saved_state
= _M_rehash_policy
._M_state();
1962 std::size_t __buckets
1963 = std::max(_M_rehash_policy
._M_bkt_for_elements(_M_element_count
+ 1),
1965 __buckets
= _M_rehash_policy
._M_next_bkt(__buckets
);
1967 if (__buckets
!= _M_bucket_count
)
1968 _M_rehash(__buckets
, __saved_state
);
1970 // No rehash, restore previous state to keep a consistent state.
1971 _M_rehash_policy
._M_reset(__saved_state
);
1974 template<typename _Key
, typename _Value
,
1975 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1976 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
1979 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1980 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
1981 _M_rehash(size_type __n
, const __rehash_state
& __state
)
1985 _M_rehash_aux(__n
, __unique_keys());
1989 // A failure here means that buckets allocation failed. We only
1990 // have to restore hash policy previous state.
1991 _M_rehash_policy
._M_reset(__state
);
1992 __throw_exception_again
;
1996 // Rehash when there is no equivalent elements.
1997 template<typename _Key
, typename _Value
,
1998 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
1999 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
2002 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
2003 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
2004 _M_rehash_aux(size_type __n
, std::true_type
)
2006 __bucket_type
* __new_buckets
= _M_allocate_buckets(__n
);
2007 __node_type
* __p
= _M_begin();
2008 _M_before_begin
._M_nxt
= nullptr;
2009 std::size_t __bbegin_bkt
= 0;
2012 __node_type
* __next
= __p
->_M_next();
2013 std::size_t __bkt
= __hash_code_base::_M_bucket_index(__p
, __n
);
2014 if (!__new_buckets
[__bkt
])
2016 __p
->_M_nxt
= _M_before_begin
._M_nxt
;
2017 _M_before_begin
._M_nxt
= __p
;
2018 __new_buckets
[__bkt
] = &_M_before_begin
;
2020 __new_buckets
[__bbegin_bkt
] = __p
;
2021 __bbegin_bkt
= __bkt
;
2025 __p
->_M_nxt
= __new_buckets
[__bkt
]->_M_nxt
;
2026 __new_buckets
[__bkt
]->_M_nxt
= __p
;
2031 _M_deallocate_buckets();
2032 _M_bucket_count
= __n
;
2033 _M_buckets
= __new_buckets
;
2036 // Rehash when there can be equivalent elements, preserve their relative
2038 template<typename _Key
, typename _Value
,
2039 typename _Alloc
, typename _ExtractKey
, typename _Equal
,
2040 typename _H1
, typename _H2
, typename _Hash
, typename _RehashPolicy
,
2043 _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
2044 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>::
2045 _M_rehash_aux(size_type __n
, std::false_type
)
2047 __bucket_type
* __new_buckets
= _M_allocate_buckets(__n
);
2049 __node_type
* __p
= _M_begin();
2050 _M_before_begin
._M_nxt
= nullptr;
2051 std::size_t __bbegin_bkt
= 0;
2052 std::size_t __prev_bkt
= 0;
2053 __node_type
* __prev_p
= nullptr;
2054 bool __check_bucket
= false;
2058 __node_type
* __next
= __p
->_M_next();
2059 std::size_t __bkt
= __hash_code_base::_M_bucket_index(__p
, __n
);
2061 if (__prev_p
&& __prev_bkt
== __bkt
)
2063 // Previous insert was already in this bucket, we insert after
2064 // the previously inserted one to preserve equivalent elements
2066 __p
->_M_nxt
= __prev_p
->_M_nxt
;
2067 __prev_p
->_M_nxt
= __p
;
2069 // Inserting after a node in a bucket require to check that we
2070 // haven't change the bucket last node, in this case next
2071 // bucket containing its before begin node must be updated. We
2072 // schedule a check as soon as we move out of the sequence of
2073 // equivalent nodes to limit the number of checks.
2074 __check_bucket
= true;
2080 // Check if we shall update the next bucket because of
2081 // insertions into __prev_bkt bucket.
2082 if (__prev_p
->_M_nxt
)
2084 std::size_t __next_bkt
2085 = __hash_code_base::_M_bucket_index(__prev_p
->_M_next(),
2087 if (__next_bkt
!= __prev_bkt
)
2088 __new_buckets
[__next_bkt
] = __prev_p
;
2090 __check_bucket
= false;
2093 if (!__new_buckets
[__bkt
])
2095 __p
->_M_nxt
= _M_before_begin
._M_nxt
;
2096 _M_before_begin
._M_nxt
= __p
;
2097 __new_buckets
[__bkt
] = &_M_before_begin
;
2099 __new_buckets
[__bbegin_bkt
] = __p
;
2100 __bbegin_bkt
= __bkt
;
2104 __p
->_M_nxt
= __new_buckets
[__bkt
]->_M_nxt
;
2105 __new_buckets
[__bkt
]->_M_nxt
= __p
;
2113 if (__check_bucket
&& __prev_p
->_M_nxt
)
2115 std::size_t __next_bkt
2116 = __hash_code_base::_M_bucket_index(__prev_p
->_M_next(), __n
);
2117 if (__next_bkt
!= __prev_bkt
)
2118 __new_buckets
[__next_bkt
] = __prev_p
;
2121 _M_deallocate_buckets();
2122 _M_bucket_count
= __n
;
2123 _M_buckets
= __new_buckets
;
2126 _GLIBCXX_END_NAMESPACE_VERSION
2129 #endif // _HASHTABLE_H