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