1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010, 2011, 2012 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 namespace std
_GLIBCXX_VISIBILITY(default)
36 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 template<typename _Key
, typename _Value
, typename _Alloc
,
39 typename _ExtractKey
, typename _Equal
,
40 typename _H1
, typename _H2
, typename _Hash
,
41 typename _RehashPolicy
, typename _Traits
>
44 _GLIBCXX_END_NAMESPACE_VERSION
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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 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
)
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
)
78 typedef typename
std::iterator_traits
<_Iterator
>::iterator_category _Tag
;
79 return __distance_fw(__first
, __last
, _Tag());
82 // Helper type used to detect whether the hash functor is noexcept.
83 template <typename _Key
, typename _Hash
>
84 struct __is_noexcept_hash
: std::integral_constant
<bool,
85 noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>()))>
90 template<typename _Tp
>
92 operator()(_Tp
&& __x
) const
93 { return std::forward
<_Tp
>(__x
); }
98 template<typename _Tp
>
100 operator()(_Tp
&& __x
) const
101 -> decltype(std::get
<0>(std::forward
<_Tp
>(__x
)))
102 { return std::get
<0>(std::forward
<_Tp
>(__x
)); }
105 // Auxiliary types used for all instantiations of _Hashtable nodes
109 * struct _Hashtable_traits
111 * Important traits for hash tables.
113 * @tparam _Cache_hash_code Boolean value. True if the value of
114 * the hash function is stored along with the value. This is a
115 * time-space tradeoff. Storing it may improve lookup speed by
116 * reducing the number of times we need to call the _Equal
119 * @tparam _Constant_iterators Boolean value. True if iterator and
120 * const_iterator are both constant iterator types. This is true
121 * for unordered_set and unordered_multiset, false for
122 * unordered_map and unordered_multimap.
124 * @tparam _Unique_keys Boolean value. True if the return value
125 * of _Hashtable::count(k) is always at most one, false if it may
126 * be an arbitrary number. This is true for unordered_set and
127 * unordered_map, false for unordered_multiset and
128 * unordered_multimap.
130 template<bool _Cache_hash_code
, bool _Constant_iterators
, bool _Unique_keys
>
131 struct _Hashtable_traits
134 using __bool_constant
= integral_constant
<bool, _Cond
>;
136 using __hash_cached
= __bool_constant
<_Cache_hash_code
>;
137 using __constant_iterators
= __bool_constant
<_Constant_iterators
>;
138 using __unique_keys
= __bool_constant
<_Unique_keys
>;
142 * struct _Hash_node_base
144 * Nodes, used to wrap elements stored in the hash table. A policy
145 * template parameter of class template _Hashtable controls whether
146 * nodes also store a hash code. In some cases (e.g. strings) this
147 * may be a performance win.
149 struct _Hash_node_base
151 _Hash_node_base
* _M_nxt
;
153 _Hash_node_base() : _M_nxt() { }
155 _Hash_node_base(_Hash_node_base
* __next
) : _M_nxt(__next
) { }
159 * Primary template struct _Hash_node.
161 template<typename _Value
, bool _Cache_hash_code
>
165 * Specialization for nodes with caches, struct _Hash_node.
167 * Base class is __detail::_Hash_node_base.
169 template<typename _Value
>
170 struct _Hash_node
<_Value
, true> : _Hash_node_base
173 std::size_t _M_hash_code
;
175 template<typename
... _Args
>
176 _Hash_node(_Args
&&... __args
)
177 : _M_v(std::forward
<_Args
>(__args
)...), _M_hash_code() { }
180 _M_next() const { return static_cast<_Hash_node
*>(_M_nxt
); }
184 * Specialization for nodes without caches, struct _Hash_node.
186 * Base class is __detail::_Hash_node_base.
188 template<typename _Value
>
189 struct _Hash_node
<_Value
, false> : _Hash_node_base
193 template<typename
... _Args
>
194 _Hash_node(_Args
&&... __args
)
195 : _M_v(std::forward
<_Args
>(__args
)...) { }
198 _M_next() const { return static_cast<_Hash_node
*>(_M_nxt
); }
201 /// Base class for node iterators.
202 template<typename _Value
, bool _Cache_hash_code
>
203 struct _Node_iterator_base
205 typedef _Hash_node
<_Value
, _Cache_hash_code
> __node_type
;
209 _Node_iterator_base(__node_type
* __p
)
214 { _M_cur
= _M_cur
->_M_next(); }
217 template<typename _Value
, bool _Cache_hash_code
>
219 operator==(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
220 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
221 { return __x
._M_cur
== __y
._M_cur
; }
223 template<typename _Value
, bool _Cache_hash_code
>
225 operator!=(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
226 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
227 { return __x
._M_cur
!= __y
._M_cur
; }
229 /// Node iterators, used to iterate through all the hashtable.
230 template<typename _Value
, bool __constant_iterators
, bool __cache
>
231 struct _Node_iterator
232 : public _Node_iterator_base
<_Value
, __cache
>
235 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
236 using __node_type
= typename
__base_type::__node_type
;
239 typedef _Value value_type
;
240 typedef std::ptrdiff_t difference_type
;
241 typedef std::forward_iterator_tag iterator_category
;
243 using pointer
= typename
std::conditional
<__constant_iterators
,
244 const _Value
*, _Value
*>::type
;
246 using reference
= typename
std::conditional
<__constant_iterators
,
247 const _Value
&, _Value
&>::type
;
253 _Node_iterator(__node_type
* __p
)
254 : __base_type(__p
) { }
258 { return this->_M_cur
->_M_v
; }
262 { return std::__addressof(this->_M_cur
->_M_v
); }
274 _Node_iterator
__tmp(*this);
280 /// Node const_iterators, used to iterate through all the hashtable.
281 template<typename _Value
, bool __constant_iterators
, bool __cache
>
282 struct _Node_const_iterator
283 : public _Node_iterator_base
<_Value
, __cache
>
286 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
287 using __node_type
= typename
__base_type::__node_type
;
290 typedef _Value value_type
;
291 typedef std::ptrdiff_t difference_type
;
292 typedef std::forward_iterator_tag iterator_category
;
294 typedef const _Value
* pointer
;
295 typedef const _Value
& reference
;
297 _Node_const_iterator()
301 _Node_const_iterator(__node_type
* __p
)
302 : __base_type(__p
) { }
304 _Node_const_iterator(const _Node_iterator
<_Value
, __constant_iterators
,
306 : __base_type(__x
._M_cur
) { }
310 { return this->_M_cur
->_M_v
; }
314 { return std::__addressof(this->_M_cur
->_M_v
); }
316 _Node_const_iterator
&
326 _Node_const_iterator
__tmp(*this);
332 // Many of class template _Hashtable's template parameters are policy
333 // classes. These are defaults for the policies.
335 /// Default range hashing function: use division to fold a large number
336 /// into the range [0, N).
337 struct _Mod_range_hashing
339 typedef std::size_t first_argument_type
;
340 typedef std::size_t second_argument_type
;
341 typedef std::size_t result_type
;
344 operator()(first_argument_type __num
, second_argument_type __den
) const
345 { return __num
% __den
; }
348 /// Default ranged hash function H. In principle it should be a
349 /// function object composed from objects of type H1 and H2 such that
350 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
351 /// h1 and h2. So instead we'll just use a tag to tell class template
352 /// hashtable to do that composition.
353 struct _Default_ranged_hash
{ };
355 /// Default value for rehash policy. Bucket size is (usually) the
356 /// smallest prime that keeps the load factor small enough.
357 struct _Prime_rehash_policy
359 _Prime_rehash_policy(float __z
= 1.0)
360 : _M_max_load_factor(__z
), _M_next_resize(0) { }
363 max_load_factor() const noexcept
364 { return _M_max_load_factor
; }
366 // Return a bucket size no smaller than n.
368 _M_next_bkt(std::size_t __n
) const;
370 // Return a bucket count appropriate for n elements
372 _M_bkt_for_elements(std::size_t __n
) const;
374 // __n_bkt is current bucket count, __n_elt is current element count,
375 // and __n_ins is number of elements to be inserted. Do we need to
376 // increase bucket count? If so, return make_pair(true, n), where n
377 // is the new bucket count. If not, return make_pair(false, 0).
378 std::pair
<bool, std::size_t>
379 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
380 std::size_t __n_ins
) const;
382 typedef std::size_t _State
;
386 { return _M_next_resize
; }
389 _M_reset(_State __state
)
390 { _M_next_resize
= __state
; }
392 enum { _S_n_primes
= sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
394 static const std::size_t _S_growth_factor
= 2;
396 float _M_max_load_factor
;
397 mutable std::size_t _M_next_resize
;
400 extern const unsigned long __prime_list
[];
402 // XXX This is a hack. There's no good reason for any of
403 // _Prime_rehash_policy's member functions to be inline.
405 // Return a prime no smaller than n.
407 _Prime_rehash_policy::
408 _M_next_bkt(std::size_t __n
) const
410 // Optimize lookups involving the first elements of __prime_list.
411 // (useful to speed-up, eg, constructors)
412 static const unsigned char __fast_bkt
[12]
413 = { 2, 2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11 };
418 = __builtin_ceil(__fast_bkt
[__n
]
419 * (long double)_M_max_load_factor
);
420 return __fast_bkt
[__n
];
423 const unsigned long* __next_bkt
424 = std::lower_bound(__prime_list
+ 5, __prime_list
+ _S_n_primes
,
427 = __builtin_ceil(*__next_bkt
* (long double)_M_max_load_factor
);
431 // Return the smallest integer p such that alpha p >= n, where alpha
432 // is the load factor.
434 _Prime_rehash_policy::
435 _M_bkt_for_elements(std::size_t __n
) const
436 { return __builtin_ceil(__n
/ (long double)_M_max_load_factor
); }
438 // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
439 // If p > __n_bkt, return make_pair(true, p); otherwise return
440 // make_pair(false, 0). In principle this isn't very different from
441 // _M_bkt_for_elements.
443 // The only tricky part is that we're caching the element count at
444 // which we need to rehash, so we don't have to do a floating-point
445 // multiply for every insertion.
447 inline std::pair
<bool, std::size_t>
448 _Prime_rehash_policy::
449 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
450 std::size_t __n_ins
) const
452 if (__n_elt
+ __n_ins
>= _M_next_resize
)
454 long double __min_bkts
= (__n_elt
+ __n_ins
)
455 / (long double)_M_max_load_factor
;
456 if (__min_bkts
>= __n_bkt
)
457 return std::make_pair(true,
458 _M_next_bkt(std::max
<std::size_t>(__builtin_floor(__min_bkts
) + 1,
459 __n_bkt
* _S_growth_factor
)));
463 = __builtin_floor(__n_bkt
* (long double)_M_max_load_factor
);
464 return std::make_pair(false, 0);
468 return std::make_pair(false, 0);
471 // Base classes for std::_Hashtable. We define these base classes
472 // because in some cases we want to do different things depending on
473 // the value of a policy class. In some cases the policy class
474 // affects which member functions and nested typedefs are defined;
475 // we handle that by specializing base class templates. Several of
476 // the base class templates need to access other members of class
477 // template _Hashtable, so we use a variant of the "Curiously
478 // Recurring Template Pattern" (CRTP) technique.
481 * Primary class template _Map_base.
483 * If the hashtable has a value type of the form pair<T1, T2> and a
484 * key extraction policy (_ExtractKey) that returns the first part
485 * of the pair, the hashtable gets a mapped_type typedef. If it
486 * satisfies those criteria and also has unique keys, then it also
487 * gets an operator[].
489 template<typename _Key
, typename _Value
, typename _Alloc
,
490 typename _ExtractKey
, typename _Equal
,
491 typename _H1
, typename _H2
, typename _Hash
,
492 typename _RehashPolicy
, typename _Traits
,
493 bool _Unique_keys
= _Traits::__unique_keys::value
>
494 struct _Map_base
{ };
496 /// Partial specialization, __unique_keys set to false.
497 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
498 typename _H1
, typename _H2
, typename _Hash
,
499 typename _RehashPolicy
, typename _Traits
>
500 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
501 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
503 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
506 /// Partial specialization, __unique_keys set to true.
507 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
508 typename _H1
, typename _H2
, typename _Hash
,
509 typename _RehashPolicy
, typename _Traits
>
510 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
511 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
514 using __hashtable_base
= __detail::_Hashtable_base
<_Key
, _Pair
,
516 _Equal
, _H1
, _H2
, _Hash
,
519 using __hashtable
= _Hashtable
<_Key
, _Pair
, _Alloc
,
521 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
523 using __hash_code
= typename
__hashtable_base::__hash_code
;
524 using __node_type
= typename
__hashtable_base::__node_type
;
527 using key_type
= typename
__hashtable_base::key_type
;
528 using iterator
= typename
__hashtable_base::iterator
;
529 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
532 operator[](const key_type
& __k
);
535 operator[](key_type
&& __k
);
537 // _GLIBCXX_RESOLVE_LIB_DEFECTS
538 // DR 761. unordered_map needs an at() member function.
540 at(const key_type
& __k
);
543 at(const key_type
& __k
) const;
546 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
547 typename _H1
, typename _H2
, typename _Hash
,
548 typename _RehashPolicy
, typename _Traits
>
549 typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
550 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
552 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
553 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
554 operator[](const key_type
& __k
)
556 __hashtable
* __h
= static_cast<__hashtable
*>(this);
557 __hash_code __code
= __h
->_M_hash_code(__k
);
558 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
559 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
563 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
564 std::tuple
<const key_type
&>(__k
),
566 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
569 return (__p
->_M_v
).second
;
572 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
573 typename _H1
, typename _H2
, typename _Hash
,
574 typename _RehashPolicy
, typename _Traits
>
575 typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
576 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
578 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
579 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
580 operator[](key_type
&& __k
)
582 __hashtable
* __h
= static_cast<__hashtable
*>(this);
583 __hash_code __code
= __h
->_M_hash_code(__k
);
584 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
585 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
589 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
590 std::forward_as_tuple(std::move(__k
)),
592 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
595 return (__p
->_M_v
).second
;
598 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
599 typename _H1
, typename _H2
, typename _Hash
,
600 typename _RehashPolicy
, typename _Traits
>
601 typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
602 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
604 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
605 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
606 at(const key_type
& __k
)
608 __hashtable
* __h
= static_cast<__hashtable
*>(this);
609 __hash_code __code
= __h
->_M_hash_code(__k
);
610 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
611 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
614 __throw_out_of_range(__N("_Map_base::at"));
615 return (__p
->_M_v
).second
;
618 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
619 typename _H1
, typename _H2
, typename _Hash
,
620 typename _RehashPolicy
, typename _Traits
>
621 const typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
,
622 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
623 _Traits
, true>::mapped_type
&
624 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
625 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
626 at(const key_type
& __k
) const
628 const __hashtable
* __h
= static_cast<const __hashtable
*>(this);
629 __hash_code __code
= __h
->_M_hash_code(__k
);
630 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
631 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
634 __throw_out_of_range(__N("_Map_base::at"));
635 return (__p
->_M_v
).second
;
639 * Primary class template _Insert_base.
641 * insert member functions appropriate to all _Hashtables.
643 template<typename _Key
, typename _Value
, typename _Alloc
,
644 typename _ExtractKey
, typename _Equal
,
645 typename _H1
, typename _H2
, typename _Hash
,
646 typename _RehashPolicy
, typename _Traits
>
649 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
650 _Equal
, _H1
, _H2
, _Hash
,
651 _RehashPolicy
, _Traits
>;
653 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
654 _Equal
, _H1
, _H2
, _Hash
,
657 using value_type
= typename
__hashtable_base::value_type
;
658 using iterator
= typename
__hashtable_base::iterator
;
659 using const_iterator
= typename
__hashtable_base::const_iterator
;
660 using size_type
= typename
__hashtable_base::size_type
;
662 using __unique_keys
= typename
__hashtable_base::__unique_keys
;
663 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
664 using __iconv_type
= typename
__hashtable_base::__iconv_type
;
667 _M_conjure_hashtable()
668 { return *(static_cast<__hashtable
*>(this)); }
671 insert(const value_type
& __v
)
673 __hashtable
& __h
= _M_conjure_hashtable();
674 return __h
._M_insert(__v
, __unique_keys());
678 insert(const_iterator
, const value_type
& __v
)
679 { return __iconv_type()(insert(__v
)); }
682 insert(initializer_list
<value_type
> __l
)
683 { this->insert(__l
.begin(), __l
.end()); }
685 template<typename _InputIterator
>
687 insert(_InputIterator __first
, _InputIterator __last
);
690 template<typename _Key
, typename _Value
, typename _Alloc
,
691 typename _ExtractKey
, typename _Equal
,
692 typename _H1
, typename _H2
, typename _Hash
,
693 typename _RehashPolicy
, typename _Traits
>
694 template<typename _InputIterator
>
696 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
697 _RehashPolicy
, _Traits
>::
698 insert(_InputIterator __first
, _InputIterator __last
)
700 using __rehash_type
= typename
__hashtable::__rehash_type
;
701 using __rehash_state
= typename
__hashtable::__rehash_state
;
702 using pair_type
= std::pair
<bool, std::size_t>;
704 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
706 __hashtable
& __h
= _M_conjure_hashtable();
707 __rehash_type
& __rehash
= __h
._M_rehash_policy
;
708 const __rehash_state
& __saved_state
= __rehash
._M_state();
709 pair_type __do_rehash
= __rehash
._M_need_rehash(__h
._M_bucket_count
,
710 __h
._M_element_count
,
713 if (__do_rehash
.first
)
714 __h
._M_rehash(__do_rehash
.second
, __saved_state
);
716 for (; __first
!= __last
; ++__first
)
717 this->insert(*__first
);
721 * Primary class template _Insert.
723 * Select insert member functions appropriate to _Hashtable policy choices.
725 template<typename _Key
, typename _Value
, typename _Alloc
,
726 typename _ExtractKey
, typename _Equal
,
727 typename _H1
, typename _H2
, typename _Hash
,
728 typename _RehashPolicy
, typename _Traits
,
729 bool _Constant_iterators
= _Traits::__constant_iterators::value
,
730 bool _Unique_keys
= _Traits::__unique_keys::value
>
734 template<typename _Key
, typename _Value
, typename _Alloc
,
735 typename _ExtractKey
, typename _Equal
,
736 typename _H1
, typename _H2
, typename _Hash
,
737 typename _RehashPolicy
, typename _Traits
>
738 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
739 _RehashPolicy
, _Traits
, true, true>
740 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
741 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
743 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
744 _Equal
, _H1
, _H2
, _Hash
,
745 _RehashPolicy
, _Traits
>;
746 using value_type
= typename
__base_type::value_type
;
747 using iterator
= typename
__base_type::iterator
;
748 using const_iterator
= typename
__base_type::const_iterator
;
750 using __unique_keys
= typename
__base_type::__unique_keys
;
751 using __hashtable
= typename
__base_type::__hashtable
;
753 using __base_type::insert
;
755 std::pair
<iterator
, bool>
756 insert(value_type
&& __v
)
758 __hashtable
& __h
= this->_M_conjure_hashtable();
759 return __h
._M_insert(std::move(__v
), __unique_keys());
763 insert(const_iterator
, value_type
&& __v
)
764 { return insert(std::move(__v
)).first
; }
768 template<typename _Key
, typename _Value
, typename _Alloc
,
769 typename _ExtractKey
, typename _Equal
,
770 typename _H1
, typename _H2
, typename _Hash
,
771 typename _RehashPolicy
, typename _Traits
>
772 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
773 _RehashPolicy
, _Traits
, true, false>
774 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
775 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
777 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
778 _Equal
, _H1
, _H2
, _Hash
,
779 _RehashPolicy
, _Traits
>;
780 using value_type
= typename
__base_type::value_type
;
781 using iterator
= typename
__base_type::iterator
;
782 using const_iterator
= typename
__base_type::const_iterator
;
784 using __unique_keys
= typename
__base_type::__unique_keys
;
785 using __hashtable
= typename
__base_type::__hashtable
;
787 using __base_type::insert
;
790 insert(value_type
&& __v
)
792 __hashtable
& __h
= this->_M_conjure_hashtable();
793 return __h
._M_insert(std::move(__v
), __unique_keys());
797 insert(const_iterator
, value_type
&& __v
)
798 { return insert(std::move(__v
)); }
802 template<typename _Key
, typename _Value
, typename _Alloc
,
803 typename _ExtractKey
, typename _Equal
,
804 typename _H1
, typename _H2
, typename _Hash
,
805 typename _RehashPolicy
, typename _Traits
, bool _Unique_keys
>
806 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
807 _RehashPolicy
, _Traits
, false, _Unique_keys
>
808 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
809 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
811 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
812 _Equal
, _H1
, _H2
, _Hash
,
813 _RehashPolicy
, _Traits
>;
814 using value_type
= typename
__base_type::value_type
;
815 using iterator
= typename
__base_type::iterator
;
816 using const_iterator
= typename
__base_type::const_iterator
;
818 using __unique_keys
= typename
__base_type::__unique_keys
;
819 using __hashtable
= typename
__base_type::__hashtable
;
820 using __ireturn_type
= typename
__base_type::__ireturn_type
;
821 using __iconv_type
= typename
__base_type::__iconv_type
;
823 using __base_type::insert
;
825 template<typename _Pair
>
826 using __is_cons
= std::is_constructible
<value_type
, _Pair
&&>;
828 template<typename _Pair
>
829 using _IFcons
= std::enable_if
<__is_cons
<_Pair
>::value
>;
831 template<typename _Pair
>
832 using _IFconsp
= typename _IFcons
<_Pair
>::type
;
834 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
838 __hashtable
& __h
= this->_M_conjure_hashtable();
839 return __h
._M_insert(std::forward
<_Pair
>(__v
), __unique_keys());
842 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
844 insert(const_iterator
, _Pair
&& __v
)
845 { return __iconv_type()(insert(std::forward
<_Pair
>(__v
))); }
849 * Primary class template _Rehash_base.
851 * Give hashtable the max_load_factor functions and reserve iff the
852 * rehash policy is _Prime_rehash_policy.
854 template<typename _Key
, typename _Value
, typename _Alloc
,
855 typename _ExtractKey
, typename _Equal
,
856 typename _H1
, typename _H2
, typename _Hash
,
857 typename _RehashPolicy
, typename _Traits
>
861 template<typename _Key
, typename _Value
, typename _Alloc
,
862 typename _ExtractKey
, typename _Equal
,
863 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
864 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
865 _H1
, _H2
, _Hash
, _Prime_rehash_policy
, _Traits
>
867 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
868 _Equal
, _H1
, _H2
, _Hash
,
869 _Prime_rehash_policy
, _Traits
>;
872 max_load_factor() const noexcept
874 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
875 return __this
->__rehash_policy().max_load_factor();
879 max_load_factor(float __z
)
881 __hashtable
* __this
= static_cast<__hashtable
*>(this);
882 __this
->__rehash_policy(_Prime_rehash_policy(__z
));
886 reserve(std::size_t __n
)
888 __hashtable
* __this
= static_cast<__hashtable
*>(this);
889 __this
->rehash(__builtin_ceil(__n
/ max_load_factor()));
894 * Primary class template _Hashtable_ebo_helper.
896 * Helper class using EBO when it is not forbidden, type is not
897 * final, and when it worth it, type is empty.
899 template<int _Nm
, typename _Tp
,
900 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
901 struct _Hashtable_ebo_helper
;
903 /// Specialization using EBO.
904 template<int _Nm
, typename _Tp
>
905 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, true>
908 _Hashtable_ebo_helper() = default;
910 _Hashtable_ebo_helper(const _Tp
& __tp
) : _Tp(__tp
)
914 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
915 { return static_cast<const _Tp
&>(__eboh
); }
918 _S_get(_Hashtable_ebo_helper
& __eboh
)
919 { return static_cast<_Tp
&>(__eboh
); }
922 /// Specialization not using EBO.
923 template<int _Nm
, typename _Tp
>
924 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, false>
926 _Hashtable_ebo_helper() = default;
928 _Hashtable_ebo_helper(const _Tp
& __tp
) : _M_tp(__tp
)
932 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
933 { return __eboh
._M_tp
; }
936 _S_get(_Hashtable_ebo_helper
& __eboh
)
937 { return __eboh
._M_tp
; }
944 * Primary class template _Hash_code_base.
946 * Encapsulates two policy issues that aren't quite orthogonal.
947 * (1) the difference between using a ranged hash function and using
948 * the combination of a hash function and a range-hashing function.
949 * In the former case we don't have such things as hash codes, so
950 * we have a dummy type as placeholder.
951 * (2) Whether or not we cache hash codes. Caching hash codes is
952 * meaningless if we have a ranged hash function.
954 * We also put the key extraction objects here, for convenience.
955 * Each specialization derives from one or more of the template
956 * parameters to benefit from Ebo. This is important as this type
957 * is inherited in some cases by the _Local_iterator_base type used
958 * to implement local_iterator and const_local_iterator. As with
959 * any iterator type we prefer to make it as small as possible.
961 * Primary template is unused except as a hook for specializations.
963 template<typename _Key
, typename _Value
, typename _ExtractKey
,
964 typename _H1
, typename _H2
, typename _Hash
,
965 bool __cache_hash_code
>
966 struct _Hash_code_base
;
968 /// Specialization: ranged hash function, no caching hash codes. H1
969 /// and H2 are provided but ignored. We define a dummy hash code type.
970 template<typename _Key
, typename _Value
, typename _ExtractKey
,
971 typename _H1
, typename _H2
, typename _Hash
>
972 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, false>
973 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
974 private _Hashtable_ebo_helper
<1, _Hash
>
977 typedef _Hashtable_ebo_helper
<0, _ExtractKey
> _EboExtractKey
;
978 typedef _Hashtable_ebo_helper
<1, _Hash
> _EboHash
;
981 typedef void* __hash_code
;
982 typedef _Hash_node
<_Value
, false> __node_type
;
984 // We need the default constructor for the local iterators.
985 _Hash_code_base() = default;
987 _Hash_code_base(const _ExtractKey
& __ex
, const _H1
&, const _H2
&,
989 : _EboExtractKey(__ex
), _EboHash(__h
) { }
992 _M_hash_code(const _Key
& __key
) const
996 _M_bucket_index(const _Key
& __k
, __hash_code
, std::size_t __n
) const
997 { return _M_ranged_hash()(__k
, __n
); }
1000 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1001 { return _M_ranged_hash()(_M_extract()(__p
->_M_v
), __n
); }
1004 _M_store_code(__node_type
*, __hash_code
) const
1008 _M_copy_code(__node_type
*, const __node_type
*) const
1012 _M_swap(_Hash_code_base
& __x
)
1014 std::swap(_M_extract(), __x
._M_extract());
1015 std::swap(_M_ranged_hash(), __x
._M_ranged_hash());
1020 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
1023 _M_extract() { return _EboExtractKey::_S_get(*this); }
1026 _M_ranged_hash() const { return _EboHash::_S_cget(*this); }
1029 _M_ranged_hash() { return _EboHash::_S_get(*this); }
1032 // No specialization for ranged hash function while caching hash codes.
1033 // That combination is meaningless, and trying to do it is an error.
1035 /// Specialization: ranged hash function, cache hash codes. This
1036 /// combination is meaningless, so we provide only a declaration
1037 /// and no definition.
1038 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1039 typename _H1
, typename _H2
, typename _Hash
>
1040 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, true>;
1042 /// Specialization: hash function and range-hashing function, no
1043 /// caching of hash codes.
1044 /// Provides typedef and accessor required by TR1.
1045 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1046 typename _H1
, typename _H2
>
1047 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1048 _Default_ranged_hash
, false>
1049 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1050 private _Hashtable_ebo_helper
<1, _H1
>,
1051 private _Hashtable_ebo_helper
<2, _H2
>
1054 typedef _Hashtable_ebo_helper
<0, _ExtractKey
> _EboExtractKey
;
1055 typedef _Hashtable_ebo_helper
<1, _H1
> _EboH1
;
1056 typedef _Hashtable_ebo_helper
<2, _H2
> _EboH2
;
1062 hash_function() const
1065 typedef std::size_t __hash_code
;
1066 typedef _Hash_node
<_Value
, false> __node_type
;
1069 // We need the default constructor for the local iterators.
1070 _Hash_code_base() = default;
1072 _Hash_code_base(const _ExtractKey
& __ex
,
1073 const _H1
& __h1
, const _H2
& __h2
,
1074 const _Default_ranged_hash
&)
1075 : _EboExtractKey(__ex
), _EboH1(__h1
), _EboH2(__h2
) { }
1078 _M_hash_code(const _Key
& __k
) const
1079 { return _M_h1()(__k
); }
1082 _M_bucket_index(const _Key
&, __hash_code __c
, std::size_t __n
) const
1083 { return _M_h2()(__c
, __n
); }
1086 _M_bucket_index(const __node_type
* __p
,
1087 std::size_t __n
) const
1088 { return _M_h2()(_M_h1()(_M_extract()(__p
->_M_v
)), __n
); }
1091 _M_store_code(__node_type
*, __hash_code
) const
1095 _M_copy_code(__node_type
*, const __node_type
*) const
1099 _M_swap(_Hash_code_base
& __x
)
1101 std::swap(_M_extract(), __x
._M_extract());
1102 std::swap(_M_h1(), __x
._M_h1());
1103 std::swap(_M_h2(), __x
._M_h2());
1107 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
1110 _M_extract() { return _EboExtractKey::_S_get(*this); }
1113 _M_h1() const { return _EboH1::_S_cget(*this); }
1116 _M_h1() { return _EboH1::_S_get(*this); }
1119 _M_h2() const { return _EboH2::_S_cget(*this); }
1122 _M_h2() { return _EboH2::_S_get(*this); }
1125 /// Specialization: hash function and range-hashing function,
1126 /// caching hash codes. H is provided but ignored. Provides
1127 /// typedef and accessor required by TR1.
1128 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1129 typename _H1
, typename _H2
>
1130 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1131 _Default_ranged_hash
, true>
1132 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1133 private _Hashtable_ebo_helper
<1, _H1
>,
1134 private _Hashtable_ebo_helper
<2, _H2
>
1137 typedef _Hashtable_ebo_helper
<0, _ExtractKey
> _EboExtractKey
;
1138 typedef _Hashtable_ebo_helper
<1, _H1
> _EboH1
;
1139 typedef _Hashtable_ebo_helper
<2, _H2
> _EboH2
;
1145 hash_function() const
1148 typedef std::size_t __hash_code
;
1149 typedef _Hash_node
<_Value
, true> __node_type
;
1152 _Hash_code_base(const _ExtractKey
& __ex
,
1153 const _H1
& __h1
, const _H2
& __h2
,
1154 const _Default_ranged_hash
&)
1155 : _EboExtractKey(__ex
), _EboH1(__h1
), _EboH2(__h2
) { }
1158 _M_hash_code(const _Key
& __k
) const
1159 { return _M_h1()(__k
); }
1162 _M_bucket_index(const _Key
&, __hash_code __c
,
1163 std::size_t __n
) const
1164 { return _M_h2()(__c
, __n
); }
1167 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1168 { return _M_h2()(__p
->_M_hash_code
, __n
); }
1171 _M_store_code(__node_type
* __n
, __hash_code __c
) const
1172 { __n
->_M_hash_code
= __c
; }
1175 _M_copy_code(__node_type
* __to
, const __node_type
* __from
) const
1176 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
1179 _M_swap(_Hash_code_base
& __x
)
1181 std::swap(_M_extract(), __x
._M_extract());
1182 std::swap(_M_h1(), __x
._M_h1());
1183 std::swap(_M_h2(), __x
._M_h2());
1187 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
1190 _M_extract() { return _EboExtractKey::_S_get(*this); }
1193 _M_h1() const { return _EboH1::_S_cget(*this); }
1196 _M_h1() { return _EboH1::_S_get(*this); }
1199 _M_h2() const { return _EboH2::_S_cget(*this); }
1202 _M_h2() { return _EboH2::_S_get(*this); }
1206 * Primary class template _Equal_helper.
1209 template <typename _Key
, typename _Value
, typename _ExtractKey
,
1210 typename _Equal
, typename _HashCodeType
,
1211 bool __cache_hash_code
>
1212 struct _Equal_helper
;
1215 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1216 typename _Equal
, typename _HashCodeType
>
1217 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, true>
1220 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1221 const _Key
& __k
, _HashCodeType __c
, _Hash_node
<_Value
, true>* __n
)
1222 { return __c
== __n
->_M_hash_code
&& __eq(__k
, __extract(__n
->_M_v
)); }
1226 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1227 typename _Equal
, typename _HashCodeType
>
1228 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, false>
1231 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1232 const _Key
& __k
, _HashCodeType
, _Hash_node
<_Value
, false>* __n
)
1233 { return __eq(__k
, __extract(__n
->_M_v
)); }
1238 * Primary class template _Local_iterator_base.
1240 * Base class for local iterators, used to iterate within a bucket
1241 * but not between buckets.
1243 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1244 typename _H1
, typename _H2
, typename _Hash
,
1245 bool __cache_hash_code
>
1246 struct _Local_iterator_base
;
1249 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1250 typename _H1
, typename _H2
, typename _Hash
>
1251 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1252 _H1
, _H2
, _Hash
, true>
1255 _Local_iterator_base() = default;
1256 _Local_iterator_base(_Hash_node
<_Value
, true>* __p
,
1257 std::size_t __bkt
, std::size_t __bkt_count
)
1258 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1263 _M_cur
= _M_cur
->_M_next();
1266 std::size_t __bkt
= _M_h2()(_M_cur
->_M_hash_code
, _M_bucket_count
);
1267 if (__bkt
!= _M_bucket
)
1272 const _H2
& _M_h2() const
1275 _Hash_node
<_Value
, true>* _M_cur
;
1276 std::size_t _M_bucket
;
1277 std::size_t _M_bucket_count
;
1281 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1282 typename _H1
, typename _H2
, typename _Hash
>
1283 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1284 _H1
, _H2
, _Hash
, false>
1285 : private _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1286 _H1
, _H2
, _Hash
, false>
1288 _Local_iterator_base() = default;
1289 _Local_iterator_base(_Hash_node
<_Value
, false>* __p
,
1290 std::size_t __bkt
, std::size_t __bkt_count
)
1291 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1296 _M_cur
= _M_cur
->_M_next();
1299 std::size_t __bkt
= this->_M_bucket_index(_M_cur
, _M_bucket_count
);
1300 if (__bkt
!= _M_bucket
)
1305 _Hash_node
<_Value
, false>* _M_cur
;
1306 std::size_t _M_bucket
;
1307 std::size_t _M_bucket_count
;
1310 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1311 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1313 operator==(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1314 _H1
, _H2
, _Hash
, __cache
>& __x
,
1315 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1316 _H1
, _H2
, _Hash
, __cache
>& __y
)
1317 { return __x
._M_cur
== __y
._M_cur
; }
1319 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1320 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1322 operator!=(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1323 _H1
, _H2
, _Hash
, __cache
>& __x
,
1324 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1325 _H1
, _H2
, _Hash
, __cache
>& __y
)
1326 { return __x
._M_cur
!= __y
._M_cur
; }
1329 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1330 typename _H1
, typename _H2
, typename _Hash
,
1331 bool __constant_iterators
, bool __cache
>
1332 struct _Local_iterator
1333 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1334 _H1
, _H2
, _Hash
, __cache
>
1336 typedef _Value value_type
;
1337 typedef typename
std::conditional
<__constant_iterators
,
1338 const _Value
*, _Value
*>::type
1340 typedef typename
std::conditional
<__constant_iterators
,
1341 const _Value
&, _Value
&>::type
1343 typedef std::ptrdiff_t difference_type
;
1344 typedef std::forward_iterator_tag iterator_category
;
1346 _Local_iterator() = default;
1349 _Local_iterator(_Hash_node
<_Value
, __cache
>* __p
,
1350 std::size_t __bkt
, std::size_t __bkt_count
)
1351 : _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1352 __cache
>(__p
, __bkt
, __bkt_count
)
1357 { return this->_M_cur
->_M_v
; }
1361 { return std::__addressof(this->_M_cur
->_M_v
); }
1373 _Local_iterator
__tmp(*this);
1379 /// local const_iterators
1380 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1381 typename _H1
, typename _H2
, typename _Hash
,
1382 bool __constant_iterators
, bool __cache
>
1383 struct _Local_const_iterator
1384 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1385 _H1
, _H2
, _Hash
, __cache
>
1387 typedef _Value value_type
;
1388 typedef const _Value
* pointer
;
1389 typedef const _Value
& reference
;
1390 typedef std::ptrdiff_t difference_type
;
1391 typedef std::forward_iterator_tag iterator_category
;
1393 _Local_const_iterator() = default;
1396 _Local_const_iterator(_Hash_node
<_Value
, __cache
>* __p
,
1397 std::size_t __bkt
, std::size_t __bkt_count
)
1398 : _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1399 __cache
>(__p
, __bkt
, __bkt_count
)
1402 _Local_const_iterator(const _Local_iterator
<_Key
, _Value
, _ExtractKey
,
1404 __constant_iterators
,
1406 : _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1407 __cache
>(__x
._M_cur
, __x
._M_bucket
,
1408 __x
._M_bucket_count
)
1413 { return this->_M_cur
->_M_v
; }
1417 { return std::__addressof(this->_M_cur
->_M_v
); }
1419 _Local_const_iterator
&
1426 _Local_const_iterator
1429 _Local_const_iterator
__tmp(*this);
1436 * Primary class template _Hashtable_base.
1438 * Helper class adding management of _Equal functor to
1439 * _Hash_code_base type.
1441 * Base class templates are:
1442 * - __detail::_Hash_code_base
1443 * - __detail::_Hashtable_ebo_helper
1445 template<typename _Key
, typename _Value
,
1446 typename _ExtractKey
, typename _Equal
,
1447 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
1448 struct _Hashtable_base
1449 : public _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1450 _Traits::__hash_cached::value
>,
1451 private _Hashtable_ebo_helper
<0, _Equal
>
1454 typedef _Key key_type
;
1455 typedef _Value value_type
;
1456 typedef _Equal key_equal
;
1457 typedef std::size_t size_type
;
1458 typedef std::ptrdiff_t difference_type
;
1460 using __traits_type
= _Traits
;
1461 using __hash_cached
= typename
__traits_type::__hash_cached
;
1462 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
1463 using __unique_keys
= typename
__traits_type::__unique_keys
;
1465 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1467 __hash_cached::value
>;
1469 using __hash_code
= typename
__hash_code_base::__hash_code
;
1470 using __node_type
= typename
__hash_code_base::__node_type
;
1472 using iterator
= __detail::_Node_iterator
<value_type
,
1473 __constant_iterators::value
,
1474 __hash_cached::value
>;
1476 using const_iterator
= __detail::_Node_const_iterator
<value_type
,
1477 __constant_iterators::value
,
1478 __hash_cached::value
>;
1480 using local_iterator
= __detail::_Local_iterator
<key_type
, value_type
,
1481 _ExtractKey
, _H1
, _H2
, _Hash
,
1482 __constant_iterators::value
,
1483 __hash_cached::value
>;
1485 using const_local_iterator
= __detail::_Local_const_iterator
<key_type
,
1487 _ExtractKey
, _H1
, _H2
, _Hash
,
1488 __constant_iterators::value
,
1489 __hash_cached::value
>;
1491 using __ireturn_type
= typename
std::conditional
<__unique_keys::value
,
1492 std::pair
<iterator
, bool>,
1495 using __iconv_type
= typename
std::conditional
<__unique_keys::value
,
1496 _Select1st
, _Identity
1499 using _EqualEBO
= _Hashtable_ebo_helper
<0, _Equal
>;
1500 using _EqualHelper
= _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
,
1501 __hash_code
, __hash_cached::value
>;
1504 using __node_base
= __detail::_Hash_node_base
;
1505 using __bucket_type
= __node_base
*;
1507 _Hashtable_base(const _ExtractKey
& __ex
, const _H1
& __h1
, const _H2
& __h2
,
1508 const _Hash
& __hash
, const _Equal
& __eq
)
1509 : __hash_code_base(__ex
, __h1
, __h2
, __hash
), _EqualEBO(__eq
)
1513 _M_equals(const _Key
& __k
, __hash_code __c
, __node_type
* __n
) const
1515 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
1520 _M_swap(_Hashtable_base
& __x
)
1522 __hash_code_base::_M_swap(__x
);
1523 std::swap(_M_eq(), __x
._M_eq());
1527 _M_eq() const { return _EqualEBO::_S_cget(*this); }
1530 _M_eq() { return _EqualEBO::_S_get(*this); }
1534 * struct _Equality_base.
1536 * Common types and functions for class _Equality.
1538 struct _Equality_base
1541 template<typename _Uiterator
>
1543 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
1546 // See std::is_permutation in N3068.
1547 template<typename _Uiterator
>
1550 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
1551 _Uiterator __first2
)
1553 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1554 if (!(*__first1
== *__first2
))
1557 if (__first1
== __last1
)
1560 _Uiterator __last2
= __first2
;
1561 std::advance(__last2
, std::distance(__first1
, __last1
));
1563 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
1565 _Uiterator __tmp
= __first1
;
1566 while (__tmp
!= __it1
&& !bool(*__tmp
== *__it1
))
1569 // We've seen this one before.
1573 std::ptrdiff_t __n2
= 0;
1574 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
1575 if (*__tmp
== *__it1
)
1581 std::ptrdiff_t __n1
= 0;
1582 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
1583 if (*__tmp
== *__it1
)
1593 * Primary class template _Equality.
1595 * This is for implementing equality comparison for unordered
1596 * containers, per N3068, by John Lakos and Pablo Halpern.
1597 * Algorithmically, we follow closely the reference implementations
1600 template<typename _Key
, typename _Value
, typename _Alloc
,
1601 typename _ExtractKey
, typename _Equal
,
1602 typename _H1
, typename _H2
, typename _Hash
,
1603 typename _RehashPolicy
, typename _Traits
,
1604 bool _Unique_keys
= _Traits::__unique_keys::value
>
1608 template<typename _Key
, typename _Value
, typename _Alloc
,
1609 typename _ExtractKey
, typename _Equal
,
1610 typename _H1
, typename _H2
, typename _Hash
,
1611 typename _RehashPolicy
, typename _Traits
>
1612 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1613 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
1615 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1616 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1619 _M_equal(const __hashtable
&) const;
1622 template<typename _Key
, typename _Value
, typename _Alloc
,
1623 typename _ExtractKey
, typename _Equal
,
1624 typename _H1
, typename _H2
, typename _Hash
,
1625 typename _RehashPolicy
, typename _Traits
>
1627 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1628 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
1629 _M_equal(const __hashtable
& __other
) const
1631 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1633 if (__this
->size() != __other
.size())
1636 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
1638 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
1639 if (__ity
== __other
.end() || !bool(*__ity
== *__itx
))
1646 template<typename _Key
, typename _Value
, typename _Alloc
,
1647 typename _ExtractKey
, typename _Equal
,
1648 typename _H1
, typename _H2
, typename _Hash
,
1649 typename _RehashPolicy
, typename _Traits
>
1650 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1651 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
1652 : public _Equality_base
1654 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1655 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1658 _M_equal(const __hashtable
&) const;
1661 template<typename _Key
, typename _Value
, typename _Alloc
,
1662 typename _ExtractKey
, typename _Equal
,
1663 typename _H1
, typename _H2
, typename _Hash
,
1664 typename _RehashPolicy
, typename _Traits
>
1666 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1667 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>::
1668 _M_equal(const __hashtable
& __other
) const
1670 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1672 if (__this
->size() != __other
.size())
1675 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
1677 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
1678 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
1680 if (std::distance(__xrange
.first
, __xrange
.second
)
1681 != std::distance(__yrange
.first
, __yrange
.second
))
1684 if (!_S_is_permutation(__xrange
.first
, __xrange
.second
,
1688 __itx
= __xrange
.second
;
1694 * This type is to combine a _Hash_node_base instance with an allocator
1695 * instance through inheritance to benefit from EBO when possible.
1697 template<typename _NodeAlloc
>
1698 struct _Before_begin
: public _NodeAlloc
1700 _Hash_node_base _M_node
;
1702 _Before_begin(const _Before_begin
&) = default;
1703 _Before_begin(_Before_begin
&&) = default;
1705 template<typename _Alloc
>
1706 _Before_begin(_Alloc
&& __a
)
1707 : _NodeAlloc(std::forward
<_Alloc
>(__a
))
1711 //@} hashtable-detail
1712 _GLIBCXX_END_NAMESPACE_VERSION
1713 } // namespace __detail
1716 #endif // _HASHTABLE_POLICY_H