1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010-2018 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 <cstdint> // for std::uint_fast64_t
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 __node
->~__node_type();
139 __node_alloc_traits::deallocate(__a
, __node
, 1);
140 __throw_exception_again
;
144 return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
));
148 mutable __node_type
* _M_nodes
;
149 __hashtable_alloc
& _M_h
;
152 // Functor similar to the previous one but without any pool of nodes to
154 template<typename _NodeAlloc
>
158 using __hashtable_alloc
= _Hashtable_alloc
<_NodeAlloc
>;
159 using __node_type
= typename
__hashtable_alloc::__node_type
;
162 _AllocNode(__hashtable_alloc
& __h
)
165 template<typename _Arg
>
167 operator()(_Arg
&& __arg
) const
168 { return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
)); }
171 __hashtable_alloc
& _M_h
;
174 // Auxiliary types used for all instantiations of _Hashtable nodes
178 * struct _Hashtable_traits
180 * Important traits for hash tables.
182 * @tparam _Cache_hash_code Boolean value. True if the value of
183 * the hash function is stored along with the value. This is a
184 * time-space tradeoff. Storing it may improve lookup speed by
185 * reducing the number of times we need to call the _Equal
188 * @tparam _Constant_iterators Boolean value. True if iterator and
189 * const_iterator are both constant iterator types. This is true
190 * for unordered_set and unordered_multiset, false for
191 * unordered_map and unordered_multimap.
193 * @tparam _Unique_keys Boolean value. True if the return value
194 * of _Hashtable::count(k) is always at most one, false if it may
195 * be an arbitrary number. This is true for unordered_set and
196 * unordered_map, false for unordered_multiset and
197 * unordered_multimap.
199 template<bool _Cache_hash_code
, bool _Constant_iterators
, bool _Unique_keys
>
200 struct _Hashtable_traits
202 using __hash_cached
= __bool_constant
<_Cache_hash_code
>;
203 using __constant_iterators
= __bool_constant
<_Constant_iterators
>;
204 using __unique_keys
= __bool_constant
<_Unique_keys
>;
208 * struct _Hash_node_base
210 * Nodes, used to wrap elements stored in the hash table. A policy
211 * template parameter of class template _Hashtable controls whether
212 * nodes also store a hash code. In some cases (e.g. strings) this
213 * may be a performance win.
215 struct _Hash_node_base
217 _Hash_node_base
* _M_nxt
;
219 _Hash_node_base() noexcept
: _M_nxt() { }
221 _Hash_node_base(_Hash_node_base
* __next
) noexcept
: _M_nxt(__next
) { }
225 * struct _Hash_node_value_base
227 * Node type with the value to store.
229 template<typename _Value
>
230 struct _Hash_node_value_base
: _Hash_node_base
232 typedef _Value value_type
;
234 __gnu_cxx::__aligned_buffer
<_Value
> _M_storage
;
238 { return _M_storage
._M_ptr(); }
241 _M_valptr() const noexcept
242 { return _M_storage
._M_ptr(); }
246 { return *_M_valptr(); }
249 _M_v() const noexcept
250 { return *_M_valptr(); }
254 * Primary template struct _Hash_node.
256 template<typename _Value
, bool _Cache_hash_code
>
260 * Specialization for nodes with caches, struct _Hash_node.
262 * Base class is __detail::_Hash_node_value_base.
264 template<typename _Value
>
265 struct _Hash_node
<_Value
, true> : _Hash_node_value_base
<_Value
>
267 std::size_t _M_hash_code
;
270 _M_next() const noexcept
271 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
275 * Specialization for nodes without caches, struct _Hash_node.
277 * Base class is __detail::_Hash_node_value_base.
279 template<typename _Value
>
280 struct _Hash_node
<_Value
, false> : _Hash_node_value_base
<_Value
>
283 _M_next() const noexcept
284 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
287 /// Base class for node iterators.
288 template<typename _Value
, bool _Cache_hash_code
>
289 struct _Node_iterator_base
291 using __node_type
= _Hash_node
<_Value
, _Cache_hash_code
>;
295 _Node_iterator_base(__node_type
* __p
) noexcept
300 { _M_cur
= _M_cur
->_M_next(); }
303 template<typename _Value
, bool _Cache_hash_code
>
305 operator==(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
306 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
308 { return __x
._M_cur
== __y
._M_cur
; }
310 template<typename _Value
, bool _Cache_hash_code
>
312 operator!=(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
313 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
315 { return __x
._M_cur
!= __y
._M_cur
; }
317 /// Node iterators, used to iterate through all the hashtable.
318 template<typename _Value
, bool __constant_iterators
, bool __cache
>
319 struct _Node_iterator
320 : public _Node_iterator_base
<_Value
, __cache
>
323 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
324 using __node_type
= typename
__base_type::__node_type
;
327 typedef _Value value_type
;
328 typedef std::ptrdiff_t difference_type
;
329 typedef std::forward_iterator_tag iterator_category
;
331 using pointer
= typename
std::conditional
<__constant_iterators
,
332 const _Value
*, _Value
*>::type
;
334 using reference
= typename
std::conditional
<__constant_iterators
,
335 const _Value
&, _Value
&>::type
;
337 _Node_iterator() noexcept
341 _Node_iterator(__node_type
* __p
) noexcept
342 : __base_type(__p
) { }
345 operator*() const noexcept
346 { return this->_M_cur
->_M_v(); }
349 operator->() const noexcept
350 { return this->_M_cur
->_M_valptr(); }
353 operator++() noexcept
360 operator++(int) noexcept
362 _Node_iterator
__tmp(*this);
368 /// Node const_iterators, used to iterate through all the hashtable.
369 template<typename _Value
, bool __constant_iterators
, bool __cache
>
370 struct _Node_const_iterator
371 : public _Node_iterator_base
<_Value
, __cache
>
374 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
375 using __node_type
= typename
__base_type::__node_type
;
378 typedef _Value value_type
;
379 typedef std::ptrdiff_t difference_type
;
380 typedef std::forward_iterator_tag iterator_category
;
382 typedef const _Value
* pointer
;
383 typedef const _Value
& reference
;
385 _Node_const_iterator() noexcept
389 _Node_const_iterator(__node_type
* __p
) noexcept
390 : __base_type(__p
) { }
392 _Node_const_iterator(const _Node_iterator
<_Value
, __constant_iterators
,
393 __cache
>& __x
) noexcept
394 : __base_type(__x
._M_cur
) { }
397 operator*() const noexcept
398 { return this->_M_cur
->_M_v(); }
401 operator->() const noexcept
402 { return this->_M_cur
->_M_valptr(); }
404 _Node_const_iterator
&
405 operator++() noexcept
412 operator++(int) noexcept
414 _Node_const_iterator
__tmp(*this);
420 // Many of class template _Hashtable's template parameters are policy
421 // classes. These are defaults for the policies.
423 /// Default range hashing function: use division to fold a large number
424 /// into the range [0, N).
425 struct _Mod_range_hashing
427 typedef std::size_t first_argument_type
;
428 typedef std::size_t second_argument_type
;
429 typedef std::size_t result_type
;
432 operator()(first_argument_type __num
,
433 second_argument_type __den
) const noexcept
434 { return __num
% __den
; }
437 /// Default ranged hash function H. In principle it should be a
438 /// function object composed from objects of type H1 and H2 such that
439 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
440 /// h1 and h2. So instead we'll just use a tag to tell class template
441 /// hashtable to do that composition.
442 struct _Default_ranged_hash
{ };
444 /// Default value for rehash policy. Bucket size is (usually) the
445 /// smallest prime that keeps the load factor small enough.
446 struct _Prime_rehash_policy
448 using __has_load_factor
= std::true_type
;
450 _Prime_rehash_policy(float __z
= 1.0) noexcept
451 : _M_max_load_factor(__z
), _M_next_resize(0) { }
454 max_load_factor() const noexcept
455 { return _M_max_load_factor
; }
457 // Return a bucket size no smaller than n.
459 _M_next_bkt(std::size_t __n
) const;
461 // Return a bucket count appropriate for n elements
463 _M_bkt_for_elements(std::size_t __n
) const
464 { return __builtin_ceil(__n
/ (long double)_M_max_load_factor
); }
466 // __n_bkt is current bucket count, __n_elt is current element count,
467 // and __n_ins is number of elements to be inserted. Do we need to
468 // increase bucket count? If so, return make_pair(true, n), where n
469 // is the new bucket count. If not, return make_pair(false, 0).
470 std::pair
<bool, std::size_t>
471 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
472 std::size_t __n_ins
) const;
474 typedef std::size_t _State
;
478 { return _M_next_resize
; }
482 { _M_next_resize
= 0; }
485 _M_reset(_State __state
)
486 { _M_next_resize
= __state
; }
488 static const std::size_t _S_growth_factor
= 2;
490 float _M_max_load_factor
;
491 mutable std::size_t _M_next_resize
;
494 /// Range hashing function assuming that second arg is a power of 2.
495 struct _Mask_range_hashing
497 typedef std::size_t first_argument_type
;
498 typedef std::size_t second_argument_type
;
499 typedef std::size_t result_type
;
502 operator()(first_argument_type __num
,
503 second_argument_type __den
) const noexcept
504 { return __num
& (__den
- 1); }
507 /// Compute closest power of 2.
510 __clp2(std::size_t __n
) noexcept
512 #if __SIZEOF_SIZE_T__ >= 8
513 std::uint_fast64_t __x
= __n
;
515 std::uint_fast32_t __x
= __n
;
517 // Algorithm from Hacker's Delight, Figure 3-3.
519 __x
= __x
| (__x
>> 1);
520 __x
= __x
| (__x
>> 2);
521 __x
= __x
| (__x
>> 4);
522 __x
= __x
| (__x
>> 8);
523 __x
= __x
| (__x
>>16);
524 #if __SIZEOF_SIZE_T__ >= 8
525 __x
= __x
| (__x
>>32);
530 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
532 struct _Power2_rehash_policy
534 using __has_load_factor
= std::true_type
;
536 _Power2_rehash_policy(float __z
= 1.0) noexcept
537 : _M_max_load_factor(__z
), _M_next_resize(0) { }
540 max_load_factor() const noexcept
541 { return _M_max_load_factor
; }
543 // Return a bucket size no smaller than n (as long as n is not above the
544 // highest power of 2).
546 _M_next_bkt(std::size_t __n
) noexcept
548 const auto __max_width
= std::min
<size_t>(sizeof(size_t), 8);
549 const auto __max_bkt
= size_t(1) << (__max_width
* __CHAR_BIT__
- 1);
550 std::size_t __res
= __clp2(__n
);
558 if (__res
== __max_bkt
)
559 // Set next resize to the max value so that we never try to rehash again
560 // as we already reach the biggest possible bucket number.
561 // Note that it might result in max_load_factor not being respected.
562 _M_next_resize
= std::size_t(-1);
565 = __builtin_ceil(__res
* (long double)_M_max_load_factor
);
570 // Return a bucket count appropriate for n elements
572 _M_bkt_for_elements(std::size_t __n
) const noexcept
573 { return __builtin_ceil(__n
/ (long double)_M_max_load_factor
); }
575 // __n_bkt is current bucket count, __n_elt is current element count,
576 // and __n_ins is number of elements to be inserted. Do we need to
577 // increase bucket count? If so, return make_pair(true, n), where n
578 // is the new bucket count. If not, return make_pair(false, 0).
579 std::pair
<bool, std::size_t>
580 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
581 std::size_t __n_ins
) noexcept
583 if (__n_elt
+ __n_ins
>= _M_next_resize
)
585 long double __min_bkts
= (__n_elt
+ __n_ins
)
586 / (long double)_M_max_load_factor
;
587 if (__min_bkts
>= __n_bkt
)
588 return std::make_pair(true,
589 _M_next_bkt(std::max
<std::size_t>(__builtin_floor(__min_bkts
) + 1,
590 __n_bkt
* _S_growth_factor
)));
593 = __builtin_floor(__n_bkt
* (long double)_M_max_load_factor
);
594 return std::make_pair(false, 0);
597 return std::make_pair(false, 0);
600 typedef std::size_t _State
;
603 _M_state() const noexcept
604 { return _M_next_resize
; }
608 { _M_next_resize
= 0; }
611 _M_reset(_State __state
) noexcept
612 { _M_next_resize
= __state
; }
614 static const std::size_t _S_growth_factor
= 2;
616 float _M_max_load_factor
;
617 std::size_t _M_next_resize
;
620 // Base classes for std::_Hashtable. We define these base classes
621 // because in some cases we want to do different things depending on
622 // the value of a policy class. In some cases the policy class
623 // affects which member functions and nested typedefs are defined;
624 // we handle that by specializing base class templates. Several of
625 // the base class templates need to access other members of class
626 // template _Hashtable, so we use a variant of the "Curiously
627 // Recurring Template Pattern" (CRTP) technique.
630 * Primary class template _Map_base.
632 * If the hashtable has a value type of the form pair<T1, T2> and a
633 * key extraction policy (_ExtractKey) that returns the first part
634 * of the pair, the hashtable gets a mapped_type typedef. If it
635 * satisfies those criteria and also has unique keys, then it also
636 * gets an operator[].
638 template<typename _Key
, typename _Value
, typename _Alloc
,
639 typename _ExtractKey
, typename _Equal
,
640 typename _H1
, typename _H2
, typename _Hash
,
641 typename _RehashPolicy
, typename _Traits
,
642 bool _Unique_keys
= _Traits::__unique_keys::value
>
643 struct _Map_base
{ };
645 /// Partial specialization, __unique_keys set to false.
646 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
647 typename _H1
, typename _H2
, typename _Hash
,
648 typename _RehashPolicy
, typename _Traits
>
649 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
650 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
652 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
655 /// Partial specialization, __unique_keys set to true.
656 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
657 typename _H1
, typename _H2
, typename _Hash
,
658 typename _RehashPolicy
, typename _Traits
>
659 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
660 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
663 using __hashtable_base
= __detail::_Hashtable_base
<_Key
, _Pair
,
665 _Equal
, _H1
, _H2
, _Hash
,
668 using __hashtable
= _Hashtable
<_Key
, _Pair
, _Alloc
,
670 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
672 using __hash_code
= typename
__hashtable_base::__hash_code
;
673 using __node_type
= typename
__hashtable_base::__node_type
;
676 using key_type
= typename
__hashtable_base::key_type
;
677 using iterator
= typename
__hashtable_base::iterator
;
678 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
681 operator[](const key_type
& __k
);
684 operator[](key_type
&& __k
);
686 // _GLIBCXX_RESOLVE_LIB_DEFECTS
687 // DR 761. unordered_map needs an at() member function.
689 at(const key_type
& __k
);
692 at(const key_type
& __k
) const;
695 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
696 typename _H1
, typename _H2
, typename _Hash
,
697 typename _RehashPolicy
, typename _Traits
>
699 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
700 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
701 operator[](const key_type
& __k
)
704 __hashtable
* __h
= static_cast<__hashtable
*>(this);
705 __hash_code __code
= __h
->_M_hash_code(__k
);
706 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
707 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
711 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
712 std::tuple
<const key_type
&>(__k
),
714 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
717 return __p
->_M_v().second
;
720 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
721 typename _H1
, typename _H2
, typename _Hash
,
722 typename _RehashPolicy
, typename _Traits
>
724 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
725 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
726 operator[](key_type
&& __k
)
729 __hashtable
* __h
= static_cast<__hashtable
*>(this);
730 __hash_code __code
= __h
->_M_hash_code(__k
);
731 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
732 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
736 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
737 std::forward_as_tuple(std::move(__k
)),
739 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
742 return __p
->_M_v().second
;
745 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
746 typename _H1
, typename _H2
, typename _Hash
,
747 typename _RehashPolicy
, typename _Traits
>
749 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
750 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
751 at(const key_type
& __k
)
754 __hashtable
* __h
= static_cast<__hashtable
*>(this);
755 __hash_code __code
= __h
->_M_hash_code(__k
);
756 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
757 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
760 __throw_out_of_range(__N("_Map_base::at"));
761 return __p
->_M_v().second
;
764 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
765 typename _H1
, typename _H2
, typename _Hash
,
766 typename _RehashPolicy
, typename _Traits
>
768 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
769 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
770 at(const key_type
& __k
) const
771 -> const mapped_type
&
773 const __hashtable
* __h
= static_cast<const __hashtable
*>(this);
774 __hash_code __code
= __h
->_M_hash_code(__k
);
775 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
776 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
779 __throw_out_of_range(__N("_Map_base::at"));
780 return __p
->_M_v().second
;
784 * Primary class template _Insert_base.
786 * Defines @c insert member functions appropriate to all _Hashtables.
788 template<typename _Key
, typename _Value
, typename _Alloc
,
789 typename _ExtractKey
, typename _Equal
,
790 typename _H1
, typename _H2
, typename _Hash
,
791 typename _RehashPolicy
, typename _Traits
>
795 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
796 _Equal
, _H1
, _H2
, _Hash
,
797 _RehashPolicy
, _Traits
>;
799 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
800 _Equal
, _H1
, _H2
, _Hash
,
803 using value_type
= typename
__hashtable_base::value_type
;
804 using iterator
= typename
__hashtable_base::iterator
;
805 using const_iterator
= typename
__hashtable_base::const_iterator
;
806 using size_type
= typename
__hashtable_base::size_type
;
808 using __unique_keys
= typename
__hashtable_base::__unique_keys
;
809 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
810 using __node_type
= _Hash_node
<_Value
, _Traits::__hash_cached::value
>;
811 using __node_alloc_type
= __alloc_rebind
<_Alloc
, __node_type
>;
812 using __node_gen_type
= _AllocNode
<__node_alloc_type
>;
815 _M_conjure_hashtable()
816 { return *(static_cast<__hashtable
*>(this)); }
818 template<typename _InputIterator
, typename _NodeGetter
>
820 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
821 const _NodeGetter
&, true_type
);
823 template<typename _InputIterator
, typename _NodeGetter
>
825 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
826 const _NodeGetter
&, false_type
);
830 insert(const value_type
& __v
)
832 __hashtable
& __h
= _M_conjure_hashtable();
833 __node_gen_type
__node_gen(__h
);
834 return __h
._M_insert(__v
, __node_gen
, __unique_keys());
838 insert(const_iterator __hint
, const value_type
& __v
)
840 __hashtable
& __h
= _M_conjure_hashtable();
841 __node_gen_type
__node_gen(__h
);
842 return __h
._M_insert(__hint
, __v
, __node_gen
, __unique_keys());
846 insert(initializer_list
<value_type
> __l
)
847 { this->insert(__l
.begin(), __l
.end()); }
849 template<typename _InputIterator
>
851 insert(_InputIterator __first
, _InputIterator __last
)
853 __hashtable
& __h
= _M_conjure_hashtable();
854 __node_gen_type
__node_gen(__h
);
855 return _M_insert_range(__first
, __last
, __node_gen
, __unique_keys());
859 template<typename _Key
, typename _Value
, typename _Alloc
,
860 typename _ExtractKey
, typename _Equal
,
861 typename _H1
, typename _H2
, typename _Hash
,
862 typename _RehashPolicy
, typename _Traits
>
863 template<typename _InputIterator
, typename _NodeGetter
>
865 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
866 _RehashPolicy
, _Traits
>::
867 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
868 const _NodeGetter
& __node_gen
, true_type
)
870 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
874 __hashtable
& __h
= _M_conjure_hashtable();
875 for (; __first
!= __last
; ++__first
)
877 if (__h
._M_insert(*__first
, __node_gen
, __unique_keys(),
880 else if (__n_elt
!= 1)
885 template<typename _Key
, typename _Value
, typename _Alloc
,
886 typename _ExtractKey
, typename _Equal
,
887 typename _H1
, typename _H2
, typename _Hash
,
888 typename _RehashPolicy
, typename _Traits
>
889 template<typename _InputIterator
, typename _NodeGetter
>
891 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
892 _RehashPolicy
, _Traits
>::
893 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
894 const _NodeGetter
& __node_gen
, false_type
)
896 using __rehash_type
= typename
__hashtable::__rehash_type
;
897 using __rehash_state
= typename
__hashtable::__rehash_state
;
898 using pair_type
= std::pair
<bool, std::size_t>;
900 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
904 __hashtable
& __h
= _M_conjure_hashtable();
905 __rehash_type
& __rehash
= __h
._M_rehash_policy
;
906 const __rehash_state
& __saved_state
= __rehash
._M_state();
907 pair_type __do_rehash
= __rehash
._M_need_rehash(__h
._M_bucket_count
,
908 __h
._M_element_count
,
911 if (__do_rehash
.first
)
912 __h
._M_rehash(__do_rehash
.second
, __saved_state
);
914 for (; __first
!= __last
; ++__first
)
915 __h
._M_insert(*__first
, __node_gen
, __unique_keys());
919 * Primary class template _Insert.
921 * Defines @c insert member functions that depend on _Hashtable policies,
922 * via partial specializations.
924 template<typename _Key
, typename _Value
, typename _Alloc
,
925 typename _ExtractKey
, typename _Equal
,
926 typename _H1
, typename _H2
, typename _Hash
,
927 typename _RehashPolicy
, typename _Traits
,
928 bool _Constant_iterators
= _Traits::__constant_iterators::value
>
932 template<typename _Key
, typename _Value
, typename _Alloc
,
933 typename _ExtractKey
, typename _Equal
,
934 typename _H1
, typename _H2
, typename _Hash
,
935 typename _RehashPolicy
, typename _Traits
>
936 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
937 _RehashPolicy
, _Traits
, true>
938 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
939 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
941 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
942 _Equal
, _H1
, _H2
, _Hash
,
943 _RehashPolicy
, _Traits
>;
945 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
946 _Equal
, _H1
, _H2
, _Hash
,
949 using value_type
= typename
__base_type::value_type
;
950 using iterator
= typename
__base_type::iterator
;
951 using const_iterator
= typename
__base_type::const_iterator
;
953 using __unique_keys
= typename
__base_type::__unique_keys
;
954 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
955 using __hashtable
= typename
__base_type::__hashtable
;
956 using __node_gen_type
= typename
__base_type::__node_gen_type
;
958 using __base_type::insert
;
961 insert(value_type
&& __v
)
963 __hashtable
& __h
= this->_M_conjure_hashtable();
964 __node_gen_type
__node_gen(__h
);
965 return __h
._M_insert(std::move(__v
), __node_gen
, __unique_keys());
969 insert(const_iterator __hint
, value_type
&& __v
)
971 __hashtable
& __h
= this->_M_conjure_hashtable();
972 __node_gen_type
__node_gen(__h
);
973 return __h
._M_insert(__hint
, std::move(__v
), __node_gen
,
979 template<typename _Key
, typename _Value
, typename _Alloc
,
980 typename _ExtractKey
, typename _Equal
,
981 typename _H1
, typename _H2
, typename _Hash
,
982 typename _RehashPolicy
, typename _Traits
>
983 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
984 _RehashPolicy
, _Traits
, false>
985 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
986 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
988 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
989 _Equal
, _H1
, _H2
, _Hash
,
990 _RehashPolicy
, _Traits
>;
991 using value_type
= typename
__base_type::value_type
;
992 using iterator
= typename
__base_type::iterator
;
993 using const_iterator
= typename
__base_type::const_iterator
;
995 using __unique_keys
= typename
__base_type::__unique_keys
;
996 using __hashtable
= typename
__base_type::__hashtable
;
997 using __ireturn_type
= typename
__base_type::__ireturn_type
;
999 using __base_type::insert
;
1001 template<typename _Pair
>
1002 using __is_cons
= std::is_constructible
<value_type
, _Pair
&&>;
1004 template<typename _Pair
>
1005 using _IFcons
= std::enable_if
<__is_cons
<_Pair
>::value
>;
1007 template<typename _Pair
>
1008 using _IFconsp
= typename _IFcons
<_Pair
>::type
;
1010 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
1014 __hashtable
& __h
= this->_M_conjure_hashtable();
1015 return __h
._M_emplace(__unique_keys(), std::forward
<_Pair
>(__v
));
1018 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
1020 insert(const_iterator __hint
, _Pair
&& __v
)
1022 __hashtable
& __h
= this->_M_conjure_hashtable();
1023 return __h
._M_emplace(__hint
, __unique_keys(),
1024 std::forward
<_Pair
>(__v
));
1028 template<typename _Policy
>
1029 using __has_load_factor
= typename
_Policy::__has_load_factor
;
1032 * Primary class template _Rehash_base.
1034 * Give hashtable the max_load_factor functions and reserve iff the
1035 * rehash policy supports it.
1037 template<typename _Key
, typename _Value
, typename _Alloc
,
1038 typename _ExtractKey
, typename _Equal
,
1039 typename _H1
, typename _H2
, typename _Hash
,
1040 typename _RehashPolicy
, typename _Traits
,
1042 __detected_or_t
<std::false_type
, __has_load_factor
, _RehashPolicy
>>
1043 struct _Rehash_base
;
1045 /// Specialization when rehash policy doesn't provide load factor management.
1046 template<typename _Key
, typename _Value
, typename _Alloc
,
1047 typename _ExtractKey
, typename _Equal
,
1048 typename _H1
, typename _H2
, typename _Hash
,
1049 typename _RehashPolicy
, typename _Traits
>
1050 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1051 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1056 /// Specialization when rehash policy provide load factor management.
1057 template<typename _Key
, typename _Value
, typename _Alloc
,
1058 typename _ExtractKey
, typename _Equal
,
1059 typename _H1
, typename _H2
, typename _Hash
,
1060 typename _RehashPolicy
, typename _Traits
>
1061 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1062 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1065 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1066 _Equal
, _H1
, _H2
, _Hash
,
1067 _RehashPolicy
, _Traits
>;
1070 max_load_factor() const noexcept
1072 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1073 return __this
->__rehash_policy().max_load_factor();
1077 max_load_factor(float __z
)
1079 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1080 __this
->__rehash_policy(_RehashPolicy(__z
));
1084 reserve(std::size_t __n
)
1086 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1087 __this
->rehash(__builtin_ceil(__n
/ max_load_factor()));
1092 * Primary class template _Hashtable_ebo_helper.
1094 * Helper class using EBO when it is not forbidden (the type is not
1095 * final) and when it is worth it (the type is empty.)
1097 template<int _Nm
, typename _Tp
,
1098 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
1099 struct _Hashtable_ebo_helper
;
1101 /// Specialization using EBO.
1102 template<int _Nm
, typename _Tp
>
1103 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, true>
1106 _Hashtable_ebo_helper() = default;
1108 template<typename _OtherTp
>
1109 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1110 : _Tp(std::forward
<_OtherTp
>(__tp
))
1114 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
1115 { return static_cast<const _Tp
&>(__eboh
); }
1118 _S_get(_Hashtable_ebo_helper
& __eboh
)
1119 { return static_cast<_Tp
&>(__eboh
); }
1122 /// Specialization not using EBO.
1123 template<int _Nm
, typename _Tp
>
1124 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, false>
1126 _Hashtable_ebo_helper() = default;
1128 template<typename _OtherTp
>
1129 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1130 : _M_tp(std::forward
<_OtherTp
>(__tp
))
1134 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
1135 { return __eboh
._M_tp
; }
1138 _S_get(_Hashtable_ebo_helper
& __eboh
)
1139 { return __eboh
._M_tp
; }
1146 * Primary class template _Local_iterator_base.
1148 * Base class for local iterators, used to iterate within a bucket
1149 * but not between buckets.
1151 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1152 typename _H1
, typename _H2
, typename _Hash
,
1153 bool __cache_hash_code
>
1154 struct _Local_iterator_base
;
1157 * Primary class template _Hash_code_base.
1159 * Encapsulates two policy issues that aren't quite orthogonal.
1160 * (1) the difference between using a ranged hash function and using
1161 * the combination of a hash function and a range-hashing function.
1162 * In the former case we don't have such things as hash codes, so
1163 * we have a dummy type as placeholder.
1164 * (2) Whether or not we cache hash codes. Caching hash codes is
1165 * meaningless if we have a ranged hash function.
1167 * We also put the key extraction objects here, for convenience.
1168 * Each specialization derives from one or more of the template
1169 * parameters to benefit from Ebo. This is important as this type
1170 * is inherited in some cases by the _Local_iterator_base type used
1171 * to implement local_iterator and const_local_iterator. As with
1172 * any iterator type we prefer to make it as small as possible.
1174 * Primary template is unused except as a hook for specializations.
1176 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1177 typename _H1
, typename _H2
, typename _Hash
,
1178 bool __cache_hash_code
>
1179 struct _Hash_code_base
;
1181 /// Specialization: ranged hash function, no caching hash codes. H1
1182 /// and H2 are provided but ignored. We define a dummy hash code type.
1183 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1184 typename _H1
, typename _H2
, typename _Hash
>
1185 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, false>
1186 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1187 private _Hashtable_ebo_helper
<1, _Hash
>
1190 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1191 using __ebo_hash
= _Hashtable_ebo_helper
<1, _Hash
>;
1194 typedef void* __hash_code
;
1195 typedef _Hash_node
<_Value
, false> __node_type
;
1197 // We need the default constructor for the local iterators and _Hashtable
1198 // default constructor.
1199 _Hash_code_base() = default;
1201 _Hash_code_base(const _ExtractKey
& __ex
, const _H1
&, const _H2
&,
1203 : __ebo_extract_key(__ex
), __ebo_hash(__h
) { }
1206 _M_hash_code(const _Key
& __key
) const
1210 _M_bucket_index(const _Key
& __k
, __hash_code
, std::size_t __n
) const
1211 { return _M_ranged_hash()(__k
, __n
); }
1214 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1215 noexcept( noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>(),
1217 { return _M_ranged_hash()(_M_extract()(__p
->_M_v()), __n
); }
1220 _M_store_code(__node_type
*, __hash_code
) const
1224 _M_copy_code(__node_type
*, const __node_type
*) const
1228 _M_swap(_Hash_code_base
& __x
)
1230 std::swap(_M_extract(), __x
._M_extract());
1231 std::swap(_M_ranged_hash(), __x
._M_ranged_hash());
1235 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1238 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1241 _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
1244 _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
1247 // No specialization for ranged hash function while caching hash codes.
1248 // That combination is meaningless, and trying to do it is an error.
1250 /// Specialization: ranged hash function, cache hash codes. This
1251 /// combination is meaningless, so we provide only a declaration
1252 /// and no definition.
1253 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1254 typename _H1
, typename _H2
, typename _Hash
>
1255 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, true>;
1257 /// Specialization: hash function and range-hashing function, no
1258 /// caching of hash codes.
1259 /// Provides typedef and accessor required by C++ 11.
1260 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1261 typename _H1
, typename _H2
>
1262 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1263 _Default_ranged_hash
, false>
1264 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1265 private _Hashtable_ebo_helper
<1, _H1
>,
1266 private _Hashtable_ebo_helper
<2, _H2
>
1269 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1270 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1271 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1273 // Gives the local iterator implementation access to _M_bucket_index().
1274 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1275 _Default_ranged_hash
, false>;
1281 hash_function() const
1285 typedef std::size_t __hash_code
;
1286 typedef _Hash_node
<_Value
, false> __node_type
;
1288 // We need the default constructor for the local iterators and _Hashtable
1289 // default constructor.
1290 _Hash_code_base() = default;
1292 _Hash_code_base(const _ExtractKey
& __ex
,
1293 const _H1
& __h1
, const _H2
& __h2
,
1294 const _Default_ranged_hash
&)
1295 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1298 _M_hash_code(const _Key
& __k
) const
1299 { return _M_h1()(__k
); }
1302 _M_bucket_index(const _Key
&, __hash_code __c
, std::size_t __n
) const
1303 { return _M_h2()(__c
, __n
); }
1306 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1307 noexcept( noexcept(declval
<const _H1
&>()(declval
<const _Key
&>()))
1308 && noexcept(declval
<const _H2
&>()((__hash_code
)0,
1310 { return _M_h2()(_M_h1()(_M_extract()(__p
->_M_v())), __n
); }
1313 _M_store_code(__node_type
*, __hash_code
) const
1317 _M_copy_code(__node_type
*, const __node_type
*) const
1321 _M_swap(_Hash_code_base
& __x
)
1323 std::swap(_M_extract(), __x
._M_extract());
1324 std::swap(_M_h1(), __x
._M_h1());
1325 std::swap(_M_h2(), __x
._M_h2());
1329 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1332 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1335 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1338 _M_h1() { return __ebo_h1::_S_get(*this); }
1341 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1344 _M_h2() { return __ebo_h2::_S_get(*this); }
1347 /// Specialization: hash function and range-hashing function,
1348 /// caching hash codes. H is provided but ignored. Provides
1349 /// typedef and accessor required by C++ 11.
1350 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1351 typename _H1
, typename _H2
>
1352 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1353 _Default_ranged_hash
, true>
1354 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1355 private _Hashtable_ebo_helper
<1, _H1
>,
1356 private _Hashtable_ebo_helper
<2, _H2
>
1359 // Gives the local iterator implementation access to _M_h2().
1360 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1361 _Default_ranged_hash
, true>;
1363 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1364 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1365 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1371 hash_function() const
1375 typedef std::size_t __hash_code
;
1376 typedef _Hash_node
<_Value
, true> __node_type
;
1378 // We need the default constructor for _Hashtable default constructor.
1379 _Hash_code_base() = default;
1380 _Hash_code_base(const _ExtractKey
& __ex
,
1381 const _H1
& __h1
, const _H2
& __h2
,
1382 const _Default_ranged_hash
&)
1383 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1386 _M_hash_code(const _Key
& __k
) const
1387 { return _M_h1()(__k
); }
1390 _M_bucket_index(const _Key
&, __hash_code __c
,
1391 std::size_t __n
) const
1392 { return _M_h2()(__c
, __n
); }
1395 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1396 noexcept( noexcept(declval
<const _H2
&>()((__hash_code
)0,
1398 { return _M_h2()(__p
->_M_hash_code
, __n
); }
1401 _M_store_code(__node_type
* __n
, __hash_code __c
) const
1402 { __n
->_M_hash_code
= __c
; }
1405 _M_copy_code(__node_type
* __to
, const __node_type
* __from
) const
1406 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
1409 _M_swap(_Hash_code_base
& __x
)
1411 std::swap(_M_extract(), __x
._M_extract());
1412 std::swap(_M_h1(), __x
._M_h1());
1413 std::swap(_M_h2(), __x
._M_h2());
1417 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1420 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1423 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1426 _M_h1() { return __ebo_h1::_S_get(*this); }
1429 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1432 _M_h2() { return __ebo_h2::_S_get(*this); }
1436 * Primary class template _Equal_helper.
1439 template <typename _Key
, typename _Value
, typename _ExtractKey
,
1440 typename _Equal
, typename _HashCodeType
,
1441 bool __cache_hash_code
>
1442 struct _Equal_helper
;
1445 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1446 typename _Equal
, typename _HashCodeType
>
1447 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, true>
1450 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1451 const _Key
& __k
, _HashCodeType __c
, _Hash_node
<_Value
, true>* __n
)
1452 { return __c
== __n
->_M_hash_code
&& __eq(__k
, __extract(__n
->_M_v())); }
1456 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1457 typename _Equal
, typename _HashCodeType
>
1458 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, false>
1461 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1462 const _Key
& __k
, _HashCodeType
, _Hash_node
<_Value
, false>* __n
)
1463 { return __eq(__k
, __extract(__n
->_M_v())); }
1467 /// Partial specialization used when nodes contain a cached hash code.
1468 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1469 typename _H1
, typename _H2
, typename _Hash
>
1470 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1471 _H1
, _H2
, _Hash
, true>
1472 : private _Hashtable_ebo_helper
<0, _H2
>
1475 using __base_type
= _Hashtable_ebo_helper
<0, _H2
>;
1476 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1477 _H1
, _H2
, _Hash
, true>;
1479 _Local_iterator_base() = default;
1480 _Local_iterator_base(const __hash_code_base
& __base
,
1481 _Hash_node
<_Value
, true>* __p
,
1482 std::size_t __bkt
, std::size_t __bkt_count
)
1483 : __base_type(__base
._M_h2()),
1484 _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1489 _M_cur
= _M_cur
->_M_next();
1493 = __base_type::_S_get(*this)(_M_cur
->_M_hash_code
,
1495 if (__bkt
!= _M_bucket
)
1500 _Hash_node
<_Value
, true>* _M_cur
;
1501 std::size_t _M_bucket
;
1502 std::size_t _M_bucket_count
;
1506 _M_curr() const { return _M_cur
; } // for equality ops
1509 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1512 // Uninitialized storage for a _Hash_code_base.
1513 // This type is DefaultConstructible and Assignable even if the
1514 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1515 // can be DefaultConstructible and Assignable.
1516 template<typename _Tp
, bool _IsEmpty
= std::is_empty
<_Tp
>::value
>
1517 struct _Hash_code_storage
1519 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
1522 _M_h() { return _M_storage
._M_ptr(); }
1525 _M_h() const { return _M_storage
._M_ptr(); }
1528 // Empty partial specialization for empty _Hash_code_base types.
1529 template<typename _Tp
>
1530 struct _Hash_code_storage
<_Tp
, true>
1532 static_assert( std::is_empty
<_Tp
>::value
, "Type must be empty" );
1534 // As _Tp is an empty type there will be no bytes written/read through
1535 // the cast pointer, so no strict-aliasing violation.
1537 _M_h() { return reinterpret_cast<_Tp
*>(this); }
1540 _M_h() const { return reinterpret_cast<const _Tp
*>(this); }
1543 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1544 typename _H1
, typename _H2
, typename _Hash
>
1545 using __hash_code_for_local_iter
1546 = _Hash_code_storage
<_Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1547 _H1
, _H2
, _Hash
, false>>;
1549 // Partial specialization used when hash codes are not cached
1550 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1551 typename _H1
, typename _H2
, typename _Hash
>
1552 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1553 _H1
, _H2
, _Hash
, false>
1554 : __hash_code_for_local_iter
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
>
1557 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1558 _H1
, _H2
, _Hash
, false>;
1560 _Local_iterator_base() : _M_bucket_count(-1) { }
1562 _Local_iterator_base(const __hash_code_base
& __base
,
1563 _Hash_node
<_Value
, false>* __p
,
1564 std::size_t __bkt
, std::size_t __bkt_count
)
1565 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
)
1566 { _M_init(__base
); }
1568 ~_Local_iterator_base()
1570 if (_M_bucket_count
!= -1)
1574 _Local_iterator_base(const _Local_iterator_base
& __iter
)
1575 : _M_cur(__iter
._M_cur
), _M_bucket(__iter
._M_bucket
),
1576 _M_bucket_count(__iter
._M_bucket_count
)
1578 if (_M_bucket_count
!= -1)
1579 _M_init(*__iter
._M_h());
1582 _Local_iterator_base
&
1583 operator=(const _Local_iterator_base
& __iter
)
1585 if (_M_bucket_count
!= -1)
1587 _M_cur
= __iter
._M_cur
;
1588 _M_bucket
= __iter
._M_bucket
;
1589 _M_bucket_count
= __iter
._M_bucket_count
;
1590 if (_M_bucket_count
!= -1)
1591 _M_init(*__iter
._M_h());
1598 _M_cur
= _M_cur
->_M_next();
1601 std::size_t __bkt
= this->_M_h()->_M_bucket_index(_M_cur
,
1603 if (__bkt
!= _M_bucket
)
1608 _Hash_node
<_Value
, false>* _M_cur
;
1609 std::size_t _M_bucket
;
1610 std::size_t _M_bucket_count
;
1613 _M_init(const __hash_code_base
& __base
)
1614 { ::new(this->_M_h()) __hash_code_base(__base
); }
1617 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1621 _M_curr() const { return _M_cur
; } // for equality ops and debug mode
1624 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1627 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1628 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1630 operator==(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1631 _H1
, _H2
, _Hash
, __cache
>& __x
,
1632 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1633 _H1
, _H2
, _Hash
, __cache
>& __y
)
1634 { return __x
._M_curr() == __y
._M_curr(); }
1636 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1637 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1639 operator!=(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1640 _H1
, _H2
, _Hash
, __cache
>& __x
,
1641 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1642 _H1
, _H2
, _Hash
, __cache
>& __y
)
1643 { return __x
._M_curr() != __y
._M_curr(); }
1646 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1647 typename _H1
, typename _H2
, typename _Hash
,
1648 bool __constant_iterators
, bool __cache
>
1649 struct _Local_iterator
1650 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1651 _H1
, _H2
, _Hash
, __cache
>
1654 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1655 _H1
, _H2
, _Hash
, __cache
>;
1656 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1658 typedef _Value value_type
;
1659 typedef typename
std::conditional
<__constant_iterators
,
1660 const _Value
*, _Value
*>::type
1662 typedef typename
std::conditional
<__constant_iterators
,
1663 const _Value
&, _Value
&>::type
1665 typedef std::ptrdiff_t difference_type
;
1666 typedef std::forward_iterator_tag iterator_category
;
1668 _Local_iterator() = default;
1670 _Local_iterator(const __hash_code_base
& __base
,
1671 _Hash_node
<_Value
, __cache
>* __p
,
1672 std::size_t __bkt
, std::size_t __bkt_count
)
1673 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1678 { return this->_M_cur
->_M_v(); }
1682 { return this->_M_cur
->_M_valptr(); }
1694 _Local_iterator
__tmp(*this);
1700 /// local const_iterators
1701 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1702 typename _H1
, typename _H2
, typename _Hash
,
1703 bool __constant_iterators
, bool __cache
>
1704 struct _Local_const_iterator
1705 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1706 _H1
, _H2
, _Hash
, __cache
>
1709 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1710 _H1
, _H2
, _Hash
, __cache
>;
1711 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1714 typedef _Value value_type
;
1715 typedef const _Value
* pointer
;
1716 typedef const _Value
& reference
;
1717 typedef std::ptrdiff_t difference_type
;
1718 typedef std::forward_iterator_tag iterator_category
;
1720 _Local_const_iterator() = default;
1722 _Local_const_iterator(const __hash_code_base
& __base
,
1723 _Hash_node
<_Value
, __cache
>* __p
,
1724 std::size_t __bkt
, std::size_t __bkt_count
)
1725 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1728 _Local_const_iterator(const _Local_iterator
<_Key
, _Value
, _ExtractKey
,
1730 __constant_iterators
,
1737 { return this->_M_cur
->_M_v(); }
1741 { return this->_M_cur
->_M_valptr(); }
1743 _Local_const_iterator
&
1750 _Local_const_iterator
1753 _Local_const_iterator
__tmp(*this);
1760 * Primary class template _Hashtable_base.
1762 * Helper class adding management of _Equal functor to
1763 * _Hash_code_base type.
1765 * Base class templates are:
1766 * - __detail::_Hash_code_base
1767 * - __detail::_Hashtable_ebo_helper
1769 template<typename _Key
, typename _Value
,
1770 typename _ExtractKey
, typename _Equal
,
1771 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
1772 struct _Hashtable_base
1773 : public _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1774 _Traits::__hash_cached::value
>,
1775 private _Hashtable_ebo_helper
<0, _Equal
>
1778 typedef _Key key_type
;
1779 typedef _Value value_type
;
1780 typedef _Equal key_equal
;
1781 typedef std::size_t size_type
;
1782 typedef std::ptrdiff_t difference_type
;
1784 using __traits_type
= _Traits
;
1785 using __hash_cached
= typename
__traits_type::__hash_cached
;
1786 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
1787 using __unique_keys
= typename
__traits_type::__unique_keys
;
1789 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1791 __hash_cached::value
>;
1793 using __hash_code
= typename
__hash_code_base::__hash_code
;
1794 using __node_type
= typename
__hash_code_base::__node_type
;
1796 using iterator
= __detail::_Node_iterator
<value_type
,
1797 __constant_iterators::value
,
1798 __hash_cached::value
>;
1800 using const_iterator
= __detail::_Node_const_iterator
<value_type
,
1801 __constant_iterators::value
,
1802 __hash_cached::value
>;
1804 using local_iterator
= __detail::_Local_iterator
<key_type
, value_type
,
1805 _ExtractKey
, _H1
, _H2
, _Hash
,
1806 __constant_iterators::value
,
1807 __hash_cached::value
>;
1809 using const_local_iterator
= __detail::_Local_const_iterator
<key_type
,
1811 _ExtractKey
, _H1
, _H2
, _Hash
,
1812 __constant_iterators::value
,
1813 __hash_cached::value
>;
1815 using __ireturn_type
= typename
std::conditional
<__unique_keys::value
,
1816 std::pair
<iterator
, bool>,
1819 using _EqualEBO
= _Hashtable_ebo_helper
<0, _Equal
>;
1820 using _EqualHelper
= _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
,
1821 __hash_code
, __hash_cached::value
>;
1824 _Hashtable_base() = default;
1825 _Hashtable_base(const _ExtractKey
& __ex
, const _H1
& __h1
, const _H2
& __h2
,
1826 const _Hash
& __hash
, const _Equal
& __eq
)
1827 : __hash_code_base(__ex
, __h1
, __h2
, __hash
), _EqualEBO(__eq
)
1831 _M_equals(const _Key
& __k
, __hash_code __c
, __node_type
* __n
) const
1833 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
1838 _M_swap(_Hashtable_base
& __x
)
1840 __hash_code_base::_M_swap(__x
);
1841 std::swap(_M_eq(), __x
._M_eq());
1845 _M_eq() const { return _EqualEBO::_S_cget(*this); }
1848 _M_eq() { return _EqualEBO::_S_get(*this); }
1852 * struct _Equality_base.
1854 * Common types and functions for class _Equality.
1856 struct _Equality_base
1859 template<typename _Uiterator
>
1861 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
1864 // See std::is_permutation in N3068.
1865 template<typename _Uiterator
>
1868 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
1869 _Uiterator __first2
)
1871 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1872 if (!(*__first1
== *__first2
))
1875 if (__first1
== __last1
)
1878 _Uiterator __last2
= __first2
;
1879 std::advance(__last2
, std::distance(__first1
, __last1
));
1881 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
1883 _Uiterator __tmp
= __first1
;
1884 while (__tmp
!= __it1
&& !bool(*__tmp
== *__it1
))
1887 // We've seen this one before.
1891 std::ptrdiff_t __n2
= 0;
1892 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
1893 if (*__tmp
== *__it1
)
1899 std::ptrdiff_t __n1
= 0;
1900 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
1901 if (*__tmp
== *__it1
)
1911 * Primary class template _Equality.
1913 * This is for implementing equality comparison for unordered
1914 * containers, per N3068, by John Lakos and Pablo Halpern.
1915 * Algorithmically, we follow closely the reference implementations
1918 template<typename _Key
, typename _Value
, typename _Alloc
,
1919 typename _ExtractKey
, typename _Equal
,
1920 typename _H1
, typename _H2
, typename _Hash
,
1921 typename _RehashPolicy
, typename _Traits
,
1922 bool _Unique_keys
= _Traits::__unique_keys::value
>
1926 template<typename _Key
, typename _Value
, typename _Alloc
,
1927 typename _ExtractKey
, typename _Equal
,
1928 typename _H1
, typename _H2
, typename _Hash
,
1929 typename _RehashPolicy
, typename _Traits
>
1930 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1931 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
1933 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1934 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1937 _M_equal(const __hashtable
&) const;
1940 template<typename _Key
, typename _Value
, typename _Alloc
,
1941 typename _ExtractKey
, typename _Equal
,
1942 typename _H1
, typename _H2
, typename _Hash
,
1943 typename _RehashPolicy
, typename _Traits
>
1945 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1946 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
1947 _M_equal(const __hashtable
& __other
) const
1949 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1951 if (__this
->size() != __other
.size())
1954 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
1956 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
1957 if (__ity
== __other
.end() || !bool(*__ity
== *__itx
))
1964 template<typename _Key
, typename _Value
, typename _Alloc
,
1965 typename _ExtractKey
, typename _Equal
,
1966 typename _H1
, typename _H2
, typename _Hash
,
1967 typename _RehashPolicy
, typename _Traits
>
1968 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1969 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
1970 : public _Equality_base
1972 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1973 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1976 _M_equal(const __hashtable
&) const;
1979 template<typename _Key
, typename _Value
, typename _Alloc
,
1980 typename _ExtractKey
, typename _Equal
,
1981 typename _H1
, typename _H2
, typename _Hash
,
1982 typename _RehashPolicy
, typename _Traits
>
1984 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1985 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>::
1986 _M_equal(const __hashtable
& __other
) const
1988 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1990 if (__this
->size() != __other
.size())
1993 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
1995 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
1996 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
1998 if (std::distance(__xrange
.first
, __xrange
.second
)
1999 != std::distance(__yrange
.first
, __yrange
.second
))
2002 if (!_S_is_permutation(__xrange
.first
, __xrange
.second
,
2006 __itx
= __xrange
.second
;
2012 * This type deals with all allocation and keeps an allocator instance through
2013 * inheritance to benefit from EBO when possible.
2015 template<typename _NodeAlloc
>
2016 struct _Hashtable_alloc
: private _Hashtable_ebo_helper
<0, _NodeAlloc
>
2019 using __ebo_node_alloc
= _Hashtable_ebo_helper
<0, _NodeAlloc
>;
2021 using __node_type
= typename
_NodeAlloc::value_type
;
2022 using __node_alloc_type
= _NodeAlloc
;
2023 // Use __gnu_cxx to benefit from _S_always_equal and al.
2024 using __node_alloc_traits
= __gnu_cxx::__alloc_traits
<__node_alloc_type
>;
2026 using __value_alloc_traits
= typename
__node_alloc_traits::template
2027 rebind_traits
<typename
__node_type::value_type
>;
2029 using __node_base
= __detail::_Hash_node_base
;
2030 using __bucket_type
= __node_base
*;
2031 using __bucket_alloc_type
=
2032 __alloc_rebind
<__node_alloc_type
, __bucket_type
>;
2033 using __bucket_alloc_traits
= std::allocator_traits
<__bucket_alloc_type
>;
2035 _Hashtable_alloc() = default;
2036 _Hashtable_alloc(const _Hashtable_alloc
&) = default;
2037 _Hashtable_alloc(_Hashtable_alloc
&&) = default;
2039 template<typename _Alloc
>
2040 _Hashtable_alloc(_Alloc
&& __a
)
2041 : __ebo_node_alloc(std::forward
<_Alloc
>(__a
))
2046 { return __ebo_node_alloc::_S_get(*this); }
2048 const __node_alloc_type
&
2049 _M_node_allocator() const
2050 { return __ebo_node_alloc::_S_cget(*this); }
2052 template<typename
... _Args
>
2054 _M_allocate_node(_Args
&&... __args
);
2057 _M_deallocate_node(__node_type
* __n
);
2059 // Deallocate the linked list of nodes pointed to by __n
2061 _M_deallocate_nodes(__node_type
* __n
);
2064 _M_allocate_buckets(std::size_t __n
);
2067 _M_deallocate_buckets(__bucket_type
*, std::size_t __n
);
2070 // Definitions of class template _Hashtable_alloc's out-of-line member
2072 template<typename _NodeAlloc
>
2073 template<typename
... _Args
>
2074 typename _Hashtable_alloc
<_NodeAlloc
>::__node_type
*
2075 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_node(_Args
&&... __args
)
2077 auto __nptr
= __node_alloc_traits::allocate(_M_node_allocator(), 1);
2078 __node_type
* __n
= std::__to_address(__nptr
);
2081 ::new ((void*)__n
) __node_type
;
2082 __node_alloc_traits::construct(_M_node_allocator(),
2084 std::forward
<_Args
>(__args
)...);
2089 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr
, 1);
2090 __throw_exception_again
;
2094 template<typename _NodeAlloc
>
2096 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_node(__node_type
* __n
)
2098 typedef typename
__node_alloc_traits::pointer _Ptr
;
2099 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__n
);
2100 __node_alloc_traits::destroy(_M_node_allocator(), __n
->_M_valptr());
2101 __n
->~__node_type();
2102 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr
, 1);
2105 template<typename _NodeAlloc
>
2107 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_nodes(__node_type
* __n
)
2111 __node_type
* __tmp
= __n
;
2112 __n
= __n
->_M_next();
2113 _M_deallocate_node(__tmp
);
2117 template<typename _NodeAlloc
>
2118 typename _Hashtable_alloc
<_NodeAlloc
>::__bucket_type
*
2119 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_buckets(std::size_t __n
)
2121 __bucket_alloc_type
__alloc(_M_node_allocator());
2123 auto __ptr
= __bucket_alloc_traits::allocate(__alloc
, __n
);
2124 __bucket_type
* __p
= std::__to_address(__ptr
);
2125 __builtin_memset(__p
, 0, __n
* sizeof(__bucket_type
));
2129 template<typename _NodeAlloc
>
2131 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_buckets(__bucket_type
* __bkts
,
2134 typedef typename
__bucket_alloc_traits::pointer _Ptr
;
2135 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__bkts
);
2136 __bucket_alloc_type
__alloc(_M_node_allocator());
2137 __bucket_alloc_traits::deallocate(__alloc
, __ptr
, __n
);
2140 //@} hashtable-detail
2141 } // namespace __detail
2142 _GLIBCXX_END_NAMESPACE_VERSION
2145 #endif // _HASHTABLE_POLICY_H