]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/hashtable.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / hashtable.h
CommitLineData
e133ace8
PC
1// hashtable.h header -*- C++ -*-
2
99dee823 3// Copyright (C) 2007-2021 Free Software Foundation, Inc.
e133ace8
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
e133ace8
PC
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
748086b7
JJ
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/>.
e133ace8 24
94dabea7 25/** @file bits/hashtable.h
e133ace8 26 * This is an internal header file, included by other library headers.
30f276c1 27 * Do not attempt to use it directly. @headername{unordered_map, unordered_set}
e133ace8
PC
28 */
29
30#ifndef _HASHTABLE_H
31#define _HASHTABLE_H 1
32
33#pragma GCC system_header
34
3b2524b1 35#include <bits/hashtable_policy.h>
2dbe56bd
JW
36#if __cplusplus > 201402L
37# include <bits/node_handle.h>
38#endif
e133ace8 39
12ffa228
BK
40namespace std _GLIBCXX_VISIBILITY(default)
41{
42_GLIBCXX_BEGIN_NAMESPACE_VERSION
7c3e9502 43
4dad8b49 44 template<typename _Tp, typename _Hash>
5b3be7cf 45 using __cache_default
4df047dd
FD
46 = __not_<__and_<// Do not cache for fast hasher.
47 __is_fast_hash<_Hash>,
5b3be7cf 48 // Mandatory to have erase not throwing.
866e4d38 49 __is_nothrow_invocable<const _Hash&, const _Tp&>>>;
7c3e9502 50
da29608a 51 /**
4dad8b49
BK
52 * Primary class template _Hashtable.
53 *
54 * @ingroup hashtable-detail
55 *
56 * @tparam _Value CopyConstructible type.
57 *
58 * @tparam _Key CopyConstructible type.
59 *
60 * @tparam _Alloc An allocator type
61 * ([lib.allocator.requirements]) whose _Alloc::value_type is
62 * _Value. As a conforming extension, we allow for
63 * _Alloc::value_type != _Value.
64 *
65 * @tparam _ExtractKey Function object that takes an object of type
66 * _Value and returns a value of type _Key.
67 *
68 * @tparam _Equal Function object that takes two objects of type k
69 * and returns a bool-like value that is true if the two objects
70 * are considered equal.
71 *
4797a61c 72 * @tparam _Hash The hash function. A unary function object with
4dad8b49
BK
73 * argument type _Key and result type size_t. Return values should
74 * be distributed over the entire range [0, numeric_limits<size_t>:::max()].
75 *
4797a61c 76 * @tparam _RangeHash The range-hashing function (in the terminology of
4dad8b49
BK
77 * Tavori and Dreizin). A binary function object whose argument
78 * types and result type are all size_t. Given arguments r and N,
79 * the return value is in the range [0, N).
80 *
4797a61c 81 * @tparam _Unused Not used.
4dad8b49
BK
82 *
83 * @tparam _RehashPolicy Policy class with three members, all of
84 * which govern the bucket count. _M_next_bkt(n) returns a bucket
85 * count no smaller than n. _M_bkt_for_elements(n) returns a
86 * bucket count appropriate for an element count of n.
87 * _M_need_rehash(n_bkt, n_elt, n_ins) determines whether, if the
88 * current bucket count is n_bkt and the current element count is
4797a61c
FD
89 * n_elt, we need to increase the bucket count for n_ins insertions.
90 * If so, returns make_pair(true, n), where n is the new bucket count. If
91 * not, returns make_pair(false, <anything>)
4dad8b49
BK
92 *
93 * @tparam _Traits Compile-time class with three boolean
94 * std::integral_constant members: __cache_hash_code, __constant_iterators,
95 * __unique_keys.
da29608a 96 *
4dad8b49 97 * Each _Hashtable data structure has:
da29608a 98 *
4dad8b49 99 * - _Bucket[] _M_buckets
b09bcf83 100 * - _Hash_node_base _M_before_begin
4dad8b49
BK
101 * - size_type _M_bucket_count
102 * - size_type _M_element_count
da29608a 103 *
1b6f0476 104 * with _Bucket being _Hash_node_base* and _Hash_node containing:
da29608a 105 *
4dad8b49
BK
106 * - _Hash_node* _M_next
107 * - Tp _M_value
207585a6 108 * - size_t _M_hash_code if cache_hash_code is true
da29608a 109 *
207585a6 110 * In terms of Standard containers the hashtable is like the aggregation of:
da29608a 111 *
4dad8b49
BK
112 * - std::forward_list<_Node> containing the elements
113 * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
114 *
207585a6
JW
115 * The non-empty buckets contain the node before the first node in the
116 * bucket. This design makes it possible to implement something like a
4dad8b49
BK
117 * std::forward_list::insert_after on container insertion and
118 * std::forward_list::erase_after on container erase
119 * calls. _M_before_begin is equivalent to
207585a6
JW
120 * std::forward_list::before_begin. Empty buckets contain
121 * nullptr. Note that one of the non-empty buckets contains
122 * &_M_before_begin which is not a dereferenceable node so the
123 * node pointer in a bucket shall never be dereferenced, only its
4dad8b49
BK
124 * next node can be.
125 *
207585a6
JW
126 * Walking through a bucket's nodes requires a check on the hash code to
127 * see if each node is still in the bucket. Such a design assumes a
4dad8b49 128 * quite efficient hash functor and is one of the reasons it is
207585a6 129 * highly advisable to set __cache_hash_code to true.
4dad8b49
BK
130 *
131 * The container iterators are simply built from nodes. This way
132 * incrementing the iterator is perfectly efficient independent of
133 * how many empty buckets there are in the container.
134 *
207585a6
JW
135 * On insert we compute the element's hash code and use it to find the
136 * bucket index. If the element must be inserted in an empty bucket
4dad8b49
BK
137 * we add it at the beginning of the singly linked list and make the
138 * bucket point to _M_before_begin. The bucket that used to point to
139 * _M_before_begin, if any, is updated to point to its new before
140 * begin node.
141 *
207585a6 142 * On erase, the simple iterator design requires using the hash
4dad8b49 143 * functor to get the index of the bucket to update. For this
207585a6
JW
144 * reason, when __cache_hash_code is set to false the hash functor must
145 * not throw and this is enforced by a static assertion.
4dad8b49
BK
146 *
147 * Functionality is implemented by decomposition into base classes,
148 * where the derived _Hashtable class is used in _Map_base,
149 * _Insert, _Rehash_base, and _Equality base classes to access the
150 * "this" pointer. _Hashtable_base is used in the base classes as a
151 * non-recursive, fully-completed-type so that detailed nested type
152 * information, such as iterator type and node type, can be
153 * used. This is similar to the "Curiously Recurring Template
154 * Pattern" (CRTP) technique, but uses a reconstructed, not
155 * explicitly passed, template pattern.
156 *
157 * Base class templates are:
af6204cc
BK
158 * - __detail::_Hashtable_base
159 * - __detail::_Map_base
160 * - __detail::_Insert
161 * - __detail::_Rehash_base
162 * - __detail::_Equality
da29608a 163 */
4dad8b49 164 template<typename _Key, typename _Value, typename _Alloc,
3b2524b1 165 typename _ExtractKey, typename _Equal,
4797a61c 166 typename _Hash, typename _RangeHash, typename _Unused,
4dad8b49 167 typename _RehashPolicy, typename _Traits>
3b2524b1 168 class _Hashtable
4dad8b49 169 : public __detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal,
4797a61c 170 _Hash, _RangeHash, _Unused, _Traits>,
4dad8b49 171 public __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
172 _Hash, _RangeHash, _Unused,
173 _RehashPolicy, _Traits>,
4dad8b49 174 public __detail::_Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
175 _Hash, _RangeHash, _Unused,
176 _RehashPolicy, _Traits>,
4dad8b49 177 public __detail::_Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
178 _Hash, _RangeHash, _Unused,
179 _RehashPolicy, _Traits>,
4dad8b49 180 public __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
181 _Hash, _RangeHash, _Unused,
182 _RehashPolicy, _Traits>,
b09bcf83 183 private __detail::_Hashtable_alloc<
79466838
JW
184 __alloc_rebind<_Alloc,
185 __detail::_Hash_node<_Value,
186 _Traits::__hash_cached::value>>>
3b2524b1 187 {
866e4d38
JW
188 static_assert(is_same<typename remove_cv<_Value>::type, _Value>::value,
189 "unordered container must have a non-const, non-volatile value_type");
ebaf3659 190#if __cplusplus > 201703L || defined __STRICT_ANSI__
866e4d38
JW
191 static_assert(is_same<typename _Alloc::value_type, _Value>{},
192 "unordered container must have the same value_type as its allocator");
193#endif
866e4d38 194
b09bcf83
FD
195 using __traits_type = _Traits;
196 using __hash_cached = typename __traits_type::__hash_cached;
1b6f0476 197 using __constant_iterators = typename __traits_type::__constant_iterators;
b09bcf83 198 using __node_type = __detail::_Hash_node<_Value, __hash_cached::value>;
79466838 199 using __node_alloc_type = __alloc_rebind<_Alloc, __node_type>;
b09bcf83
FD
200
201 using __hashtable_alloc = __detail::_Hashtable_alloc<__node_alloc_type>;
202
1b6f0476
FD
203 using __node_value_type =
204 __detail::_Hash_node_value<_Value, __hash_cached::value>;
205 using __node_ptr = typename __hashtable_alloc::__node_ptr;
b09bcf83
FD
206 using __value_alloc_traits =
207 typename __hashtable_alloc::__value_alloc_traits;
208 using __node_alloc_traits =
209 typename __hashtable_alloc::__node_alloc_traits;
210 using __node_base = typename __hashtable_alloc::__node_base;
1b6f0476
FD
211 using __node_base_ptr = typename __hashtable_alloc::__node_base_ptr;
212 using __buckets_ptr = typename __hashtable_alloc::__buckets_ptr;
213
214 using __insert_base = __detail::_Insert<_Key, _Value, _Alloc, _ExtractKey,
215 _Equal, _Hash,
216 _RangeHash, _Unused,
217 _RehashPolicy, _Traits>;
0462b6aa 218
4dad8b49 219 public:
0462b6aa
FD
220 typedef _Key key_type;
221 typedef _Value value_type;
222 typedef _Alloc allocator_type;
223 typedef _Equal key_equal;
4dad8b49
BK
224
225 // mapped_type, if present, comes from _Map_base.
226 // hasher, if present, comes from _Hash_code_base/_Hashtable_base.
b09bcf83
FD
227 typedef typename __value_alloc_traits::pointer pointer;
228 typedef typename __value_alloc_traits::const_pointer const_pointer;
0462b6aa
FD
229 typedef value_type& reference;
230 typedef const value_type& const_reference;
4dad8b49 231
1b6f0476
FD
232 using iterator = typename __insert_base::iterator;
233
234 using const_iterator = typename __insert_base::const_iterator;
235
236 using local_iterator = __detail::_Local_iterator<key_type, _Value,
237 _ExtractKey, _Hash, _RangeHash, _Unused,
238 __constant_iterators::value,
239 __hash_cached::value>;
240
241 using const_local_iterator = __detail::_Local_const_iterator<
242 key_type, _Value,
243 _ExtractKey, _Hash, _RangeHash, _Unused,
244 __constant_iterators::value, __hash_cached::value>;
245
4dad8b49
BK
246 private:
247 using __rehash_type = _RehashPolicy;
248 using __rehash_state = typename __rehash_type::_State;
249
4dad8b49
BK
250 using __unique_keys = typename __traits_type::__unique_keys;
251
4dad8b49 252 using __hashtable_base = __detail::
4797a61c
FD
253 _Hashtable_base<_Key, _Value, _ExtractKey,
254 _Equal, _Hash, _RangeHash, _Unused, _Traits>;
4dad8b49
BK
255
256 using __hash_code_base = typename __hashtable_base::__hash_code_base;
257 using __hash_code = typename __hashtable_base::__hash_code;
1b6f0476 258 using __ireturn_type = typename __insert_base::__ireturn_type;
4dad8b49
BK
259
260 using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
4797a61c 261 _Equal, _Hash, _RangeHash, _Unused,
4dad8b49
BK
262 _RehashPolicy, _Traits>;
263
264 using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
265 _ExtractKey, _Equal,
4797a61c 266 _Hash, _RangeHash, _Unused,
4dad8b49
BK
267 _RehashPolicy, _Traits>;
268
269 using __eq_base = __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey,
4797a61c 270 _Equal, _Hash, _RangeHash, _Unused,
4dad8b49
BK
271 _RehashPolicy, _Traits>;
272
7cfe71d1 273 using __reuse_or_alloc_node_gen_t =
b09bcf83 274 __detail::_ReuseOrAllocNode<__node_alloc_type>;
b9c84e95
FD
275 using __alloc_node_gen_t =
276 __detail::_AllocNode<__node_alloc_type>;
b3abc9d8 277
b0c849fa
FD
278 // Simple RAII type for managing a node containing an element
279 struct _Scoped_node
280 {
281 // Take ownership of a node with a constructed element.
1b6f0476 282 _Scoped_node(__node_ptr __n, __hashtable_alloc* __h)
b0c849fa
FD
283 : _M_h(__h), _M_node(__n) { }
284
285 // Allocate a node and construct an element within it.
286 template<typename... _Args>
287 _Scoped_node(__hashtable_alloc* __h, _Args&&... __args)
288 : _M_h(__h),
289 _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...))
290 { }
291
292 // Destroy element and deallocate node.
293 ~_Scoped_node() { if (_M_node) _M_h->_M_deallocate_node(_M_node); };
294
295 _Scoped_node(const _Scoped_node&) = delete;
296 _Scoped_node& operator=(const _Scoped_node&) = delete;
297
298 __hashtable_alloc* _M_h;
1b6f0476 299 __node_ptr _M_node;
b0c849fa
FD
300 };
301
b9c84e95
FD
302 template<typename _Ht>
303 static constexpr
304 typename conditional<std::is_lvalue_reference<_Ht>::value,
305 const value_type&, value_type&&>::type
306 __fwd_value_for(value_type& __val) noexcept
307 { return std::move(__val); }
308
4dad8b49
BK
309 // Compile-time diagnostics.
310
1bb59e05
FD
311 // _Hash_code_base has everything protected, so use this derived type to
312 // access it.
313 struct __hash_code_base_access : __hash_code_base
314 { using __hash_code_base::_M_bucket_index; };
315
0462b6aa
FD
316 // Getting a bucket index from a node shall not throw because it is used
317 // in methods (erase, swap...) that shall not throw.
1bb59e05 318 static_assert(noexcept(declval<const __hash_code_base_access&>()
1b6f0476
FD
319 ._M_bucket_index(declval<const __node_value_type&>(),
320 (std::size_t)0)),
0462b6aa
FD
321 "Cache the hash code or qualify your functors involved"
322 " in hash code and bucket index computation with noexcept");
4dad8b49 323
4797a61c
FD
324 // To get bucket index we need _RangeHash not to throw.
325 static_assert(is_nothrow_default_constructible<_RangeHash>::value,
4dad8b49 326 "Functor used to map hash code to bucket index"
4797a61c
FD
327 " must be nothrow default constructible");
328 static_assert(noexcept(
329 std::declval<const _RangeHash&>()((std::size_t)0, (std::size_t)0)),
330 "Functor used to map hash code to bucket index must be"
331 " noexcept");
332
333 // To compute bucket index we also need _ExtratKey not to throw.
334 static_assert(is_nothrow_default_constructible<_ExtractKey>::value,
335 "_ExtractKey must be nothrow default constructible");
336 static_assert(noexcept(
337 std::declval<const _ExtractKey&>()(std::declval<_Value>())),
338 "_ExtractKey functor must be noexcept invocable");
4dad8b49 339
4dad8b49
BK
340 template<typename _Keya, typename _Valuea, typename _Alloca,
341 typename _ExtractKeya, typename _Equala,
4797a61c 342 typename _Hasha, typename _RangeHasha, typename _Unuseda,
4dad8b49
BK
343 typename _RehashPolicya, typename _Traitsa,
344 bool _Unique_keysa>
7c3e9502 345 friend struct __detail::_Map_base;
3b2524b1 346
4dad8b49
BK
347 template<typename _Keya, typename _Valuea, typename _Alloca,
348 typename _ExtractKeya, typename _Equala,
4797a61c 349 typename _Hasha, typename _RangeHasha, typename _Unuseda,
4dad8b49
BK
350 typename _RehashPolicya, typename _Traitsa>
351 friend struct __detail::_Insert_base;
352
353 template<typename _Keya, typename _Valuea, typename _Alloca,
354 typename _ExtractKeya, typename _Equala,
4797a61c 355 typename _Hasha, typename _RangeHasha, typename _Unuseda,
4dad8b49 356 typename _RehashPolicya, typename _Traitsa,
dc448fa0 357 bool _Constant_iteratorsa>
4dad8b49
BK
358 friend struct __detail::_Insert;
359
d9165389
FD
360 template<typename _Keya, typename _Valuea, typename _Alloca,
361 typename _ExtractKeya, typename _Equala,
4797a61c 362 typename _Hasha, typename _RangeHasha, typename _Unuseda,
d9165389
FD
363 typename _RehashPolicya, typename _Traitsa,
364 bool _Unique_keysa>
365 friend struct __detail::_Equality;
366
b3abc9d8 367 public:
4dad8b49
BK
368 using size_type = typename __hashtable_base::size_type;
369 using difference_type = typename __hashtable_base::difference_type;
370
2dbe56bd
JW
371#if __cplusplus > 201402L
372 using node_type = _Node_handle<_Key, _Value, __node_alloc_type>;
373 using insert_return_type = _Node_insert_return<iterator, node_type>;
374#endif
375
3b2524b1 376 private:
1b6f0476 377 __buckets_ptr _M_buckets = &_M_single_bucket;
da27f556 378 size_type _M_bucket_count = 1;
b09bcf83 379 __node_base _M_before_begin;
da27f556 380 size_type _M_element_count = 0;
f86b266c 381 _RehashPolicy _M_rehash_policy;
fb7342fd 382
95539f21 383 // A single bucket used when only need for 1 bucket. Especially
7cfe71d1 384 // interesting in move semantic to leave hashtable with only 1 bucket
95539f21
FD
385 // which is not allocated so that we can have those operations noexcept
386 // qualified.
387 // Note that we can't leave hashtable with 0 bucket without adding
388 // numerous checks in the code to avoid 0 modulus.
1b6f0476 389 __node_base_ptr _M_single_bucket = nullptr;
95539f21 390
f9d98fa7
FD
391 void
392 _M_update_bbegin()
393 {
394 if (_M_begin())
1b6f0476 395 _M_buckets[_M_bucket_index(*_M_begin())] = &_M_before_begin;
f9d98fa7
FD
396 }
397
398 void
1b6f0476 399 _M_update_bbegin(__node_ptr __n)
f9d98fa7
FD
400 {
401 _M_before_begin._M_nxt = __n;
402 _M_update_bbegin();
403 }
404
95539f21 405 bool
1b6f0476 406 _M_uses_single_bucket(__buckets_ptr __bkts) const
e6fb44d8 407 { return __builtin_expect(__bkts == &_M_single_bucket, false); }
95539f21
FD
408
409 bool
410 _M_uses_single_bucket() const
411 { return _M_uses_single_bucket(_M_buckets); }
412
b09bcf83
FD
413 __hashtable_alloc&
414 _M_base_alloc() { return *this; }
0dd49691 415
1b6f0476 416 __buckets_ptr
7cfe71d1 417 _M_allocate_buckets(size_type __bkt_count)
95539f21 418 {
7cfe71d1 419 if (__builtin_expect(__bkt_count == 1, false))
95539f21
FD
420 {
421 _M_single_bucket = nullptr;
422 return &_M_single_bucket;
423 }
424
7cfe71d1 425 return __hashtable_alloc::_M_allocate_buckets(__bkt_count);
95539f21
FD
426 }
427
428 void
1b6f0476 429 _M_deallocate_buckets(__buckets_ptr __bkts, size_type __bkt_count)
95539f21
FD
430 {
431 if (_M_uses_single_bucket(__bkts))
432 return;
433
7cfe71d1 434 __hashtable_alloc::_M_deallocate_buckets(__bkts, __bkt_count);
95539f21 435 }
da29608a 436
0462b6aa
FD
437 void
438 _M_deallocate_buckets()
95539f21 439 { _M_deallocate_buckets(_M_buckets, _M_bucket_count); }
0462b6aa 440
f86b266c
FD
441 // Gets bucket begin, deals with the fact that non-empty buckets contain
442 // their before begin node.
1b6f0476 443 __node_ptr
da29608a
FD
444 _M_bucket_begin(size_type __bkt) const;
445
1b6f0476 446 __node_ptr
f86b266c 447 _M_begin() const
1b6f0476 448 { return static_cast<__node_ptr>(_M_before_begin._M_nxt); }
da29608a 449
b9c84e95
FD
450 // Assign *this using another _Hashtable instance. Whether elements
451 // are copied or moved depends on the _Ht reference.
452 template<typename _Ht>
01021139 453 void
b9c84e95 454 _M_assign_elements(_Ht&&);
01021139 455
b9c84e95 456 template<typename _Ht, typename _NodeGenerator>
0462b6aa 457 void
b9c84e95 458 _M_assign(_Ht&&, const _NodeGenerator&);
0462b6aa
FD
459
460 void
7cfe71d1 461 _M_move_assign(_Hashtable&&, true_type);
0462b6aa
FD
462
463 void
7cfe71d1 464 _M_move_assign(_Hashtable&&, false_type);
0462b6aa
FD
465
466 void
467 _M_reset() noexcept;
468
4797a61c 469 _Hashtable(const _Hash& __h, const _Equal& __eq,
da27f556 470 const allocator_type& __a)
4797a61c 471 : __hashtable_base(__h, __eq),
7cfe71d1 472 __hashtable_alloc(__node_alloc_type(__a))
da27f556
FD
473 { }
474
12324b9a
FD
475 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
476 true_type /* alloc always equal */)
4797a61c 477 noexcept(std::is_nothrow_copy_constructible<_Hash>::value &&
12324b9a
FD
478 std::is_nothrow_copy_constructible<_Equal>::value);
479
480 _Hashtable(_Hashtable&&, __node_alloc_type&&,
481 false_type /* alloc always equal */);
482
6dcf0423
FD
483 template<typename _InputIterator>
484 _Hashtable(_InputIterator __first, _InputIterator __last,
485 size_type __bkt_count_hint,
4797a61c 486 const _Hash&, const _Equal&, const allocator_type&,
6dcf0423
FD
487 true_type __uks);
488
489 template<typename _InputIterator>
490 _Hashtable(_InputIterator __first, _InputIterator __last,
491 size_type __bkt_count_hint,
4797a61c 492 const _Hash&, const _Equal&, const allocator_type&,
6dcf0423
FD
493 false_type __uks);
494
7c3e9502 495 public:
3b2524b1 496 // Constructor, destructor, assignment, swap
da27f556 497 _Hashtable() = default;
7c3e9502 498
3b2524b1
PC
499 _Hashtable(const _Hashtable&);
500
4797a61c
FD
501 _Hashtable(const _Hashtable&, const allocator_type&);
502
503 explicit
504 _Hashtable(size_type __bkt_count_hint,
505 const _Hash& __hf = _Hash(),
506 const key_equal& __eql = key_equal(),
507 const allocator_type& __a = allocator_type());
508
509 // Use delegating constructors.
12324b9a
FD
510 _Hashtable(_Hashtable&& __ht)
511 noexcept( noexcept(
512 _Hashtable(std::declval<_Hashtable>(),
513 std::declval<__node_alloc_type>(),
514 true_type{})) )
515 : _Hashtable(std::move(__ht), std::move(__ht._M_node_allocator()),
516 true_type{})
517 { }
0462b6aa 518
12324b9a
FD
519 _Hashtable(_Hashtable&& __ht, const allocator_type& __a)
520 noexcept( noexcept(
521 _Hashtable(std::declval<_Hashtable>(),
522 std::declval<__node_alloc_type>(),
523 typename __node_alloc_traits::is_always_equal{})) )
524 : _Hashtable(std::move(__ht), __node_alloc_type(__a),
525 typename __node_alloc_traits::is_always_equal{})
526 { }
b24a9835 527
0462b6aa
FD
528 explicit
529 _Hashtable(const allocator_type& __a)
7cfe71d1 530 : __hashtable_alloc(__node_alloc_type(__a))
0462b6aa
FD
531 { }
532
4dad8b49
BK
533 template<typename _InputIterator>
534 _Hashtable(_InputIterator __f, _InputIterator __l,
7cfe71d1 535 size_type __bkt_count_hint = 0,
4797a61c 536 const _Hash& __hf = _Hash(),
4dad8b49
BK
537 const key_equal& __eql = key_equal(),
538 const allocator_type& __a = allocator_type())
4797a61c
FD
539 : _Hashtable(__f, __l, __bkt_count_hint, __hf, __eql, __a,
540 __unique_keys{})
4dad8b49
BK
541 { }
542
543 _Hashtable(initializer_list<value_type> __l,
7cfe71d1 544 size_type __bkt_count_hint = 0,
4797a61c 545 const _Hash& __hf = _Hash(),
4dad8b49
BK
546 const key_equal& __eql = key_equal(),
547 const allocator_type& __a = allocator_type())
7cfe71d1 548 : _Hashtable(__l.begin(), __l.end(), __bkt_count_hint,
4797a61c 549 __hf, __eql, __a, __unique_keys{})
4dad8b49
BK
550 { }
551
3b2524b1 552 _Hashtable&
0462b6aa 553 operator=(const _Hashtable& __ht);
417e896e
PC
554
555 _Hashtable&
556 operator=(_Hashtable&& __ht)
a2b5fdcb 557 noexcept(__node_alloc_traits::_S_nothrow_move()
4797a61c 558 && is_nothrow_move_assignable<_Hash>::value
a2b5fdcb 559 && is_nothrow_move_assignable<_Equal>::value)
417e896e 560 {
4797a61c 561 constexpr bool __move_storage =
a2b5fdcb
JW
562 __node_alloc_traits::_S_propagate_on_move_assign()
563 || __node_alloc_traits::_S_always_equal();
564 _M_move_assign(std::move(__ht), __bool_constant<__move_storage>());
417e896e
PC
565 return *this;
566 }
3b2524b1 567
4dad8b49
BK
568 _Hashtable&
569 operator=(initializer_list<value_type> __l)
570 {
7cfe71d1 571 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *this);
b09bcf83 572 _M_before_begin._M_nxt = nullptr;
0462b6aa 573 clear();
6dcf0423
FD
574
575 // We consider that all elements of __l are going to be inserted.
576 auto __l_bkt_count = _M_rehash_policy._M_bkt_for_elements(__l.size());
577
578 // Do not shrink to keep potential user reservation.
579 if (_M_bucket_count < __l_bkt_count)
580 rehash(__l_bkt_count);
581
582 this->_M_insert_range(__l.begin(), __l.end(), __roan, __unique_keys{});
4dad8b49
BK
583 return *this;
584 }
585
6f59ea25 586 ~_Hashtable() noexcept;
3b2524b1 587
0462b6aa
FD
588 void
589 swap(_Hashtable&)
4797a61c
FD
590 noexcept(__and_<__is_nothrow_swappable<_Hash>,
591 __is_nothrow_swappable<_Equal>>::value);
3b2524b1
PC
592
593 // Basic container operations
594 iterator
d3677132 595 begin() noexcept
f86b266c 596 { return iterator(_M_begin()); }
3b2524b1
PC
597
598 const_iterator
d3677132 599 begin() const noexcept
f86b266c 600 { return const_iterator(_M_begin()); }
3b2524b1
PC
601
602 iterator
d3677132 603 end() noexcept
da29608a 604 { return iterator(nullptr); }
3b2524b1
PC
605
606 const_iterator
d3677132 607 end() const noexcept
da29608a 608 { return const_iterator(nullptr); }
3b2524b1
PC
609
610 const_iterator
d3677132 611 cbegin() const noexcept
f86b266c 612 { return const_iterator(_M_begin()); }
3b2524b1
PC
613
614 const_iterator
d3677132 615 cend() const noexcept
da29608a 616 { return const_iterator(nullptr); }
3b2524b1
PC
617
618 size_type
d3677132 619 size() const noexcept
3b2524b1 620 { return _M_element_count; }
7c3e9502 621
d715f554 622 _GLIBCXX_NODISCARD bool
d3677132 623 empty() const noexcept
3b2524b1
PC
624 { return size() == 0; }
625
626 allocator_type
d3677132 627 get_allocator() const noexcept
b09bcf83 628 { return allocator_type(this->_M_node_allocator()); }
3b2524b1 629
3b2524b1 630 size_type
d3677132 631 max_size() const noexcept
b09bcf83 632 { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
3b2524b1
PC
633
634 // Observers
635 key_equal
636 key_eq() const
f7d6ad0a 637 { return this->_M_eq(); }
3b2524b1
PC
638
639 // hash_function, if present, comes from _Hash_code_base.
640
641 // Bucket operations
642 size_type
d3677132 643 bucket_count() const noexcept
3b2524b1 644 { return _M_bucket_count; }
7c3e9502 645
3b2524b1 646 size_type
d3677132 647 max_bucket_count() const noexcept
3b2524b1 648 { return max_size(); }
7c3e9502 649
3b2524b1 650 size_type
7cfe71d1
FD
651 bucket_size(size_type __bkt) const
652 { return std::distance(begin(__bkt), end(__bkt)); }
7c3e9502 653
3b2524b1
PC
654 size_type
655 bucket(const key_type& __k) const
4797a61c 656 { return _M_bucket_index(this->_M_hash_code(__k)); }
3b2524b1
PC
657
658 local_iterator
7cfe71d1 659 begin(size_type __bkt)
5b3be7cf 660 {
7cfe71d1
FD
661 return local_iterator(*this, _M_bucket_begin(__bkt),
662 __bkt, _M_bucket_count);
5b3be7cf 663 }
3b2524b1
PC
664
665 local_iterator
7cfe71d1
FD
666 end(size_type __bkt)
667 { return local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
3b2524b1
PC
668
669 const_local_iterator
7cfe71d1 670 begin(size_type __bkt) const
5b3be7cf 671 {
7cfe71d1
FD
672 return const_local_iterator(*this, _M_bucket_begin(__bkt),
673 __bkt, _M_bucket_count);
5b3be7cf 674 }
3b2524b1
PC
675
676 const_local_iterator
7cfe71d1
FD
677 end(size_type __bkt) const
678 { return const_local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
3b2524b1
PC
679
680 // DR 691.
681 const_local_iterator
7cfe71d1 682 cbegin(size_type __bkt) const
5b3be7cf 683 {
7cfe71d1
FD
684 return const_local_iterator(*this, _M_bucket_begin(__bkt),
685 __bkt, _M_bucket_count);
5b3be7cf 686 }
3b2524b1
PC
687
688 const_local_iterator
7cfe71d1
FD
689 cend(size_type __bkt) const
690 { return const_local_iterator(*this, nullptr, __bkt, _M_bucket_count); }
3b2524b1
PC
691
692 float
d3677132 693 load_factor() const noexcept
7c3e9502 694 {
3b2524b1
PC
695 return static_cast<float>(size()) / static_cast<float>(bucket_count());
696 }
697
698 // max_load_factor, if present, comes from _Rehash_base.
699
4dad8b49
BK
700 // Generalization of max_load_factor. Extension, not found in
701 // TR1. Only useful if _RehashPolicy is something other than
702 // the default.
3b2524b1
PC
703 const _RehashPolicy&
704 __rehash_policy() const
705 { return _M_rehash_policy; }
7c3e9502
BK
706
707 void
291e91da
FD
708 __rehash_policy(const _RehashPolicy& __pol)
709 { _M_rehash_policy = __pol; }
3b2524b1
PC
710
711 // Lookup.
712 iterator
713 find(const key_type& __k);
714
715 const_iterator
716 find(const key_type& __k) const;
717
718 size_type
719 count(const key_type& __k) const;
720
721 std::pair<iterator, iterator>
722 equal_range(const key_type& __k);
723
724 std::pair<const_iterator, const_iterator>
725 equal_range(const key_type& __k) const;
726
f9d98fa7 727 private:
f86b266c 728 // Bucket index computation helpers.
a188284c 729 size_type
1b6f0476 730 _M_bucket_index(const __node_value_type& __n) const noexcept
4dad8b49 731 { return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
a188284c
FD
732
733 size_type
4797a61c
FD
734 _M_bucket_index(__hash_code __c) const
735 { return __hash_code_base::_M_bucket_index(__c, _M_bucket_count); }
a188284c 736
1b4ec7f0 737 // Find and insert helper functions and types
f86b266c 738 // Find the node before the one matching the criteria.
1b6f0476 739 __node_base_ptr
4dad8b49 740 _M_find_before_node(size_type, const key_type&, __hash_code) const;
f86b266c 741
1b6f0476 742 __node_ptr
f86b266c 743 _M_find_node(size_type __bkt, const key_type& __key,
4dad8b49 744 __hash_code __c) const
f86b266c 745 {
1b6f0476 746 __node_base_ptr __before_n = _M_find_before_node(__bkt, __key, __c);
f86b266c 747 if (__before_n)
1b6f0476 748 return static_cast<__node_ptr>(__before_n->_M_nxt);
f86b266c
FD
749 return nullptr;
750 }
fb7342fd 751
f86b266c 752 // Insert a node at the beginning of a bucket.
da29608a 753 void
1b6f0476 754 _M_insert_bucket_begin(size_type, __node_ptr);
da29608a 755
da29608a
FD
756 // Remove the bucket first node
757 void
1b6f0476 758 _M_remove_bucket_begin(size_type __bkt, __node_ptr __next_n,
da29608a
FD
759 size_type __next_bkt);
760
761 // Get the node before __n in the bucket __bkt
1b6f0476
FD
762 __node_base_ptr
763 _M_get_previous_node(size_type __bkt, __node_ptr __n);
da29608a 764
4797a61c
FD
765 // Insert node __n with hash code __code, in bucket __bkt if no
766 // rehash (assumes no element with same key already present).
b0c849fa 767 // Takes ownership of __n if insertion succeeds, throws otherwise.
181a5a13 768 iterator
4797a61c 769 _M_insert_unique_node(size_type __bkt, __hash_code,
1b6f0476 770 __node_ptr __n, size_type __n_elt = 1);
fb7342fd 771
b0c849fa
FD
772 // Insert node __n with key __k and hash code __code.
773 // Takes ownership of __n if insertion succeeds, throws otherwise.
181a5a13 774 iterator
1b6f0476
FD
775 _M_insert_multi_node(__node_ptr __hint,
776 __hash_code __code, __node_ptr __n);
3b2524b1 777
9f285ccb
JM
778 template<typename... _Args>
779 std::pair<iterator, bool>
4797a61c 780 _M_emplace(true_type __uks, _Args&&... __args);
9b81593b 781
9f285ccb
JM
782 template<typename... _Args>
783 iterator
4797a61c
FD
784 _M_emplace(false_type __uks, _Args&&... __args)
785 { return _M_emplace(cend(), __uks, std::forward<_Args>(__args)...); }
41349aec
FD
786
787 // Emplace with hint, useless when keys are unique.
788 template<typename... _Args>
789 iterator
6dcf0423
FD
790 _M_emplace(const_iterator, true_type __uks, _Args&&... __args)
791 { return _M_emplace(__uks, std::forward<_Args>(__args)...).first; }
41349aec
FD
792
793 template<typename... _Args>
794 iterator
6dcf0423 795 _M_emplace(const_iterator, false_type __uks, _Args&&... __args);
9b81593b 796
b3abc9d8 797 template<typename _Arg, typename _NodeGenerator>
1c299612 798 std::pair<iterator, bool>
6dcf0423 799 _M_insert(_Arg&&, const _NodeGenerator&, true_type __uks);
1c299612 800
b3abc9d8 801 template<typename _Arg, typename _NodeGenerator>
1c299612 802 iterator
b3abc9d8 803 _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
6dcf0423 804 false_type __uks)
b3abc9d8
FD
805 {
806 return _M_insert(cend(), std::forward<_Arg>(__arg), __node_gen,
6dcf0423 807 __uks);
b3abc9d8 808 }
41349aec
FD
809
810 // Insert with hint, not used when keys are unique.
b3abc9d8 811 template<typename _Arg, typename _NodeGenerator>
41349aec 812 iterator
db97b3b0 813 _M_insert(const_iterator, _Arg&& __arg,
6dcf0423 814 const _NodeGenerator& __node_gen, true_type __uks)
b3abc9d8
FD
815 {
816 return
6dcf0423 817 _M_insert(std::forward<_Arg>(__arg), __node_gen, __uks).first;
b3abc9d8 818 }
41349aec
FD
819
820 // Insert with hint when keys are not unique.
b3abc9d8 821 template<typename _Arg, typename _NodeGenerator>
41349aec 822 iterator
db97b3b0 823 _M_insert(const_iterator, _Arg&&,
6dcf0423 824 const _NodeGenerator&, false_type __uks);
1c299612 825
31578792 826 size_type
4797a61c 827 _M_erase(true_type __uks, const key_type&);
31578792
FD
828
829 size_type
4797a61c 830 _M_erase(false_type __uks, const key_type&);
31578792
FD
831
832 iterator
1b6f0476 833 _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n);
31578792 834
1b4ec7f0 835 public:
4dad8b49 836 // Emplace
9b81593b 837 template<typename... _Args>
4dad8b49 838 __ireturn_type
9b81593b 839 emplace(_Args&&... __args)
4797a61c 840 { return _M_emplace(__unique_keys{}, std::forward<_Args>(__args)...); }
9b81593b
FD
841
842 template<typename... _Args>
843 iterator
41349aec
FD
844 emplace_hint(const_iterator __hint, _Args&&... __args)
845 {
4797a61c 846 return _M_emplace(__hint, __unique_keys{},
9f285ccb 847 std::forward<_Args>(__args)...);
41349aec 848 }
9b81593b 849
4dad8b49 850 // Insert member functions via inheritance.
3b2524b1 851
4dad8b49 852 // Erase
d723ced2 853 iterator
3b2524b1
PC
854 erase(const_iterator);
855
6dc88283
PC
856 // LWG 2059.
857 iterator
858 erase(iterator __it)
859 { return erase(const_iterator(__it)); }
860
3b2524b1 861 size_type
31578792 862 erase(const key_type& __k)
4797a61c 863 { return _M_erase(__unique_keys{}, __k); }
e133ace8 864
d723ced2 865 iterator
3b2524b1
PC
866 erase(const_iterator, const_iterator);
867
868 void
d3677132 869 clear() noexcept;
3b2524b1 870
7cfe71d1
FD
871 // Set number of buckets keeping it appropriate for container's number
872 // of elements.
873 void rehash(size_type __bkt_count);
9155c0e3
PC
874
875 // DR 1189.
876 // reserve, if present, comes from _Rehash_base.
877
2dbe56bd
JW
878#if __cplusplus > 201402L
879 /// Re-insert an extracted node into a container with unique keys.
880 insert_return_type
881 _M_reinsert_node(node_type&& __nh)
882 {
883 insert_return_type __ret;
884 if (__nh.empty())
885 __ret.position = end();
886 else
887 {
888 __glibcxx_assert(get_allocator() == __nh.get_allocator());
889
890 const key_type& __k = __nh._M_key();
891 __hash_code __code = this->_M_hash_code(__k);
4797a61c 892 size_type __bkt = _M_bucket_index(__code);
1b6f0476 893 if (__node_ptr __n = _M_find_node(__bkt, __k, __code))
2dbe56bd
JW
894 {
895 __ret.node = std::move(__nh);
896 __ret.position = iterator(__n);
897 __ret.inserted = false;
898 }
899 else
900 {
901 __ret.position
4797a61c 902 = _M_insert_unique_node(__bkt, __code, __nh._M_ptr);
2dbe56bd
JW
903 __nh._M_ptr = nullptr;
904 __ret.inserted = true;
905 }
906 }
907 return __ret;
908 }
909
910 /// Re-insert an extracted node into a container with equivalent keys.
911 iterator
912 _M_reinsert_node_multi(const_iterator __hint, node_type&& __nh)
913 {
2dbe56bd 914 if (__nh.empty())
b0c849fa 915 return end();
2dbe56bd 916
b0c849fa
FD
917 __glibcxx_assert(get_allocator() == __nh.get_allocator());
918
919 const key_type& __k = __nh._M_key();
920 auto __code = this->_M_hash_code(__k);
921 auto __ret
4797a61c 922 = _M_insert_multi_node(__hint._M_cur, __code, __nh._M_ptr);
b0c849fa 923 __nh._M_ptr = nullptr;
2dbe56bd
JW
924 return __ret;
925 }
926
b0c849fa 927 private:
2dbe56bd 928 node_type
1b6f0476 929 _M_extract_node(size_t __bkt, __node_base_ptr __prev_n)
2dbe56bd 930 {
1b6f0476 931 __node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);
2dbe56bd
JW
932 if (__prev_n == _M_buckets[__bkt])
933 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1b6f0476 934 __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
2dbe56bd
JW
935 else if (__n->_M_nxt)
936 {
1b6f0476 937 size_type __next_bkt = _M_bucket_index(*__n->_M_next());
2dbe56bd
JW
938 if (__next_bkt != __bkt)
939 _M_buckets[__next_bkt] = __prev_n;
940 }
941
942 __prev_n->_M_nxt = __n->_M_nxt;
943 __n->_M_nxt = nullptr;
944 --_M_element_count;
945 return { __n, this->_M_node_allocator() };
946 }
947
b0c849fa
FD
948 public:
949 // Extract a node.
950 node_type
951 extract(const_iterator __pos)
952 {
1b6f0476 953 size_t __bkt = _M_bucket_index(*__pos._M_cur);
b0c849fa
FD
954 return _M_extract_node(__bkt,
955 _M_get_previous_node(__bkt, __pos._M_cur));
956 }
957
2dbe56bd
JW
958 /// Extract a node.
959 node_type
960 extract(const _Key& __k)
961 {
962 node_type __nh;
b0c849fa 963 __hash_code __code = this->_M_hash_code(__k);
4797a61c 964 std::size_t __bkt = _M_bucket_index(__code);
1b6f0476 965 if (__node_base_ptr __prev_node = _M_find_before_node(__bkt, __k, __code))
b0c849fa 966 __nh = _M_extract_node(__bkt, __prev_node);
2dbe56bd
JW
967 return __nh;
968 }
969
970 /// Merge from a compatible container into one with unique keys.
971 template<typename _Compatible_Hashtable>
972 void
973 _M_merge_unique(_Compatible_Hashtable& __src) noexcept
974 {
975 static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
976 node_type>, "Node types are compatible");
977 __glibcxx_assert(get_allocator() == __src.get_allocator());
978
0f146257 979 auto __n_elt = __src.size();
2dbe56bd
JW
980 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
981 {
982 auto __pos = __i++;
4797a61c 983 const key_type& __k = _ExtractKey{}(*__pos);
2dbe56bd 984 __hash_code __code = this->_M_hash_code(__k);
4797a61c 985 size_type __bkt = _M_bucket_index(__code);
2dbe56bd
JW
986 if (_M_find_node(__bkt, __k, __code) == nullptr)
987 {
988 auto __nh = __src.extract(__pos);
4797a61c 989 _M_insert_unique_node(__bkt, __code, __nh._M_ptr, __n_elt);
2dbe56bd 990 __nh._M_ptr = nullptr;
0f146257 991 __n_elt = 1;
2dbe56bd 992 }
0f146257
FD
993 else if (__n_elt != 1)
994 --__n_elt;
2dbe56bd
JW
995 }
996 }
997
998 /// Merge from a compatible container into one with equivalent keys.
999 template<typename _Compatible_Hashtable>
1000 void
1001 _M_merge_multi(_Compatible_Hashtable& __src) noexcept
1002 {
1003 static_assert(is_same_v<typename _Compatible_Hashtable::node_type,
1004 node_type>, "Node types are compatible");
1005 __glibcxx_assert(get_allocator() == __src.get_allocator());
1006
1007 this->reserve(size() + __src.size());
1008 for (auto __i = __src.begin(), __end = __src.end(); __i != __end;)
1009 _M_reinsert_node_multi(cend(), __src.extract(__i++));
1010 }
1011#endif // C++17
1012
3b2524b1 1013 private:
ac0ab963 1014 // Helper rehash method used when keys are unique.
4797a61c 1015 void _M_rehash_aux(size_type __bkt_count, true_type __uks);
ac0ab963
FD
1016
1017 // Helper rehash method used when keys can be non-unique.
4797a61c 1018 void _M_rehash_aux(size_type __bkt_count, false_type __uks);
ac0ab963 1019
4dad8b49
BK
1020 // Unconditionally change size of bucket array to n, restore
1021 // hash policy state to __state on exception.
7cfe71d1 1022 void _M_rehash(size_type __bkt_count, const __rehash_state& __state);
3b2524b1
PC
1023 };
1024
1025
1026 // Definitions of class template _Hashtable's out-of-line member functions.
4797a61c
FD
1027 template<typename _Key, typename _Value, typename _Alloc,
1028 typename _ExtractKey, typename _Equal,
1029 typename _Hash, typename _RangeHash, typename _Unused,
1030 typename _RehashPolicy, typename _Traits>
db97b3b0 1031 auto
4dad8b49 1032 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1033 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
da29608a 1034 _M_bucket_begin(size_type __bkt) const
1b6f0476 1035 -> __node_ptr
da29608a 1036 {
1b6f0476
FD
1037 __node_base_ptr __n = _M_buckets[__bkt];
1038 return __n ? static_cast<__node_ptr>(__n->_M_nxt) : nullptr;
da29608a
FD
1039 }
1040
4797a61c
FD
1041 template<typename _Key, typename _Value, typename _Alloc,
1042 typename _ExtractKey, typename _Equal,
1043 typename _Hash, typename _RangeHash, typename _Unused,
1044 typename _RehashPolicy, typename _Traits>
4dad8b49 1045 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1046 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
7cfe71d1 1047 _Hashtable(size_type __bkt_count_hint,
4797a61c
FD
1048 const _Hash& __h, const _Equal& __eq, const allocator_type& __a)
1049 : _Hashtable(__h, __eq, __a)
3b2524b1 1050 {
7cfe71d1
FD
1051 auto __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count_hint);
1052 if (__bkt_count > _M_bucket_count)
da27f556 1053 {
7cfe71d1
FD
1054 _M_buckets = _M_allocate_buckets(__bkt_count);
1055 _M_bucket_count = __bkt_count;
da27f556 1056 }
3b2524b1
PC
1057 }
1058
4797a61c
FD
1059 template<typename _Key, typename _Value, typename _Alloc,
1060 typename _ExtractKey, typename _Equal,
1061 typename _Hash, typename _RangeHash, typename _Unused,
1062 typename _RehashPolicy, typename _Traits>
3b2524b1 1063 template<typename _InputIterator>
4dad8b49 1064 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1065 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1066 _Hashtable(_InputIterator __f, _InputIterator __l,
7cfe71d1 1067 size_type __bkt_count_hint,
4797a61c 1068 const _Hash& __h, const _Equal& __eq,
6dcf0423 1069 const allocator_type& __a, true_type /* __uks */)
4797a61c 1070 : _Hashtable(__bkt_count_hint, __h, __eq, __a)
6dcf0423
FD
1071 {
1072 for (; __f != __l; ++__f)
1073 this->insert(*__f);
1074 }
1075
4797a61c
FD
1076 template<typename _Key, typename _Value, typename _Alloc,
1077 typename _ExtractKey, typename _Equal,
1078 typename _Hash, typename _RangeHash, typename _Unused,
1079 typename _RehashPolicy, typename _Traits>
6dcf0423
FD
1080 template<typename _InputIterator>
1081 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1082 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
6dcf0423
FD
1083 _Hashtable(_InputIterator __f, _InputIterator __l,
1084 size_type __bkt_count_hint,
4797a61c 1085 const _Hash& __h, const _Equal& __eq,
6dcf0423 1086 const allocator_type& __a, false_type /* __uks */)
4797a61c 1087 : _Hashtable(__h, __eq, __a)
3b2524b1 1088 {
d4a7f7a1 1089 auto __nb_elems = __detail::__distance_fw(__f, __l);
da27f556 1090 auto __bkt_count =
d4a7f7a1
FD
1091 _M_rehash_policy._M_next_bkt(
1092 std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
7cfe71d1 1093 __bkt_count_hint));
d4a7f7a1 1094
da27f556
FD
1095 if (__bkt_count > _M_bucket_count)
1096 {
1097 _M_buckets = _M_allocate_buckets(__bkt_count);
1098 _M_bucket_count = __bkt_count;
1099 }
1100
676c4146
JW
1101 for (; __f != __l; ++__f)
1102 this->insert(*__f);
3b2524b1 1103 }
7c3e9502 1104
4797a61c
FD
1105 template<typename _Key, typename _Value, typename _Alloc,
1106 typename _ExtractKey, typename _Equal,
1107 typename _Hash, typename _RangeHash, typename _Unused,
1108 typename _RehashPolicy, typename _Traits>
db97b3b0 1109 auto
0462b6aa 1110 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1111 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
db97b3b0
JW
1112 operator=(const _Hashtable& __ht)
1113 -> _Hashtable&
1114 {
1115 if (&__ht == this)
1116 return *this;
0462b6aa 1117
db97b3b0
JW
1118 if (__node_alloc_traits::_S_propagate_on_copy_assign())
1119 {
1120 auto& __this_alloc = this->_M_node_allocator();
1121 auto& __that_alloc = __ht._M_node_allocator();
1122 if (!__node_alloc_traits::_S_always_equal()
1123 && __this_alloc != __that_alloc)
1124 {
1125 // Replacement allocator cannot free existing storage.
1126 this->_M_deallocate_nodes(_M_begin());
1127 _M_before_begin._M_nxt = nullptr;
1128 _M_deallocate_buckets();
1129 _M_buckets = nullptr;
1130 std::__alloc_on_copy(__this_alloc, __that_alloc);
1131 __hashtable_base::operator=(__ht);
1132 _M_bucket_count = __ht._M_bucket_count;
1133 _M_element_count = __ht._M_element_count;
1134 _M_rehash_policy = __ht._M_rehash_policy;
b9c84e95 1135 __alloc_node_gen_t __alloc_node_gen(*this);
db97b3b0
JW
1136 __try
1137 {
b9c84e95 1138 _M_assign(__ht, __alloc_node_gen);
db97b3b0
JW
1139 }
1140 __catch(...)
1141 {
1142 // _M_assign took care of deallocating all memory. Now we
1143 // must make sure this instance remains in a usable state.
1144 _M_reset();
1145 __throw_exception_again;
1146 }
1147 return *this;
1148 }
1149 std::__alloc_on_copy(__this_alloc, __that_alloc);
1150 }
0462b6aa 1151
db97b3b0 1152 // Reuse allocated buckets and nodes.
b9c84e95 1153 _M_assign_elements(__ht);
01021139
FD
1154 return *this;
1155 }
db97b3b0 1156
4797a61c
FD
1157 template<typename _Key, typename _Value, typename _Alloc,
1158 typename _ExtractKey, typename _Equal,
1159 typename _Hash, typename _RangeHash, typename _Unused,
1160 typename _RehashPolicy, typename _Traits>
b9c84e95 1161 template<typename _Ht>
01021139
FD
1162 void
1163 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1164 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
b9c84e95 1165 _M_assign_elements(_Ht&& __ht)
01021139 1166 {
1b6f0476 1167 __buckets_ptr __former_buckets = nullptr;
01021139
FD
1168 std::size_t __former_bucket_count = _M_bucket_count;
1169 const __rehash_state& __former_state = _M_rehash_policy._M_state();
db97b3b0 1170
01021139
FD
1171 if (_M_bucket_count != __ht._M_bucket_count)
1172 {
1173 __former_buckets = _M_buckets;
1174 _M_buckets = _M_allocate_buckets(__ht._M_bucket_count);
1175 _M_bucket_count = __ht._M_bucket_count;
1176 }
1177 else
0462b6aa 1178 __builtin_memset(_M_buckets, 0,
1b6f0476 1179 _M_bucket_count * sizeof(__node_base_ptr));
01021139
FD
1180
1181 __try
1182 {
1183 __hashtable_base::operator=(std::forward<_Ht>(__ht));
1184 _M_element_count = __ht._M_element_count;
1185 _M_rehash_policy = __ht._M_rehash_policy;
7cfe71d1 1186 __reuse_or_alloc_node_gen_t __roan(_M_begin(), *this);
01021139 1187 _M_before_begin._M_nxt = nullptr;
b9c84e95 1188 _M_assign(std::forward<_Ht>(__ht), __roan);
01021139
FD
1189 if (__former_buckets)
1190 _M_deallocate_buckets(__former_buckets, __former_bucket_count);
1191 }
1192 __catch(...)
1193 {
1194 if (__former_buckets)
1195 {
1196 // Restore previous buckets.
1197 _M_deallocate_buckets();
1198 _M_rehash_policy._M_reset(__former_state);
1199 _M_buckets = __former_buckets;
1200 _M_bucket_count = __former_bucket_count;
1201 }
1202 __builtin_memset(_M_buckets, 0,
1b6f0476 1203 _M_bucket_count * sizeof(__node_base_ptr));
01021139
FD
1204 __throw_exception_again;
1205 }
1206 }
0462b6aa 1207
4797a61c
FD
1208 template<typename _Key, typename _Value, typename _Alloc,
1209 typename _ExtractKey, typename _Equal,
1210 typename _Hash, typename _RangeHash, typename _Unused,
1211 typename _RehashPolicy, typename _Traits>
b9c84e95 1212 template<typename _Ht, typename _NodeGenerator>
0462b6aa
FD
1213 void
1214 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1215 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
b9c84e95 1216 _M_assign(_Ht&& __ht, const _NodeGenerator& __node_gen)
0462b6aa 1217 {
1b6f0476 1218 __buckets_ptr __buckets = nullptr;
0462b6aa 1219 if (!_M_buckets)
95539f21 1220 _M_buckets = __buckets = _M_allocate_buckets(_M_bucket_count);
0462b6aa
FD
1221
1222 __try
1223 {
b09bcf83 1224 if (!__ht._M_before_begin._M_nxt)
0462b6aa
FD
1225 return;
1226
1227 // First deal with the special first node pointed to by
1228 // _M_before_begin.
1b6f0476
FD
1229 __node_ptr __ht_n = __ht._M_begin();
1230 __node_ptr __this_n
b9c84e95 1231 = __node_gen(__fwd_value_for<_Ht>(__ht_n->_M_v()));
1b6f0476 1232 this->_M_copy_code(*__this_n, *__ht_n);
f9d98fa7 1233 _M_update_bbegin(__this_n);
0462b6aa
FD
1234
1235 // Then deal with other nodes.
1b6f0476 1236 __node_ptr __prev_n = __this_n;
0462b6aa
FD
1237 for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
1238 {
b9c84e95 1239 __this_n = __node_gen(__fwd_value_for<_Ht>(__ht_n->_M_v()));
0462b6aa 1240 __prev_n->_M_nxt = __this_n;
1b6f0476
FD
1241 this->_M_copy_code(*__this_n, *__ht_n);
1242 size_type __bkt = _M_bucket_index(*__this_n);
0462b6aa
FD
1243 if (!_M_buckets[__bkt])
1244 _M_buckets[__bkt] = __prev_n;
1245 __prev_n = __this_n;
1246 }
1247 }
1248 __catch(...)
1249 {
1250 clear();
1251 if (__buckets)
1252 _M_deallocate_buckets();
1253 __throw_exception_again;
1254 }
1255 }
1256
4797a61c
FD
1257 template<typename _Key, typename _Value, typename _Alloc,
1258 typename _ExtractKey, typename _Equal,
1259 typename _Hash, typename _RangeHash, typename _Unused,
1260 typename _RehashPolicy, typename _Traits>
0462b6aa
FD
1261 void
1262 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1263 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
0462b6aa
FD
1264 _M_reset() noexcept
1265 {
1266 _M_rehash_policy._M_reset();
95539f21
FD
1267 _M_bucket_count = 1;
1268 _M_single_bucket = nullptr;
1269 _M_buckets = &_M_single_bucket;
b09bcf83 1270 _M_before_begin._M_nxt = nullptr;
0462b6aa
FD
1271 _M_element_count = 0;
1272 }
1273
4797a61c
FD
1274 template<typename _Key, typename _Value, typename _Alloc,
1275 typename _ExtractKey, typename _Equal,
1276 typename _Hash, typename _RangeHash, typename _Unused,
1277 typename _RehashPolicy, typename _Traits>
0462b6aa
FD
1278 void
1279 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1280 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
7cfe71d1 1281 _M_move_assign(_Hashtable&& __ht, true_type)
0462b6aa 1282 {
c2fb0a1a
JW
1283 if (__builtin_expect(std::__addressof(__ht) == this, false))
1284 return;
1285
b09bcf83 1286 this->_M_deallocate_nodes(_M_begin());
95539f21 1287 _M_deallocate_buckets();
0462b6aa
FD
1288 __hashtable_base::operator=(std::move(__ht));
1289 _M_rehash_policy = __ht._M_rehash_policy;
95539f21
FD
1290 if (!__ht._M_uses_single_bucket())
1291 _M_buckets = __ht._M_buckets;
1292 else
1293 {
1294 _M_buckets = &_M_single_bucket;
1295 _M_single_bucket = __ht._M_single_bucket;
1296 }
f9d98fa7 1297
0462b6aa 1298 _M_bucket_count = __ht._M_bucket_count;
b09bcf83 1299 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
0462b6aa 1300 _M_element_count = __ht._M_element_count;
b09bcf83 1301 std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
0462b6aa 1302
f9d98fa7
FD
1303 // Fix bucket containing the _M_before_begin pointer that can't be moved.
1304 _M_update_bbegin();
0462b6aa
FD
1305 __ht._M_reset();
1306 }
1307
4797a61c
FD
1308 template<typename _Key, typename _Value, typename _Alloc,
1309 typename _ExtractKey, typename _Equal,
1310 typename _Hash, typename _RangeHash, typename _Unused,
1311 typename _RehashPolicy, typename _Traits>
0462b6aa
FD
1312 void
1313 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1314 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
7cfe71d1 1315 _M_move_assign(_Hashtable&& __ht, false_type)
0462b6aa 1316 {
b09bcf83 1317 if (__ht._M_node_allocator() == this->_M_node_allocator())
4797a61c 1318 _M_move_assign(std::move(__ht), true_type{});
0462b6aa
FD
1319 else
1320 {
1321 // Can't move memory, move elements then.
b9c84e95 1322 _M_assign_elements(std::move(__ht));
01021139 1323 __ht.clear();
0462b6aa
FD
1324 }
1325 }
1326
4797a61c
FD
1327 template<typename _Key, typename _Value, typename _Alloc,
1328 typename _ExtractKey, typename _Equal,
1329 typename _Hash, typename _RangeHash, typename _Unused,
1330 typename _RehashPolicy, typename _Traits>
4dad8b49 1331 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1332 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1333 _Hashtable(const _Hashtable& __ht)
4dad8b49
BK
1334 : __hashtable_base(__ht),
1335 __map_base(__ht),
1336 __rehash_base(__ht),
b09bcf83
FD
1337 __hashtable_alloc(
1338 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
da27f556 1339 _M_buckets(nullptr),
3b2524b1
PC
1340 _M_bucket_count(__ht._M_bucket_count),
1341 _M_element_count(__ht._M_element_count),
1342 _M_rehash_policy(__ht._M_rehash_policy)
1343 {
b9c84e95
FD
1344 __alloc_node_gen_t __alloc_node_gen(*this);
1345 _M_assign(__ht, __alloc_node_gen);
3b2524b1
PC
1346 }
1347
4797a61c
FD
1348 template<typename _Key, typename _Value, typename _Alloc,
1349 typename _ExtractKey, typename _Equal,
1350 typename _Hash, typename _RangeHash, typename _Unused,
1351 typename _RehashPolicy, typename _Traits>
4dad8b49 1352 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1353 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
12324b9a
FD
1354 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1355 true_type /* alloc always equal */)
4797a61c 1356 noexcept(std::is_nothrow_copy_constructible<_Hash>::value &&
12324b9a 1357 std::is_nothrow_copy_constructible<_Equal>::value)
4dad8b49
BK
1358 : __hashtable_base(__ht),
1359 __map_base(__ht),
1360 __rehash_base(__ht),
12324b9a 1361 __hashtable_alloc(std::move(__a)),
20b84bef 1362 _M_buckets(__ht._M_buckets),
3b2524b1 1363 _M_bucket_count(__ht._M_bucket_count),
b09bcf83 1364 _M_before_begin(__ht._M_before_begin._M_nxt),
3b2524b1 1365 _M_element_count(__ht._M_element_count),
20b84bef 1366 _M_rehash_policy(__ht._M_rehash_policy)
3b2524b1 1367 {
12324b9a 1368 // Update buckets if __ht is using its single bucket.
95539f21
FD
1369 if (__ht._M_uses_single_bucket())
1370 {
1371 _M_buckets = &_M_single_bucket;
1372 _M_single_bucket = __ht._M_single_bucket;
1373 }
1374
f9d98fa7
FD
1375 // Fix bucket containing the _M_before_begin pointer that can't be moved.
1376 _M_update_bbegin();
95539f21 1377
0462b6aa
FD
1378 __ht._M_reset();
1379 }
1380
4797a61c
FD
1381 template<typename _Key, typename _Value, typename _Alloc,
1382 typename _ExtractKey, typename _Equal,
1383 typename _Hash, typename _RangeHash, typename _Unused,
1384 typename _RehashPolicy, typename _Traits>
0462b6aa 1385 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1386 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
0462b6aa
FD
1387 _Hashtable(const _Hashtable& __ht, const allocator_type& __a)
1388 : __hashtable_base(__ht),
1389 __map_base(__ht),
1390 __rehash_base(__ht),
b09bcf83 1391 __hashtable_alloc(__node_alloc_type(__a)),
0462b6aa
FD
1392 _M_buckets(),
1393 _M_bucket_count(__ht._M_bucket_count),
0462b6aa
FD
1394 _M_element_count(__ht._M_element_count),
1395 _M_rehash_policy(__ht._M_rehash_policy)
1396 {
b9c84e95
FD
1397 __alloc_node_gen_t __alloc_node_gen(*this);
1398 _M_assign(__ht, __alloc_node_gen);
0462b6aa
FD
1399 }
1400
4797a61c
FD
1401 template<typename _Key, typename _Value, typename _Alloc,
1402 typename _ExtractKey, typename _Equal,
1403 typename _Hash, typename _RangeHash, typename _Unused,
1404 typename _RehashPolicy, typename _Traits>
0462b6aa 1405 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1406 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
12324b9a
FD
1407 _Hashtable(_Hashtable&& __ht, __node_alloc_type&& __a,
1408 false_type /* alloc always equal */)
0462b6aa
FD
1409 : __hashtable_base(__ht),
1410 __map_base(__ht),
1411 __rehash_base(__ht),
12324b9a 1412 __hashtable_alloc(std::move(__a)),
da27f556 1413 _M_buckets(nullptr),
0462b6aa 1414 _M_bucket_count(__ht._M_bucket_count),
0462b6aa
FD
1415 _M_element_count(__ht._M_element_count),
1416 _M_rehash_policy(__ht._M_rehash_policy)
1417 {
b09bcf83 1418 if (__ht._M_node_allocator() == this->_M_node_allocator())
0462b6aa 1419 {
95539f21
FD
1420 if (__ht._M_uses_single_bucket())
1421 {
1422 _M_buckets = &_M_single_bucket;
1423 _M_single_bucket = __ht._M_single_bucket;
1424 }
1425 else
1426 _M_buckets = __ht._M_buckets;
1427
f9d98fa7 1428 // Fix bucket containing the _M_before_begin pointer that can't be
0462b6aa 1429 // moved.
f9d98fa7
FD
1430 _M_update_bbegin(__ht._M_begin());
1431
0462b6aa
FD
1432 __ht._M_reset();
1433 }
1434 else
1435 {
b9c84e95
FD
1436 __alloc_node_gen_t __alloc_gen(*this);
1437
1438 using _Fwd_Ht = typename
1439 conditional<__move_if_noexcept_cond<value_type>::value,
1440 const _Hashtable&, _Hashtable&&>::type;
b32a3f32 1441 _M_assign(std::forward<_Fwd_Ht>(__ht), __alloc_gen);
0462b6aa
FD
1442 __ht.clear();
1443 }
3b2524b1
PC
1444 }
1445
4797a61c
FD
1446 template<typename _Key, typename _Value, typename _Alloc,
1447 typename _ExtractKey, typename _Equal,
1448 typename _Hash, typename _RangeHash, typename _Unused,
1449 typename _RehashPolicy, typename _Traits>
4dad8b49 1450 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1451 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
6f59ea25 1452 ~_Hashtable() noexcept
3b2524b1
PC
1453 {
1454 clear();
da27f556 1455 _M_deallocate_buckets();
3b2524b1
PC
1456 }
1457
4797a61c
FD
1458 template<typename _Key, typename _Value, typename _Alloc,
1459 typename _ExtractKey, typename _Equal,
1460 typename _Hash, typename _RangeHash, typename _Unused,
1461 typename _RehashPolicy, typename _Traits>
3b2524b1 1462 void
4dad8b49 1463 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1464 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1465 swap(_Hashtable& __x)
4797a61c
FD
1466 noexcept(__and_<__is_nothrow_swappable<_Hash>,
1467 __is_nothrow_swappable<_Equal>>::value)
3b2524b1 1468 {
4dad8b49
BK
1469 // The only base class with member variables is hash_code_base.
1470 // We define _Hash_code_base::_M_swap because different
1471 // specializations have different members.
a188284c 1472 this->_M_swap(__x);
3b2524b1 1473
b09bcf83 1474 std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
3b2524b1 1475 std::swap(_M_rehash_policy, __x._M_rehash_policy);
95539f21
FD
1476
1477 // Deal properly with potentially moved instances.
1478 if (this->_M_uses_single_bucket())
1479 {
1480 if (!__x._M_uses_single_bucket())
1481 {
1482 _M_buckets = __x._M_buckets;
1483 __x._M_buckets = &__x._M_single_bucket;
1484 }
1485 }
1486 else if (__x._M_uses_single_bucket())
1487 {
1488 __x._M_buckets = _M_buckets;
1489 _M_buckets = &_M_single_bucket;
1490 }
1491 else
1492 std::swap(_M_buckets, __x._M_buckets);
1493
3b2524b1 1494 std::swap(_M_bucket_count, __x._M_bucket_count);
b09bcf83 1495 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
3b2524b1 1496 std::swap(_M_element_count, __x._M_element_count);
95539f21 1497 std::swap(_M_single_bucket, __x._M_single_bucket);
4dad8b49 1498
0462b6aa
FD
1499 // Fix buckets containing the _M_before_begin pointers that can't be
1500 // swapped.
f9d98fa7
FD
1501 _M_update_bbegin();
1502 __x._M_update_bbegin();
3b2524b1
PC
1503 }
1504
4797a61c
FD
1505 template<typename _Key, typename _Value, typename _Alloc,
1506 typename _ExtractKey, typename _Equal,
1507 typename _Hash, typename _RangeHash, typename _Unused,
1508 typename _RehashPolicy, typename _Traits>
db97b3b0 1509 auto
4dad8b49 1510 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1511 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1512 find(const key_type& __k)
db97b3b0 1513 -> iterator
3b2524b1 1514 {
4dad8b49 1515 __hash_code __code = this->_M_hash_code(__k);
4797a61c 1516 std::size_t __bkt = _M_bucket_index(__code);
f9d98fa7 1517 return iterator(_M_find_node(__bkt, __k, __code));
3b2524b1
PC
1518 }
1519
4797a61c
FD
1520 template<typename _Key, typename _Value, typename _Alloc,
1521 typename _ExtractKey, typename _Equal,
1522 typename _Hash, typename _RangeHash, typename _Unused,
1523 typename _RehashPolicy, typename _Traits>
db97b3b0 1524 auto
4dad8b49 1525 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1526 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1527 find(const key_type& __k) const
db97b3b0 1528 -> const_iterator
3b2524b1 1529 {
4dad8b49 1530 __hash_code __code = this->_M_hash_code(__k);
4797a61c 1531 std::size_t __bkt = _M_bucket_index(__code);
f9d98fa7 1532 return const_iterator(_M_find_node(__bkt, __k, __code));
3b2524b1
PC
1533 }
1534
4797a61c
FD
1535 template<typename _Key, typename _Value, typename _Alloc,
1536 typename _ExtractKey, typename _Equal,
1537 typename _Hash, typename _RangeHash, typename _Unused,
1538 typename _RehashPolicy, typename _Traits>
db97b3b0 1539 auto
4dad8b49 1540 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1541 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1542 count(const key_type& __k) const
db97b3b0 1543 -> size_type
3b2524b1 1544 {
f9d98fa7
FD
1545 auto __it = find(__k);
1546 if (!__it._M_cur)
da29608a
FD
1547 return 0;
1548
f9d98fa7
FD
1549 if (__unique_keys::value)
1550 return 1;
1551
1552 // All equivalent values are next to each other, if we find a
1553 // non-equivalent value after an equivalent one it means that we won't
1554 // find any new equivalent value.
1555 size_type __result = 1;
1556 for (auto __ref = __it++;
1b6f0476 1557 __it._M_cur && this->_M_node_equals(*__ref._M_cur, *__it._M_cur);
f9d98fa7
FD
1558 ++__it)
1559 ++__result;
1560
3b2524b1
PC
1561 return __result;
1562 }
1563
4797a61c
FD
1564 template<typename _Key, typename _Value, typename _Alloc,
1565 typename _ExtractKey, typename _Equal,
1566 typename _Hash, typename _RangeHash, typename _Unused,
1567 typename _RehashPolicy, typename _Traits>
db97b3b0 1568 auto
4dad8b49 1569 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1570 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1571 equal_range(const key_type& __k)
db97b3b0 1572 -> pair<iterator, iterator>
3b2524b1 1573 {
f9d98fa7
FD
1574 auto __ite = find(__k);
1575 if (!__ite._M_cur)
1576 return { __ite, __ite };
7c3e9502 1577
f9d98fa7
FD
1578 auto __beg = __ite++;
1579 if (__unique_keys::value)
1580 return { __beg, __ite };
3b2524b1 1581
f9d98fa7
FD
1582 // All equivalent values are next to each other, if we find a
1583 // non-equivalent value after an equivalent one it means that we won't
1584 // find any new equivalent value.
1b6f0476 1585 while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
f9d98fa7
FD
1586 ++__ite;
1587
1588 return { __beg, __ite };
3b2524b1
PC
1589 }
1590
4797a61c
FD
1591 template<typename _Key, typename _Value, typename _Alloc,
1592 typename _ExtractKey, typename _Equal,
1593 typename _Hash, typename _RangeHash, typename _Unused,
1594 typename _RehashPolicy, typename _Traits>
db97b3b0 1595 auto
4dad8b49 1596 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1597 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1598 equal_range(const key_type& __k) const
db97b3b0 1599 -> pair<const_iterator, const_iterator>
3b2524b1 1600 {
f9d98fa7
FD
1601 auto __ite = find(__k);
1602 if (!__ite._M_cur)
1603 return { __ite, __ite };
3b2524b1 1604
f9d98fa7
FD
1605 auto __beg = __ite++;
1606 if (__unique_keys::value)
1607 return { __beg, __ite };
3b2524b1 1608
f9d98fa7
FD
1609 // All equivalent values are next to each other, if we find a
1610 // non-equivalent value after an equivalent one it means that we won't
1611 // find any new equivalent value.
1b6f0476 1612 while (__ite._M_cur && this->_M_node_equals(*__beg._M_cur, *__ite._M_cur))
f9d98fa7
FD
1613 ++__ite;
1614
1615 return { __beg, __ite };
3b2524b1
PC
1616 }
1617
f9d98fa7
FD
1618 // Find the node before the one whose key compares equal to k in the bucket
1619 // bkt. Return nullptr if no node is found.
4797a61c
FD
1620 template<typename _Key, typename _Value, typename _Alloc,
1621 typename _ExtractKey, typename _Equal,
1622 typename _Hash, typename _RangeHash, typename _Unused,
1623 typename _RehashPolicy, typename _Traits>
db97b3b0 1624 auto
4dad8b49 1625 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1626 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
7cfe71d1 1627 _M_find_before_node(size_type __bkt, const key_type& __k,
4dad8b49 1628 __hash_code __code) const
1b6f0476 1629 -> __node_base_ptr
3b2524b1 1630 {
1b6f0476 1631 __node_base_ptr __prev_p = _M_buckets[__bkt];
f86b266c 1632 if (!__prev_p)
da29608a 1633 return nullptr;
b3abc9d8 1634
1b6f0476 1635 for (__node_ptr __p = static_cast<__node_ptr>(__prev_p->_M_nxt);;
b3abc9d8 1636 __p = __p->_M_next())
da29608a 1637 {
1b6f0476 1638 if (this->_M_equals(__k, __code, *__p))
f86b266c 1639 return __prev_p;
b3abc9d8 1640
1b6f0476 1641 if (!__p->_M_nxt || _M_bucket_index(*__p->_M_next()) != __bkt)
da29608a 1642 break;
f86b266c 1643 __prev_p = __p;
da29608a 1644 }
f9d98fa7 1645
919f402c 1646 return nullptr;
3b2524b1
PC
1647 }
1648
4797a61c
FD
1649 template<typename _Key, typename _Value, typename _Alloc,
1650 typename _ExtractKey, typename _Equal,
1651 typename _Hash, typename _RangeHash, typename _Unused,
1652 typename _RehashPolicy, typename _Traits>
da29608a 1653 void
4dad8b49 1654 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1655 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1b6f0476 1656 _M_insert_bucket_begin(size_type __bkt, __node_ptr __node)
da29608a 1657 {
f86b266c 1658 if (_M_buckets[__bkt])
da29608a 1659 {
4dad8b49
BK
1660 // Bucket is not empty, we just need to insert the new node
1661 // after the bucket before begin.
1662 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
1663 _M_buckets[__bkt]->_M_nxt = __node;
da29608a
FD
1664 }
1665 else
1666 {
4dad8b49 1667 // The bucket is empty, the new node is inserted at the
207585a6 1668 // beginning of the singly-linked list and the bucket will
4dad8b49 1669 // contain _M_before_begin pointer.
b09bcf83
FD
1670 __node->_M_nxt = _M_before_begin._M_nxt;
1671 _M_before_begin._M_nxt = __node;
f9d98fa7 1672
4dad8b49 1673 if (__node->_M_nxt)
f86b266c
FD
1674 // We must update former begin bucket that is pointing to
1675 // _M_before_begin.
1b6f0476 1676 _M_buckets[_M_bucket_index(*__node->_M_next())] = __node;
f9d98fa7 1677
b09bcf83 1678 _M_buckets[__bkt] = &_M_before_begin;
da29608a 1679 }
da29608a
FD
1680 }
1681
4797a61c
FD
1682 template<typename _Key, typename _Value, typename _Alloc,
1683 typename _ExtractKey, typename _Equal,
1684 typename _Hash, typename _RangeHash, typename _Unused,
1685 typename _RehashPolicy, typename _Traits>
da29608a 1686 void
4dad8b49 1687 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1688 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1b6f0476 1689 _M_remove_bucket_begin(size_type __bkt, __node_ptr __next,
4dad8b49 1690 size_type __next_bkt)
da29608a
FD
1691 {
1692 if (!__next || __next_bkt != __bkt)
1693 {
1694 // Bucket is now empty
f86b266c
FD
1695 // First update next bucket if any
1696 if (__next)
da29608a 1697 _M_buckets[__next_bkt] = _M_buckets[__bkt];
4dad8b49 1698
f86b266c 1699 // Second update before begin node if necessary
b09bcf83
FD
1700 if (&_M_before_begin == _M_buckets[__bkt])
1701 _M_before_begin._M_nxt = __next;
da29608a 1702 _M_buckets[__bkt] = nullptr;
da29608a 1703 }
da29608a
FD
1704 }
1705
4797a61c
FD
1706 template<typename _Key, typename _Value, typename _Alloc,
1707 typename _ExtractKey, typename _Equal,
1708 typename _Hash, typename _RangeHash, typename _Unused,
1709 typename _RehashPolicy, typename _Traits>
db97b3b0 1710 auto
4dad8b49 1711 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1712 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1b6f0476
FD
1713 _M_get_previous_node(size_type __bkt, __node_ptr __n)
1714 -> __node_base_ptr
da29608a 1715 {
1b6f0476 1716 __node_base_ptr __prev_n = _M_buckets[__bkt];
f86b266c
FD
1717 while (__prev_n->_M_nxt != __n)
1718 __prev_n = __prev_n->_M_nxt;
da29608a
FD
1719 return __prev_n;
1720 }
1721
4797a61c
FD
1722 template<typename _Key, typename _Value, typename _Alloc,
1723 typename _ExtractKey, typename _Equal,
1724 typename _Hash, typename _RangeHash, typename _Unused,
1725 typename _RehashPolicy, typename _Traits>
9f285ccb 1726 template<typename... _Args>
db97b3b0 1727 auto
4dad8b49 1728 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1729 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1730 _M_emplace(true_type /* __uks */, _Args&&... __args)
9f285ccb 1731 -> pair<iterator, bool>
9b81593b
FD
1732 {
1733 // First build the node to get access to the hash code
b0c849fa 1734 _Scoped_node __node { this, std::forward<_Args>(__args)... };
4797a61c 1735 const key_type& __k = _ExtractKey{}(__node._M_node->_M_v());
b0c849fa 1736 __hash_code __code = this->_M_hash_code(__k);
4797a61c 1737 size_type __bkt = _M_bucket_index(__code);
1b6f0476 1738 if (__node_ptr __p = _M_find_node(__bkt, __k, __code))
b0c849fa
FD
1739 // There is already an equivalent node, no insertion
1740 return std::make_pair(iterator(__p), false);
181a5a13
FD
1741
1742 // Insert the node
4797a61c 1743 auto __pos = _M_insert_unique_node(__bkt, __code, __node._M_node);
b0c849fa
FD
1744 __node._M_node = nullptr;
1745 return { __pos, true };
9b81593b
FD
1746 }
1747
4797a61c
FD
1748 template<typename _Key, typename _Value, typename _Alloc,
1749 typename _ExtractKey, typename _Equal,
1750 typename _Hash, typename _RangeHash, typename _Unused,
1751 typename _RehashPolicy, typename _Traits>
9b81593b 1752 template<typename... _Args>
db97b3b0 1753 auto
4dad8b49 1754 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1755 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1756 _M_emplace(const_iterator __hint, false_type /* __uks */,
1757 _Args&&... __args)
db97b3b0 1758 -> iterator
9b81593b 1759 {
f86b266c 1760 // First build the node to get its hash code.
b0c849fa 1761 _Scoped_node __node { this, std::forward<_Args>(__args)... };
4797a61c 1762 const key_type& __k = _ExtractKey{}(__node._M_node->_M_v());
181a5a13 1763
b0c849fa
FD
1764 __hash_code __code = this->_M_hash_code(__k);
1765 auto __pos
4797a61c 1766 = _M_insert_multi_node(__hint._M_cur, __code, __node._M_node);
b0c849fa
FD
1767 __node._M_node = nullptr;
1768 return __pos;
9b81593b
FD
1769 }
1770
4797a61c
FD
1771 template<typename _Key, typename _Value, typename _Alloc,
1772 typename _ExtractKey, typename _Equal,
1773 typename _Hash, typename _RangeHash, typename _Unused,
1774 typename _RehashPolicy, typename _Traits>
db97b3b0 1775 auto
181a5a13 1776 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1777 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1778 _M_insert_unique_node(size_type __bkt, __hash_code __code,
1b6f0476 1779 __node_ptr __node, size_type __n_elt)
db97b3b0 1780 -> iterator
181a5a13
FD
1781 {
1782 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1783 std::pair<bool, std::size_t> __do_rehash
0f146257
FD
1784 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count,
1785 __n_elt);
3b2524b1 1786
b0c849fa 1787 if (__do_rehash.first)
181a5a13 1788 {
b0c849fa 1789 _M_rehash(__do_rehash.second, __saved_state);
4797a61c 1790 __bkt = _M_bucket_index(__code);
b0c849fa 1791 }
3b2524b1 1792
1b6f0476 1793 this->_M_store_code(*__node, __code);
181a5a13 1794
b0c849fa
FD
1795 // Always insert at the beginning of the bucket.
1796 _M_insert_bucket_begin(__bkt, __node);
1797 ++_M_element_count;
1798 return iterator(__node);
181a5a13
FD
1799 }
1800
4797a61c
FD
1801 template<typename _Key, typename _Value, typename _Alloc,
1802 typename _ExtractKey, typename _Equal,
1803 typename _Hash, typename _RangeHash, typename _Unused,
1804 typename _RehashPolicy, typename _Traits>
db97b3b0 1805 auto
181a5a13 1806 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1807 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1b6f0476
FD
1808 _M_insert_multi_node(__node_ptr __hint,
1809 __hash_code __code, __node_ptr __node)
db97b3b0 1810 -> iterator
181a5a13
FD
1811 {
1812 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1813 std::pair<bool, std::size_t> __do_rehash
1814 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1815
b0c849fa
FD
1816 if (__do_rehash.first)
1817 _M_rehash(__do_rehash.second, __saved_state);
1818
1b6f0476 1819 this->_M_store_code(*__node, __code);
4797a61c
FD
1820 const key_type& __k = _ExtractKey{}(__node->_M_v());
1821 size_type __bkt = _M_bucket_index(__code);
b0c849fa
FD
1822
1823 // Find the node before an equivalent one or use hint if it exists and
1824 // if it is equivalent.
1b6f0476 1825 __node_base_ptr __prev
b0c849fa 1826 = __builtin_expect(__hint != nullptr, false)
1b6f0476 1827 && this->_M_equals(__k, __code, *__hint)
b0c849fa
FD
1828 ? __hint
1829 : _M_find_before_node(__bkt, __k, __code);
1b6f0476 1830
b0c849fa 1831 if (__prev)
181a5a13 1832 {
b0c849fa
FD
1833 // Insert after the node before the equivalent one.
1834 __node->_M_nxt = __prev->_M_nxt;
1835 __prev->_M_nxt = __node;
1836 if (__builtin_expect(__prev == __hint, false))
1837 // hint might be the last bucket node, in this case we need to
1838 // update next bucket.
1839 if (__node->_M_nxt
1b6f0476 1840 && !this->_M_equals(__k, __code, *__node->_M_next()))
b0c849fa 1841 {
1b6f0476 1842 size_type __next_bkt = _M_bucket_index(*__node->_M_next());
b0c849fa
FD
1843 if (__next_bkt != __bkt)
1844 _M_buckets[__next_bkt] = __node;
1845 }
181a5a13 1846 }
b0c849fa
FD
1847 else
1848 // The inserted node has no equivalent in the hashtable. We must
1849 // insert the new node at the beginning of the bucket to preserve
1850 // equivalent elements' relative positions.
1851 _M_insert_bucket_begin(__bkt, __node);
1852 ++_M_element_count;
1853 return iterator(__node);
181a5a13 1854 }
3b2524b1
PC
1855
1856 // Insert v if no element with its key is already present.
4797a61c
FD
1857 template<typename _Key, typename _Value, typename _Alloc,
1858 typename _ExtractKey, typename _Equal,
1859 typename _Hash, typename _RangeHash, typename _Unused,
1860 typename _RehashPolicy, typename _Traits>
b3abc9d8 1861 template<typename _Arg, typename _NodeGenerator>
db97b3b0 1862 auto
4dad8b49 1863 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1864 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
6dcf0423
FD
1865 _M_insert(_Arg&& __v, const _NodeGenerator& __node_gen,
1866 true_type /* __uks */)
db97b3b0 1867 -> pair<iterator, bool>
fb7342fd 1868 {
4797a61c 1869 const key_type& __k = _ExtractKey{}(__v);
4dad8b49 1870 __hash_code __code = this->_M_hash_code(__k);
4797a61c 1871 size_type __bkt = _M_bucket_index(__code);
181a5a13 1872
1b6f0476 1873 if (__node_ptr __node = _M_find_node(__bkt, __k, __code))
b0c849fa 1874 return { iterator(__node), false };
fb7342fd 1875
b0c849fa
FD
1876 _Scoped_node __node{ __node_gen(std::forward<_Arg>(__v)), this };
1877 auto __pos
4797a61c 1878 = _M_insert_unique_node(__bkt, __code, __node._M_node);
b0c849fa
FD
1879 __node._M_node = nullptr;
1880 return { __pos, true };
fb7342fd 1881 }
3b2524b1
PC
1882
1883 // Insert v unconditionally.
4797a61c
FD
1884 template<typename _Key, typename _Value, typename _Alloc,
1885 typename _ExtractKey, typename _Equal,
1886 typename _Hash, typename _RangeHash, typename _Unused,
1887 typename _RehashPolicy, typename _Traits>
b3abc9d8 1888 template<typename _Arg, typename _NodeGenerator>
db97b3b0 1889 auto
4dad8b49 1890 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1891 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
b3abc9d8 1892 _M_insert(const_iterator __hint, _Arg&& __v,
6dcf0423
FD
1893 const _NodeGenerator& __node_gen,
1894 false_type /* __uks */)
db97b3b0 1895 -> iterator
fb7342fd 1896 {
181a5a13
FD
1897 // First compute the hash code so that we don't do anything if it
1898 // throws.
4797a61c 1899 __hash_code __code = this->_M_hash_code(_ExtractKey{}(__v));
3b2524b1 1900
181a5a13 1901 // Second allocate new node so that we don't rehash if it throws.
b0c849fa 1902 _Scoped_node __node{ __node_gen(std::forward<_Arg>(__v)), this };
b0c849fa 1903 auto __pos
4797a61c 1904 = _M_insert_multi_node(__hint._M_cur, __code, __node._M_node);
b0c849fa
FD
1905 __node._M_node = nullptr;
1906 return __pos;
181a5a13 1907 }
3b2524b1 1908
4797a61c
FD
1909 template<typename _Key, typename _Value, typename _Alloc,
1910 typename _ExtractKey, typename _Equal,
1911 typename _Hash, typename _RangeHash, typename _Unused,
1912 typename _RehashPolicy, typename _Traits>
db97b3b0 1913 auto
4dad8b49 1914 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1915 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 1916 erase(const_iterator __it)
db97b3b0 1917 -> iterator
3b2524b1 1918 {
1b6f0476
FD
1919 __node_ptr __n = __it._M_cur;
1920 std::size_t __bkt = _M_bucket_index(*__n);
da29608a 1921
4dad8b49
BK
1922 // Look for previous node to unlink it from the erased one, this
1923 // is why we need buckets to contain the before begin to make
207585a6 1924 // this search fast.
1b6f0476 1925 __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
31578792
FD
1926 return _M_erase(__bkt, __prev_n, __n);
1927 }
1928
4797a61c
FD
1929 template<typename _Key, typename _Value, typename _Alloc,
1930 typename _ExtractKey, typename _Equal,
1931 typename _Hash, typename _RangeHash, typename _Unused,
1932 typename _RehashPolicy, typename _Traits>
db97b3b0 1933 auto
31578792 1934 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 1935 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1b6f0476 1936 _M_erase(size_type __bkt, __node_base_ptr __prev_n, __node_ptr __n)
db97b3b0 1937 -> iterator
31578792
FD
1938 {
1939 if (__prev_n == _M_buckets[__bkt])
f86b266c 1940 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1b6f0476 1941 __n->_M_nxt ? _M_bucket_index(*__n->_M_next()) : 0);
f86b266c 1942 else if (__n->_M_nxt)
18dbb859 1943 {
1b6f0476 1944 size_type __next_bkt = _M_bucket_index(*__n->_M_next());
da29608a
FD
1945 if (__next_bkt != __bkt)
1946 _M_buckets[__next_bkt] = __prev_n;
18dbb859
PC
1947 }
1948
f86b266c
FD
1949 __prev_n->_M_nxt = __n->_M_nxt;
1950 iterator __result(__n->_M_next());
b09bcf83 1951 this->_M_deallocate_node(__n);
18dbb859
PC
1952 --_M_element_count;
1953
d723ced2 1954 return __result;
3b2524b1
PC
1955 }
1956
4797a61c
FD
1957 template<typename _Key, typename _Value, typename _Alloc,
1958 typename _ExtractKey, typename _Equal,
1959 typename _Hash, typename _RangeHash, typename _Unused,
1960 typename _RehashPolicy, typename _Traits>
db97b3b0 1961 auto
4dad8b49 1962 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1963 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1964 _M_erase(true_type /* __uks */, const key_type& __k)
db97b3b0 1965 -> size_type
31578792
FD
1966 {
1967 __hash_code __code = this->_M_hash_code(__k);
4797a61c 1968 std::size_t __bkt = _M_bucket_index(__code);
31578792
FD
1969
1970 // Look for the node before the first matching node.
1b6f0476 1971 __node_base_ptr __prev_n = _M_find_before_node(__bkt, __k, __code);
31578792
FD
1972 if (!__prev_n)
1973 return 0;
1974
1975 // We found a matching node, erase it.
1b6f0476 1976 __node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);
31578792
FD
1977 _M_erase(__bkt, __prev_n, __n);
1978 return 1;
1979 }
1980
4797a61c
FD
1981 template<typename _Key, typename _Value, typename _Alloc,
1982 typename _ExtractKey, typename _Equal,
1983 typename _Hash, typename _RangeHash, typename _Unused,
1984 typename _RehashPolicy, typename _Traits>
db97b3b0 1985 auto
31578792 1986 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
1987 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
1988 _M_erase(false_type /* __uks */, const key_type& __k)
db97b3b0 1989 -> size_type
3b2524b1 1990 {
4dad8b49 1991 __hash_code __code = this->_M_hash_code(__k);
4797a61c 1992 std::size_t __bkt = _M_bucket_index(__code);
4dad8b49 1993
f86b266c 1994 // Look for the node before the first matching node.
1b6f0476 1995 __node_base_ptr __prev_n = _M_find_before_node(__bkt, __k, __code);
f86b266c 1996 if (!__prev_n)
da29608a 1997 return 0;
31578792
FD
1998
1999 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2000 // 526. Is it undefined if a function in the standard changes
2001 // in parameters?
2002 // We use one loop to find all matching nodes and another to deallocate
2003 // them so that the key stays valid during the first loop. It might be
2004 // invalidated indirectly when destroying nodes.
1b6f0476
FD
2005 __node_ptr __n = static_cast<__node_ptr>(__prev_n->_M_nxt);
2006 __node_ptr __n_last = __n->_M_next();
2007 while (__n_last && this->_M_node_equals(*__n, *__n_last))
f9d98fa7
FD
2008 __n_last = __n_last->_M_next();
2009
1b6f0476 2010 std::size_t __n_last_bkt = __n_last ? _M_bucket_index(*__n_last) : __bkt;
3b2524b1 2011
31578792 2012 // Deallocate nodes.
da29608a 2013 size_type __result = 0;
da29608a 2014 do
3b2524b1 2015 {
1b6f0476 2016 __node_ptr __p = __n->_M_next();
b09bcf83 2017 this->_M_deallocate_node(__n);
31578792 2018 __n = __p;
3b2524b1
PC
2019 ++__result;
2020 }
31578792
FD
2021 while (__n != __n_last);
2022
f9d98fa7 2023 _M_element_count -= __result;
31578792
FD
2024 if (__prev_n == _M_buckets[__bkt])
2025 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
f9d98fa7 2026 else if (__n_last_bkt != __bkt)
31578792
FD
2027 _M_buckets[__n_last_bkt] = __prev_n;
2028 __prev_n->_M_nxt = __n_last;
3b2524b1
PC
2029 return __result;
2030 }
2031
4797a61c
FD
2032 template<typename _Key, typename _Value, typename _Alloc,
2033 typename _ExtractKey, typename _Equal,
2034 typename _Hash, typename _RangeHash, typename _Unused,
2035 typename _RehashPolicy, typename _Traits>
db97b3b0 2036 auto
4dad8b49 2037 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 2038 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
3b2524b1 2039 erase(const_iterator __first, const_iterator __last)
db97b3b0 2040 -> iterator
3b2524b1 2041 {
1b6f0476
FD
2042 __node_ptr __n = __first._M_cur;
2043 __node_ptr __last_n = __last._M_cur;
da29608a
FD
2044 if (__n == __last_n)
2045 return iterator(__n);
2046
1b6f0476 2047 std::size_t __bkt = _M_bucket_index(*__n);
da29608a 2048
1b6f0476 2049 __node_base_ptr __prev_n = _M_get_previous_node(__bkt, __n);
da29608a
FD
2050 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
2051 std::size_t __n_bkt = __bkt;
2052 for (;;)
2053 {
2054 do
2055 {
1b6f0476 2056 __node_ptr __tmp = __n;
f86b266c 2057 __n = __n->_M_next();
b09bcf83 2058 this->_M_deallocate_node(__tmp);
da29608a
FD
2059 --_M_element_count;
2060 if (!__n)
2061 break;
1b6f0476 2062 __n_bkt = _M_bucket_index(*__n);
da29608a
FD
2063 }
2064 while (__n != __last_n && __n_bkt == __bkt);
2065 if (__is_bucket_begin)
2066 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
2067 if (__n == __last_n)
2068 break;
2069 __is_bucket_begin = true;
2070 __bkt = __n_bkt;
2071 }
2072
ac1384b7 2073 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
da29608a 2074 _M_buckets[__n_bkt] = __prev_n;
f86b266c 2075 __prev_n->_M_nxt = __n;
da29608a 2076 return iterator(__n);
d723ced2 2077 }
3b2524b1 2078
4797a61c
FD
2079 template<typename _Key, typename _Value, typename _Alloc,
2080 typename _ExtractKey, typename _Equal,
2081 typename _Hash, typename _RangeHash, typename _Unused,
2082 typename _RehashPolicy, typename _Traits>
3b2524b1 2083 void
4dad8b49 2084 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 2085 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
d3677132 2086 clear() noexcept
3b2524b1 2087 {
b09bcf83 2088 this->_M_deallocate_nodes(_M_begin());
1b6f0476
FD
2089 __builtin_memset(_M_buckets, 0,
2090 _M_bucket_count * sizeof(__node_base_ptr));
3b2524b1 2091 _M_element_count = 0;
b09bcf83 2092 _M_before_begin._M_nxt = nullptr;
3b2524b1 2093 }
7c3e9502 2094
4797a61c
FD
2095 template<typename _Key, typename _Value, typename _Alloc,
2096 typename _ExtractKey, typename _Equal,
2097 typename _Hash, typename _RangeHash, typename _Unused,
2098 typename _RehashPolicy, typename _Traits>
3b2524b1 2099 void
4dad8b49 2100 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 2101 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
7cfe71d1 2102 rehash(size_type __bkt_count)
3b2524b1 2103 {
4dad8b49 2104 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
7cfe71d1 2105 __bkt_count
d4a7f7a1 2106 = std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
7cfe71d1
FD
2107 __bkt_count);
2108 __bkt_count = _M_rehash_policy._M_next_bkt(__bkt_count);
2514d7f1 2109
7cfe71d1
FD
2110 if (__bkt_count != _M_bucket_count)
2111 _M_rehash(__bkt_count, __saved_state);
ecf07a67 2112 else
de6f5f57
FD
2113 // No rehash, restore previous state to keep it consistent with
2114 // container state.
ecf07a67 2115 _M_rehash_policy._M_reset(__saved_state);
3b2524b1
PC
2116 }
2117
4797a61c
FD
2118 template<typename _Key, typename _Value, typename _Alloc,
2119 typename _ExtractKey, typename _Equal,
2120 typename _Hash, typename _RangeHash, typename _Unused,
2121 typename _RehashPolicy, typename _Traits>
3b2524b1 2122 void
4dad8b49 2123 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c 2124 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
7cfe71d1 2125 _M_rehash(size_type __bkt_count, const __rehash_state& __state)
3b2524b1 2126 {
3b2524b1
PC
2127 __try
2128 {
4797a61c 2129 _M_rehash_aux(__bkt_count, __unique_keys{});
ac0ab963
FD
2130 }
2131 __catch(...)
2132 {
2133 // A failure here means that buckets allocation failed. We only
2134 // have to restore hash policy previous state.
2135 _M_rehash_policy._M_reset(__state);
2136 __throw_exception_again;
2137 }
2138 }
2139
2140 // Rehash when there is no equivalent elements.
4797a61c
FD
2141 template<typename _Key, typename _Value, typename _Alloc,
2142 typename _ExtractKey, typename _Equal,
2143 typename _Hash, typename _RangeHash, typename _Unused,
2144 typename _RehashPolicy, typename _Traits>
ac0ab963 2145 void
4dad8b49 2146 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
2147 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2148 _M_rehash_aux(size_type __bkt_count, true_type /* __uks */)
ac0ab963 2149 {
1b6f0476
FD
2150 __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2151 __node_ptr __p = _M_begin();
b09bcf83 2152 _M_before_begin._M_nxt = nullptr;
25caa91e 2153 std::size_t __bbegin_bkt = 0;
ac0ab963
FD
2154 while (__p)
2155 {
1b6f0476 2156 __node_ptr __next = __p->_M_next();
7cfe71d1 2157 std::size_t __bkt
1b6f0476 2158 = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
ac0ab963 2159 if (!__new_buckets[__bkt])
da29608a 2160 {
b09bcf83
FD
2161 __p->_M_nxt = _M_before_begin._M_nxt;
2162 _M_before_begin._M_nxt = __p;
2163 __new_buckets[__bkt] = &_M_before_begin;
ac0ab963
FD
2164 if (__p->_M_nxt)
2165 __new_buckets[__bbegin_bkt] = __p;
2166 __bbegin_bkt = __bkt;
2167 }
2168 else
2169 {
2170 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2171 __new_buckets[__bkt]->_M_nxt = __p;
2172 }
f9d98fa7 2173
ac0ab963
FD
2174 __p = __next;
2175 }
0462b6aa 2176
95539f21 2177 _M_deallocate_buckets();
7cfe71d1 2178 _M_bucket_count = __bkt_count;
ac0ab963
FD
2179 _M_buckets = __new_buckets;
2180 }
2181
2182 // Rehash when there can be equivalent elements, preserve their relative
2183 // order.
4797a61c
FD
2184 template<typename _Key, typename _Value, typename _Alloc,
2185 typename _ExtractKey, typename _Equal,
2186 typename _Hash, typename _RangeHash, typename _Unused,
2187 typename _RehashPolicy, typename _Traits>
ac0ab963 2188 void
4dad8b49 2189 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
4797a61c
FD
2190 _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::
2191 _M_rehash_aux(size_type __bkt_count, false_type /* __uks */)
ac0ab963 2192 {
1b6f0476
FD
2193 __buckets_ptr __new_buckets = _M_allocate_buckets(__bkt_count);
2194 __node_ptr __p = _M_begin();
b09bcf83 2195 _M_before_begin._M_nxt = nullptr;
25caa91e
PC
2196 std::size_t __bbegin_bkt = 0;
2197 std::size_t __prev_bkt = 0;
1b6f0476 2198 __node_ptr __prev_p = nullptr;
ac0ab963
FD
2199 bool __check_bucket = false;
2200
2201 while (__p)
2202 {
1b6f0476 2203 __node_ptr __next = __p->_M_next();
7cfe71d1 2204 std::size_t __bkt
1b6f0476 2205 = __hash_code_base::_M_bucket_index(*__p, __bkt_count);
ac0ab963 2206
b7a9facb 2207 if (__prev_p && __prev_bkt == __bkt)
ac0ab963 2208 {
b7a9facb
FD
2209 // Previous insert was already in this bucket, we insert after
2210 // the previously inserted one to preserve equivalent elements
2211 // relative order.
2212 __p->_M_nxt = __prev_p->_M_nxt;
2213 __prev_p->_M_nxt = __p;
2214
2215 // Inserting after a node in a bucket require to check that we
2216 // haven't change the bucket last node, in this case next
2217 // bucket containing its before begin node must be updated. We
2218 // schedule a check as soon as we move out of the sequence of
2219 // equivalent nodes to limit the number of checks.
2220 __check_bucket = true;
ac0ab963
FD
2221 }
2222 else
2223 {
b7a9facb 2224 if (__check_bucket)
f86b266c 2225 {
25caa91e
PC
2226 // Check if we shall update the next bucket because of
2227 // insertions into __prev_bkt bucket.
b7a9facb
FD
2228 if (__prev_p->_M_nxt)
2229 {
2230 std::size_t __next_bkt
1b6f0476
FD
2231 = __hash_code_base::_M_bucket_index(
2232 *__prev_p->_M_next(), __bkt_count);
b7a9facb
FD
2233 if (__next_bkt != __prev_bkt)
2234 __new_buckets[__next_bkt] = __prev_p;
2235 }
2236 __check_bucket = false;
2237 }
2238
2239 if (!__new_buckets[__bkt])
2240 {
b09bcf83
FD
2241 __p->_M_nxt = _M_before_begin._M_nxt;
2242 _M_before_begin._M_nxt = __p;
2243 __new_buckets[__bkt] = &_M_before_begin;
b7a9facb
FD
2244 if (__p->_M_nxt)
2245 __new_buckets[__bbegin_bkt] = __p;
2246 __bbegin_bkt = __bkt;
f86b266c
FD
2247 }
2248 else
2249 {
ac0ab963
FD
2250 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2251 __new_buckets[__bkt]->_M_nxt = __p;
f86b266c 2252 }
da29608a 2253 }
ac0ab963
FD
2254 __prev_p = __p;
2255 __prev_bkt = __bkt;
2256 __p = __next;
3b2524b1 2257 }
ac0ab963
FD
2258
2259 if (__check_bucket && __prev_p->_M_nxt)
3b2524b1 2260 {
ac0ab963 2261 std::size_t __next_bkt
1b6f0476 2262 = __hash_code_base::_M_bucket_index(*__prev_p->_M_next(),
7cfe71d1 2263 __bkt_count);
ac0ab963
FD
2264 if (__next_bkt != __prev_bkt)
2265 __new_buckets[__next_bkt] = __prev_p;
3b2524b1 2266 }
ac0ab963 2267
95539f21 2268 _M_deallocate_buckets();
7cfe71d1 2269 _M_bucket_count = __bkt_count;
ac0ab963 2270 _M_buckets = __new_buckets;
3b2524b1 2271 }
7c3e9502 2272
2dbe56bd
JW
2273#if __cplusplus > 201402L
2274 template<typename, typename, typename> class _Hash_merge_helper { };
2275#endif // C++17
2276
08abbdda
JW
2277#if __cpp_deduction_guides >= 201606
2278 // Used to constrain deduction guides
2279 template<typename _Hash>
2280 using _RequireNotAllocatorOrIntegral
2281 = __enable_if_t<!__or_<is_integral<_Hash>, __is_allocator<_Hash>>::value>;
2282#endif
2283
12ffa228
BK
2284_GLIBCXX_END_NAMESPACE_VERSION
2285} // namespace std
3b2524b1
PC
2286
2287#endif // _HASHTABLE_H