1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010-2020 Free Software Foundation, Inc.
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)
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_policy.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly.
28 * @headername{unordered_map,unordered_set}
31 #ifndef _HASHTABLE_POLICY_H
32 #define _HASHTABLE_POLICY_H 1
34 #include <tuple> // for std::tuple, std::forward_as_tuple
35 #include <limits> // for std::numeric_limits
36 #include <bits/stl_algobase.h> // for std::min.
38 namespace std
_GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 template<typename _Key
, typename _Value
, typename _Alloc
,
43 typename _ExtractKey
, typename _Equal
,
44 typename _H1
, typename _H2
, typename _Hash
,
45 typename _RehashPolicy
, typename _Traits
>
51 * @defgroup hashtable-detail Base and Implementation Classes
52 * @ingroup unordered_associative_containers
55 template<typename _Key
, typename _Value
,
56 typename _ExtractKey
, typename _Equal
,
57 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
58 struct _Hashtable_base
;
60 // Helper function: return distance(first, last) for forward
61 // iterators, or 0/1 for input iterators.
62 template<class _Iterator
>
63 inline typename
std::iterator_traits
<_Iterator
>::difference_type
64 __distance_fw(_Iterator __first
, _Iterator __last
,
65 std::input_iterator_tag
)
66 { return __first
!= __last
? 1 : 0; }
68 template<class _Iterator
>
69 inline typename
std::iterator_traits
<_Iterator
>::difference_type
70 __distance_fw(_Iterator __first
, _Iterator __last
,
71 std::forward_iterator_tag
)
72 { return std::distance(__first
, __last
); }
74 template<class _Iterator
>
75 inline typename
std::iterator_traits
<_Iterator
>::difference_type
76 __distance_fw(_Iterator __first
, _Iterator __last
)
77 { return __distance_fw(__first
, __last
,
78 std::__iterator_category(__first
)); }
82 template<typename _Tp
>
84 operator()(_Tp
&& __x
) const
85 { return std::forward
<_Tp
>(__x
); }
90 template<typename _Tp
>
92 operator()(_Tp
&& __x
) const
93 -> decltype(std::get
<0>(std::forward
<_Tp
>(__x
)))
94 { return std::get
<0>(std::forward
<_Tp
>(__x
)); }
97 template<typename _NodeAlloc
>
98 struct _Hashtable_alloc
;
100 // Functor recycling a pool of nodes and using allocation once the pool is
102 template<typename _NodeAlloc
>
103 struct _ReuseOrAllocNode
106 using __node_alloc_type
= _NodeAlloc
;
107 using __hashtable_alloc
= _Hashtable_alloc
<__node_alloc_type
>;
108 using __node_alloc_traits
=
109 typename
__hashtable_alloc::__node_alloc_traits
;
110 using __node_type
= typename
__hashtable_alloc::__node_type
;
113 _ReuseOrAllocNode(__node_type
* __nodes
, __hashtable_alloc
& __h
)
114 : _M_nodes(__nodes
), _M_h(__h
) { }
115 _ReuseOrAllocNode(const _ReuseOrAllocNode
&) = delete;
118 { _M_h
._M_deallocate_nodes(_M_nodes
); }
120 template<typename _Arg
>
122 operator()(_Arg
&& __arg
) const
126 __node_type
* __node
= _M_nodes
;
127 _M_nodes
= _M_nodes
->_M_next();
128 __node
->_M_nxt
= nullptr;
129 auto& __a
= _M_h
._M_node_allocator();
130 __node_alloc_traits::destroy(__a
, __node
->_M_valptr());
133 __node_alloc_traits::construct(__a
, __node
->_M_valptr(),
134 std::forward
<_Arg
>(__arg
));
138 _M_h
._M_deallocate_node_ptr(__node
);
139 __throw_exception_again
;
143 return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
));
147 mutable __node_type
* _M_nodes
;
148 __hashtable_alloc
& _M_h
;
151 // Functor similar to the previous one but without any pool of nodes to
153 template<typename _NodeAlloc
>
157 using __hashtable_alloc
= _Hashtable_alloc
<_NodeAlloc
>;
158 using __node_type
= typename
__hashtable_alloc::__node_type
;
161 _AllocNode(__hashtable_alloc
& __h
)
164 template<typename _Arg
>
166 operator()(_Arg
&& __arg
) const
167 { return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
)); }
170 __hashtable_alloc
& _M_h
;
173 // Auxiliary types used for all instantiations of _Hashtable nodes
177 * struct _Hashtable_traits
179 * Important traits for hash tables.
181 * @tparam _Cache_hash_code Boolean value. True if the value of
182 * the hash function is stored along with the value. This is a
183 * time-space tradeoff. Storing it may improve lookup speed by
184 * reducing the number of times we need to call the _Hash or _Equal
187 * @tparam _Constant_iterators Boolean value. True if iterator and
188 * const_iterator are both constant iterator types. This is true
189 * for unordered_set and unordered_multiset, false for
190 * unordered_map and unordered_multimap.
192 * @tparam _Unique_keys Boolean value. True if the return value
193 * of _Hashtable::count(k) is always at most one, false if it may
194 * be an arbitrary number. This is true for unordered_set and
195 * unordered_map, false for unordered_multiset and
196 * unordered_multimap.
198 template<bool _Cache_hash_code
, bool _Constant_iterators
, bool _Unique_keys
>
199 struct _Hashtable_traits
201 using __hash_cached
= __bool_constant
<_Cache_hash_code
>;
202 using __constant_iterators
= __bool_constant
<_Constant_iterators
>;
203 using __unique_keys
= __bool_constant
<_Unique_keys
>;
207 * struct _Hash_node_base
209 * Nodes, used to wrap elements stored in the hash table. A policy
210 * template parameter of class template _Hashtable controls whether
211 * nodes also store a hash code. In some cases (e.g. strings) this
212 * may be a performance win.
214 struct _Hash_node_base
216 _Hash_node_base
* _M_nxt
;
218 _Hash_node_base() noexcept
: _M_nxt() { }
220 _Hash_node_base(_Hash_node_base
* __next
) noexcept
: _M_nxt(__next
) { }
224 * struct _Hash_node_value_base
226 * Node type with the value to store.
228 template<typename _Value
>
229 struct _Hash_node_value_base
: _Hash_node_base
231 typedef _Value value_type
;
233 __gnu_cxx::__aligned_buffer
<_Value
> _M_storage
;
237 { return _M_storage
._M_ptr(); }
240 _M_valptr() const noexcept
241 { return _M_storage
._M_ptr(); }
245 { return *_M_valptr(); }
248 _M_v() const noexcept
249 { return *_M_valptr(); }
253 * Primary template struct _Hash_node.
255 template<typename _Value
, bool _Cache_hash_code
>
259 * Specialization for nodes with caches, struct _Hash_node.
261 * Base class is __detail::_Hash_node_value_base.
263 template<typename _Value
>
264 struct _Hash_node
<_Value
, true> : _Hash_node_value_base
<_Value
>
266 std::size_t _M_hash_code
;
269 _M_next() const noexcept
270 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
274 * Specialization for nodes without caches, struct _Hash_node.
276 * Base class is __detail::_Hash_node_value_base.
278 template<typename _Value
>
279 struct _Hash_node
<_Value
, false> : _Hash_node_value_base
<_Value
>
282 _M_next() const noexcept
283 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
286 /// Base class for node iterators.
287 template<typename _Value
, bool _Cache_hash_code
>
288 struct _Node_iterator_base
290 using __node_type
= _Hash_node
<_Value
, _Cache_hash_code
>;
294 _Node_iterator_base(__node_type
* __p
) noexcept
299 { _M_cur
= _M_cur
->_M_next(); }
302 template<typename _Value
, bool _Cache_hash_code
>
304 operator==(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
305 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
307 { return __x
._M_cur
== __y
._M_cur
; }
309 template<typename _Value
, bool _Cache_hash_code
>
311 operator!=(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
312 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
314 { return __x
._M_cur
!= __y
._M_cur
; }
316 /// Node iterators, used to iterate through all the hashtable.
317 template<typename _Value
, bool __constant_iterators
, bool __cache
>
318 struct _Node_iterator
319 : public _Node_iterator_base
<_Value
, __cache
>
322 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
323 using __node_type
= typename
__base_type::__node_type
;
326 typedef _Value value_type
;
327 typedef std::ptrdiff_t difference_type
;
328 typedef std::forward_iterator_tag iterator_category
;
330 using pointer
= typename
std::conditional
<__constant_iterators
,
331 const _Value
*, _Value
*>::type
;
333 using reference
= typename
std::conditional
<__constant_iterators
,
334 const _Value
&, _Value
&>::type
;
336 _Node_iterator() noexcept
340 _Node_iterator(__node_type
* __p
) noexcept
341 : __base_type(__p
) { }
344 operator*() const noexcept
345 { return this->_M_cur
->_M_v(); }
348 operator->() const noexcept
349 { return this->_M_cur
->_M_valptr(); }
352 operator++() noexcept
359 operator++(int) noexcept
361 _Node_iterator
__tmp(*this);
367 /// Node const_iterators, used to iterate through all the hashtable.
368 template<typename _Value
, bool __constant_iterators
, bool __cache
>
369 struct _Node_const_iterator
370 : public _Node_iterator_base
<_Value
, __cache
>
373 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
374 using __node_type
= typename
__base_type::__node_type
;
377 typedef _Value value_type
;
378 typedef std::ptrdiff_t difference_type
;
379 typedef std::forward_iterator_tag iterator_category
;
381 typedef const _Value
* pointer
;
382 typedef const _Value
& reference
;
384 _Node_const_iterator() noexcept
388 _Node_const_iterator(__node_type
* __p
) noexcept
389 : __base_type(__p
) { }
391 _Node_const_iterator(const _Node_iterator
<_Value
, __constant_iterators
,
392 __cache
>& __x
) noexcept
393 : __base_type(__x
._M_cur
) { }
396 operator*() const noexcept
397 { return this->_M_cur
->_M_v(); }
400 operator->() const noexcept
401 { return this->_M_cur
->_M_valptr(); }
403 _Node_const_iterator
&
404 operator++() noexcept
411 operator++(int) noexcept
413 _Node_const_iterator
__tmp(*this);
419 // Many of class template _Hashtable's template parameters are policy
420 // classes. These are defaults for the policies.
422 /// Default range hashing function: use division to fold a large number
423 /// into the range [0, N).
424 struct _Mod_range_hashing
426 typedef std::size_t first_argument_type
;
427 typedef std::size_t second_argument_type
;
428 typedef std::size_t result_type
;
431 operator()(first_argument_type __num
,
432 second_argument_type __den
) const noexcept
433 { return __num
% __den
; }
436 /// Default ranged hash function H. In principle it should be a
437 /// function object composed from objects of type H1 and H2 such that
438 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
439 /// h1 and h2. So instead we'll just use a tag to tell class template
440 /// hashtable to do that composition.
441 struct _Default_ranged_hash
{ };
443 /// Default value for rehash policy. Bucket size is (usually) the
444 /// smallest prime that keeps the load factor small enough.
445 struct _Prime_rehash_policy
447 using __has_load_factor
= true_type
;
449 _Prime_rehash_policy(float __z
= 1.0) noexcept
450 : _M_max_load_factor(__z
), _M_next_resize(0) { }
453 max_load_factor() const noexcept
454 { return _M_max_load_factor
; }
456 // Return a bucket size no smaller than n.
458 _M_next_bkt(std::size_t __n
) const;
460 // Return a bucket count appropriate for n elements
462 _M_bkt_for_elements(std::size_t __n
) const
463 { return __builtin_ceill(__n
/ (long double)_M_max_load_factor
); }
465 // __n_bkt is current bucket count, __n_elt is current element count,
466 // and __n_ins is number of elements to be inserted. Do we need to
467 // increase bucket count? If so, return make_pair(true, n), where n
468 // is the new bucket count. If not, return make_pair(false, 0).
469 std::pair
<bool, std::size_t>
470 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
471 std::size_t __n_ins
) const;
473 typedef std::size_t _State
;
477 { return _M_next_resize
; }
481 { _M_next_resize
= 0; }
484 _M_reset(_State __state
)
485 { _M_next_resize
= __state
; }
487 static const std::size_t _S_growth_factor
= 2;
489 float _M_max_load_factor
;
490 mutable std::size_t _M_next_resize
;
493 /// Range hashing function assuming that second arg is a power of 2.
494 struct _Mask_range_hashing
496 typedef std::size_t first_argument_type
;
497 typedef std::size_t second_argument_type
;
498 typedef std::size_t result_type
;
501 operator()(first_argument_type __num
,
502 second_argument_type __den
) const noexcept
503 { return __num
& (__den
- 1); }
506 /// Compute closest power of 2 not less than __n
508 __clp2(std::size_t __n
) noexcept
510 // Equivalent to return __n ? std::ceil2(__n) : 0;
513 const unsigned __lz
= sizeof(size_t) > sizeof(long)
514 ? __builtin_clzll(__n
- 1ull)
515 : __builtin_clzl(__n
- 1ul);
516 // Doing two shifts avoids undefined behaviour when __lz == 0.
517 return (size_t(1) << (numeric_limits
<size_t>::digits
- __lz
- 1)) << 1;
520 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
522 struct _Power2_rehash_policy
524 using __has_load_factor
= true_type
;
526 _Power2_rehash_policy(float __z
= 1.0) noexcept
527 : _M_max_load_factor(__z
), _M_next_resize(0) { }
530 max_load_factor() const noexcept
531 { return _M_max_load_factor
; }
533 // Return a bucket size no smaller than n (as long as n is not above the
534 // highest power of 2).
536 _M_next_bkt(std::size_t __n
) noexcept
539 // Special case on container 1st initialization with 0 bucket count
540 // hint. We keep _M_next_resize to 0 to make sure that next time we
541 // want to add an element allocation will take place.
544 const auto __max_width
= std::min
<size_t>(sizeof(size_t), 8);
545 const auto __max_bkt
= size_t(1) << (__max_width
* __CHAR_BIT__
- 1);
546 std::size_t __res
= __clp2(__n
);
551 // If __res is 1 we force it to 2 to make sure there will be an
552 // allocation so that nothing need to be stored in the initial
556 if (__res
== __max_bkt
)
557 // Set next resize to the max value so that we never try to rehash again
558 // as we already reach the biggest possible bucket number.
559 // Note that it might result in max_load_factor not being respected.
560 _M_next_resize
= numeric_limits
<size_t>::max();
563 = __builtin_floorl(__res
* (long double)_M_max_load_factor
);
568 // Return a bucket count appropriate for n elements
570 _M_bkt_for_elements(std::size_t __n
) const noexcept
571 { return __builtin_ceill(__n
/ (long double)_M_max_load_factor
); }
573 // __n_bkt is current bucket count, __n_elt is current element count,
574 // and __n_ins is number of elements to be inserted. Do we need to
575 // increase bucket count? If so, return make_pair(true, n), where n
576 // is the new bucket count. If not, return make_pair(false, 0).
577 std::pair
<bool, std::size_t>
578 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
579 std::size_t __n_ins
) noexcept
581 if (__n_elt
+ __n_ins
> _M_next_resize
)
583 // If _M_next_resize is 0 it means that we have nothing allocated so
584 // far and that we start inserting elements. In this case we start
585 // with an initial bucket size of 11.
586 long double __min_bkts
587 = std::max
<std::size_t>(__n_elt
+ __n_ins
, _M_next_resize
? 0 : 11)
588 / (long double)_M_max_load_factor
;
589 if (__min_bkts
>= __n_bkt
)
591 _M_next_bkt(std::max
<std::size_t>(__builtin_floorl(__min_bkts
) + 1,
592 __n_bkt
* _S_growth_factor
)) };
595 = __builtin_floorl(__n_bkt
* (long double)_M_max_load_factor
);
602 typedef std::size_t _State
;
605 _M_state() const noexcept
606 { return _M_next_resize
; }
610 { _M_next_resize
= 0; }
613 _M_reset(_State __state
) noexcept
614 { _M_next_resize
= __state
; }
616 static const std::size_t _S_growth_factor
= 2;
618 float _M_max_load_factor
;
619 std::size_t _M_next_resize
;
622 // Base classes for std::_Hashtable. We define these base classes
623 // because in some cases we want to do different things depending on
624 // the value of a policy class. In some cases the policy class
625 // affects which member functions and nested typedefs are defined;
626 // we handle that by specializing base class templates. Several of
627 // the base class templates need to access other members of class
628 // template _Hashtable, so we use a variant of the "Curiously
629 // Recurring Template Pattern" (CRTP) technique.
632 * Primary class template _Map_base.
634 * If the hashtable has a value type of the form pair<T1, T2> and a
635 * key extraction policy (_ExtractKey) that returns the first part
636 * of the pair, the hashtable gets a mapped_type typedef. If it
637 * satisfies those criteria and also has unique keys, then it also
638 * gets an operator[].
640 template<typename _Key
, typename _Value
, typename _Alloc
,
641 typename _ExtractKey
, typename _Equal
,
642 typename _H1
, typename _H2
, typename _Hash
,
643 typename _RehashPolicy
, typename _Traits
,
644 bool _Unique_keys
= _Traits::__unique_keys::value
>
645 struct _Map_base
{ };
647 /// Partial specialization, __unique_keys set to false.
648 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
649 typename _H1
, typename _H2
, typename _Hash
,
650 typename _RehashPolicy
, typename _Traits
>
651 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
652 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
654 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
657 /// Partial specialization, __unique_keys set to true.
658 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
659 typename _H1
, typename _H2
, typename _Hash
,
660 typename _RehashPolicy
, typename _Traits
>
661 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
662 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
665 using __hashtable_base
= __detail::_Hashtable_base
<_Key
, _Pair
,
667 _Equal
, _H1
, _H2
, _Hash
,
670 using __hashtable
= _Hashtable
<_Key
, _Pair
, _Alloc
,
672 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
674 using __hash_code
= typename
__hashtable_base::__hash_code
;
675 using __node_type
= typename
__hashtable_base::__node_type
;
678 using key_type
= typename
__hashtable_base::key_type
;
679 using iterator
= typename
__hashtable_base::iterator
;
680 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
683 operator[](const key_type
& __k
);
686 operator[](key_type
&& __k
);
688 // _GLIBCXX_RESOLVE_LIB_DEFECTS
689 // DR 761. unordered_map needs an at() member function.
691 at(const key_type
& __k
);
694 at(const key_type
& __k
) const;
697 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
698 typename _H1
, typename _H2
, typename _Hash
,
699 typename _RehashPolicy
, typename _Traits
>
701 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
702 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
703 operator[](const key_type
& __k
)
706 __hashtable
* __h
= static_cast<__hashtable
*>(this);
707 __hash_code __code
= __h
->_M_hash_code(__k
);
708 std::size_t __bkt
= __h
->_M_bucket_index(__k
, __code
);
709 if (__node_type
* __node
= __h
->_M_find_node(__bkt
, __k
, __code
))
710 return __node
->_M_v().second
;
712 typename
__hashtable::_Scoped_node __node
{
714 std::piecewise_construct
,
715 std::tuple
<const key_type
&>(__k
),
719 = __h
->_M_insert_unique_node(__k
, __bkt
, __code
, __node
._M_node
);
720 __node
._M_node
= nullptr;
721 return __pos
->second
;
724 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
725 typename _H1
, typename _H2
, typename _Hash
,
726 typename _RehashPolicy
, typename _Traits
>
728 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
729 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
730 operator[](key_type
&& __k
)
733 __hashtable
* __h
= static_cast<__hashtable
*>(this);
734 __hash_code __code
= __h
->_M_hash_code(__k
);
735 std::size_t __bkt
= __h
->_M_bucket_index(__k
, __code
);
736 if (__node_type
* __node
= __h
->_M_find_node(__bkt
, __k
, __code
))
737 return __node
->_M_v().second
;
739 typename
__hashtable::_Scoped_node __node
{
741 std::piecewise_construct
,
742 std::forward_as_tuple(std::move(__k
)),
746 = __h
->_M_insert_unique_node(__k
, __bkt
, __code
, __node
._M_node
);
747 __node
._M_node
= nullptr;
748 return __pos
->second
;
751 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
752 typename _H1
, typename _H2
, typename _Hash
,
753 typename _RehashPolicy
, typename _Traits
>
755 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
756 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
757 at(const key_type
& __k
)
760 __hashtable
* __h
= static_cast<__hashtable
*>(this);
761 __hash_code __code
= __h
->_M_hash_code(__k
);
762 std::size_t __bkt
= __h
->_M_bucket_index(__k
, __code
);
763 __node_type
* __p
= __h
->_M_find_node(__bkt
, __k
, __code
);
766 __throw_out_of_range(__N("_Map_base::at"));
767 return __p
->_M_v().second
;
770 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
771 typename _H1
, typename _H2
, typename _Hash
,
772 typename _RehashPolicy
, typename _Traits
>
774 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
775 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
776 at(const key_type
& __k
) const
777 -> const mapped_type
&
779 const __hashtable
* __h
= static_cast<const __hashtable
*>(this);
780 __hash_code __code
= __h
->_M_hash_code(__k
);
781 std::size_t __bkt
= __h
->_M_bucket_index(__k
, __code
);
782 __node_type
* __p
= __h
->_M_find_node(__bkt
, __k
, __code
);
785 __throw_out_of_range(__N("_Map_base::at"));
786 return __p
->_M_v().second
;
790 * Primary class template _Insert_base.
792 * Defines @c insert member functions appropriate to all _Hashtables.
794 template<typename _Key
, typename _Value
, typename _Alloc
,
795 typename _ExtractKey
, typename _Equal
,
796 typename _H1
, typename _H2
, typename _Hash
,
797 typename _RehashPolicy
, typename _Traits
>
801 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
802 _Equal
, _H1
, _H2
, _Hash
,
803 _RehashPolicy
, _Traits
>;
805 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
806 _Equal
, _H1
, _H2
, _Hash
,
809 using value_type
= typename
__hashtable_base::value_type
;
810 using iterator
= typename
__hashtable_base::iterator
;
811 using const_iterator
= typename
__hashtable_base::const_iterator
;
812 using size_type
= typename
__hashtable_base::size_type
;
814 using __unique_keys
= typename
__hashtable_base::__unique_keys
;
815 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
816 using __node_type
= _Hash_node
<_Value
, _Traits::__hash_cached::value
>;
817 using __node_alloc_type
= __alloc_rebind
<_Alloc
, __node_type
>;
818 using __node_gen_type
= _AllocNode
<__node_alloc_type
>;
821 _M_conjure_hashtable()
822 { return *(static_cast<__hashtable
*>(this)); }
824 template<typename _InputIterator
, typename _NodeGetter
>
826 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
827 const _NodeGetter
&, true_type
);
829 template<typename _InputIterator
, typename _NodeGetter
>
831 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
832 const _NodeGetter
&, false_type
);
836 insert(const value_type
& __v
)
838 __hashtable
& __h
= _M_conjure_hashtable();
839 __node_gen_type
__node_gen(__h
);
840 return __h
._M_insert(__v
, __node_gen
, __unique_keys());
844 insert(const_iterator __hint
, const value_type
& __v
)
846 __hashtable
& __h
= _M_conjure_hashtable();
847 __node_gen_type
__node_gen(__h
);
848 return __h
._M_insert(__hint
, __v
, __node_gen
, __unique_keys());
852 insert(initializer_list
<value_type
> __l
)
853 { this->insert(__l
.begin(), __l
.end()); }
855 template<typename _InputIterator
>
857 insert(_InputIterator __first
, _InputIterator __last
)
859 __hashtable
& __h
= _M_conjure_hashtable();
860 __node_gen_type
__node_gen(__h
);
861 return _M_insert_range(__first
, __last
, __node_gen
, __unique_keys());
865 template<typename _Key
, typename _Value
, typename _Alloc
,
866 typename _ExtractKey
, typename _Equal
,
867 typename _H1
, typename _H2
, typename _Hash
,
868 typename _RehashPolicy
, typename _Traits
>
869 template<typename _InputIterator
, typename _NodeGetter
>
871 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
872 _RehashPolicy
, _Traits
>::
873 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
874 const _NodeGetter
& __node_gen
, true_type
)
876 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
880 __hashtable
& __h
= _M_conjure_hashtable();
881 for (; __first
!= __last
; ++__first
)
883 if (__h
._M_insert(*__first
, __node_gen
, __unique_keys(),
886 else if (__n_elt
!= 1)
891 template<typename _Key
, typename _Value
, typename _Alloc
,
892 typename _ExtractKey
, typename _Equal
,
893 typename _H1
, typename _H2
, typename _Hash
,
894 typename _RehashPolicy
, typename _Traits
>
895 template<typename _InputIterator
, typename _NodeGetter
>
897 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
898 _RehashPolicy
, _Traits
>::
899 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
900 const _NodeGetter
& __node_gen
, false_type
)
902 using __rehash_type
= typename
__hashtable::__rehash_type
;
903 using __rehash_state
= typename
__hashtable::__rehash_state
;
904 using pair_type
= std::pair
<bool, std::size_t>;
906 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
910 __hashtable
& __h
= _M_conjure_hashtable();
911 __rehash_type
& __rehash
= __h
._M_rehash_policy
;
912 const __rehash_state
& __saved_state
= __rehash
._M_state();
913 pair_type __do_rehash
= __rehash
._M_need_rehash(__h
._M_bucket_count
,
914 __h
._M_element_count
,
917 if (__do_rehash
.first
)
918 __h
._M_rehash(__do_rehash
.second
, __saved_state
);
920 for (; __first
!= __last
; ++__first
)
921 __h
._M_insert(*__first
, __node_gen
, __unique_keys());
925 * Primary class template _Insert.
927 * Defines @c insert member functions that depend on _Hashtable policies,
928 * via partial specializations.
930 template<typename _Key
, typename _Value
, typename _Alloc
,
931 typename _ExtractKey
, typename _Equal
,
932 typename _H1
, typename _H2
, typename _Hash
,
933 typename _RehashPolicy
, typename _Traits
,
934 bool _Constant_iterators
= _Traits::__constant_iterators::value
>
938 template<typename _Key
, typename _Value
, typename _Alloc
,
939 typename _ExtractKey
, typename _Equal
,
940 typename _H1
, typename _H2
, typename _Hash
,
941 typename _RehashPolicy
, typename _Traits
>
942 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
943 _RehashPolicy
, _Traits
, true>
944 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
945 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
947 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
948 _Equal
, _H1
, _H2
, _Hash
,
949 _RehashPolicy
, _Traits
>;
951 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
952 _Equal
, _H1
, _H2
, _Hash
,
955 using value_type
= typename
__base_type::value_type
;
956 using iterator
= typename
__base_type::iterator
;
957 using const_iterator
= typename
__base_type::const_iterator
;
959 using __unique_keys
= typename
__base_type::__unique_keys
;
960 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
961 using __hashtable
= typename
__base_type::__hashtable
;
962 using __node_gen_type
= typename
__base_type::__node_gen_type
;
964 using __base_type::insert
;
967 insert(value_type
&& __v
)
969 __hashtable
& __h
= this->_M_conjure_hashtable();
970 __node_gen_type
__node_gen(__h
);
971 return __h
._M_insert(std::move(__v
), __node_gen
, __unique_keys());
975 insert(const_iterator __hint
, value_type
&& __v
)
977 __hashtable
& __h
= this->_M_conjure_hashtable();
978 __node_gen_type
__node_gen(__h
);
979 return __h
._M_insert(__hint
, std::move(__v
), __node_gen
,
985 template<typename _Key
, typename _Value
, typename _Alloc
,
986 typename _ExtractKey
, typename _Equal
,
987 typename _H1
, typename _H2
, typename _Hash
,
988 typename _RehashPolicy
, typename _Traits
>
989 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
990 _RehashPolicy
, _Traits
, false>
991 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
992 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
994 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
995 _Equal
, _H1
, _H2
, _Hash
,
996 _RehashPolicy
, _Traits
>;
997 using value_type
= typename
__base_type::value_type
;
998 using iterator
= typename
__base_type::iterator
;
999 using const_iterator
= typename
__base_type::const_iterator
;
1001 using __unique_keys
= typename
__base_type::__unique_keys
;
1002 using __hashtable
= typename
__base_type::__hashtable
;
1003 using __ireturn_type
= typename
__base_type::__ireturn_type
;
1005 using __base_type::insert
;
1007 template<typename _Pair
>
1008 using __is_cons
= std::is_constructible
<value_type
, _Pair
&&>;
1010 template<typename _Pair
>
1011 using _IFcons
= std::enable_if
<__is_cons
<_Pair
>::value
>;
1013 template<typename _Pair
>
1014 using _IFconsp
= typename _IFcons
<_Pair
>::type
;
1016 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
1020 __hashtable
& __h
= this->_M_conjure_hashtable();
1021 return __h
._M_emplace(__unique_keys(), std::forward
<_Pair
>(__v
));
1024 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
1026 insert(const_iterator __hint
, _Pair
&& __v
)
1028 __hashtable
& __h
= this->_M_conjure_hashtable();
1029 return __h
._M_emplace(__hint
, __unique_keys(),
1030 std::forward
<_Pair
>(__v
));
1034 template<typename _Policy
>
1035 using __has_load_factor
= typename
_Policy::__has_load_factor
;
1038 * Primary class template _Rehash_base.
1040 * Give hashtable the max_load_factor functions and reserve iff the
1041 * rehash policy supports it.
1043 template<typename _Key
, typename _Value
, typename _Alloc
,
1044 typename _ExtractKey
, typename _Equal
,
1045 typename _H1
, typename _H2
, typename _Hash
,
1046 typename _RehashPolicy
, typename _Traits
,
1048 __detected_or_t
<false_type
, __has_load_factor
, _RehashPolicy
>>
1049 struct _Rehash_base
;
1051 /// Specialization when rehash policy doesn't provide load factor management.
1052 template<typename _Key
, typename _Value
, typename _Alloc
,
1053 typename _ExtractKey
, typename _Equal
,
1054 typename _H1
, typename _H2
, typename _Hash
,
1055 typename _RehashPolicy
, typename _Traits
>
1056 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1057 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1062 /// Specialization when rehash policy provide load factor management.
1063 template<typename _Key
, typename _Value
, typename _Alloc
,
1064 typename _ExtractKey
, typename _Equal
,
1065 typename _H1
, typename _H2
, typename _Hash
,
1066 typename _RehashPolicy
, typename _Traits
>
1067 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1068 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1071 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1072 _Equal
, _H1
, _H2
, _Hash
,
1073 _RehashPolicy
, _Traits
>;
1076 max_load_factor() const noexcept
1078 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1079 return __this
->__rehash_policy().max_load_factor();
1083 max_load_factor(float __z
)
1085 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1086 __this
->__rehash_policy(_RehashPolicy(__z
));
1090 reserve(std::size_t __n
)
1092 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1093 __this
->rehash(__this
->__rehash_policy()._M_bkt_for_elements(__n
));
1098 * Primary class template _Hashtable_ebo_helper.
1100 * Helper class using EBO when it is not forbidden (the type is not
1101 * final) and when it is worth it (the type is empty.)
1103 template<int _Nm
, typename _Tp
,
1104 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
1105 struct _Hashtable_ebo_helper
;
1107 /// Specialization using EBO.
1108 template<int _Nm
, typename _Tp
>
1109 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, true>
1112 _Hashtable_ebo_helper() = default;
1114 template<typename _OtherTp
>
1115 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1116 : _Tp(std::forward
<_OtherTp
>(__tp
))
1119 const _Tp
& _M_cget() const { return static_cast<const _Tp
&>(*this); }
1120 _Tp
& _M_get() { return static_cast<_Tp
&>(*this); }
1123 /// Specialization not using EBO.
1124 template<int _Nm
, typename _Tp
>
1125 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, false>
1127 _Hashtable_ebo_helper() = default;
1129 template<typename _OtherTp
>
1130 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1131 : _M_tp(std::forward
<_OtherTp
>(__tp
))
1134 const _Tp
& _M_cget() const { return _M_tp
; }
1135 _Tp
& _M_get() { return _M_tp
; }
1142 * Primary class template _Local_iterator_base.
1144 * Base class for local iterators, used to iterate within a bucket
1145 * but not between buckets.
1147 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1148 typename _H1
, typename _H2
, typename _Hash
,
1149 bool __cache_hash_code
>
1150 struct _Local_iterator_base
;
1153 * Primary class template _Hash_code_base.
1155 * Encapsulates two policy issues that aren't quite orthogonal.
1156 * (1) the difference between using a ranged hash function and using
1157 * the combination of a hash function and a range-hashing function.
1158 * In the former case we don't have such things as hash codes, so
1159 * we have a dummy type as placeholder.
1160 * (2) Whether or not we cache hash codes. Caching hash codes is
1161 * meaningless if we have a ranged hash function.
1163 * We also put the key extraction objects here, for convenience.
1164 * Each specialization derives from one or more of the template
1165 * parameters to benefit from Ebo. This is important as this type
1166 * is inherited in some cases by the _Local_iterator_base type used
1167 * to implement local_iterator and const_local_iterator. As with
1168 * any iterator type we prefer to make it as small as possible.
1170 * Primary template is unused except as a hook for specializations.
1172 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1173 typename _H1
, typename _H2
, typename _Hash
,
1174 bool __cache_hash_code
>
1175 struct _Hash_code_base
;
1177 /// Specialization: ranged hash function, no caching hash codes. H1
1178 /// and H2 are provided but ignored. We define a dummy hash code type.
1179 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1180 typename _H1
, typename _H2
, typename _Hash
>
1181 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, false>
1182 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1183 private _Hashtable_ebo_helper
<1, _Hash
>
1186 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1187 using __ebo_hash
= _Hashtable_ebo_helper
<1, _Hash
>;
1190 typedef void* __hash_code
;
1191 typedef _Hash_node
<_Value
, false> __node_type
;
1193 // We need the default constructor for the local iterators and _Hashtable
1194 // default constructor.
1195 _Hash_code_base() = default;
1197 _Hash_code_base(const _ExtractKey
& __ex
, const _H1
&, const _H2
&,
1199 : __ebo_extract_key(__ex
), __ebo_hash(__h
) { }
1202 _M_hash_code(const _Key
& __key
) const
1206 _M_bucket_index(const _Key
& __k
, __hash_code
,
1207 std::size_t __bkt_count
) const
1208 { return _M_ranged_hash()(__k
, __bkt_count
); }
1211 _M_bucket_index(const __node_type
* __p
, std::size_t __bkt_count
) const
1212 noexcept( noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>(),
1214 { return _M_ranged_hash()(_M_extract()(__p
->_M_v()), __bkt_count
); }
1217 _M_store_code(__node_type
*, __hash_code
) const
1221 _M_copy_code(__node_type
*, const __node_type
*) const
1225 _M_swap(_Hash_code_base
& __x
)
1227 std::swap(__ebo_extract_key::_M_get(),
1228 __x
.__ebo_extract_key::_M_get());
1229 std::swap(__ebo_hash::_M_get(), __x
.__ebo_hash::_M_get());
1233 _M_extract() const { return __ebo_extract_key::_M_cget(); }
1236 _M_ranged_hash() const { return __ebo_hash::_M_cget(); }
1239 // No specialization for ranged hash function while caching hash codes.
1240 // That combination is meaningless, and trying to do it is an error.
1242 /// Specialization: ranged hash function, cache hash codes. This
1243 /// combination is meaningless, so we provide only a declaration
1244 /// and no definition.
1245 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1246 typename _H1
, typename _H2
, typename _Hash
>
1247 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, true>;
1249 /// Specialization: hash function and range-hashing function, no
1250 /// caching of hash codes.
1251 /// Provides typedef and accessor required by C++ 11.
1252 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1253 typename _H1
, typename _H2
>
1254 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1255 _Default_ranged_hash
, false>
1256 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1257 private _Hashtable_ebo_helper
<1, _H1
>,
1258 private _Hashtable_ebo_helper
<2, _H2
>
1261 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1262 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1263 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1265 // Gives the local iterator implementation access to _M_bucket_index().
1266 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1267 _Default_ranged_hash
, false>;
1273 hash_function() const
1277 typedef std::size_t __hash_code
;
1278 typedef _Hash_node
<_Value
, false> __node_type
;
1280 // We need the default constructor for the local iterators and _Hashtable
1281 // default constructor.
1282 _Hash_code_base() = default;
1284 _Hash_code_base(const _ExtractKey
& __ex
,
1285 const _H1
& __h1
, const _H2
& __h2
,
1286 const _Default_ranged_hash
&)
1287 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1290 _M_hash_code(const _Key
& __k
) const
1292 static_assert(__is_invocable
<const _H1
&, const _Key
&>{},
1293 "hash function must be invocable with an argument of key type");
1294 return _M_h1()(__k
);
1298 _M_bucket_index(const _Key
&, __hash_code __c
,
1299 std::size_t __bkt_count
) const
1300 { return _M_h2()(__c
, __bkt_count
); }
1303 _M_bucket_index(const __node_type
* __p
, std::size_t __bkt_count
) const
1304 noexcept( noexcept(declval
<const _H1
&>()(declval
<const _Key
&>()))
1305 && noexcept(declval
<const _H2
&>()((__hash_code
)0,
1307 { return _M_h2()(_M_h1()(_M_extract()(__p
->_M_v())), __bkt_count
); }
1310 _M_store_code(__node_type
*, __hash_code
) const
1314 _M_copy_code(__node_type
*, const __node_type
*) const
1318 _M_swap(_Hash_code_base
& __x
)
1320 std::swap(__ebo_extract_key::_M_get(),
1321 __x
.__ebo_extract_key::_M_get());
1322 std::swap(__ebo_h1::_M_get(), __x
.__ebo_h1::_M_get());
1323 std::swap(__ebo_h2::_M_get(), __x
.__ebo_h2::_M_get());
1327 _M_extract() const { return __ebo_extract_key::_M_cget(); }
1330 _M_h1() const { return __ebo_h1::_M_cget(); }
1333 _M_h2() const { return __ebo_h2::_M_cget(); }
1336 /// Specialization: hash function and range-hashing function,
1337 /// caching hash codes. H is provided but ignored. Provides
1338 /// typedef and accessor required by C++ 11.
1339 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1340 typename _H1
, typename _H2
>
1341 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1342 _Default_ranged_hash
, true>
1343 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1344 private _Hashtable_ebo_helper
<1, _H1
>,
1345 private _Hashtable_ebo_helper
<2, _H2
>
1348 // Gives the local iterator implementation access to _M_h2().
1349 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1350 _Default_ranged_hash
, true>;
1352 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1353 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1354 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1360 hash_function() const
1364 typedef std::size_t __hash_code
;
1365 typedef _Hash_node
<_Value
, true> __node_type
;
1367 // We need the default constructor for _Hashtable default constructor.
1368 _Hash_code_base() = default;
1369 _Hash_code_base(const _ExtractKey
& __ex
,
1370 const _H1
& __h1
, const _H2
& __h2
,
1371 const _Default_ranged_hash
&)
1372 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1375 _M_hash_code(const _Key
& __k
) const
1377 static_assert(__is_invocable
<const _H1
&, const _Key
&>{},
1378 "hash function must be invocable with an argument of key type");
1379 return _M_h1()(__k
);
1383 _M_bucket_index(const _Key
&, __hash_code __c
,
1384 std::size_t __bkt_count
) const
1385 { return _M_h2()(__c
, __bkt_count
); }
1388 _M_bucket_index(const __node_type
* __p
, std::size_t __bkt_count
) const
1389 noexcept( noexcept(declval
<const _H2
&>()((__hash_code
)0,
1391 { return _M_h2()(__p
->_M_hash_code
, __bkt_count
); }
1394 _M_store_code(__node_type
* __n
, __hash_code __c
) const
1395 { __n
->_M_hash_code
= __c
; }
1398 _M_copy_code(__node_type
* __to
, const __node_type
* __from
) const
1399 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
1402 _M_swap(_Hash_code_base
& __x
)
1404 std::swap(__ebo_extract_key::_M_get(),
1405 __x
.__ebo_extract_key::_M_get());
1406 std::swap(__ebo_h1::_M_get(), __x
.__ebo_h1::_M_get());
1407 std::swap(__ebo_h2::_M_get(), __x
.__ebo_h2::_M_get());
1411 _M_extract() const { return __ebo_extract_key::_M_cget(); }
1414 _M_h1() const { return __ebo_h1::_M_cget(); }
1417 _M_h2() const { return __ebo_h2::_M_cget(); }
1420 /// Partial specialization used when nodes contain a cached hash code.
1421 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1422 typename _H1
, typename _H2
, typename _Hash
>
1423 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1424 _H1
, _H2
, _Hash
, true>
1425 : private _Hashtable_ebo_helper
<0, _H2
>
1428 using __base_type
= _Hashtable_ebo_helper
<0, _H2
>;
1429 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1430 _H1
, _H2
, _Hash
, true>;
1432 _Local_iterator_base() = default;
1433 _Local_iterator_base(const __hash_code_base
& __base
,
1434 _Hash_node
<_Value
, true>* __p
,
1435 std::size_t __bkt
, std::size_t __bkt_count
)
1436 : __base_type(__base
._M_h2()),
1437 _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1442 _M_cur
= _M_cur
->_M_next();
1446 = __base_type::_M_get()(_M_cur
->_M_hash_code
,
1448 if (__bkt
!= _M_bucket
)
1453 _Hash_node
<_Value
, true>* _M_cur
;
1454 std::size_t _M_bucket
;
1455 std::size_t _M_bucket_count
;
1459 _M_curr() const { return _M_cur
; } // for equality ops
1462 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1465 // Uninitialized storage for a _Hash_code_base.
1466 // This type is DefaultConstructible and Assignable even if the
1467 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1468 // can be DefaultConstructible and Assignable.
1469 template<typename _Tp
, bool _IsEmpty
= std::is_empty
<_Tp
>::value
>
1470 struct _Hash_code_storage
1472 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
1475 _M_h() { return _M_storage
._M_ptr(); }
1478 _M_h() const { return _M_storage
._M_ptr(); }
1481 // Empty partial specialization for empty _Hash_code_base types.
1482 template<typename _Tp
>
1483 struct _Hash_code_storage
<_Tp
, true>
1485 static_assert( std::is_empty
<_Tp
>::value
, "Type must be empty" );
1487 // As _Tp is an empty type there will be no bytes written/read through
1488 // the cast pointer, so no strict-aliasing violation.
1490 _M_h() { return reinterpret_cast<_Tp
*>(this); }
1493 _M_h() const { return reinterpret_cast<const _Tp
*>(this); }
1496 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1497 typename _H1
, typename _H2
, typename _Hash
>
1498 using __hash_code_for_local_iter
1499 = _Hash_code_storage
<_Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1500 _H1
, _H2
, _Hash
, false>>;
1502 // Partial specialization used when hash codes are not cached
1503 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1504 typename _H1
, typename _H2
, typename _Hash
>
1505 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1506 _H1
, _H2
, _Hash
, false>
1507 : __hash_code_for_local_iter
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
>
1510 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1511 _H1
, _H2
, _Hash
, false>;
1513 _Local_iterator_base() : _M_bucket_count(-1) { }
1515 _Local_iterator_base(const __hash_code_base
& __base
,
1516 _Hash_node
<_Value
, false>* __p
,
1517 std::size_t __bkt
, std::size_t __bkt_count
)
1518 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
)
1519 { _M_init(__base
); }
1521 ~_Local_iterator_base()
1523 if (_M_bucket_count
!= -1)
1527 _Local_iterator_base(const _Local_iterator_base
& __iter
)
1528 : _M_cur(__iter
._M_cur
), _M_bucket(__iter
._M_bucket
),
1529 _M_bucket_count(__iter
._M_bucket_count
)
1531 if (_M_bucket_count
!= -1)
1532 _M_init(*__iter
._M_h());
1535 _Local_iterator_base
&
1536 operator=(const _Local_iterator_base
& __iter
)
1538 if (_M_bucket_count
!= -1)
1540 _M_cur
= __iter
._M_cur
;
1541 _M_bucket
= __iter
._M_bucket
;
1542 _M_bucket_count
= __iter
._M_bucket_count
;
1543 if (_M_bucket_count
!= -1)
1544 _M_init(*__iter
._M_h());
1551 _M_cur
= _M_cur
->_M_next();
1554 std::size_t __bkt
= this->_M_h()->_M_bucket_index(_M_cur
,
1556 if (__bkt
!= _M_bucket
)
1561 _Hash_node
<_Value
, false>* _M_cur
;
1562 std::size_t _M_bucket
;
1563 std::size_t _M_bucket_count
;
1566 _M_init(const __hash_code_base
& __base
)
1567 { ::new(this->_M_h()) __hash_code_base(__base
); }
1570 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1574 _M_curr() const { return _M_cur
; } // for equality ops and debug mode
1577 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1580 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1581 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1583 operator==(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1584 _H1
, _H2
, _Hash
, __cache
>& __x
,
1585 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1586 _H1
, _H2
, _Hash
, __cache
>& __y
)
1587 { return __x
._M_curr() == __y
._M_curr(); }
1589 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1590 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1592 operator!=(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1593 _H1
, _H2
, _Hash
, __cache
>& __x
,
1594 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1595 _H1
, _H2
, _Hash
, __cache
>& __y
)
1596 { return __x
._M_curr() != __y
._M_curr(); }
1599 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1600 typename _H1
, typename _H2
, typename _Hash
,
1601 bool __constant_iterators
, bool __cache
>
1602 struct _Local_iterator
1603 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1604 _H1
, _H2
, _Hash
, __cache
>
1607 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1608 _H1
, _H2
, _Hash
, __cache
>;
1609 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1611 typedef _Value value_type
;
1612 typedef typename
std::conditional
<__constant_iterators
,
1613 const _Value
*, _Value
*>::type
1615 typedef typename
std::conditional
<__constant_iterators
,
1616 const _Value
&, _Value
&>::type
1618 typedef std::ptrdiff_t difference_type
;
1619 typedef std::forward_iterator_tag iterator_category
;
1621 _Local_iterator() = default;
1623 _Local_iterator(const __hash_code_base
& __base
,
1624 _Hash_node
<_Value
, __cache
>* __n
,
1625 std::size_t __bkt
, std::size_t __bkt_count
)
1626 : __base_type(__base
, __n
, __bkt
, __bkt_count
)
1631 { return this->_M_cur
->_M_v(); }
1635 { return this->_M_cur
->_M_valptr(); }
1647 _Local_iterator
__tmp(*this);
1653 /// local const_iterators
1654 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1655 typename _H1
, typename _H2
, typename _Hash
,
1656 bool __constant_iterators
, bool __cache
>
1657 struct _Local_const_iterator
1658 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1659 _H1
, _H2
, _Hash
, __cache
>
1662 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1663 _H1
, _H2
, _Hash
, __cache
>;
1664 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1667 typedef _Value value_type
;
1668 typedef const _Value
* pointer
;
1669 typedef const _Value
& reference
;
1670 typedef std::ptrdiff_t difference_type
;
1671 typedef std::forward_iterator_tag iterator_category
;
1673 _Local_const_iterator() = default;
1675 _Local_const_iterator(const __hash_code_base
& __base
,
1676 _Hash_node
<_Value
, __cache
>* __n
,
1677 std::size_t __bkt
, std::size_t __bkt_count
)
1678 : __base_type(__base
, __n
, __bkt
, __bkt_count
)
1681 _Local_const_iterator(const _Local_iterator
<_Key
, _Value
, _ExtractKey
,
1683 __constant_iterators
,
1690 { return this->_M_cur
->_M_v(); }
1694 { return this->_M_cur
->_M_valptr(); }
1696 _Local_const_iterator
&
1703 _Local_const_iterator
1706 _Local_const_iterator
__tmp(*this);
1713 * Primary class template _Hashtable_base.
1715 * Helper class adding management of _Equal functor to
1716 * _Hash_code_base type.
1718 * Base class templates are:
1719 * - __detail::_Hash_code_base
1720 * - __detail::_Hashtable_ebo_helper
1722 template<typename _Key
, typename _Value
,
1723 typename _ExtractKey
, typename _Equal
,
1724 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
1725 struct _Hashtable_base
1726 : public _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1727 _Traits::__hash_cached::value
>,
1728 private _Hashtable_ebo_helper
<0, _Equal
>
1731 typedef _Key key_type
;
1732 typedef _Value value_type
;
1733 typedef _Equal key_equal
;
1734 typedef std::size_t size_type
;
1735 typedef std::ptrdiff_t difference_type
;
1737 using __traits_type
= _Traits
;
1738 using __hash_cached
= typename
__traits_type::__hash_cached
;
1739 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
1740 using __unique_keys
= typename
__traits_type::__unique_keys
;
1742 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1744 __hash_cached::value
>;
1746 using __hash_code
= typename
__hash_code_base::__hash_code
;
1747 using __node_type
= typename
__hash_code_base::__node_type
;
1749 using iterator
= __detail::_Node_iterator
<value_type
,
1750 __constant_iterators::value
,
1751 __hash_cached::value
>;
1753 using const_iterator
= __detail::_Node_const_iterator
<value_type
,
1754 __constant_iterators::value
,
1755 __hash_cached::value
>;
1757 using local_iterator
= __detail::_Local_iterator
<key_type
, value_type
,
1758 _ExtractKey
, _H1
, _H2
, _Hash
,
1759 __constant_iterators::value
,
1760 __hash_cached::value
>;
1762 using const_local_iterator
= __detail::_Local_const_iterator
<key_type
,
1764 _ExtractKey
, _H1
, _H2
, _Hash
,
1765 __constant_iterators::value
,
1766 __hash_cached::value
>;
1768 using __ireturn_type
= typename
std::conditional
<__unique_keys::value
,
1769 std::pair
<iterator
, bool>,
1772 using _EqualEBO
= _Hashtable_ebo_helper
<0, _Equal
>;
1774 template<typename _NodeT
>
1775 struct _Equal_hash_code
1778 _S_equals(__hash_code
, const _NodeT
&)
1782 template<typename _Ptr2
>
1783 struct _Equal_hash_code
<_Hash_node
<_Ptr2
, true>>
1786 _S_equals(__hash_code __c
, const _Hash_node
<_Ptr2
, true>& __n
)
1787 { return __c
== __n
._M_hash_code
; }
1791 _Hashtable_base() = default;
1792 _Hashtable_base(const _ExtractKey
& __ex
, const _H1
& __h1
, const _H2
& __h2
,
1793 const _Hash
& __hash
, const _Equal
& __eq
)
1794 : __hash_code_base(__ex
, __h1
, __h2
, __hash
), _EqualEBO(__eq
)
1798 _M_equals(const _Key
& __k
, __hash_code __c
, __node_type
* __n
) const
1800 static_assert(__is_invocable
<const _Equal
&, const _Key
&, const _Key
&>{},
1801 "key equality predicate must be invocable with two arguments of "
1803 return _Equal_hash_code
<__node_type
>::_S_equals(__c
, *__n
)
1804 && _M_eq()(__k
, this->_M_extract()(__n
->_M_v()));
1808 _M_swap(_Hashtable_base
& __x
)
1810 __hash_code_base::_M_swap(__x
);
1811 std::swap(_EqualEBO::_M_get(), __x
._EqualEBO::_M_get());
1815 _M_eq() const { return _EqualEBO::_M_cget(); }
1819 * struct _Equality_base.
1821 * Common types and functions for class _Equality.
1823 struct _Equality_base
1826 template<typename _Uiterator
>
1828 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
1831 // See std::is_permutation in N3068.
1832 template<typename _Uiterator
>
1835 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
1836 _Uiterator __first2
)
1838 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1839 if (!(*__first1
== *__first2
))
1842 if (__first1
== __last1
)
1845 _Uiterator __last2
= __first2
;
1846 std::advance(__last2
, std::distance(__first1
, __last1
));
1848 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
1850 _Uiterator __tmp
= __first1
;
1851 while (__tmp
!= __it1
&& !bool(*__tmp
== *__it1
))
1854 // We've seen this one before.
1858 std::ptrdiff_t __n2
= 0;
1859 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
1860 if (*__tmp
== *__it1
)
1866 std::ptrdiff_t __n1
= 0;
1867 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
1868 if (*__tmp
== *__it1
)
1878 * Primary class template _Equality.
1880 * This is for implementing equality comparison for unordered
1881 * containers, per N3068, by John Lakos and Pablo Halpern.
1882 * Algorithmically, we follow closely the reference implementations
1885 template<typename _Key
, typename _Value
, typename _Alloc
,
1886 typename _ExtractKey
, typename _Equal
,
1887 typename _H1
, typename _H2
, typename _Hash
,
1888 typename _RehashPolicy
, typename _Traits
,
1889 bool _Unique_keys
= _Traits::__unique_keys::value
>
1893 template<typename _Key
, typename _Value
, typename _Alloc
,
1894 typename _ExtractKey
, typename _Equal
,
1895 typename _H1
, typename _H2
, typename _Hash
,
1896 typename _RehashPolicy
, typename _Traits
>
1897 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1898 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
1900 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1901 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1904 _M_equal(const __hashtable
&) const;
1907 template<typename _Key
, typename _Value
, typename _Alloc
,
1908 typename _ExtractKey
, typename _Equal
,
1909 typename _H1
, typename _H2
, typename _Hash
,
1910 typename _RehashPolicy
, typename _Traits
>
1912 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1913 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
1914 _M_equal(const __hashtable
& __other
) const
1916 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1918 if (__this
->size() != __other
.size())
1921 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
1923 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
1924 if (__ity
== __other
.end() || !bool(*__ity
== *__itx
))
1931 template<typename _Key
, typename _Value
, typename _Alloc
,
1932 typename _ExtractKey
, typename _Equal
,
1933 typename _H1
, typename _H2
, typename _Hash
,
1934 typename _RehashPolicy
, typename _Traits
>
1935 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1936 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
1937 : public _Equality_base
1939 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1940 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1943 _M_equal(const __hashtable
&) const;
1946 template<typename _Key
, typename _Value
, typename _Alloc
,
1947 typename _ExtractKey
, typename _Equal
,
1948 typename _H1
, typename _H2
, typename _Hash
,
1949 typename _RehashPolicy
, typename _Traits
>
1951 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1952 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>::
1953 _M_equal(const __hashtable
& __other
) const
1955 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1957 if (__this
->size() != __other
.size())
1960 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
1962 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
1963 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
1965 if (std::distance(__xrange
.first
, __xrange
.second
)
1966 != std::distance(__yrange
.first
, __yrange
.second
))
1969 if (!_S_is_permutation(__xrange
.first
, __xrange
.second
,
1973 __itx
= __xrange
.second
;
1979 * This type deals with all allocation and keeps an allocator instance
1980 * through inheritance to benefit from EBO when possible.
1982 template<typename _NodeAlloc
>
1983 struct _Hashtable_alloc
: private _Hashtable_ebo_helper
<0, _NodeAlloc
>
1986 using __ebo_node_alloc
= _Hashtable_ebo_helper
<0, _NodeAlloc
>;
1988 using __node_type
= typename
_NodeAlloc::value_type
;
1989 using __node_alloc_type
= _NodeAlloc
;
1990 // Use __gnu_cxx to benefit from _S_always_equal and al.
1991 using __node_alloc_traits
= __gnu_cxx::__alloc_traits
<__node_alloc_type
>;
1993 using __value_alloc_traits
= typename
__node_alloc_traits::template
1994 rebind_traits
<typename
__node_type::value_type
>;
1996 using __node_base
= __detail::_Hash_node_base
;
1997 using __bucket_type
= __node_base
*;
1998 using __bucket_alloc_type
=
1999 __alloc_rebind
<__node_alloc_type
, __bucket_type
>;
2000 using __bucket_alloc_traits
= std::allocator_traits
<__bucket_alloc_type
>;
2002 _Hashtable_alloc() = default;
2003 _Hashtable_alloc(const _Hashtable_alloc
&) = default;
2004 _Hashtable_alloc(_Hashtable_alloc
&&) = default;
2006 template<typename _Alloc
>
2007 _Hashtable_alloc(_Alloc
&& __a
)
2008 : __ebo_node_alloc(std::forward
<_Alloc
>(__a
))
2013 { return __ebo_node_alloc::_M_get(); }
2015 const __node_alloc_type
&
2016 _M_node_allocator() const
2017 { return __ebo_node_alloc::_M_cget(); }
2019 // Allocate a node and construct an element within it.
2020 template<typename
... _Args
>
2022 _M_allocate_node(_Args
&&... __args
);
2024 // Destroy the element within a node and deallocate the node.
2026 _M_deallocate_node(__node_type
* __n
);
2028 // Deallocate a node.
2030 _M_deallocate_node_ptr(__node_type
* __n
);
2032 // Deallocate the linked list of nodes pointed to by __n.
2033 // The elements within the nodes are destroyed.
2035 _M_deallocate_nodes(__node_type
* __n
);
2038 _M_allocate_buckets(std::size_t __bkt_count
);
2041 _M_deallocate_buckets(__bucket_type
*, std::size_t __bkt_count
);
2044 // Definitions of class template _Hashtable_alloc's out-of-line member
2046 template<typename _NodeAlloc
>
2047 template<typename
... _Args
>
2049 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_node(_Args
&&... __args
)
2052 auto __nptr
= __node_alloc_traits::allocate(_M_node_allocator(), 1);
2053 __node_type
* __n
= std::__to_address(__nptr
);
2056 ::new ((void*)__n
) __node_type
;
2057 __node_alloc_traits::construct(_M_node_allocator(),
2059 std::forward
<_Args
>(__args
)...);
2064 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr
, 1);
2065 __throw_exception_again
;
2069 template<typename _NodeAlloc
>
2071 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_node(__node_type
* __n
)
2073 __node_alloc_traits::destroy(_M_node_allocator(), __n
->_M_valptr());
2074 _M_deallocate_node_ptr(__n
);
2077 template<typename _NodeAlloc
>
2079 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_node_ptr(__node_type
* __n
)
2081 typedef typename
__node_alloc_traits::pointer _Ptr
;
2082 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__n
);
2083 __n
->~__node_type();
2084 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr
, 1);
2087 template<typename _NodeAlloc
>
2089 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_nodes(__node_type
* __n
)
2093 __node_type
* __tmp
= __n
;
2094 __n
= __n
->_M_next();
2095 _M_deallocate_node(__tmp
);
2099 template<typename _NodeAlloc
>
2100 typename _Hashtable_alloc
<_NodeAlloc
>::__bucket_type
*
2101 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_buckets(std::size_t __bkt_count
)
2103 __bucket_alloc_type
__alloc(_M_node_allocator());
2105 auto __ptr
= __bucket_alloc_traits::allocate(__alloc
, __bkt_count
);
2106 __bucket_type
* __p
= std::__to_address(__ptr
);
2107 __builtin_memset(__p
, 0, __bkt_count
* sizeof(__bucket_type
));
2111 template<typename _NodeAlloc
>
2113 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_buckets(__bucket_type
* __bkts
,
2114 std::size_t __bkt_count
)
2116 typedef typename
__bucket_alloc_traits::pointer _Ptr
;
2117 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__bkts
);
2118 __bucket_alloc_type
__alloc(_M_node_allocator());
2119 __bucket_alloc_traits::deallocate(__alloc
, __ptr
, __bkt_count
);
2122 //@} hashtable-detail
2123 } // namespace __detail
2124 _GLIBCXX_END_NAMESPACE_VERSION
2127 #endif // _HASHTABLE_POLICY_H