* include/bits/regex_automaton.h (__detail::_State): Split
[official-gcc.git] / libstdc++-v3 / include / bits / hashtable.h
blobaae146b41be40a089612f3255fc6e94c6ccc295f
1 // hashtable.h header -*- C++ -*-
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.
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.
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.
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/>.
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}
30 #ifndef _HASHTABLE_H
31 #define _HASHTABLE_H 1
33 #pragma GCC system_header
35 #include <bits/hashtable_policy.h>
37 namespace std _GLIBCXX_VISIBILITY(default)
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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>>>;
52 /**
53 * Primary class template _Hashtable.
55 * @ingroup hashtable-detail
57 * @tparam _Value CopyConstructible type.
59 * @tparam _Key CopyConstructible type.
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.
66 * @tparam _ExtractKey Function object that takes an object of type
67 * _Value and returns a value of type _Key.
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.
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()].
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).
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.
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>)
99 * @tparam _Traits Compile-time class with three boolean
100 * std::integral_constant members: __cache_hash_code, __constant_iterators,
101 * __unique_keys.
103 * Each _Hashtable data structure has:
105 * - _Bucket[] _M_buckets
106 * - _Hash_node_base _M_before_begin
107 * - size_type _M_bucket_count
108 * - size_type _M_element_count
110 * with _Bucket being _Hash_node* and _Hash_node containing:
112 * - _Hash_node* _M_next
113 * - Tp _M_value
114 * - size_t _M_hash_code if cache_hash_code is true
116 * In terms of Standard containers the hashtable is like the aggregation of:
118 * - std::forward_list<_Node> containing the elements
119 * - std::vector<std::forward_list<_Node>::iterator> representing the buckets
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.
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.
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.
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.
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.
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.
163 * Base class templates are:
164 * - __detail::_Hashtable_base
165 * - __detail::_Map_base
166 * - __detail::_Insert
167 * - __detail::_Rehash_base
168 * - __detail::_Equality
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>
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;
196 using __hashtable_alloc = __detail::_Hashtable_alloc<__node_alloc_type>;
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;
205 public:
206 typedef _Key key_type;
207 typedef _Value value_type;
208 typedef _Alloc allocator_type;
209 typedef _Equal key_equal;
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;
218 private:
219 using __rehash_type = _RehashPolicy;
220 using __rehash_state = typename __rehash_type::_State;
222 using __constant_iterators = typename __traits_type::__constant_iterators;
223 using __unique_keys = typename __traits_type::__unique_keys;
225 using __key_extract = typename std::conditional<
226 __constant_iterators::value,
227 __detail::_Identity,
228 __detail::_Select1st>::type;
230 using __hashtable_base = __detail::
231 _Hashtable_base<_Key, _Value, _ExtractKey,
232 _Equal, _H1, _H2, _Hash, _Traits>;
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;
238 using __map_base = __detail::_Map_base<_Key, _Value, _Alloc, _ExtractKey,
239 _Equal, _H1, _H2, _Hash,
240 _RehashPolicy, _Traits>;
242 using __rehash_base = __detail::_Rehash_base<_Key, _Value, _Alloc,
243 _ExtractKey, _Equal,
244 _H1, _H2, _Hash,
245 _RehashPolicy, _Traits>;
247 using __eq_base = __detail::_Equality<_Key, _Value, _Alloc, _ExtractKey,
248 _Equal, _H1, _H2, _Hash,
249 _RehashPolicy, _Traits>;
251 using __reuse_or_alloc_node_type =
252 __detail::_ReuseOrAllocNode<__node_alloc_type>;
254 // Metaprogramming for picking apart hash caching.
255 template<typename _Cond>
256 using __if_hash_cached = __or_<__not_<__hash_cached>, _Cond>;
258 template<typename _Cond>
259 using __if_hash_not_cached = __or_<__hash_cached, _Cond>;
261 // Compile-time diagnostics.
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");
271 // Following two static assertions are necessary to guarantee
272 // that local_iterator will be default constructible.
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");
280 // _Hash_code_base has a protected default constructor, so use this
281 // derived type to tell if it's usable.
282 struct __access_protected_ctor : __hash_code_base { };
284 // When hash codes are not cached local iterator inherits from
285 // __hash_code_base above to compute node bucket index so it has to be
286 // default constructible.
287 static_assert(__if_hash_not_cached<
288 is_default_constructible<__access_protected_ctor>>::value,
289 "Cache the hash code or make functors involved in hash code"
290 " and bucket index computation default constructible");
292 // When hash codes are not cached local iterator inherits from
293 // __hash_code_base above to compute node bucket index so it has to be
294 // assignable.
295 static_assert(__if_hash_not_cached<
296 is_copy_assignable<__hash_code_base>>::value,
297 "Cache the hash code or make functors involved in hash code"
298 " and bucket index computation copy assignable");
300 template<typename _Keya, typename _Valuea, typename _Alloca,
301 typename _ExtractKeya, typename _Equala,
302 typename _H1a, typename _H2a, typename _Hasha,
303 typename _RehashPolicya, typename _Traitsa,
304 bool _Unique_keysa>
305 friend struct __detail::_Map_base;
307 template<typename _Keya, typename _Valuea, typename _Alloca,
308 typename _ExtractKeya, typename _Equala,
309 typename _H1a, typename _H2a, typename _Hasha,
310 typename _RehashPolicya, typename _Traitsa>
311 friend struct __detail::_Insert_base;
313 template<typename _Keya, typename _Valuea, typename _Alloca,
314 typename _ExtractKeya, typename _Equala,
315 typename _H1a, typename _H2a, typename _Hasha,
316 typename _RehashPolicya, typename _Traitsa,
317 bool _Constant_iteratorsa, bool _Unique_keysa>
318 friend struct __detail::_Insert;
320 public:
321 using size_type = typename __hashtable_base::size_type;
322 using difference_type = typename __hashtable_base::difference_type;
324 using iterator = typename __hashtable_base::iterator;
325 using const_iterator = typename __hashtable_base::const_iterator;
327 using local_iterator = typename __hashtable_base::local_iterator;
328 using const_local_iterator = typename __hashtable_base::
329 const_local_iterator;
331 private:
332 __bucket_type* _M_buckets;
333 size_type _M_bucket_count;
334 __node_base _M_before_begin;
335 size_type _M_element_count;
336 _RehashPolicy _M_rehash_policy;
338 __hashtable_alloc&
339 _M_base_alloc() { return *this; }
341 using __hashtable_alloc::_M_deallocate_buckets;
343 void
344 _M_deallocate_buckets()
345 { this->_M_deallocate_buckets(_M_buckets, _M_bucket_count); }
347 // Gets bucket begin, deals with the fact that non-empty buckets contain
348 // their before begin node.
349 __node_type*
350 _M_bucket_begin(size_type __bkt) const;
352 __node_type*
353 _M_begin() const
354 { return static_cast<__node_type*>(_M_before_begin._M_nxt); }
356 template<typename _NodeGenerator>
357 void
358 _M_assign(const _Hashtable&, const _NodeGenerator&);
360 void
361 _M_move_assign(_Hashtable&&, std::true_type);
363 void
364 _M_move_assign(_Hashtable&&, std::false_type);
366 void
367 _M_reset() noexcept;
369 public:
370 // Constructor, destructor, assignment, swap
371 _Hashtable(size_type __bucket_hint,
372 const _H1&, const _H2&, const _Hash&,
373 const _Equal&, const _ExtractKey&,
374 const allocator_type&);
376 template<typename _InputIterator>
377 _Hashtable(_InputIterator __first, _InputIterator __last,
378 size_type __bucket_hint,
379 const _H1&, const _H2&, const _Hash&,
380 const _Equal&, const _ExtractKey&,
381 const allocator_type&);
383 _Hashtable(const _Hashtable&);
385 _Hashtable(_Hashtable&&) noexcept;
387 _Hashtable(const _Hashtable&, const allocator_type&);
389 _Hashtable(_Hashtable&&, const allocator_type&);
391 // Use delegating constructors.
392 explicit
393 _Hashtable(const allocator_type& __a)
394 : _Hashtable(10, _H1(), __detail::_Mod_range_hashing(),
395 __detail::_Default_ranged_hash(), key_equal(),
396 __key_extract(), __a)
399 explicit
400 _Hashtable(size_type __n = 10,
401 const _H1& __hf = _H1(),
402 const key_equal& __eql = key_equal(),
403 const allocator_type& __a = allocator_type())
404 : _Hashtable(__n, __hf, __detail::_Mod_range_hashing(),
405 __detail::_Default_ranged_hash(), __eql,
406 __key_extract(), __a)
409 template<typename _InputIterator>
410 _Hashtable(_InputIterator __f, _InputIterator __l,
411 size_type __n = 0,
412 const _H1& __hf = _H1(),
413 const key_equal& __eql = key_equal(),
414 const allocator_type& __a = allocator_type())
415 : _Hashtable(__f, __l, __n, __hf, __detail::_Mod_range_hashing(),
416 __detail::_Default_ranged_hash(), __eql,
417 __key_extract(), __a)
420 _Hashtable(initializer_list<value_type> __l,
421 size_type __n = 0,
422 const _H1& __hf = _H1(),
423 const key_equal& __eql = key_equal(),
424 const allocator_type& __a = allocator_type())
425 : _Hashtable(__l.begin(), __l.end(), __n, __hf,
426 __detail::_Mod_range_hashing(),
427 __detail::_Default_ranged_hash(), __eql,
428 __key_extract(), __a)
431 _Hashtable&
432 operator=(const _Hashtable& __ht);
434 _Hashtable&
435 operator=(_Hashtable&& __ht)
436 noexcept(__node_alloc_traits::_S_nothrow_move())
438 constexpr bool __move_storage =
439 __node_alloc_traits::_S_propagate_on_move_assign()
440 || __node_alloc_traits::_S_always_equal();
441 _M_move_assign(std::move(__ht),
442 integral_constant<bool, __move_storage>());
443 return *this;
446 _Hashtable&
447 operator=(initializer_list<value_type> __l)
449 __reuse_or_alloc_node_type __roan(_M_begin(), *this);
450 _M_before_begin._M_nxt = nullptr;
451 clear();
452 this->_M_insert_range(__l.begin(), __l.end(), __roan);
453 return *this;
456 ~_Hashtable() noexcept;
458 void
459 swap(_Hashtable&)
460 noexcept(__node_alloc_traits::_S_nothrow_swap());
462 // Basic container operations
463 iterator
464 begin() noexcept
465 { return iterator(_M_begin()); }
467 const_iterator
468 begin() const noexcept
469 { return const_iterator(_M_begin()); }
471 iterator
472 end() noexcept
473 { return iterator(nullptr); }
475 const_iterator
476 end() const noexcept
477 { return const_iterator(nullptr); }
479 const_iterator
480 cbegin() const noexcept
481 { return const_iterator(_M_begin()); }
483 const_iterator
484 cend() const noexcept
485 { return const_iterator(nullptr); }
487 size_type
488 size() const noexcept
489 { return _M_element_count; }
491 bool
492 empty() const noexcept
493 { return size() == 0; }
495 allocator_type
496 get_allocator() const noexcept
497 { return allocator_type(this->_M_node_allocator()); }
499 size_type
500 max_size() const noexcept
501 { return __node_alloc_traits::max_size(this->_M_node_allocator()); }
503 // Observers
504 key_equal
505 key_eq() const
506 { return this->_M_eq(); }
508 // hash_function, if present, comes from _Hash_code_base.
510 // Bucket operations
511 size_type
512 bucket_count() const noexcept
513 { return _M_bucket_count; }
515 size_type
516 max_bucket_count() const noexcept
517 { return max_size(); }
519 size_type
520 bucket_size(size_type __n) const
521 { return std::distance(begin(__n), end(__n)); }
523 size_type
524 bucket(const key_type& __k) const
525 { return _M_bucket_index(__k, this->_M_hash_code(__k)); }
527 local_iterator
528 begin(size_type __n)
530 return local_iterator(*this, _M_bucket_begin(__n),
531 __n, _M_bucket_count);
534 local_iterator
535 end(size_type __n)
536 { return local_iterator(*this, nullptr, __n, _M_bucket_count); }
538 const_local_iterator
539 begin(size_type __n) const
541 return const_local_iterator(*this, _M_bucket_begin(__n),
542 __n, _M_bucket_count);
545 const_local_iterator
546 end(size_type __n) const
547 { return const_local_iterator(*this, nullptr, __n, _M_bucket_count); }
549 // DR 691.
550 const_local_iterator
551 cbegin(size_type __n) const
553 return const_local_iterator(*this, _M_bucket_begin(__n),
554 __n, _M_bucket_count);
557 const_local_iterator
558 cend(size_type __n) const
559 { return const_local_iterator(*this, nullptr, __n, _M_bucket_count); }
561 float
562 load_factor() const noexcept
564 return static_cast<float>(size()) / static_cast<float>(bucket_count());
567 // max_load_factor, if present, comes from _Rehash_base.
569 // Generalization of max_load_factor. Extension, not found in
570 // TR1. Only useful if _RehashPolicy is something other than
571 // the default.
572 const _RehashPolicy&
573 __rehash_policy() const
574 { return _M_rehash_policy; }
576 void
577 __rehash_policy(const _RehashPolicy&);
579 // Lookup.
580 iterator
581 find(const key_type& __k);
583 const_iterator
584 find(const key_type& __k) const;
586 size_type
587 count(const key_type& __k) const;
589 std::pair<iterator, iterator>
590 equal_range(const key_type& __k);
592 std::pair<const_iterator, const_iterator>
593 equal_range(const key_type& __k) const;
595 protected:
596 // Bucket index computation helpers.
597 size_type
598 _M_bucket_index(__node_type* __n) const noexcept
599 { return __hash_code_base::_M_bucket_index(__n, _M_bucket_count); }
601 size_type
602 _M_bucket_index(const key_type& __k, __hash_code __c) const
603 { return __hash_code_base::_M_bucket_index(__k, __c, _M_bucket_count); }
605 // Find and insert helper functions and types
606 // Find the node before the one matching the criteria.
607 __node_base*
608 _M_find_before_node(size_type, const key_type&, __hash_code) const;
610 __node_type*
611 _M_find_node(size_type __bkt, const key_type& __key,
612 __hash_code __c) const
614 __node_base* __before_n = _M_find_before_node(__bkt, __key, __c);
615 if (__before_n)
616 return static_cast<__node_type*>(__before_n->_M_nxt);
617 return nullptr;
620 // Insert a node at the beginning of a bucket.
621 void
622 _M_insert_bucket_begin(size_type, __node_type*);
624 // Remove the bucket first node
625 void
626 _M_remove_bucket_begin(size_type __bkt, __node_type* __next_n,
627 size_type __next_bkt);
629 // Get the node before __n in the bucket __bkt
630 __node_base*
631 _M_get_previous_node(size_type __bkt, __node_base* __n);
633 // Insert node with hash code __code, in bucket bkt if no rehash (assumes
634 // no element with its key already present). Take ownership of the node,
635 // deallocate it on exception.
636 iterator
637 _M_insert_unique_node(size_type __bkt, __hash_code __code,
638 __node_type* __n);
640 // Insert node with hash code __code. Take ownership of the node,
641 // deallocate it on exception.
642 iterator
643 _M_insert_multi_node(__node_type* __hint,
644 __hash_code __code, __node_type* __n);
646 template<typename... _Args>
647 std::pair<iterator, bool>
648 _M_emplace(std::true_type, _Args&&... __args);
650 template<typename... _Args>
651 iterator
652 _M_emplace(std::false_type __uk, _Args&&... __args)
653 { return _M_emplace(cend(), __uk, std::forward<_Args>(__args)...); }
655 // Emplace with hint, useless when keys are unique.
656 template<typename... _Args>
657 iterator
658 _M_emplace(const_iterator, std::true_type __uk, _Args&&... __args)
659 { return _M_emplace(__uk, std::forward<_Args>(__args)...).first; }
661 template<typename... _Args>
662 iterator
663 _M_emplace(const_iterator, std::false_type, _Args&&... __args);
665 template<typename _Arg, typename _NodeGenerator>
666 std::pair<iterator, bool>
667 _M_insert(_Arg&&, const _NodeGenerator&, std::true_type);
669 template<typename _Arg, typename _NodeGenerator>
670 iterator
671 _M_insert(_Arg&& __arg, const _NodeGenerator& __node_gen,
672 std::false_type __uk)
674 return _M_insert(cend(), std::forward<_Arg>(__arg), __node_gen,
675 __uk);
678 // Insert with hint, not used when keys are unique.
679 template<typename _Arg, typename _NodeGenerator>
680 iterator
681 _M_insert(const_iterator, _Arg&& __arg, const _NodeGenerator& __node_gen,
682 std::true_type __uk)
684 return
685 _M_insert(std::forward<_Arg>(__arg), __node_gen, __uk).first;
688 // Insert with hint when keys are not unique.
689 template<typename _Arg, typename _NodeGenerator>
690 iterator
691 _M_insert(const_iterator, _Arg&&, const _NodeGenerator&, std::false_type);
693 size_type
694 _M_erase(std::true_type, const key_type&);
696 size_type
697 _M_erase(std::false_type, const key_type&);
699 iterator
700 _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n);
702 public:
703 // Emplace
704 template<typename... _Args>
705 __ireturn_type
706 emplace(_Args&&... __args)
707 { return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
709 template<typename... _Args>
710 iterator
711 emplace_hint(const_iterator __hint, _Args&&... __args)
713 return _M_emplace(__hint, __unique_keys(),
714 std::forward<_Args>(__args)...);
717 // Insert member functions via inheritance.
719 // Erase
720 iterator
721 erase(const_iterator);
723 // LWG 2059.
724 iterator
725 erase(iterator __it)
726 { return erase(const_iterator(__it)); }
728 size_type
729 erase(const key_type& __k)
731 if (__builtin_expect(_M_bucket_count == 0, false))
732 return 0;
733 return _M_erase(__unique_keys(), __k);
736 iterator
737 erase(const_iterator, const_iterator);
739 void
740 clear() noexcept;
742 // Set number of buckets to be appropriate for container of n element.
743 void rehash(size_type __n);
745 // DR 1189.
746 // reserve, if present, comes from _Rehash_base.
748 private:
749 // Helper rehash method used when keys are unique.
750 void _M_rehash_aux(size_type __n, std::true_type);
752 // Helper rehash method used when keys can be non-unique.
753 void _M_rehash_aux(size_type __n, std::false_type);
755 // Unconditionally change size of bucket array to n, restore
756 // hash policy state to __state on exception.
757 void _M_rehash(size_type __n, const __rehash_state& __state);
761 // Definitions of class template _Hashtable's out-of-line member functions.
762 template<typename _Key, typename _Value,
763 typename _Alloc, typename _ExtractKey, typename _Equal,
764 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
765 typename _Traits>
766 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
767 _Equal, _H1, _H2, _Hash, _RehashPolicy,
768 _Traits>::__node_type*
769 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
770 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
771 _M_bucket_begin(size_type __bkt) const
773 __node_base* __n = _M_buckets[__bkt];
774 return __n ? static_cast<__node_type*>(__n->_M_nxt) : nullptr;
777 template<typename _Key, typename _Value,
778 typename _Alloc, typename _ExtractKey, typename _Equal,
779 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
780 typename _Traits>
781 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
782 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
783 _Hashtable(size_type __bucket_hint,
784 const _H1& __h1, const _H2& __h2, const _Hash& __h,
785 const _Equal& __eq, const _ExtractKey& __exk,
786 const allocator_type& __a)
787 : __hashtable_base(__exk, __h1, __h2, __h, __eq),
788 __map_base(),
789 __rehash_base(),
790 __hashtable_alloc(__node_alloc_type(__a)),
791 _M_element_count(0),
792 _M_rehash_policy()
794 _M_bucket_count = _M_rehash_policy._M_next_bkt(__bucket_hint);
795 _M_buckets = this->_M_allocate_buckets(_M_bucket_count);
798 template<typename _Key, typename _Value,
799 typename _Alloc, typename _ExtractKey, typename _Equal,
800 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
801 typename _Traits>
802 template<typename _InputIterator>
803 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
804 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
805 _Hashtable(_InputIterator __f, _InputIterator __l,
806 size_type __bucket_hint,
807 const _H1& __h1, const _H2& __h2, const _Hash& __h,
808 const _Equal& __eq, const _ExtractKey& __exk,
809 const allocator_type& __a)
810 : __hashtable_base(__exk, __h1, __h2, __h, __eq),
811 __map_base(),
812 __rehash_base(),
813 __hashtable_alloc(__node_alloc_type(__a)),
814 _M_element_count(0),
815 _M_rehash_policy()
817 auto __nb_elems = __detail::__distance_fw(__f, __l);
818 _M_bucket_count =
819 _M_rehash_policy._M_next_bkt(
820 std::max(_M_rehash_policy._M_bkt_for_elements(__nb_elems),
821 __bucket_hint));
823 _M_buckets = this->_M_allocate_buckets(_M_bucket_count);
824 __try
826 for (; __f != __l; ++__f)
827 this->insert(*__f);
829 __catch(...)
831 clear();
832 _M_deallocate_buckets();
833 __throw_exception_again;
837 template<typename _Key, typename _Value,
838 typename _Alloc, typename _ExtractKey, typename _Equal,
839 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
840 typename _Traits>
841 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
842 _H1, _H2, _Hash, _RehashPolicy, _Traits>&
843 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
844 _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(
845 const _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
846 _H1, _H2, _Hash, _RehashPolicy, _Traits>& __ht)
848 if (&__ht == this)
849 return *this;
851 if (__node_alloc_traits::_S_propagate_on_copy_assign())
853 auto& __this_alloc = this->_M_node_allocator();
854 auto& __that_alloc = __ht._M_node_allocator();
855 if (!__node_alloc_traits::_S_always_equal()
856 && __this_alloc != __that_alloc)
858 // Replacement allocator cannot free existing storage.
859 this->_M_deallocate_nodes(_M_begin());
860 if (__builtin_expect(_M_bucket_count != 0, true))
861 _M_deallocate_buckets();
862 _M_reset();
863 std::__alloc_on_copy(__this_alloc, __that_alloc);
864 __hashtable_base::operator=(__ht);
865 _M_bucket_count = __ht._M_bucket_count;
866 _M_element_count = __ht._M_element_count;
867 _M_rehash_policy = __ht._M_rehash_policy;
868 __try
870 _M_assign(__ht,
871 [this](const __node_type* __n)
872 { return this->_M_allocate_node(__n->_M_v()); });
874 __catch(...)
876 // _M_assign took care of deallocating all memory. Now we
877 // must make sure this instance remains in a usable state.
878 _M_reset();
879 __throw_exception_again;
881 return *this;
883 std::__alloc_on_copy(__this_alloc, __that_alloc);
886 // Reuse allocated buckets and nodes.
887 __bucket_type* __former_buckets = nullptr;
888 std::size_t __former_bucket_count = _M_bucket_count;
889 const __rehash_state& __former_state = _M_rehash_policy._M_state();
891 if (_M_bucket_count != __ht._M_bucket_count)
893 __former_buckets = _M_buckets;
894 _M_buckets = this->_M_allocate_buckets(__ht._M_bucket_count);
895 _M_bucket_count = __ht._M_bucket_count;
897 else
898 __builtin_memset(_M_buckets, 0,
899 _M_bucket_count * sizeof(__bucket_type));
901 __try
903 __hashtable_base::operator=(__ht);
904 _M_element_count = __ht._M_element_count;
905 _M_rehash_policy = __ht._M_rehash_policy;
906 __reuse_or_alloc_node_type __roan(_M_begin(), *this);
907 _M_before_begin._M_nxt = nullptr;
908 _M_assign(__ht,
909 [&__roan](const __node_type* __n)
910 { return __roan(__n->_M_v()); });
911 if (__former_buckets)
912 this->_M_deallocate_buckets(__former_buckets,
913 __former_bucket_count);
915 __catch(...)
917 if (__former_buckets)
919 // Restore previous buckets.
920 _M_deallocate_buckets();
921 _M_rehash_policy._M_reset(__former_state);
922 _M_buckets = __former_buckets;
923 _M_bucket_count = __former_bucket_count;
925 __builtin_memset(_M_buckets, 0,
926 _M_bucket_count * sizeof(__bucket_type));
927 __throw_exception_again;
929 return *this;
932 template<typename _Key, typename _Value,
933 typename _Alloc, typename _ExtractKey, typename _Equal,
934 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
935 typename _Traits>
936 template<typename _NodeGenerator>
937 void
938 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
939 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
940 _M_assign(const _Hashtable& __ht, const _NodeGenerator& __node_gen)
942 __bucket_type* __buckets = nullptr;
943 if (!_M_buckets)
944 _M_buckets = __buckets = this->_M_allocate_buckets(_M_bucket_count);
946 __try
948 if (!__ht._M_before_begin._M_nxt)
949 return;
951 // First deal with the special first node pointed to by
952 // _M_before_begin.
953 __node_type* __ht_n = __ht._M_begin();
954 __node_type* __this_n = __node_gen(__ht_n);
955 this->_M_copy_code(__this_n, __ht_n);
956 _M_before_begin._M_nxt = __this_n;
957 _M_buckets[_M_bucket_index(__this_n)] = &_M_before_begin;
959 // Then deal with other nodes.
960 __node_base* __prev_n = __this_n;
961 for (__ht_n = __ht_n->_M_next(); __ht_n; __ht_n = __ht_n->_M_next())
963 __this_n = __node_gen(__ht_n);
964 __prev_n->_M_nxt = __this_n;
965 this->_M_copy_code(__this_n, __ht_n);
966 size_type __bkt = _M_bucket_index(__this_n);
967 if (!_M_buckets[__bkt])
968 _M_buckets[__bkt] = __prev_n;
969 __prev_n = __this_n;
972 __catch(...)
974 clear();
975 if (__buckets)
976 _M_deallocate_buckets();
977 __throw_exception_again;
981 template<typename _Key, typename _Value,
982 typename _Alloc, typename _ExtractKey, typename _Equal,
983 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
984 typename _Traits>
985 void
986 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
987 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
988 _M_reset() noexcept
990 _M_rehash_policy._M_reset();
991 _M_bucket_count = 0;
992 _M_buckets = nullptr;
993 _M_before_begin._M_nxt = nullptr;
994 _M_element_count = 0;
997 template<typename _Key, typename _Value,
998 typename _Alloc, typename _ExtractKey, typename _Equal,
999 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1000 typename _Traits>
1001 void
1002 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1003 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1004 _M_move_assign(_Hashtable&& __ht, std::true_type)
1006 this->_M_deallocate_nodes(_M_begin());
1007 if (__builtin_expect(_M_bucket_count != 0, true))
1008 _M_deallocate_buckets();
1010 __hashtable_base::operator=(std::move(__ht));
1011 _M_rehash_policy = __ht._M_rehash_policy;
1012 _M_buckets = __ht._M_buckets;
1013 _M_bucket_count = __ht._M_bucket_count;
1014 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1015 _M_element_count = __ht._M_element_count;
1016 std::__alloc_on_move(this->_M_node_allocator(), __ht._M_node_allocator());
1018 // Fix buckets containing the _M_before_begin pointers that can't be
1019 // moved.
1020 if (_M_begin())
1021 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1022 __ht._M_reset();
1025 template<typename _Key, typename _Value,
1026 typename _Alloc, typename _ExtractKey, typename _Equal,
1027 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1028 typename _Traits>
1029 void
1030 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1031 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1032 _M_move_assign(_Hashtable&& __ht, std::false_type)
1034 if (__ht._M_node_allocator() == this->_M_node_allocator())
1035 _M_move_assign(std::move(__ht), std::true_type());
1036 else
1038 // Can't move memory, move elements then.
1039 __bucket_type* __former_buckets = nullptr;
1040 size_type __former_bucket_count = _M_bucket_count;
1041 const __rehash_state& __former_state = _M_rehash_policy._M_state();
1043 if (_M_bucket_count != __ht._M_bucket_count)
1045 __former_buckets = _M_buckets;
1046 _M_buckets = this->_M_allocate_buckets(__ht._M_bucket_count);
1047 _M_bucket_count = __ht._M_bucket_count;
1049 else
1050 __builtin_memset(_M_buckets, 0,
1051 _M_bucket_count * sizeof(__bucket_type));
1053 __try
1055 __hashtable_base::operator=(std::move(__ht));
1056 _M_element_count = __ht._M_element_count;
1057 _M_rehash_policy = __ht._M_rehash_policy;
1058 __reuse_or_alloc_node_type __roan(_M_begin(), *this);
1059 _M_before_begin._M_nxt = nullptr;
1060 _M_assign(__ht,
1061 [&__roan](__node_type* __n)
1062 { return __roan(std::move_if_noexcept(__n->_M_v())); });
1063 __ht.clear();
1065 __catch(...)
1067 if (__former_buckets)
1069 _M_deallocate_buckets();
1070 _M_rehash_policy._M_reset(__former_state);
1071 _M_buckets = __former_buckets;
1072 _M_bucket_count = __former_bucket_count;
1074 __builtin_memset(_M_buckets, 0,
1075 _M_bucket_count * sizeof(__bucket_type));
1076 __throw_exception_again;
1081 template<typename _Key, typename _Value,
1082 typename _Alloc, typename _ExtractKey, typename _Equal,
1083 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1084 typename _Traits>
1085 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1086 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1087 _Hashtable(const _Hashtable& __ht)
1088 : __hashtable_base(__ht),
1089 __map_base(__ht),
1090 __rehash_base(__ht),
1091 __hashtable_alloc(
1092 __node_alloc_traits::_S_select_on_copy(__ht._M_node_allocator())),
1093 _M_buckets(),
1094 _M_bucket_count(__ht._M_bucket_count),
1095 _M_element_count(__ht._M_element_count),
1096 _M_rehash_policy(__ht._M_rehash_policy)
1098 _M_assign(__ht,
1099 [this](const __node_type* __n)
1100 { return this->_M_allocate_node(__n->_M_v()); });
1103 template<typename _Key, typename _Value,
1104 typename _Alloc, typename _ExtractKey, typename _Equal,
1105 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1106 typename _Traits>
1107 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1108 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1109 _Hashtable(_Hashtable&& __ht) noexcept
1110 : __hashtable_base(__ht),
1111 __map_base(__ht),
1112 __rehash_base(__ht),
1113 __hashtable_alloc(std::move(__ht._M_base_alloc())),
1114 _M_buckets(__ht._M_buckets),
1115 _M_bucket_count(__ht._M_bucket_count),
1116 _M_before_begin(__ht._M_before_begin._M_nxt),
1117 _M_element_count(__ht._M_element_count),
1118 _M_rehash_policy(__ht._M_rehash_policy)
1120 // Update, if necessary, bucket pointing to before begin that hasn't
1121 // moved.
1122 if (_M_begin())
1123 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1124 __ht._M_reset();
1127 template<typename _Key, typename _Value,
1128 typename _Alloc, typename _ExtractKey, typename _Equal,
1129 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1130 typename _Traits>
1131 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1132 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1133 _Hashtable(const _Hashtable& __ht, const allocator_type& __a)
1134 : __hashtable_base(__ht),
1135 __map_base(__ht),
1136 __rehash_base(__ht),
1137 __hashtable_alloc(__node_alloc_type(__a)),
1138 _M_buckets(),
1139 _M_bucket_count(__ht._M_bucket_count),
1140 _M_element_count(__ht._M_element_count),
1141 _M_rehash_policy(__ht._M_rehash_policy)
1143 _M_assign(__ht,
1144 [this](const __node_type* __n)
1145 { return this->_M_allocate_node(__n->_M_v()); });
1148 template<typename _Key, typename _Value,
1149 typename _Alloc, typename _ExtractKey, typename _Equal,
1150 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1151 typename _Traits>
1152 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1153 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1154 _Hashtable(_Hashtable&& __ht, const allocator_type& __a)
1155 : __hashtable_base(__ht),
1156 __map_base(__ht),
1157 __rehash_base(__ht),
1158 __hashtable_alloc(__node_alloc_type(__a)),
1159 _M_buckets(),
1160 _M_bucket_count(__ht._M_bucket_count),
1161 _M_element_count(__ht._M_element_count),
1162 _M_rehash_policy(__ht._M_rehash_policy)
1164 if (__ht._M_node_allocator() == this->_M_node_allocator())
1166 _M_buckets = __ht._M_buckets;
1167 _M_before_begin._M_nxt = __ht._M_before_begin._M_nxt;
1168 // Update, if necessary, bucket pointing to before begin that hasn't
1169 // moved.
1170 if (_M_begin())
1171 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1172 __ht._M_reset();
1174 else
1176 _M_assign(__ht,
1177 [this](__node_type* __n)
1179 return this->_M_allocate_node(
1180 std::move_if_noexcept(__n->_M_v()));
1182 __ht.clear();
1186 template<typename _Key, typename _Value,
1187 typename _Alloc, typename _ExtractKey, typename _Equal,
1188 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1189 typename _Traits>
1190 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1191 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1192 ~_Hashtable() noexcept
1194 clear();
1195 if (_M_buckets)
1196 _M_deallocate_buckets();
1199 template<typename _Key, typename _Value,
1200 typename _Alloc, typename _ExtractKey, typename _Equal,
1201 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1202 typename _Traits>
1203 void
1204 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1205 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1206 swap(_Hashtable& __x)
1207 noexcept(__node_alloc_traits::_S_nothrow_swap())
1209 // The only base class with member variables is hash_code_base.
1210 // We define _Hash_code_base::_M_swap because different
1211 // specializations have different members.
1212 this->_M_swap(__x);
1214 std::__alloc_on_swap(this->_M_node_allocator(), __x._M_node_allocator());
1215 std::swap(_M_rehash_policy, __x._M_rehash_policy);
1216 std::swap(_M_buckets, __x._M_buckets);
1217 std::swap(_M_bucket_count, __x._M_bucket_count);
1218 std::swap(_M_before_begin._M_nxt, __x._M_before_begin._M_nxt);
1219 std::swap(_M_element_count, __x._M_element_count);
1221 // Fix buckets containing the _M_before_begin pointers that can't be
1222 // swapped.
1223 if (_M_begin())
1224 _M_buckets[_M_bucket_index(_M_begin())] = &_M_before_begin;
1225 if (__x._M_begin())
1226 __x._M_buckets[__x._M_bucket_index(__x._M_begin())]
1227 = &__x._M_before_begin;
1230 template<typename _Key, typename _Value,
1231 typename _Alloc, typename _ExtractKey, typename _Equal,
1232 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1233 typename _Traits>
1234 void
1235 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1236 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1237 __rehash_policy(const _RehashPolicy& __pol)
1239 size_type __n_bkt = __pol._M_bkt_for_elements(_M_element_count);
1240 __n_bkt = __pol._M_next_bkt(__n_bkt);
1241 if (__n_bkt != _M_bucket_count)
1242 _M_rehash(__n_bkt, _M_rehash_policy._M_state());
1243 _M_rehash_policy = __pol;
1246 template<typename _Key, typename _Value,
1247 typename _Alloc, typename _ExtractKey, typename _Equal,
1248 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1249 typename _Traits>
1250 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1251 _H1, _H2, _Hash, _RehashPolicy,
1252 _Traits>::iterator
1253 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1254 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1255 find(const key_type& __k)
1257 if (__builtin_expect(_M_bucket_count == 0, false))
1258 return end();
1260 __hash_code __code = this->_M_hash_code(__k);
1261 std::size_t __n = _M_bucket_index(__k, __code);
1262 __node_type* __p = _M_find_node(__n, __k, __code);
1263 return __p ? iterator(__p) : end();
1266 template<typename _Key, typename _Value,
1267 typename _Alloc, typename _ExtractKey, typename _Equal,
1268 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1269 typename _Traits>
1270 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1271 _H1, _H2, _Hash, _RehashPolicy,
1272 _Traits>::const_iterator
1273 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1274 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1275 find(const key_type& __k) const
1277 if (__builtin_expect(_M_bucket_count == 0, false))
1278 return end();
1280 __hash_code __code = this->_M_hash_code(__k);
1281 std::size_t __n = _M_bucket_index(__k, __code);
1282 __node_type* __p = _M_find_node(__n, __k, __code);
1283 return __p ? const_iterator(__p) : end();
1286 template<typename _Key, typename _Value,
1287 typename _Alloc, typename _ExtractKey, typename _Equal,
1288 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1289 typename _Traits>
1290 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1291 _H1, _H2, _Hash, _RehashPolicy,
1292 _Traits>::size_type
1293 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1294 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1295 count(const key_type& __k) const
1297 if (__builtin_expect(_M_bucket_count == 0, false))
1298 return 0;
1300 __hash_code __code = this->_M_hash_code(__k);
1301 std::size_t __n = _M_bucket_index(__k, __code);
1302 __node_type* __p = _M_bucket_begin(__n);
1303 if (!__p)
1304 return 0;
1306 std::size_t __result = 0;
1307 for (;; __p = __p->_M_next())
1309 if (this->_M_equals(__k, __code, __p))
1310 ++__result;
1311 else if (__result)
1312 // All equivalent values are next to each other, if we
1313 // found a non-equivalent value after an equivalent one it
1314 // means that we won't find any more equivalent values.
1315 break;
1316 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1317 break;
1319 return __result;
1322 template<typename _Key, typename _Value,
1323 typename _Alloc, typename _ExtractKey, typename _Equal,
1324 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1325 typename _Traits>
1326 std::pair<typename _Hashtable<_Key, _Value, _Alloc,
1327 _ExtractKey, _Equal, _H1,
1328 _H2, _Hash, _RehashPolicy,
1329 _Traits>::iterator,
1330 typename _Hashtable<_Key, _Value, _Alloc,
1331 _ExtractKey, _Equal, _H1,
1332 _H2, _Hash, _RehashPolicy,
1333 _Traits>::iterator>
1334 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1335 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1336 equal_range(const key_type& __k)
1338 if (__builtin_expect(_M_bucket_count == 0, false))
1339 return std::make_pair(end(), end());
1341 __hash_code __code = this->_M_hash_code(__k);
1342 std::size_t __n = _M_bucket_index(__k, __code);
1343 __node_type* __p = _M_find_node(__n, __k, __code);
1345 if (__p)
1347 __node_type* __p1 = __p->_M_next();
1348 while (__p1 && _M_bucket_index(__p1) == __n
1349 && this->_M_equals(__k, __code, __p1))
1350 __p1 = __p1->_M_next();
1352 return std::make_pair(iterator(__p), iterator(__p1));
1354 else
1355 return std::make_pair(end(), end());
1358 template<typename _Key, typename _Value,
1359 typename _Alloc, typename _ExtractKey, typename _Equal,
1360 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1361 typename _Traits>
1362 std::pair<typename _Hashtable<_Key, _Value, _Alloc,
1363 _ExtractKey, _Equal, _H1,
1364 _H2, _Hash, _RehashPolicy,
1365 _Traits>::const_iterator,
1366 typename _Hashtable<_Key, _Value, _Alloc,
1367 _ExtractKey, _Equal, _H1,
1368 _H2, _Hash, _RehashPolicy,
1369 _Traits>::const_iterator>
1370 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1371 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1372 equal_range(const key_type& __k) const
1374 if (__builtin_expect(_M_bucket_count == 0, false))
1375 return std::make_pair(end(), end());
1377 __hash_code __code = this->_M_hash_code(__k);
1378 std::size_t __n = _M_bucket_index(__k, __code);
1379 __node_type* __p = _M_find_node(__n, __k, __code);
1381 if (__p)
1383 __node_type* __p1 = __p->_M_next();
1384 while (__p1 && _M_bucket_index(__p1) == __n
1385 && this->_M_equals(__k, __code, __p1))
1386 __p1 = __p1->_M_next();
1388 return std::make_pair(const_iterator(__p), const_iterator(__p1));
1390 else
1391 return std::make_pair(end(), end());
1394 // Find the node whose key compares equal to k in the bucket n.
1395 // Return nullptr if no node is found.
1396 template<typename _Key, typename _Value,
1397 typename _Alloc, typename _ExtractKey, typename _Equal,
1398 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1399 typename _Traits>
1400 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1401 _Equal, _H1, _H2, _Hash, _RehashPolicy,
1402 _Traits>::__node_base*
1403 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1404 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1405 _M_find_before_node(size_type __n, const key_type& __k,
1406 __hash_code __code) const
1408 __node_base* __prev_p = _M_buckets[__n];
1409 if (!__prev_p)
1410 return nullptr;
1412 for (__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);;
1413 __p = __p->_M_next())
1415 if (this->_M_equals(__k, __code, __p))
1416 return __prev_p;
1418 if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)
1419 break;
1420 __prev_p = __p;
1422 return nullptr;
1425 template<typename _Key, typename _Value,
1426 typename _Alloc, typename _ExtractKey, typename _Equal,
1427 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1428 typename _Traits>
1429 void
1430 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1431 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1432 _M_insert_bucket_begin(size_type __bkt, __node_type* __node)
1434 if (_M_buckets[__bkt])
1436 // Bucket is not empty, we just need to insert the new node
1437 // after the bucket before begin.
1438 __node->_M_nxt = _M_buckets[__bkt]->_M_nxt;
1439 _M_buckets[__bkt]->_M_nxt = __node;
1441 else
1443 // The bucket is empty, the new node is inserted at the
1444 // beginning of the singly-linked list and the bucket will
1445 // contain _M_before_begin pointer.
1446 __node->_M_nxt = _M_before_begin._M_nxt;
1447 _M_before_begin._M_nxt = __node;
1448 if (__node->_M_nxt)
1449 // We must update former begin bucket that is pointing to
1450 // _M_before_begin.
1451 _M_buckets[_M_bucket_index(__node->_M_next())] = __node;
1452 _M_buckets[__bkt] = &_M_before_begin;
1456 template<typename _Key, typename _Value,
1457 typename _Alloc, typename _ExtractKey, typename _Equal,
1458 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1459 typename _Traits>
1460 void
1461 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1462 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1463 _M_remove_bucket_begin(size_type __bkt, __node_type* __next,
1464 size_type __next_bkt)
1466 if (!__next || __next_bkt != __bkt)
1468 // Bucket is now empty
1469 // First update next bucket if any
1470 if (__next)
1471 _M_buckets[__next_bkt] = _M_buckets[__bkt];
1473 // Second update before begin node if necessary
1474 if (&_M_before_begin == _M_buckets[__bkt])
1475 _M_before_begin._M_nxt = __next;
1476 _M_buckets[__bkt] = nullptr;
1480 template<typename _Key, typename _Value,
1481 typename _Alloc, typename _ExtractKey, typename _Equal,
1482 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1483 typename _Traits>
1484 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
1485 _Equal, _H1, _H2, _Hash, _RehashPolicy,
1486 _Traits>::__node_base*
1487 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1488 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1489 _M_get_previous_node(size_type __bkt, __node_base* __n)
1491 __node_base* __prev_n = _M_buckets[__bkt];
1492 while (__prev_n->_M_nxt != __n)
1493 __prev_n = __prev_n->_M_nxt;
1494 return __prev_n;
1497 template<typename _Key, typename _Value,
1498 typename _Alloc, typename _ExtractKey, typename _Equal,
1499 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1500 typename _Traits>
1501 template<typename... _Args>
1502 std::pair<typename _Hashtable<_Key, _Value, _Alloc,
1503 _ExtractKey, _Equal, _H1,
1504 _H2, _Hash, _RehashPolicy,
1505 _Traits>::iterator, bool>
1506 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1507 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1508 _M_emplace(std::true_type, _Args&&... __args)
1510 // First build the node to get access to the hash code
1511 __node_type* __node = this->_M_allocate_node(std::forward<_Args>(__args)...);
1512 const key_type& __k = this->_M_extract()(__node->_M_v());
1513 __hash_code __code;
1514 __try
1516 __code = this->_M_hash_code(__k);
1518 __catch(...)
1520 this->_M_deallocate_node(__node);
1521 __throw_exception_again;
1524 size_type __bkt = _M_bucket_index(__k, __code);
1525 if (__node_type* __p = _M_find_node(__bkt, __k, __code))
1527 // There is already an equivalent node, no insertion
1528 this->_M_deallocate_node(__node);
1529 return std::make_pair(iterator(__p), false);
1532 // Insert the node
1533 return std::make_pair(_M_insert_unique_node(__bkt, __code, __node),
1534 true);
1537 template<typename _Key, typename _Value,
1538 typename _Alloc, typename _ExtractKey, typename _Equal,
1539 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1540 typename _Traits>
1541 template<typename... _Args>
1542 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1543 _H1, _H2, _Hash, _RehashPolicy,
1544 _Traits>::iterator
1545 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1546 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1547 _M_emplace(const_iterator __hint, std::false_type, _Args&&... __args)
1549 // First build the node to get its hash code.
1550 __node_type* __node =
1551 this->_M_allocate_node(std::forward<_Args>(__args)...);
1553 __hash_code __code;
1554 __try
1556 __code = this->_M_hash_code(this->_M_extract()(__node->_M_v()));
1558 __catch(...)
1560 this->_M_deallocate_node(__node);
1561 __throw_exception_again;
1564 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1567 template<typename _Key, typename _Value,
1568 typename _Alloc, typename _ExtractKey, typename _Equal,
1569 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1570 typename _Traits>
1571 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1572 _H1, _H2, _Hash, _RehashPolicy,
1573 _Traits>::iterator
1574 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1575 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1576 _M_insert_unique_node(size_type __bkt, __hash_code __code,
1577 __node_type* __node)
1579 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1580 std::pair<bool, std::size_t> __do_rehash
1581 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1583 __try
1585 if (__do_rehash.first)
1587 _M_rehash(__do_rehash.second, __saved_state);
1588 __bkt = _M_bucket_index(this->_M_extract()(__node->_M_v()), __code);
1591 this->_M_store_code(__node, __code);
1593 // Always insert at the beginning of the bucket.
1594 _M_insert_bucket_begin(__bkt, __node);
1595 ++_M_element_count;
1596 return iterator(__node);
1598 __catch(...)
1600 this->_M_deallocate_node(__node);
1601 __throw_exception_again;
1605 // Insert node, in bucket bkt if no rehash (assumes no element with its key
1606 // already present). Take ownership of the node, deallocate it on exception.
1607 template<typename _Key, typename _Value,
1608 typename _Alloc, typename _ExtractKey, typename _Equal,
1609 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1610 typename _Traits>
1611 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1612 _H1, _H2, _Hash, _RehashPolicy,
1613 _Traits>::iterator
1614 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1615 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1616 _M_insert_multi_node(__node_type* __hint, __hash_code __code,
1617 __node_type* __node)
1619 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1620 std::pair<bool, std::size_t> __do_rehash
1621 = _M_rehash_policy._M_need_rehash(_M_bucket_count, _M_element_count, 1);
1623 __try
1625 if (__do_rehash.first)
1626 _M_rehash(__do_rehash.second, __saved_state);
1628 this->_M_store_code(__node, __code);
1629 const key_type& __k = this->_M_extract()(__node->_M_v());
1630 size_type __bkt = _M_bucket_index(__k, __code);
1632 // Find the node before an equivalent one or use hint if it exists and
1633 // if it is equivalent.
1634 __node_base* __prev
1635 = __builtin_expect(__hint != nullptr, false)
1636 && this->_M_equals(__k, __code, __hint)
1637 ? __hint
1638 : _M_find_before_node(__bkt, __k, __code);
1639 if (__prev)
1641 // Insert after the node before the equivalent one.
1642 __node->_M_nxt = __prev->_M_nxt;
1643 __prev->_M_nxt = __node;
1644 if (__builtin_expect(__prev == __hint, false))
1645 // hint might be the last bucket node, in this case we need to
1646 // update next bucket.
1647 if (__node->_M_nxt
1648 && !this->_M_equals(__k, __code, __node->_M_next()))
1650 size_type __next_bkt = _M_bucket_index(__node->_M_next());
1651 if (__next_bkt != __bkt)
1652 _M_buckets[__next_bkt] = __node;
1655 else
1656 // The inserted node has no equivalent in the
1657 // hashtable. We must insert the new node at the
1658 // beginning of the bucket to preserve equivalent
1659 // elements' relative positions.
1660 _M_insert_bucket_begin(__bkt, __node);
1661 ++_M_element_count;
1662 return iterator(__node);
1664 __catch(...)
1666 this->_M_deallocate_node(__node);
1667 __throw_exception_again;
1671 // Insert v if no element with its key is already present.
1672 template<typename _Key, typename _Value,
1673 typename _Alloc, typename _ExtractKey, typename _Equal,
1674 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1675 typename _Traits>
1676 template<typename _Arg, typename _NodeGenerator>
1677 std::pair<typename _Hashtable<_Key, _Value, _Alloc,
1678 _ExtractKey, _Equal, _H1,
1679 _H2, _Hash, _RehashPolicy,
1680 _Traits>::iterator, bool>
1681 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1682 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1683 _M_insert(_Arg&& __v, const _NodeGenerator& __node_gen, std::true_type)
1685 const key_type& __k = this->_M_extract()(__v);
1686 __hash_code __code = this->_M_hash_code(__k);
1687 size_type __bkt = _M_bucket_index(__k, __code);
1689 __node_type* __n = _M_find_node(__bkt, __k, __code);
1690 if (__n)
1691 return std::make_pair(iterator(__n), false);
1693 __n = __node_gen(std::forward<_Arg>(__v));
1694 return std::make_pair(_M_insert_unique_node(__bkt, __code, __n), true);
1697 // Insert v unconditionally.
1698 template<typename _Key, typename _Value,
1699 typename _Alloc, typename _ExtractKey, typename _Equal,
1700 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1701 typename _Traits>
1702 template<typename _Arg, typename _NodeGenerator>
1703 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1704 _H1, _H2, _Hash, _RehashPolicy,
1705 _Traits>::iterator
1706 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1707 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1708 _M_insert(const_iterator __hint, _Arg&& __v,
1709 const _NodeGenerator& __node_gen,
1710 std::false_type)
1712 // First compute the hash code so that we don't do anything if it
1713 // throws.
1714 __hash_code __code = this->_M_hash_code(this->_M_extract()(__v));
1716 // Second allocate new node so that we don't rehash if it throws.
1717 __node_type* __node = __node_gen(std::forward<_Arg>(__v));
1719 return _M_insert_multi_node(__hint._M_cur, __code, __node);
1722 template<typename _Key, typename _Value,
1723 typename _Alloc, typename _ExtractKey, typename _Equal,
1724 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1725 typename _Traits>
1726 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1727 _H1, _H2, _Hash, _RehashPolicy,
1728 _Traits>::iterator
1729 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1730 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1731 erase(const_iterator __it)
1733 __node_type* __n = __it._M_cur;
1734 std::size_t __bkt = _M_bucket_index(__n);
1736 // Look for previous node to unlink it from the erased one, this
1737 // is why we need buckets to contain the before begin to make
1738 // this search fast.
1739 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
1740 return _M_erase(__bkt, __prev_n, __n);
1743 template<typename _Key, typename _Value,
1744 typename _Alloc, typename _ExtractKey, typename _Equal,
1745 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1746 typename _Traits>
1747 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1748 _H1, _H2, _Hash, _RehashPolicy,
1749 _Traits>::iterator
1750 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1751 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1752 _M_erase(size_type __bkt, __node_base* __prev_n, __node_type* __n)
1754 if (__prev_n == _M_buckets[__bkt])
1755 _M_remove_bucket_begin(__bkt, __n->_M_next(),
1756 __n->_M_nxt ? _M_bucket_index(__n->_M_next()) : 0);
1757 else if (__n->_M_nxt)
1759 size_type __next_bkt = _M_bucket_index(__n->_M_next());
1760 if (__next_bkt != __bkt)
1761 _M_buckets[__next_bkt] = __prev_n;
1764 __prev_n->_M_nxt = __n->_M_nxt;
1765 iterator __result(__n->_M_next());
1766 this->_M_deallocate_node(__n);
1767 --_M_element_count;
1769 return __result;
1772 template<typename _Key, typename _Value,
1773 typename _Alloc, typename _ExtractKey, typename _Equal,
1774 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1775 typename _Traits>
1776 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1777 _H1, _H2, _Hash, _RehashPolicy,
1778 _Traits>::size_type
1779 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1780 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1781 _M_erase(std::true_type, const key_type& __k)
1783 __hash_code __code = this->_M_hash_code(__k);
1784 std::size_t __bkt = _M_bucket_index(__k, __code);
1786 // Look for the node before the first matching node.
1787 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1788 if (!__prev_n)
1789 return 0;
1791 // We found a matching node, erase it.
1792 __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
1793 _M_erase(__bkt, __prev_n, __n);
1794 return 1;
1797 template<typename _Key, typename _Value,
1798 typename _Alloc, typename _ExtractKey, typename _Equal,
1799 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1800 typename _Traits>
1801 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1802 _H1, _H2, _Hash, _RehashPolicy,
1803 _Traits>::size_type
1804 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1805 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1806 _M_erase(std::false_type, const key_type& __k)
1808 __hash_code __code = this->_M_hash_code(__k);
1809 std::size_t __bkt = _M_bucket_index(__k, __code);
1811 // Look for the node before the first matching node.
1812 __node_base* __prev_n = _M_find_before_node(__bkt, __k, __code);
1813 if (!__prev_n)
1814 return 0;
1816 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1817 // 526. Is it undefined if a function in the standard changes
1818 // in parameters?
1819 // We use one loop to find all matching nodes and another to deallocate
1820 // them so that the key stays valid during the first loop. It might be
1821 // invalidated indirectly when destroying nodes.
1822 __node_type* __n = static_cast<__node_type*>(__prev_n->_M_nxt);
1823 __node_type* __n_last = __n;
1824 std::size_t __n_last_bkt = __bkt;
1827 __n_last = __n_last->_M_next();
1828 if (!__n_last)
1829 break;
1830 __n_last_bkt = _M_bucket_index(__n_last);
1832 while (__n_last_bkt == __bkt && this->_M_equals(__k, __code, __n_last));
1834 // Deallocate nodes.
1835 size_type __result = 0;
1838 __node_type* __p = __n->_M_next();
1839 this->_M_deallocate_node(__n);
1840 __n = __p;
1841 ++__result;
1842 --_M_element_count;
1844 while (__n != __n_last);
1846 if (__prev_n == _M_buckets[__bkt])
1847 _M_remove_bucket_begin(__bkt, __n_last, __n_last_bkt);
1848 else if (__n_last && __n_last_bkt != __bkt)
1849 _M_buckets[__n_last_bkt] = __prev_n;
1850 __prev_n->_M_nxt = __n_last;
1851 return __result;
1854 template<typename _Key, typename _Value,
1855 typename _Alloc, typename _ExtractKey, typename _Equal,
1856 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1857 typename _Traits>
1858 typename _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1859 _H1, _H2, _Hash, _RehashPolicy,
1860 _Traits>::iterator
1861 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1862 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1863 erase(const_iterator __first, const_iterator __last)
1865 __node_type* __n = __first._M_cur;
1866 __node_type* __last_n = __last._M_cur;
1867 if (__n == __last_n)
1868 return iterator(__n);
1870 std::size_t __bkt = _M_bucket_index(__n);
1872 __node_base* __prev_n = _M_get_previous_node(__bkt, __n);
1873 bool __is_bucket_begin = __n == _M_bucket_begin(__bkt);
1874 std::size_t __n_bkt = __bkt;
1875 for (;;)
1879 __node_type* __tmp = __n;
1880 __n = __n->_M_next();
1881 this->_M_deallocate_node(__tmp);
1882 --_M_element_count;
1883 if (!__n)
1884 break;
1885 __n_bkt = _M_bucket_index(__n);
1887 while (__n != __last_n && __n_bkt == __bkt);
1888 if (__is_bucket_begin)
1889 _M_remove_bucket_begin(__bkt, __n, __n_bkt);
1890 if (__n == __last_n)
1891 break;
1892 __is_bucket_begin = true;
1893 __bkt = __n_bkt;
1896 if (__n && (__n_bkt != __bkt || __is_bucket_begin))
1897 _M_buckets[__n_bkt] = __prev_n;
1898 __prev_n->_M_nxt = __n;
1899 return iterator(__n);
1902 template<typename _Key, typename _Value,
1903 typename _Alloc, typename _ExtractKey, typename _Equal,
1904 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1905 typename _Traits>
1906 void
1907 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1908 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1909 clear() noexcept
1911 this->_M_deallocate_nodes(_M_begin());
1912 __builtin_memset(_M_buckets, 0, _M_bucket_count * sizeof(__bucket_type));
1913 _M_element_count = 0;
1914 _M_before_begin._M_nxt = nullptr;
1917 template<typename _Key, typename _Value,
1918 typename _Alloc, typename _ExtractKey, typename _Equal,
1919 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1920 typename _Traits>
1921 void
1922 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1923 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1924 rehash(size_type __n)
1926 const __rehash_state& __saved_state = _M_rehash_policy._M_state();
1927 std::size_t __buckets
1928 = std::max(_M_rehash_policy._M_bkt_for_elements(_M_element_count + 1),
1929 __n);
1930 __buckets = _M_rehash_policy._M_next_bkt(__buckets);
1932 if (__buckets != _M_bucket_count)
1933 _M_rehash(__buckets, __saved_state);
1934 else
1935 // No rehash, restore previous state to keep a consistent state.
1936 _M_rehash_policy._M_reset(__saved_state);
1939 template<typename _Key, typename _Value,
1940 typename _Alloc, typename _ExtractKey, typename _Equal,
1941 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1942 typename _Traits>
1943 void
1944 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1945 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1946 _M_rehash(size_type __n, const __rehash_state& __state)
1948 __try
1950 _M_rehash_aux(__n, __unique_keys());
1952 __catch(...)
1954 // A failure here means that buckets allocation failed. We only
1955 // have to restore hash policy previous state.
1956 _M_rehash_policy._M_reset(__state);
1957 __throw_exception_again;
1961 // Rehash when there is no equivalent elements.
1962 template<typename _Key, typename _Value,
1963 typename _Alloc, typename _ExtractKey, typename _Equal,
1964 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
1965 typename _Traits>
1966 void
1967 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1968 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
1969 _M_rehash_aux(size_type __n, std::true_type)
1971 __bucket_type* __new_buckets = this->_M_allocate_buckets(__n);
1972 __node_type* __p = _M_begin();
1973 _M_before_begin._M_nxt = nullptr;
1974 std::size_t __bbegin_bkt = 0;
1975 while (__p)
1977 __node_type* __next = __p->_M_next();
1978 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
1979 if (!__new_buckets[__bkt])
1981 __p->_M_nxt = _M_before_begin._M_nxt;
1982 _M_before_begin._M_nxt = __p;
1983 __new_buckets[__bkt] = &_M_before_begin;
1984 if (__p->_M_nxt)
1985 __new_buckets[__bbegin_bkt] = __p;
1986 __bbegin_bkt = __bkt;
1988 else
1990 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
1991 __new_buckets[__bkt]->_M_nxt = __p;
1993 __p = __next;
1996 if (__builtin_expect(_M_bucket_count != 0, true))
1997 _M_deallocate_buckets();
1998 _M_bucket_count = __n;
1999 _M_buckets = __new_buckets;
2002 // Rehash when there can be equivalent elements, preserve their relative
2003 // order.
2004 template<typename _Key, typename _Value,
2005 typename _Alloc, typename _ExtractKey, typename _Equal,
2006 typename _H1, typename _H2, typename _Hash, typename _RehashPolicy,
2007 typename _Traits>
2008 void
2009 _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
2010 _H1, _H2, _Hash, _RehashPolicy, _Traits>::
2011 _M_rehash_aux(size_type __n, std::false_type)
2013 __bucket_type* __new_buckets = this->_M_allocate_buckets(__n);
2015 __node_type* __p = _M_begin();
2016 _M_before_begin._M_nxt = nullptr;
2017 std::size_t __bbegin_bkt = 0;
2018 std::size_t __prev_bkt = 0;
2019 __node_type* __prev_p = nullptr;
2020 bool __check_bucket = false;
2022 while (__p)
2024 __node_type* __next = __p->_M_next();
2025 std::size_t __bkt = __hash_code_base::_M_bucket_index(__p, __n);
2027 if (__prev_p && __prev_bkt == __bkt)
2029 // Previous insert was already in this bucket, we insert after
2030 // the previously inserted one to preserve equivalent elements
2031 // relative order.
2032 __p->_M_nxt = __prev_p->_M_nxt;
2033 __prev_p->_M_nxt = __p;
2035 // Inserting after a node in a bucket require to check that we
2036 // haven't change the bucket last node, in this case next
2037 // bucket containing its before begin node must be updated. We
2038 // schedule a check as soon as we move out of the sequence of
2039 // equivalent nodes to limit the number of checks.
2040 __check_bucket = true;
2042 else
2044 if (__check_bucket)
2046 // Check if we shall update the next bucket because of
2047 // insertions into __prev_bkt bucket.
2048 if (__prev_p->_M_nxt)
2050 std::size_t __next_bkt
2051 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(),
2052 __n);
2053 if (__next_bkt != __prev_bkt)
2054 __new_buckets[__next_bkt] = __prev_p;
2056 __check_bucket = false;
2059 if (!__new_buckets[__bkt])
2061 __p->_M_nxt = _M_before_begin._M_nxt;
2062 _M_before_begin._M_nxt = __p;
2063 __new_buckets[__bkt] = &_M_before_begin;
2064 if (__p->_M_nxt)
2065 __new_buckets[__bbegin_bkt] = __p;
2066 __bbegin_bkt = __bkt;
2068 else
2070 __p->_M_nxt = __new_buckets[__bkt]->_M_nxt;
2071 __new_buckets[__bkt]->_M_nxt = __p;
2074 __prev_p = __p;
2075 __prev_bkt = __bkt;
2076 __p = __next;
2079 if (__check_bucket && __prev_p->_M_nxt)
2081 std::size_t __next_bkt
2082 = __hash_code_base::_M_bucket_index(__prev_p->_M_next(), __n);
2083 if (__next_bkt != __prev_bkt)
2084 __new_buckets[__next_bkt] = __prev_p;
2087 if (__builtin_expect(_M_bucket_count != 0, true))
2088 _M_deallocate_buckets();
2089 _M_bucket_count = __n;
2090 _M_buckets = __new_buckets;
2093 _GLIBCXX_END_NAMESPACE_VERSION
2094 } // namespace std
2096 #endif // _HASHTABLE_H