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