libstdc++
bits/hashtable.h
Go to the documentation of this file.
1// hashtable.h header -*- C++ -*-
2
3// Copyright (C) 2007-2025 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
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}
28 */
29
30#ifndef _HASHTABLE_H
31#define _HASHTABLE_H 1
32
33#ifdef _GLIBCXX_SYSHDR
34#pragma GCC system_header
35#endif
36
39#include <bits/stl_algobase.h> // fill_n, is_permutation
40#include <bits/stl_function.h> // __has_is_transparent_t
41#if __cplusplus > 201402L
42# include <bits/node_handle.h>
43#endif
44
45#pragma GCC diagnostic push
46#pragma GCC diagnostic ignored "-Wc++11-extensions"
47
48namespace std _GLIBCXX_VISIBILITY(default)
49{
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51/// @cond undocumented
52
53 template<typename _Tp, typename _Hash>
54 using __cache_default
55 = __not_<__and_<// Do not cache for fast hasher.
56 __is_fast_hash<_Hash>,
57 // Mandatory for the rehash process.
58 __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
59
60 // Helper to conditionally delete the default constructor.
61 // The _Hash_node_base type is used to distinguish this specialization
62 // from any other potentially-overlapping subobjects of the hashtable.
63 template<typename _Equal, typename _Hash, typename _Allocator>
64 using _Hashtable_enable_default_ctor
65 = _Enable_default_constructor<__and_<is_default_constructible<_Equal>,
66 is_default_constructible<_Hash>,
67 is_default_constructible<_Allocator>>{},
68 __detail::_Hash_node_base>;
69
70 /**
71 * Primary class template _Hashtable.
72 *
73 * @ingroup hashtable-detail
74 *
75 * @tparam _Value CopyConstructible type.
76 *
77 * @tparam _Key CopyConstructible type.
78 *
79 * @tparam _Alloc An allocator type
80 * ([lib.allocator.requirements]) whose _Alloc::value_type is
81 * _Value. As a conforming extension, we allow for
82 * _Alloc::value_type != _Value.
83 *
84 * @tparam _ExtractKey Function object that takes an object of type
85 * _Value and returns a value of type _Key.
86 *
87 * @tparam _Equal Function object that takes two objects of type k
88 * and returns a bool-like value that is true if the two objects
89 * are considered equal.
90 *
91 * @tparam _Hash The hash function. A unary function object with
92 * argument type _Key and result type size_t. Return values should
93 * be distributed over the entire range [0, numeric_limits<size_t>:::max()].
94 *
95 * @tparam _RangeHash The range-hashing function (in the terminology of
96 * Tavori and Dreizin). A binary function object whose argument
97 * types and result type are all size_t. Given arguments r and N,
98 * the return value is in the range [0, N).
99 *
100 * @tparam _Unused Not used.
101 *
102 * @tparam _RehashPolicy Policy class with three members, all of
103 * which govern the bucket count. _M_next_bkt(n) returns a bucket
104 * count no smaller than n. _M_bkt_for_elements(n) returns a
105 * bucket count appropriate for an element count of n.
106 * _M_need_rehash(n_bkt, n_elt, n_ins) determines whether, if the
107 * current bucket count is n_bkt and the current element count is
108 * n_elt, we need to increase the bucket count for n_ins insertions.
109 * If so, returns make_pair(true, n), where n is the new bucket count. If
110 * not, returns make_pair(false, <anything>)
111 *
112 * @tparam _Traits Compile-time class with three boolean
113 * std::integral_constant members: __cache_hash_code, __constant_iterators,
114 * __unique_keys.
115 *
116 * Each _Hashtable data structure has:
117 *
118 * - _Bucket[] _M_buckets
119 * - _Hash_node_base _M_before_begin
120 * - size_type _M_bucket_count
121 * - size_type _M_element_count
122 *
123 * with _Bucket being _Hash_node_base* and _Hash_node containing:
124 *
125 * - _Hash_node* _M_next
126 * - Tp _M_value
127 * - size_t _M_hash_code if cache_hash_code is true
128 *
129 * In terms of Standard containers the hashtable is like the aggregation of:
130 *
131 * - std::forward_list<_Node> containing the elements
132 * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
133 *
134 * The non-empty buckets contain the node before the first node in the
135 * bucket. This design makes it possible to implement something like a
136 * std::forward_list::insert_after on container insertion and
137 * std::forward_list::erase_after on container erase
138 * calls. _M_before_begin is equivalent to
139 * std::forward_list::before_begin. Empty buckets contain
140 * nullptr. Note that one of the non-empty buckets contains
141 * &_M_before_begin which is not a dereferenceable node so the
142 * node pointer in a bucket shall never be dereferenced, only its
143 * next node can be.
144 *
145 * Walking through a bucket's nodes requires a check on the hash code to
146 * see if each node is still in the bucket. Such a design assumes a
147 * quite efficient hash functor and is one of the reasons it is
148 * highly advisable to set __cache_hash_code to true.
149 *
150 * The container iterators are simply built from nodes. This way
151 * incrementing the iterator is perfectly efficient independent of
152 * how many empty buckets there are in the container.
153 *
154 * On insert we compute the element's hash code and use it to find the
155 * bucket index. If the element must be inserted in an empty bucket
156 * we add it at the beginning of the singly linked list and make the
157 * bucket point to _M_before_begin. The bucket that used to point to
158 * _M_before_begin, if any, is updated to point to its new before
159 * begin node.
160 *
161 * Note that all equivalent values, if any, are next to each other, if
162 * we find a non-equivalent value after an equivalent one it means that
163 * we won't find any new equivalent value.
164 *
165 * On erase, the simple iterator design requires using the hash
166 * functor to get the index of the bucket to update. For this
167 * reason, when __cache_hash_code is set to false the hash functor must
168 * not throw and this is enforced by a static assertion.
169 *
170 * Functionality is implemented by decomposition into base classes,
171 * where the derived _Hashtable class is used in _Map_base and
172 * _Rehash_base base classes to access the
173 * "this" pointer. _Hashtable_base is used in the base classes as a
174 * non-recursive, fully-completed-type so that detailed nested type
175 * information, such as iterator type and node type, can be
176 * used. This is similar to the "Curiously Recurring Template
177 * Pattern" (CRTP) technique, but uses a reconstructed, not
178 * explicitly passed, template pattern.
179 *
180 * Base class templates are:
181 * - __detail::_Hashtable_base
182 * - __detail::_Map_base
183 * - __detail::_Rehash_base
184 */
185 template<typename _Key, typename _Value, typename _Alloc,
186 typename _ExtractKey, typename _Equal,
187 typename _Hash, typename _RangeHash, typename _Unused,
188 typename _RehashPolicy, typename _Traits>
189 class _Hashtable
190 : public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
191 _Hash, _RangeHash, _Unused, _Traits>,
192 public __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
193 _Hash, _RangeHash, _Unused,
194 _RehashPolicy, _Traits>,
195 public __detail::_Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
196 _Hash, _RangeHash, _Unused,
197 _RehashPolicy, _Traits>,
198 private __detail::_Hashtable_alloc<
199 __alloc_rebind<_Alloc,
200 __detail::_Hash_node<_Value,
201 _Traits::__hash_cached::value>>>,
202 private _Hashtable_enable_default_ctor<_Equal, _Hash, _Alloc>
203 {
204 static_assert(is_same<typename remove_cv<_Value>::type, _Value>::value,
205 "unordered container must have a non-const, non-volatile value_type");
206#if __cplusplus > 201703L || defined __STRICT_ANSI__
207 static_assert(is_same<typename _Alloc::value_type, _Value>{},
208 "unordered container must have the same value_type as its allocator");
209#endif
210 static_assert(is_copy_constructible<_Hash>::value,
211 "hash function must be copy constructible");
212
213 using __traits_type = _Traits;
214 using __hash_cached = typename __traits_type::__hash_cached;
215 using __constant_iterators = typename __traits_type::__constant_iterators;
216 using __node_type = __detail::_Hash_node<_Value, __hash_cached::value>;
217 using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
218
219 using __hashtable_alloc = __detail::_Hashtable_alloc<__node_alloc_type>;
220
221 using __node_value_type =
222 __detail::_Hash_node_value<_Value, __hash_cached::value>;
223 using __node_ptr = typename __hashtable_alloc::__node_ptr;
224 using __value_alloc_traits =
225 typename __hashtable_alloc::__value_alloc_traits;
226 using __node_alloc_traits =
227 typename __hashtable_alloc::__node_alloc_traits;
228 using __node_base = typename __hashtable_alloc::__node_base;
229 using __node_base_ptr = typename __hashtable_alloc::__node_base_ptr;
230 using __buckets_ptr = typename __hashtable_alloc::__buckets_ptr;
231
232 using __enable_default_ctor
233 = _Hashtable_enable_default_ctor<_Equal, _Hash, _Alloc>;
234 using __rehash_guard_t
235 = __detail::_RehashStateGuard<_RehashPolicy>;
236
237 public:
238 typedef _Key key_type;
239 typedef _Value value_type;
240 typedef _Alloc allocator_type;
241 typedef _Equal key_equal;
242
243 // mapped_type, if present, comes from _Map_base.
244 // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
245 typedef typename __value_alloc_traits::pointer pointer;
246 typedef typename __value_alloc_traits::const_pointer const_pointer;
247 typedef value_type& reference;
248 typedef const value_type& const_reference;
249
250 using iterator
251 = __detail::_Node_iterator<_Value, __constant_iterators::value,
252 __hash_cached::value>;
253
254 using const_iterator
255 = __detail::_Node_const_iterator<_Value, __constant_iterators::value,
256 __hash_cached::value>;
257
258 using local_iterator = __detail::_Local_iterator<key_type, _Value,
259 _ExtractKey, _Hash, _RangeHash, _Unused,
260 __constant_iterators::value,
261 __hash_cached::value>;
262
263 using const_local_iterator = __detail::_Local_const_iterator<
264 key_type, _Value,
265 _ExtractKey, _Hash, _RangeHash, _Unused,
266 __constant_iterators::value, __hash_cached::value>;
267
268 private:
269 using __rehash_type = _RehashPolicy;
270
271 using __unique_keys = typename __traits_type::__unique_keys;
272
273 using __hashtable_base = __detail::
274 _Hashtable_base<_Key, _Value, _ExtractKey,
275 _Equal, _Hash, _RangeHash, _Unused, _Traits>;
276
277 using __hash_code_base = typename __hashtable_base::__hash_code_base;
278 using __hash_code = typename __hashtable_base::__hash_code;
279 using __ireturn_type = __conditional_t<__unique_keys::value,
281 iterator>;
282
283 using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
284 _Equal, _Hash, _RangeHash, _Unused,
285 _RehashPolicy, _Traits>;
286
287 using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
288 _ExtractKey, _Equal,
289 _Hash, _RangeHash, _Unused,
290 _RehashPolicy, _Traits>;
291
292 using __node_builder_t = __detail::_NodeBuilder<_ExtractKey>;
293
294 // Simple RAII type for managing a node containing an element
295 struct _Scoped_node
296 {
297 // Take ownership of a node with a constructed element.
298 _Scoped_node(__node_ptr __n, __hashtable_alloc* __h)
299 : _M_h(__h), _M_node(__n) { }
300
301 // Allocate a node and construct an element within it.
302 template<typename... _Args>
303 _Scoped_node(__hashtable_alloc* __h, _Args&&... __args)
304 : _M_h(__h),
305 _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...))
306 { }
307
308 // Destroy element and deallocate node.
309 ~_Scoped_node() { if (_M_node) _M_h->_M_deallocate_node(_M_node); };
310
311 _Scoped_node(const _Scoped_node&) = delete;
312 _Scoped_node& operator=(const _Scoped_node&) = delete;
313
314 __hashtable_alloc* _M_h;
315 __node_ptr _M_node;
316 };
317
318 // Compile-time diagnostics.
319
320 // _Hash_code_base has everything protected, so use this derived type to
321 // access it.
322 struct __hash_code_base_access : __hash_code_base
323 { using __hash_code_base::_M_bucket_index; };
324
325 // To get bucket index we need _RangeHash to be non-throwing.
326 static_assert(is_nothrow_default_constructible<_RangeHash>::value,
327 "Functor used to map hash code to bucket index"
328 " must be nothrow default constructible");
329 static_assert(noexcept(
330 std::declval<const _RangeHash&>()((std::size_t)0, (std::size_t)0)),
331 "Functor used to map hash code to bucket index must be"
332 " noexcept");
333
334 // To compute bucket index we also need _ExtractKey to be non-throwing.
335 static_assert(is_nothrow_default_constructible<_ExtractKey>::value,
336 "_ExtractKey must be nothrow default constructible");
337 static_assert(noexcept(
338 std::declval<const _ExtractKey&>()(std::declval<_Value>())),
339 "_ExtractKey functor must be noexcept invocable");
340
341 template<typename _Keya, typename _Valuea, typename _Alloca,
342 typename _ExtractKeya, typename _Equala,
343 typename _Hasha, typename _RangeHasha, typename _Unuseda,
344 typename _RehashPolicya, typename _Traitsa,
345 bool _Unique_keysa>
346 friend struct __detail::_Map_base;
347
348 public:
349 using size_type = typename __hashtable_base::size_type;
350 using difference_type = typename __hashtable_base::difference_type;
351
352#if __cplusplus > 201402L
353 using node_type = _Node_handle<_Key, _Value, __node_alloc_type>;
354 using insert_return_type = _Node_insert_return<iterator, node_type>;
355#endif
356
357 private:
358 __buckets_ptr _M_buckets = &_M_single_bucket;
359 size_type _M_bucket_count = 1;
360 __node_base _M_before_begin;
361 size_type _M_element_count = 0;
362 _RehashPolicy _M_rehash_policy;
363
364 // A single bucket used when only need for 1 bucket. Especially
365 // interesting in move semantic to leave hashtable with only 1 bucket
366 // which is not allocated so that we can have those operations noexcept
367 // qualified.
368 // Note that we can't leave hashtable with 0 bucket without adding
369 // numerous checks in the code to avoid 0 modulus.
370 __node_base_ptr _M_single_bucket = nullptr;
371
372 void
373 _M_update_bbegin()
374 {
375 if (auto __begin = _M_begin())
376 _M_buckets[_M_bucket_index(*__begin)] = &_M_before_begin;
377 }
378
379 void
380 _M_update_bbegin(__node_ptr __n)
381 {
382 _M_before_begin._M_nxt = __n;
383 _M_update_bbegin();
384 }
385
386 bool
387 _M_uses_single_bucket(__buckets_ptr __bkts) const
388 { return __builtin_expect(__bkts == &_M_single_bucket, false); }
389
390 bool
391 _M_uses_single_bucket() const
392 { return _M_uses_single_bucket(_M_buckets); }
393
394 static constexpr size_t
395 __small_size_threshold() noexcept
396 {
397 return
398 __detail::_Hashtable_hash_traits<_Hash>::__small_size_threshold();
399 }
400
401 __hashtable_alloc&
402 _M_base_alloc() { return *this; }
403
404 __buckets_ptr
405 _M_allocate_buckets(size_type __bkt_count)
406 {
407 if (__builtin_expect(__bkt_count == 1, false))
408 {
409 _M_single_bucket = nullptr;
410 return &_M_single_bucket;
411 }
412
413 return __hashtable_alloc::_M_allocate_buckets(__bkt_count);
414 }
415
416 void
417 _M_deallocate_buckets(__buckets_ptr __bkts, size_type __bkt_count)
418 {
419 if (_M_uses_single_bucket(__bkts))
420 return;
421
422 __hashtable_alloc::_M_deallocate_buckets(__bkts, __bkt_count);
423 }
424
425 void
426 _M_deallocate_buckets()
427 { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
428
429 // Gets bucket begin, deals with the fact that non-empty buckets contain
430 // their before begin node.
431 __node_ptr
432 _M_bucket_begin(size_type __bkt) const
433 {
434 __node_base_ptr __n = _M_buckets[__bkt];
435 return __n ? static_cast<__node_ptr>(__n->_M_nxt) : nullptr;
436 }
437
438 __node_ptr
439 _M_begin() const
440 { return static_cast<__node_ptr>(_M_before_begin._M_nxt); }
441
442 // Assign *this using another _Hashtable instance. Whether elements
443 // are copied or moved depends on the _Ht reference.
444 template<typename _Ht>
445 void
446 _M_assign_elements(_Ht&&);
447
448 template<typename _Ht>
449 void
450 _M_assign(_Ht&& __ht)
451 {
452 __detail::_AllocNode<__node_alloc_type> __alloc_node_gen(*this);
453 _M_assign(std::forward<_Ht>(__ht), __alloc_node_gen);
454 }
455
456 template<typename _Ht, typename _NodeGenerator>
457 void
458 _M_assign(_Ht&&, _NodeGenerator&);
459
460 void
461 _M_move_assign(_Hashtable&&, true_type);
462
463 void
464 _M_move_assign(_Hashtable&&, false_type);
465
466 void
467 _M_reset() noexcept;
468
469 _Hashtable(const _Hash& __h, const _Equal& __eq,
470 const allocator_type& __a)
471 : __hashtable_base(__h, __eq),
472 __hashtable_alloc(__node_alloc_type(__a)),
473 __enable_default_ctor(_Enable_default_constructor_tag{})
474 { }
475
476 template<bool _No_realloc = true>
477 static constexpr bool
478 _S_nothrow_move()
479 {
480#if __cplusplus <= 201402L
481 return __and_<__bool_constant<_No_realloc>,
482 is_nothrow_copy_constructible<_Hash>,
483 is_nothrow_copy_constructible<_Equal>>::value;
484#else
485 if constexpr (_No_realloc)
486 if constexpr (is_nothrow_copy_constructible<_Hash>())
487 return is_nothrow_copy_constructible<_Equal>();
488 return false;
489#endif
490 }
491
492 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
493 true_type /* alloc always equal */)
494 noexcept(_S_nothrow_move());
495
496 _Hashtable(_Hashtable&&, __node_alloc_type&&,
497 false_type /* alloc always equal */);
498
499 template<typename _InputIterator>
500 _Hashtable(_InputIterator __first, _InputIterator __last,
501 size_type __bkt_count_hint,
502 const _Hash&, const _Equal&, const allocator_type&,
503 true_type __uks);
504
505 template<typename _InputIterator>
506 _Hashtable(_InputIterator __first, _InputIterator __last,
507 size_type __bkt_count_hint,
508 const _Hash&, const _Equal&, const allocator_type&,
509 false_type __uks);
510
511 public:
512 // Constructor, destructor, assignment, swap
513 _Hashtable() = default;
514
515 _Hashtable(const _Hashtable&);
516
517 _Hashtable(const _Hashtable&, const allocator_type&);
518
519 explicit
520 _Hashtable(size_type __bkt_count_hint,
521 const _Hash& __hf = _Hash(),
522 const key_equal& __eql = key_equal(),
523 const allocator_type& __a = allocator_type());
524
525 // Use delegating constructors.
526 _Hashtable(_Hashtable&& __ht)
527 noexcept(_S_nothrow_move())
528 : _Hashtable(std::move(__ht), std::move(__ht._M_node_allocator()),
529 true_type{})
530 { }
531
532 _Hashtable(_Hashtable&& __ht, const allocator_type& __a)
533 noexcept(_S_nothrow_move<__node_alloc_traits::_S_always_equal()>())
534 : _Hashtable(std::move(__ht), __node_alloc_type(__a),
535 typename __node_alloc_traits::is_always_equal{})
536 { }
537
538 explicit
539 _Hashtable(const allocator_type& __a)
540 : __hashtable_alloc(__node_alloc_type(__a)),
541 __enable_default_ctor(_Enable_default_constructor_tag{})
542 { }
543
544 template<typename _InputIterator>
545 _Hashtable(_InputIterator __f, _InputIterator __l,
546 size_type __bkt_count_hint = 0,
547 const _Hash& __hf = _Hash(),
548 const key_equal& __eql = key_equal(),
549 const allocator_type& __a = allocator_type())
550 : _Hashtable(__f, __l, __bkt_count_hint, __hf, __eql, __a,
551 __unique_keys{})
552 { }
553
554 _Hashtable(initializer_list<value_type> __l,
555 size_type __bkt_count_hint = 0,
556 const _Hash& __hf = _Hash(),
557 const key_equal& __eql = key_equal(),
558 const allocator_type& __a = allocator_type())
559 : _Hashtable(__l.begin(), __l.end(), __bkt_count_hint,
560 __hf, __eql, __a, __unique_keys{})
561 { }
562
563 _Hashtable&
564 operator=(const _Hashtable& __ht);
565
566 _Hashtable&
567 operator=(_Hashtable&& __ht)
568 noexcept(__node_alloc_traits::_S_nothrow_move()
569 && is_nothrow_move_assignable<_Hash>::value
570 && is_nothrow_move_assignable<_Equal>::value)
571 {
572 constexpr bool __move_storage =
573 __node_alloc_traits::_S_propagate_on_move_assign()
574 || __node_alloc_traits::_S_always_equal();
575 _M_move_assign(std::move(__ht), __bool_constant<__move_storage>());
576 return *this;
577 }
578
579#pragma GCC diagnostic push
580#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
581 _Hashtable&
582 operator=(initializer_list<value_type> __l)
583 {
584 using __reuse_or_alloc_node_gen_t =
585 __detail::_ReuseOrAllocNode<__node_alloc_type>;
586
587 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *this);
588 _M_before_begin._M_nxt = nullptr;
589 clear();
590
591 // We assume that all elements of __l are likely to be inserted.
592 auto __l_bkt_count = _M_rehash_policy._M_bkt_for_elements(__l.size());
593
594 // Excess buckets might have been intentionally reserved by the user,
595 // so rehash if we need to grow, but don't shrink.
596 if (_M_bucket_count < __l_bkt_count)
597 rehash(__l_bkt_count);
598
599 __hash_code __code;
600 size_type __bkt;
601 for (auto& __e : __l)
602 {
603 const key_type& __k = _ExtractKey{}(__e);
604
605 if constexpr (__unique_keys::value)
606 {
607 if (auto __loc = _M_locate(__k))
608 continue; // Found existing element with equivalent key
609 else
610 {
611 __code = __loc._M_hash_code;
612 __bkt = __loc._M_bucket_index;
613 }
614 }
615 else
616 {
617 __code = this->_M_hash_code(__k);
618 __bkt = _M_bucket_index(__code);
619 }
620
621 _M_insert_unique_node(__bkt, __code, __roan(__e));
622 }
623
624 return *this;
625 }
626#pragma GCC diagnostic pop
627
628 ~_Hashtable() noexcept;
629
630 void
631 swap(_Hashtable&)
632 noexcept(__and_<__is_nothrow_swappable<_Hash>,
633 __is_nothrow_swappable<_Equal>>::value);
634
635 // Basic container operations
636 iterator
637 begin() noexcept
638 { return iterator(_M_begin()); }
639
640 const_iterator
641 begin() const noexcept
642 { return const_iterator(_M_begin()); }
643
644 iterator
645 end() noexcept
646 { return iterator(nullptr); }
647
648 const_iterator
649 end() const noexcept
650 { return const_iterator(nullptr); }
651
652 const_iterator
653 cbegin() const noexcept
654 { return const_iterator(_M_begin()); }
655
656 const_iterator
657 cend() const noexcept
658 { return const_iterator(nullptr); }
659
660 size_type
661 size() const noexcept
662 { return _M_element_count; }
663
664 _GLIBCXX_NODISCARD bool
665 empty() const noexcept
666 { return size() == 0; }
667
668 allocator_type
669 get_allocator() const noexcept
670 { return allocator_type(this->_M_node_allocator()); }
671
672 size_type
673 max_size() const noexcept
674 { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
675
676 // Observers
677 key_equal
678 key_eq() const
679 { return this->_M_eq(); }
680
681 // hash_function, if present, comes from _Hash_code_base.
682
683 // Bucket operations
684 size_type
685 bucket_count() const noexcept
686 { return _M_bucket_count; }
687
688 size_type
689 max_bucket_count() const noexcept
690 { return max_size(); }
691
692 size_type
693 bucket_size(size_type __bkt) const
694 { return std::distance(begin(__bkt), end(__bkt)); }
695
696 size_type
697 bucket(const key_type& __k) const
698 { return _M_bucket_index(this->_M_hash_code(__k)); }
699
700 local_iterator
701 begin(size_type __bkt)
702 {
703 return local_iterator(*this, _M_bucket_begin(__bkt),
704 __bkt, _M_bucket_count);
705 }
706
707 local_iterator
708 end(size_type __bkt)
709 { return local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
710
711 const_local_iterator
712 begin(size_type __bkt) const
713 {
714 return const_local_iterator(*this, _M_bucket_begin(__bkt),
715 __bkt, _M_bucket_count);
716 }
717
718 const_local_iterator
719 end(size_type __bkt) const
720 { return const_local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
721
722 // DR 691.
723 const_local_iterator
724 cbegin(size_type __bkt) const
725 {
726 return const_local_iterator(*this, _M_bucket_begin(__bkt),
727 __bkt, _M_bucket_count);
728 }
729
730 const_local_iterator
731 cend(size_type __bkt) const
732 { return const_local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
733
734 float
735 load_factor() const noexcept
736 {
737 return static_cast<float>(size()) / static_cast<float>(bucket_count());
738 }
739
740 // max_load_factor, if present, comes from _Rehash_base.
741
742 // Generalization of max_load_factor. Extension, not found in
743 // TR1. Only useful if _RehashPolicy is something other than
744 // the default.
745 const _RehashPolicy&
746 __rehash_policy() const
747 { return _M_rehash_policy; }
748
749 void
750 __rehash_policy(const _RehashPolicy& __pol)
751 { _M_rehash_policy = __pol; }
752
753 // Lookup.
754 iterator
755 find(const key_type& __k);
756
757 const_iterator
758 find(const key_type& __k) const;
759
760 size_type
761 count(const key_type& __k) const;
762
764 equal_range(const key_type& __k);
765
767 equal_range(const key_type& __k) const;
768
769#ifdef __glibcxx_generic_unordered_lookup // C++ >= 20 && HOSTED
770 template<typename _Kt,
771 typename = __has_is_transparent_t<_Hash, _Kt>,
772 typename = __has_is_transparent_t<_Equal, _Kt>>
773 iterator
774 _M_find_tr(const _Kt& __k);
775
776 template<typename _Kt,
777 typename = __has_is_transparent_t<_Hash, _Kt>,
778 typename = __has_is_transparent_t<_Equal, _Kt>>
779 const_iterator
780 _M_find_tr(const _Kt& __k) const;
781
782 template<typename _Kt,
783 typename = __has_is_transparent_t<_Hash, _Kt>,
784 typename = __has_is_transparent_t<_Equal, _Kt>>
785 size_type
786 _M_count_tr(const _Kt& __k) const;
787
788 template<typename _Kt,
789 typename = __has_is_transparent_t<_Hash, _Kt>,
790 typename = __has_is_transparent_t<_Equal, _Kt>>
791 pair<iterator, iterator>
792 _M_equal_range_tr(const _Kt& __k);
793
794 template<typename _Kt,
795 typename = __has_is_transparent_t<_Hash, _Kt>,
796 typename = __has_is_transparent_t<_Equal, _Kt>>
797 pair<const_iterator, const_iterator>
798 _M_equal_range_tr(const _Kt& __k) const;
799#endif // __glibcxx_generic_unordered_lookup
800
801 private:
802 // Bucket index computation helpers.
803 size_type
804 _M_bucket_index(const __node_value_type& __n) const noexcept
805 { return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
806
807 size_type
808 _M_bucket_index(__hash_code __c) const
809 { return __hash_code_base::_M_bucket_index(__c, _M_bucket_count); }
810
811 // Find and insert helper functions and types
812
813 // Find the node before the one matching the criteria.
814 __node_base_ptr
815 _M_find_before_node(size_type, const key_type&, __hash_code) const;
816
817 template<typename _Kt>
818 __node_base_ptr
819 _M_find_before_node_tr(size_type, const _Kt&, __hash_code) const;
820
821 // A pointer to a particular node and/or a hash code and bucket index
822 // where such a node would be found in the container.
823 struct __location_type
824 {
825 // True if _M_node() is a valid node pointer.
826 explicit operator bool() const noexcept
827 { return static_cast<bool>(_M_before); }
828
829 // An iterator that refers to the node, or end().
830 explicit operator iterator() const noexcept
831 { return iterator(_M_node()); }
832
833 // A const_iterator that refers to the node, or cend().
834 explicit operator const_iterator() const noexcept
835 { return const_iterator(_M_node()); }
836
837 // A pointer to the node, or null.
838 __node_ptr _M_node() const
839 {
840 if (_M_before)
841 return static_cast<__node_ptr>(_M_before->_M_nxt);
842 return __node_ptr();
843 }
844
845 __node_base_ptr _M_before{}; // Must only be used to get _M_nxt
846 __hash_code _M_hash_code{}; // Only valid if _M_bucket_index != -1
847 size_type _M_bucket_index = size_type(-1);
848 };
849
850 // Adaptive lookup to find key, or which bucket it would be in.
851 // For a container smaller than the small size threshold use a linear
852 // search through the whole container, just testing for equality.
853 // Otherwise, calculate the hash code and bucket index for the key,
854 // and search in that bucket.
855 // The return value will have a pointer to the node _before_ the first
856 // node matching the key, if any such node exists. Returning the node
857 // before the desired one allows the result to be used for erasure.
858 // If no matching element is present, the hash code and bucket for the
859 // key will be set, allowing a new node to be inserted at that location.
860 // (The hash code and bucket might also be set when a node is found.)
861 // The _M_before pointer might point to _M_before_begin, so must not be
862 // cast to __node_ptr, and it must not be used to modify *_M_before
863 // except in non-const member functions, such as erase.
864 __location_type
865 _M_locate(const key_type& __k) const;
866
867 __node_ptr
868 _M_find_node(size_type __bkt, const key_type& __key,
869 __hash_code __c) const
870 {
871 if (__node_base_ptr __before_n = _M_find_before_node(__bkt, __key, __c))
872 return static_cast<__node_ptr>(__before_n->_M_nxt);
873 return nullptr;
874 }
875
876 template<typename _Kt>
877 __node_ptr
878 _M_find_node_tr(size_type __bkt, const _Kt& __key,
879 __hash_code __c) const
880 {
881 if (auto __before_n = _M_find_before_node_tr(__bkt, __key, __c))
882 return static_cast<__node_ptr>(__before_n->_M_nxt);
883 return nullptr;
884 }
885
886 // Insert a node at the beginning of a bucket.
887 void
888 _M_insert_bucket_begin(size_type __bkt, __node_ptr __node)
889 {
890 if (_M_buckets[__bkt])
891 {
892 // Bucket is not empty, we just need to insert the new node
893 // after the bucket before begin.
894 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
895 _M_buckets[__bkt]->_M_nxt = __node;
896 }
897 else
898 {
899 // The bucket is empty, the new node is inserted at the
900 // beginning of the singly-linked list and the bucket will
901 // contain _M_before_begin pointer.
902 __node->_M_nxt = _M_before_begin._M_nxt;
903 _M_before_begin._M_nxt = __node;
904
905 if (__node->_M_nxt)
906 // We must update former begin bucket that is pointing to
907 // _M_before_begin.
908 _M_buckets[_M_bucket_index(*__node->_M_next())] = __node;
909
910 _M_buckets[__bkt] = &_M_before_begin;
911 }
912 }
913
914 // Remove the bucket first node
915 void
916 _M_remove_bucket_begin(size_type __bkt, __node_ptr __next_n,
917 size_type __next_bkt)
918 {
919 if (!__next_n)
920 _M_buckets[__bkt] = nullptr;
921 else if (__next_bkt != __bkt)
922 {
923 _M_buckets[__next_bkt] = _M_buckets[__bkt];
924 _M_buckets[__bkt] = nullptr;
925 }
926 }
927
928 // Get the node before __n in the bucket __bkt
929 __node_base_ptr
930 _M_get_previous_node(size_type __bkt, __node_ptr __n);
931
932 pair<__node_ptr, __hash_code>
933 _M_compute_hash_code(__node_ptr __hint, const key_type& __k) const;
934
935 // Insert node __n with hash code __code, in bucket __bkt (or another
936 // bucket if rehashing is needed).
937 // Assumes no element with equivalent key is already present.
938 // Takes ownership of __n if insertion succeeds, throws otherwise.
939 // __n_elt is an estimated number of elements we expect to insert,
940 // used as a hint for rehashing when inserting a range.
941 iterator
942 _M_insert_unique_node(size_type __bkt, __hash_code,
943 __node_ptr __n, size_type __n_elt = 1);
944
945 // Insert node __n with key __k and hash code __code.
946 // Takes ownership of __n if insertion succeeds, throws otherwise.
947 iterator
948 _M_insert_multi_node(__node_ptr __hint,
949 __hash_code __code, __node_ptr __n);
950
951 template<typename... _Args>
953 _M_emplace_uniq(_Args&&... __args);
954
955#pragma GCC diagnostic push
956#pragma GCC diagnostic ignored "-Wc++14-extensions" // variable templates
957 template<typename _Arg, typename _DArg = __remove_cvref_t<_Arg>,
958 typename = _ExtractKey>
959 static constexpr bool __is_key_type = false;
960
961 template<typename _Arg>
962 static constexpr bool
963 __is_key_type<_Arg, key_type, __detail::_Identity> = true;
964
965 template<typename _Arg, typename _Arg1, typename _Arg2>
966 static constexpr bool
967 __is_key_type<_Arg, pair<_Arg1, _Arg2>, __detail::_Select1st>
968 = is_same<__remove_cvref_t<_Arg1>, key_type>::value;
969#pragma GCC diagnostic pop
970
971 template<typename... _Args>
972 iterator
973 _M_emplace_multi(const_iterator, _Args&&... __args);
974
975 iterator
976 _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n);
977
978 template<typename _InputIterator>
979 void
980 _M_insert_range_multi(_InputIterator __first, _InputIterator __last);
981
982 public:
983#pragma GCC diagnostic push
984#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
985 // Emplace
986 template<typename... _Args>
987 __ireturn_type
988 emplace(_Args&&... __args)
989 {
990 if constexpr (__unique_keys::value)
991 return _M_emplace_uniq(std::forward<_Args>(__args)...);
992 else
993 return _M_emplace_multi(cend(), std::forward<_Args>(__args)...);
994 }
995
996 template<typename... _Args>
997 iterator
998 emplace_hint(const_iterator __hint, _Args&&... __args)
999 {
1000 if constexpr (__unique_keys::value)
1001 return _M_emplace_uniq(std::forward<_Args>(__args)...).first;
1002 else
1003 return _M_emplace_multi(__hint, std::forward<_Args>(__args)...);
1004 }
1005
1006 // Insert
1007 __ireturn_type
1008 insert(const value_type& __v)
1009 {
1010 if constexpr (__unique_keys::value)
1011 return _M_emplace_uniq(__v);
1012 else
1013 return _M_emplace_multi(cend(), __v);
1014 }
1015
1016 iterator
1017 insert(const_iterator __hint, const value_type& __v)
1018 {
1019 if constexpr (__unique_keys::value)
1020 return _M_emplace_uniq(__v).first;
1021 else
1022 return _M_emplace_multi(__hint, __v);
1023 }
1024
1025 __ireturn_type
1026 insert(value_type&& __v)
1027 {
1028 if constexpr (__unique_keys::value)
1029 return _M_emplace_uniq(std::move(__v));
1030 else
1031 return _M_emplace_multi(cend(), std::move(__v));
1032 }
1033
1034 iterator
1035 insert(const_iterator __hint, value_type&& __v)
1036 {
1037 if constexpr (__unique_keys::value)
1038 return _M_emplace_uniq(std::move(__v)).first;
1039 else
1040 return _M_emplace_multi(__hint, std::move(__v));
1041 }
1042
1043#ifdef __glibcxx_unordered_map_try_emplace // C++ >= 17 && HOSTED
1044 template<typename _KType, typename... _Args>
1046 try_emplace(const_iterator, _KType&& __k, _Args&&... __args)
1047 {
1048 __hash_code __code;
1049 size_type __bkt;
1050 if (auto __loc = _M_locate(__k))
1051 return { iterator(__loc), false };
1052 else
1053 {
1054 __code = __loc._M_hash_code;
1055 __bkt = __loc._M_bucket_index;
1056 }
1057
1058 _Scoped_node __node {
1059 this,
1061 std::forward_as_tuple(std::forward<_KType>(__k)),
1062 std::forward_as_tuple(std::forward<_Args>(__args)...)
1063 };
1064 auto __it = _M_insert_unique_node(__bkt, __code, __node._M_node);
1065 __node._M_node = nullptr;
1066 return { __it, true };
1067 }
1068#endif
1069
1070 void
1071 insert(initializer_list<value_type> __l)
1072 { this->insert(__l.begin(), __l.end()); }
1073
1074 template<typename _InputIterator>
1075 void
1076 insert(_InputIterator __first, _InputIterator __last)
1077 {
1078 if constexpr (__unique_keys::value)
1079 for (; __first != __last; ++__first)
1080 _M_emplace_uniq(*__first);
1081 else
1082 return _M_insert_range_multi(__first, __last);
1083 }
1084
1085 // This overload is only defined for maps, not sets.
1086 template<typename _Pair,
1087 typename = _Require<__not_<is_same<_Key, _Value>>,
1088 is_constructible<value_type, _Pair&&>>>
1089 __ireturn_type
1090 insert(_Pair&& __v)
1091 {
1092 if constexpr (__unique_keys::value)
1093 return _M_emplace_uniq(std::forward<_Pair>(__v));
1094 else
1095 return _M_emplace_multi(cend(), std::forward<_Pair>(__v));
1096 }
1097
1098 // This overload is only defined for maps, not sets.
1099 template<typename _Pair,
1100 typename = _Require<__not_<is_same<_Key, _Value>>,
1101 is_constructible<value_type, _Pair&&>>>
1102 iterator
1103 insert(const_iterator __hint, _Pair&& __v)
1104 {
1105 if constexpr (__unique_keys::value)
1106 return _M_emplace_uniq(std::forward<_Pair>(__v));
1107 else
1108 return _M_emplace_multi(__hint, std::forward<_Pair>(__v));
1109 }
1110#pragma GCC diagnostic pop
1111
1112 // Erase
1113 iterator
1114 erase(const_iterator);
1115
1116 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1117 // 2059. C++0x ambiguity problem with map::erase
1118 iterator
1119 erase(iterator __it)
1120 { return erase(const_iterator(__it)); }
1121
1122 size_type
1123 erase(const key_type& __k);
1124
1125 iterator
1126 erase(const_iterator, const_iterator);
1127
1128 void
1129 clear() noexcept;
1130
1131 // Set number of buckets keeping it appropriate for container's number
1132 // of elements.
1133 void rehash(size_type __bkt_count);
1134
1135 // DR 1189.
1136 // reserve, if present, comes from _Rehash_base.
1137
1138#if __glibcxx_node_extract // >= C++17 && HOSTED
1139 /// Re-insert an extracted node into a container with unique keys.
1140 insert_return_type
1141 _M_reinsert_node(node_type&& __nh)
1142 {
1143 insert_return_type __ret;
1144 if (__nh.empty())
1145 __ret.position = end();
1146 else
1147 {
1148 __glibcxx_assert(get_allocator() == __nh.get_allocator());
1149
1150 if (auto __loc = _M_locate(__nh._M_key()))
1151 {
1152 __ret.node = std::move(__nh);
1153 __ret.position = iterator(__loc);
1154 __ret.inserted = false;
1155 }
1156 else
1157 {
1158 auto __code = __loc._M_hash_code;
1159 auto __bkt = __loc._M_bucket_index;
1160 __ret.position
1161 = _M_insert_unique_node(__bkt, __code, __nh._M_ptr);
1162 __ret.inserted = true;
1163 __nh.release();
1164 }
1165 }
1166 return __ret;
1167 }
1168
1169 /// Re-insert an extracted node into a container with equivalent keys.
1170 iterator
1171 _M_reinsert_node_multi(const_iterator __hint, node_type&& __nh)
1172 {
1173 if (__nh.empty())
1174 return end();
1175
1176 __glibcxx_assert(get_allocator() == __nh.get_allocator());
1177
1178 const key_type& __k = __nh._M_key();
1179 auto __code = this->_M_hash_code(__k);
1180 auto __ret
1181 = _M_insert_multi_node(__hint._M_cur, __code, __nh._M_ptr);
1182 __nh.release();
1183 return __ret;
1184 }
1185
1186 private:
1187 node_type
1188 _M_extract_node(size_t __bkt, __node_base_ptr __prev_n)
1189 {
1190 __node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);
1191 if (__prev_n == _M_buckets[__bkt])
1192 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1193 __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
1194 else if (__n->_M_nxt)
1195 {
1196 size_type __next_bkt = _M_bucket_index(*__n->_M_next());
1197 if (__next_bkt != __bkt)
1198 _M_buckets[__next_bkt] = __prev_n;
1199 }
1200
1201 __prev_n->_M_nxt = __n->_M_nxt;
1202 __n->_M_nxt = nullptr;
1203 --_M_element_count;
1204 return { __n, this->_M_node_allocator() };
1205 }
1206
1207 // Hash code for node __src_n with key __k, using this->hash_function().
1208 // Will use a hash code cached in the node if safe to do so. This is
1209 // for use in _M_merge_multi where the node comes from another container
1210 // with a hash function that might not match this->hash_function().
1211 template<typename _H2>
1212 __hash_code
1213 _M_src_hash_code(const _H2&, const key_type& __k,
1214 const __node_value_type& __src_n) const
1215 {
1216 if constexpr (std::is_same_v<_H2, _Hash>)
1217 if constexpr (std::is_empty_v<_Hash>)
1218 // If the node has a cached hash code, it's OK to use it.
1219 return this->_M_hash_code(__src_n);
1220
1221 return this->_M_hash_code(__k);
1222 }
1223
1224 public:
1225 // Extract a node.
1226 node_type
1227 extract(const_iterator __pos)
1228 {
1229 size_t __bkt = _M_bucket_index(*__pos._M_cur);
1230 return _M_extract_node(__bkt,
1231 _M_get_previous_node(__bkt, __pos._M_cur));
1232 }
1233
1234 /// Extract a node.
1235 node_type
1236 extract(const _Key& __k)
1237 {
1238 node_type __nh;
1239 __hash_code __code = this->_M_hash_code(__k);
1240 std::size_t __bkt = _M_bucket_index(__code);
1241 if (__node_base_ptr __prev_node = _M_find_before_node(__bkt, __k, __code))
1242 __nh = _M_extract_node(__bkt, __prev_node);
1243 return __nh;
1244 }
1245
1246 /// Merge from another container of the same type.
1247 void
1248 _M_merge_unique(_Hashtable& __src)
1249 {
1250 __glibcxx_assert(get_allocator() == __src.get_allocator());
1251
1252 using _PTr = pointer_traits<__node_base_ptr>;
1253
1254 auto __n_elt = __src.size();
1255 size_type __first = 1;
1256 // For a container of identical type we can use its private members,
1257 // __src._M_before_begin, __src._M_bucket_index etc.
1258 auto __prev = _PTr::pointer_to(__src._M_before_begin);
1259 while (__n_elt--)
1260 {
1261 const auto __next = __prev->_M_nxt;
1262 const auto& __node = static_cast<__node_type&>(*__next);
1263 const key_type& __k = _ExtractKey{}(__node._M_v());
1264 const auto __loc = _M_locate(__k);
1265 if (__loc)
1266 {
1267 __prev = __next;
1268 continue;
1269 }
1270
1271 auto __src_bkt = __src._M_bucket_index(__node);
1272 auto __nh = __src._M_extract_node(__src_bkt, __prev);
1273 _M_insert_unique_node(__loc._M_bucket_index, __loc._M_hash_code,
1274 __nh._M_ptr, __first * __n_elt + 1);
1275 __nh.release();
1276 __first = 0;
1277 }
1278 }
1279
1280 /// Merge from a compatible container into one with unique keys.
1281 template<typename _Compatible_Hashtable>
1282 void
1283 _M_merge_unique(_Compatible_Hashtable& __src)
1284 {
1285 static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
1286 node_type>, "Node types are compatible");
1287 __glibcxx_assert(get_allocator() == __src.get_allocator());
1288
1289 auto __n_elt = __src.size();
1290 size_type __first = 1;
1291 // For a compatible container we can only use the public API,
1292 // so cbegin(), cend(), hash_function(), and extract(iterator).
1293 for (auto __i = __src.cbegin(), __end = __src.cend(); __i != __end;)
1294 {
1295 --__n_elt;
1296 auto __pos = __i++;
1297 const key_type& __k = _ExtractKey{}(*__pos);
1298 const auto __loc = _M_locate(__k);
1299 if (__loc)
1300 continue;
1301
1302 auto __nh = __src.extract(__pos);
1303 _M_insert_unique_node(__loc._M_bucket_index,
1304 __loc._M_hash_code, __nh._M_ptr,
1305 __first * __n_elt + 1);
1306 __nh.release();
1307 __first = 0;
1308 }
1309 }
1310
1311 /// Merge from another container of the same type.
1312 void
1313 _M_merge_multi(_Hashtable& __src)
1314 {
1315 __glibcxx_assert(get_allocator() == __src.get_allocator());
1316
1317 if (__src.size() == 0) [[__unlikely__]]
1318 return;
1319
1320 using _PTr = pointer_traits<__node_base_ptr>;
1321
1322 __node_ptr __hint = nullptr;
1323 this->reserve(size() + __src.size());
1324 // For a container of identical type we can use its private members,
1325 // __src._M_before_begin, __src._M_bucket_index etc.
1326 auto __prev = _PTr::pointer_to(__src._M_before_begin);
1327 do
1328 {
1329 const auto& __node = static_cast<__node_type&>(*__prev->_M_nxt);
1330 const key_type& __k = _ExtractKey{}(__node._M_v());
1331 // Hash code from this->hash_function():
1332 auto __code = _M_src_hash_code(__src.hash_function(), __k, __node);
1333 // Bucket index in __src, using code from __src.hash_function():
1334 size_type __src_bkt = __src._M_bucket_index(__node);
1335 auto __nh = __src._M_extract_node(__src_bkt, __prev);
1336 __hint = _M_insert_multi_node(__hint, __code, __nh._M_ptr)._M_cur;
1337 __nh.release();
1338 }
1339 while (__prev->_M_nxt != nullptr);
1340 }
1341
1342 /// Merge from a compatible container into one with equivalent keys.
1343 template<typename _Compatible_Hashtable>
1344 void
1345 _M_merge_multi(_Compatible_Hashtable& __src)
1346 {
1347 static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
1348 node_type>, "Node types are compatible");
1349 __glibcxx_assert(get_allocator() == __src.get_allocator());
1350
1351 __node_ptr __hint = nullptr;
1352 this->reserve(size() + __src.size());
1353 // For a compatible container we can only use the public API,
1354 // so cbegin(), cend(), hash_function(), and extract(iterator).
1355 for (auto __i = __src.cbegin(), __end = __src.cend(); __i != __end;)
1356 {
1357 auto __pos = __i++;
1358 const key_type& __k = _ExtractKey{}(*__pos);
1359 __hash_code __code
1360 = _M_src_hash_code(__src.hash_function(), __k, *__pos._M_cur);
1361 auto __nh = __src.extract(__pos);
1362 __hint = _M_insert_multi_node(__hint, __code, __nh._M_ptr)._M_cur;
1363 __nh.release();
1364 }
1365 }
1366#endif // C++17 __glibcxx_node_extract
1367
1368 bool
1369 _M_equal(const _Hashtable& __other) const;
1370
1371 private:
1372 // Helper rehash method used when keys are unique.
1373 void _M_rehash(size_type __bkt_count, true_type __uks);
1374
1375 // Helper rehash method used when keys can be non-unique.
1376 void _M_rehash(size_type __bkt_count, false_type __uks);
1377 };
1378
1379 // Definitions of class template _Hashtable's out-of-line member functions.
1380 template<typename _Key, typename _Value, typename _Alloc,
1381 typename _ExtractKey, typename _Equal,
1382 typename _Hash, typename _RangeHash, typename _Unused,
1383 typename _RehashPolicy, typename _Traits>
1384 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1385 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1386 _Hashtable(size_type __bkt_count_hint,
1387 const _Hash& __h, const _Equal& __eq, const allocator_type& __a)
1388 : _Hashtable(__h, __eq, __a)
1389 {
1390 auto __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count_hint);
1391 if (__bkt_count > _M_bucket_count)
1392 {
1393 _M_buckets = _M_allocate_buckets(__bkt_count);
1394 _M_bucket_count = __bkt_count;
1395 }
1396 }
1397
1398 template<typename _Key, typename _Value, typename _Alloc,
1399 typename _ExtractKey, typename _Equal,
1400 typename _Hash, typename _RangeHash, typename _Unused,
1401 typename _RehashPolicy, typename _Traits>
1402 template<typename _InputIterator>
1403 inline
1404 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1405 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1406 _Hashtable(_InputIterator __f, _InputIterator __l,
1407 size_type __bkt_count_hint,
1408 const _Hash& __h, const _Equal& __eq,
1409 const allocator_type& __a, true_type /* __uks */)
1410 : _Hashtable(__bkt_count_hint, __h, __eq, __a)
1411 { this->insert(__f, __l); }
1412
1413 template<typename _Key, typename _Value, typename _Alloc,
1414 typename _ExtractKey, typename _Equal,
1415 typename _Hash, typename _RangeHash, typename _Unused,
1416 typename _RehashPolicy, typename _Traits>
1417 template<typename _InputIterator>
1418 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1419 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1420 _Hashtable(_InputIterator __f, _InputIterator __l,
1421 size_type __bkt_count_hint,
1422 const _Hash& __h, const _Equal& __eq,
1423 const allocator_type& __a, false_type __uks)
1424 : _Hashtable(__h, __eq, __a)
1425 {
1426 auto __nb_elems = __detail::__distance_fw(__f, __l);
1427 auto __bkt_count =
1428 _M_rehash_policy._M_next_bkt(
1429 std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
1430 __bkt_count_hint));
1431
1432 if (__bkt_count > _M_bucket_count)
1433 {
1434 _M_buckets = _M_allocate_buckets(__bkt_count);
1435 _M_bucket_count = __bkt_count;
1436 }
1437
1438 for (; __f != __l; ++__f)
1439 _M_emplace_multi(cend(), *__f);
1440 }
1441
1442 template<typename _Key, typename _Value, typename _Alloc,
1443 typename _ExtractKey, typename _Equal,
1444 typename _Hash, typename _RangeHash, typename _Unused,
1445 typename _RehashPolicy, typename _Traits>
1446 auto
1447 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1448 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1449 operator=(const _Hashtable& __ht)
1450 -> _Hashtable&
1451 {
1452 if (&__ht == this)
1453 return *this;
1454
1455 if (__node_alloc_traits::_S_propagate_on_copy_assign())
1456 {
1457 auto& __this_alloc = this->_M_node_allocator();
1458 auto& __that_alloc = __ht._M_node_allocator();
1459 if (!__node_alloc_traits::_S_always_equal()
1460 && __this_alloc != __that_alloc)
1461 {
1462 // Replacement allocator cannot free existing storage.
1463 this->_M_deallocate_nodes(_M_begin());
1464 _M_before_begin._M_nxt = nullptr;
1465 _M_deallocate_buckets();
1466 _M_buckets = nullptr;
1467 std::__alloc_on_copy(__this_alloc, __that_alloc);
1468 __hashtable_base::operator=(__ht);
1469 _M_bucket_count = __ht._M_bucket_count;
1470 _M_element_count = __ht._M_element_count;
1471 _M_rehash_policy = __ht._M_rehash_policy;
1472
1473 struct _Guard
1474 {
1475 ~_Guard() { if (_M_ht) _M_ht->_M_reset(); }
1476 _Hashtable* _M_ht;
1477 };
1478 // If _M_assign exits via an exception it will have deallocated
1479 // all memory. This guard will ensure *this is in a usable state.
1480 _Guard __guard{this};
1481 _M_assign(__ht);
1482 __guard._M_ht = nullptr;
1483 return *this;
1484 }
1485 std::__alloc_on_copy(__this_alloc, __that_alloc);
1486 }
1487
1488 // Reuse allocated buckets and nodes.
1489 _M_assign_elements(__ht);
1490 return *this;
1491 }
1492
1493 template<typename _Key, typename _Value, typename _Alloc,
1494 typename _ExtractKey, typename _Equal,
1495 typename _Hash, typename _RangeHash, typename _Unused,
1496 typename _RehashPolicy, typename _Traits>
1497 template<typename _Ht>
1498 void
1499 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1500 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1501 _M_assign_elements(_Ht&& __ht)
1502 {
1503 using __reuse_or_alloc_node_gen_t =
1504 __detail::_ReuseOrAllocNode<__node_alloc_type>;
1505
1506 __buckets_ptr __former_buckets = nullptr;
1507 std::size_t __former_bucket_count = _M_bucket_count;
1508 __rehash_guard_t __rehash_guard(_M_rehash_policy);
1509
1510 if (_M_bucket_count != __ht._M_bucket_count)
1511 {
1512 __former_buckets = _M_buckets;
1513 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1514 _M_bucket_count = __ht._M_bucket_count;
1515 }
1516 else
1517 std::fill_n(_M_buckets, _M_bucket_count, nullptr);
1518
1519 __try
1520 {
1521 __hashtable_base::operator=(std::forward<_Ht>(__ht));
1522 _M_element_count = __ht._M_element_count;
1523 _M_rehash_policy = __ht._M_rehash_policy;
1524 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *this);
1525 _M_before_begin._M_nxt = nullptr;
1526 _M_assign(std::forward<_Ht>(__ht), __roan);
1527 if (__former_buckets)
1528 _M_deallocate_buckets(__former_buckets, __former_bucket_count);
1529 __rehash_guard._M_guarded_obj = nullptr;
1530 }
1531 __catch(...)
1532 {
1533 if (__former_buckets)
1534 {
1535 // Restore previous buckets.
1536 _M_deallocate_buckets();
1537 _M_buckets = __former_buckets;
1538 _M_bucket_count = __former_bucket_count;
1539 }
1540 std::fill_n(_M_buckets, _M_bucket_count, nullptr);
1541 __throw_exception_again;
1542 }
1543 }
1544
1545 template<typename _Key, typename _Value, typename _Alloc,
1546 typename _ExtractKey, typename _Equal,
1547 typename _Hash, typename _RangeHash, typename _Unused,
1548 typename _RehashPolicy, typename _Traits>
1549 template<typename _Ht, typename _NodeGenerator>
1550 void
1551 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1552 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1553 _M_assign(_Ht&& __ht, _NodeGenerator& __node_gen)
1554 {
1555 struct _Guard
1556 {
1557 ~_Guard()
1558 {
1559 if (_M_ht)
1560 {
1561 _M_ht->clear();
1562 if (_M_dealloc_buckets)
1563 _M_ht->_M_deallocate_buckets();
1564 }
1565 }
1566 _Hashtable* _M_ht = nullptr;
1567 bool _M_dealloc_buckets = false;
1568 };
1569 _Guard __guard;
1570
1571 if (!_M_buckets)
1572 {
1573 _M_buckets = _M_allocate_buckets(_M_bucket_count);
1574 __guard._M_dealloc_buckets = true;
1575 }
1576
1577 if (!__ht._M_before_begin._M_nxt)
1578 return;
1579
1580 __guard._M_ht = this;
1581
1582 using _FromVal = __conditional_t<is_lvalue_reference<_Ht>::value,
1583 const value_type&, value_type&&>;
1584
1585 // First deal with the special first node pointed to by
1586 // _M_before_begin.
1587 __node_ptr __ht_n = __ht._M_begin();
1588 __node_ptr __this_n
1589 = __node_gen(static_cast<_FromVal>(__ht_n->_M_v()));
1590 this->_M_copy_code(*__this_n, *__ht_n);
1591 _M_update_bbegin(__this_n);
1592
1593 // Then deal with other nodes.
1594 __node_ptr __prev_n = __this_n;
1595 for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
1596 {
1597 __this_n = __node_gen(static_cast<_FromVal>(__ht_n->_M_v()));
1598 __prev_n->_M_nxt = __this_n;
1599 this->_M_copy_code(*__this_n, *__ht_n);
1600 size_type __bkt = _M_bucket_index(*__this_n);
1601 if (!_M_buckets[__bkt])
1602 _M_buckets[__bkt] = __prev_n;
1603 __prev_n = __this_n;
1604 }
1605 __guard._M_ht = nullptr;
1606 }
1607
1608 template<typename _Key, typename _Value, typename _Alloc,
1609 typename _ExtractKey, typename _Equal,
1610 typename _Hash, typename _RangeHash, typename _Unused,
1611 typename _RehashPolicy, typename _Traits>
1612 void
1613 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1614 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1615 _M_reset() noexcept
1616 {
1617 _M_rehash_policy._M_reset();
1618 _M_bucket_count = 1;
1619 _M_single_bucket = nullptr;
1620 _M_buckets = &_M_single_bucket;
1621 _M_before_begin._M_nxt = nullptr;
1622 _M_element_count = 0;
1623 }
1624
1625 template<typename _Key, typename _Value, typename _Alloc,
1626 typename _ExtractKey, typename _Equal,
1627 typename _Hash, typename _RangeHash, typename _Unused,
1628 typename _RehashPolicy, typename _Traits>
1629 void
1630 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1631 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1632 _M_move_assign(_Hashtable&& __ht, true_type)
1633 {
1634 if (__builtin_expect(std::__addressof(__ht) == this, false))
1635 return;
1636
1637 this->_M_deallocate_nodes(_M_begin());
1638 _M_deallocate_buckets();
1639 __hashtable_base::operator=(std::move(__ht));
1640 _M_rehash_policy = __ht._M_rehash_policy;
1641 if (!__ht._M_uses_single_bucket())
1642 _M_buckets = __ht._M_buckets;
1643 else
1644 {
1645 _M_buckets = &_M_single_bucket;
1646 _M_single_bucket = __ht._M_single_bucket;
1647 }
1648
1649 _M_bucket_count = __ht._M_bucket_count;
1650 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1651 _M_element_count = __ht._M_element_count;
1652 std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
1653
1654 // Fix bucket containing the _M_before_begin pointer that can't be moved.
1655 _M_update_bbegin();
1656 __ht._M_reset();
1657 }
1658
1659 template<typename _Key, typename _Value, typename _Alloc,
1660 typename _ExtractKey, typename _Equal,
1661 typename _Hash, typename _RangeHash, typename _Unused,
1662 typename _RehashPolicy, typename _Traits>
1663 void
1664 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1665 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1666 _M_move_assign(_Hashtable&& __ht, false_type)
1667 {
1668 if (__ht._M_node_allocator() == this->_M_node_allocator())
1669 _M_move_assign(std::move(__ht), true_type{});
1670 else
1671 {
1672 // Can't move memory, move elements then.
1673 _M_assign_elements(std::move(__ht));
1674 __ht.clear();
1675 }
1676 }
1677
1678 template<typename _Key, typename _Value, typename _Alloc,
1679 typename _ExtractKey, typename _Equal,
1680 typename _Hash, typename _RangeHash, typename _Unused,
1681 typename _RehashPolicy, typename _Traits>
1682 inline
1683 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1684 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1685 _Hashtable(const _Hashtable& __ht)
1686 : __hashtable_base(__ht),
1687 __map_base(__ht),
1688 __rehash_base(__ht),
1689 __hashtable_alloc(
1690 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1691 __enable_default_ctor(__ht),
1692 _M_buckets(nullptr),
1693 _M_bucket_count(__ht._M_bucket_count),
1694 _M_element_count(__ht._M_element_count),
1695 _M_rehash_policy(__ht._M_rehash_policy)
1696 {
1697 _M_assign(__ht);
1698 }
1699
1700 template<typename _Key, typename _Value, typename _Alloc,
1701 typename _ExtractKey, typename _Equal,
1702 typename _Hash, typename _RangeHash, typename _Unused,
1703 typename _RehashPolicy, typename _Traits>
1704 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1705 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1706 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1707 true_type /* alloc always equal */)
1708 noexcept(_S_nothrow_move())
1709 : __hashtable_base(__ht),
1710 __map_base(__ht),
1711 __rehash_base(__ht),
1712 __hashtable_alloc(std::move(__a)),
1713 __enable_default_ctor(__ht),
1714 _M_buckets(__ht._M_buckets),
1715 _M_bucket_count(__ht._M_bucket_count),
1716 _M_before_begin(__ht._M_before_begin._M_nxt),
1717 _M_element_count(__ht._M_element_count),
1718 _M_rehash_policy(__ht._M_rehash_policy)
1719 {
1720 // Update buckets if __ht is using its single bucket.
1721 if (__ht._M_uses_single_bucket())
1722 {
1723 _M_buckets = &_M_single_bucket;
1724 _M_single_bucket = __ht._M_single_bucket;
1725 }
1726
1727 // Fix bucket containing the _M_before_begin pointer that can't be moved.
1728 _M_update_bbegin();
1729
1730 __ht._M_reset();
1731 }
1732
1733 template<typename _Key, typename _Value, typename _Alloc,
1734 typename _ExtractKey, typename _Equal,
1735 typename _Hash, typename _RangeHash, typename _Unused,
1736 typename _RehashPolicy, typename _Traits>
1737 inline
1738 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1739 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1740 _Hashtable(const _Hashtable& __ht, const allocator_type& __a)
1741 : __hashtable_base(__ht),
1742 __map_base(__ht),
1743 __rehash_base(__ht),
1744 __hashtable_alloc(__node_alloc_type(__a)),
1745 __enable_default_ctor(__ht),
1746 _M_buckets(),
1747 _M_bucket_count(__ht._M_bucket_count),
1748 _M_element_count(__ht._M_element_count),
1749 _M_rehash_policy(__ht._M_rehash_policy)
1750 {
1751 _M_assign(__ht);
1752 }
1753
1754 template<typename _Key, typename _Value, typename _Alloc,
1755 typename _ExtractKey, typename _Equal,
1756 typename _Hash, typename _RangeHash, typename _Unused,
1757 typename _RehashPolicy, typename _Traits>
1758 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1759 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1760 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1761 false_type /* alloc always equal */)
1762 : __hashtable_base(__ht),
1763 __map_base(__ht),
1764 __rehash_base(__ht),
1765 __hashtable_alloc(std::move(__a)),
1766 __enable_default_ctor(__ht),
1767 _M_buckets(nullptr),
1768 _M_bucket_count(__ht._M_bucket_count),
1769 _M_element_count(__ht._M_element_count),
1770 _M_rehash_policy(__ht._M_rehash_policy)
1771 {
1772 if (__ht._M_node_allocator() == this->_M_node_allocator())
1773 {
1774 if (__ht._M_uses_single_bucket())
1775 {
1776 _M_buckets = &_M_single_bucket;
1777 _M_single_bucket = __ht._M_single_bucket;
1778 }
1779 else
1780 _M_buckets = __ht._M_buckets;
1781
1782 // Fix bucket containing the _M_before_begin pointer that can't be
1783 // moved.
1784 _M_update_bbegin(__ht._M_begin());
1785
1786 __ht._M_reset();
1787 }
1788 else
1789 {
1790 using _Fwd_Ht = __conditional_t<
1791 __move_if_noexcept_cond<value_type>::value,
1792 const _Hashtable&, _Hashtable&&>;
1793 _M_assign(std::forward<_Fwd_Ht>(__ht));
1794 __ht.clear();
1795 }
1796 }
1797
1798 template<typename _Key, typename _Value, typename _Alloc,
1799 typename _ExtractKey, typename _Equal,
1800 typename _Hash, typename _RangeHash, typename _Unused,
1801 typename _RehashPolicy, typename _Traits>
1802 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1803 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1804 ~_Hashtable() noexcept
1805 {
1806 // Getting a bucket index from a node shall not throw because it is used
1807 // during the rehash process. This static_assert purpose is limited to usage
1808 // of _Hashtable with _Hashtable_traits requesting non-cached hash code.
1809 // Need a complete type to check this, so do it in the destructor not at
1810 // class scope.
1811 static_assert(noexcept(declval<const __hash_code_base_access&>()
1812 ._M_bucket_index(declval<const __node_value_type&>(),
1813 (std::size_t)0)),
1814 "Cache the hash code or qualify your functors involved"
1815 " in hash code and bucket index computation with noexcept");
1816
1817 this->_M_deallocate_nodes(_M_begin());
1818 _M_deallocate_buckets();
1819 }
1820
1821 template<typename _Key, typename _Value, typename _Alloc,
1822 typename _ExtractKey, typename _Equal,
1823 typename _Hash, typename _RangeHash, typename _Unused,
1824 typename _RehashPolicy, typename _Traits>
1825 void
1826 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1827 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1828 swap(_Hashtable& __x)
1829 noexcept(__and_<__is_nothrow_swappable<_Hash>,
1830 __is_nothrow_swappable<_Equal>>::value)
1831 {
1832 using std::swap;
1833 swap(__hash_code_base::_M_hash._M_obj,
1834 __x.__hash_code_base::_M_hash._M_obj);
1835 swap(__hashtable_base::_M_equal._M_obj,
1836 __x.__hashtable_base::_M_equal._M_obj);
1837
1838#pragma GCC diagnostic push
1839#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
1840 if constexpr (__node_alloc_traits::propagate_on_container_swap::value)
1841 swap(this->_M_node_allocator(), __x._M_node_allocator());
1842#pragma GCC diagnostic pop
1843
1844 std::swap(_M_rehash_policy, __x._M_rehash_policy);
1845
1846 // Deal properly with potentially moved instances.
1847 if (this->_M_uses_single_bucket())
1848 {
1849 if (!__x._M_uses_single_bucket())
1850 {
1851 _M_buckets = __x._M_buckets;
1852 __x._M_buckets = &__x._M_single_bucket;
1853 }
1854 }
1855 else if (__x._M_uses_single_bucket())
1856 {
1857 __x._M_buckets = _M_buckets;
1858 _M_buckets = &_M_single_bucket;
1859 }
1860 else
1861 std::swap(_M_buckets, __x._M_buckets);
1862
1863 std::swap(_M_bucket_count, __x._M_bucket_count);
1864 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1865 std::swap(_M_element_count, __x._M_element_count);
1866 std::swap(_M_single_bucket, __x._M_single_bucket);
1867
1868 // Fix buckets containing the _M_before_begin pointers that can't be
1869 // swapped.
1870 _M_update_bbegin();
1871 __x._M_update_bbegin();
1872 }
1873
1874 template<typename _Key, typename _Value, typename _Alloc,
1875 typename _ExtractKey, typename _Equal,
1876 typename _Hash, typename _RangeHash, typename _Unused,
1877 typename _RehashPolicy, typename _Traits>
1878 auto
1879 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1880 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1881 find(const key_type& __k)
1882 -> iterator
1883 { return iterator(_M_locate(__k)); }
1884
1885 template<typename _Key, typename _Value, typename _Alloc,
1886 typename _ExtractKey, typename _Equal,
1887 typename _Hash, typename _RangeHash, typename _Unused,
1888 typename _RehashPolicy, typename _Traits>
1889 auto
1890 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1891 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1892 find(const key_type& __k) const
1893 -> const_iterator
1894 { return const_iterator(_M_locate(__k)); }
1895
1896#if __cplusplus > 201703L
1897 template<typename _Key, typename _Value, typename _Alloc,
1898 typename _ExtractKey, typename _Equal,
1899 typename _Hash, typename _RangeHash, typename _Unused,
1900 typename _RehashPolicy, typename _Traits>
1901 template<typename _Kt, typename, typename>
1902 auto
1903 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1904 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1905 _M_find_tr(const _Kt& __k)
1906 -> iterator
1907 {
1908 if (size() <= __small_size_threshold())
1909 {
1910 for (auto __n = _M_begin(); __n; __n = __n->_M_next())
1911 if (this->_M_key_equals_tr(__k, *__n))
1912 return iterator(__n);
1913 return end();
1914 }
1915
1916 __hash_code __code = this->_M_hash_code_tr(__k);
1917 std::size_t __bkt = _M_bucket_index(__code);
1918 return iterator(_M_find_node_tr(__bkt, __k, __code));
1919 }
1920
1921 template<typename _Key, typename _Value, typename _Alloc,
1922 typename _ExtractKey, typename _Equal,
1923 typename _Hash, typename _RangeHash, typename _Unused,
1924 typename _RehashPolicy, typename _Traits>
1925 template<typename _Kt, typename, typename>
1926 auto
1927 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1928 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1929 _M_find_tr(const _Kt& __k) const
1930 -> const_iterator
1931 {
1932 if (size() <= __small_size_threshold())
1933 {
1934 for (auto __n = _M_begin(); __n; __n = __n->_M_next())
1935 if (this->_M_key_equals_tr(__k, *__n))
1936 return const_iterator(__n);
1937 return end();
1938 }
1939
1940 __hash_code __code = this->_M_hash_code_tr(__k);
1941 std::size_t __bkt = _M_bucket_index(__code);
1942 return const_iterator(_M_find_node_tr(__bkt, __k, __code));
1943 }
1944#endif
1945
1946 template<typename _Key, typename _Value, typename _Alloc,
1947 typename _ExtractKey, typename _Equal,
1948 typename _Hash, typename _RangeHash, typename _Unused,
1949 typename _RehashPolicy, typename _Traits>
1950 auto
1951 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1952 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1953 count(const key_type& __k) const
1954 -> size_type
1955 {
1956 auto __it = find(__k);
1957 if (!__it._M_cur)
1958 return 0;
1959
1960 if (__unique_keys::value)
1961 return 1;
1962
1963 size_type __result = 1;
1964 for (auto __ref = __it++;
1965 __it._M_cur && this->_M_node_equals(*__ref._M_cur, *__it._M_cur);
1966 ++__it)
1967 ++__result;
1968
1969 return __result;
1970 }
1971
1972#if __cplusplus > 201703L
1973 template<typename _Key, typename _Value, typename _Alloc,
1974 typename _ExtractKey, typename _Equal,
1975 typename _Hash, typename _RangeHash, typename _Unused,
1976 typename _RehashPolicy, typename _Traits>
1977 template<typename _Kt, typename, typename>
1978 auto
1979 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1980 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1981 _M_count_tr(const _Kt& __k) const
1982 -> size_type
1983 {
1984 if (size() <= __small_size_threshold())
1985 {
1986 size_type __result = 0;
1987 for (auto __n = _M_begin(); __n; __n = __n->_M_next())
1988 {
1989 if (this->_M_key_equals_tr(__k, *__n))
1990 {
1991 ++__result;
1992 continue;
1993 }
1994
1995 if (__result)
1996 break;
1997 }
1998
1999 return __result;
2000 }
2001
2002 __hash_code __code = this->_M_hash_code_tr(__k);
2003 std::size_t __bkt = _M_bucket_index(__code);
2004 auto __n = _M_find_node_tr(__bkt, __k, __code);
2005 if (!__n)
2006 return 0;
2007
2008 iterator __it(__n);
2009 size_type __result = 1;
2010 for (++__it;
2011 __it._M_cur && this->_M_equals_tr(__k, __code, *__it._M_cur);
2012 ++__it)
2013 ++__result;
2014
2015 return __result;
2016 }
2017#endif
2018
2019 template<typename _Key, typename _Value, typename _Alloc,
2020 typename _ExtractKey, typename _Equal,
2021 typename _Hash, typename _RangeHash, typename _Unused,
2022 typename _RehashPolicy, typename _Traits>
2023 auto
2024 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2025 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2026 equal_range(const key_type& __k)
2027 -> pair<iterator, iterator>
2028 {
2029 auto __ite = find(__k);
2030 if (!__ite._M_cur)
2031 return { __ite, __ite };
2032
2033 auto __beg = __ite++;
2034 if (__unique_keys::value)
2035 return { __beg, __ite };
2036
2037 while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
2038 ++__ite;
2039
2040 return { __beg, __ite };
2041 }
2042
2043 template<typename _Key, typename _Value, typename _Alloc,
2044 typename _ExtractKey, typename _Equal,
2045 typename _Hash, typename _RangeHash, typename _Unused,
2046 typename _RehashPolicy, typename _Traits>
2047 auto
2048 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2049 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2050 equal_range(const key_type& __k) const
2051 -> pair<const_iterator, const_iterator>
2052 {
2053 auto __ite = find(__k);
2054 if (!__ite._M_cur)
2055 return { __ite, __ite };
2056
2057 auto __beg = __ite++;
2058 if (__unique_keys::value)
2059 return { __beg, __ite };
2060
2061 while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
2062 ++__ite;
2063
2064 return { __beg, __ite };
2065 }
2066
2067#if __cplusplus > 201703L
2068 template<typename _Key, typename _Value, typename _Alloc,
2069 typename _ExtractKey, typename _Equal,
2070 typename _Hash, typename _RangeHash, typename _Unused,
2071 typename _RehashPolicy, typename _Traits>
2072 template<typename _Kt, typename, typename>
2073 auto
2074 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2075 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2076 _M_equal_range_tr(const _Kt& __k)
2077 -> pair<iterator, iterator>
2078 {
2079 if (size() <= __small_size_threshold())
2080 {
2081 __node_ptr __n, __beg = nullptr;
2082 for (__n = _M_begin(); __n; __n = __n->_M_next())
2083 {
2084 if (this->_M_key_equals_tr(__k, *__n))
2085 {
2086 if (!__beg)
2087 __beg = __n;
2088 continue;
2089 }
2090
2091 if (__beg)
2092 break;
2093 }
2094
2095 return { iterator(__beg), iterator(__n) };
2096 }
2097
2098 __hash_code __code = this->_M_hash_code_tr(__k);
2099 std::size_t __bkt = _M_bucket_index(__code);
2100 auto __n = _M_find_node_tr(__bkt, __k, __code);
2101 iterator __ite(__n);
2102 if (!__n)
2103 return { __ite, __ite };
2104
2105 auto __beg = __ite++;
2106 while (__ite._M_cur && this->_M_equals_tr(__k, __code, *__ite._M_cur))
2107 ++__ite;
2108
2109 return { __beg, __ite };
2110 }
2111
2112 template<typename _Key, typename _Value, typename _Alloc,
2113 typename _ExtractKey, typename _Equal,
2114 typename _Hash, typename _RangeHash, typename _Unused,
2115 typename _RehashPolicy, typename _Traits>
2116 template<typename _Kt, typename, typename>
2117 auto
2118 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2119 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2120 _M_equal_range_tr(const _Kt& __k) const
2121 -> pair<const_iterator, const_iterator>
2122 {
2123 if (size() <= __small_size_threshold())
2124 {
2125 __node_ptr __n, __beg = nullptr;
2126 for (__n = _M_begin(); __n; __n = __n->_M_next())
2127 {
2128 if (this->_M_key_equals_tr(__k, *__n))
2129 {
2130 if (!__beg)
2131 __beg = __n;
2132 continue;
2133 }
2134
2135 if (__beg)
2136 break;
2137 }
2138
2139 return { const_iterator(__beg), const_iterator(__n) };
2140 }
2141
2142 __hash_code __code = this->_M_hash_code_tr(__k);
2143 std::size_t __bkt = _M_bucket_index(__code);
2144 auto __n = _M_find_node_tr(__bkt, __k, __code);
2145 const_iterator __ite(__n);
2146 if (!__n)
2147 return { __ite, __ite };
2148
2149 auto __beg = __ite++;
2150 while (__ite._M_cur && this->_M_equals_tr(__k, __code, *__ite._M_cur))
2151 ++__ite;
2152
2153 return { __beg, __ite };
2154 }
2155#endif
2156
2157 // Find the node before the one whose key compares equal to k in the bucket
2158 // bkt. Return nullptr if no node is found.
2159 template<typename _Key, typename _Value, typename _Alloc,
2160 typename _ExtractKey, typename _Equal,
2161 typename _Hash, typename _RangeHash, typename _Unused,
2162 typename _RehashPolicy, typename _Traits>
2163 auto
2164 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2165 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2166 _M_find_before_node(size_type __bkt, const key_type& __k,
2167 __hash_code __code) const
2168 -> __node_base_ptr
2169 {
2170 __node_base_ptr __prev_p = _M_buckets[__bkt];
2171 if (!__prev_p)
2172 return nullptr;
2173
2174 for (__node_ptr __p = static_cast<__node_ptr>(__prev_p->_M_nxt);;
2175 __p = __p->_M_next())
2176 {
2177 if (this->_M_equals(__k, __code, *__p))
2178 return __prev_p;
2179
2180 if (__builtin_expect (!__p->_M_nxt || _M_bucket_index(*__p->_M_next()) != __bkt, 0))
2181 break;
2182 __prev_p = __p;
2183 }
2184
2185 return nullptr;
2186 }
2187
2188 template<typename _Key, typename _Value, typename _Alloc,
2189 typename _ExtractKey, typename _Equal,
2190 typename _Hash, typename _RangeHash, typename _Unused,
2191 typename _RehashPolicy, typename _Traits>
2192 template<typename _Kt>
2193 auto
2194 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2195 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2196 _M_find_before_node_tr(size_type __bkt, const _Kt& __k,
2197 __hash_code __code) const
2198 -> __node_base_ptr
2199 {
2200 __node_base_ptr __prev_p = _M_buckets[__bkt];
2201 if (!__prev_p)
2202 return nullptr;
2203
2204 for (__node_ptr __p = static_cast<__node_ptr>(__prev_p->_M_nxt);;
2205 __p = __p->_M_next())
2206 {
2207 if (this->_M_equals_tr(__k, __code, *__p))
2208 return __prev_p;
2209
2210 if (__builtin_expect (!__p->_M_nxt || _M_bucket_index(*__p->_M_next()) != __bkt, 0))
2211 break;
2212 __prev_p = __p;
2213 }
2214
2215 return nullptr;
2216 }
2217
2218 template<typename _Key, typename _Value, typename _Alloc,
2219 typename _ExtractKey, typename _Equal,
2220 typename _Hash, typename _RangeHash, typename _Unused,
2221 typename _RehashPolicy, typename _Traits>
2222 inline auto
2223 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2224 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2225 _M_locate(const key_type& __k) const
2226 -> __location_type
2227 {
2228 __location_type __loc;
2229 const auto __size = size();
2230
2231 if (__size <= __small_size_threshold())
2232 {
2233 __loc._M_before = pointer_traits<__node_base_ptr>::
2234 pointer_to(const_cast<__node_base&>(_M_before_begin));
2235 while (__loc._M_before->_M_nxt)
2236 {
2237 if (this->_M_key_equals(__k, *__loc._M_node()))
2238 return __loc;
2239 __loc._M_before = __loc._M_before->_M_nxt;
2240 }
2241 __loc._M_before = nullptr; // Didn't find it.
2242 }
2243
2244 __loc._M_hash_code = this->_M_hash_code(__k);
2245 __loc._M_bucket_index = _M_bucket_index(__loc._M_hash_code);
2246
2247 if (__size > __small_size_threshold())
2248 __loc._M_before = _M_find_before_node(__loc._M_bucket_index, __k,
2249 __loc._M_hash_code);
2250
2251 return __loc;
2252 }
2253
2254 template<typename _Key, typename _Value, typename _Alloc,
2255 typename _ExtractKey, typename _Equal,
2256 typename _Hash, typename _RangeHash, typename _Unused,
2257 typename _RehashPolicy, typename _Traits>
2258 auto
2259 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2260 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2261 _M_get_previous_node(size_type __bkt, __node_ptr __n)
2262 -> __node_base_ptr
2263 {
2264 __node_base_ptr __prev_n = _M_buckets[__bkt];
2265 while (__prev_n->_M_nxt != __n)
2266 __prev_n = __prev_n->_M_nxt;
2267 return __prev_n;
2268 }
2269
2270#pragma GCC diagnostic push
2271#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2272 template<typename _Key, typename _Value, typename _Alloc,
2273 typename _ExtractKey, typename _Equal,
2274 typename _Hash, typename _RangeHash, typename _Unused,
2275 typename _RehashPolicy, typename _Traits>
2276 template<typename... _Args>
2277 auto
2278 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2279 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2280 _M_emplace_uniq(_Args&&... __args)
2281 -> pair<iterator, bool>
2282 {
2283 const key_type* __kp = nullptr;
2284
2285 if constexpr (sizeof...(_Args) == 1)
2286 {
2287 if constexpr (__is_key_type<_Args...>)
2288 {
2289 const auto& __key = _ExtractKey{}(__args...);
2290 __kp = std::__addressof(__key);
2291 }
2292 }
2293 else if constexpr (sizeof...(_Args) == 2)
2294 {
2295 if constexpr (__is_key_type<pair<const _Args&...>>)
2296 {
2297 pair<const _Args&...> __refs(__args...);
2298 const auto& __key = _ExtractKey{}(__refs);
2299 __kp = std::__addressof(__key);
2300 }
2301 }
2302
2303 _Scoped_node __node { __node_ptr(), this }; // Do not create node yet.
2304 __hash_code __code = 0;
2305 size_type __bkt = 0;
2306
2307 if (__kp == nullptr)
2308 {
2309 // Didn't extract a key from the args, so build the node.
2310 __node._M_node
2311 = this->_M_allocate_node(std::forward<_Args>(__args)...);
2312 const key_type& __key = _ExtractKey{}(__node._M_node->_M_v());
2313 __kp = std::__addressof(__key);
2314 }
2315
2316 if (auto __loc = _M_locate(*__kp))
2317 // There is already an equivalent node, no insertion.
2318 return { iterator(__loc), false };
2319 else
2320 {
2321 __code = __loc._M_hash_code;
2322 __bkt = __loc._M_bucket_index;
2323 }
2324
2325 if (!__node._M_node)
2326 __node._M_node
2327 = this->_M_allocate_node(std::forward<_Args>(__args)...);
2328
2329 // Insert the node
2330 auto __pos = _M_insert_unique_node(__bkt, __code, __node._M_node);
2331 __node._M_node = nullptr;
2332 return { __pos, true };
2333 }
2334#pragma GCC diagnostic pop
2335
2336 template<typename _Key, typename _Value, typename _Alloc,
2337 typename _ExtractKey, typename _Equal,
2338 typename _Hash, typename _RangeHash, typename _Unused,
2339 typename _RehashPolicy, typename _Traits>
2340 template<typename... _Args>
2341 auto
2342 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2343 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2344 _M_emplace_multi(const_iterator __hint, _Args&&... __args)
2345 -> iterator
2346 {
2347 // First build the node to get its hash code.
2348 _Scoped_node __node { this, std::forward<_Args>(__args)... };
2349 const key_type& __k = _ExtractKey{}(__node._M_node->_M_v());
2350
2351 auto __res = this->_M_compute_hash_code(__hint._M_cur, __k);
2352 auto __pos
2353 = _M_insert_multi_node(__res.first, __res.second, __node._M_node);
2354 __node._M_node = nullptr;
2355 return __pos;
2356 }
2357
2358 template<typename _Key, typename _Value, typename _Alloc,
2359 typename _ExtractKey, typename _Equal,
2360 typename _Hash, typename _RangeHash, typename _Unused,
2361 typename _RehashPolicy, typename _Traits>
2362 template<typename _InputIterator>
2363 void
2364 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2365 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2366 _M_insert_range_multi(_InputIterator __first, _InputIterator __last)
2367 {
2368 using __pair_type = std::pair<bool, std::size_t>;
2369
2370 size_type __n_elt = __detail::__distance_fw(__first, __last);
2371 if (__n_elt == 0)
2372 return;
2373
2374 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2375 __pair_type __do_rehash
2376 = _M_rehash_policy._M_need_rehash(_M_bucket_count,
2377 _M_element_count,
2378 __n_elt);
2379
2380 if (__do_rehash.first)
2381 _M_rehash(__do_rehash.second, false_type{});
2382
2383 __rehash_guard._M_guarded_obj = nullptr;
2384 for (; __first != __last; ++__first)
2385 _M_emplace_multi(cend(), *__first);
2386 }
2387
2388 template<typename _Key, typename _Value, typename _Alloc,
2389 typename _ExtractKey, typename _Equal,
2390 typename _Hash, typename _RangeHash, typename _Unused,
2391 typename _RehashPolicy, typename _Traits>
2392 auto
2393 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2394 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2395 _M_compute_hash_code(__node_ptr __hint, const key_type& __k) const
2396 -> pair<__node_ptr, __hash_code>
2397 {
2398 if (size() <= __small_size_threshold())
2399 {
2400 if (__hint)
2401 {
2402 for (auto __it = __hint; __it; __it = __it->_M_next())
2403 if (this->_M_key_equals(__k, *__it))
2404 return { __it, this->_M_hash_code(*__it) };
2405 }
2406
2407 for (auto __it = _M_begin(); __it != __hint; __it = __it->_M_next())
2408 if (this->_M_key_equals(__k, *__it))
2409 return { __it, this->_M_hash_code(*__it) };
2410
2411 __hint = nullptr;
2412 }
2413
2414 return { __hint, this->_M_hash_code(__k) };
2415 }
2416
2417 template<typename _Key, typename _Value, typename _Alloc,
2418 typename _ExtractKey, typename _Equal,
2419 typename _Hash, typename _RangeHash, typename _Unused,
2420 typename _RehashPolicy, typename _Traits>
2421 auto
2422 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2423 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2424 _M_insert_unique_node(size_type __bkt, __hash_code __code,
2425 __node_ptr __node, size_type __n_elt)
2426 -> iterator
2427 {
2428 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2430 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
2431 __n_elt);
2432
2433 if (__do_rehash.first)
2434 {
2435 _M_rehash(__do_rehash.second, true_type{});
2436 __bkt = _M_bucket_index(__code);
2437 }
2438
2439 __rehash_guard._M_guarded_obj = nullptr;
2440 this->_M_store_code(*__node, __code);
2441
2442 // Always insert at the beginning of the bucket.
2443 _M_insert_bucket_begin(__bkt, __node);
2444 ++_M_element_count;
2445 return iterator(__node);
2446 }
2447
2448 template<typename _Key, typename _Value, typename _Alloc,
2449 typename _ExtractKey, typename _Equal,
2450 typename _Hash, typename _RangeHash, typename _Unused,
2451 typename _RehashPolicy, typename _Traits>
2452 auto
2453 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2454 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2455 _M_insert_multi_node(__node_ptr __hint,
2456 __hash_code __code, __node_ptr __node)
2457 -> iterator
2458 {
2459 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2461 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
2462
2463 if (__do_rehash.first)
2464 _M_rehash(__do_rehash.second, false_type{});
2465
2466 __rehash_guard._M_guarded_obj = nullptr;
2467 this->_M_store_code(*__node, __code);
2468 const key_type& __k = _ExtractKey{}(__node->_M_v());
2469 size_type __bkt = _M_bucket_index(__code);
2470
2471 // Find the node before an equivalent one or use hint if it exists and
2472 // if it is equivalent.
2473 __node_base_ptr __prev
2474 = __builtin_expect(__hint != nullptr, false)
2475 && this->_M_equals(__k, __code, *__hint)
2476 ? __hint
2477 : _M_find_before_node(__bkt, __k, __code);
2478
2479 if (__prev)
2480 {
2481 // Insert after the node before the equivalent one.
2482 __node->_M_nxt = __prev->_M_nxt;
2483 __prev->_M_nxt = __node;
2484 if (__builtin_expect(__prev == __hint, false))
2485 // hint might be the last bucket node, in this case we need to
2486 // update next bucket.
2487 if (__node->_M_nxt
2488 && !this->_M_equals(__k, __code, *__node->_M_next()))
2489 {
2490 size_type __next_bkt = _M_bucket_index(*__node->_M_next());
2491 if (__next_bkt != __bkt)
2492 _M_buckets[__next_bkt] = __node;
2493 }
2494 }
2495 else
2496 // The inserted node has no equivalent in the hashtable. We must
2497 // insert the new node at the beginning of the bucket to preserve
2498 // equivalent elements' relative positions.
2499 _M_insert_bucket_begin(__bkt, __node);
2500 ++_M_element_count;
2501 return iterator(__node);
2502 }
2503
2504 template<typename _Key, typename _Value, typename _Alloc,
2505 typename _ExtractKey, typename _Equal,
2506 typename _Hash, typename _RangeHash, typename _Unused,
2507 typename _RehashPolicy, typename _Traits>
2508 auto
2509 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2510 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2511 erase(const_iterator __it)
2512 -> iterator
2513 {
2514 __node_ptr __n = __it._M_cur;
2515 std::size_t __bkt = _M_bucket_index(*__n);
2516
2517 // Look for previous node to unlink it from the erased one, this
2518 // is why we need buckets to contain the before begin to make
2519 // this search fast.
2520 __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
2521 return _M_erase(__bkt, __prev_n, __n);
2522 }
2523
2524 template<typename _Key, typename _Value, typename _Alloc,
2525 typename _ExtractKey, typename _Equal,
2526 typename _Hash, typename _RangeHash, typename _Unused,
2527 typename _RehashPolicy, typename _Traits>
2528 auto
2529 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2530 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2531 _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n)
2532 -> iterator
2533 {
2534 if (__prev_n == _M_buckets[__bkt])
2535 _M_remove_bucket_begin(__bkt, __n->_M_next(),
2536 __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
2537 else if (__n->_M_nxt)
2538 {
2539 size_type __next_bkt = _M_bucket_index(*__n->_M_next());
2540 if (__next_bkt != __bkt)
2541 _M_buckets[__next_bkt] = __prev_n;
2542 }
2543
2544 __prev_n->_M_nxt = __n->_M_nxt;
2545 iterator __result(__n->_M_next());
2546 this->_M_deallocate_node(__n);
2547 --_M_element_count;
2548
2549 return __result;
2550 }
2551
2552#pragma GCC diagnostic push
2553#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2554 template<typename _Key, typename _Value, typename _Alloc,
2555 typename _ExtractKey, typename _Equal,
2556 typename _Hash, typename _RangeHash, typename _Unused,
2557 typename _RehashPolicy, typename _Traits>
2558 auto
2559 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2560 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2561 erase(const key_type& __k)
2562 -> size_type
2563 {
2564 auto __loc = _M_locate(__k);
2565 if (!__loc)
2566 return 0;
2567
2568 __node_base_ptr __prev_n = __loc._M_before;
2569 __node_ptr __n = __loc._M_node();
2570 auto __bkt = __loc._M_bucket_index;
2571 if (__bkt == size_type(-1))
2572 __bkt = _M_bucket_index(*__n);
2573
2574 if constexpr (__unique_keys::value)
2575 {
2576 _M_erase(__bkt, __prev_n, __n);
2577 return 1;
2578 }
2579 else
2580 {
2581 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2582 // 526. Is it undefined if a function in the standard changes
2583 // in parameters?
2584 // We use one loop to find all matching nodes and another to
2585 // deallocate them so that the key stays valid during the first loop.
2586 // It might be invalidated indirectly when destroying nodes.
2587 __node_ptr __n_last = __n->_M_next();
2588 while (__n_last && this->_M_node_equals(*__n, *__n_last))
2589 __n_last = __n_last->_M_next();
2590
2591 std::size_t __n_last_bkt
2592 = __n_last ? _M_bucket_index(*__n_last) : __bkt;
2593
2594 // Deallocate nodes.
2595 size_type __result = 0;
2596 do
2597 {
2598 __node_ptr __p = __n->_M_next();
2599 this->_M_deallocate_node(__n);
2600 __n = __p;
2601 ++__result;
2602 }
2603 while (__n != __n_last);
2604
2605 _M_element_count -= __result;
2606 if (__prev_n == _M_buckets[__bkt])
2607 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
2608 else if (__n_last_bkt != __bkt)
2609 _M_buckets[__n_last_bkt] = __prev_n;
2610 __prev_n->_M_nxt = __n_last;
2611 return __result;
2612 }
2613 }
2614#pragma GCC diagnostic pop
2615
2616 template<typename _Key, typename _Value, typename _Alloc,
2617 typename _ExtractKey, typename _Equal,
2618 typename _Hash, typename _RangeHash, typename _Unused,
2619 typename _RehashPolicy, typename _Traits>
2620 auto
2621 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2622 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2623 erase(const_iterator __first, const_iterator __last)
2624 -> iterator
2625 {
2626 __node_ptr __n = __first._M_cur;
2627 __node_ptr __last_n = __last._M_cur;
2628 if (__n == __last_n)
2629 return iterator(__n);
2630
2631 std::size_t __bkt = _M_bucket_index(*__n);
2632
2633 __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
2634 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
2635 std::size_t __n_bkt = __bkt;
2636 for (;;)
2637 {
2638 do
2639 {
2640 __node_ptr __tmp = __n;
2641 __n = __n->_M_next();
2642 this->_M_deallocate_node(__tmp);
2643 --_M_element_count;
2644 if (!__n)
2645 break;
2646 __n_bkt = _M_bucket_index(*__n);
2647 }
2648 while (__n != __last_n && __n_bkt == __bkt);
2649 if (__is_bucket_begin)
2650 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
2651 if (__n == __last_n)
2652 break;
2653 __is_bucket_begin = true;
2654 __bkt = __n_bkt;
2655 }
2656
2657 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
2658 _M_buckets[__n_bkt] = __prev_n;
2659 __prev_n->_M_nxt = __n;
2660 return iterator(__n);
2661 }
2662
2663 template<typename _Key, typename _Value, typename _Alloc,
2664 typename _ExtractKey, typename _Equal,
2665 typename _Hash, typename _RangeHash, typename _Unused,
2666 typename _RehashPolicy, typename _Traits>
2667 void
2668 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2669 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2670 clear() noexcept
2671 {
2672 this->_M_deallocate_nodes(_M_begin());
2673 std::fill_n(_M_buckets, _M_bucket_count, nullptr);
2674 _M_element_count = 0;
2675 _M_before_begin._M_nxt = nullptr;
2676 }
2677
2678 template<typename _Key, typename _Value, typename _Alloc,
2679 typename _ExtractKey, typename _Equal,
2680 typename _Hash, typename _RangeHash, typename _Unused,
2681 typename _RehashPolicy, typename _Traits>
2682 void
2683 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2684 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2685 rehash(size_type __bkt_count)
2686 {
2687 __rehash_guard_t __rehash_guard(_M_rehash_policy);
2688 __bkt_count
2689 = std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
2690 __bkt_count);
2691 __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count);
2692
2693 if (__bkt_count != _M_bucket_count)
2694 {
2695 _M_rehash(__bkt_count, __unique_keys{});
2696 __rehash_guard._M_guarded_obj = nullptr;
2697 }
2698 }
2699
2700 // Rehash when there is no equivalent elements.
2701 template<typename _Key, typename _Value, typename _Alloc,
2702 typename _ExtractKey, typename _Equal,
2703 typename _Hash, typename _RangeHash, typename _Unused,
2704 typename _RehashPolicy, typename _Traits>
2705 void
2706 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2707 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2708 _M_rehash(size_type __bkt_count, true_type /* __uks */)
2709 {
2710 __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2711 __node_ptr __p = _M_begin();
2712 _M_before_begin._M_nxt = nullptr;
2713 std::size_t __bbegin_bkt = 0;
2714 while (__p)
2715 {
2716 __node_ptr __next = __p->_M_next();
2717 std::size_t __bkt
2718 = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
2719 if (!__new_buckets[__bkt])
2720 {
2721 __p->_M_nxt = _M_before_begin._M_nxt;
2722 _M_before_begin._M_nxt = __p;
2723 __new_buckets[__bkt] = &_M_before_begin;
2724 if (__p->_M_nxt)
2725 __new_buckets[__bbegin_bkt] = __p;
2726 __bbegin_bkt = __bkt;
2727 }
2728 else
2729 {
2730 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2731 __new_buckets[__bkt]->_M_nxt = __p;
2732 }
2733
2734 __p = __next;
2735 }
2736
2737 _M_deallocate_buckets();
2738 _M_bucket_count = __bkt_count;
2739 _M_buckets = __new_buckets;
2740 }
2741
2742 // Rehash when there can be equivalent elements, preserve their relative
2743 // order.
2744 template<typename _Key, typename _Value, typename _Alloc,
2745 typename _ExtractKey, typename _Equal,
2746 typename _Hash, typename _RangeHash, typename _Unused,
2747 typename _RehashPolicy, typename _Traits>
2748 void
2749 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2750 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2751 _M_rehash(size_type __bkt_count, false_type /* __uks */)
2752 {
2753 __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2754 __node_ptr __p = _M_begin();
2755 _M_before_begin._M_nxt = nullptr;
2756 std::size_t __bbegin_bkt = 0;
2757 std::size_t __prev_bkt = 0;
2758 __node_ptr __prev_p = nullptr;
2759 bool __check_bucket = false;
2760
2761 while (__p)
2762 {
2763 __node_ptr __next = __p->_M_next();
2764 std::size_t __bkt
2765 = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
2766
2767 if (__prev_p && __prev_bkt == __bkt)
2768 {
2769 // Previous insert was already in this bucket, we insert after
2770 // the previously inserted one to preserve equivalent elements
2771 // relative order.
2772 __p->_M_nxt = __prev_p->_M_nxt;
2773 __prev_p->_M_nxt = __p;
2774
2775 // Inserting after a node in a bucket require to check that we
2776 // haven't change the bucket last node, in this case next
2777 // bucket containing its before begin node must be updated. We
2778 // schedule a check as soon as we move out of the sequence of
2779 // equivalent nodes to limit the number of checks.
2780 __check_bucket = true;
2781 }
2782 else
2783 {
2784 if (__check_bucket)
2785 {
2786 // Check if we shall update the next bucket because of
2787 // insertions into __prev_bkt bucket.
2788 if (__prev_p->_M_nxt)
2789 {
2790 std::size_t __next_bkt
2791 = __hash_code_base::_M_bucket_index(
2792 *__prev_p->_M_next(), __bkt_count);
2793 if (__next_bkt != __prev_bkt)
2794 __new_buckets[__next_bkt] = __prev_p;
2795 }
2796 __check_bucket = false;
2797 }
2798
2799 if (!__new_buckets[__bkt])
2800 {
2801 __p->_M_nxt = _M_before_begin._M_nxt;
2802 _M_before_begin._M_nxt = __p;
2803 __new_buckets[__bkt] = &_M_before_begin;
2804 if (__p->_M_nxt)
2805 __new_buckets[__bbegin_bkt] = __p;
2806 __bbegin_bkt = __bkt;
2807 }
2808 else
2809 {
2810 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2811 __new_buckets[__bkt]->_M_nxt = __p;
2812 }
2813 }
2814 __prev_p = __p;
2815 __prev_bkt = __bkt;
2816 __p = __next;
2817 }
2818
2819 if (__check_bucket && __prev_p->_M_nxt)
2820 {
2821 std::size_t __next_bkt
2822 = __hash_code_base::_M_bucket_index(*__prev_p->_M_next(),
2823 __bkt_count);
2824 if (__next_bkt != __prev_bkt)
2825 __new_buckets[__next_bkt] = __prev_p;
2826 }
2827
2828 _M_deallocate_buckets();
2829 _M_bucket_count = __bkt_count;
2830 _M_buckets = __new_buckets;
2831 }
2832
2833#pragma GCC diagnostic push
2834#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
2835
2836 // This is for implementing equality comparison for unordered containers,
2837 // per N3068, by John Lakos and Pablo Halpern.
2838 // Algorithmically, we follow closely the reference implementations therein.
2839 template<typename _Key, typename _Value, typename _Alloc,
2840 typename _ExtractKey, typename _Equal,
2841 typename _Hash, typename _RangeHash, typename _Unused,
2842 typename _RehashPolicy, typename _Traits>
2843 bool
2844 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2845 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2846 _M_equal(const _Hashtable& __other) const
2847 {
2848 if (size() != __other.size())
2849 return false;
2850
2851 if constexpr (__unique_keys::value)
2852 for (auto __x_n = _M_begin(); __x_n; __x_n = __x_n->_M_next())
2853 {
2854 std::size_t __ybkt = __other._M_bucket_index(*__x_n);
2855 auto __prev_n = __other._M_buckets[__ybkt];
2856 if (!__prev_n)
2857 return false;
2858
2859 for (__node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);;
2860 __n = __n->_M_next())
2861 {
2862 if (__n->_M_v() == __x_n->_M_v())
2863 break;
2864
2865 if (!__n->_M_nxt
2866 || __other._M_bucket_index(*__n->_M_next()) != __ybkt)
2867 return false;
2868 }
2869 }
2870 else // non-unique keys
2871 for (auto __x_n = _M_begin(); __x_n;)
2872 {
2873 std::size_t __x_count = 1;
2874 auto __x_n_end = __x_n->_M_next();
2875 for (; __x_n_end
2876 && key_eq()(_ExtractKey{}(__x_n->_M_v()),
2877 _ExtractKey{}(__x_n_end->_M_v()));
2878 __x_n_end = __x_n_end->_M_next())
2879 ++__x_count;
2880
2881 std::size_t __ybkt = __other._M_bucket_index(*__x_n);
2882 auto __y_prev_n = __other._M_buckets[__ybkt];
2883 if (!__y_prev_n)
2884 return false;
2885
2886 __node_ptr __y_n = static_cast<__node_ptr>(__y_prev_n->_M_nxt);
2887 for (;;)
2888 {
2889 if (key_eq()(_ExtractKey{}(__y_n->_M_v()),
2890 _ExtractKey{}(__x_n->_M_v())))
2891 break;
2892
2893 auto __y_ref_n = __y_n;
2894 for (__y_n = __y_n->_M_next(); __y_n; __y_n = __y_n->_M_next())
2895 if (!__other._M_node_equals(*__y_ref_n, *__y_n))
2896 break;
2897
2898 if (!__y_n || __other._M_bucket_index(*__y_n) != __ybkt)
2899 return false;
2900 }
2901
2902 auto __y_n_end = __y_n;
2903 for (; __y_n_end; __y_n_end = __y_n_end->_M_next())
2904 if (--__x_count == 0)
2905 break;
2906
2907 if (__x_count != 0)
2908 return false;
2909
2910 const_iterator __itx(__x_n), __itx_end(__x_n_end);
2911 const_iterator __ity(__y_n);
2912 if (!std::is_permutation(__itx, __itx_end, __ity))
2913 return false;
2914
2915 __x_n = __x_n_end;
2916 }
2917
2918 return true;
2919 }
2920#pragma GCC diagnostic pop
2921
2922#if __cplusplus > 201402L
2923 template<typename, typename, typename> class _Hash_merge_helper { };
2924#endif // C++17
2925
2926#if __cpp_deduction_guides >= 201606
2927 // Used to constrain deduction guides
2928 template<typename _Hash>
2929 using _RequireNotAllocatorOrIntegral
2930 = __enable_if_t<!__or_<is_integral<_Hash>, __is_allocator<_Hash>>::value>;
2931#endif
2932
2933/// @endcond
2934_GLIBCXX_END_NAMESPACE_VERSION
2935} // namespace std
2936
2937#pragma GCC diagnostic pop
2938
2939#endif // _HASHTABLE_H
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:116
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition tuple:2678
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr piecewise_construct_t piecewise_construct
Tag for piecewise construction of std::pair objects.
Definition stl_pair.h:82
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
Definition valarray:1251
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
Definition valarray:1229
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
Struct holding two objects of arbitrary type.
Definition stl_pair.h:286
_T1 first
The first member.
Definition stl_pair.h:290
_T2 second
The second member.
Definition stl_pair.h:291