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 <limits> // for std::numeric_limits
36 #include <bits/stl_algobase.h> // for std::min.
38 namespace std
_GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 template<typename _Key
, typename _Value
, typename _Alloc
,
43 typename _ExtractKey
, typename _Equal
,
44 typename _H1
, typename _H2
, typename _Hash
,
45 typename _RehashPolicy
, typename _Traits
>
51 * @defgroup hashtable-detail Base and Implementation Classes
52 * @ingroup unordered_associative_containers
55 template<typename _Key
, typename _Value
,
56 typename _ExtractKey
, typename _Equal
,
57 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
58 struct _Hashtable_base
;
60 // Helper function: return distance(first, last) for forward
61 // iterators, or 0/1 for input iterators.
62 template<class _Iterator
>
63 inline typename
std::iterator_traits
<_Iterator
>::difference_type
64 __distance_fw(_Iterator __first
, _Iterator __last
,
65 std::input_iterator_tag
)
66 { return __first
!= __last
? 1 : 0; }
68 template<class _Iterator
>
69 inline typename
std::iterator_traits
<_Iterator
>::difference_type
70 __distance_fw(_Iterator __first
, _Iterator __last
,
71 std::forward_iterator_tag
)
72 { return std::distance(__first
, __last
); }
74 template<class _Iterator
>
75 inline typename
std::iterator_traits
<_Iterator
>::difference_type
76 __distance_fw(_Iterator __first
, _Iterator __last
)
77 { return __distance_fw(__first
, __last
,
78 std::__iterator_category(__first
)); }
82 template<typename _Tp
>
84 operator()(_Tp
&& __x
) const
85 { return std::forward
<_Tp
>(__x
); }
90 template<typename _Tp
>
92 operator()(_Tp
&& __x
) const
93 -> decltype(std::get
<0>(std::forward
<_Tp
>(__x
)))
94 { return std::get
<0>(std::forward
<_Tp
>(__x
)); }
97 template<typename _NodeAlloc
>
98 struct _Hashtable_alloc
;
100 // Functor recycling a pool of nodes and using allocation once the pool is
102 template<typename _NodeAlloc
>
103 struct _ReuseOrAllocNode
106 using __node_alloc_type
= _NodeAlloc
;
107 using __hashtable_alloc
= _Hashtable_alloc
<__node_alloc_type
>;
108 using __node_alloc_traits
=
109 typename
__hashtable_alloc::__node_alloc_traits
;
110 using __node_type
= typename
__hashtable_alloc::__node_type
;
113 _ReuseOrAllocNode(__node_type
* __nodes
, __hashtable_alloc
& __h
)
114 : _M_nodes(__nodes
), _M_h(__h
) { }
115 _ReuseOrAllocNode(const _ReuseOrAllocNode
&) = delete;
118 { _M_h
._M_deallocate_nodes(_M_nodes
); }
120 template<typename _Arg
>
122 operator()(_Arg
&& __arg
) const
126 __node_type
* __node
= _M_nodes
;
127 _M_nodes
= _M_nodes
->_M_next();
128 __node
->_M_nxt
= nullptr;
129 auto& __a
= _M_h
._M_node_allocator();
130 __node_alloc_traits::destroy(__a
, __node
->_M_valptr());
133 __node_alloc_traits::construct(__a
, __node
->_M_valptr(),
134 std::forward
<_Arg
>(__arg
));
138 __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 not less than __n
509 __clp2(std::size_t __n
) noexcept
511 // Equivalent to return __n ? std::ceil2(__n) : 0;
514 const unsigned __lz
= sizeof(size_t) > sizeof(long)
515 ? __builtin_clzll(__n
- 1ull)
516 : __builtin_clzl(__n
- 1ul);
517 // Doing two shifts avoids undefined behaviour when __lz == 0.
518 return (size_t(1) << (numeric_limits
<size_t>::digits
- __lz
- 1)) << 1;
521 /// Rehash policy providing power of 2 bucket numbers. Avoids modulo
523 struct _Power2_rehash_policy
525 using __has_load_factor
= std::true_type
;
527 _Power2_rehash_policy(float __z
= 1.0) noexcept
528 : _M_max_load_factor(__z
), _M_next_resize(0) { }
531 max_load_factor() const noexcept
532 { return _M_max_load_factor
; }
534 // Return a bucket size no smaller than n (as long as n is not above the
535 // highest power of 2).
537 _M_next_bkt(std::size_t __n
) noexcept
539 const auto __max_width
= std::min
<size_t>(sizeof(size_t), 8);
540 const auto __max_bkt
= size_t(1) << (__max_width
* __CHAR_BIT__
- 1);
541 std::size_t __res
= __clp2(__n
);
549 if (__res
== __max_bkt
)
550 // Set next resize to the max value so that we never try to rehash again
551 // as we already reach the biggest possible bucket number.
552 // Note that it might result in max_load_factor not being respected.
553 _M_next_resize
= std::size_t(-1);
556 = __builtin_ceil(__res
* (long double)_M_max_load_factor
);
561 // Return a bucket count appropriate for n elements
563 _M_bkt_for_elements(std::size_t __n
) const noexcept
564 { return __builtin_ceil(__n
/ (long double)_M_max_load_factor
); }
566 // __n_bkt is current bucket count, __n_elt is current element count,
567 // and __n_ins is number of elements to be inserted. Do we need to
568 // increase bucket count? If so, return make_pair(true, n), where n
569 // is the new bucket count. If not, return make_pair(false, 0).
570 std::pair
<bool, std::size_t>
571 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
572 std::size_t __n_ins
) noexcept
574 if (__n_elt
+ __n_ins
>= _M_next_resize
)
576 long double __min_bkts
= (__n_elt
+ __n_ins
)
577 / (long double)_M_max_load_factor
;
578 if (__min_bkts
>= __n_bkt
)
579 return std::make_pair(true,
580 _M_next_bkt(std::max
<std::size_t>(__builtin_floor(__min_bkts
) + 1,
581 __n_bkt
* _S_growth_factor
)));
584 = __builtin_floor(__n_bkt
* (long double)_M_max_load_factor
);
585 return std::make_pair(false, 0);
588 return std::make_pair(false, 0);
591 typedef std::size_t _State
;
594 _M_state() const noexcept
595 { return _M_next_resize
; }
599 { _M_next_resize
= 0; }
602 _M_reset(_State __state
) noexcept
603 { _M_next_resize
= __state
; }
605 static const std::size_t _S_growth_factor
= 2;
607 float _M_max_load_factor
;
608 std::size_t _M_next_resize
;
611 // Base classes for std::_Hashtable. We define these base classes
612 // because in some cases we want to do different things depending on
613 // the value of a policy class. In some cases the policy class
614 // affects which member functions and nested typedefs are defined;
615 // we handle that by specializing base class templates. Several of
616 // the base class templates need to access other members of class
617 // template _Hashtable, so we use a variant of the "Curiously
618 // Recurring Template Pattern" (CRTP) technique.
621 * Primary class template _Map_base.
623 * If the hashtable has a value type of the form pair<T1, T2> and a
624 * key extraction policy (_ExtractKey) that returns the first part
625 * of the pair, the hashtable gets a mapped_type typedef. If it
626 * satisfies those criteria and also has unique keys, then it also
627 * gets an operator[].
629 template<typename _Key
, typename _Value
, typename _Alloc
,
630 typename _ExtractKey
, typename _Equal
,
631 typename _H1
, typename _H2
, typename _Hash
,
632 typename _RehashPolicy
, typename _Traits
,
633 bool _Unique_keys
= _Traits::__unique_keys::value
>
634 struct _Map_base
{ };
636 /// Partial specialization, __unique_keys set to false.
637 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
638 typename _H1
, typename _H2
, typename _Hash
,
639 typename _RehashPolicy
, typename _Traits
>
640 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
641 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
643 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
646 /// Partial specialization, __unique_keys set to true.
647 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
648 typename _H1
, typename _H2
, typename _Hash
,
649 typename _RehashPolicy
, typename _Traits
>
650 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
651 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
654 using __hashtable_base
= __detail::_Hashtable_base
<_Key
, _Pair
,
656 _Equal
, _H1
, _H2
, _Hash
,
659 using __hashtable
= _Hashtable
<_Key
, _Pair
, _Alloc
,
661 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
663 using __hash_code
= typename
__hashtable_base::__hash_code
;
664 using __node_type
= typename
__hashtable_base::__node_type
;
667 using key_type
= typename
__hashtable_base::key_type
;
668 using iterator
= typename
__hashtable_base::iterator
;
669 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
672 operator[](const key_type
& __k
);
675 operator[](key_type
&& __k
);
677 // _GLIBCXX_RESOLVE_LIB_DEFECTS
678 // DR 761. unordered_map needs an at() member function.
680 at(const key_type
& __k
);
683 at(const key_type
& __k
) const;
686 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
687 typename _H1
, typename _H2
, typename _Hash
,
688 typename _RehashPolicy
, typename _Traits
>
690 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
691 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
692 operator[](const key_type
& __k
)
695 __hashtable
* __h
= static_cast<__hashtable
*>(this);
696 __hash_code __code
= __h
->_M_hash_code(__k
);
697 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
698 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
702 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
703 std::tuple
<const key_type
&>(__k
),
705 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
708 return __p
->_M_v().second
;
711 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
712 typename _H1
, typename _H2
, typename _Hash
,
713 typename _RehashPolicy
, typename _Traits
>
715 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
716 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
717 operator[](key_type
&& __k
)
720 __hashtable
* __h
= static_cast<__hashtable
*>(this);
721 __hash_code __code
= __h
->_M_hash_code(__k
);
722 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
723 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
727 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
728 std::forward_as_tuple(std::move(__k
)),
730 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
733 return __p
->_M_v().second
;
736 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
737 typename _H1
, typename _H2
, typename _Hash
,
738 typename _RehashPolicy
, typename _Traits
>
740 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
741 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
742 at(const key_type
& __k
)
745 __hashtable
* __h
= static_cast<__hashtable
*>(this);
746 __hash_code __code
= __h
->_M_hash_code(__k
);
747 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
748 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
751 __throw_out_of_range(__N("_Map_base::at"));
752 return __p
->_M_v().second
;
755 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
756 typename _H1
, typename _H2
, typename _Hash
,
757 typename _RehashPolicy
, typename _Traits
>
759 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
760 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
761 at(const key_type
& __k
) const
762 -> const mapped_type
&
764 const __hashtable
* __h
= static_cast<const __hashtable
*>(this);
765 __hash_code __code
= __h
->_M_hash_code(__k
);
766 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
767 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
770 __throw_out_of_range(__N("_Map_base::at"));
771 return __p
->_M_v().second
;
775 * Primary class template _Insert_base.
777 * Defines @c insert member functions appropriate to all _Hashtables.
779 template<typename _Key
, typename _Value
, typename _Alloc
,
780 typename _ExtractKey
, typename _Equal
,
781 typename _H1
, typename _H2
, typename _Hash
,
782 typename _RehashPolicy
, typename _Traits
>
786 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
787 _Equal
, _H1
, _H2
, _Hash
,
788 _RehashPolicy
, _Traits
>;
790 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
791 _Equal
, _H1
, _H2
, _Hash
,
794 using value_type
= typename
__hashtable_base::value_type
;
795 using iterator
= typename
__hashtable_base::iterator
;
796 using const_iterator
= typename
__hashtable_base::const_iterator
;
797 using size_type
= typename
__hashtable_base::size_type
;
799 using __unique_keys
= typename
__hashtable_base::__unique_keys
;
800 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
801 using __node_type
= _Hash_node
<_Value
, _Traits::__hash_cached::value
>;
802 using __node_alloc_type
= __alloc_rebind
<_Alloc
, __node_type
>;
803 using __node_gen_type
= _AllocNode
<__node_alloc_type
>;
806 _M_conjure_hashtable()
807 { return *(static_cast<__hashtable
*>(this)); }
809 template<typename _InputIterator
, typename _NodeGetter
>
811 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
812 const _NodeGetter
&, true_type
);
814 template<typename _InputIterator
, typename _NodeGetter
>
816 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
817 const _NodeGetter
&, false_type
);
821 insert(const value_type
& __v
)
823 __hashtable
& __h
= _M_conjure_hashtable();
824 __node_gen_type
__node_gen(__h
);
825 return __h
._M_insert(__v
, __node_gen
, __unique_keys());
829 insert(const_iterator __hint
, const value_type
& __v
)
831 __hashtable
& __h
= _M_conjure_hashtable();
832 __node_gen_type
__node_gen(__h
);
833 return __h
._M_insert(__hint
, __v
, __node_gen
, __unique_keys());
837 insert(initializer_list
<value_type
> __l
)
838 { this->insert(__l
.begin(), __l
.end()); }
840 template<typename _InputIterator
>
842 insert(_InputIterator __first
, _InputIterator __last
)
844 __hashtable
& __h
= _M_conjure_hashtable();
845 __node_gen_type
__node_gen(__h
);
846 return _M_insert_range(__first
, __last
, __node_gen
, __unique_keys());
850 template<typename _Key
, typename _Value
, typename _Alloc
,
851 typename _ExtractKey
, typename _Equal
,
852 typename _H1
, typename _H2
, typename _Hash
,
853 typename _RehashPolicy
, typename _Traits
>
854 template<typename _InputIterator
, typename _NodeGetter
>
856 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
857 _RehashPolicy
, _Traits
>::
858 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
859 const _NodeGetter
& __node_gen
, true_type
)
861 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
865 __hashtable
& __h
= _M_conjure_hashtable();
866 for (; __first
!= __last
; ++__first
)
868 if (__h
._M_insert(*__first
, __node_gen
, __unique_keys(),
871 else if (__n_elt
!= 1)
876 template<typename _Key
, typename _Value
, typename _Alloc
,
877 typename _ExtractKey
, typename _Equal
,
878 typename _H1
, typename _H2
, typename _Hash
,
879 typename _RehashPolicy
, typename _Traits
>
880 template<typename _InputIterator
, typename _NodeGetter
>
882 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
883 _RehashPolicy
, _Traits
>::
884 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
885 const _NodeGetter
& __node_gen
, false_type
)
887 using __rehash_type
= typename
__hashtable::__rehash_type
;
888 using __rehash_state
= typename
__hashtable::__rehash_state
;
889 using pair_type
= std::pair
<bool, std::size_t>;
891 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
895 __hashtable
& __h
= _M_conjure_hashtable();
896 __rehash_type
& __rehash
= __h
._M_rehash_policy
;
897 const __rehash_state
& __saved_state
= __rehash
._M_state();
898 pair_type __do_rehash
= __rehash
._M_need_rehash(__h
._M_bucket_count
,
899 __h
._M_element_count
,
902 if (__do_rehash
.first
)
903 __h
._M_rehash(__do_rehash
.second
, __saved_state
);
905 for (; __first
!= __last
; ++__first
)
906 __h
._M_insert(*__first
, __node_gen
, __unique_keys());
910 * Primary class template _Insert.
912 * Defines @c insert member functions that depend on _Hashtable policies,
913 * via partial specializations.
915 template<typename _Key
, typename _Value
, typename _Alloc
,
916 typename _ExtractKey
, typename _Equal
,
917 typename _H1
, typename _H2
, typename _Hash
,
918 typename _RehashPolicy
, typename _Traits
,
919 bool _Constant_iterators
= _Traits::__constant_iterators::value
>
923 template<typename _Key
, typename _Value
, typename _Alloc
,
924 typename _ExtractKey
, typename _Equal
,
925 typename _H1
, typename _H2
, typename _Hash
,
926 typename _RehashPolicy
, typename _Traits
>
927 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
928 _RehashPolicy
, _Traits
, true>
929 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
930 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
932 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
933 _Equal
, _H1
, _H2
, _Hash
,
934 _RehashPolicy
, _Traits
>;
936 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
937 _Equal
, _H1
, _H2
, _Hash
,
940 using value_type
= typename
__base_type::value_type
;
941 using iterator
= typename
__base_type::iterator
;
942 using const_iterator
= typename
__base_type::const_iterator
;
944 using __unique_keys
= typename
__base_type::__unique_keys
;
945 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
946 using __hashtable
= typename
__base_type::__hashtable
;
947 using __node_gen_type
= typename
__base_type::__node_gen_type
;
949 using __base_type::insert
;
952 insert(value_type
&& __v
)
954 __hashtable
& __h
= this->_M_conjure_hashtable();
955 __node_gen_type
__node_gen(__h
);
956 return __h
._M_insert(std::move(__v
), __node_gen
, __unique_keys());
960 insert(const_iterator __hint
, value_type
&& __v
)
962 __hashtable
& __h
= this->_M_conjure_hashtable();
963 __node_gen_type
__node_gen(__h
);
964 return __h
._M_insert(__hint
, std::move(__v
), __node_gen
,
970 template<typename _Key
, typename _Value
, typename _Alloc
,
971 typename _ExtractKey
, typename _Equal
,
972 typename _H1
, typename _H2
, typename _Hash
,
973 typename _RehashPolicy
, typename _Traits
>
974 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
975 _RehashPolicy
, _Traits
, false>
976 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
977 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
979 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
980 _Equal
, _H1
, _H2
, _Hash
,
981 _RehashPolicy
, _Traits
>;
982 using value_type
= typename
__base_type::value_type
;
983 using iterator
= typename
__base_type::iterator
;
984 using const_iterator
= typename
__base_type::const_iterator
;
986 using __unique_keys
= typename
__base_type::__unique_keys
;
987 using __hashtable
= typename
__base_type::__hashtable
;
988 using __ireturn_type
= typename
__base_type::__ireturn_type
;
990 using __base_type::insert
;
992 template<typename _Pair
>
993 using __is_cons
= std::is_constructible
<value_type
, _Pair
&&>;
995 template<typename _Pair
>
996 using _IFcons
= std::enable_if
<__is_cons
<_Pair
>::value
>;
998 template<typename _Pair
>
999 using _IFconsp
= typename _IFcons
<_Pair
>::type
;
1001 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
1005 __hashtable
& __h
= this->_M_conjure_hashtable();
1006 return __h
._M_emplace(__unique_keys(), std::forward
<_Pair
>(__v
));
1009 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
1011 insert(const_iterator __hint
, _Pair
&& __v
)
1013 __hashtable
& __h
= this->_M_conjure_hashtable();
1014 return __h
._M_emplace(__hint
, __unique_keys(),
1015 std::forward
<_Pair
>(__v
));
1019 template<typename _Policy
>
1020 using __has_load_factor
= typename
_Policy::__has_load_factor
;
1023 * Primary class template _Rehash_base.
1025 * Give hashtable the max_load_factor functions and reserve iff the
1026 * rehash policy supports it.
1028 template<typename _Key
, typename _Value
, typename _Alloc
,
1029 typename _ExtractKey
, typename _Equal
,
1030 typename _H1
, typename _H2
, typename _Hash
,
1031 typename _RehashPolicy
, typename _Traits
,
1033 __detected_or_t
<std::false_type
, __has_load_factor
, _RehashPolicy
>>
1034 struct _Rehash_base
;
1036 /// Specialization when rehash policy doesn't provide load factor management.
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
>
1041 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1042 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1047 /// Specialization when rehash policy provide load factor management.
1048 template<typename _Key
, typename _Value
, typename _Alloc
,
1049 typename _ExtractKey
, typename _Equal
,
1050 typename _H1
, typename _H2
, typename _Hash
,
1051 typename _RehashPolicy
, typename _Traits
>
1052 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1053 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
,
1056 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
1057 _Equal
, _H1
, _H2
, _Hash
,
1058 _RehashPolicy
, _Traits
>;
1061 max_load_factor() const noexcept
1063 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1064 return __this
->__rehash_policy().max_load_factor();
1068 max_load_factor(float __z
)
1070 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1071 __this
->__rehash_policy(_RehashPolicy(__z
));
1075 reserve(std::size_t __n
)
1077 __hashtable
* __this
= static_cast<__hashtable
*>(this);
1078 __this
->rehash(__builtin_ceil(__n
/ max_load_factor()));
1083 * Primary class template _Hashtable_ebo_helper.
1085 * Helper class using EBO when it is not forbidden (the type is not
1086 * final) and when it is worth it (the type is empty.)
1088 template<int _Nm
, typename _Tp
,
1089 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
1090 struct _Hashtable_ebo_helper
;
1092 /// Specialization using EBO.
1093 template<int _Nm
, typename _Tp
>
1094 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, true>
1097 _Hashtable_ebo_helper() = default;
1099 template<typename _OtherTp
>
1100 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1101 : _Tp(std::forward
<_OtherTp
>(__tp
))
1105 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
1106 { return static_cast<const _Tp
&>(__eboh
); }
1109 _S_get(_Hashtable_ebo_helper
& __eboh
)
1110 { return static_cast<_Tp
&>(__eboh
); }
1113 /// Specialization not using EBO.
1114 template<int _Nm
, typename _Tp
>
1115 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, false>
1117 _Hashtable_ebo_helper() = default;
1119 template<typename _OtherTp
>
1120 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1121 : _M_tp(std::forward
<_OtherTp
>(__tp
))
1125 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
1126 { return __eboh
._M_tp
; }
1129 _S_get(_Hashtable_ebo_helper
& __eboh
)
1130 { return __eboh
._M_tp
; }
1137 * Primary class template _Local_iterator_base.
1139 * Base class for local iterators, used to iterate within a bucket
1140 * but not between buckets.
1142 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1143 typename _H1
, typename _H2
, typename _Hash
,
1144 bool __cache_hash_code
>
1145 struct _Local_iterator_base
;
1148 * Primary class template _Hash_code_base.
1150 * Encapsulates two policy issues that aren't quite orthogonal.
1151 * (1) the difference between using a ranged hash function and using
1152 * the combination of a hash function and a range-hashing function.
1153 * In the former case we don't have such things as hash codes, so
1154 * we have a dummy type as placeholder.
1155 * (2) Whether or not we cache hash codes. Caching hash codes is
1156 * meaningless if we have a ranged hash function.
1158 * We also put the key extraction objects here, for convenience.
1159 * Each specialization derives from one or more of the template
1160 * parameters to benefit from Ebo. This is important as this type
1161 * is inherited in some cases by the _Local_iterator_base type used
1162 * to implement local_iterator and const_local_iterator. As with
1163 * any iterator type we prefer to make it as small as possible.
1165 * Primary template is unused except as a hook for specializations.
1167 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1168 typename _H1
, typename _H2
, typename _Hash
,
1169 bool __cache_hash_code
>
1170 struct _Hash_code_base
;
1172 /// Specialization: ranged hash function, no caching hash codes. H1
1173 /// and H2 are provided but ignored. We define a dummy hash code type.
1174 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1175 typename _H1
, typename _H2
, typename _Hash
>
1176 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, false>
1177 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1178 private _Hashtable_ebo_helper
<1, _Hash
>
1181 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1182 using __ebo_hash
= _Hashtable_ebo_helper
<1, _Hash
>;
1185 typedef void* __hash_code
;
1186 typedef _Hash_node
<_Value
, false> __node_type
;
1188 // We need the default constructor for the local iterators and _Hashtable
1189 // default constructor.
1190 _Hash_code_base() = default;
1192 _Hash_code_base(const _ExtractKey
& __ex
, const _H1
&, const _H2
&,
1194 : __ebo_extract_key(__ex
), __ebo_hash(__h
) { }
1197 _M_hash_code(const _Key
& __key
) const
1201 _M_bucket_index(const _Key
& __k
, __hash_code
, std::size_t __n
) const
1202 { return _M_ranged_hash()(__k
, __n
); }
1205 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1206 noexcept( noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>(),
1208 { return _M_ranged_hash()(_M_extract()(__p
->_M_v()), __n
); }
1211 _M_store_code(__node_type
*, __hash_code
) const
1215 _M_copy_code(__node_type
*, const __node_type
*) const
1219 _M_swap(_Hash_code_base
& __x
)
1221 std::swap(_M_extract(), __x
._M_extract());
1222 std::swap(_M_ranged_hash(), __x
._M_ranged_hash());
1226 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1229 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1232 _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
1235 _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
1238 // No specialization for ranged hash function while caching hash codes.
1239 // That combination is meaningless, and trying to do it is an error.
1241 /// Specialization: ranged hash function, cache hash codes. This
1242 /// combination is meaningless, so we provide only a declaration
1243 /// and no definition.
1244 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1245 typename _H1
, typename _H2
, typename _Hash
>
1246 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, true>;
1248 /// Specialization: hash function and range-hashing function, no
1249 /// caching of hash codes.
1250 /// Provides typedef and accessor required by C++ 11.
1251 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1252 typename _H1
, typename _H2
>
1253 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1254 _Default_ranged_hash
, false>
1255 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1256 private _Hashtable_ebo_helper
<1, _H1
>,
1257 private _Hashtable_ebo_helper
<2, _H2
>
1260 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1261 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1262 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1264 // Gives the local iterator implementation access to _M_bucket_index().
1265 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1266 _Default_ranged_hash
, false>;
1272 hash_function() const
1276 typedef std::size_t __hash_code
;
1277 typedef _Hash_node
<_Value
, false> __node_type
;
1279 // We need the default constructor for the local iterators and _Hashtable
1280 // default constructor.
1281 _Hash_code_base() = default;
1283 _Hash_code_base(const _ExtractKey
& __ex
,
1284 const _H1
& __h1
, const _H2
& __h2
,
1285 const _Default_ranged_hash
&)
1286 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1289 _M_hash_code(const _Key
& __k
) const
1290 { return _M_h1()(__k
); }
1293 _M_bucket_index(const _Key
&, __hash_code __c
, std::size_t __n
) const
1294 { return _M_h2()(__c
, __n
); }
1297 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1298 noexcept( noexcept(declval
<const _H1
&>()(declval
<const _Key
&>()))
1299 && noexcept(declval
<const _H2
&>()((__hash_code
)0,
1301 { return _M_h2()(_M_h1()(_M_extract()(__p
->_M_v())), __n
); }
1304 _M_store_code(__node_type
*, __hash_code
) const
1308 _M_copy_code(__node_type
*, const __node_type
*) const
1312 _M_swap(_Hash_code_base
& __x
)
1314 std::swap(_M_extract(), __x
._M_extract());
1315 std::swap(_M_h1(), __x
._M_h1());
1316 std::swap(_M_h2(), __x
._M_h2());
1320 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1323 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1326 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1329 _M_h1() { return __ebo_h1::_S_get(*this); }
1332 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1335 _M_h2() { return __ebo_h2::_S_get(*this); }
1338 /// Specialization: hash function and range-hashing function,
1339 /// caching hash codes. H is provided but ignored. Provides
1340 /// typedef and accessor required by C++ 11.
1341 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1342 typename _H1
, typename _H2
>
1343 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1344 _Default_ranged_hash
, true>
1345 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1346 private _Hashtable_ebo_helper
<1, _H1
>,
1347 private _Hashtable_ebo_helper
<2, _H2
>
1350 // Gives the local iterator implementation access to _M_h2().
1351 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1352 _Default_ranged_hash
, true>;
1354 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1355 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1356 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1362 hash_function() const
1366 typedef std::size_t __hash_code
;
1367 typedef _Hash_node
<_Value
, true> __node_type
;
1369 // We need the default constructor for _Hashtable default constructor.
1370 _Hash_code_base() = default;
1371 _Hash_code_base(const _ExtractKey
& __ex
,
1372 const _H1
& __h1
, const _H2
& __h2
,
1373 const _Default_ranged_hash
&)
1374 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1377 _M_hash_code(const _Key
& __k
) const
1378 { return _M_h1()(__k
); }
1381 _M_bucket_index(const _Key
&, __hash_code __c
,
1382 std::size_t __n
) const
1383 { return _M_h2()(__c
, __n
); }
1386 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1387 noexcept( noexcept(declval
<const _H2
&>()((__hash_code
)0,
1389 { return _M_h2()(__p
->_M_hash_code
, __n
); }
1392 _M_store_code(__node_type
* __n
, __hash_code __c
) const
1393 { __n
->_M_hash_code
= __c
; }
1396 _M_copy_code(__node_type
* __to
, const __node_type
* __from
) const
1397 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
1400 _M_swap(_Hash_code_base
& __x
)
1402 std::swap(_M_extract(), __x
._M_extract());
1403 std::swap(_M_h1(), __x
._M_h1());
1404 std::swap(_M_h2(), __x
._M_h2());
1408 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1411 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1414 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1417 _M_h1() { return __ebo_h1::_S_get(*this); }
1420 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1423 _M_h2() { return __ebo_h2::_S_get(*this); }
1427 * Primary class template _Equal_helper.
1430 template <typename _Key
, typename _Value
, typename _ExtractKey
,
1431 typename _Equal
, typename _HashCodeType
,
1432 bool __cache_hash_code
>
1433 struct _Equal_helper
;
1436 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1437 typename _Equal
, typename _HashCodeType
>
1438 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, true>
1441 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1442 const _Key
& __k
, _HashCodeType __c
, _Hash_node
<_Value
, true>* __n
)
1443 { return __c
== __n
->_M_hash_code
&& __eq(__k
, __extract(__n
->_M_v())); }
1447 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1448 typename _Equal
, typename _HashCodeType
>
1449 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, false>
1452 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1453 const _Key
& __k
, _HashCodeType
, _Hash_node
<_Value
, false>* __n
)
1454 { return __eq(__k
, __extract(__n
->_M_v())); }
1458 /// Partial specialization used when nodes contain a cached hash code.
1459 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1460 typename _H1
, typename _H2
, typename _Hash
>
1461 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1462 _H1
, _H2
, _Hash
, true>
1463 : private _Hashtable_ebo_helper
<0, _H2
>
1466 using __base_type
= _Hashtable_ebo_helper
<0, _H2
>;
1467 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1468 _H1
, _H2
, _Hash
, true>;
1470 _Local_iterator_base() = default;
1471 _Local_iterator_base(const __hash_code_base
& __base
,
1472 _Hash_node
<_Value
, true>* __p
,
1473 std::size_t __bkt
, std::size_t __bkt_count
)
1474 : __base_type(__base
._M_h2()),
1475 _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1480 _M_cur
= _M_cur
->_M_next();
1484 = __base_type::_S_get(*this)(_M_cur
->_M_hash_code
,
1486 if (__bkt
!= _M_bucket
)
1491 _Hash_node
<_Value
, true>* _M_cur
;
1492 std::size_t _M_bucket
;
1493 std::size_t _M_bucket_count
;
1497 _M_curr() const { return _M_cur
; } // for equality ops
1500 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1503 // Uninitialized storage for a _Hash_code_base.
1504 // This type is DefaultConstructible and Assignable even if the
1505 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1506 // can be DefaultConstructible and Assignable.
1507 template<typename _Tp
, bool _IsEmpty
= std::is_empty
<_Tp
>::value
>
1508 struct _Hash_code_storage
1510 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
1513 _M_h() { return _M_storage
._M_ptr(); }
1516 _M_h() const { return _M_storage
._M_ptr(); }
1519 // Empty partial specialization for empty _Hash_code_base types.
1520 template<typename _Tp
>
1521 struct _Hash_code_storage
<_Tp
, true>
1523 static_assert( std::is_empty
<_Tp
>::value
, "Type must be empty" );
1525 // As _Tp is an empty type there will be no bytes written/read through
1526 // the cast pointer, so no strict-aliasing violation.
1528 _M_h() { return reinterpret_cast<_Tp
*>(this); }
1531 _M_h() const { return reinterpret_cast<const _Tp
*>(this); }
1534 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1535 typename _H1
, typename _H2
, typename _Hash
>
1536 using __hash_code_for_local_iter
1537 = _Hash_code_storage
<_Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1538 _H1
, _H2
, _Hash
, false>>;
1540 // Partial specialization used when hash codes are not cached
1541 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1542 typename _H1
, typename _H2
, typename _Hash
>
1543 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1544 _H1
, _H2
, _Hash
, false>
1545 : __hash_code_for_local_iter
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
>
1548 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1549 _H1
, _H2
, _Hash
, false>;
1551 _Local_iterator_base() : _M_bucket_count(-1) { }
1553 _Local_iterator_base(const __hash_code_base
& __base
,
1554 _Hash_node
<_Value
, false>* __p
,
1555 std::size_t __bkt
, std::size_t __bkt_count
)
1556 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
)
1557 { _M_init(__base
); }
1559 ~_Local_iterator_base()
1561 if (_M_bucket_count
!= -1)
1565 _Local_iterator_base(const _Local_iterator_base
& __iter
)
1566 : _M_cur(__iter
._M_cur
), _M_bucket(__iter
._M_bucket
),
1567 _M_bucket_count(__iter
._M_bucket_count
)
1569 if (_M_bucket_count
!= -1)
1570 _M_init(*__iter
._M_h());
1573 _Local_iterator_base
&
1574 operator=(const _Local_iterator_base
& __iter
)
1576 if (_M_bucket_count
!= -1)
1578 _M_cur
= __iter
._M_cur
;
1579 _M_bucket
= __iter
._M_bucket
;
1580 _M_bucket_count
= __iter
._M_bucket_count
;
1581 if (_M_bucket_count
!= -1)
1582 _M_init(*__iter
._M_h());
1589 _M_cur
= _M_cur
->_M_next();
1592 std::size_t __bkt
= this->_M_h()->_M_bucket_index(_M_cur
,
1594 if (__bkt
!= _M_bucket
)
1599 _Hash_node
<_Value
, false>* _M_cur
;
1600 std::size_t _M_bucket
;
1601 std::size_t _M_bucket_count
;
1604 _M_init(const __hash_code_base
& __base
)
1605 { ::new(this->_M_h()) __hash_code_base(__base
); }
1608 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1612 _M_curr() const { return _M_cur
; } // for equality ops and debug mode
1615 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1618 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1619 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1621 operator==(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1622 _H1
, _H2
, _Hash
, __cache
>& __x
,
1623 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1624 _H1
, _H2
, _Hash
, __cache
>& __y
)
1625 { return __x
._M_curr() == __y
._M_curr(); }
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(); }
1637 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1638 typename _H1
, typename _H2
, typename _Hash
,
1639 bool __constant_iterators
, bool __cache
>
1640 struct _Local_iterator
1641 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1642 _H1
, _H2
, _Hash
, __cache
>
1645 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1646 _H1
, _H2
, _Hash
, __cache
>;
1647 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1649 typedef _Value value_type
;
1650 typedef typename
std::conditional
<__constant_iterators
,
1651 const _Value
*, _Value
*>::type
1653 typedef typename
std::conditional
<__constant_iterators
,
1654 const _Value
&, _Value
&>::type
1656 typedef std::ptrdiff_t difference_type
;
1657 typedef std::forward_iterator_tag iterator_category
;
1659 _Local_iterator() = default;
1661 _Local_iterator(const __hash_code_base
& __base
,
1662 _Hash_node
<_Value
, __cache
>* __p
,
1663 std::size_t __bkt
, std::size_t __bkt_count
)
1664 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1669 { return this->_M_cur
->_M_v(); }
1673 { return this->_M_cur
->_M_valptr(); }
1685 _Local_iterator
__tmp(*this);
1691 /// local const_iterators
1692 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1693 typename _H1
, typename _H2
, typename _Hash
,
1694 bool __constant_iterators
, bool __cache
>
1695 struct _Local_const_iterator
1696 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1697 _H1
, _H2
, _Hash
, __cache
>
1700 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1701 _H1
, _H2
, _Hash
, __cache
>;
1702 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1705 typedef _Value value_type
;
1706 typedef const _Value
* pointer
;
1707 typedef const _Value
& reference
;
1708 typedef std::ptrdiff_t difference_type
;
1709 typedef std::forward_iterator_tag iterator_category
;
1711 _Local_const_iterator() = default;
1713 _Local_const_iterator(const __hash_code_base
& __base
,
1714 _Hash_node
<_Value
, __cache
>* __p
,
1715 std::size_t __bkt
, std::size_t __bkt_count
)
1716 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1719 _Local_const_iterator(const _Local_iterator
<_Key
, _Value
, _ExtractKey
,
1721 __constant_iterators
,
1728 { return this->_M_cur
->_M_v(); }
1732 { return this->_M_cur
->_M_valptr(); }
1734 _Local_const_iterator
&
1741 _Local_const_iterator
1744 _Local_const_iterator
__tmp(*this);
1751 * Primary class template _Hashtable_base.
1753 * Helper class adding management of _Equal functor to
1754 * _Hash_code_base type.
1756 * Base class templates are:
1757 * - __detail::_Hash_code_base
1758 * - __detail::_Hashtable_ebo_helper
1760 template<typename _Key
, typename _Value
,
1761 typename _ExtractKey
, typename _Equal
,
1762 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
1763 struct _Hashtable_base
1764 : public _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1765 _Traits::__hash_cached::value
>,
1766 private _Hashtable_ebo_helper
<0, _Equal
>
1769 typedef _Key key_type
;
1770 typedef _Value value_type
;
1771 typedef _Equal key_equal
;
1772 typedef std::size_t size_type
;
1773 typedef std::ptrdiff_t difference_type
;
1775 using __traits_type
= _Traits
;
1776 using __hash_cached
= typename
__traits_type::__hash_cached
;
1777 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
1778 using __unique_keys
= typename
__traits_type::__unique_keys
;
1780 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1782 __hash_cached::value
>;
1784 using __hash_code
= typename
__hash_code_base::__hash_code
;
1785 using __node_type
= typename
__hash_code_base::__node_type
;
1787 using iterator
= __detail::_Node_iterator
<value_type
,
1788 __constant_iterators::value
,
1789 __hash_cached::value
>;
1791 using const_iterator
= __detail::_Node_const_iterator
<value_type
,
1792 __constant_iterators::value
,
1793 __hash_cached::value
>;
1795 using local_iterator
= __detail::_Local_iterator
<key_type
, value_type
,
1796 _ExtractKey
, _H1
, _H2
, _Hash
,
1797 __constant_iterators::value
,
1798 __hash_cached::value
>;
1800 using const_local_iterator
= __detail::_Local_const_iterator
<key_type
,
1802 _ExtractKey
, _H1
, _H2
, _Hash
,
1803 __constant_iterators::value
,
1804 __hash_cached::value
>;
1806 using __ireturn_type
= typename
std::conditional
<__unique_keys::value
,
1807 std::pair
<iterator
, bool>,
1810 using _EqualEBO
= _Hashtable_ebo_helper
<0, _Equal
>;
1811 using _EqualHelper
= _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
,
1812 __hash_code
, __hash_cached::value
>;
1815 _Hashtable_base() = default;
1816 _Hashtable_base(const _ExtractKey
& __ex
, const _H1
& __h1
, const _H2
& __h2
,
1817 const _Hash
& __hash
, const _Equal
& __eq
)
1818 : __hash_code_base(__ex
, __h1
, __h2
, __hash
), _EqualEBO(__eq
)
1822 _M_equals(const _Key
& __k
, __hash_code __c
, __node_type
* __n
) const
1824 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
1829 _M_swap(_Hashtable_base
& __x
)
1831 __hash_code_base::_M_swap(__x
);
1832 std::swap(_M_eq(), __x
._M_eq());
1836 _M_eq() const { return _EqualEBO::_S_cget(*this); }
1839 _M_eq() { return _EqualEBO::_S_get(*this); }
1843 * struct _Equality_base.
1845 * Common types and functions for class _Equality.
1847 struct _Equality_base
1850 template<typename _Uiterator
>
1852 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
1855 // See std::is_permutation in N3068.
1856 template<typename _Uiterator
>
1859 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
1860 _Uiterator __first2
)
1862 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1863 if (!(*__first1
== *__first2
))
1866 if (__first1
== __last1
)
1869 _Uiterator __last2
= __first2
;
1870 std::advance(__last2
, std::distance(__first1
, __last1
));
1872 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
1874 _Uiterator __tmp
= __first1
;
1875 while (__tmp
!= __it1
&& !bool(*__tmp
== *__it1
))
1878 // We've seen this one before.
1882 std::ptrdiff_t __n2
= 0;
1883 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
1884 if (*__tmp
== *__it1
)
1890 std::ptrdiff_t __n1
= 0;
1891 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
1892 if (*__tmp
== *__it1
)
1902 * Primary class template _Equality.
1904 * This is for implementing equality comparison for unordered
1905 * containers, per N3068, by John Lakos and Pablo Halpern.
1906 * Algorithmically, we follow closely the reference implementations
1909 template<typename _Key
, typename _Value
, typename _Alloc
,
1910 typename _ExtractKey
, typename _Equal
,
1911 typename _H1
, typename _H2
, typename _Hash
,
1912 typename _RehashPolicy
, typename _Traits
,
1913 bool _Unique_keys
= _Traits::__unique_keys::value
>
1917 template<typename _Key
, typename _Value
, typename _Alloc
,
1918 typename _ExtractKey
, typename _Equal
,
1919 typename _H1
, typename _H2
, typename _Hash
,
1920 typename _RehashPolicy
, typename _Traits
>
1921 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1922 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
1924 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1925 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1928 _M_equal(const __hashtable
&) const;
1931 template<typename _Key
, typename _Value
, typename _Alloc
,
1932 typename _ExtractKey
, typename _Equal
,
1933 typename _H1
, typename _H2
, typename _Hash
,
1934 typename _RehashPolicy
, typename _Traits
>
1936 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1937 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
1938 _M_equal(const __hashtable
& __other
) const
1940 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1942 if (__this
->size() != __other
.size())
1945 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
1947 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
1948 if (__ity
== __other
.end() || !bool(*__ity
== *__itx
))
1955 template<typename _Key
, typename _Value
, typename _Alloc
,
1956 typename _ExtractKey
, typename _Equal
,
1957 typename _H1
, typename _H2
, typename _Hash
,
1958 typename _RehashPolicy
, typename _Traits
>
1959 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1960 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
1961 : public _Equality_base
1963 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1964 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1967 _M_equal(const __hashtable
&) const;
1970 template<typename _Key
, typename _Value
, typename _Alloc
,
1971 typename _ExtractKey
, typename _Equal
,
1972 typename _H1
, typename _H2
, typename _Hash
,
1973 typename _RehashPolicy
, typename _Traits
>
1975 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1976 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>::
1977 _M_equal(const __hashtable
& __other
) const
1979 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1981 if (__this
->size() != __other
.size())
1984 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
1986 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
1987 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
1989 if (std::distance(__xrange
.first
, __xrange
.second
)
1990 != std::distance(__yrange
.first
, __yrange
.second
))
1993 if (!_S_is_permutation(__xrange
.first
, __xrange
.second
,
1997 __itx
= __xrange
.second
;
2003 * This type deals with all allocation and keeps an allocator instance through
2004 * inheritance to benefit from EBO when possible.
2006 template<typename _NodeAlloc
>
2007 struct _Hashtable_alloc
: private _Hashtable_ebo_helper
<0, _NodeAlloc
>
2010 using __ebo_node_alloc
= _Hashtable_ebo_helper
<0, _NodeAlloc
>;
2012 using __node_type
= typename
_NodeAlloc::value_type
;
2013 using __node_alloc_type
= _NodeAlloc
;
2014 // Use __gnu_cxx to benefit from _S_always_equal and al.
2015 using __node_alloc_traits
= __gnu_cxx::__alloc_traits
<__node_alloc_type
>;
2017 using __value_alloc_traits
= typename
__node_alloc_traits::template
2018 rebind_traits
<typename
__node_type::value_type
>;
2020 using __node_base
= __detail::_Hash_node_base
;
2021 using __bucket_type
= __node_base
*;
2022 using __bucket_alloc_type
=
2023 __alloc_rebind
<__node_alloc_type
, __bucket_type
>;
2024 using __bucket_alloc_traits
= std::allocator_traits
<__bucket_alloc_type
>;
2026 _Hashtable_alloc() = default;
2027 _Hashtable_alloc(const _Hashtable_alloc
&) = default;
2028 _Hashtable_alloc(_Hashtable_alloc
&&) = default;
2030 template<typename _Alloc
>
2031 _Hashtable_alloc(_Alloc
&& __a
)
2032 : __ebo_node_alloc(std::forward
<_Alloc
>(__a
))
2037 { return __ebo_node_alloc::_S_get(*this); }
2039 const __node_alloc_type
&
2040 _M_node_allocator() const
2041 { return __ebo_node_alloc::_S_cget(*this); }
2043 template<typename
... _Args
>
2045 _M_allocate_node(_Args
&&... __args
);
2048 _M_deallocate_node(__node_type
* __n
);
2050 // Deallocate the linked list of nodes pointed to by __n
2052 _M_deallocate_nodes(__node_type
* __n
);
2055 _M_allocate_buckets(std::size_t __n
);
2058 _M_deallocate_buckets(__bucket_type
*, std::size_t __n
);
2061 // Definitions of class template _Hashtable_alloc's out-of-line member
2063 template<typename _NodeAlloc
>
2064 template<typename
... _Args
>
2065 typename _Hashtable_alloc
<_NodeAlloc
>::__node_type
*
2066 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_node(_Args
&&... __args
)
2068 auto __nptr
= __node_alloc_traits::allocate(_M_node_allocator(), 1);
2069 __node_type
* __n
= std::__to_address(__nptr
);
2072 ::new ((void*)__n
) __node_type
;
2073 __node_alloc_traits::construct(_M_node_allocator(),
2075 std::forward
<_Args
>(__args
)...);
2080 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr
, 1);
2081 __throw_exception_again
;
2085 template<typename _NodeAlloc
>
2087 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_node(__node_type
* __n
)
2089 typedef typename
__node_alloc_traits::pointer _Ptr
;
2090 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__n
);
2091 __node_alloc_traits::destroy(_M_node_allocator(), __n
->_M_valptr());
2092 __n
->~__node_type();
2093 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr
, 1);
2096 template<typename _NodeAlloc
>
2098 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_nodes(__node_type
* __n
)
2102 __node_type
* __tmp
= __n
;
2103 __n
= __n
->_M_next();
2104 _M_deallocate_node(__tmp
);
2108 template<typename _NodeAlloc
>
2109 typename _Hashtable_alloc
<_NodeAlloc
>::__bucket_type
*
2110 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_buckets(std::size_t __n
)
2112 __bucket_alloc_type
__alloc(_M_node_allocator());
2114 auto __ptr
= __bucket_alloc_traits::allocate(__alloc
, __n
);
2115 __bucket_type
* __p
= std::__to_address(__ptr
);
2116 __builtin_memset(__p
, 0, __n
* sizeof(__bucket_type
));
2120 template<typename _NodeAlloc
>
2122 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_buckets(__bucket_type
* __bkts
,
2125 typedef typename
__bucket_alloc_traits::pointer _Ptr
;
2126 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__bkts
);
2127 __bucket_alloc_type
__alloc(_M_node_allocator());
2128 __bucket_alloc_traits::deallocate(__alloc
, __ptr
, __n
);
2131 //@} hashtable-detail
2132 } // namespace __detail
2133 _GLIBCXX_END_NAMESPACE_VERSION
2136 #endif // _HASHTABLE_POLICY_H