1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010, 2011 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)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 // Helper function: return distance(first, last) for forward
41 // iterators, or 0 for input iterators.
42 template<class _Iterator>
43 inline typename std::iterator_traits<_Iterator>::difference_type
44 __distance_fw(_Iterator __first, _Iterator __last,
45 std::input_iterator_tag)
48 template<class _Iterator>
49 inline typename std::iterator_traits<_Iterator>::difference_type
50 __distance_fw(_Iterator __first, _Iterator __last,
51 std::forward_iterator_tag)
52 { return std::distance(__first, __last); }
54 template<class _Iterator>
55 inline typename std::iterator_traits<_Iterator>::difference_type
56 __distance_fw(_Iterator __first, _Iterator __last)
58 typedef typename std::iterator_traits<_Iterator>::iterator_category _Tag;
59 return __distance_fw(__first, __last, _Tag());
62 // Helper type used to detect when the hash functor is noexcept qualified or
64 template <typename _Key, typename _Hash>
65 struct __is_noexcept_hash : std::integral_constant<bool,
66 noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
69 // Auxiliary types used for all instantiations of _Hashtable: nodes
72 // Nodes, used to wrap elements stored in the hash table. A policy
73 // template parameter of class template _Hashtable controls whether
74 // nodes also store a hash code. In some cases (e.g. strings) this
75 // may be a performance win.
76 template<typename _Value, bool __cache_hash_code>
79 template<typename _Value>
80 struct _Hash_node<_Value, true>
83 std::size_t _M_hash_code;
86 template<typename... _Args>
87 _Hash_node(_Args&&... __args)
88 : _M_v(std::forward<_Args>(__args)...),
89 _M_hash_code(), _M_next() { }
92 template<typename _Value>
93 struct _Hash_node<_Value, false>
98 template<typename... _Args>
99 _Hash_node(_Args&&... __args)
100 : _M_v(std::forward<_Args>(__args)...),
104 // Node iterators, used to iterate through all the hashtable.
105 template<typename _Value, bool __cache>
106 struct _Node_iterator_base
108 _Node_iterator_base(_Hash_node<_Value, __cache>* __p)
113 { _M_cur = _M_cur->_M_next; }
115 _Hash_node<_Value, __cache>* _M_cur;
118 template<typename _Value, bool __cache>
120 operator==(const _Node_iterator_base<_Value, __cache>& __x,
121 const _Node_iterator_base<_Value, __cache>& __y)
122 { return __x._M_cur == __y._M_cur; }
124 template<typename _Value, bool __cache>
126 operator!=(const _Node_iterator_base<_Value, __cache>& __x,
127 const _Node_iterator_base<_Value, __cache>& __y)
128 { return __x._M_cur != __y._M_cur; }
130 template<typename _Value, bool __constant_iterators, bool __cache>
131 struct _Node_iterator
132 : public _Node_iterator_base<_Value, __cache>
134 typedef _Value value_type;
135 typedef typename std::conditional<__constant_iterators,
136 const _Value*, _Value*>::type
138 typedef typename std::conditional<__constant_iterators,
139 const _Value&, _Value&>::type
141 typedef std::ptrdiff_t difference_type;
142 typedef std::forward_iterator_tag iterator_category;
145 : _Node_iterator_base<_Value, __cache>(0) { }
148 _Node_iterator(_Hash_node<_Value, __cache>* __p)
149 : _Node_iterator_base<_Value, __cache>(__p) { }
153 { return this->_M_cur->_M_v; }
157 { return std::__addressof(this->_M_cur->_M_v); }
169 _Node_iterator __tmp(*this);
175 template<typename _Value, bool __constant_iterators, bool __cache>
176 struct _Node_const_iterator
177 : public _Node_iterator_base<_Value, __cache>
179 typedef _Value value_type;
180 typedef const _Value* pointer;
181 typedef const _Value& reference;
182 typedef std::ptrdiff_t difference_type;
183 typedef std::forward_iterator_tag iterator_category;
185 _Node_const_iterator()
186 : _Node_iterator_base<_Value, __cache>(0) { }
189 _Node_const_iterator(_Hash_node<_Value, __cache>* __p)
190 : _Node_iterator_base<_Value, __cache>(__p) { }
192 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
194 : _Node_iterator_base<_Value, __cache>(__x._M_cur) { }
198 { return this->_M_cur->_M_v; }
202 { return std::__addressof(this->_M_cur->_M_v); }
204 _Node_const_iterator&
214 _Node_const_iterator __tmp(*this);
220 // Many of class template _Hashtable's template parameters are policy
221 // classes. These are defaults for the policies.
223 // Default range hashing function: use division to fold a large number
224 // into the range [0, N).
225 struct _Mod_range_hashing
227 typedef std::size_t first_argument_type;
228 typedef std::size_t second_argument_type;
229 typedef std::size_t result_type;
232 operator()(first_argument_type __num, second_argument_type __den) const
233 { return __num % __den; }
236 // Default ranged hash function H. In principle it should be a
237 // function object composed from objects of type H1 and H2 such that
238 // h(k, N) = h2(h1(k), N), but that would mean making extra copies of
239 // h1 and h2. So instead we'll just use a tag to tell class template
240 // hashtable to do that composition.
241 struct _Default_ranged_hash { };
243 // Default value for rehash policy. Bucket size is (usually) the
244 // smallest prime that keeps the load factor small enough.
245 struct _Prime_rehash_policy
247 _Prime_rehash_policy(float __z = 1.0)
248 : _M_max_load_factor(__z), _M_prev_resize(0), _M_next_resize(0) { }
251 max_load_factor() const noexcept
252 { return _M_max_load_factor; }
254 // Return a bucket size no smaller than n.
256 _M_next_bkt(std::size_t __n) const;
258 // Return a bucket count appropriate for n elements
260 _M_bkt_for_elements(std::size_t __n) const;
262 // __n_bkt is current bucket count, __n_elt is current element count,
263 // and __n_ins is number of elements to be inserted. Do we need to
264 // increase bucket count? If so, return make_pair(true, n), where n
265 // is the new bucket count. If not, return make_pair(false, 0).
266 std::pair<bool, std::size_t>
267 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
268 std::size_t __n_ins) const;
270 typedef std::pair<std::size_t, std::size_t> _State;
274 { return std::make_pair(_M_prev_resize, _M_next_resize); }
277 _M_reset(const _State& __state)
279 _M_prev_resize = __state.first;
280 _M_next_resize = __state.second;
283 enum { _S_n_primes = sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
285 float _M_max_load_factor;
286 mutable std::size_t _M_prev_resize;
287 mutable std::size_t _M_next_resize;
290 extern const unsigned long __prime_list[];
292 // XXX This is a hack. There's no good reason for any of
293 // _Prime_rehash_policy's member functions to be inline.
295 // Return a prime no smaller than n.
297 _Prime_rehash_policy::
298 _M_next_bkt(std::size_t __n) const
300 // Optimize lookups involving the first elements of __prime_list.
301 // (useful to speed-up, eg, constructors)
302 static const unsigned char __fast_bkt[12]
303 = { 2, 2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11 };
309 = __builtin_ceil(__fast_bkt[__n] * (long double)_M_max_load_factor);
310 return __fast_bkt[__n];
313 const unsigned long* __p
314 = std::lower_bound(__prime_list + 5, __prime_list + _S_n_primes, __n);
316 // Shrink will take place only if the number of elements is small enough
317 // so that the prime number 2 steps before __p is large enough to still
318 // conform to the max load factor:
320 = __builtin_floor(*(__p - 2) * (long double)_M_max_load_factor);
322 // Let's guaranty that a minimal grow step of 11 is used
324 __p = std::lower_bound(__p, __prime_list + _S_n_primes, __n + 11);
325 _M_next_resize = __builtin_ceil(*__p * (long double)_M_max_load_factor);
329 // Return the smallest prime p such that alpha p >= n, where alpha
330 // is the load factor.
332 _Prime_rehash_policy::
333 _M_bkt_for_elements(std::size_t __n) const
334 { return _M_next_bkt(__builtin_ceil(__n / (long double)_M_max_load_factor)); }
336 // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
337 // If p > __n_bkt, return make_pair(true, p); otherwise return
338 // make_pair(false, 0). In principle this isn't very different from
339 // _M_bkt_for_elements.
341 // The only tricky part is that we're caching the element count at
342 // which we need to rehash, so we don't have to do a floating-point
343 // multiply for every insertion.
345 inline std::pair<bool, std::size_t>
346 _Prime_rehash_policy::
347 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
348 std::size_t __n_ins) const
350 if (__n_elt + __n_ins >= _M_next_resize)
352 long double __min_bkts = (__n_elt + __n_ins)
353 / (long double)_M_max_load_factor;
354 if (__min_bkts >= __n_bkt)
355 return std::make_pair(true,
356 _M_next_bkt(__builtin_floor(__min_bkts) + 1));
360 = __builtin_floor(__n_bkt * (long double)_M_max_load_factor);
361 return std::make_pair(false, 0);
364 else if (__n_elt + __n_ins < _M_prev_resize)
366 long double __min_bkts = (__n_elt + __n_ins)
367 / (long double)_M_max_load_factor;
368 return std::make_pair(true,
369 _M_next_bkt(__builtin_floor(__min_bkts) + 1));
372 return std::make_pair(false, 0);
375 // Base classes for std::_Hashtable. We define these base classes
376 // because in some cases we want to do different things depending
377 // on the value of a policy class. In some cases the policy class
378 // affects which member functions and nested typedefs are defined;
379 // we handle that by specializing base class templates. Several of
380 // the base class templates need to access other members of class
381 // template _Hashtable, so we use the "curiously recurring template
382 // pattern" for them.
384 // class template _Map_base. If the hashtable has a value type of
385 // the form pair<T1, T2> and a key extraction policy that returns the
386 // first part of the pair, the hashtable gets a mapped_type typedef.
387 // If it satisfies those criteria and also has unique keys, then it
388 // also gets an operator[].
389 template<typename _Key, typename _Value, typename _Ex, bool __unique,
391 struct _Map_base { };
393 template<typename _Key, typename _Pair, typename _Hashtable>
394 struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, false, _Hashtable>
396 typedef typename _Pair::second_type mapped_type;
399 template<typename _Key, typename _Pair, typename _Hashtable>
400 struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>
402 typedef typename _Pair::second_type mapped_type;
405 operator[](const _Key& __k);
408 operator[](_Key&& __k);
410 // _GLIBCXX_RESOLVE_LIB_DEFECTS
411 // DR 761. unordered_map needs an at() member function.
416 at(const _Key& __k) const;
419 template<typename _Key, typename _Pair, typename _Hashtable>
420 typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
421 true, _Hashtable>::mapped_type&
422 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
423 operator[](const _Key& __k)
425 _Hashtable* __h = static_cast<_Hashtable*>(this);
426 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
427 std::size_t __n = __h->_M_bucket_index(__k, __code);
429 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
431 return __h->_M_insert_bucket(std::make_pair(__k, mapped_type()),
432 __n, __code)->second;
433 return (__p->_M_v).second;
436 template<typename _Key, typename _Pair, typename _Hashtable>
437 typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
438 true, _Hashtable>::mapped_type&
439 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
440 operator[](_Key&& __k)
442 _Hashtable* __h = static_cast<_Hashtable*>(this);
443 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
444 std::size_t __n = __h->_M_bucket_index(__k, __code);
446 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
448 return __h->_M_insert_bucket(std::make_pair(std::move(__k),
450 __n, __code)->second;
451 return (__p->_M_v).second;
454 template<typename _Key, typename _Pair, typename _Hashtable>
455 typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
456 true, _Hashtable>::mapped_type&
457 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
460 _Hashtable* __h = static_cast<_Hashtable*>(this);
461 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
462 std::size_t __n = __h->_M_bucket_index(__k, __code);
464 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
466 __throw_out_of_range(__N("_Map_base::at"));
467 return (__p->_M_v).second;
470 template<typename _Key, typename _Pair, typename _Hashtable>
471 const typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
472 true, _Hashtable>::mapped_type&
473 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
474 at(const _Key& __k) const
476 const _Hashtable* __h = static_cast<const _Hashtable*>(this);
477 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
478 std::size_t __n = __h->_M_bucket_index(__k, __code);
480 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
482 __throw_out_of_range(__N("_Map_base::at"));
483 return (__p->_M_v).second;
486 // class template _Rehash_base. Give hashtable the max_load_factor
487 // functions and reserve iff the rehash policy is _Prime_rehash_policy.
488 template<typename _RehashPolicy, typename _Hashtable>
489 struct _Rehash_base { };
491 template<typename _Hashtable>
492 struct _Rehash_base<_Prime_rehash_policy, _Hashtable>
495 max_load_factor() const noexcept
497 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
498 return __this->__rehash_policy().max_load_factor();
502 max_load_factor(float __z)
504 _Hashtable* __this = static_cast<_Hashtable*>(this);
505 __this->__rehash_policy(_Prime_rehash_policy(__z));
509 reserve(std::size_t __n)
511 _Hashtable* __this = static_cast<_Hashtable*>(this);
512 __this->rehash(__builtin_ceil(__n / max_load_factor()));
516 // Helper class using EBO when it is not forbidden, type is not final,
517 // and when it worth it, type is empty.
518 template<int _N, typename _Tp,
519 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
522 // Specialization using EBO
523 template<int _N, typename _Tp>
524 struct _Ebo_helper<_N, _Tp, true> : _Tp
526 _Ebo_helper() = default;
527 _Ebo_helper(const _Tp& __tp) : _Tp(__tp)
531 _S_cget(const _Ebo_helper<_N, _Tp, true>& __eboh)
532 { return static_cast<const _Tp&>(__eboh); }
535 _S_get(_Ebo_helper<_N, _Tp, true>& __eboh)
536 { return static_cast<_Tp&>(__eboh); }
539 // Specialization not using EBO
540 template<int _N, typename _Tp>
541 struct _Ebo_helper<_N, _Tp, false>
543 _Ebo_helper() = default;
544 _Ebo_helper(const _Tp& __tp) : m_tp(__tp)
548 _S_cget(const _Ebo_helper<_N, _Tp, false>& __eboh)
549 { return __eboh.m_tp; }
552 _S_get(_Ebo_helper<_N, _Tp, false>& __eboh)
553 { return __eboh.m_tp; }
559 // Class template _Hash_code_base. Encapsulates two policy issues that
560 // aren't quite orthogonal.
561 // (1) the difference between using a ranged hash function and using
562 // the combination of a hash function and a range-hashing function.
563 // In the former case we don't have such things as hash codes, so
564 // we have a dummy type as placeholder.
565 // (2) Whether or not we cache hash codes. Caching hash codes is
566 // meaningless if we have a ranged hash function.
567 // We also put the key extraction objects here, for convenience.
569 // Each specialization derives from one or more of the template parameters to
570 // benefit from Ebo. This is important as this type is inherited in some cases
571 // by the _Local_iterator_base type used to implement local_iterator and
572 // const_local_iterator. As with any iterator type we prefer to make it as
573 // small as possible.
575 // Primary template: unused except as a hook for specializations.
576 template<typename _Key, typename _Value, typename _ExtractKey,
577 typename _H1, typename _H2, typename _Hash,
578 bool __cache_hash_code>
579 struct _Hash_code_base;
581 // Specialization: ranged hash function, no caching hash codes. H1
582 // and H2 are provided but ignored. We define a dummy hash code type.
583 template<typename _Key, typename _Value, typename _ExtractKey,
584 typename _H1, typename _H2, typename _Hash>
585 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
586 : _Ebo_helper<0, _ExtractKey>, _Ebo_helper<1, _Hash>
589 typedef _Ebo_helper<0, _ExtractKey> _EboExtractKey;
590 typedef _Ebo_helper<1, _Hash> _EboHash;
592 // We need the default constructor for the local iterators.
593 _Hash_code_base() = default;
594 _Hash_code_base(const _ExtractKey& __ex,
595 const _H1&, const _H2&, const _Hash& __h)
596 : _EboExtractKey(__ex), _EboHash(__h) { }
598 typedef void* _Hash_code_type;
601 _M_hash_code(const _Key& __key) const
605 _M_bucket_index(const _Key& __k, _Hash_code_type,
606 std::size_t __n) const
607 { return _M_ranged_hash()(__k, __n); }
610 _M_bucket_index(const _Hash_node<_Value, false>* __p,
611 std::size_t __n) const
612 { return _M_ranged_hash()(_M_extract()(__p->_M_v), __n); }
615 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
619 _M_copy_code(_Hash_node<_Value, false>*,
620 const _Hash_node<_Value, false>*) const
624 _M_swap(_Hash_code_base& __x)
626 std::swap(_M_extract(), __x._M_extract());
627 std::swap(_M_ranged_hash(), __x._M_ranged_hash());
632 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
634 _M_extract() { return _EboExtractKey::_S_get(*this); }
636 _M_ranged_hash() const { return _EboHash::_S_cget(*this); }
638 _M_ranged_hash() { return _EboHash::_S_get(*this); }
641 // No specialization for ranged hash function while caching hash codes.
642 // That combination is meaningless, and trying to do it is an error.
644 // Specialization: ranged hash function, cache hash codes. This
645 // combination is meaningless, so we provide only a declaration
646 // and no definition.
647 template<typename _Key, typename _Value, typename _ExtractKey,
648 typename _H1, typename _H2, typename _Hash>
649 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
651 // Specialization: hash function and range-hashing function, no
652 // caching of hash codes.
653 // Provides typedef and accessor required by TR1.
654 template<typename _Key, typename _Value, typename _ExtractKey,
655 typename _H1, typename _H2>
656 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
657 _Default_ranged_hash, false>
658 : _Ebo_helper<0, _ExtractKey>, _Ebo_helper<1, _H1>, _Ebo_helper<2, _H2>
661 typedef _Ebo_helper<0, _ExtractKey> _EboExtractKey;
662 typedef _Ebo_helper<1, _H1> _EboH1;
663 typedef _Ebo_helper<2, _H2> _EboH2;
669 hash_function() const
673 // We need the default constructor for the local iterators.
674 _Hash_code_base() = default;
675 _Hash_code_base(const _ExtractKey& __ex,
676 const _H1& __h1, const _H2& __h2,
677 const _Default_ranged_hash&)
678 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
680 typedef std::size_t _Hash_code_type;
683 _M_hash_code(const _Key& __k) const
684 { return _M_h1()(__k); }
687 _M_bucket_index(const _Key&, _Hash_code_type __c,
688 std::size_t __n) const
689 { return _M_h2()(__c, __n); }
692 _M_bucket_index(const _Hash_node<_Value, false>* __p,
693 std::size_t __n) const
694 { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v)), __n); }
697 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
701 _M_copy_code(_Hash_node<_Value, false>*,
702 const _Hash_node<_Value, false>*) const
706 _M_swap(_Hash_code_base& __x)
708 std::swap(_M_extract(), __x._M_extract());
709 std::swap(_M_h1(), __x._M_h1());
710 std::swap(_M_h2(), __x._M_h2());
715 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
717 _M_extract() { return _EboExtractKey::_S_get(*this); }
719 _M_h1() const { return _EboH1::_S_cget(*this); }
721 _M_h1() { return _EboH1::_S_get(*this); }
723 _M_h2() const { return _EboH2::_S_cget(*this); }
725 _M_h2() { return _EboH2::_S_get(*this); }
728 // Specialization: hash function and range-hashing function,
729 // caching hash codes. H is provided but ignored. Provides
730 // typedef and accessor required by TR1.
731 template<typename _Key, typename _Value, typename _ExtractKey,
732 typename _H1, typename _H2>
733 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
734 _Default_ranged_hash, true>
735 : _Ebo_helper<0, _ExtractKey>, _Ebo_helper<1, _H1>, _Ebo_helper<2, _H2>
738 typedef _Ebo_helper<0, _ExtractKey> _EboExtractKey;
739 typedef _Ebo_helper<1, _H1> _EboH1;
740 typedef _Ebo_helper<2, _H2> _EboH2;
746 hash_function() const
750 _Hash_code_base(const _ExtractKey& __ex,
751 const _H1& __h1, const _H2& __h2,
752 const _Default_ranged_hash&)
753 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
755 typedef std::size_t _Hash_code_type;
758 _M_hash_code(const _Key& __k) const
759 { return _M_h1()(__k); }
762 _M_bucket_index(const _Key&, _Hash_code_type __c,
763 std::size_t __n) const
764 { return _M_h2()(__c, __n); }
767 _M_bucket_index(const _Hash_node<_Value, true>* __p,
768 std::size_t __n) const
769 { return _M_h2()(__p->_M_hash_code, __n); }
772 _M_store_code(_Hash_node<_Value, true>* __n, _Hash_code_type __c) const
773 { __n->_M_hash_code = __c; }
776 _M_copy_code(_Hash_node<_Value, true>* __to,
777 const _Hash_node<_Value, true>* __from) const
778 { __to->_M_hash_code = __from->_M_hash_code; }
781 _M_swap(_Hash_code_base& __x)
783 std::swap(_M_extract(), __x._M_extract());
784 std::swap(_M_h1(), __x._M_h1());
785 std::swap(_M_h2(), __x._M_h2());
790 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
792 _M_extract() { return _EboExtractKey::_S_get(*this); }
794 _M_h1() const { return _EboH1::_S_cget(*this); }
796 _M_h1() { return _EboH1::_S_get(*this); }
798 _M_h2() const { return _EboH2::_S_cget(*this); }
800 _M_h2() { return _EboH2::_S_get(*this); }
803 template <typename _Key, typename _Value, typename _ExtractKey,
804 typename _Equal, typename _HashCodeType,
805 bool __cache_hash_code>
806 struct _Equal_helper;
808 template<typename _Key, typename _Value, typename _ExtractKey,
809 typename _Equal, typename _HashCodeType>
810 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
813 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
814 const _Key& __k, _HashCodeType __c,
815 _Hash_node<_Value, true>* __n)
816 { return __c == __n->_M_hash_code
817 && __eq(__k, __extract(__n->_M_v)); }
820 template<typename _Key, typename _Value, typename _ExtractKey,
821 typename _Equal, typename _HashCodeType>
822 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
825 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
826 const _Key& __k, _HashCodeType,
827 _Hash_node<_Value, false>* __n)
828 { return __eq(__k, __extract(__n->_M_v)); }
831 // Helper class adding management of _Equal functor to _Hash_code_base
833 template<typename _Key, typename _Value,
834 typename _ExtractKey, typename _Equal,
835 typename _H1, typename _H2, typename _Hash,
836 bool __cache_hash_code>
837 struct _Hashtable_base
838 : _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
840 _Ebo_helper<0, _Equal>
843 typedef _Ebo_helper<0, _Equal> _EboEqual;
846 typedef _Hash_code_base<_Key, _Value, _ExtractKey,
847 _H1, _H2, _Hash, __cache_hash_code> _HCBase;
848 typedef typename _HCBase::_Hash_code_type _Hash_code_type;
850 _Hashtable_base(const _ExtractKey& __ex,
851 const _H1& __h1, const _H2& __h2,
852 const _Hash& __hash, const _Equal& __eq)
853 : _HCBase(__ex, __h1, __h2, __hash), _EboEqual(__eq) { }
856 _M_equals(const _Key& __k, _Hash_code_type __c,
857 _Hash_node<_Value, __cache_hash_code>* __n) const
859 typedef _Equal_helper<_Key, _Value, _ExtractKey,
860 _Equal, _Hash_code_type,
861 __cache_hash_code> _EqualHelper;
862 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(), __k, __c, __n);
866 _M_swap(_Hashtable_base& __x)
868 _HCBase::_M_swap(__x);
869 std::swap(_M_eq(), __x._M_eq());
874 _M_eq() const { return _EboEqual::_S_cget(*this); }
876 _M_eq() { return _EboEqual::_S_get(*this); }
879 // Local iterators, used to iterate within a bucket but not between
881 template<typename _Key, typename _Value, typename _ExtractKey,
882 typename _H1, typename _H2, typename _Hash,
883 bool __cache_hash_code>
884 struct _Local_iterator_base;
886 template<typename _Key, typename _Value, typename _ExtractKey,
887 typename _H1, typename _H2, typename _Hash>
888 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
889 _H1, _H2, _Hash, true>
892 _Local_iterator_base() = default;
893 _Local_iterator_base(_Hash_node<_Value, true>* __p,
894 std::size_t __bkt, std::size_t __bkt_count)
895 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
900 _M_cur = _M_cur->_M_next;
903 std::size_t __bkt = _M_h2()(_M_cur->_M_hash_code, _M_bucket_count);
904 if (__bkt != _M_bucket)
909 const _H2& _M_h2() const
912 _Hash_node<_Value, true>* _M_cur;
913 std::size_t _M_bucket;
914 std::size_t _M_bucket_count;
917 template<typename _Key, typename _Value, typename _ExtractKey,
918 typename _H1, typename _H2, typename _Hash>
919 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
920 _H1, _H2, _Hash, false>
921 : _Hash_code_base<_Key, _Value, _ExtractKey,
922 _H1, _H2, _Hash, false>
924 _Local_iterator_base() = default;
925 _Local_iterator_base(_Hash_node<_Value, false>* __p,
926 std::size_t __bkt, std::size_t __bkt_count)
927 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
932 _M_cur = _M_cur->_M_next;
935 std::size_t __bkt = this->_M_bucket_index(_M_cur, _M_bucket_count);
936 if (__bkt != _M_bucket)
941 _Hash_node<_Value, false>* _M_cur;
942 std::size_t _M_bucket;
943 std::size_t _M_bucket_count;
946 template<typename _Key, typename _Value, typename _ExtractKey,
947 typename _H1, typename _H2, typename _Hash, bool __cache>
949 operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
950 _H1, _H2, _Hash, __cache>& __x,
951 const _Local_iterator_base<_Key, _Value, _ExtractKey,
952 _H1, _H2, _Hash, __cache>& __y)
953 { return __x._M_cur == __y._M_cur; }
955 template<typename _Key, typename _Value, typename _ExtractKey,
956 typename _H1, typename _H2, typename _Hash, bool __cache>
958 operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
959 _H1, _H2, _Hash, __cache>& __x,
960 const _Local_iterator_base<_Key, _Value, _ExtractKey,
961 _H1, _H2, _Hash, __cache>& __y)
962 { return __x._M_cur != __y._M_cur; }
964 template<typename _Key, typename _Value, typename _ExtractKey,
965 typename _H1, typename _H2, typename _Hash,
966 bool __constant_iterators, bool __cache>
967 struct _Local_iterator
968 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
969 _H1, _H2, _Hash, __cache>
971 typedef _Value value_type;
972 typedef typename std::conditional<__constant_iterators,
973 const _Value*, _Value*>::type
975 typedef typename std::conditional<__constant_iterators,
976 const _Value&, _Value&>::type
978 typedef std::ptrdiff_t difference_type;
979 typedef std::forward_iterator_tag iterator_category;
981 _Local_iterator() = default;
984 _Local_iterator(_Hash_node<_Value, __cache>* __p,
985 std::size_t __bkt, std::size_t __bkt_count)
986 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
987 __cache>(__p, __bkt, __bkt_count)
992 { return this->_M_cur->_M_v; }
996 { return std::__addressof(this->_M_cur->_M_v); }
1008 _Local_iterator __tmp(*this);
1014 template<typename _Key, typename _Value, typename _ExtractKey,
1015 typename _H1, typename _H2, typename _Hash,
1016 bool __constant_iterators, bool __cache>
1017 struct _Local_const_iterator
1018 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1019 _H1, _H2, _Hash, __cache>
1021 typedef _Value value_type;
1022 typedef const _Value* pointer;
1023 typedef const _Value& reference;
1024 typedef std::ptrdiff_t difference_type;
1025 typedef std::forward_iterator_tag iterator_category;
1027 _Local_const_iterator() = default;
1030 _Local_const_iterator(_Hash_node<_Value, __cache>* __p,
1031 std::size_t __bkt, std::size_t __bkt_count)
1032 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1033 __cache>(__p, __bkt, __bkt_count)
1036 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1038 __constant_iterators,
1040 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1041 __cache>(__x._M_cur, __x._M_bucket,
1042 __x._M_bucket_count)
1047 { return this->_M_cur->_M_v; }
1051 { return std::__addressof(this->_M_cur->_M_v); }
1053 _Local_const_iterator&
1060 _Local_const_iterator
1063 _Local_const_iterator __tmp(*this);
1070 // Class template _Equality_base. This is for implementing equality
1071 // comparison for unordered containers, per N3068, by John Lakos and
1072 // Pablo Halpern. Algorithmically, we follow closely the reference
1073 // implementations therein.
1074 template<typename _ExtractKey, bool __unique_keys,
1075 typename _Hashtable>
1076 struct _Equality_base;
1078 template<typename _ExtractKey, typename _Hashtable>
1079 struct _Equality_base<_ExtractKey, true, _Hashtable>
1081 bool _M_equal(const _Hashtable&) const;
1084 template<typename _ExtractKey, typename _Hashtable>
1086 _Equality_base<_ExtractKey, true, _Hashtable>::
1087 _M_equal(const _Hashtable& __other) const
1089 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1091 if (__this->size() != __other.size())
1094 for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
1096 const auto __ity = __other.find(_ExtractKey()(*__itx));
1097 if (__ity == __other.end() || *__ity != *__itx)
1103 template<typename _ExtractKey, typename _Hashtable>
1104 struct _Equality_base<_ExtractKey, false, _Hashtable>
1106 bool _M_equal(const _Hashtable&) const;
1109 template<typename _Uiterator>
1111 _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
1114 // See std::is_permutation in N3068.
1115 template<typename _ExtractKey, typename _Hashtable>
1116 template<typename _Uiterator>
1118 _Equality_base<_ExtractKey, false, _Hashtable>::
1119 _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
1120 _Uiterator __first2)
1122 for (; __first1 != __last1; ++__first1, ++__first2)
1123 if (!(*__first1 == *__first2))
1126 if (__first1 == __last1)
1129 _Uiterator __last2 = __first2;
1130 std::advance(__last2, std::distance(__first1, __last1));
1132 for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
1134 _Uiterator __tmp = __first1;
1135 while (__tmp != __it1 && !(*__tmp == *__it1))
1138 // We've seen this one before.
1142 std::ptrdiff_t __n2 = 0;
1143 for (__tmp = __first2; __tmp != __last2; ++__tmp)
1144 if (*__tmp == *__it1)
1150 std::ptrdiff_t __n1 = 0;
1151 for (__tmp = __it1; __tmp != __last1; ++__tmp)
1152 if (*__tmp == *__it1)
1161 template<typename _ExtractKey, typename _Hashtable>
1163 _Equality_base<_ExtractKey, false, _Hashtable>::
1164 _M_equal(const _Hashtable& __other) const
1166 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1168 if (__this->size() != __other.size())
1171 for (auto __itx = __this->begin(); __itx != __this->end();)
1173 const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
1174 const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
1176 if (std::distance(__xrange.first, __xrange.second)
1177 != std::distance(__yrange.first, __yrange.second))
1180 if (!_S_is_permutation(__xrange.first,
1185 __itx = __xrange.second;
1190 _GLIBCXX_END_NAMESPACE_VERSION
1191 } // namespace __detail
1194 #endif // _HASHTABLE_POLICY_H