libstdc++
hashtable_policy.h
Go to the documentation of this file.
1// Internal policy header for unordered_set and unordered_map -*- C++ -*-
2
3// Copyright (C) 2010-2024 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_policy.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly.
28 * @headername{unordered_map,unordered_set}
29 */
30
31#ifndef _HASHTABLE_POLICY_H
32#define _HASHTABLE_POLICY_H 1
33
34#include <tuple> // for std::tuple, std::forward_as_tuple
35#include <bits/functional_hash.h> // for __is_fast_hash
36#include <bits/stl_algobase.h> // for std::min, std::is_permutation.
37#include <bits/stl_pair.h> // for std::pair
38#include <ext/aligned_buffer.h> // for __gnu_cxx::__aligned_buffer
39#include <ext/alloc_traits.h> // for std::__alloc_rebind
40#include <ext/numeric_traits.h> // for __gnu_cxx::__int_traits
41
42namespace std _GLIBCXX_VISIBILITY(default)
43{
44_GLIBCXX_BEGIN_NAMESPACE_VERSION
45/// @cond undocumented
46
47 template<typename _Key, typename _Value, typename _Alloc,
48 typename _ExtractKey, typename _Equal,
49 typename _Hash, typename _RangeHash, typename _Unused,
50 typename _RehashPolicy, typename _Traits>
51 class _Hashtable;
52
53namespace __detail
54{
55 /**
56 * @defgroup hashtable-detail Base and Implementation Classes
57 * @ingroup unordered_associative_containers
58 * @{
59 */
60 template<typename _Key, typename _Value, typename _ExtractKey,
61 typename _Equal, typename _Hash, typename _RangeHash,
62 typename _Unused, typename _Traits>
63 struct _Hashtable_base;
64
65 // Helper function: return distance(first, last) for forward
66 // iterators, or 0/1 for input iterators.
67 template<typename _Iterator>
69 __distance_fw(_Iterator __first, _Iterator __last,
71 { return __first != __last ? 1 : 0; }
72
73 template<typename _Iterator>
75 __distance_fw(_Iterator __first, _Iterator __last,
77 { return std::distance(__first, __last); }
78
79 template<typename _Iterator>
81 __distance_fw(_Iterator __first, _Iterator __last)
82 { return __distance_fw(__first, __last,
83 std::__iterator_category(__first)); }
84
85 struct _Identity
86 {
87 template<typename _Tp>
88 _Tp&&
89 operator()(_Tp&& __x) const noexcept
90 { return std::forward<_Tp>(__x); }
91 };
92
93 struct _Select1st
94 {
95 template<typename _Pair>
96 struct __1st_type;
97
98 template<typename _Tp, typename _Up>
99 struct __1st_type<pair<_Tp, _Up>>
100 { using type = _Tp; };
101
102 template<typename _Tp, typename _Up>
103 struct __1st_type<const pair<_Tp, _Up>>
104 { using type = const _Tp; };
105
106 template<typename _Pair>
107 struct __1st_type<_Pair&>
108 { using type = typename __1st_type<_Pair>::type&; };
109
110 template<typename _Tp>
111 typename __1st_type<_Tp>::type&&
112 operator()(_Tp&& __x) const noexcept
113 { return std::forward<_Tp>(__x).first; }
114 };
115
116 template<typename _ExKey, typename _Value>
117 struct _ConvertToValueType;
118
119 template<typename _Value>
120 struct _ConvertToValueType<_Identity, _Value>
121 {
122 template<typename _Kt>
123 constexpr _Kt&&
124 operator()(_Kt&& __k) const noexcept
125 { return std::forward<_Kt>(__k); }
126 };
127
128 template<typename _Value>
129 struct _ConvertToValueType<_Select1st, _Value>
130 {
131 constexpr _Value&&
132 operator()(_Value&& __x) const noexcept
133 { return std::move(__x); }
134
135 constexpr const _Value&
136 operator()(const _Value& __x) const noexcept
137 { return __x; }
138
139 template<typename _Kt, typename _Val>
140 constexpr std::pair<_Kt, _Val>&&
141 operator()(std::pair<_Kt, _Val>&& __x) const noexcept
142 { return std::move(__x); }
143
144 template<typename _Kt, typename _Val>
145 constexpr const std::pair<_Kt, _Val>&
146 operator()(const std::pair<_Kt, _Val>& __x) const noexcept
147 { return __x; }
148 };
149
150 template<typename _ExKey>
151 struct _NodeBuilder;
152
153 template<>
154 struct _NodeBuilder<_Select1st>
155 {
156 template<typename _Kt, typename _Arg, typename _NodeGenerator>
157 static auto
158 _S_build(_Kt&& __k, _Arg&& __arg, const _NodeGenerator& __node_gen)
159 -> typename _NodeGenerator::__node_ptr
160 {
161 return __node_gen(std::forward<_Kt>(__k),
162 std::forward<_Arg>(__arg).second);
163 }
164 };
165
166 template<>
167 struct _NodeBuilder<_Identity>
168 {
169 template<typename _Kt, typename _Arg, typename _NodeGenerator>
170 static auto
171 _S_build(_Kt&& __k, _Arg&&, const _NodeGenerator& __node_gen)
172 -> typename _NodeGenerator::__node_ptr
173 { return __node_gen(std::forward<_Kt>(__k)); }
174 };
175
176 template<typename _HashtableAlloc, typename _NodePtr>
177 struct _NodePtrGuard
178 {
179 _HashtableAlloc& _M_h;
180 _NodePtr _M_ptr;
181
182 ~_NodePtrGuard()
183 {
184 if (_M_ptr)
185 _M_h._M_deallocate_node_ptr(_M_ptr);
186 }
187 };
188
189 template<typename _NodeAlloc>
190 struct _Hashtable_alloc;
191
192 // Functor recycling a pool of nodes and using allocation once the pool is
193 // empty.
194 template<typename _NodeAlloc>
195 struct _ReuseOrAllocNode
196 {
197 private:
198 using __node_alloc_type = _NodeAlloc;
199 using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
200 using __node_alloc_traits =
201 typename __hashtable_alloc::__node_alloc_traits;
202
203 public:
204 using __node_ptr = typename __hashtable_alloc::__node_ptr;
205
206 _ReuseOrAllocNode(__node_ptr __nodes, __hashtable_alloc& __h)
207 : _M_nodes(__nodes), _M_h(__h) { }
208 _ReuseOrAllocNode(const _ReuseOrAllocNode&) = delete;
209
210 ~_ReuseOrAllocNode()
211 { _M_h._M_deallocate_nodes(_M_nodes); }
212
213 template<typename... _Args>
214 __node_ptr
215 operator()(_Args&&... __args) const
216 {
217 if (!_M_nodes)
218 return _M_h._M_allocate_node(std::forward<_Args>(__args)...);
219
220 __node_ptr __node = _M_nodes;
221 _M_nodes = _M_nodes->_M_next();
222 __node->_M_nxt = nullptr;
223 auto& __a = _M_h._M_node_allocator();
224 __node_alloc_traits::destroy(__a, __node->_M_valptr());
225 _NodePtrGuard<__hashtable_alloc, __node_ptr> __guard { _M_h, __node };
226 __node_alloc_traits::construct(__a, __node->_M_valptr(),
227 std::forward<_Args>(__args)...);
228 __guard._M_ptr = nullptr;
229 return __node;
230 }
231
232 private:
233 mutable __node_ptr _M_nodes;
234 __hashtable_alloc& _M_h;
235 };
236
237 // Functor similar to the previous one but without any pool of nodes to
238 // recycle.
239 template<typename _NodeAlloc>
240 struct _AllocNode
241 {
242 private:
243 using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
244
245 public:
246 using __node_ptr = typename __hashtable_alloc::__node_ptr;
247
248 _AllocNode(__hashtable_alloc& __h)
249 : _M_h(__h) { }
250
251 template<typename... _Args>
252 __node_ptr
253 operator()(_Args&&... __args) const
254 { return _M_h._M_allocate_node(std::forward<_Args>(__args)...); }
255
256 private:
257 __hashtable_alloc& _M_h;
258 };
259
260 // Auxiliary types used for all instantiations of _Hashtable nodes
261 // and iterators.
262
263 /**
264 * struct _Hashtable_traits
265 *
266 * Important traits for hash tables.
267 *
268 * @tparam _Cache_hash_code Boolean value. True if the value of
269 * the hash function is stored along with the value. This is a
270 * time-space tradeoff. Storing it may improve lookup speed by
271 * reducing the number of times we need to call the _Hash or _Equal
272 * functors.
273 *
274 * @tparam _Constant_iterators Boolean value. True if iterator and
275 * const_iterator are both constant iterator types. This is true
276 * for unordered_set and unordered_multiset, false for
277 * unordered_map and unordered_multimap.
278 *
279 * @tparam _Unique_keys Boolean value. True if the return value
280 * of _Hashtable::count(k) is always at most one, false if it may
281 * be an arbitrary number. This is true for unordered_set and
282 * unordered_map, false for unordered_multiset and
283 * unordered_multimap.
284 */
285 template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
286 struct _Hashtable_traits
287 {
288 using __hash_cached = __bool_constant<_Cache_hash_code>;
289 using __constant_iterators = __bool_constant<_Constant_iterators>;
290 using __unique_keys = __bool_constant<_Unique_keys>;
291 };
292
293 /**
294 * struct _Hashtable_hash_traits
295 *
296 * Important traits for hash tables depending on associated hasher.
297 *
298 */
299 template<typename _Hash>
300 struct _Hashtable_hash_traits
301 {
302 static constexpr std::size_t
303 __small_size_threshold() noexcept
304 { return std::__is_fast_hash<_Hash>::value ? 0 : 20; }
305 };
306
307 /**
308 * struct _Hash_node_base
309 *
310 * Nodes, used to wrap elements stored in the hash table. A policy
311 * template parameter of class template _Hashtable controls whether
312 * nodes also store a hash code. In some cases (e.g. strings) this
313 * may be a performance win.
314 */
315 struct _Hash_node_base
316 {
317 _Hash_node_base* _M_nxt;
318
319 _Hash_node_base() noexcept : _M_nxt() { }
320
321 _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
322 };
323
324 /**
325 * struct _Hash_node_value_base
326 *
327 * Node type with the value to store.
328 */
329 template<typename _Value>
330 struct _Hash_node_value_base
331 {
332 typedef _Value value_type;
333
334 __gnu_cxx::__aligned_buffer<_Value> _M_storage;
335
336 [[__gnu__::__always_inline__]]
337 _Value*
338 _M_valptr() noexcept
339 { return _M_storage._M_ptr(); }
340
341 [[__gnu__::__always_inline__]]
342 const _Value*
343 _M_valptr() const noexcept
344 { return _M_storage._M_ptr(); }
345
346 [[__gnu__::__always_inline__]]
347 _Value&
348 _M_v() noexcept
349 { return *_M_valptr(); }
350
351 [[__gnu__::__always_inline__]]
352 const _Value&
353 _M_v() const noexcept
354 { return *_M_valptr(); }
355 };
356
357 /**
358 * Primary template struct _Hash_node_code_cache.
359 */
360 template<bool _Cache_hash_code>
361 struct _Hash_node_code_cache
362 { };
363
364 /**
365 * Specialization for node with cache, struct _Hash_node_code_cache.
366 */
367 template<>
368 struct _Hash_node_code_cache<true>
369 { std::size_t _M_hash_code; };
370
371 template<typename _Value, bool _Cache_hash_code>
372 struct _Hash_node_value
373 : _Hash_node_value_base<_Value>
374 , _Hash_node_code_cache<_Cache_hash_code>
375 { };
376
377 /**
378 * Primary template struct _Hash_node.
379 */
380 template<typename _Value, bool _Cache_hash_code>
381 struct _Hash_node
382 : _Hash_node_base
383 , _Hash_node_value<_Value, _Cache_hash_code>
384 {
385 _Hash_node*
386 _M_next() const noexcept
387 { return static_cast<_Hash_node*>(this->_M_nxt); }
388 };
389
390 /// Base class for node iterators.
391 template<typename _Value, bool _Cache_hash_code>
392 struct _Node_iterator_base
393 {
394 using __node_type = _Hash_node<_Value, _Cache_hash_code>;
395
396 __node_type* _M_cur;
397
398 _Node_iterator_base() : _M_cur(nullptr) { }
399 _Node_iterator_base(__node_type* __p) noexcept
400 : _M_cur(__p) { }
401
402 void
403 _M_incr() noexcept
404 { _M_cur = _M_cur->_M_next(); }
405
406 friend bool
407 operator==(const _Node_iterator_base& __x, const _Node_iterator_base& __y)
408 noexcept
409 { return __x._M_cur == __y._M_cur; }
410
411#if __cpp_impl_three_way_comparison < 201907L
412 friend bool
413 operator!=(const _Node_iterator_base& __x, const _Node_iterator_base& __y)
414 noexcept
415 { return __x._M_cur != __y._M_cur; }
416#endif
417 };
418
419 /// Node iterators, used to iterate through all the hashtable.
420 template<typename _Value, bool __constant_iterators, bool __cache>
421 struct _Node_iterator
422 : public _Node_iterator_base<_Value, __cache>
423 {
424 private:
425 using __base_type = _Node_iterator_base<_Value, __cache>;
426 using __node_type = typename __base_type::__node_type;
427
428 public:
429 using value_type = _Value;
430 using difference_type = std::ptrdiff_t;
431 using iterator_category = std::forward_iterator_tag;
432
433 using pointer = __conditional_t<__constant_iterators,
434 const value_type*, value_type*>;
435
436 using reference = __conditional_t<__constant_iterators,
437 const value_type&, value_type&>;
438
439 _Node_iterator() = default;
440
441 explicit
442 _Node_iterator(__node_type* __p) noexcept
443 : __base_type(__p) { }
444
445 reference
446 operator*() const noexcept
447 { return this->_M_cur->_M_v(); }
448
449 pointer
450 operator->() const noexcept
451 { return this->_M_cur->_M_valptr(); }
452
453 _Node_iterator&
454 operator++() noexcept
455 {
456 this->_M_incr();
457 return *this;
458 }
459
460 _Node_iterator
461 operator++(int) noexcept
462 {
463 _Node_iterator __tmp(*this);
464 this->_M_incr();
465 return __tmp;
466 }
467 };
468
469 /// Node const_iterators, used to iterate through all the hashtable.
470 template<typename _Value, bool __constant_iterators, bool __cache>
471 struct _Node_const_iterator
472 : public _Node_iterator_base<_Value, __cache>
473 {
474 private:
475 using __base_type = _Node_iterator_base<_Value, __cache>;
476 using __node_type = typename __base_type::__node_type;
477
478 public:
479 typedef _Value value_type;
480 typedef std::ptrdiff_t difference_type;
481 typedef std::forward_iterator_tag iterator_category;
482
483 typedef const value_type* pointer;
484 typedef const value_type& reference;
485
486 _Node_const_iterator() = default;
487
488 explicit
489 _Node_const_iterator(__node_type* __p) noexcept
490 : __base_type(__p) { }
491
492 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
493 __cache>& __x) noexcept
494 : __base_type(__x._M_cur) { }
495
496 reference
497 operator*() const noexcept
498 { return this->_M_cur->_M_v(); }
499
500 pointer
501 operator->() const noexcept
502 { return this->_M_cur->_M_valptr(); }
503
504 _Node_const_iterator&
505 operator++() noexcept
506 {
507 this->_M_incr();
508 return *this;
509 }
510
511 _Node_const_iterator
512 operator++(int) noexcept
513 {
514 _Node_const_iterator __tmp(*this);
515 this->_M_incr();
516 return __tmp;
517 }
518 };
519
520 // Many of class template _Hashtable's template parameters are policy
521 // classes. These are defaults for the policies.
522
523 /// Default range hashing function: use division to fold a large number
524 /// into the range [0, N).
525 struct _Mod_range_hashing
526 {
527 typedef std::size_t first_argument_type;
528 typedef std::size_t second_argument_type;
529 typedef std::size_t result_type;
530
531 result_type
532 operator()(first_argument_type __num,
533 second_argument_type __den) const noexcept
534 { return __num % __den; }
535 };
536
537 /// Default ranged hash function H. In principle it should be a
538 /// function object composed from objects of type H1 and H2 such that
539 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
540 /// h1 and h2. So instead we'll just use a tag to tell class template
541 /// hashtable to do that composition.
542 struct _Default_ranged_hash { };
543
544 /// Default value for rehash policy. Bucket size is (usually) the
545 /// smallest prime that keeps the load factor small enough.
546 struct _Prime_rehash_policy
547 {
548 using __has_load_factor = true_type;
549
550 _Prime_rehash_policy(float __z = 1.0) noexcept
551 : _M_max_load_factor(__z), _M_next_resize(0) { }
552
553 float
554 max_load_factor() const noexcept
555 { return _M_max_load_factor; }
556
557 // Return a bucket size no smaller than n.
558 std::size_t
559 _M_next_bkt(std::size_t __n) const;
560
561 // Return a bucket count appropriate for n elements
562 std::size_t
563 _M_bkt_for_elements(std::size_t __n) const
564 { return __builtin_ceil(__n / (double)_M_max_load_factor); }
565
566 // __n_bkt is current bucket count, __n_elt is current element count,
567 // and __n_ins is number of elements to be inserted. Do we need to
568 // increase bucket count? If so, return make_pair(true, n), where n
569 // is the new bucket count. If not, return make_pair(false, 0).
571 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
572 std::size_t __n_ins) const;
573
574 typedef std::size_t _State;
575
576 _State
577 _M_state() const
578 { return _M_next_resize; }
579
580 void
581 _M_reset() noexcept
582 { _M_next_resize = 0; }
583
584 void
585 _M_reset(_State __state)
586 { _M_next_resize = __state; }
587
588 static const std::size_t _S_growth_factor = 2;
589
590 float _M_max_load_factor;
591 mutable std::size_t _M_next_resize;
592 };
593
594 /// Range hashing function assuming that second arg is a power of 2.
595 struct _Mask_range_hashing
596 {
597 typedef std::size_t first_argument_type;
598 typedef std::size_t second_argument_type;
599 typedef std::size_t result_type;
600
601 result_type
602 operator()(first_argument_type __num,
603 second_argument_type __den) const noexcept
604 { return __num & (__den - 1); }
605 };
606
607 /// Compute closest power of 2 not less than __n
608 inline std::size_t
609 __clp2(std::size_t __n) noexcept
610 {
612 // Equivalent to return __n ? std::bit_ceil(__n) : 0;
613 if (__n < 2)
614 return __n;
615 const unsigned __lz = sizeof(size_t) > sizeof(long)
616 ? __builtin_clzll(__n - 1ull)
617 : __builtin_clzl(__n - 1ul);
618 // Doing two shifts avoids undefined behaviour when __lz == 0.
619 return (size_t(1) << (__int_traits<size_t>::__digits - __lz - 1)) << 1;
620 }
621
622 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
623 /// operations.
624 struct _Power2_rehash_policy
625 {
626 using __has_load_factor = true_type;
627
628 _Power2_rehash_policy(float __z = 1.0) noexcept
629 : _M_max_load_factor(__z), _M_next_resize(0) { }
630
631 float
632 max_load_factor() const noexcept
633 { return _M_max_load_factor; }
634
635 // Return a bucket size no smaller than n (as long as n is not above the
636 // highest power of 2).
637 std::size_t
638 _M_next_bkt(std::size_t __n) noexcept
639 {
640 if (__n == 0)
641 // Special case on container 1st initialization with 0 bucket count
642 // hint. We keep _M_next_resize to 0 to make sure that next time we
643 // want to add an element allocation will take place.
644 return 1;
645
646 const auto __max_width = std::min<size_t>(sizeof(size_t), 8);
647 const auto __max_bkt = size_t(1) << (__max_width * __CHAR_BIT__ - 1);
648 std::size_t __res = __clp2(__n);
649
650 if (__res == 0)
651 __res = __max_bkt;
652 else if (__res == 1)
653 // If __res is 1 we force it to 2 to make sure there will be an
654 // allocation so that nothing need to be stored in the initial
655 // single bucket
656 __res = 2;
657
658 if (__res == __max_bkt)
659 // Set next resize to the max value so that we never try to rehash again
660 // as we already reach the biggest possible bucket number.
661 // Note that it might result in max_load_factor not being respected.
662 _M_next_resize = size_t(-1);
663 else
664 _M_next_resize
665 = __builtin_floor(__res * (double)_M_max_load_factor);
666
667 return __res;
668 }
669
670 // Return a bucket count appropriate for n elements
671 std::size_t
672 _M_bkt_for_elements(std::size_t __n) const noexcept
673 { return __builtin_ceil(__n / (double)_M_max_load_factor); }
674
675 // __n_bkt is current bucket count, __n_elt is current element count,
676 // and __n_ins is number of elements to be inserted. Do we need to
677 // increase bucket count? If so, return make_pair(true, n), where n
678 // is the new bucket count. If not, return make_pair(false, 0).
680 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
681 std::size_t __n_ins) noexcept
682 {
683 if (__n_elt + __n_ins > _M_next_resize)
684 {
685 // If _M_next_resize is 0 it means that we have nothing allocated so
686 // far and that we start inserting elements. In this case we start
687 // with an initial bucket size of 11.
688 double __min_bkts
689 = std::max<std::size_t>(__n_elt + __n_ins, _M_next_resize ? 0 : 11)
690 / (double)_M_max_load_factor;
691 if (__min_bkts >= __n_bkt)
692 return { true,
693 _M_next_bkt(std::max<std::size_t>(__builtin_floor(__min_bkts) + 1,
694 __n_bkt * _S_growth_factor)) };
695
696 _M_next_resize
697 = __builtin_floor(__n_bkt * (double)_M_max_load_factor);
698 return { false, 0 };
699 }
700 else
701 return { false, 0 };
702 }
703
704 typedef std::size_t _State;
705
706 _State
707 _M_state() const noexcept
708 { return _M_next_resize; }
709
710 void
711 _M_reset() noexcept
712 { _M_next_resize = 0; }
713
714 void
715 _M_reset(_State __state) noexcept
716 { _M_next_resize = __state; }
717
718 static const std::size_t _S_growth_factor = 2;
719
720 float _M_max_load_factor;
721 std::size_t _M_next_resize;
722 };
723
724 template<typename _RehashPolicy>
725 struct _RehashStateGuard
726 {
727 _RehashPolicy* _M_guarded_obj;
728 typename _RehashPolicy::_State _M_prev_state;
729
730 _RehashStateGuard(_RehashPolicy& __policy)
731 : _M_guarded_obj(std::__addressof(__policy))
732 , _M_prev_state(__policy._M_state())
733 { }
734 _RehashStateGuard(const _RehashStateGuard&) = delete;
735
736 ~_RehashStateGuard()
737 {
738 if (_M_guarded_obj)
739 _M_guarded_obj->_M_reset(_M_prev_state);
740 }
741 };
742
743 // Base classes for std::_Hashtable. We define these base classes
744 // because in some cases we want to do different things depending on
745 // the value of a policy class. In some cases the policy class
746 // affects which member functions and nested typedefs are defined;
747 // we handle that by specializing base class templates. Several of
748 // the base class templates need to access other members of class
749 // template _Hashtable, so we use a variant of the "Curiously
750 // Recurring Template Pattern" (CRTP) technique.
751
752 /**
753 * Primary class template _Map_base.
754 *
755 * If the hashtable has a value type of the form pair<const T1, T2> and
756 * a key extraction policy (_ExtractKey) that returns the first part
757 * of the pair, the hashtable gets a mapped_type typedef. If it
758 * satisfies those criteria and also has unique keys, then it also
759 * gets an operator[].
760 */
761 template<typename _Key, typename _Value, typename _Alloc,
762 typename _ExtractKey, typename _Equal,
763 typename _Hash, typename _RangeHash, typename _Unused,
764 typename _RehashPolicy, typename _Traits,
765 bool _Unique_keys = _Traits::__unique_keys::value>
766 struct _Map_base { };
767
768 /// Partial specialization, __unique_keys set to false, std::pair value type.
769 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
770 typename _Hash, typename _RangeHash, typename _Unused,
771 typename _RehashPolicy, typename _Traits>
772 struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
773 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
774 {
775 using mapped_type = _Val;
776 };
777
778 /// Partial specialization, __unique_keys set to true.
779 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
780 typename _Hash, typename _RangeHash, typename _Unused,
781 typename _RehashPolicy, typename _Traits>
782 struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
783 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
784 {
785 private:
786 using __hashtable_base = _Hashtable_base<_Key, pair<const _Key, _Val>,
787 _Select1st, _Equal, _Hash,
788 _RangeHash, _Unused,
789 _Traits>;
790
791 using __hashtable = _Hashtable<_Key, pair<const _Key, _Val>, _Alloc,
792 _Select1st, _Equal, _Hash, _RangeHash,
793 _Unused, _RehashPolicy, _Traits>;
794
795 using __hash_code = typename __hashtable_base::__hash_code;
796
797 public:
798 using key_type = typename __hashtable_base::key_type;
799 using mapped_type = _Val;
800
801 mapped_type&
802 operator[](const key_type& __k);
803
804 mapped_type&
805 operator[](key_type&& __k);
806
807 // _GLIBCXX_RESOLVE_LIB_DEFECTS
808 // DR 761. unordered_map needs an at() member function.
809 mapped_type&
810 at(const key_type& __k)
811 {
812 auto __ite = static_cast<__hashtable*>(this)->find(__k);
813 if (!__ite._M_cur)
814 __throw_out_of_range(__N("unordered_map::at"));
815 return __ite->second;
816 }
817
818 const mapped_type&
819 at(const key_type& __k) const
820 {
821 auto __ite = static_cast<const __hashtable*>(this)->find(__k);
822 if (!__ite._M_cur)
823 __throw_out_of_range(__N("unordered_map::at"));
824 return __ite->second;
825 }
826 };
827
828 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
829 typename _Hash, typename _RangeHash, typename _Unused,
830 typename _RehashPolicy, typename _Traits>
831 auto
832 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
833 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
834 operator[](const key_type& __k)
835 -> mapped_type&
836 {
837 __hashtable* __h = static_cast<__hashtable*>(this);
838 __hash_code __code = __h->_M_hash_code(__k);
839 std::size_t __bkt = __h->_M_bucket_index(__code);
840 if (auto __node = __h->_M_find_node(__bkt, __k, __code))
841 return __node->_M_v().second;
842
843 typename __hashtable::_Scoped_node __node {
844 __h,
847 std::tuple<>()
848 };
849 auto __pos
850 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
851 __node._M_node = nullptr;
852 return __pos->second;
853 }
854
855 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
856 typename _Hash, typename _RangeHash, typename _Unused,
857 typename _RehashPolicy, typename _Traits>
858 auto
859 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
860 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
861 operator[](key_type&& __k)
862 -> mapped_type&
863 {
864 __hashtable* __h = static_cast<__hashtable*>(this);
865 __hash_code __code = __h->_M_hash_code(__k);
866 std::size_t __bkt = __h->_M_bucket_index(__code);
867 if (auto __node = __h->_M_find_node(__bkt, __k, __code))
868 return __node->_M_v().second;
869
870 typename __hashtable::_Scoped_node __node {
871 __h,
874 std::tuple<>()
875 };
876 auto __pos
877 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
878 __node._M_node = nullptr;
879 return __pos->second;
880 }
881
882 // Partial specialization for unordered_map<const T, U>, see PR 104174.
883 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
884 typename _Hash, typename _RangeHash, typename _Unused,
885 typename _RehashPolicy, typename _Traits, bool __uniq>
886 struct _Map_base<const _Key, pair<const _Key, _Val>,
887 _Alloc, _Select1st, _Equal, _Hash,
888 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
889 : _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal, _Hash,
890 _RangeHash, _Unused, _RehashPolicy, _Traits, __uniq>
891 { };
892
893 /**
894 * Primary class template _Insert_base.
895 *
896 * Defines @c insert member functions appropriate to all _Hashtables.
897 */
898 template<typename _Key, typename _Value, typename _Alloc,
899 typename _ExtractKey, typename _Equal,
900 typename _Hash, typename _RangeHash, typename _Unused,
901 typename _RehashPolicy, typename _Traits>
902 struct _Insert_base
903 {
904 protected:
905 using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
906 _Equal, _Hash, _RangeHash,
907 _Unused, _Traits>;
908
909 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
910 _Hash, _RangeHash,
911 _Unused, _RehashPolicy, _Traits>;
912
913 using __hash_cached = typename _Traits::__hash_cached;
914 using __constant_iterators = typename _Traits::__constant_iterators;
915
916 using __hashtable_alloc = _Hashtable_alloc<
917 __alloc_rebind<_Alloc, _Hash_node<_Value,
918 __hash_cached::value>>>;
919
920 using value_type = typename __hashtable_base::value_type;
921 using size_type = typename __hashtable_base::size_type;
922
923 using __unique_keys = typename _Traits::__unique_keys;
924 using __node_alloc_type = typename __hashtable_alloc::__node_alloc_type;
925 using __node_gen_type = _AllocNode<__node_alloc_type>;
926
927 __hashtable&
928 _M_conjure_hashtable()
929 { return *(static_cast<__hashtable*>(this)); }
930
931 template<typename _InputIterator, typename _NodeGetter>
932 void
933 _M_insert_range(_InputIterator __first, _InputIterator __last,
934 const _NodeGetter&, true_type __uks);
935
936 template<typename _InputIterator, typename _NodeGetter>
937 void
938 _M_insert_range(_InputIterator __first, _InputIterator __last,
939 const _NodeGetter&, false_type __uks);
940
941 public:
942 using iterator = _Node_iterator<_Value, __constant_iterators::value,
943 __hash_cached::value>;
944
945 using const_iterator = _Node_const_iterator<_Value,
946 __constant_iterators::value,
947 __hash_cached::value>;
948
949 using __ireturn_type = __conditional_t<__unique_keys::value,
951 iterator>;
952
953 __ireturn_type
954 insert(const value_type& __v)
955 {
956 __hashtable& __h = _M_conjure_hashtable();
957 __node_gen_type __node_gen(__h);
958 return __h._M_insert(__v, __node_gen, __unique_keys{});
959 }
960
961 iterator
962 insert(const_iterator __hint, const value_type& __v)
963 {
964 __hashtable& __h = _M_conjure_hashtable();
965 __node_gen_type __node_gen(__h);
966 return __h._M_insert(__hint, __v, __node_gen, __unique_keys{});
967 }
968
969#ifdef __glibcxx_unordered_map_try_emplace // C++ >= 17 && HOSTED
970 template<typename _KType, typename... _Args>
972 try_emplace(const_iterator, _KType&& __k, _Args&&... __args)
973 {
974 __hashtable& __h = _M_conjure_hashtable();
975 auto __code = __h._M_hash_code(__k);
976 std::size_t __bkt = __h._M_bucket_index(__code);
977 if (auto __node = __h._M_find_node(__bkt, __k, __code))
978 return { iterator(__node), false };
979
980 typename __hashtable::_Scoped_node __node {
981 &__h,
985 };
986 auto __it
987 = __h._M_insert_unique_node(__bkt, __code, __node._M_node);
988 __node._M_node = nullptr;
989 return { __it, true };
990 }
991#endif
992
993 void
994 insert(initializer_list<value_type> __l)
995 { this->insert(__l.begin(), __l.end()); }
996
997 template<typename _InputIterator>
998 void
999 insert(_InputIterator __first, _InputIterator __last)
1000 {
1001 __hashtable& __h = _M_conjure_hashtable();
1002 __node_gen_type __node_gen(__h);
1003 return _M_insert_range(__first, __last, __node_gen, __unique_keys{});
1004 }
1005 };
1006
1007 template<typename _Key, typename _Value, typename _Alloc,
1008 typename _ExtractKey, typename _Equal,
1009 typename _Hash, typename _RangeHash, typename _Unused,
1010 typename _RehashPolicy, typename _Traits>
1011 template<typename _InputIterator, typename _NodeGetter>
1012 void
1013 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1014 _Hash, _RangeHash, _Unused,
1015 _RehashPolicy, _Traits>::
1016 _M_insert_range(_InputIterator __first, _InputIterator __last,
1017 const _NodeGetter& __node_gen, true_type __uks)
1018 {
1019 __hashtable& __h = _M_conjure_hashtable();
1020 for (; __first != __last; ++__first)
1021 __h._M_insert(*__first, __node_gen, __uks);
1022 }
1023
1024 template<typename _Key, typename _Value, typename _Alloc,
1025 typename _ExtractKey, typename _Equal,
1026 typename _Hash, typename _RangeHash, typename _Unused,
1027 typename _RehashPolicy, typename _Traits>
1028 template<typename _InputIterator, typename _NodeGetter>
1029 void
1030 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1031 _Hash, _RangeHash, _Unused,
1032 _RehashPolicy, _Traits>::
1033 _M_insert_range(_InputIterator __first, _InputIterator __last,
1034 const _NodeGetter& __node_gen, false_type __uks)
1035 {
1036 using __rehash_guard_t = typename __hashtable::__rehash_guard_t;
1037 using __pair_type = std::pair<bool, std::size_t>;
1038
1039 size_type __n_elt = __detail::__distance_fw(__first, __last);
1040 if (__n_elt == 0)
1041 return;
1042
1043 __hashtable& __h = _M_conjure_hashtable();
1044 __rehash_guard_t __rehash_guard(__h._M_rehash_policy);
1045 __pair_type __do_rehash
1046 = __h._M_rehash_policy._M_need_rehash(__h._M_bucket_count,
1047 __h._M_element_count,
1048 __n_elt);
1049
1050 if (__do_rehash.first)
1051 __h._M_rehash(__do_rehash.second, __uks);
1052
1053 __rehash_guard._M_guarded_obj = nullptr;
1054 for (; __first != __last; ++__first)
1055 __h._M_insert(*__first, __node_gen, __uks);
1056 }
1057
1058 /**
1059 * Primary class template _Insert.
1060 *
1061 * Defines @c insert member functions that depend on _Hashtable policies,
1062 * via partial specializations.
1063 */
1064 template<typename _Key, typename _Value, typename _Alloc,
1065 typename _ExtractKey, typename _Equal,
1066 typename _Hash, typename _RangeHash, typename _Unused,
1067 typename _RehashPolicy, typename _Traits,
1068 bool _Constant_iterators = _Traits::__constant_iterators::value>
1069 struct _Insert;
1070
1071 /// Specialization.
1072 template<typename _Key, typename _Value, typename _Alloc,
1073 typename _ExtractKey, typename _Equal,
1074 typename _Hash, typename _RangeHash, typename _Unused,
1075 typename _RehashPolicy, typename _Traits>
1076 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1077 _Hash, _RangeHash, _Unused,
1078 _RehashPolicy, _Traits, true>
1079 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1080 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>
1081 {
1082 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
1083 _Equal, _Hash, _RangeHash, _Unused,
1084 _RehashPolicy, _Traits>;
1085
1086 using value_type = typename __base_type::value_type;
1087 using iterator = typename __base_type::iterator;
1088 using const_iterator = typename __base_type::const_iterator;
1089 using __ireturn_type = typename __base_type::__ireturn_type;
1090
1091 using __unique_keys = typename __base_type::__unique_keys;
1092 using __hashtable = typename __base_type::__hashtable;
1093 using __node_gen_type = typename __base_type::__node_gen_type;
1094
1095 using __base_type::insert;
1096
1097 __ireturn_type
1098 insert(value_type&& __v)
1099 {
1100 __hashtable& __h = this->_M_conjure_hashtable();
1101 __node_gen_type __node_gen(__h);
1102 return __h._M_insert(std::move(__v), __node_gen, __unique_keys{});
1103 }
1104
1105 iterator
1106 insert(const_iterator __hint, value_type&& __v)
1107 {
1108 __hashtable& __h = this->_M_conjure_hashtable();
1109 __node_gen_type __node_gen(__h);
1110 return __h._M_insert(__hint, std::move(__v), __node_gen,
1111 __unique_keys{});
1112 }
1113 };
1114
1115 /// Specialization.
1116 template<typename _Key, typename _Value, typename _Alloc,
1117 typename _ExtractKey, typename _Equal,
1118 typename _Hash, typename _RangeHash, typename _Unused,
1119 typename _RehashPolicy, typename _Traits>
1120 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1121 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
1122 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1123 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>
1124 {
1125 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
1126 _Equal, _Hash, _RangeHash, _Unused,
1127 _RehashPolicy, _Traits>;
1128 using value_type = typename __base_type::value_type;
1129 using iterator = typename __base_type::iterator;
1130 using const_iterator = typename __base_type::const_iterator;
1131
1132 using __unique_keys = typename __base_type::__unique_keys;
1133 using __hashtable = typename __base_type::__hashtable;
1134 using __ireturn_type = typename __base_type::__ireturn_type;
1135
1136 using __base_type::insert;
1137
1138 template<typename _Pair>
1140
1141 template<typename _Pair>
1143
1144 template<typename _Pair>
1145 using _IFconsp = typename _IFcons<_Pair>::type;
1146
1147 template<typename _Pair, typename = _IFconsp<_Pair>>
1148 __ireturn_type
1149 insert(_Pair&& __v)
1150 {
1151 __hashtable& __h = this->_M_conjure_hashtable();
1152 return __h._M_emplace(__unique_keys{}, std::forward<_Pair>(__v));
1153 }
1154
1155 template<typename _Pair, typename = _IFconsp<_Pair>>
1156 iterator
1157 insert(const_iterator __hint, _Pair&& __v)
1158 {
1159 __hashtable& __h = this->_M_conjure_hashtable();
1160 return __h._M_emplace(__hint, __unique_keys{},
1161 std::forward<_Pair>(__v));
1162 }
1163 };
1164
1165 template<typename _Policy>
1166 using __has_load_factor = typename _Policy::__has_load_factor;
1167
1168 /**
1169 * Primary class template _Rehash_base.
1170 *
1171 * Give hashtable the max_load_factor functions and reserve iff the
1172 * rehash policy supports it.
1173 */
1174 template<typename _Key, typename _Value, typename _Alloc,
1175 typename _ExtractKey, typename _Equal,
1176 typename _Hash, typename _RangeHash, typename _Unused,
1177 typename _RehashPolicy, typename _Traits,
1178 typename =
1179 __detected_or_t<false_type, __has_load_factor, _RehashPolicy>>
1180 struct _Rehash_base;
1181
1182 /// Specialization when rehash policy doesn't provide load factor management.
1183 template<typename _Key, typename _Value, typename _Alloc,
1184 typename _ExtractKey, typename _Equal,
1185 typename _Hash, typename _RangeHash, typename _Unused,
1186 typename _RehashPolicy, typename _Traits>
1187 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1188 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
1189 false_type /* Has load factor */>
1190 {
1191 };
1192
1193 /// Specialization when rehash policy provide load factor management.
1194 template<typename _Key, typename _Value, typename _Alloc,
1195 typename _ExtractKey, typename _Equal,
1196 typename _Hash, typename _RangeHash, typename _Unused,
1197 typename _RehashPolicy, typename _Traits>
1198 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1199 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
1200 true_type /* Has load factor */>
1201 {
1202 private:
1203 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1204 _Equal, _Hash, _RangeHash, _Unused,
1205 _RehashPolicy, _Traits>;
1206
1207 public:
1208 float
1209 max_load_factor() const noexcept
1210 {
1211 const __hashtable* __this = static_cast<const __hashtable*>(this);
1212 return __this->__rehash_policy().max_load_factor();
1213 }
1214
1215 void
1216 max_load_factor(float __z)
1217 {
1218 __hashtable* __this = static_cast<__hashtable*>(this);
1219 __this->__rehash_policy(_RehashPolicy(__z));
1220 }
1221
1222 void
1223 reserve(std::size_t __n)
1224 {
1225 __hashtable* __this = static_cast<__hashtable*>(this);
1226 __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n));
1227 }
1228 };
1229
1230 /**
1231 * Primary class template _Hashtable_ebo_helper.
1232 *
1233 * Helper class using EBO when it is not forbidden (the type is not
1234 * final) and when it is worth it (the type is empty.)
1235 */
1236 template<int _Nm, typename _Tp,
1237 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
1238 struct _Hashtable_ebo_helper;
1239
1240 /// Specialization using EBO.
1241 template<int _Nm, typename _Tp>
1242 struct _Hashtable_ebo_helper<_Nm, _Tp, true>
1243 : private _Tp
1244 {
1245 _Hashtable_ebo_helper() noexcept(noexcept(_Tp())) : _Tp() { }
1246
1247 template<typename _OtherTp>
1248 _Hashtable_ebo_helper(_OtherTp&& __tp)
1249 : _Tp(std::forward<_OtherTp>(__tp))
1250 { }
1251
1252 const _Tp& _M_cget() const { return static_cast<const _Tp&>(*this); }
1253 _Tp& _M_get() { return static_cast<_Tp&>(*this); }
1254 };
1255
1256 /// Specialization not using EBO.
1257 template<int _Nm, typename _Tp>
1258 struct _Hashtable_ebo_helper<_Nm, _Tp, false>
1259 {
1260 _Hashtable_ebo_helper() = default;
1261
1262 template<typename _OtherTp>
1263 _Hashtable_ebo_helper(_OtherTp&& __tp)
1264 : _M_tp(std::forward<_OtherTp>(__tp))
1265 { }
1266
1267 const _Tp& _M_cget() const { return _M_tp; }
1268 _Tp& _M_get() { return _M_tp; }
1269
1270 private:
1271 _Tp _M_tp{};
1272 };
1273
1274 /**
1275 * Primary class template _Local_iterator_base.
1276 *
1277 * Base class for local iterators, used to iterate within a bucket
1278 * but not between buckets.
1279 */
1280 template<typename _Key, typename _Value, typename _ExtractKey,
1281 typename _Hash, typename _RangeHash, typename _Unused,
1282 bool __cache_hash_code>
1283 struct _Local_iterator_base;
1284
1285 /**
1286 * Primary class template _Hash_code_base.
1287 *
1288 * Encapsulates two policy issues that aren't quite orthogonal.
1289 * (1) the difference between using a ranged hash function and using
1290 * the combination of a hash function and a range-hashing function.
1291 * In the former case we don't have such things as hash codes, so
1292 * we have a dummy type as placeholder.
1293 * (2) Whether or not we cache hash codes. Caching hash codes is
1294 * meaningless if we have a ranged hash function.
1295 *
1296 * We also put the key extraction objects here, for convenience.
1297 * Each specialization derives from one or more of the template
1298 * parameters to benefit from Ebo. This is important as this type
1299 * is inherited in some cases by the _Local_iterator_base type used
1300 * to implement local_iterator and const_local_iterator. As with
1301 * any iterator type we prefer to make it as small as possible.
1302 */
1303 template<typename _Key, typename _Value, typename _ExtractKey,
1304 typename _Hash, typename _RangeHash, typename _Unused,
1305 bool __cache_hash_code>
1306 struct _Hash_code_base
1307 : private _Hashtable_ebo_helper<1, _Hash>
1308 {
1309 private:
1310 using __ebo_hash = _Hashtable_ebo_helper<1, _Hash>;
1311
1312 // Gives the local iterator implementation access to _M_bucket_index().
1313 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1314 _Hash, _RangeHash, _Unused, false>;
1315
1316 public:
1317 typedef _Hash hasher;
1318
1319 hasher
1320 hash_function() const
1321 { return _M_hash(); }
1322
1323 protected:
1324 typedef std::size_t __hash_code;
1325
1326 // We need the default constructor for the local iterators and _Hashtable
1327 // default constructor.
1328 _Hash_code_base() = default;
1329
1330 _Hash_code_base(const _Hash& __hash) : __ebo_hash(__hash) { }
1331
1332 __hash_code
1333 _M_hash_code(const _Key& __k) const
1334 {
1335 static_assert(__is_invocable<const _Hash&, const _Key&>{},
1336 "hash function must be invocable with an argument of key type");
1337 return _M_hash()(__k);
1338 }
1339
1340 template<typename _Kt>
1341 __hash_code
1342 _M_hash_code_tr(const _Kt& __k) const
1343 {
1344 static_assert(__is_invocable<const _Hash&, const _Kt&>{},
1345 "hash function must be invocable with an argument of key type");
1346 return _M_hash()(__k);
1347 }
1348
1349 __hash_code
1350 _M_hash_code(const _Hash_node_value<_Value, false>& __n) const
1351 { return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1352
1353 __hash_code
1354 _M_hash_code(const _Hash_node_value<_Value, true>& __n) const
1355 { return __n._M_hash_code; }
1356
1357 std::size_t
1358 _M_bucket_index(__hash_code __c, std::size_t __bkt_count) const
1359 { return _RangeHash{}(__c, __bkt_count); }
1360
1361 std::size_t
1362 _M_bucket_index(const _Hash_node_value<_Value, false>& __n,
1363 std::size_t __bkt_count) const
1364 noexcept( noexcept(declval<const _Hash&>()(declval<const _Key&>()))
1365 && noexcept(declval<const _RangeHash&>()((__hash_code)0,
1366 (std::size_t)0)) )
1367 {
1368 return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())),
1369 __bkt_count);
1370 }
1371
1372 std::size_t
1373 _M_bucket_index(const _Hash_node_value<_Value, true>& __n,
1374 std::size_t __bkt_count) const
1375 noexcept( noexcept(declval<const _RangeHash&>()((__hash_code)0,
1376 (std::size_t)0)) )
1377 { return _RangeHash{}(__n._M_hash_code, __bkt_count); }
1378
1379 void
1380 _M_store_code(_Hash_node_code_cache<false>&, __hash_code) const
1381 { }
1382
1383 void
1384 _M_copy_code(_Hash_node_code_cache<false>&,
1385 const _Hash_node_code_cache<false>&) const
1386 { }
1387
1388 void
1389 _M_store_code(_Hash_node_code_cache<true>& __n, __hash_code __c) const
1390 { __n._M_hash_code = __c; }
1391
1392 void
1393 _M_copy_code(_Hash_node_code_cache<true>& __to,
1394 const _Hash_node_code_cache<true>& __from) const
1395 { __to._M_hash_code = __from._M_hash_code; }
1396
1397 void
1398 _M_swap(_Hash_code_base& __x)
1399 { std::swap(__ebo_hash::_M_get(), __x.__ebo_hash::_M_get()); }
1400
1401 const _Hash&
1402 _M_hash() const { return __ebo_hash::_M_cget(); }
1403 };
1404
1405 /// Partial specialization used when nodes contain a cached hash code.
1406 template<typename _Key, typename _Value, typename _ExtractKey,
1407 typename _Hash, typename _RangeHash, typename _Unused>
1408 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1409 _Hash, _RangeHash, _Unused, true>
1410 : public _Node_iterator_base<_Value, true>
1411 {
1412 protected:
1413 using __base_node_iter = _Node_iterator_base<_Value, true>;
1414 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1415 _Hash, _RangeHash, _Unused, true>;
1416
1417 _Local_iterator_base() = default;
1418 _Local_iterator_base(const __hash_code_base&,
1419 _Hash_node<_Value, true>* __p,
1420 std::size_t __bkt, std::size_t __bkt_count)
1421 : __base_node_iter(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1422 { }
1423
1424 void
1425 _M_incr()
1426 {
1427 __base_node_iter::_M_incr();
1428 if (this->_M_cur)
1429 {
1430 std::size_t __bkt
1431 = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count);
1432 if (__bkt != _M_bucket)
1433 this->_M_cur = nullptr;
1434 }
1435 }
1436
1437 std::size_t _M_bucket;
1438 std::size_t _M_bucket_count;
1439
1440 public:
1441 std::size_t
1442 _M_get_bucket() const { return _M_bucket; } // for debug mode
1443 };
1444
1445 // Uninitialized storage for a _Hash_code_base.
1446 // This type is DefaultConstructible and Assignable even if the
1447 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1448 // can be DefaultConstructible and Assignable.
1449 template<typename _Tp, bool _IsEmpty = std::is_empty<_Tp>::value>
1450 struct _Hash_code_storage
1451 {
1452 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
1453
1454 _Tp*
1455 _M_h() { return _M_storage._M_ptr(); }
1456
1457 const _Tp*
1458 _M_h() const { return _M_storage._M_ptr(); }
1459 };
1460
1461 // Empty partial specialization for empty _Hash_code_base types.
1462 template<typename _Tp>
1463 struct _Hash_code_storage<_Tp, true>
1464 {
1465 static_assert( std::is_empty<_Tp>::value, "Type must be empty" );
1466
1467 // As _Tp is an empty type there will be no bytes written/read through
1468 // the cast pointer, so no strict-aliasing violation.
1469 _Tp*
1470 _M_h() { return reinterpret_cast<_Tp*>(this); }
1471
1472 const _Tp*
1473 _M_h() const { return reinterpret_cast<const _Tp*>(this); }
1474 };
1475
1476 template<typename _Key, typename _Value, typename _ExtractKey,
1477 typename _Hash, typename _RangeHash, typename _Unused>
1478 using __hash_code_for_local_iter
1479 = _Hash_code_storage<_Hash_code_base<_Key, _Value, _ExtractKey,
1480 _Hash, _RangeHash, _Unused, false>>;
1481
1482 // Partial specialization used when hash codes are not cached
1483 template<typename _Key, typename _Value, typename _ExtractKey,
1484 typename _Hash, typename _RangeHash, typename _Unused>
1485 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1486 _Hash, _RangeHash, _Unused, false>
1487 : __hash_code_for_local_iter<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1488 _Unused>
1489 , _Node_iterator_base<_Value, false>
1490 {
1491 protected:
1492 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1493 _Hash, _RangeHash, _Unused, false>;
1494 using __node_iter_base = _Node_iterator_base<_Value, false>;
1495
1496 _Local_iterator_base() : _M_bucket_count(-1) { }
1497
1498 _Local_iterator_base(const __hash_code_base& __base,
1499 _Hash_node<_Value, false>* __p,
1500 std::size_t __bkt, std::size_t __bkt_count)
1501 : __node_iter_base(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1502 { _M_init(__base); }
1503
1504 ~_Local_iterator_base()
1505 {
1506 if (_M_bucket_count != size_t(-1))
1507 _M_destroy();
1508 }
1509
1510 _Local_iterator_base(const _Local_iterator_base& __iter)
1511 : __node_iter_base(__iter._M_cur), _M_bucket(__iter._M_bucket)
1512 , _M_bucket_count(__iter._M_bucket_count)
1513 {
1514 if (_M_bucket_count != size_t(-1))
1515 _M_init(*__iter._M_h());
1516 }
1517
1518 _Local_iterator_base&
1519 operator=(const _Local_iterator_base& __iter)
1520 {
1521 if (_M_bucket_count != -1)
1522 _M_destroy();
1523 this->_M_cur = __iter._M_cur;
1524 _M_bucket = __iter._M_bucket;
1525 _M_bucket_count = __iter._M_bucket_count;
1526 if (_M_bucket_count != -1)
1527 _M_init(*__iter._M_h());
1528 return *this;
1529 }
1530
1531 void
1532 _M_incr()
1533 {
1534 __node_iter_base::_M_incr();
1535 if (this->_M_cur)
1536 {
1537 std::size_t __bkt = this->_M_h()->_M_bucket_index(*this->_M_cur,
1538 _M_bucket_count);
1539 if (__bkt != _M_bucket)
1540 this->_M_cur = nullptr;
1541 }
1542 }
1543
1544 std::size_t _M_bucket;
1545 std::size_t _M_bucket_count;
1546
1547 void
1548 _M_init(const __hash_code_base& __base)
1549 { ::new(this->_M_h()) __hash_code_base(__base); }
1550
1551 void
1552 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1553
1554 public:
1555 std::size_t
1556 _M_get_bucket() const { return _M_bucket; } // for debug mode
1557 };
1558
1559 /// local iterators
1560 template<typename _Key, typename _Value, typename _ExtractKey,
1561 typename _Hash, typename _RangeHash, typename _Unused,
1562 bool __constant_iterators, bool __cache>
1563 struct _Local_iterator
1564 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1565 _Hash, _RangeHash, _Unused, __cache>
1566 {
1567 private:
1568 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1569 _Hash, _RangeHash, _Unused, __cache>;
1570 using __hash_code_base = typename __base_type::__hash_code_base;
1571
1572 public:
1573 using value_type = _Value;
1574 using pointer = __conditional_t<__constant_iterators,
1575 const value_type*, value_type*>;
1576 using reference = __conditional_t<__constant_iterators,
1577 const value_type&, value_type&>;
1578 using difference_type = ptrdiff_t;
1579 using iterator_category = forward_iterator_tag;
1580
1581 _Local_iterator() = default;
1582
1583 _Local_iterator(const __hash_code_base& __base,
1584 _Hash_node<_Value, __cache>* __n,
1585 std::size_t __bkt, std::size_t __bkt_count)
1586 : __base_type(__base, __n, __bkt, __bkt_count)
1587 { }
1588
1589 reference
1590 operator*() const
1591 { return this->_M_cur->_M_v(); }
1592
1593 pointer
1594 operator->() const
1595 { return this->_M_cur->_M_valptr(); }
1596
1597 _Local_iterator&
1598 operator++()
1599 {
1600 this->_M_incr();
1601 return *this;
1602 }
1603
1604 _Local_iterator
1605 operator++(int)
1606 {
1607 _Local_iterator __tmp(*this);
1608 this->_M_incr();
1609 return __tmp;
1610 }
1611 };
1612
1613 /// local const_iterators
1614 template<typename _Key, typename _Value, typename _ExtractKey,
1615 typename _Hash, typename _RangeHash, typename _Unused,
1616 bool __constant_iterators, bool __cache>
1617 struct _Local_const_iterator
1618 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1619 _Hash, _RangeHash, _Unused, __cache>
1620 {
1621 private:
1622 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
1623 _Hash, _RangeHash, _Unused, __cache>;
1624 using __hash_code_base = typename __base_type::__hash_code_base;
1625
1626 public:
1627 typedef _Value value_type;
1628 typedef const value_type* pointer;
1629 typedef const value_type& reference;
1630 typedef std::ptrdiff_t difference_type;
1631 typedef std::forward_iterator_tag iterator_category;
1632
1633 _Local_const_iterator() = default;
1634
1635 _Local_const_iterator(const __hash_code_base& __base,
1636 _Hash_node<_Value, __cache>* __n,
1637 std::size_t __bkt, std::size_t __bkt_count)
1638 : __base_type(__base, __n, __bkt, __bkt_count)
1639 { }
1640
1641 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1642 _Hash, _RangeHash, _Unused,
1643 __constant_iterators,
1644 __cache>& __x)
1645 : __base_type(__x)
1646 { }
1647
1648 reference
1649 operator*() const
1650 { return this->_M_cur->_M_v(); }
1651
1652 pointer
1653 operator->() const
1654 { return this->_M_cur->_M_valptr(); }
1655
1656 _Local_const_iterator&
1657 operator++()
1658 {
1659 this->_M_incr();
1660 return *this;
1661 }
1662
1663 _Local_const_iterator
1664 operator++(int)
1665 {
1666 _Local_const_iterator __tmp(*this);
1667 this->_M_incr();
1668 return __tmp;
1669 }
1670 };
1671
1672 /**
1673 * Primary class template _Hashtable_base.
1674 *
1675 * Helper class adding management of _Equal functor to
1676 * _Hash_code_base type.
1677 *
1678 * Base class templates are:
1679 * - __detail::_Hash_code_base
1680 * - __detail::_Hashtable_ebo_helper
1681 */
1682 template<typename _Key, typename _Value, typename _ExtractKey,
1683 typename _Equal, typename _Hash, typename _RangeHash,
1684 typename _Unused, typename _Traits>
1685 struct _Hashtable_base
1686 : public _Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1687 _Unused, _Traits::__hash_cached::value>,
1688 private _Hashtable_ebo_helper<0, _Equal>
1689 {
1690 public:
1691 typedef _Key key_type;
1692 typedef _Value value_type;
1693 typedef _Equal key_equal;
1694 typedef std::size_t size_type;
1695 typedef std::ptrdiff_t difference_type;
1696
1697 using __traits_type = _Traits;
1698 using __hash_cached = typename __traits_type::__hash_cached;
1699
1700 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1701 _Hash, _RangeHash, _Unused,
1702 __hash_cached::value>;
1703
1704 using __hash_code = typename __hash_code_base::__hash_code;
1705
1706 private:
1707 using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
1708
1709 static bool
1710 _S_equals(__hash_code, const _Hash_node_code_cache<false>&)
1711 { return true; }
1712
1713 static bool
1714 _S_node_equals(const _Hash_node_code_cache<false>&,
1715 const _Hash_node_code_cache<false>&)
1716 { return true; }
1717
1718 static bool
1719 _S_equals(__hash_code __c, const _Hash_node_code_cache<true>& __n)
1720 { return __c == __n._M_hash_code; }
1721
1722 static bool
1723 _S_node_equals(const _Hash_node_code_cache<true>& __lhn,
1724 const _Hash_node_code_cache<true>& __rhn)
1725 { return __lhn._M_hash_code == __rhn._M_hash_code; }
1726
1727 protected:
1728 _Hashtable_base() = default;
1729
1730 _Hashtable_base(const _Hash& __hash, const _Equal& __eq)
1731 : __hash_code_base(__hash), _EqualEBO(__eq)
1732 { }
1733
1734 bool
1735 _M_key_equals(const _Key& __k,
1736 const _Hash_node_value<_Value,
1737 __hash_cached::value>& __n) const
1738 {
1739 static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
1740 "key equality predicate must be invocable with two arguments of "
1741 "key type");
1742 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1743 }
1744
1745 template<typename _Kt>
1746 bool
1747 _M_key_equals_tr(const _Kt& __k,
1748 const _Hash_node_value<_Value,
1749 __hash_cached::value>& __n) const
1750 {
1751 static_assert(
1752 __is_invocable<const _Equal&, const _Kt&, const _Key&>{},
1753 "key equality predicate must be invocable with two arguments of "
1754 "key type");
1755 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1756 }
1757
1758 bool
1759 _M_equals(const _Key& __k, __hash_code __c,
1760 const _Hash_node_value<_Value, __hash_cached::value>& __n) const
1761 { return _S_equals(__c, __n) && _M_key_equals(__k, __n); }
1762
1763 template<typename _Kt>
1764 bool
1765 _M_equals_tr(const _Kt& __k, __hash_code __c,
1766 const _Hash_node_value<_Value,
1767 __hash_cached::value>& __n) const
1768 { return _S_equals(__c, __n) && _M_key_equals_tr(__k, __n); }
1769
1770 bool
1771 _M_node_equals(
1772 const _Hash_node_value<_Value, __hash_cached::value>& __lhn,
1773 const _Hash_node_value<_Value, __hash_cached::value>& __rhn) const
1774 {
1775 return _S_node_equals(__lhn, __rhn)
1776 && _M_key_equals(_ExtractKey{}(__lhn._M_v()), __rhn);
1777 }
1778
1779 void
1780 _M_swap(_Hashtable_base& __x)
1781 {
1782 __hash_code_base::_M_swap(__x);
1783 std::swap(_EqualEBO::_M_get(), __x._EqualEBO::_M_get());
1784 }
1785
1786 const _Equal&
1787 _M_eq() const { return _EqualEBO::_M_cget(); }
1788 };
1789
1790 /**
1791 * Primary class template _Equality.
1792 *
1793 * This is for implementing equality comparison for unordered
1794 * containers, per N3068, by John Lakos and Pablo Halpern.
1795 * Algorithmically, we follow closely the reference implementations
1796 * therein.
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 bool _Unique_keys = _Traits::__unique_keys::value>
1803 struct _Equality;
1804
1805 /// unordered_map and unordered_set specializations.
1806 template<typename _Key, typename _Value, typename _Alloc,
1807 typename _ExtractKey, typename _Equal,
1808 typename _Hash, typename _RangeHash, typename _Unused,
1809 typename _RehashPolicy, typename _Traits>
1810 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1811 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
1812 {
1813 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1814 _Hash, _RangeHash, _Unused,
1815 _RehashPolicy, _Traits>;
1816
1817 bool
1818 _M_equal(const __hashtable&) const;
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 bool
1826 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1827 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
1828 _M_equal(const __hashtable& __other) const
1829 {
1830 using __node_ptr = typename __hashtable::__node_ptr;
1831 const __hashtable* __this = static_cast<const __hashtable*>(this);
1832 if (__this->size() != __other.size())
1833 return false;
1834
1835 for (auto __x_n = __this->_M_begin(); __x_n; __x_n = __x_n->_M_next())
1836 {
1837 std::size_t __ybkt = __other._M_bucket_index(*__x_n);
1838 auto __prev_n = __other._M_buckets[__ybkt];
1839 if (!__prev_n)
1840 return false;
1841
1842 for (__node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);;
1843 __n = __n->_M_next())
1844 {
1845 if (__n->_M_v() == __x_n->_M_v())
1846 break;
1847
1848 if (!__n->_M_nxt
1849 || __other._M_bucket_index(*__n->_M_next()) != __ybkt)
1850 return false;
1851 }
1852 }
1853
1854 return true;
1855 }
1856
1857 /// unordered_multiset and unordered_multimap specializations.
1858 template<typename _Key, typename _Value, typename _Alloc,
1859 typename _ExtractKey, typename _Equal,
1860 typename _Hash, typename _RangeHash, typename _Unused,
1861 typename _RehashPolicy, typename _Traits>
1862 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1863 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
1864 {
1865 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1866 _Hash, _RangeHash, _Unused,
1867 _RehashPolicy, _Traits>;
1868
1869 bool
1870 _M_equal(const __hashtable&) const;
1871 };
1872
1873 template<typename _Key, typename _Value, typename _Alloc,
1874 typename _ExtractKey, typename _Equal,
1875 typename _Hash, typename _RangeHash, typename _Unused,
1876 typename _RehashPolicy, typename _Traits>
1877 bool
1878 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1879 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>::
1880 _M_equal(const __hashtable& __other) const
1881 {
1882 using __node_ptr = typename __hashtable::__node_ptr;
1883 using const_iterator = typename __hashtable::const_iterator;
1884 const __hashtable* __this = static_cast<const __hashtable*>(this);
1885 if (__this->size() != __other.size())
1886 return false;
1887
1888 for (auto __x_n = __this->_M_begin(); __x_n;)
1889 {
1890 std::size_t __x_count = 1;
1891 auto __x_n_end = __x_n->_M_next();
1892 for (; __x_n_end
1893 && __this->key_eq()(_ExtractKey{}(__x_n->_M_v()),
1894 _ExtractKey{}(__x_n_end->_M_v()));
1895 __x_n_end = __x_n_end->_M_next())
1896 ++__x_count;
1897
1898 std::size_t __ybkt = __other._M_bucket_index(*__x_n);
1899 auto __y_prev_n = __other._M_buckets[__ybkt];
1900 if (!__y_prev_n)
1901 return false;
1902
1903 __node_ptr __y_n = static_cast<__node_ptr>(__y_prev_n->_M_nxt);
1904 for (;;)
1905 {
1906 if (__this->key_eq()(_ExtractKey{}(__y_n->_M_v()),
1907 _ExtractKey{}(__x_n->_M_v())))
1908 break;
1909
1910 auto __y_ref_n = __y_n;
1911 for (__y_n = __y_n->_M_next(); __y_n; __y_n = __y_n->_M_next())
1912 if (!__other._M_node_equals(*__y_ref_n, *__y_n))
1913 break;
1914
1915 if (!__y_n || __other._M_bucket_index(*__y_n) != __ybkt)
1916 return false;
1917 }
1918
1919 auto __y_n_end = __y_n;
1920 for (; __y_n_end; __y_n_end = __y_n_end->_M_next())
1921 if (--__x_count == 0)
1922 break;
1923
1924 if (__x_count != 0)
1925 return false;
1926
1927 const_iterator __itx(__x_n), __itx_end(__x_n_end);
1928 const_iterator __ity(__y_n);
1929 if (!std::is_permutation(__itx, __itx_end, __ity))
1930 return false;
1931
1932 __x_n = __x_n_end;
1933 }
1934 return true;
1935 }
1936
1937 /**
1938 * This type deals with all allocation and keeps an allocator instance
1939 * through inheritance to benefit from EBO when possible.
1940 */
1941 template<typename _NodeAlloc>
1942 struct _Hashtable_alloc : private _Hashtable_ebo_helper<0, _NodeAlloc>
1943 {
1944 private:
1945 using __ebo_node_alloc = _Hashtable_ebo_helper<0, _NodeAlloc>;
1946
1947 template<typename>
1948 struct __get_value_type;
1949 template<typename _Val, bool _Cache_hash_code>
1950 struct __get_value_type<_Hash_node<_Val, _Cache_hash_code>>
1951 { using type = _Val; };
1952
1953 public:
1954 using __node_type = typename _NodeAlloc::value_type;
1955 using __node_alloc_type = _NodeAlloc;
1956 // Use __gnu_cxx to benefit from _S_always_equal and al.
1957 using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
1958
1959 using __value_alloc_traits = typename __node_alloc_traits::template
1960 rebind_traits<typename __get_value_type<__node_type>::type>;
1961
1962 using __node_ptr = __node_type*;
1963 using __node_base = _Hash_node_base;
1964 using __node_base_ptr = __node_base*;
1965 using __buckets_alloc_type =
1966 __alloc_rebind<__node_alloc_type, __node_base_ptr>;
1967 using __buckets_alloc_traits = std::allocator_traits<__buckets_alloc_type>;
1968 using __buckets_ptr = __node_base_ptr*;
1969
1970 _Hashtable_alloc() = default;
1971 _Hashtable_alloc(const _Hashtable_alloc&) = default;
1972 _Hashtable_alloc(_Hashtable_alloc&&) = default;
1973
1974 template<typename _Alloc>
1975 _Hashtable_alloc(_Alloc&& __a)
1976 : __ebo_node_alloc(std::forward<_Alloc>(__a))
1977 { }
1978
1979 __node_alloc_type&
1980 _M_node_allocator()
1981 { return __ebo_node_alloc::_M_get(); }
1982
1983 const __node_alloc_type&
1984 _M_node_allocator() const
1985 { return __ebo_node_alloc::_M_cget(); }
1986
1987 // Allocate a node and construct an element within it.
1988 template<typename... _Args>
1989 __node_ptr
1990 _M_allocate_node(_Args&&... __args);
1991
1992 // Destroy the element within a node and deallocate the node.
1993 void
1994 _M_deallocate_node(__node_ptr __n);
1995
1996 // Deallocate a node.
1997 void
1998 _M_deallocate_node_ptr(__node_ptr __n);
1999
2000 // Deallocate the linked list of nodes pointed to by __n.
2001 // The elements within the nodes are destroyed.
2002 void
2003 _M_deallocate_nodes(__node_ptr __n);
2004
2005 __buckets_ptr
2006 _M_allocate_buckets(std::size_t __bkt_count);
2007
2008 void
2009 _M_deallocate_buckets(__buckets_ptr, std::size_t __bkt_count);
2010 };
2011
2012 // Definitions of class template _Hashtable_alloc's out-of-line member
2013 // functions.
2014 template<typename _NodeAlloc>
2015 template<typename... _Args>
2016 auto
2017 _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
2018 -> __node_ptr
2019 {
2020 auto& __alloc = _M_node_allocator();
2021 auto __nptr = __node_alloc_traits::allocate(__alloc, 1);
2022 __node_ptr __n = std::__to_address(__nptr);
2023 __try
2024 {
2025 ::new ((void*)__n) __node_type;
2026 __node_alloc_traits::construct(__alloc, __n->_M_valptr(),
2027 std::forward<_Args>(__args)...);
2028 return __n;
2029 }
2030 __catch(...)
2031 {
2032 __n->~__node_type();
2033 __node_alloc_traits::deallocate(__alloc, __nptr, 1);
2034 __throw_exception_again;
2035 }
2036 }
2037
2038 template<typename _NodeAlloc>
2039 void
2040 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n)
2041 {
2042 __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
2043 _M_deallocate_node_ptr(__n);
2044 }
2045
2046 template<typename _NodeAlloc>
2047 void
2048 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n)
2049 {
2050 typedef typename __node_alloc_traits::pointer _Ptr;
2051 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
2052 __n->~__node_type();
2053 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
2054 }
2055
2056 template<typename _NodeAlloc>
2057 void
2058 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n)
2059 {
2060 while (__n)
2061 {
2062 __node_ptr __tmp = __n;
2063 __n = __n->_M_next();
2064 _M_deallocate_node(__tmp);
2065 }
2066 }
2067
2068 template<typename _NodeAlloc>
2069 auto
2070 _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(std::size_t __bkt_count)
2071 -> __buckets_ptr
2072 {
2073 __buckets_alloc_type __alloc(_M_node_allocator());
2074
2075 auto __ptr = __buckets_alloc_traits::allocate(__alloc, __bkt_count);
2076 __buckets_ptr __p = std::__to_address(__ptr);
2077 __builtin_memset(__p, 0, __bkt_count * sizeof(__node_base_ptr));
2078 return __p;
2079 }
2080
2081 template<typename _NodeAlloc>
2082 void
2083 _Hashtable_alloc<_NodeAlloc>::
2084 _M_deallocate_buckets(__buckets_ptr __bkts,
2085 std::size_t __bkt_count)
2086 {
2087 typedef typename __buckets_alloc_traits::pointer _Ptr;
2088 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts);
2089 __buckets_alloc_type __alloc(_M_node_allocator());
2090 __buckets_alloc_traits::deallocate(__alloc, __ptr, __bkt_count);
2091 }
2092
2093 ///@} hashtable-detail
2094} // namespace __detail
2095/// @endcond
2096_GLIBCXX_END_NAMESPACE_VERSION
2097} // namespace std
2098
2099#endif // _HASHTABLE_POLICY_H
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
Definition complex:400
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs of the same type are equal iff their members are equal.
constexpr tuple< _Elements &&... > forward_as_tuple(_Elements &&... __args) noexcept
Create a tuple of lvalue or rvalue references to the arguments.
Definition tuple:2649
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:137
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:51
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:71
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
constexpr _Iterator __base(_Iterator __it)
Primary class template, tuple.
Definition tuple:834
Define a member typedef type only if a boolean constant is true.
Definition type_traits:129
is_empty
Definition type_traits:914
is_constructible
Definition type_traits:1116
Uniform interface to all allocator types.
Definition memoryfwd.h:76
Traits class for iterators.
Uniform interface to all pointer-like types.
Definition ptr_traits.h:178
Struct holding two objects of arbitrary type.
Definition stl_pair.h:286
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Uniform interface to C++98 and C++11 allocators.