]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/hashtable_policy.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / hashtable_policy.h
CommitLineData
3b2524b1
PC
1// Internal policy header for unordered_set and unordered_map -*- C++ -*-
2
a945c346 3// Copyright (C) 2010-2024 Free Software Foundation, Inc.
3b2524b1
PC
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.
7c3e9502 27 * Do not attempt to use it directly.
f910786b 28 * @headername{unordered_map,unordered_set}
3b2524b1
PC
29 */
30
31#ifndef _HASHTABLE_POLICY_H
32#define _HASHTABLE_POLICY_H 1
33
02a2c630 34#include <tuple> // for std::tuple, std::forward_as_tuple
692643c3 35#include <bits/functional_hash.h> // for __is_fast_hash
44c85722 36#include <bits/stl_algobase.h> // for std::min, std::is_permutation.
692643c3 37#include <bits/stl_pair.h> // for std::pair
a4438054
NS
38#include <ext/aligned_buffer.h> // for __gnu_cxx::__aligned_buffer
39#include <ext/alloc_traits.h> // for std::__alloc_rebind
9af65c2b 40#include <ext/numeric_traits.h> // for __gnu_cxx::__int_traits
732eb076 41
12ffa228
BK
42namespace std _GLIBCXX_VISIBILITY(default)
43{
63dc7fbe 44_GLIBCXX_BEGIN_NAMESPACE_VERSION
6667d5fe 45/// @cond undocumented
63dc7fbe 46
4dad8b49
BK
47 template<typename _Key, typename _Value, typename _Alloc,
48 typename _ExtractKey, typename _Equal,
4797a61c 49 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49
BK
50 typename _RehashPolicy, typename _Traits>
51 class _Hashtable;
52
3b2524b1
PC
53namespace __detail
54{
4dad8b49
BK
55 /**
56 * @defgroup hashtable-detail Base and Implementation Classes
57 * @ingroup unordered_associative_containers
58 * @{
59 */
4797a61c
FD
60 template<typename _Key, typename _Value, typename _ExtractKey,
61 typename _Equal, typename _Hash, typename _RangeHash,
62 typename _Unused, typename _Traits>
4dad8b49
BK
63 struct _Hashtable_base;
64
3b2524b1 65 // Helper function: return distance(first, last) for forward
0f146257 66 // iterators, or 0/1 for input iterators.
f4b4ce15 67 template<typename _Iterator>
3b2524b1
PC
68 inline typename std::iterator_traits<_Iterator>::difference_type
69 __distance_fw(_Iterator __first, _Iterator __last,
70 std::input_iterator_tag)
0f146257 71 { return __first != __last ? 1 : 0; }
3b2524b1 72
f4b4ce15 73 template<typename _Iterator>
3b2524b1
PC
74 inline typename std::iterator_traits<_Iterator>::difference_type
75 __distance_fw(_Iterator __first, _Iterator __last,
76 std::forward_iterator_tag)
77 { return std::distance(__first, __last); }
78
f4b4ce15 79 template<typename _Iterator>
3b2524b1
PC
80 inline typename std::iterator_traits<_Iterator>::difference_type
81 __distance_fw(_Iterator __first, _Iterator __last)
0f146257
FD
82 { return __distance_fw(__first, __last,
83 std::__iterator_category(__first)); }
3b2524b1 84
5ac4e73a
PC
85 struct _Identity
86 {
87 template<typename _Tp>
88 _Tp&&
4797a61c 89 operator()(_Tp&& __x) const noexcept
5ac4e73a
PC
90 { return std::forward<_Tp>(__x); }
91 };
92
93 struct _Select1st
94 {
d87105d6
JW
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&; };
5ac4e73a 109
2c43f5ec 110 template<typename _Tp>
d87105d6 111 typename __1st_type<_Tp>::type&&
2c43f5ec 112 operator()(_Tp&& __x) const noexcept
d87105d6 113 { return std::forward<_Tp>(__x).first; }
2c43f5ec
FD
114 };
115
dc9b92fa
FD
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
2c43f5ec
FD
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)
5476c914 159 -> typename _NodeGenerator::__node_ptr
2c43f5ec 160 {
d87105d6
JW
161 return __node_gen(std::forward<_Kt>(__k),
162 std::forward<_Arg>(__arg).second);
2c43f5ec
FD
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)
5476c914 172 -> typename _NodeGenerator::__node_ptr
2c43f5ec
FD
173 { return __node_gen(std::forward<_Kt>(__k)); }
174 };
175
04d8a476
FD
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
b09bcf83
FD
189 template<typename _NodeAlloc>
190 struct _Hashtable_alloc;
191
b3abc9d8
FD
192 // Functor recycling a pool of nodes and using allocation once the pool is
193 // empty.
b09bcf83 194 template<typename _NodeAlloc>
b3abc9d8
FD
195 struct _ReuseOrAllocNode
196 {
197 private:
b09bcf83
FD
198 using __node_alloc_type = _NodeAlloc;
199 using __hashtable_alloc = _Hashtable_alloc<__node_alloc_type>;
b09bcf83
FD
200 using __node_alloc_traits =
201 typename __hashtable_alloc::__node_alloc_traits;
b3abc9d8
FD
202
203 public:
5476c914 204 using __node_ptr = typename __hashtable_alloc::__node_ptr;
d87105d6 205
5476c914 206 _ReuseOrAllocNode(__node_ptr __nodes, __hashtable_alloc& __h)
7cfe71d1 207 : _M_nodes(__nodes), _M_h(__h) { }
b3abc9d8
FD
208 _ReuseOrAllocNode(const _ReuseOrAllocNode&) = delete;
209
210 ~_ReuseOrAllocNode()
211 { _M_h._M_deallocate_nodes(_M_nodes); }
212
2c43f5ec 213 template<typename... _Args>
5476c914 214 __node_ptr
2c43f5ec 215 operator()(_Args&&... __args) const
b3abc9d8 216 {
04d8a476
FD
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;
b3abc9d8
FD
230 }
231
232 private:
5476c914 233 mutable __node_ptr _M_nodes;
b09bcf83 234 __hashtable_alloc& _M_h;
b3abc9d8
FD
235 };
236
1bb59e05
FD
237 // Functor similar to the previous one but without any pool of nodes to
238 // recycle.
b09bcf83 239 template<typename _NodeAlloc>
b3abc9d8
FD
240 struct _AllocNode
241 {
242 private:
b09bcf83 243 using __hashtable_alloc = _Hashtable_alloc<_NodeAlloc>;
b3abc9d8
FD
244
245 public:
5476c914 246 using __node_ptr = typename __hashtable_alloc::__node_ptr;
d87105d6 247
b09bcf83 248 _AllocNode(__hashtable_alloc& __h)
7cfe71d1 249 : _M_h(__h) { }
b3abc9d8 250
2c43f5ec 251 template<typename... _Args>
5476c914 252 __node_ptr
2c43f5ec
FD
253 operator()(_Args&&... __args) const
254 { return _M_h._M_allocate_node(std::forward<_Args>(__args)...); }
b3abc9d8
FD
255
256 private:
b09bcf83 257 __hashtable_alloc& _M_h;
b3abc9d8
FD
258 };
259
4dad8b49 260 // Auxiliary types used for all instantiations of _Hashtable nodes
3b2524b1 261 // and iterators.
7c3e9502 262
4dad8b49
BK
263 /**
264 * struct _Hashtable_traits
265 *
266 * Important traits for hash tables.
267 *
207585a6 268 * @tparam _Cache_hash_code Boolean value. True if the value of
4dad8b49
BK
269 * the hash function is stored along with the value. This is a
270 * time-space tradeoff. Storing it may improve lookup speed by
7cfe71d1
FD
271 * reducing the number of times we need to call the _Hash or _Equal
272 * functors.
4dad8b49 273 *
207585a6 274 * @tparam _Constant_iterators Boolean value. True if iterator and
4dad8b49
BK
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 *
207585a6 279 * @tparam _Unique_keys Boolean value. True if the return value
4dad8b49 280 * of _Hashtable::count(k) is always at most one, false if it may
207585a6 281 * be an arbitrary number. This is true for unordered_set and
4dad8b49
BK
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 {
4dad8b49
BK
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
e3ef832a
FD
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
4dad8b49
BK
307 /**
308 * struct _Hash_node_base
309 *
af6204cc
BK
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.
4dad8b49 314 */
f86b266c
FD
315 struct _Hash_node_base
316 {
317 _Hash_node_base* _M_nxt;
318
34e6625a 319 _Hash_node_base() noexcept : _M_nxt() { }
4dad8b49 320
34e6625a 321 _Hash_node_base(_Hash_node_base* __next) noexcept : _M_nxt(__next) { }
f86b266c
FD
322 };
323
0462b6aa
FD
324 /**
325 * struct _Hash_node_value_base
326 *
327 * Node type with the value to store.
328 */
329 template<typename _Value>
1b6f0476 330 struct _Hash_node_value_base
0462b6aa 331 {
b09bcf83
FD
332 typedef _Value value_type;
333
0462b6aa
FD
334 __gnu_cxx::__aligned_buffer<_Value> _M_storage;
335
2c1e3544 336 [[__gnu__::__always_inline__]]
0462b6aa
FD
337 _Value*
338 _M_valptr() noexcept
339 { return _M_storage._M_ptr(); }
340
2c1e3544 341 [[__gnu__::__always_inline__]]
0462b6aa
FD
342 const _Value*
343 _M_valptr() const noexcept
344 { return _M_storage._M_ptr(); }
345
2c1e3544 346 [[__gnu__::__always_inline__]]
0462b6aa
FD
347 _Value&
348 _M_v() noexcept
349 { return *_M_valptr(); }
350
2c1e3544 351 [[__gnu__::__always_inline__]]
0462b6aa
FD
352 const _Value&
353 _M_v() const noexcept
354 { return *_M_valptr(); }
355 };
356
4dad8b49 357 /**
1b6f0476 358 * Primary template struct _Hash_node_code_cache.
4dad8b49 359 */
1b6f0476
FD
360 template<bool _Cache_hash_code>
361 struct _Hash_node_code_cache
362 { };
3b2524b1 363
af6204cc 364 /**
1b6f0476 365 * Specialization for node with cache, struct _Hash_node_code_cache.
af6204cc 366 */
1b6f0476
FD
367 template<>
368 struct _Hash_node_code_cache<true>
369 { std::size_t _M_hash_code; };
3b2524b1 370
1b6f0476
FD
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 { };
3b2524b1 376
af6204cc 377 /**
1b6f0476 378 * Primary template struct _Hash_node.
af6204cc 379 */
1b6f0476
FD
380 template<typename _Value, bool _Cache_hash_code>
381 struct _Hash_node
382 : _Hash_node_base
383 , _Hash_node_value<_Value, _Cache_hash_code>
3b2524b1 384 {
4dad8b49 385 _Hash_node*
34e6625a
MG
386 _M_next() const noexcept
387 { return static_cast<_Hash_node*>(this->_M_nxt); }
3b2524b1
PC
388 };
389
4dad8b49
BK
390 /// Base class for node iterators.
391 template<typename _Value, bool _Cache_hash_code>
3b2524b1
PC
392 struct _Node_iterator_base
393 {
5b3be7cf 394 using __node_type = _Hash_node<_Value, _Cache_hash_code>;
4dad8b49 395
1b6f0476 396 __node_type* _M_cur;
4dad8b49 397
05a30af3 398 _Node_iterator_base() : _M_cur(nullptr) { }
34e6625a 399 _Node_iterator_base(__node_type* __p) noexcept
3b2524b1 400 : _M_cur(__p) { }
7c3e9502 401
3b2524b1 402 void
34e6625a 403 _M_incr() noexcept
f86b266c 404 { _M_cur = _M_cur->_M_next(); }
3b2524b1 405
acc1d1a9
FD
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 };
3b2524b1 418
4dad8b49 419 /// Node iterators, used to iterate through all the hashtable.
3b2524b1
PC
420 template<typename _Value, bool __constant_iterators, bool __cache>
421 struct _Node_iterator
422 : public _Node_iterator_base<_Value, __cache>
423 {
4dad8b49
BK
424 private:
425 using __base_type = _Node_iterator_base<_Value, __cache>;
426 using __node_type = typename __base_type::__node_type;
427
428 public:
a09bb4a8
JW
429 using value_type = _Value;
430 using difference_type = std::ptrdiff_t;
431 using iterator_category = std::forward_iterator_tag;
3b2524b1 432
a09bb4a8
JW
433 using pointer = __conditional_t<__constant_iterators,
434 const value_type*, value_type*>;
4dad8b49 435
a09bb4a8
JW
436 using reference = __conditional_t<__constant_iterators,
437 const value_type&, value_type&>;
4dad8b49 438
05a30af3 439 _Node_iterator() = default;
3b2524b1
PC
440
441 explicit
34e6625a 442 _Node_iterator(__node_type* __p) noexcept
4dad8b49 443 : __base_type(__p) { }
3b2524b1
PC
444
445 reference
34e6625a 446 operator*() const noexcept
0462b6aa 447 { return this->_M_cur->_M_v(); }
7c3e9502 448
3b2524b1 449 pointer
34e6625a 450 operator->() const noexcept
0462b6aa 451 { return this->_M_cur->_M_valptr(); }
3b2524b1
PC
452
453 _Node_iterator&
34e6625a 454 operator++() noexcept
7c3e9502 455 {
3b2524b1 456 this->_M_incr();
7c3e9502 457 return *this;
3b2524b1 458 }
7c3e9502 459
3b2524b1 460 _Node_iterator
34e6625a 461 operator++(int) noexcept
7c3e9502 462 {
3b2524b1
PC
463 _Node_iterator __tmp(*this);
464 this->_M_incr();
465 return __tmp;
466 }
467 };
468
4dad8b49 469 /// Node const_iterators, used to iterate through all the hashtable.
3b2524b1
PC
470 template<typename _Value, bool __constant_iterators, bool __cache>
471 struct _Node_const_iterator
472 : public _Node_iterator_base<_Value, __cache>
473 {
5b3be7cf 474 private:
4dad8b49
BK
475 using __base_type = _Node_iterator_base<_Value, __cache>;
476 using __node_type = typename __base_type::__node_type;
477
478 public:
b09bcf83
FD
479 typedef _Value value_type;
480 typedef std::ptrdiff_t difference_type;
481 typedef std::forward_iterator_tag iterator_category;
3b2524b1 482
1b6f0476
FD
483 typedef const value_type* pointer;
484 typedef const value_type& reference;
4dad8b49 485
05a30af3 486 _Node_const_iterator() = default;
3b2524b1
PC
487
488 explicit
34e6625a 489 _Node_const_iterator(__node_type* __p) noexcept
4dad8b49 490 : __base_type(__p) { }
3b2524b1
PC
491
492 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
34e6625a 493 __cache>& __x) noexcept
4dad8b49 494 : __base_type(__x._M_cur) { }
3b2524b1
PC
495
496 reference
34e6625a 497 operator*() const noexcept
0462b6aa 498 { return this->_M_cur->_M_v(); }
7c3e9502 499
3b2524b1 500 pointer
34e6625a 501 operator->() const noexcept
0462b6aa 502 { return this->_M_cur->_M_valptr(); }
3b2524b1
PC
503
504 _Node_const_iterator&
34e6625a 505 operator++() noexcept
7c3e9502 506 {
3b2524b1 507 this->_M_incr();
7c3e9502 508 return *this;
3b2524b1 509 }
7c3e9502 510
3b2524b1 511 _Node_const_iterator
34e6625a 512 operator++(int) noexcept
7c3e9502 513 {
3b2524b1
PC
514 _Node_const_iterator __tmp(*this);
515 this->_M_incr();
516 return __tmp;
517 }
518 };
519
3b2524b1
PC
520 // Many of class template _Hashtable's template parameters are policy
521 // classes. These are defaults for the policies.
522
4dad8b49
BK
523 /// Default range hashing function: use division to fold a large number
524 /// into the range [0, N).
3b2524b1
PC
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
0462b6aa
FD
532 operator()(first_argument_type __num,
533 second_argument_type __den) const noexcept
3b2524b1
PC
534 { return __num % __den; }
535 };
536
4dad8b49
BK
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.
3b2524b1
PC
542 struct _Default_ranged_hash { };
543
4dad8b49
BK
544 /// Default value for rehash policy. Bucket size is (usually) the
545 /// smallest prime that keeps the load factor small enough.
3b2524b1
PC
546 struct _Prime_rehash_policy
547 {
7cfe71d1 548 using __has_load_factor = true_type;
732eb076 549
da27f556 550 _Prime_rehash_policy(float __z = 1.0) noexcept
d4a7f7a1 551 : _M_max_load_factor(__z), _M_next_resize(0) { }
3b2524b1
PC
552
553 float
d3677132 554 max_load_factor() const noexcept
7c3e9502 555 { return _M_max_load_factor; }
3b2524b1
PC
556
557 // Return a bucket size no smaller than n.
558 std::size_t
559 _M_next_bkt(std::size_t __n) const;
7c3e9502 560
3b2524b1
PC
561 // Return a bucket count appropriate for n elements
562 std::size_t
6e147946 563 _M_bkt_for_elements(std::size_t __n) const
943cc2a1 564 { return __builtin_ceil(__n / (double)_M_max_load_factor); }
7c3e9502 565
3b2524b1
PC
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).
570 std::pair<bool, std::size_t>
571 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
572 std::size_t __n_ins) const;
573
d4a7f7a1 574 typedef std::size_t _State;
da29608a
FD
575
576 _State
577 _M_state() const
d4a7f7a1 578 { return _M_next_resize; }
da29608a 579
0462b6aa
FD
580 void
581 _M_reset() noexcept
582 { _M_next_resize = 0; }
583
da29608a 584 void
d4a7f7a1
FD
585 _M_reset(_State __state)
586 { _M_next_resize = __state; }
da29608a 587
78aa145d
FD
588 static const std::size_t _S_growth_factor = 2;
589
b09bcf83
FD
590 float _M_max_load_factor;
591 mutable std::size_t _M_next_resize;
3b2524b1
PC
592 };
593
732eb076
FD
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
4957b284 607 /// Compute closest power of 2 not less than __n
732eb076 608 inline std::size_t
d34d36ef 609 __clp2(std::size_t __n) noexcept
732eb076 610 {
9af65c2b 611 using __gnu_cxx::__int_traits;
9866abe3 612 // Equivalent to return __n ? std::bit_ceil(__n) : 0;
4957b284
JW
613 if (__n < 2)
614 return __n;
2fb17d2d
JW
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.
9af65c2b 619 return (size_t(1) << (__int_traits<size_t>::__digits - __lz - 1)) << 1;
732eb076
FD
620 }
621
622 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
623 /// operations.
624 struct _Power2_rehash_policy
625 {
7cfe71d1 626 using __has_load_factor = true_type;
732eb076
FD
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
834f4c43 638 _M_next_bkt(std::size_t __n) noexcept
732eb076 639 {
de6f5f57
FD
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
834f4c43
JW
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);
732eb076
FD
648 std::size_t __res = __clp2(__n);
649
732eb076
FD
650 if (__res == 0)
651 __res = __max_bkt;
de6f5f57
FD
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;
732eb076
FD
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.
9af65c2b 662 _M_next_resize = size_t(-1);
732eb076
FD
663 else
664 _M_next_resize
943cc2a1 665 = __builtin_floor(__res * (double)_M_max_load_factor);
732eb076
FD
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
943cc2a1 673 { return __builtin_ceil(__n / (double)_M_max_load_factor); }
732eb076
FD
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).
679 std::pair<bool, std::size_t>
680 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
834f4c43 681 std::size_t __n_ins) noexcept
732eb076 682 {
de6f5f57 683 if (__n_elt + __n_ins > _M_next_resize)
732eb076 684 {
de6f5f57
FD
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.
a1343e5c 688 double __min_bkts
de6f5f57 689 = std::max<std::size_t>(__n_elt + __n_ins, _M_next_resize ? 0 : 11)
a1343e5c 690 / (double)_M_max_load_factor;
732eb076 691 if (__min_bkts >= __n_bkt)
de6f5f57 692 return { true,
943cc2a1 693 _M_next_bkt(std::max<std::size_t>(__builtin_floor(__min_bkts) + 1,
de6f5f57 694 __n_bkt * _S_growth_factor)) };
732eb076
FD
695
696 _M_next_resize
943cc2a1 697 = __builtin_floor(__n_bkt * (double)_M_max_load_factor);
de6f5f57 698 return { false, 0 };
732eb076
FD
699 }
700 else
de6f5f57 701 return { false, 0 };
732eb076
FD
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
834f4c43
JW
720 float _M_max_load_factor;
721 std::size_t _M_next_resize;
732eb076
FD
722 };
723
c4cf9aa2
FD
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
5dc22714 743 // Base classes for std::_Hashtable. We define these base classes
4dad8b49
BK
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
5dc22714
PC
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
4dad8b49
BK
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 *
d87105d6
JW
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
4dad8b49
BK
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,
4797a61c 763 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49
BK
764 typename _RehashPolicy, typename _Traits,
765 bool _Unique_keys = _Traits::__unique_keys::value>
3b2524b1 766 struct _Map_base { };
5dc22714 767
d87105d6
JW
768 /// Partial specialization, __unique_keys set to false, std::pair value type.
769 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
4797a61c 770 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 771 typename _RehashPolicy, typename _Traits>
d87105d6 772 struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
4797a61c 773 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
3b2524b1 774 {
d87105d6 775 using mapped_type = _Val;
3b2524b1
PC
776 };
777
4dad8b49 778 /// Partial specialization, __unique_keys set to true.
d87105d6 779 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
4797a61c 780 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 781 typename _RehashPolicy, typename _Traits>
d87105d6 782 struct _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
4797a61c 783 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
3b2524b1 784 {
4dad8b49 785 private:
d87105d6
JW
786 using __hashtable_base = _Hashtable_base<_Key, pair<const _Key, _Val>,
787 _Select1st, _Equal, _Hash,
788 _RangeHash, _Unused,
4797a61c 789 _Traits>;
4dad8b49 790
d87105d6
JW
791 using __hashtable = _Hashtable<_Key, pair<const _Key, _Val>, _Alloc,
792 _Select1st, _Equal, _Hash, _RangeHash,
4797a61c 793 _Unused, _RehashPolicy, _Traits>;
4dad8b49
BK
794
795 using __hash_code = typename __hashtable_base::__hash_code;
4dad8b49
BK
796
797 public:
798 using key_type = typename __hashtable_base::key_type;
d87105d6 799 using mapped_type = _Val;
5dc22714 800
3b2524b1 801 mapped_type&
4dad8b49 802 operator[](const key_type& __k);
3b2524b1 803
fb7342fd 804 mapped_type&
4dad8b49 805 operator[](key_type&& __k);
fb7342fd 806
3b2524b1
PC
807 // _GLIBCXX_RESOLVE_LIB_DEFECTS
808 // DR 761. unordered_map needs an at() member function.
809 mapped_type&
d87105d6
JW
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 }
3b2524b1
PC
817
818 const mapped_type&
d87105d6
JW
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 }
3b2524b1
PC
826 };
827
d87105d6 828 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
4797a61c 829 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 830 typename _RehashPolicy, typename _Traits>
612b4711 831 auto
d87105d6 832 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
4797a61c 833 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
4dad8b49 834 operator[](const key_type& __k)
612b4711 835 -> mapped_type&
3b2524b1 836 {
4dad8b49
BK
837 __hashtable* __h = static_cast<__hashtable*>(this);
838 __hash_code __code = __h->_M_hash_code(__k);
4797a61c 839 std::size_t __bkt = __h->_M_bucket_index(__code);
1b6f0476 840 if (auto __node = __h->_M_find_node(__bkt, __k, __code))
b0c849fa
FD
841 return __node->_M_v().second;
842
843 typename __hashtable::_Scoped_node __node {
844 __h,
845 std::piecewise_construct,
846 std::tuple<const key_type&>(__k),
847 std::tuple<>()
848 };
849 auto __pos
4797a61c 850 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
b0c849fa
FD
851 __node._M_node = nullptr;
852 return __pos->second;
3b2524b1
PC
853 }
854
d87105d6 855 template<typename _Key, typename _Val, typename _Alloc, typename _Equal,
4797a61c 856 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 857 typename _RehashPolicy, typename _Traits>
612b4711 858 auto
d87105d6 859 _Map_base<_Key, pair<const _Key, _Val>, _Alloc, _Select1st, _Equal,
4797a61c 860 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
4dad8b49 861 operator[](key_type&& __k)
612b4711 862 -> mapped_type&
fb7342fd 863 {
4dad8b49
BK
864 __hashtable* __h = static_cast<__hashtable*>(this);
865 __hash_code __code = __h->_M_hash_code(__k);
4797a61c 866 std::size_t __bkt = __h->_M_bucket_index(__code);
1b6f0476 867 if (auto __node = __h->_M_find_node(__bkt, __k, __code))
b0c849fa
FD
868 return __node->_M_v().second;
869
870 typename __hashtable::_Scoped_node __node {
871 __h,
872 std::piecewise_construct,
873 std::forward_as_tuple(std::move(__k)),
874 std::tuple<>()
875 };
876 auto __pos
4797a61c 877 = __h->_M_insert_unique_node(__bkt, __code, __node._M_node);
b0c849fa
FD
878 __node._M_node = nullptr;
879 return __pos->second;
fb7342fd
PC
880 }
881
084680db
JW
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
4dad8b49
BK
893 /**
894 * Primary class template _Insert_base.
895 *
272b2ce4 896 * Defines @c insert member functions appropriate to all _Hashtables.
4dad8b49
BK
897 */
898 template<typename _Key, typename _Value, typename _Alloc,
899 typename _ExtractKey, typename _Equal,
4797a61c 900 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49
BK
901 typename _RehashPolicy, typename _Traits>
902 struct _Insert_base
903 {
b3abc9d8 904 protected:
4dad8b49 905 using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
4797a61c
FD
906 _Equal, _Hash, _RangeHash,
907 _Unused, _Traits>;
908
909 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
910 _Hash, _RangeHash,
911 _Unused, _RehashPolicy, _Traits>;
4dad8b49 912
1b6f0476
FD
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
4dad8b49 920 using value_type = typename __hashtable_base::value_type;
4dad8b49
BK
921 using size_type = typename __hashtable_base::size_type;
922
1b6f0476
FD
923 using __unique_keys = typename _Traits::__unique_keys;
924 using __node_alloc_type = typename __hashtable_alloc::__node_alloc_type;
b09bcf83 925 using __node_gen_type = _AllocNode<__node_alloc_type>;
4dad8b49
BK
926
927 __hashtable&
928 _M_conjure_hashtable()
929 { return *(static_cast<__hashtable*>(this)); }
930
b3abc9d8
FD
931 template<typename _InputIterator, typename _NodeGetter>
932 void
933 _M_insert_range(_InputIterator __first, _InputIterator __last,
6dcf0423 934 const _NodeGetter&, true_type __uks);
0f146257
FD
935
936 template<typename _InputIterator, typename _NodeGetter>
937 void
938 _M_insert_range(_InputIterator __first, _InputIterator __last,
6dcf0423 939 const _NodeGetter&, false_type __uks);
b3abc9d8
FD
940
941 public:
1b6f0476
FD
942 using iterator = _Node_iterator<_Value, __constant_iterators::value,
943 __hash_cached::value>;
944
a09bb4a8
JW
945 using const_iterator = _Node_const_iterator<_Value,
946 __constant_iterators::value,
1b6f0476
FD
947 __hash_cached::value>;
948
a09bb4a8
JW
949 using __ireturn_type = __conditional_t<__unique_keys::value,
950 std::pair<iterator, bool>,
951 iterator>;
1b6f0476 952
4dad8b49
BK
953 __ireturn_type
954 insert(const value_type& __v)
955 {
956 __hashtable& __h = _M_conjure_hashtable();
b3abc9d8 957 __node_gen_type __node_gen(__h);
6dcf0423 958 return __h._M_insert(__v, __node_gen, __unique_keys{});
4dad8b49
BK
959 }
960
961 iterator
41349aec
FD
962 insert(const_iterator __hint, const value_type& __v)
963 {
964 __hashtable& __h = _M_conjure_hashtable();
b3abc9d8 965 __node_gen_type __node_gen(__h);
6dcf0423 966 return __h._M_insert(__hint, __v, __node_gen, __unique_keys{});
41349aec 967 }
3b2524b1 968
7688e5e8
FD
969 template<typename _KType, typename... _Args>
970 std::pair<iterator, bool>
971 try_emplace(const_iterator, _KType&& __k, _Args&&... __args)
972 {
973 __hashtable& __h = _M_conjure_hashtable();
974 auto __code = __h._M_hash_code(__k);
4797a61c 975 std::size_t __bkt = __h._M_bucket_index(__code);
1b6f0476 976 if (auto __node = __h._M_find_node(__bkt, __k, __code))
7688e5e8
FD
977 return { iterator(__node), false };
978
979 typename __hashtable::_Scoped_node __node {
980 &__h,
981 std::piecewise_construct,
982 std::forward_as_tuple(std::forward<_KType>(__k)),
983 std::forward_as_tuple(std::forward<_Args>(__args)...)
984 };
985 auto __it
4797a61c 986 = __h._M_insert_unique_node(__bkt, __code, __node._M_node);
7688e5e8
FD
987 __node._M_node = nullptr;
988 return { __it, true };
989 }
990
4dad8b49
BK
991 void
992 insert(initializer_list<value_type> __l)
993 { this->insert(__l.begin(), __l.end()); }
994
995 template<typename _InputIterator>
996 void
b3abc9d8
FD
997 insert(_InputIterator __first, _InputIterator __last)
998 {
999 __hashtable& __h = _M_conjure_hashtable();
1000 __node_gen_type __node_gen(__h);
6dcf0423 1001 return _M_insert_range(__first, __last, __node_gen, __unique_keys{});
b3abc9d8 1002 }
4dad8b49
BK
1003 };
1004
1005 template<typename _Key, typename _Value, typename _Alloc,
1006 typename _ExtractKey, typename _Equal,
4797a61c 1007 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 1008 typename _RehashPolicy, typename _Traits>
b3abc9d8 1009 template<typename _InputIterator, typename _NodeGetter>
4dad8b49 1010 void
4797a61c
FD
1011 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1012 _Hash, _RangeHash, _Unused,
1013 _RehashPolicy, _Traits>::
b3abc9d8 1014 _M_insert_range(_InputIterator __first, _InputIterator __last,
6dcf0423 1015 const _NodeGetter& __node_gen, true_type __uks)
0f146257 1016 {
0f146257
FD
1017 __hashtable& __h = _M_conjure_hashtable();
1018 for (; __first != __last; ++__first)
6dcf0423 1019 __h._M_insert(*__first, __node_gen, __uks);
0f146257
FD
1020 }
1021
1022 template<typename _Key, typename _Value, typename _Alloc,
1023 typename _ExtractKey, typename _Equal,
4797a61c 1024 typename _Hash, typename _RangeHash, typename _Unused,
0f146257
FD
1025 typename _RehashPolicy, typename _Traits>
1026 template<typename _InputIterator, typename _NodeGetter>
1027 void
4797a61c
FD
1028 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1029 _Hash, _RangeHash, _Unused,
1030 _RehashPolicy, _Traits>::
0f146257 1031 _M_insert_range(_InputIterator __first, _InputIterator __last,
6dcf0423 1032 const _NodeGetter& __node_gen, false_type __uks)
4dad8b49 1033 {
c4cf9aa2
FD
1034 using __rehash_guard_t = typename __hashtable::__rehash_guard_t;
1035 using __pair_type = std::pair<bool, std::size_t>;
4dad8b49
BK
1036
1037 size_type __n_elt = __detail::__distance_fw(__first, __last);
0f146257
FD
1038 if (__n_elt == 0)
1039 return;
4dad8b49
BK
1040
1041 __hashtable& __h = _M_conjure_hashtable();
c4cf9aa2
FD
1042 __rehash_guard_t __rehash_guard(__h._M_rehash_policy);
1043 __pair_type __do_rehash
1044 = __h._M_rehash_policy._M_need_rehash(__h._M_bucket_count,
1045 __h._M_element_count,
1046 __n_elt);
4dad8b49
BK
1047
1048 if (__do_rehash.first)
c4cf9aa2 1049 __h._M_rehash(__do_rehash.second, __uks);
4dad8b49 1050
c4cf9aa2 1051 __rehash_guard._M_guarded_obj = nullptr;
4dad8b49 1052 for (; __first != __last; ++__first)
6dcf0423 1053 __h._M_insert(*__first, __node_gen, __uks);
4dad8b49
BK
1054 }
1055
1056 /**
1057 * Primary class template _Insert.
1058 *
272b2ce4
JW
1059 * Defines @c insert member functions that depend on _Hashtable policies,
1060 * via partial specializations.
4dad8b49
BK
1061 */
1062 template<typename _Key, typename _Value, typename _Alloc,
1063 typename _ExtractKey, typename _Equal,
4797a61c 1064 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 1065 typename _RehashPolicy, typename _Traits,
732eb076 1066 bool _Constant_iterators = _Traits::__constant_iterators::value>
4dad8b49
BK
1067 struct _Insert;
1068
1069 /// Specialization.
1070 template<typename _Key, typename _Value, typename _Alloc,
1071 typename _ExtractKey, typename _Equal,
4797a61c 1072 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 1073 typename _RehashPolicy, typename _Traits>
4797a61c
FD
1074 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1075 _Hash, _RangeHash, _Unused,
732eb076 1076 _RehashPolicy, _Traits, true>
4dad8b49 1077 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1078 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>
4dad8b49
BK
1079 {
1080 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
4797a61c
FD
1081 _Equal, _Hash, _RangeHash, _Unused,
1082 _RehashPolicy, _Traits>;
4dad8b49 1083
4dad8b49
BK
1084 using value_type = typename __base_type::value_type;
1085 using iterator = typename __base_type::iterator;
1086 using const_iterator = typename __base_type::const_iterator;
1b6f0476 1087 using __ireturn_type = typename __base_type::__ireturn_type;
4dad8b49
BK
1088
1089 using __unique_keys = typename __base_type::__unique_keys;
1090 using __hashtable = typename __base_type::__hashtable;
b3abc9d8 1091 using __node_gen_type = typename __base_type::__node_gen_type;
4dad8b49
BK
1092
1093 using __base_type::insert;
1094
732eb076 1095 __ireturn_type
4dad8b49
BK
1096 insert(value_type&& __v)
1097 {
1098 __hashtable& __h = this->_M_conjure_hashtable();
b3abc9d8 1099 __node_gen_type __node_gen(__h);
6dcf0423 1100 return __h._M_insert(std::move(__v), __node_gen, __unique_keys{});
4dad8b49
BK
1101 }
1102
1103 iterator
41349aec
FD
1104 insert(const_iterator __hint, value_type&& __v)
1105 {
1106 __hashtable& __h = this->_M_conjure_hashtable();
b3abc9d8
FD
1107 __node_gen_type __node_gen(__h);
1108 return __h._M_insert(__hint, std::move(__v), __node_gen,
6dcf0423 1109 __unique_keys{});
41349aec
FD
1110 }
1111 };
4dad8b49
BK
1112
1113 /// Specialization.
1114 template<typename _Key, typename _Value, typename _Alloc,
1115 typename _ExtractKey, typename _Equal,
4797a61c 1116 typename _Hash, typename _RangeHash, typename _Unused,
732eb076 1117 typename _RehashPolicy, typename _Traits>
4797a61c
FD
1118 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1119 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
4dad8b49 1120 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1121 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>
3b2524b1 1122 {
4dad8b49 1123 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
4797a61c 1124 _Equal, _Hash, _RangeHash, _Unused,
4dad8b49
BK
1125 _RehashPolicy, _Traits>;
1126 using value_type = typename __base_type::value_type;
1127 using iterator = typename __base_type::iterator;
1128 using const_iterator = typename __base_type::const_iterator;
1129
1130 using __unique_keys = typename __base_type::__unique_keys;
1131 using __hashtable = typename __base_type::__hashtable;
1132 using __ireturn_type = typename __base_type::__ireturn_type;
4dad8b49
BK
1133
1134 using __base_type::insert;
1135
1136 template<typename _Pair>
57cee56a 1137 using __is_cons = std::is_constructible<value_type, _Pair&&>;
4dad8b49
BK
1138
1139 template<typename _Pair>
57cee56a 1140 using _IFcons = std::enable_if<__is_cons<_Pair>::value>;
4dad8b49
BK
1141
1142 template<typename _Pair>
57cee56a 1143 using _IFconsp = typename _IFcons<_Pair>::type;
4dad8b49 1144
57cee56a 1145 template<typename _Pair, typename = _IFconsp<_Pair>>
4dad8b49
BK
1146 __ireturn_type
1147 insert(_Pair&& __v)
1148 {
1149 __hashtable& __h = this->_M_conjure_hashtable();
6dcf0423 1150 return __h._M_emplace(__unique_keys{}, std::forward<_Pair>(__v));
4dad8b49
BK
1151 }
1152
57cee56a 1153 template<typename _Pair, typename = _IFconsp<_Pair>>
4dad8b49 1154 iterator
41349aec
FD
1155 insert(const_iterator __hint, _Pair&& __v)
1156 {
1157 __hashtable& __h = this->_M_conjure_hashtable();
6dcf0423 1158 return __h._M_emplace(__hint, __unique_keys{},
9f285ccb 1159 std::forward<_Pair>(__v));
41349aec 1160 }
4dad8b49
BK
1161 };
1162
732eb076
FD
1163 template<typename _Policy>
1164 using __has_load_factor = typename _Policy::__has_load_factor;
1165
4dad8b49
BK
1166 /**
1167 * Primary class template _Rehash_base.
1168 *
1169 * Give hashtable the max_load_factor functions and reserve iff the
732eb076 1170 * rehash policy supports it.
4dad8b49
BK
1171 */
1172 template<typename _Key, typename _Value, typename _Alloc,
1173 typename _ExtractKey, typename _Equal,
4797a61c 1174 typename _Hash, typename _RangeHash, typename _Unused,
732eb076
FD
1175 typename _RehashPolicy, typename _Traits,
1176 typename =
7cfe71d1 1177 __detected_or_t<false_type, __has_load_factor, _RehashPolicy>>
4dad8b49
BK
1178 struct _Rehash_base;
1179
732eb076 1180 /// Specialization when rehash policy doesn't provide load factor management.
4dad8b49
BK
1181 template<typename _Key, typename _Value, typename _Alloc,
1182 typename _ExtractKey, typename _Equal,
4797a61c 1183 typename _Hash, typename _RangeHash, typename _Unused,
732eb076 1184 typename _RehashPolicy, typename _Traits>
4dad8b49 1185 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1186 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
1187 false_type /* Has load factor */>
732eb076
FD
1188 {
1189 };
1190
1191 /// Specialization when rehash policy provide load factor management.
1192 template<typename _Key, typename _Value, typename _Alloc,
1193 typename _ExtractKey, typename _Equal,
4797a61c 1194 typename _Hash, typename _RangeHash, typename _Unused,
732eb076
FD
1195 typename _RehashPolicy, typename _Traits>
1196 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1197 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits,
1198 true_type /* Has load factor */>
4dad8b49 1199 {
e3ef832a 1200 private:
4dad8b49 1201 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
4797a61c 1202 _Equal, _Hash, _RangeHash, _Unused,
732eb076 1203 _RehashPolicy, _Traits>;
4dad8b49 1204
e3ef832a 1205 public:
3b2524b1 1206 float
d3677132 1207 max_load_factor() const noexcept
3b2524b1 1208 {
4dad8b49 1209 const __hashtable* __this = static_cast<const __hashtable*>(this);
3b2524b1
PC
1210 return __this->__rehash_policy().max_load_factor();
1211 }
1212
1213 void
1214 max_load_factor(float __z)
1215 {
4dad8b49 1216 __hashtable* __this = static_cast<__hashtable*>(this);
732eb076 1217 __this->__rehash_policy(_RehashPolicy(__z));
3b2524b1 1218 }
9155c0e3
PC
1219
1220 void
1221 reserve(std::size_t __n)
1222 {
4dad8b49 1223 __hashtable* __this = static_cast<__hashtable*>(this);
de6f5f57 1224 __this->rehash(__this->__rehash_policy()._M_bkt_for_elements(__n));
9155c0e3 1225 }
3b2524b1
PC
1226 };
1227
4dad8b49
BK
1228 /**
1229 * Primary class template _Hashtable_ebo_helper.
1230 *
d872e4aa
JW
1231 * Helper class using EBO when it is not forbidden (the type is not
1232 * final) and when it is worth it (the type is empty.)
4dad8b49 1233 */
cc74ac5d 1234 template<int _Nm, typename _Tp,
a188284c 1235 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
346afd84 1236 struct _Hashtable_ebo_helper;
a188284c 1237
4dad8b49 1238 /// Specialization using EBO.
cc74ac5d 1239 template<int _Nm, typename _Tp>
970aa0b9 1240 struct _Hashtable_ebo_helper<_Nm, _Tp, true>
a13ab2bc 1241 : private _Tp
a188284c 1242 {
f8f0193b 1243 _Hashtable_ebo_helper() noexcept(noexcept(_Tp())) : _Tp() { }
4dad8b49 1244
b09bcf83
FD
1245 template<typename _OtherTp>
1246 _Hashtable_ebo_helper(_OtherTp&& __tp)
7cfe71d1 1247 : _Tp(std::forward<_OtherTp>(__tp))
b09bcf83 1248 { }
a188284c 1249
1f6ea968
JW
1250 const _Tp& _M_cget() const { return static_cast<const _Tp&>(*this); }
1251 _Tp& _M_get() { return static_cast<_Tp&>(*this); }
a188284c
FD
1252 };
1253
4dad8b49 1254 /// Specialization not using EBO.
cc74ac5d 1255 template<int _Nm, typename _Tp>
346afd84 1256 struct _Hashtable_ebo_helper<_Nm, _Tp, false>
a188284c 1257 {
346afd84 1258 _Hashtable_ebo_helper() = default;
4dad8b49 1259
b09bcf83
FD
1260 template<typename _OtherTp>
1261 _Hashtable_ebo_helper(_OtherTp&& __tp)
7cfe71d1 1262 : _M_tp(std::forward<_OtherTp>(__tp))
b09bcf83 1263 { }
a188284c 1264
1f6ea968
JW
1265 const _Tp& _M_cget() const { return _M_tp; }
1266 _Tp& _M_get() { return _M_tp; }
a188284c
FD
1267
1268 private:
f8f0193b 1269 _Tp _M_tp{};
a188284c
FD
1270 };
1271
5b3be7cf
FD
1272 /**
1273 * Primary class template _Local_iterator_base.
1274 *
1275 * Base class for local iterators, used to iterate within a bucket
1276 * but not between buckets.
1277 */
1278 template<typename _Key, typename _Value, typename _ExtractKey,
4797a61c 1279 typename _Hash, typename _RangeHash, typename _Unused,
5b3be7cf
FD
1280 bool __cache_hash_code>
1281 struct _Local_iterator_base;
1282
4dad8b49
BK
1283 /**
1284 * Primary class template _Hash_code_base.
1285 *
1286 * Encapsulates two policy issues that aren't quite orthogonal.
1287 * (1) the difference between using a ranged hash function and using
1288 * the combination of a hash function and a range-hashing function.
1289 * In the former case we don't have such things as hash codes, so
1290 * we have a dummy type as placeholder.
1291 * (2) Whether or not we cache hash codes. Caching hash codes is
1292 * meaningless if we have a ranged hash function.
1293 *
1294 * We also put the key extraction objects here, for convenience.
1295 * Each specialization derives from one or more of the template
1296 * parameters to benefit from Ebo. This is important as this type
1297 * is inherited in some cases by the _Local_iterator_base type used
1298 * to implement local_iterator and const_local_iterator. As with
1299 * any iterator type we prefer to make it as small as possible.
4dad8b49 1300 */
a188284c 1301 template<typename _Key, typename _Value, typename _ExtractKey,
4797a61c 1302 typename _Hash, typename _RangeHash, typename _Unused,
3b2524b1 1303 bool __cache_hash_code>
1b6f0476 1304 struct _Hash_code_base
9f9c0549 1305 : private _Hashtable_ebo_helper<1, _Hash>
3b2524b1 1306 {
a188284c 1307 private:
9f9c0549 1308 using __ebo_hash = _Hashtable_ebo_helper<1, _Hash>;
a188284c 1309
92e16228 1310 // Gives the local iterator implementation access to _M_bucket_index().
4797a61c
FD
1311 friend struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1312 _Hash, _RangeHash, _Unused, false>;
92e16228 1313
a188284c 1314 public:
4797a61c 1315 typedef _Hash hasher;
3b2524b1
PC
1316
1317 hasher
1318 hash_function() const
4797a61c 1319 { return _M_hash(); }
3b2524b1 1320
5b3be7cf 1321 protected:
4dad8b49 1322 typedef std::size_t __hash_code;
4dad8b49 1323
da27f556
FD
1324 // We need the default constructor for the local iterators and _Hashtable
1325 // default constructor.
d9a3647a 1326 _Hash_code_base() = default;
f8f0193b 1327
4797a61c 1328 _Hash_code_base(const _Hash& __hash) : __ebo_hash(__hash) { }
3b2524b1 1329
4dad8b49 1330 __hash_code
3b2524b1 1331 _M_hash_code(const _Key& __k) const
e625ccc2 1332 {
4797a61c 1333 static_assert(__is_invocable<const _Hash&, const _Key&>{},
e625ccc2 1334 "hash function must be invocable with an argument of key type");
4797a61c 1335 return _M_hash()(__k);
e625ccc2 1336 }
7c3e9502 1337
d2b1a684
FD
1338 template<typename _Kt>
1339 __hash_code
1340 _M_hash_code_tr(const _Kt& __k) const
1341 {
1342 static_assert(__is_invocable<const _Hash&, const _Kt&>{},
1343 "hash function must be invocable with an argument of key type");
1344 return _M_hash()(__k);
1345 }
1346
e3ef832a
FD
1347 __hash_code
1348 _M_hash_code(const _Hash_node_value<_Value, false>& __n) const
1349 { return _M_hash_code(_ExtractKey{}(__n._M_v())); }
1350
1351 __hash_code
1352 _M_hash_code(const _Hash_node_value<_Value, true>& __n) const
1353 { return __n._M_hash_code; }
1354
3b2524b1 1355 std::size_t
4797a61c
FD
1356 _M_bucket_index(__hash_code __c, std::size_t __bkt_count) const
1357 { return _RangeHash{}(__c, __bkt_count); }
3b2524b1
PC
1358
1359 std::size_t
1b6f0476
FD
1360 _M_bucket_index(const _Hash_node_value<_Value, false>& __n,
1361 std::size_t __bkt_count) const
4797a61c
FD
1362 noexcept( noexcept(declval<const _Hash&>()(declval<const _Key&>()))
1363 && noexcept(declval<const _RangeHash&>()((__hash_code)0,
1364 (std::size_t)0)) )
1365 {
1b6f0476 1366 return _RangeHash{}(_M_hash_code(_ExtractKey{}(__n._M_v())),
4797a61c
FD
1367 __bkt_count);
1368 }
3b2524b1 1369
1b6f0476
FD
1370 std::size_t
1371 _M_bucket_index(const _Hash_node_value<_Value, true>& __n,
1372 std::size_t __bkt_count) const
1373 noexcept( noexcept(declval<const _RangeHash&>()((__hash_code)0,
1374 (std::size_t)0)) )
1375 { return _RangeHash{}(__n._M_hash_code, __bkt_count); }
3b2524b1
PC
1376
1377 void
1b6f0476 1378 _M_store_code(_Hash_node_code_cache<false>&, __hash_code) const
3b2524b1
PC
1379 { }
1380
1381 void
1b6f0476
FD
1382 _M_copy_code(_Hash_node_code_cache<false>&,
1383 const _Hash_node_code_cache<false>&) const
1384 { }
3b2524b1
PC
1385
1386 void
1b6f0476
FD
1387 _M_store_code(_Hash_node_code_cache<true>& __n, __hash_code __c) const
1388 { __n._M_hash_code = __c; }
3b2524b1
PC
1389
1390 void
1b6f0476
FD
1391 _M_copy_code(_Hash_node_code_cache<true>& __to,
1392 const _Hash_node_code_cache<true>& __from) const
1393 { __to._M_hash_code = __from._M_hash_code; }
3b2524b1
PC
1394
1395 void
1396 _M_swap(_Hash_code_base& __x)
4797a61c 1397 { std::swap(__ebo_hash::_M_get(), __x.__ebo_hash::_M_get()); }
4dad8b49 1398
4797a61c
FD
1399 const _Hash&
1400 _M_hash() const { return __ebo_hash::_M_cget(); }
a188284c
FD
1401 };
1402
92e16228 1403 /// Partial specialization used when nodes contain a cached hash code.
a188284c 1404 template<typename _Key, typename _Value, typename _ExtractKey,
4797a61c 1405 typename _Hash, typename _RangeHash, typename _Unused>
a188284c 1406 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
4797a61c
FD
1407 _Hash, _RangeHash, _Unused, true>
1408 : public _Node_iterator_base<_Value, true>
a188284c 1409 {
5b3be7cf 1410 protected:
acc1d1a9 1411 using __base_node_iter = _Node_iterator_base<_Value, true>;
5b3be7cf 1412 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
4797a61c 1413 _Hash, _RangeHash, _Unused, true>;
5b3be7cf 1414
a188284c 1415 _Local_iterator_base() = default;
4797a61c 1416 _Local_iterator_base(const __hash_code_base&,
5b3be7cf 1417 _Hash_node<_Value, true>* __p,
a188284c 1418 std::size_t __bkt, std::size_t __bkt_count)
4797a61c
FD
1419 : __base_node_iter(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
1420 { }
a188284c
FD
1421
1422 void
1423 _M_incr()
1424 {
acc1d1a9
FD
1425 __base_node_iter::_M_incr();
1426 if (this->_M_cur)
a188284c 1427 {
5b3be7cf 1428 std::size_t __bkt
4797a61c 1429 = _RangeHash{}(this->_M_cur->_M_hash_code, _M_bucket_count);
a188284c 1430 if (__bkt != _M_bucket)
acc1d1a9 1431 this->_M_cur = nullptr;
a188284c
FD
1432 }
1433 }
1434
a188284c
FD
1435 std::size_t _M_bucket;
1436 std::size_t _M_bucket_count;
92e16228
JW
1437
1438 public:
92e16228
JW
1439 std::size_t
1440 _M_get_bucket() const { return _M_bucket; } // for debug mode
1441 };
1442
1443 // Uninitialized storage for a _Hash_code_base.
1444 // This type is DefaultConstructible and Assignable even if the
1445 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1446 // can be DefaultConstructible and Assignable.
1447 template<typename _Tp, bool _IsEmpty = std::is_empty<_Tp>::value>
1448 struct _Hash_code_storage
1449 {
1450 __gnu_cxx::__aligned_buffer<_Tp> _M_storage;
1451
1452 _Tp*
1453 _M_h() { return _M_storage._M_ptr(); }
1454
1455 const _Tp*
1456 _M_h() const { return _M_storage._M_ptr(); }
1457 };
1458
1459 // Empty partial specialization for empty _Hash_code_base types.
1460 template<typename _Tp>
1461 struct _Hash_code_storage<_Tp, true>
1462 {
1463 static_assert( std::is_empty<_Tp>::value, "Type must be empty" );
1464
1465 // As _Tp is an empty type there will be no bytes written/read through
1466 // the cast pointer, so no strict-aliasing violation.
1467 _Tp*
1468 _M_h() { return reinterpret_cast<_Tp*>(this); }
1469
1470 const _Tp*
1471 _M_h() const { return reinterpret_cast<const _Tp*>(this); }
a188284c
FD
1472 };
1473
92e16228 1474 template<typename _Key, typename _Value, typename _ExtractKey,
4797a61c 1475 typename _Hash, typename _RangeHash, typename _Unused>
92e16228
JW
1476 using __hash_code_for_local_iter
1477 = _Hash_code_storage<_Hash_code_base<_Key, _Value, _ExtractKey,
4797a61c 1478 _Hash, _RangeHash, _Unused, false>>;
92e16228
JW
1479
1480 // Partial specialization used when hash codes are not cached
a188284c 1481 template<typename _Key, typename _Value, typename _ExtractKey,
4797a61c 1482 typename _Hash, typename _RangeHash, typename _Unused>
a188284c 1483 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
4797a61c
FD
1484 _Hash, _RangeHash, _Unused, false>
1485 : __hash_code_for_local_iter<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1486 _Unused>
acc1d1a9 1487 , _Node_iterator_base<_Value, false>
a188284c 1488 {
5b3be7cf
FD
1489 protected:
1490 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
4797a61c 1491 _Hash, _RangeHash, _Unused, false>;
acc1d1a9 1492 using __node_iter_base = _Node_iterator_base<_Value, false>;
5b3be7cf 1493
92e16228
JW
1494 _Local_iterator_base() : _M_bucket_count(-1) { }
1495
5b3be7cf
FD
1496 _Local_iterator_base(const __hash_code_base& __base,
1497 _Hash_node<_Value, false>* __p,
a188284c 1498 std::size_t __bkt, std::size_t __bkt_count)
acc1d1a9 1499 : __node_iter_base(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count)
92e16228
JW
1500 { _M_init(__base); }
1501
1502 ~_Local_iterator_base()
1503 {
eb6b71b8 1504 if (_M_bucket_count != size_t(-1))
92e16228
JW
1505 _M_destroy();
1506 }
1507
1508 _Local_iterator_base(const _Local_iterator_base& __iter)
1b6f0476 1509 : __node_iter_base(__iter._M_cur), _M_bucket(__iter._M_bucket)
acc1d1a9 1510 , _M_bucket_count(__iter._M_bucket_count)
92e16228 1511 {
eb6b71b8 1512 if (_M_bucket_count != size_t(-1))
92e16228
JW
1513 _M_init(*__iter._M_h());
1514 }
1515
1516 _Local_iterator_base&
1517 operator=(const _Local_iterator_base& __iter)
1518 {
1519 if (_M_bucket_count != -1)
1520 _M_destroy();
acc1d1a9 1521 this->_M_cur = __iter._M_cur;
92e16228
JW
1522 _M_bucket = __iter._M_bucket;
1523 _M_bucket_count = __iter._M_bucket_count;
1524 if (_M_bucket_count != -1)
1525 _M_init(*__iter._M_h());
1526 return *this;
1527 }
a188284c
FD
1528
1529 void
1530 _M_incr()
1531 {
acc1d1a9
FD
1532 __node_iter_base::_M_incr();
1533 if (this->_M_cur)
a188284c 1534 {
1b6f0476 1535 std::size_t __bkt = this->_M_h()->_M_bucket_index(*this->_M_cur,
92e16228 1536 _M_bucket_count);
a188284c 1537 if (__bkt != _M_bucket)
acc1d1a9 1538 this->_M_cur = nullptr;
a188284c
FD
1539 }
1540 }
1541
a188284c
FD
1542 std::size_t _M_bucket;
1543 std::size_t _M_bucket_count;
92e16228
JW
1544
1545 void
1546 _M_init(const __hash_code_base& __base)
1547 { ::new(this->_M_h()) __hash_code_base(__base); }
1548
1549 void
1550 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1551
1552 public:
92e16228
JW
1553 std::size_t
1554 _M_get_bucket() const { return _M_bucket; } // for debug mode
a188284c
FD
1555 };
1556
4dad8b49 1557 /// local iterators
a188284c 1558 template<typename _Key, typename _Value, typename _ExtractKey,
4797a61c 1559 typename _Hash, typename _RangeHash, typename _Unused,
a188284c
FD
1560 bool __constant_iterators, bool __cache>
1561 struct _Local_iterator
1562 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
4797a61c 1563 _Hash, _RangeHash, _Unused, __cache>
a188284c 1564 {
5b3be7cf
FD
1565 private:
1566 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
4797a61c 1567 _Hash, _RangeHash, _Unused, __cache>;
5b3be7cf 1568 using __hash_code_base = typename __base_type::__hash_code_base;
acc1d1a9 1569
5b3be7cf 1570 public:
a09bb4a8
JW
1571 using value_type = _Value;
1572 using pointer = __conditional_t<__constant_iterators,
1573 const value_type*, value_type*>;
1574 using reference = __conditional_t<__constant_iterators,
1575 const value_type&, value_type&>;
1576 using difference_type = ptrdiff_t;
1577 using iterator_category = forward_iterator_tag;
a188284c
FD
1578
1579 _Local_iterator() = default;
1580
5b3be7cf 1581 _Local_iterator(const __hash_code_base& __base,
7cfe71d1 1582 _Hash_node<_Value, __cache>* __n,
a188284c 1583 std::size_t __bkt, std::size_t __bkt_count)
7cfe71d1 1584 : __base_type(__base, __n, __bkt, __bkt_count)
a188284c
FD
1585 { }
1586
1587 reference
1588 operator*() const
0462b6aa 1589 { return this->_M_cur->_M_v(); }
a188284c
FD
1590
1591 pointer
1592 operator->() const
0462b6aa 1593 { return this->_M_cur->_M_valptr(); }
a188284c
FD
1594
1595 _Local_iterator&
1596 operator++()
1597 {
1598 this->_M_incr();
1599 return *this;
1600 }
1601
1602 _Local_iterator
1603 operator++(int)
1604 {
1605 _Local_iterator __tmp(*this);
1606 this->_M_incr();
1607 return __tmp;
1608 }
1609 };
1610
4dad8b49 1611 /// local const_iterators
a188284c 1612 template<typename _Key, typename _Value, typename _ExtractKey,
4797a61c 1613 typename _Hash, typename _RangeHash, typename _Unused,
a188284c
FD
1614 bool __constant_iterators, bool __cache>
1615 struct _Local_const_iterator
1616 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
4797a61c 1617 _Hash, _RangeHash, _Unused, __cache>
a188284c 1618 {
5b3be7cf
FD
1619 private:
1620 using __base_type = _Local_iterator_base<_Key, _Value, _ExtractKey,
4797a61c 1621 _Hash, _RangeHash, _Unused, __cache>;
5b3be7cf
FD
1622 using __hash_code_base = typename __base_type::__hash_code_base;
1623
1624 public:
b09bcf83 1625 typedef _Value value_type;
1b6f0476
FD
1626 typedef const value_type* pointer;
1627 typedef const value_type& reference;
b09bcf83
FD
1628 typedef std::ptrdiff_t difference_type;
1629 typedef std::forward_iterator_tag iterator_category;
a188284c
FD
1630
1631 _Local_const_iterator() = default;
1632
5b3be7cf 1633 _Local_const_iterator(const __hash_code_base& __base,
7cfe71d1 1634 _Hash_node<_Value, __cache>* __n,
a188284c 1635 std::size_t __bkt, std::size_t __bkt_count)
7cfe71d1 1636 : __base_type(__base, __n, __bkt, __bkt_count)
a188284c
FD
1637 { }
1638
1639 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
4797a61c 1640 _Hash, _RangeHash, _Unused,
a188284c
FD
1641 __constant_iterators,
1642 __cache>& __x)
7cfe71d1 1643 : __base_type(__x)
a188284c
FD
1644 { }
1645
1646 reference
1647 operator*() const
0462b6aa 1648 { return this->_M_cur->_M_v(); }
a188284c
FD
1649
1650 pointer
1651 operator->() const
0462b6aa 1652 { return this->_M_cur->_M_valptr(); }
a188284c
FD
1653
1654 _Local_const_iterator&
1655 operator++()
1656 {
1657 this->_M_incr();
1658 return *this;
1659 }
1660
1661 _Local_const_iterator
1662 operator++(int)
1663 {
1664 _Local_const_iterator __tmp(*this);
1665 this->_M_incr();
1666 return __tmp;
1667 }
3b2524b1 1668 };
5dc22714 1669
4dad8b49
BK
1670 /**
1671 * Primary class template _Hashtable_base.
1672 *
af6204cc
BK
1673 * Helper class adding management of _Equal functor to
1674 * _Hash_code_base type.
1675 *
1676 * Base class templates are:
1677 * - __detail::_Hash_code_base
1678 * - __detail::_Hashtable_ebo_helper
4dad8b49 1679 */
4797a61c
FD
1680 template<typename _Key, typename _Value, typename _ExtractKey,
1681 typename _Equal, typename _Hash, typename _RangeHash,
1682 typename _Unused, typename _Traits>
1683 struct _Hashtable_base
1684 : public _Hash_code_base<_Key, _Value, _ExtractKey, _Hash, _RangeHash,
1685 _Unused, _Traits::__hash_cached::value>,
1b6f0476
FD
1686 private _Hashtable_ebo_helper<0, _Equal>
1687 {
1688 public:
1689 typedef _Key key_type;
1690 typedef _Value value_type;
1691 typedef _Equal key_equal;
1692 typedef std::size_t size_type;
1693 typedef std::ptrdiff_t difference_type;
eba20412 1694
1b6f0476
FD
1695 using __traits_type = _Traits;
1696 using __hash_cached = typename __traits_type::__hash_cached;
f9d98fa7 1697
1b6f0476
FD
1698 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1699 _Hash, _RangeHash, _Unused,
1700 __hash_cached::value>;
eba20412 1701
1b6f0476 1702 using __hash_code = typename __hash_code_base::__hash_code;
4dad8b49 1703
1b6f0476
FD
1704 private:
1705 using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
4dad8b49 1706
1b6f0476
FD
1707 static bool
1708 _S_equals(__hash_code, const _Hash_node_code_cache<false>&)
1709 { return true; }
1710
1711 static bool
1712 _S_node_equals(const _Hash_node_code_cache<false>&,
1713 const _Hash_node_code_cache<false>&)
1714 { return true; }
1715
1716 static bool
1717 _S_equals(__hash_code __c, const _Hash_node_code_cache<true>& __n)
1718 { return __c == __n._M_hash_code; }
1719
1720 static bool
1721 _S_node_equals(const _Hash_node_code_cache<true>& __lhn,
1722 const _Hash_node_code_cache<true>& __rhn)
1723 { return __lhn._M_hash_code == __rhn._M_hash_code; }
1724
1725 protected:
1726 _Hashtable_base() = default;
f8f0193b 1727
1b6f0476
FD
1728 _Hashtable_base(const _Hash& __hash, const _Equal& __eq)
1729 : __hash_code_base(__hash), _EqualEBO(__eq)
1730 { }
1731
1732 bool
e3ef832a
FD
1733 _M_key_equals(const _Key& __k,
1734 const _Hash_node_value<_Value,
1735 __hash_cached::value>& __n) const
1b6f0476
FD
1736 {
1737 static_assert(__is_invocable<const _Equal&, const _Key&, const _Key&>{},
e625ccc2
JW
1738 "key equality predicate must be invocable with two arguments of "
1739 "key type");
e3ef832a 1740 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
1b6f0476 1741 }
4dad8b49 1742
d2b1a684
FD
1743 template<typename _Kt>
1744 bool
e3ef832a
FD
1745 _M_key_equals_tr(const _Kt& __k,
1746 const _Hash_node_value<_Value,
1747 __hash_cached::value>& __n) const
d2b1a684
FD
1748 {
1749 static_assert(
1750 __is_invocable<const _Equal&, const _Kt&, const _Key&>{},
1751 "key equality predicate must be invocable with two arguments of "
1752 "key type");
e3ef832a 1753 return _M_eq()(__k, _ExtractKey{}(__n._M_v()));
d2b1a684
FD
1754 }
1755
e3ef832a
FD
1756 bool
1757 _M_equals(const _Key& __k, __hash_code __c,
1758 const _Hash_node_value<_Value, __hash_cached::value>& __n) const
1759 { return _S_equals(__c, __n) && _M_key_equals(__k, __n); }
1760
1761 template<typename _Kt>
1762 bool
1763 _M_equals_tr(const _Kt& __k, __hash_code __c,
1764 const _Hash_node_value<_Value,
1765 __hash_cached::value>& __n) const
1766 { return _S_equals(__c, __n) && _M_key_equals_tr(__k, __n); }
1767
1b6f0476
FD
1768 bool
1769 _M_node_equals(
1770 const _Hash_node_value<_Value, __hash_cached::value>& __lhn,
1771 const _Hash_node_value<_Value, __hash_cached::value>& __rhn) const
1772 {
1773 return _S_node_equals(__lhn, __rhn)
e3ef832a 1774 && _M_key_equals(_ExtractKey{}(__lhn._M_v()), __rhn);
1b6f0476 1775 }
f9d98fa7 1776
1b6f0476
FD
1777 void
1778 _M_swap(_Hashtable_base& __x)
1779 {
1780 __hash_code_base::_M_swap(__x);
1781 std::swap(_EqualEBO::_M_get(), __x._EqualEBO::_M_get());
1782 }
4dad8b49 1783
1b6f0476
FD
1784 const _Equal&
1785 _M_eq() const { return _EqualEBO::_M_cget(); }
1786 };
5dc22714 1787
4dad8b49
BK
1788 /**
1789 * Primary class template _Equality.
1790 *
1791 * This is for implementing equality comparison for unordered
1792 * containers, per N3068, by John Lakos and Pablo Halpern.
1793 * Algorithmically, we follow closely the reference implementations
1794 * therein.
1795 */
1796 template<typename _Key, typename _Value, typename _Alloc,
1797 typename _ExtractKey, typename _Equal,
4797a61c 1798 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49
BK
1799 typename _RehashPolicy, typename _Traits,
1800 bool _Unique_keys = _Traits::__unique_keys::value>
1801 struct _Equality;
1802
d9165389 1803 /// unordered_map and unordered_set specializations.
4dad8b49
BK
1804 template<typename _Key, typename _Value, typename _Alloc,
1805 typename _ExtractKey, typename _Equal,
4797a61c 1806 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49
BK
1807 typename _RehashPolicy, typename _Traits>
1808 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1809 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>
4dad8b49
BK
1810 {
1811 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1812 _Hash, _RangeHash, _Unused,
1813 _RehashPolicy, _Traits>;
4dad8b49
BK
1814
1815 bool
1816 _M_equal(const __hashtable&) const;
5dc22714
PC
1817 };
1818
4dad8b49
BK
1819 template<typename _Key, typename _Value, typename _Alloc,
1820 typename _ExtractKey, typename _Equal,
4797a61c 1821 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 1822 typename _RehashPolicy, typename _Traits>
5dc22714 1823 bool
4dad8b49 1824 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1825 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::
4dad8b49 1826 _M_equal(const __hashtable& __other) const
5dc22714 1827 {
5476c914 1828 using __node_ptr = typename __hashtable::__node_ptr;
4dad8b49 1829 const __hashtable* __this = static_cast<const __hashtable*>(this);
5dc22714
PC
1830 if (__this->size() != __other.size())
1831 return false;
1832
5476c914 1833 for (auto __x_n = __this->_M_begin(); __x_n; __x_n = __x_n->_M_next())
5dc22714 1834 {
5476c914 1835 std::size_t __ybkt = __other._M_bucket_index(*__x_n);
1b6f0476 1836 auto __prev_n = __other._M_buckets[__ybkt];
d9165389 1837 if (!__prev_n)
5dc22714 1838 return false;
d9165389 1839
5476c914 1840 for (__node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);;
d9165389
FD
1841 __n = __n->_M_next())
1842 {
5476c914 1843 if (__n->_M_v() == __x_n->_M_v())
d9165389
FD
1844 break;
1845
1846 if (!__n->_M_nxt
1b6f0476 1847 || __other._M_bucket_index(*__n->_M_next()) != __ybkt)
d9165389
FD
1848 return false;
1849 }
5dc22714 1850 }
d9165389 1851
5dc22714
PC
1852 return true;
1853 }
1854
d9165389 1855 /// unordered_multiset and unordered_multimap specializations.
4dad8b49
BK
1856 template<typename _Key, typename _Value, typename _Alloc,
1857 typename _ExtractKey, typename _Equal,
4797a61c 1858 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49
BK
1859 typename _RehashPolicy, typename _Traits>
1860 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1861 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>
5dc22714 1862 {
4dad8b49 1863 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1864 _Hash, _RangeHash, _Unused,
1865 _RehashPolicy, _Traits>;
5dc22714 1866
5dc22714 1867 bool
4dad8b49
BK
1868 _M_equal(const __hashtable&) const;
1869 };
5dc22714 1870
4dad8b49
BK
1871 template<typename _Key, typename _Value, typename _Alloc,
1872 typename _ExtractKey, typename _Equal,
4797a61c 1873 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 1874 typename _RehashPolicy, typename _Traits>
5dc22714 1875 bool
4dad8b49 1876 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1877 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, false>::
4dad8b49 1878 _M_equal(const __hashtable& __other) const
5dc22714 1879 {
5476c914
FD
1880 using __node_ptr = typename __hashtable::__node_ptr;
1881 using const_iterator = typename __hashtable::const_iterator;
4dad8b49 1882 const __hashtable* __this = static_cast<const __hashtable*>(this);
5dc22714
PC
1883 if (__this->size() != __other.size())
1884 return false;
1885
5476c914 1886 for (auto __x_n = __this->_M_begin(); __x_n;)
5dc22714 1887 {
d9165389 1888 std::size_t __x_count = 1;
5476c914
FD
1889 auto __x_n_end = __x_n->_M_next();
1890 for (; __x_n_end
1891 && __this->key_eq()(_ExtractKey{}(__x_n->_M_v()),
1892 _ExtractKey{}(__x_n_end->_M_v()));
1893 __x_n_end = __x_n_end->_M_next())
d9165389
FD
1894 ++__x_count;
1895
5476c914 1896 std::size_t __ybkt = __other._M_bucket_index(*__x_n);
1b6f0476 1897 auto __y_prev_n = __other._M_buckets[__ybkt];
d9165389
FD
1898 if (!__y_prev_n)
1899 return false;
1900
5476c914 1901 __node_ptr __y_n = static_cast<__node_ptr>(__y_prev_n->_M_nxt);
f9d98fa7 1902 for (;;)
d9165389 1903 {
4797a61c 1904 if (__this->key_eq()(_ExtractKey{}(__y_n->_M_v()),
5476c914 1905 _ExtractKey{}(__x_n->_M_v())))
d9165389
FD
1906 break;
1907
1b6f0476 1908 auto __y_ref_n = __y_n;
f9d98fa7 1909 for (__y_n = __y_n->_M_next(); __y_n; __y_n = __y_n->_M_next())
1b6f0476 1910 if (!__other._M_node_equals(*__y_ref_n, *__y_n))
f9d98fa7
FD
1911 break;
1912
1b6f0476 1913 if (!__y_n || __other._M_bucket_index(*__y_n) != __ybkt)
d9165389
FD
1914 return false;
1915 }
1916
5476c914
FD
1917 auto __y_n_end = __y_n;
1918 for (; __y_n_end; __y_n_end = __y_n_end->_M_next())
d9165389
FD
1919 if (--__x_count == 0)
1920 break;
5dc22714 1921
d9165389 1922 if (__x_count != 0)
5dc22714
PC
1923 return false;
1924
5476c914
FD
1925 const_iterator __itx(__x_n), __itx_end(__x_n_end);
1926 const_iterator __ity(__y_n);
d9165389 1927 if (!std::is_permutation(__itx, __itx_end, __ity))
5dc22714
PC
1928 return false;
1929
5476c914 1930 __x_n = __x_n_end;
5dc22714
PC
1931 }
1932 return true;
1933 }
7c3e9502 1934
0dd49691 1935 /**
b0c849fa
FD
1936 * This type deals with all allocation and keeps an allocator instance
1937 * through inheritance to benefit from EBO when possible.
0dd49691
FD
1938 */
1939 template<typename _NodeAlloc>
b09bcf83 1940 struct _Hashtable_alloc : private _Hashtable_ebo_helper<0, _NodeAlloc>
0dd49691 1941 {
b09bcf83
FD
1942 private:
1943 using __ebo_node_alloc = _Hashtable_ebo_helper<0, _NodeAlloc>;
64acc43d
JW
1944
1945 template<typename>
1946 struct __get_value_type;
1947 template<typename _Val, bool _Cache_hash_code>
1948 struct __get_value_type<_Hash_node<_Val, _Cache_hash_code>>
1949 { using type = _Val; };
1950
b09bcf83
FD
1951 public:
1952 using __node_type = typename _NodeAlloc::value_type;
1953 using __node_alloc_type = _NodeAlloc;
1954 // Use __gnu_cxx to benefit from _S_always_equal and al.
1955 using __node_alloc_traits = __gnu_cxx::__alloc_traits<__node_alloc_type>;
1956
b74f0db1 1957 using __value_alloc_traits = typename __node_alloc_traits::template
64acc43d 1958 rebind_traits<typename __get_value_type<__node_type>::type>;
0dd49691 1959
1b6f0476
FD
1960 using __node_ptr = __node_type*;
1961 using __node_base = _Hash_node_base;
1962 using __node_base_ptr = __node_base*;
1963 using __buckets_alloc_type =
1964 __alloc_rebind<__node_alloc_type, __node_base_ptr>;
1965 using __buckets_alloc_traits = std::allocator_traits<__buckets_alloc_type>;
1966 using __buckets_ptr = __node_base_ptr*;
b09bcf83 1967
da27f556 1968 _Hashtable_alloc() = default;
b09bcf83
FD
1969 _Hashtable_alloc(const _Hashtable_alloc&) = default;
1970 _Hashtable_alloc(_Hashtable_alloc&&) = default;
0dd49691
FD
1971
1972 template<typename _Alloc>
b09bcf83 1973 _Hashtable_alloc(_Alloc&& __a)
7cfe71d1 1974 : __ebo_node_alloc(std::forward<_Alloc>(__a))
0dd49691 1975 { }
b09bcf83
FD
1976
1977 __node_alloc_type&
1978 _M_node_allocator()
1f6ea968 1979 { return __ebo_node_alloc::_M_get(); }
b09bcf83
FD
1980
1981 const __node_alloc_type&
1982 _M_node_allocator() const
1f6ea968 1983 { return __ebo_node_alloc::_M_cget(); }
b09bcf83 1984
b0c849fa 1985 // Allocate a node and construct an element within it.
b09bcf83 1986 template<typename... _Args>
1b6f0476 1987 __node_ptr
b09bcf83
FD
1988 _M_allocate_node(_Args&&... __args);
1989
b0c849fa 1990 // Destroy the element within a node and deallocate the node.
b09bcf83 1991 void
1b6f0476 1992 _M_deallocate_node(__node_ptr __n);
b09bcf83 1993
b0c849fa 1994 // Deallocate a node.
23d5fd6c 1995 void
1b6f0476 1996 _M_deallocate_node_ptr(__node_ptr __n);
23d5fd6c 1997
b0c849fa
FD
1998 // Deallocate the linked list of nodes pointed to by __n.
1999 // The elements within the nodes are destroyed.
b09bcf83 2000 void
1b6f0476 2001 _M_deallocate_nodes(__node_ptr __n);
b09bcf83 2002
1b6f0476 2003 __buckets_ptr
7cfe71d1 2004 _M_allocate_buckets(std::size_t __bkt_count);
b09bcf83
FD
2005
2006 void
1b6f0476 2007 _M_deallocate_buckets(__buckets_ptr, std::size_t __bkt_count);
0dd49691
FD
2008 };
2009
b09bcf83
FD
2010 // Definitions of class template _Hashtable_alloc's out-of-line member
2011 // functions.
2012 template<typename _NodeAlloc>
2013 template<typename... _Args>
7cfe71d1 2014 auto
b09bcf83 2015 _Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&&... __args)
1b6f0476 2016 -> __node_ptr
b09bcf83 2017 {
8f2a59c2
FD
2018 auto& __alloc = _M_node_allocator();
2019 auto __nptr = __node_alloc_traits::allocate(__alloc, 1);
1b6f0476 2020 __node_ptr __n = std::__to_address(__nptr);
b09bcf83
FD
2021 __try
2022 {
ff90a89e 2023 ::new ((void*)__n) __node_type;
8f2a59c2 2024 __node_alloc_traits::construct(__alloc, __n->_M_valptr(),
b74f0db1 2025 std::forward<_Args>(__args)...);
b09bcf83
FD
2026 return __n;
2027 }
2028 __catch(...)
2029 {
8f2a59c2
FD
2030 __n->~__node_type();
2031 __node_alloc_traits::deallocate(__alloc, __nptr, 1);
b09bcf83
FD
2032 __throw_exception_again;
2033 }
2034 }
2035
2036 template<typename _NodeAlloc>
2037 void
1b6f0476 2038 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node(__node_ptr __n)
23d5fd6c
FD
2039 {
2040 __node_alloc_traits::destroy(_M_node_allocator(), __n->_M_valptr());
2041 _M_deallocate_node_ptr(__n);
2042 }
2043
2044 template<typename _NodeAlloc>
2045 void
1b6f0476 2046 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_node_ptr(__node_ptr __n)
b09bcf83
FD
2047 {
2048 typedef typename __node_alloc_traits::pointer _Ptr;
2049 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__n);
b09bcf83
FD
2050 __n->~__node_type();
2051 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr, 1);
2052 }
2053
2054 template<typename _NodeAlloc>
2055 void
1b6f0476 2056 _Hashtable_alloc<_NodeAlloc>::_M_deallocate_nodes(__node_ptr __n)
b09bcf83
FD
2057 {
2058 while (__n)
2059 {
1b6f0476 2060 __node_ptr __tmp = __n;
b09bcf83
FD
2061 __n = __n->_M_next();
2062 _M_deallocate_node(__tmp);
2063 }
2064 }
2065
2066 template<typename _NodeAlloc>
1b6f0476 2067 auto
7cfe71d1 2068 _Hashtable_alloc<_NodeAlloc>::_M_allocate_buckets(std::size_t __bkt_count)
1b6f0476 2069 -> __buckets_ptr
b09bcf83 2070 {
1b6f0476 2071 __buckets_alloc_type __alloc(_M_node_allocator());
b09bcf83 2072
1b6f0476
FD
2073 auto __ptr = __buckets_alloc_traits::allocate(__alloc, __bkt_count);
2074 __buckets_ptr __p = std::__to_address(__ptr);
2075 __builtin_memset(__p, 0, __bkt_count * sizeof(__node_base_ptr));
b09bcf83
FD
2076 return __p;
2077 }
2078
2079 template<typename _NodeAlloc>
2080 void
1b6f0476
FD
2081 _Hashtable_alloc<_NodeAlloc>::
2082 _M_deallocate_buckets(__buckets_ptr __bkts,
2083 std::size_t __bkt_count)
b09bcf83 2084 {
1b6f0476 2085 typedef typename __buckets_alloc_traits::pointer _Ptr;
b09bcf83 2086 auto __ptr = std::pointer_traits<_Ptr>::pointer_to(*__bkts);
1b6f0476
FD
2087 __buckets_alloc_type __alloc(_M_node_allocator());
2088 __buckets_alloc_traits::deallocate(__alloc, __ptr, __bkt_count);
b09bcf83
FD
2089 }
2090
f0b88346 2091 ///@} hashtable-detail
12ffa228 2092} // namespace __detail
6667d5fe 2093/// @endcond
4a15d842 2094_GLIBCXX_END_NAMESPACE_VERSION
12ffa228 2095} // namespace std
3b2524b1
PC
2096
2097#endif // _HASHTABLE_POLICY_H