1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010-2017 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 <bits/stl_algobase.h> // for std::min.
36 namespace std
_GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 template<typename _Key
, typename _Value
, typename _Alloc
,
41 typename _ExtractKey
, typename _Equal
,
42 typename _H1
, typename _H2
, typename _Hash
,
43 typename _RehashPolicy
, typename _Traits
>
46 _GLIBCXX_END_NAMESPACE_VERSION
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @defgroup hashtable-detail Base and Implementation Classes
54 * @ingroup unordered_associative_containers
57 template<typename _Key
, typename _Value
,
58 typename _ExtractKey
, typename _Equal
,
59 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
60 struct _Hashtable_base
;
62 // Helper function: return distance(first, last) for forward
63 // iterators, or 0 for input iterators.
64 template<class _Iterator
>
65 inline typename
std::iterator_traits
<_Iterator
>::difference_type
66 __distance_fw(_Iterator __first
, _Iterator __last
,
67 std::input_iterator_tag
)
70 template<class _Iterator
>
71 inline typename
std::iterator_traits
<_Iterator
>::difference_type
72 __distance_fw(_Iterator __first
, _Iterator __last
,
73 std::forward_iterator_tag
)
74 { return std::distance(__first
, __last
); }
76 template<class _Iterator
>
77 inline typename
std::iterator_traits
<_Iterator
>::difference_type
78 __distance_fw(_Iterator __first
, _Iterator __last
)
80 typedef typename
std::iterator_traits
<_Iterator
>::iterator_category _Tag
;
81 return __distance_fw(__first
, __last
, _Tag());
84 // Helper type used to detect whether the hash functor is noexcept.
85 template <typename _Key
, typename _Hash
>
86 struct __is_noexcept_hash
: std::__bool_constant
<
87 noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>()))>
92 template<typename _Tp
>
94 operator()(_Tp
&& __x
) const
95 { return std::forward
<_Tp
>(__x
); }
100 template<typename _Tp
>
102 operator()(_Tp
&& __x
) const
103 -> decltype(std::get
<0>(std::forward
<_Tp
>(__x
)))
104 { return std::get
<0>(std::forward
<_Tp
>(__x
)); }
107 template<typename _NodeAlloc
>
108 struct _Hashtable_alloc
;
110 // Functor recycling a pool of nodes and using allocation once the pool is
112 template<typename _NodeAlloc
>
113 struct _ReuseOrAllocNode
116 using __node_alloc_type
= _NodeAlloc
;
117 using __hashtable_alloc
= _Hashtable_alloc
<__node_alloc_type
>;
118 using __value_alloc_type
= typename
__hashtable_alloc::__value_alloc_type
;
119 using __value_alloc_traits
=
120 typename
__hashtable_alloc::__value_alloc_traits
;
121 using __node_alloc_traits
=
122 typename
__hashtable_alloc::__node_alloc_traits
;
123 using __node_type
= typename
__hashtable_alloc::__node_type
;
126 _ReuseOrAllocNode(__node_type
* __nodes
, __hashtable_alloc
& __h
)
127 : _M_nodes(__nodes
), _M_h(__h
) { }
128 _ReuseOrAllocNode(const _ReuseOrAllocNode
&) = delete;
131 { _M_h
._M_deallocate_nodes(_M_nodes
); }
133 template<typename _Arg
>
135 operator()(_Arg
&& __arg
) const
139 __node_type
* __node
= _M_nodes
;
140 _M_nodes
= _M_nodes
->_M_next();
141 __node
->_M_nxt
= nullptr;
142 __value_alloc_type
__a(_M_h
._M_node_allocator());
143 __value_alloc_traits::destroy(__a
, __node
->_M_valptr());
146 __value_alloc_traits::construct(__a
, __node
->_M_valptr(),
147 std::forward
<_Arg
>(__arg
));
151 __node
->~__node_type();
152 __node_alloc_traits::deallocate(_M_h
._M_node_allocator(),
154 __throw_exception_again
;
158 return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
));
162 mutable __node_type
* _M_nodes
;
163 __hashtable_alloc
& _M_h
;
166 // Functor similar to the previous one but without any pool of nodes to
168 template<typename _NodeAlloc
>
172 using __hashtable_alloc
= _Hashtable_alloc
<_NodeAlloc
>;
173 using __node_type
= typename
__hashtable_alloc::__node_type
;
176 _AllocNode(__hashtable_alloc
& __h
)
179 template<typename _Arg
>
181 operator()(_Arg
&& __arg
) const
182 { return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
)); }
185 __hashtable_alloc
& _M_h
;
188 // Auxiliary types used for all instantiations of _Hashtable nodes
192 * struct _Hashtable_traits
194 * Important traits for hash tables.
196 * @tparam _Cache_hash_code Boolean value. True if the value of
197 * the hash function is stored along with the value. This is a
198 * time-space tradeoff. Storing it may improve lookup speed by
199 * reducing the number of times we need to call the _Equal
202 * @tparam _Constant_iterators Boolean value. True if iterator and
203 * const_iterator are both constant iterator types. This is true
204 * for unordered_set and unordered_multiset, false for
205 * unordered_map and unordered_multimap.
207 * @tparam _Unique_keys Boolean value. True if the return value
208 * of _Hashtable::count(k) is always at most one, false if it may
209 * be an arbitrary number. This is true for unordered_set and
210 * unordered_map, false for unordered_multiset and
211 * unordered_multimap.
213 template<bool _Cache_hash_code
, bool _Constant_iterators
, bool _Unique_keys
>
214 struct _Hashtable_traits
216 using __hash_cached
= __bool_constant
<_Cache_hash_code
>;
217 using __constant_iterators
= __bool_constant
<_Constant_iterators
>;
218 using __unique_keys
= __bool_constant
<_Unique_keys
>;
222 * struct _Hash_node_base
224 * Nodes, used to wrap elements stored in the hash table. A policy
225 * template parameter of class template _Hashtable controls whether
226 * nodes also store a hash code. In some cases (e.g. strings) this
227 * may be a performance win.
229 struct _Hash_node_base
231 _Hash_node_base
* _M_nxt
;
233 _Hash_node_base() noexcept
: _M_nxt() { }
235 _Hash_node_base(_Hash_node_base
* __next
) noexcept
: _M_nxt(__next
) { }
239 * struct _Hash_node_value_base
241 * Node type with the value to store.
243 template<typename _Value
>
244 struct _Hash_node_value_base
: _Hash_node_base
246 typedef _Value value_type
;
248 __gnu_cxx::__aligned_buffer
<_Value
> _M_storage
;
252 { return _M_storage
._M_ptr(); }
255 _M_valptr() const noexcept
256 { return _M_storage
._M_ptr(); }
260 { return *_M_valptr(); }
263 _M_v() const noexcept
264 { return *_M_valptr(); }
268 * Primary template struct _Hash_node.
270 template<typename _Value
, bool _Cache_hash_code
>
274 * Specialization for nodes with caches, struct _Hash_node.
276 * Base class is __detail::_Hash_node_value_base.
278 template<typename _Value
>
279 struct _Hash_node
<_Value
, true> : _Hash_node_value_base
<_Value
>
281 std::size_t _M_hash_code
;
284 _M_next() const noexcept
285 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
289 * Specialization for nodes without caches, struct _Hash_node.
291 * Base class is __detail::_Hash_node_value_base.
293 template<typename _Value
>
294 struct _Hash_node
<_Value
, false> : _Hash_node_value_base
<_Value
>
297 _M_next() const noexcept
298 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
301 /// Base class for node iterators.
302 template<typename _Value
, bool _Cache_hash_code
>
303 struct _Node_iterator_base
305 using __node_type
= _Hash_node
<_Value
, _Cache_hash_code
>;
309 _Node_iterator_base(__node_type
* __p
) noexcept
314 { _M_cur
= _M_cur
->_M_next(); }
317 template<typename _Value
, bool _Cache_hash_code
>
319 operator==(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
320 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
322 { return __x
._M_cur
== __y
._M_cur
; }
324 template<typename _Value
, bool _Cache_hash_code
>
326 operator!=(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
327 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
329 { return __x
._M_cur
!= __y
._M_cur
; }
331 /// Node iterators, used to iterate through all the hashtable.
332 template<typename _Value
, bool __constant_iterators
, bool __cache
>
333 struct _Node_iterator
334 : public _Node_iterator_base
<_Value
, __cache
>
337 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
338 using __node_type
= typename
__base_type::__node_type
;
341 typedef _Value value_type
;
342 typedef std::ptrdiff_t difference_type
;
343 typedef std::forward_iterator_tag iterator_category
;
345 using pointer
= typename
std::conditional
<__constant_iterators
,
346 const _Value
*, _Value
*>::type
;
348 using reference
= typename
std::conditional
<__constant_iterators
,
349 const _Value
&, _Value
&>::type
;
351 _Node_iterator() noexcept
355 _Node_iterator(__node_type
* __p
) noexcept
356 : __base_type(__p
) { }
359 operator*() const noexcept
360 { return this->_M_cur
->_M_v(); }
363 operator->() const noexcept
364 { return this->_M_cur
->_M_valptr(); }
367 operator++() noexcept
374 operator++(int) noexcept
376 _Node_iterator
__tmp(*this);
382 /// Node const_iterators, used to iterate through all the hashtable.
383 template<typename _Value
, bool __constant_iterators
, bool __cache
>
384 struct _Node_const_iterator
385 : public _Node_iterator_base
<_Value
, __cache
>
388 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
389 using __node_type
= typename
__base_type::__node_type
;
392 typedef _Value value_type
;
393 typedef std::ptrdiff_t difference_type
;
394 typedef std::forward_iterator_tag iterator_category
;
396 typedef const _Value
* pointer
;
397 typedef const _Value
& reference
;
399 _Node_const_iterator() noexcept
403 _Node_const_iterator(__node_type
* __p
) noexcept
404 : __base_type(__p
) { }
406 _Node_const_iterator(const _Node_iterator
<_Value
, __constant_iterators
,
407 __cache
>& __x
) noexcept
408 : __base_type(__x
._M_cur
) { }
411 operator*() const noexcept
412 { return this->_M_cur
->_M_v(); }
415 operator->() const noexcept
416 { return this->_M_cur
->_M_valptr(); }
418 _Node_const_iterator
&
419 operator++() noexcept
426 operator++(int) noexcept
428 _Node_const_iterator
__tmp(*this);
434 // Many of class template _Hashtable's template parameters are policy
435 // classes. These are defaults for the policies.
437 /// Default range hashing function: use division to fold a large number
438 /// into the range [0, N).
439 struct _Mod_range_hashing
441 typedef std::size_t first_argument_type
;
442 typedef std::size_t second_argument_type
;
443 typedef std::size_t result_type
;
446 operator()(first_argument_type __num
,
447 second_argument_type __den
) const noexcept
448 { return __num
% __den
; }
451 /// Default ranged hash function H. In principle it should be a
452 /// function object composed from objects of type H1 and H2 such that
453 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
454 /// h1 and h2. So instead we'll just use a tag to tell class template
455 /// hashtable to do that composition.
456 struct _Default_ranged_hash
{ };
458 /// Default value for rehash policy. Bucket size is (usually) the
459 /// smallest prime that keeps the load factor small enough.
460 struct _Prime_rehash_policy
462 using __has_load_factor
= std::true_type
;
464 _Prime_rehash_policy(float __z
= 1.0) noexcept
465 : _M_max_load_factor(__z
), _M_next_resize(0) { }
468 max_load_factor() const noexcept
469 { return _M_max_load_factor
; }
471 // Return a bucket size no smaller than n.
473 _M_next_bkt(std::size_t __n
) const;
475 // Return a bucket count appropriate for n elements
477 _M_bkt_for_elements(std::size_t __n
) const
478 { return __builtin_ceil(__n
/ (long double)_M_max_load_factor
); }
480 // __n_bkt is current bucket count, __n_elt is current element count,
481 // and __n_ins is number of elements to be inserted. Do we need to
482 // increase bucket count? If so, return make_pair(true, n), where n
483 // is the new bucket count. If not, return make_pair(false, 0).
484 std::pair
<bool, std::size_t>
485 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
486 std::size_t __n_ins
) const;
488 typedef std::size_t _State
;
492 { return _M_next_resize
; }
496 { _M_next_resize
= 0; }
499 _M_reset(_State __state
)
500 { _M_next_resize
= __state
; }
502 static const std::size_t _S_growth_factor
= 2;
504 float _M_max_load_factor
;
505 mutable std::size_t _M_next_resize
;
508 /// Range hashing function assuming that second arg is a power of 2.
509 struct _Mask_range_hashing
511 typedef std::size_t first_argument_type
;
512 typedef std::size_t second_argument_type
;
513 typedef std::size_t result_type
;
516 operator()(first_argument_type __num
,
517 second_argument_type __den
) const noexcept
518 { return __num
& (__den
- 1); }
521 /// Compute closest power of 2.
524 __clp2(std::size_t __n
) noexcept
526 #if __SIZEOF_SIZE_T__ >= 8
527 std::uint_fast64_t __x
= __n
;
529 std::uint_fast32_t __x
= __n
;
531 // Algorithm from Hacker's Delight, Figure 3-3.
533 __x
= __x
| (__x
>> 1);
534 __x
= __x
| (__x
>> 2);
535 __x
= __x
| (__x
>> 4);
536 __x
= __x
| (__x
>> 8);
537 __x
= __x
| (__x
>>16);
538 #if __SIZEOF_SIZE_T__ >= 8
539 __x
= __x
| (__x
>>32);
544 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
546 struct _Power2_rehash_policy
548 using __has_load_factor
= std::true_type
;
550 _Power2_rehash_policy(float __z
= 1.0) noexcept
551 : _M_max_load_factor(__z
), _M_next_resize(0) { }
554 max_load_factor() const noexcept
555 { return _M_max_load_factor
; }
557 // Return a bucket size no smaller than n (as long as n is not above the
558 // highest power of 2).
560 _M_next_bkt(std::size_t __n
) noexcept
562 const auto __max_width
= std::min
<size_t>(sizeof(size_t), 8);
563 const auto __max_bkt
= size_t(1) << (__max_width
* __CHAR_BIT__
- 1);
564 std::size_t __res
= __clp2(__n
);
572 if (__res
== __max_bkt
)
573 // Set next resize to the max value so that we never try to rehash again
574 // as we already reach the biggest possible bucket number.
575 // Note that it might result in max_load_factor not being respected.
576 _M_next_resize
= std::size_t(-1);
579 = __builtin_ceil(__res
* (long double)_M_max_load_factor
);
584 // Return a bucket count appropriate for n elements
586 _M_bkt_for_elements(std::size_t __n
) const noexcept
587 { return __builtin_ceil(__n
/ (long double)_M_max_load_factor
); }
589 // __n_bkt is current bucket count, __n_elt is current element count,
590 // and __n_ins is number of elements to be inserted. Do we need to
591 // increase bucket count? If so, return make_pair(true, n), where n
592 // is the new bucket count. If not, return make_pair(false, 0).
593 std::pair
<bool, std::size_t>
594 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
595 std::size_t __n_ins
) noexcept
597 if (__n_elt
+ __n_ins
>= _M_next_resize
)
599 long double __min_bkts
= (__n_elt
+ __n_ins
)
600 / (long double)_M_max_load_factor
;
601 if (__min_bkts
>= __n_bkt
)
602 return std::make_pair(true,
603 _M_next_bkt(std::max
<std::size_t>(__builtin_floor(__min_bkts
) + 1,
604 __n_bkt
* _S_growth_factor
)));
607 = __builtin_floor(__n_bkt
* (long double)_M_max_load_factor
);
608 return std::make_pair(false, 0);
611 return std::make_pair(false, 0);
614 typedef std::size_t _State
;
617 _M_state() const noexcept
618 { return _M_next_resize
; }
622 { _M_next_resize
= 0; }
625 _M_reset(_State __state
) noexcept
626 { _M_next_resize
= __state
; }
628 static const std::size_t _S_growth_factor
= 2;
630 float _M_max_load_factor
;
631 std::size_t _M_next_resize
;
634 // Base classes for std::_Hashtable. We define these base classes
635 // because in some cases we want to do different things depending on
636 // the value of a policy class. In some cases the policy class
637 // affects which member functions and nested typedefs are defined;
638 // we handle that by specializing base class templates. Several of
639 // the base class templates need to access other members of class
640 // template _Hashtable, so we use a variant of the "Curiously
641 // Recurring Template Pattern" (CRTP) technique.
644 * Primary class template _Map_base.
646 * If the hashtable has a value type of the form pair<T1, T2> and a
647 * key extraction policy (_ExtractKey) that returns the first part
648 * of the pair, the hashtable gets a mapped_type typedef. If it
649 * satisfies those criteria and also has unique keys, then it also
650 * gets an operator[].
652 template<typename _Key
, typename _Value
, typename _Alloc
,
653 typename _ExtractKey
, typename _Equal
,
654 typename _H1
, typename _H2
, typename _Hash
,
655 typename _RehashPolicy
, typename _Traits
,
656 bool _Unique_keys
= _Traits::__unique_keys::value
>
657 struct _Map_base
{ };
659 /// Partial specialization, __unique_keys set to false.
660 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
661 typename _H1
, typename _H2
, typename _Hash
,
662 typename _RehashPolicy
, typename _Traits
>
663 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
664 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
666 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
669 /// Partial specialization, __unique_keys set to true.
670 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
671 typename _H1
, typename _H2
, typename _Hash
,
672 typename _RehashPolicy
, typename _Traits
>
673 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
674 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
677 using __hashtable_base
= __detail::_Hashtable_base
<_Key
, _Pair
,
679 _Equal
, _H1
, _H2
, _Hash
,
682 using __hashtable
= _Hashtable
<_Key
, _Pair
, _Alloc
,
684 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
686 using __hash_code
= typename
__hashtable_base::__hash_code
;
687 using __node_type
= typename
__hashtable_base::__node_type
;
690 using key_type
= typename
__hashtable_base::key_type
;
691 using iterator
= typename
__hashtable_base::iterator
;
692 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
695 operator[](const key_type
& __k
);
698 operator[](key_type
&& __k
);
700 // _GLIBCXX_RESOLVE_LIB_DEFECTS
701 // DR 761. unordered_map needs an at() member function.
703 at(const key_type
& __k
);
706 at(const key_type
& __k
) const;
709 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
710 typename _H1
, typename _H2
, typename _Hash
,
711 typename _RehashPolicy
, typename _Traits
>
713 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
714 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
715 operator[](const key_type
& __k
)
718 __hashtable
* __h
= static_cast<__hashtable
*>(this);
719 __hash_code __code
= __h
->_M_hash_code(__k
);
720 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
721 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
725 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
726 std::tuple
<const key_type
&>(__k
),
728 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
731 return __p
->_M_v().second
;
734 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
735 typename _H1
, typename _H2
, typename _Hash
,
736 typename _RehashPolicy
, typename _Traits
>
738 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
739 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
740 operator[](key_type
&& __k
)
743 __hashtable
* __h
= static_cast<__hashtable
*>(this);
744 __hash_code __code
= __h
->_M_hash_code(__k
);
745 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
746 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
750 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
751 std::forward_as_tuple(std::move(__k
)),
753 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
756 return __p
->_M_v().second
;
759 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
760 typename _H1
, typename _H2
, typename _Hash
,
761 typename _RehashPolicy
, typename _Traits
>
763 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
764 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
765 at(const key_type
& __k
)
768 __hashtable
* __h
= static_cast<__hashtable
*>(this);
769 __hash_code __code
= __h
->_M_hash_code(__k
);
770 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
771 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
774 __throw_out_of_range(__N("_Map_base::at"));
775 return __p
->_M_v().second
;
778 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
779 typename _H1
, typename _H2
, typename _Hash
,
780 typename _RehashPolicy
, typename _Traits
>
782 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
783 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
784 at(const key_type
& __k
) const
785 -> const mapped_type
&
787 const __hashtable
* __h
= static_cast<const __hashtable
*>(this);
788 __hash_code __code
= __h
->_M_hash_code(__k
);
789 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
790 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
793 __throw_out_of_range(__N("_Map_base::at"));
794 return __p
->_M_v().second
;
798 * Primary class template _Insert_base.
800 * Defines @c insert member functions appropriate to all _Hashtables.
802 template<typename _Key
, typename _Value
, typename _Alloc
,
803 typename _ExtractKey
, typename _Equal
,
804 typename _H1
, typename _H2
, typename _Hash
,
805 typename _RehashPolicy
, typename _Traits
>
809 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
810 _Equal
, _H1
, _H2
, _Hash
,
811 _RehashPolicy
, _Traits
>;
813 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
814 _Equal
, _H1
, _H2
, _Hash
,
817 using value_type
= typename
__hashtable_base::value_type
;
818 using iterator
= typename
__hashtable_base::iterator
;
819 using const_iterator
= typename
__hashtable_base::const_iterator
;
820 using size_type
= typename
__hashtable_base::size_type
;
822 using __unique_keys
= typename
__hashtable_base::__unique_keys
;
823 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
824 using __node_type
= _Hash_node
<_Value
, _Traits::__hash_cached::value
>;
825 using __node_alloc_type
= __alloc_rebind
<_Alloc
, __node_type
>;
826 using __node_gen_type
= _AllocNode
<__node_alloc_type
>;
829 _M_conjure_hashtable()
830 { return *(static_cast<__hashtable
*>(this)); }
832 template<typename _InputIterator
, typename _NodeGetter
>
834 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
839 insert(const value_type
& __v
)
841 __hashtable
& __h
= _M_conjure_hashtable();
842 __node_gen_type
__node_gen(__h
);
843 return __h
._M_insert(__v
, __node_gen
, __unique_keys());
847 insert(const_iterator __hint
, const value_type
& __v
)
849 __hashtable
& __h
= _M_conjure_hashtable();
850 __node_gen_type
__node_gen(__h
);
851 return __h
._M_insert(__hint
, __v
, __node_gen
, __unique_keys());
855 insert(initializer_list
<value_type
> __l
)
856 { this->insert(__l
.begin(), __l
.end()); }
858 template<typename _InputIterator
>
860 insert(_InputIterator __first
, _InputIterator __last
)
862 __hashtable
& __h
= _M_conjure_hashtable();
863 __node_gen_type
__node_gen(__h
);
864 return _M_insert_range(__first
, __last
, __node_gen
);
868 template<typename _Key
, typename _Value
, typename _Alloc
,
869 typename _ExtractKey
, typename _Equal
,
870 typename _H1
, typename _H2
, typename _Hash
,
871 typename _RehashPolicy
, typename _Traits
>
872 template<typename _InputIterator
, typename _NodeGetter
>
874 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
875 _RehashPolicy
, _Traits
>::
876 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
877 const _NodeGetter
& __node_gen
)
879 using __rehash_type
= typename
__hashtable::__rehash_type
;
880 using __rehash_state
= typename
__hashtable::__rehash_state
;
881 using pair_type
= std::pair
<bool, std::size_t>;
883 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
885 __hashtable
& __h
= _M_conjure_hashtable();
886 __rehash_type
& __rehash
= __h
._M_rehash_policy
;
887 const __rehash_state
& __saved_state
= __rehash
._M_state();
888 pair_type __do_rehash
= __rehash
._M_need_rehash(__h
._M_bucket_count
,
889 __h
._M_element_count
,
892 if (__do_rehash
.first
)
893 __h
._M_rehash(__do_rehash
.second
, __saved_state
);
895 for (; __first
!= __last
; ++__first
)
896 __h
._M_insert(*__first
, __node_gen
, __unique_keys());
900 * Primary class template _Insert.
902 * Defines @c insert member functions that depend on _Hashtable policies,
903 * via partial specializations.
905 template<typename _Key
, typename _Value
, typename _Alloc
,
906 typename _ExtractKey
, typename _Equal
,
907 typename _H1
, typename _H2
, typename _Hash
,
908 typename _RehashPolicy
, typename _Traits
,
909 bool _Constant_iterators
= _Traits::__constant_iterators::value
>
913 template<typename _Key
, typename _Value
, typename _Alloc
,
914 typename _ExtractKey
, typename _Equal
,
915 typename _H1
, typename _H2
, typename _Hash
,
916 typename _RehashPolicy
, typename _Traits
>
917 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
918 _RehashPolicy
, _Traits
, true>
919 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
920 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
922 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
923 _Equal
, _H1
, _H2
, _Hash
,
924 _RehashPolicy
, _Traits
>;
926 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
927 _Equal
, _H1
, _H2
, _Hash
,
930 using value_type
= typename
__base_type::value_type
;
931 using iterator
= typename
__base_type::iterator
;
932 using const_iterator
= typename
__base_type::const_iterator
;
934 using __unique_keys
= typename
__base_type::__unique_keys
;
935 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
936 using __hashtable
= typename
__base_type::__hashtable
;
937 using __node_gen_type
= typename
__base_type::__node_gen_type
;
939 using __base_type::insert
;
942 insert(value_type
&& __v
)
944 __hashtable
& __h
= this->_M_conjure_hashtable();
945 __node_gen_type
__node_gen(__h
);
946 return __h
._M_insert(std::move(__v
), __node_gen
, __unique_keys());
950 insert(const_iterator __hint
, value_type
&& __v
)
952 __hashtable
& __h
= this->_M_conjure_hashtable();
953 __node_gen_type
__node_gen(__h
);
954 return __h
._M_insert(__hint
, std::move(__v
), __node_gen
,
960 template<typename _Key
, typename _Value
, typename _Alloc
,
961 typename _ExtractKey
, typename _Equal
,
962 typename _H1
, typename _H2
, typename _Hash
,
963 typename _RehashPolicy
, typename _Traits
>
964 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
965 _RehashPolicy
, _Traits
, false>
966 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
967 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
969 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
970 _Equal
, _H1
, _H2
, _Hash
,
971 _RehashPolicy
, _Traits
>;
972 using value_type
= typename
__base_type::value_type
;
973 using iterator
= typename
__base_type::iterator
;
974 using const_iterator
= typename
__base_type::const_iterator
;
976 using __unique_keys
= typename
__base_type::__unique_keys
;
977 using __hashtable
= typename
__base_type::__hashtable
;
978 using __ireturn_type
= typename
__base_type::__ireturn_type
;
980 using __base_type::insert
;
982 template<typename _Pair
>
983 using __is_cons
= std::is_constructible
<value_type
, _Pair
&&>;
985 template<typename _Pair
>
986 using _IFcons
= std::enable_if
<__is_cons
<_Pair
>::value
>;
988 template<typename _Pair
>
989 using _IFconsp
= typename _IFcons
<_Pair
>::type
;
991 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
995 __hashtable
& __h
= this->_M_conjure_hashtable();
996 return __h
._M_emplace(__unique_keys(), std::forward
<_Pair
>(__v
));
999 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
1001 insert(const_iterator __hint
, _Pair
&& __v
)
1003 __hashtable
& __h
= this->_M_conjure_hashtable();
1004 return __h
._M_emplace(__hint
, __unique_keys(),
1005 std::forward
<_Pair
>(__v
));
1009 template<typename _Policy
>
1010 using __has_load_factor
= typename
_Policy::__has_load_factor
;
1013 * Primary class template _Rehash_base.
1015 * Give hashtable the max_load_factor functions and reserve iff the
1016 * rehash policy supports it.
1018 template<typename _Key
, typename _Value
, typename _Alloc
,
1019 typename _ExtractKey
, typename _Equal
,
1020 typename _H1
, typename _H2
, typename _Hash
,
1021 typename _RehashPolicy
, typename _Traits
,
1023 __detected_or_t
<std::false_type
, __has_load_factor
, _RehashPolicy
>>
1024 struct _Rehash_base
;
1026 /// Specialization when rehash policy doesn't provide load factor management.
1027 template<typename _Key
, typename _Value
, typename _Alloc
,
1028 typename _ExtractKey
, typename _Equal
,
1029 typename _H1
, typename _H2
, typename _Hash
,
1030 typename _RehashPolicy
, typename _Traits
>
1031 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1032 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1037 /// Specialization when rehash policy provide load factor management.
1038 template<typename _Key
, typename _Value
, typename _Alloc
,
1039 typename _ExtractKey
, typename _Equal
,
1040 typename _H1
, typename _H2
, typename _Hash
,
1041 typename _RehashPolicy
, typename _Traits
>
1042 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1043 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1046 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1047 _Equal
, _H1
, _H2
, _Hash
,
1048 _RehashPolicy
, _Traits
>;
1051 max_load_factor() const noexcept
1053 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1054 return __this
->__rehash_policy().max_load_factor();
1058 max_load_factor(float __z
)
1060 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1061 __this
->__rehash_policy(_RehashPolicy(__z
));
1065 reserve(std::size_t __n
)
1067 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1068 __this
->rehash(__builtin_ceil(__n
/ max_load_factor()));
1073 * Primary class template _Hashtable_ebo_helper.
1075 * Helper class using EBO when it is not forbidden (the type is not
1076 * final) and when it is worth it (the type is empty.)
1078 template<int _Nm
, typename _Tp
,
1079 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
1080 struct _Hashtable_ebo_helper
;
1082 /// Specialization using EBO.
1083 template<int _Nm
, typename _Tp
>
1084 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, true>
1087 _Hashtable_ebo_helper() = default;
1089 template<typename _OtherTp
>
1090 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1091 : _Tp(std::forward
<_OtherTp
>(__tp
))
1095 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
1096 { return static_cast<const _Tp
&>(__eboh
); }
1099 _S_get(_Hashtable_ebo_helper
& __eboh
)
1100 { return static_cast<_Tp
&>(__eboh
); }
1103 /// Specialization not using EBO.
1104 template<int _Nm
, typename _Tp
>
1105 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, false>
1107 _Hashtable_ebo_helper() = default;
1109 template<typename _OtherTp
>
1110 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1111 : _M_tp(std::forward
<_OtherTp
>(__tp
))
1115 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
1116 { return __eboh
._M_tp
; }
1119 _S_get(_Hashtable_ebo_helper
& __eboh
)
1120 { return __eboh
._M_tp
; }
1127 * Primary class template _Local_iterator_base.
1129 * Base class for local iterators, used to iterate within a bucket
1130 * but not between buckets.
1132 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1133 typename _H1
, typename _H2
, typename _Hash
,
1134 bool __cache_hash_code
>
1135 struct _Local_iterator_base
;
1138 * Primary class template _Hash_code_base.
1140 * Encapsulates two policy issues that aren't quite orthogonal.
1141 * (1) the difference between using a ranged hash function and using
1142 * the combination of a hash function and a range-hashing function.
1143 * In the former case we don't have such things as hash codes, so
1144 * we have a dummy type as placeholder.
1145 * (2) Whether or not we cache hash codes. Caching hash codes is
1146 * meaningless if we have a ranged hash function.
1148 * We also put the key extraction objects here, for convenience.
1149 * Each specialization derives from one or more of the template
1150 * parameters to benefit from Ebo. This is important as this type
1151 * is inherited in some cases by the _Local_iterator_base type used
1152 * to implement local_iterator and const_local_iterator. As with
1153 * any iterator type we prefer to make it as small as possible.
1155 * Primary template is unused except as a hook for specializations.
1157 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1158 typename _H1
, typename _H2
, typename _Hash
,
1159 bool __cache_hash_code
>
1160 struct _Hash_code_base
;
1162 /// Specialization: ranged hash function, no caching hash codes. H1
1163 /// and H2 are provided but ignored. We define a dummy hash code type.
1164 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1165 typename _H1
, typename _H2
, typename _Hash
>
1166 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, false>
1167 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1168 private _Hashtable_ebo_helper
<1, _Hash
>
1171 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1172 using __ebo_hash
= _Hashtable_ebo_helper
<1, _Hash
>;
1175 typedef void* __hash_code
;
1176 typedef _Hash_node
<_Value
, false> __node_type
;
1178 // We need the default constructor for the local iterators and _Hashtable
1179 // default constructor.
1180 _Hash_code_base() = default;
1182 _Hash_code_base(const _ExtractKey
& __ex
, const _H1
&, const _H2
&,
1184 : __ebo_extract_key(__ex
), __ebo_hash(__h
) { }
1187 _M_hash_code(const _Key
& __key
) const
1191 _M_bucket_index(const _Key
& __k
, __hash_code
, std::size_t __n
) const
1192 { return _M_ranged_hash()(__k
, __n
); }
1195 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1196 noexcept( noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>(),
1198 { return _M_ranged_hash()(_M_extract()(__p
->_M_v()), __n
); }
1201 _M_store_code(__node_type
*, __hash_code
) const
1205 _M_copy_code(__node_type
*, const __node_type
*) const
1209 _M_swap(_Hash_code_base
& __x
)
1211 std::swap(_M_extract(), __x
._M_extract());
1212 std::swap(_M_ranged_hash(), __x
._M_ranged_hash());
1216 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1219 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1222 _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
1225 _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
1228 // No specialization for ranged hash function while caching hash codes.
1229 // That combination is meaningless, and trying to do it is an error.
1231 /// Specialization: ranged hash function, cache hash codes. This
1232 /// combination is meaningless, so we provide only a declaration
1233 /// and no definition.
1234 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1235 typename _H1
, typename _H2
, typename _Hash
>
1236 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, true>;
1238 /// Specialization: hash function and range-hashing function, no
1239 /// caching of hash codes.
1240 /// Provides typedef and accessor required by C++ 11.
1241 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1242 typename _H1
, typename _H2
>
1243 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1244 _Default_ranged_hash
, false>
1245 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1246 private _Hashtable_ebo_helper
<1, _H1
>,
1247 private _Hashtable_ebo_helper
<2, _H2
>
1250 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1251 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1252 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1254 // Gives the local iterator implementation access to _M_bucket_index().
1255 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1256 _Default_ranged_hash
, false>;
1262 hash_function() const
1266 typedef std::size_t __hash_code
;
1267 typedef _Hash_node
<_Value
, false> __node_type
;
1269 // We need the default constructor for the local iterators and _Hashtable
1270 // default constructor.
1271 _Hash_code_base() = default;
1273 _Hash_code_base(const _ExtractKey
& __ex
,
1274 const _H1
& __h1
, const _H2
& __h2
,
1275 const _Default_ranged_hash
&)
1276 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1279 _M_hash_code(const _Key
& __k
) const
1280 { return _M_h1()(__k
); }
1283 _M_bucket_index(const _Key
&, __hash_code __c
, std::size_t __n
) const
1284 { return _M_h2()(__c
, __n
); }
1287 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1288 noexcept( noexcept(declval
<const _H1
&>()(declval
<const _Key
&>()))
1289 && noexcept(declval
<const _H2
&>()((__hash_code
)0,
1291 { return _M_h2()(_M_h1()(_M_extract()(__p
->_M_v())), __n
); }
1294 _M_store_code(__node_type
*, __hash_code
) const
1298 _M_copy_code(__node_type
*, const __node_type
*) const
1302 _M_swap(_Hash_code_base
& __x
)
1304 std::swap(_M_extract(), __x
._M_extract());
1305 std::swap(_M_h1(), __x
._M_h1());
1306 std::swap(_M_h2(), __x
._M_h2());
1310 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1313 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1316 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1319 _M_h1() { return __ebo_h1::_S_get(*this); }
1322 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1325 _M_h2() { return __ebo_h2::_S_get(*this); }
1328 /// Specialization: hash function and range-hashing function,
1329 /// caching hash codes. H is provided but ignored. Provides
1330 /// typedef and accessor required by C++ 11.
1331 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1332 typename _H1
, typename _H2
>
1333 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1334 _Default_ranged_hash
, true>
1335 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1336 private _Hashtable_ebo_helper
<1, _H1
>,
1337 private _Hashtable_ebo_helper
<2, _H2
>
1340 // Gives the local iterator implementation access to _M_h2().
1341 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1342 _Default_ranged_hash
, true>;
1344 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1345 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1346 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1352 hash_function() const
1356 typedef std::size_t __hash_code
;
1357 typedef _Hash_node
<_Value
, true> __node_type
;
1359 // We need the default constructor for _Hashtable default constructor.
1360 _Hash_code_base() = default;
1361 _Hash_code_base(const _ExtractKey
& __ex
,
1362 const _H1
& __h1
, const _H2
& __h2
,
1363 const _Default_ranged_hash
&)
1364 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1367 _M_hash_code(const _Key
& __k
) const
1368 { return _M_h1()(__k
); }
1371 _M_bucket_index(const _Key
&, __hash_code __c
,
1372 std::size_t __n
) const
1373 { return _M_h2()(__c
, __n
); }
1376 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1377 noexcept( noexcept(declval
<const _H2
&>()((__hash_code
)0,
1379 { return _M_h2()(__p
->_M_hash_code
, __n
); }
1382 _M_store_code(__node_type
* __n
, __hash_code __c
) const
1383 { __n
->_M_hash_code
= __c
; }
1386 _M_copy_code(__node_type
* __to
, const __node_type
* __from
) const
1387 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
1390 _M_swap(_Hash_code_base
& __x
)
1392 std::swap(_M_extract(), __x
._M_extract());
1393 std::swap(_M_h1(), __x
._M_h1());
1394 std::swap(_M_h2(), __x
._M_h2());
1398 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1401 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1404 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1407 _M_h1() { return __ebo_h1::_S_get(*this); }
1410 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1413 _M_h2() { return __ebo_h2::_S_get(*this); }
1417 * Primary class template _Equal_helper.
1420 template <typename _Key
, typename _Value
, typename _ExtractKey
,
1421 typename _Equal
, typename _HashCodeType
,
1422 bool __cache_hash_code
>
1423 struct _Equal_helper
;
1426 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1427 typename _Equal
, typename _HashCodeType
>
1428 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, true>
1431 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1432 const _Key
& __k
, _HashCodeType __c
, _Hash_node
<_Value
, true>* __n
)
1433 { return __c
== __n
->_M_hash_code
&& __eq(__k
, __extract(__n
->_M_v())); }
1437 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1438 typename _Equal
, typename _HashCodeType
>
1439 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, false>
1442 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1443 const _Key
& __k
, _HashCodeType
, _Hash_node
<_Value
, false>* __n
)
1444 { return __eq(__k
, __extract(__n
->_M_v())); }
1448 /// Partial specialization used when nodes contain a cached hash code.
1449 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1450 typename _H1
, typename _H2
, typename _Hash
>
1451 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1452 _H1
, _H2
, _Hash
, true>
1453 : private _Hashtable_ebo_helper
<0, _H2
>
1456 using __base_type
= _Hashtable_ebo_helper
<0, _H2
>;
1457 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1458 _H1
, _H2
, _Hash
, true>;
1460 _Local_iterator_base() = default;
1461 _Local_iterator_base(const __hash_code_base
& __base
,
1462 _Hash_node
<_Value
, true>* __p
,
1463 std::size_t __bkt
, std::size_t __bkt_count
)
1464 : __base_type(__base
._M_h2()),
1465 _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1470 _M_cur
= _M_cur
->_M_next();
1474 = __base_type::_S_get(*this)(_M_cur
->_M_hash_code
,
1476 if (__bkt
!= _M_bucket
)
1481 _Hash_node
<_Value
, true>* _M_cur
;
1482 std::size_t _M_bucket
;
1483 std::size_t _M_bucket_count
;
1487 _M_curr() const { return _M_cur
; } // for equality ops
1490 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1493 // Uninitialized storage for a _Hash_code_base.
1494 // This type is DefaultConstructible and Assignable even if the
1495 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1496 // can be DefaultConstructible and Assignable.
1497 template<typename _Tp
, bool _IsEmpty
= std::is_empty
<_Tp
>::value
>
1498 struct _Hash_code_storage
1500 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
1503 _M_h() { return _M_storage
._M_ptr(); }
1506 _M_h() const { return _M_storage
._M_ptr(); }
1509 // Empty partial specialization for empty _Hash_code_base types.
1510 template<typename _Tp
>
1511 struct _Hash_code_storage
<_Tp
, true>
1513 static_assert( std::is_empty
<_Tp
>::value
, "Type must be empty" );
1515 // As _Tp is an empty type there will be no bytes written/read through
1516 // the cast pointer, so no strict-aliasing violation.
1518 _M_h() { return reinterpret_cast<_Tp
*>(this); }
1521 _M_h() const { return reinterpret_cast<const _Tp
*>(this); }
1524 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1525 typename _H1
, typename _H2
, typename _Hash
>
1526 using __hash_code_for_local_iter
1527 = _Hash_code_storage
<_Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1528 _H1
, _H2
, _Hash
, false>>;
1530 // Partial specialization used when hash codes are not cached
1531 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1532 typename _H1
, typename _H2
, typename _Hash
>
1533 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1534 _H1
, _H2
, _Hash
, false>
1535 : __hash_code_for_local_iter
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
>
1538 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1539 _H1
, _H2
, _Hash
, false>;
1541 _Local_iterator_base() : _M_bucket_count(-1) { }
1543 _Local_iterator_base(const __hash_code_base
& __base
,
1544 _Hash_node
<_Value
, false>* __p
,
1545 std::size_t __bkt
, std::size_t __bkt_count
)
1546 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
)
1547 { _M_init(__base
); }
1549 ~_Local_iterator_base()
1551 if (_M_bucket_count
!= -1)
1555 _Local_iterator_base(const _Local_iterator_base
& __iter
)
1556 : _M_cur(__iter
._M_cur
), _M_bucket(__iter
._M_bucket
),
1557 _M_bucket_count(__iter
._M_bucket_count
)
1559 if (_M_bucket_count
!= -1)
1560 _M_init(*__iter
._M_h());
1563 _Local_iterator_base
&
1564 operator=(const _Local_iterator_base
& __iter
)
1566 if (_M_bucket_count
!= -1)
1568 _M_cur
= __iter
._M_cur
;
1569 _M_bucket
= __iter
._M_bucket
;
1570 _M_bucket_count
= __iter
._M_bucket_count
;
1571 if (_M_bucket_count
!= -1)
1572 _M_init(*__iter
._M_h());
1579 _M_cur
= _M_cur
->_M_next();
1582 std::size_t __bkt
= this->_M_h()->_M_bucket_index(_M_cur
,
1584 if (__bkt
!= _M_bucket
)
1589 _Hash_node
<_Value
, false>* _M_cur
;
1590 std::size_t _M_bucket
;
1591 std::size_t _M_bucket_count
;
1594 _M_init(const __hash_code_base
& __base
)
1595 { ::new(this->_M_h()) __hash_code_base(__base
); }
1598 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1602 _M_curr() const { return _M_cur
; } // for equality ops and debug mode
1605 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1608 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1609 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1611 operator==(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1612 _H1
, _H2
, _Hash
, __cache
>& __x
,
1613 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1614 _H1
, _H2
, _Hash
, __cache
>& __y
)
1615 { return __x
._M_curr() == __y
._M_curr(); }
1617 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1618 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1620 operator!=(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1621 _H1
, _H2
, _Hash
, __cache
>& __x
,
1622 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1623 _H1
, _H2
, _Hash
, __cache
>& __y
)
1624 { return __x
._M_curr() != __y
._M_curr(); }
1627 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1628 typename _H1
, typename _H2
, typename _Hash
,
1629 bool __constant_iterators
, bool __cache
>
1630 struct _Local_iterator
1631 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1632 _H1
, _H2
, _Hash
, __cache
>
1635 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1636 _H1
, _H2
, _Hash
, __cache
>;
1637 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1639 typedef _Value value_type
;
1640 typedef typename
std::conditional
<__constant_iterators
,
1641 const _Value
*, _Value
*>::type
1643 typedef typename
std::conditional
<__constant_iterators
,
1644 const _Value
&, _Value
&>::type
1646 typedef std::ptrdiff_t difference_type
;
1647 typedef std::forward_iterator_tag iterator_category
;
1649 _Local_iterator() = default;
1651 _Local_iterator(const __hash_code_base
& __base
,
1652 _Hash_node
<_Value
, __cache
>* __p
,
1653 std::size_t __bkt
, std::size_t __bkt_count
)
1654 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1659 { return this->_M_cur
->_M_v(); }
1663 { return this->_M_cur
->_M_valptr(); }
1675 _Local_iterator
__tmp(*this);
1681 /// local const_iterators
1682 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1683 typename _H1
, typename _H2
, typename _Hash
,
1684 bool __constant_iterators
, bool __cache
>
1685 struct _Local_const_iterator
1686 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1687 _H1
, _H2
, _Hash
, __cache
>
1690 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1691 _H1
, _H2
, _Hash
, __cache
>;
1692 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1695 typedef _Value value_type
;
1696 typedef const _Value
* pointer
;
1697 typedef const _Value
& reference
;
1698 typedef std::ptrdiff_t difference_type
;
1699 typedef std::forward_iterator_tag iterator_category
;
1701 _Local_const_iterator() = default;
1703 _Local_const_iterator(const __hash_code_base
& __base
,
1704 _Hash_node
<_Value
, __cache
>* __p
,
1705 std::size_t __bkt
, std::size_t __bkt_count
)
1706 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1709 _Local_const_iterator(const _Local_iterator
<_Key
, _Value
, _ExtractKey
,
1711 __constant_iterators
,
1718 { return this->_M_cur
->_M_v(); }
1722 { return this->_M_cur
->_M_valptr(); }
1724 _Local_const_iterator
&
1731 _Local_const_iterator
1734 _Local_const_iterator
__tmp(*this);
1741 * Primary class template _Hashtable_base.
1743 * Helper class adding management of _Equal functor to
1744 * _Hash_code_base type.
1746 * Base class templates are:
1747 * - __detail::_Hash_code_base
1748 * - __detail::_Hashtable_ebo_helper
1750 template<typename _Key
, typename _Value
,
1751 typename _ExtractKey
, typename _Equal
,
1752 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
1753 struct _Hashtable_base
1754 : public _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1755 _Traits::__hash_cached::value
>,
1756 private _Hashtable_ebo_helper
<0, _Equal
>
1759 typedef _Key key_type
;
1760 typedef _Value value_type
;
1761 typedef _Equal key_equal
;
1762 typedef std::size_t size_type
;
1763 typedef std::ptrdiff_t difference_type
;
1765 using __traits_type
= _Traits
;
1766 using __hash_cached
= typename
__traits_type::__hash_cached
;
1767 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
1768 using __unique_keys
= typename
__traits_type::__unique_keys
;
1770 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1772 __hash_cached::value
>;
1774 using __hash_code
= typename
__hash_code_base::__hash_code
;
1775 using __node_type
= typename
__hash_code_base::__node_type
;
1777 using iterator
= __detail::_Node_iterator
<value_type
,
1778 __constant_iterators::value
,
1779 __hash_cached::value
>;
1781 using const_iterator
= __detail::_Node_const_iterator
<value_type
,
1782 __constant_iterators::value
,
1783 __hash_cached::value
>;
1785 using local_iterator
= __detail::_Local_iterator
<key_type
, value_type
,
1786 _ExtractKey
, _H1
, _H2
, _Hash
,
1787 __constant_iterators::value
,
1788 __hash_cached::value
>;
1790 using const_local_iterator
= __detail::_Local_const_iterator
<key_type
,
1792 _ExtractKey
, _H1
, _H2
, _Hash
,
1793 __constant_iterators::value
,
1794 __hash_cached::value
>;
1796 using __ireturn_type
= typename
std::conditional
<__unique_keys::value
,
1797 std::pair
<iterator
, bool>,
1800 using _EqualEBO
= _Hashtable_ebo_helper
<0, _Equal
>;
1801 using _EqualHelper
= _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
,
1802 __hash_code
, __hash_cached::value
>;
1805 _Hashtable_base() = default;
1806 _Hashtable_base(const _ExtractKey
& __ex
, const _H1
& __h1
, const _H2
& __h2
,
1807 const _Hash
& __hash
, const _Equal
& __eq
)
1808 : __hash_code_base(__ex
, __h1
, __h2
, __hash
), _EqualEBO(__eq
)
1812 _M_equals(const _Key
& __k
, __hash_code __c
, __node_type
* __n
) const
1814 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
1819 _M_swap(_Hashtable_base
& __x
)
1821 __hash_code_base::_M_swap(__x
);
1822 std::swap(_M_eq(), __x
._M_eq());
1826 _M_eq() const { return _EqualEBO::_S_cget(*this); }
1829 _M_eq() { return _EqualEBO::_S_get(*this); }
1833 * struct _Equality_base.
1835 * Common types and functions for class _Equality.
1837 struct _Equality_base
1840 template<typename _Uiterator
>
1842 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
1845 // See std::is_permutation in N3068.
1846 template<typename _Uiterator
>
1849 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
1850 _Uiterator __first2
)
1852 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1853 if (!(*__first1
== *__first2
))
1856 if (__first1
== __last1
)
1859 _Uiterator __last2
= __first2
;
1860 std::advance(__last2
, std::distance(__first1
, __last1
));
1862 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
1864 _Uiterator __tmp
= __first1
;
1865 while (__tmp
!= __it1
&& !bool(*__tmp
== *__it1
))
1868 // We've seen this one before.
1872 std::ptrdiff_t __n2
= 0;
1873 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
1874 if (*__tmp
== *__it1
)
1880 std::ptrdiff_t __n1
= 0;
1881 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
1882 if (*__tmp
== *__it1
)
1892 * Primary class template _Equality.
1894 * This is for implementing equality comparison for unordered
1895 * containers, per N3068, by John Lakos and Pablo Halpern.
1896 * Algorithmically, we follow closely the reference implementations
1899 template<typename _Key
, typename _Value
, typename _Alloc
,
1900 typename _ExtractKey
, typename _Equal
,
1901 typename _H1
, typename _H2
, typename _Hash
,
1902 typename _RehashPolicy
, typename _Traits
,
1903 bool _Unique_keys
= _Traits::__unique_keys::value
>
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
>
1911 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1912 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
1914 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1915 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1918 _M_equal(const __hashtable
&) const;
1921 template<typename _Key
, typename _Value
, typename _Alloc
,
1922 typename _ExtractKey
, typename _Equal
,
1923 typename _H1
, typename _H2
, typename _Hash
,
1924 typename _RehashPolicy
, typename _Traits
>
1926 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1927 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
1928 _M_equal(const __hashtable
& __other
) const
1930 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1932 if (__this
->size() != __other
.size())
1935 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
1937 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
1938 if (__ity
== __other
.end() || !bool(*__ity
== *__itx
))
1945 template<typename _Key
, typename _Value
, typename _Alloc
,
1946 typename _ExtractKey
, typename _Equal
,
1947 typename _H1
, typename _H2
, typename _Hash
,
1948 typename _RehashPolicy
, typename _Traits
>
1949 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1950 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
1951 : public _Equality_base
1953 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1954 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1957 _M_equal(const __hashtable
&) const;
1960 template<typename _Key
, typename _Value
, typename _Alloc
,
1961 typename _ExtractKey
, typename _Equal
,
1962 typename _H1
, typename _H2
, typename _Hash
,
1963 typename _RehashPolicy
, typename _Traits
>
1965 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1966 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>::
1967 _M_equal(const __hashtable
& __other
) const
1969 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1971 if (__this
->size() != __other
.size())
1974 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
1976 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
1977 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
1979 if (std::distance(__xrange
.first
, __xrange
.second
)
1980 != std::distance(__yrange
.first
, __yrange
.second
))
1983 if (!_S_is_permutation(__xrange
.first
, __xrange
.second
,
1987 __itx
= __xrange
.second
;
1993 * This type deals with all allocation and keeps an allocator instance through
1994 * inheritance to benefit from EBO when possible.
1996 template<typename _NodeAlloc
>
1997 struct _Hashtable_alloc
: private _Hashtable_ebo_helper
<0, _NodeAlloc
>
2000 using __ebo_node_alloc
= _Hashtable_ebo_helper
<0, _NodeAlloc
>;
2002 using __node_type
= typename
_NodeAlloc::value_type
;
2003 using __node_alloc_type
= _NodeAlloc
;
2004 // Use __gnu_cxx to benefit from _S_always_equal and al.
2005 using __node_alloc_traits
= __gnu_cxx::__alloc_traits
<__node_alloc_type
>;
2007 using __value_type
= typename
__node_type::value_type
;
2008 using __value_alloc_type
=
2009 __alloc_rebind
<__node_alloc_type
, __value_type
>;
2010 using __value_alloc_traits
= std::allocator_traits
<__value_alloc_type
>;
2012 using __node_base
= __detail::_Hash_node_base
;
2013 using __bucket_type
= __node_base
*;
2014 using __bucket_alloc_type
=
2015 __alloc_rebind
<__node_alloc_type
, __bucket_type
>;
2016 using __bucket_alloc_traits
= std::allocator_traits
<__bucket_alloc_type
>;
2018 _Hashtable_alloc() = default;
2019 _Hashtable_alloc(const _Hashtable_alloc
&) = default;
2020 _Hashtable_alloc(_Hashtable_alloc
&&) = default;
2022 template<typename _Alloc
>
2023 _Hashtable_alloc(_Alloc
&& __a
)
2024 : __ebo_node_alloc(std::forward
<_Alloc
>(__a
))
2029 { return __ebo_node_alloc::_S_get(*this); }
2031 const __node_alloc_type
&
2032 _M_node_allocator() const
2033 { return __ebo_node_alloc::_S_cget(*this); }
2035 template<typename
... _Args
>
2037 _M_allocate_node(_Args
&&... __args
);
2040 _M_deallocate_node(__node_type
* __n
);
2042 // Deallocate the linked list of nodes pointed to by __n
2044 _M_deallocate_nodes(__node_type
* __n
);
2047 _M_allocate_buckets(std::size_t __n
);
2050 _M_deallocate_buckets(__bucket_type
*, std::size_t __n
);
2053 // Definitions of class template _Hashtable_alloc's out-of-line member
2055 template<typename _NodeAlloc
>
2056 template<typename
... _Args
>
2057 typename _Hashtable_alloc
<_NodeAlloc
>::__node_type
*
2058 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_node(_Args
&&... __args
)
2060 auto __nptr
= __node_alloc_traits::allocate(_M_node_allocator(), 1);
2061 __node_type
* __n
= std::__addressof(*__nptr
);
2064 __value_alloc_type
__a(_M_node_allocator());
2065 ::new ((void*)__n
) __node_type
;
2066 __value_alloc_traits::construct(__a
, __n
->_M_valptr(),
2067 std::forward
<_Args
>(__args
)...);
2072 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr
, 1);
2073 __throw_exception_again
;
2077 template<typename _NodeAlloc
>
2079 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_node(__node_type
* __n
)
2081 typedef typename
__node_alloc_traits::pointer _Ptr
;
2082 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__n
);
2083 __value_alloc_type
__a(_M_node_allocator());
2084 __value_alloc_traits::destroy(__a
, __n
->_M_valptr());
2085 __n
->~__node_type();
2086 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr
, 1);
2089 template<typename _NodeAlloc
>
2091 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_nodes(__node_type
* __n
)
2095 __node_type
* __tmp
= __n
;
2096 __n
= __n
->_M_next();
2097 _M_deallocate_node(__tmp
);
2101 template<typename _NodeAlloc
>
2102 typename _Hashtable_alloc
<_NodeAlloc
>::__bucket_type
*
2103 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_buckets(std::size_t __n
)
2105 __bucket_alloc_type
__alloc(_M_node_allocator());
2107 auto __ptr
= __bucket_alloc_traits::allocate(__alloc
, __n
);
2108 __bucket_type
* __p
= std::__addressof(*__ptr
);
2109 __builtin_memset(__p
, 0, __n
* sizeof(__bucket_type
));
2113 template<typename _NodeAlloc
>
2115 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_buckets(__bucket_type
* __bkts
,
2118 typedef typename
__bucket_alloc_traits::pointer _Ptr
;
2119 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__bkts
);
2120 __bucket_alloc_type
__alloc(_M_node_allocator());
2121 __bucket_alloc_traits::deallocate(__alloc
, __ptr
, __n
);
2124 //@} hashtable-detail
2125 _GLIBCXX_END_NAMESPACE_VERSION
2126 } // namespace __detail
2129 #endif // _HASHTABLE_POLICY_H