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)
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 _Nm, typename _Tp,
519 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
520 struct _Hashtable_ebo_helper;
522 // Specialization using EBO.
523 template<int _Nm, typename _Tp>
524 struct _Hashtable_ebo_helper<_Nm, _Tp, true> : private _Tp
526 _Hashtable_ebo_helper() = default;
527 _Hashtable_ebo_helper(const _Tp& __tp) : _Tp(__tp)
531 _S_cget(const _Hashtable_ebo_helper& __eboh)
532 { return static_cast<const _Tp&>(__eboh); }
535 _S_get(_Hashtable_ebo_helper& __eboh)
536 { return static_cast<_Tp&>(__eboh); }
539 // Specialization not using EBO.
540 template<int _Nm, typename _Tp>
541 struct _Hashtable_ebo_helper<_Nm, _Tp, false>
543 _Hashtable_ebo_helper() = default;
544 _Hashtable_ebo_helper(const _Tp& __tp) : _M_tp(__tp)
548 _S_cget(const _Hashtable_ebo_helper& __eboh)
549 { return __eboh._M_tp; }
552 _S_get(_Hashtable_ebo_helper& __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 : private _Hashtable_ebo_helper<0, _ExtractKey>,
587 private _Hashtable_ebo_helper<1, _Hash>
590 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
591 typedef _Hashtable_ebo_helper<1, _Hash> _EboHash;
594 // We need the default constructor for the local iterators.
595 _Hash_code_base() = default;
596 _Hash_code_base(const _ExtractKey& __ex,
597 const _H1&, const _H2&, const _Hash& __h)
598 : _EboExtractKey(__ex), _EboHash(__h) { }
600 typedef void* _Hash_code_type;
603 _M_hash_code(const _Key& __key) const
607 _M_bucket_index(const _Key& __k, _Hash_code_type,
608 std::size_t __n) const
609 { return _M_ranged_hash()(__k, __n); }
612 _M_bucket_index(const _Hash_node<_Value, false>* __p,
613 std::size_t __n) const
614 { return _M_ranged_hash()(_M_extract()(__p->_M_v), __n); }
617 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
621 _M_copy_code(_Hash_node<_Value, false>*,
622 const _Hash_node<_Value, false>*) const
626 _M_swap(_Hash_code_base& __x)
628 std::swap(_M_extract(), __x._M_extract());
629 std::swap(_M_ranged_hash(), __x._M_ranged_hash());
634 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
636 _M_extract() { return _EboExtractKey::_S_get(*this); }
638 _M_ranged_hash() const { return _EboHash::_S_cget(*this); }
640 _M_ranged_hash() { return _EboHash::_S_get(*this); }
643 // No specialization for ranged hash function while caching hash codes.
644 // That combination is meaningless, and trying to do it is an error.
646 // Specialization: ranged hash function, cache hash codes. This
647 // combination is meaningless, so we provide only a declaration
648 // and no definition.
649 template<typename _Key, typename _Value, typename _ExtractKey,
650 typename _H1, typename _H2, typename _Hash>
651 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
653 // Specialization: hash function and range-hashing function, no
654 // caching of hash codes.
655 // Provides typedef and accessor required by TR1.
656 template<typename _Key, typename _Value, typename _ExtractKey,
657 typename _H1, typename _H2>
658 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
659 _Default_ranged_hash, false>
660 : private _Hashtable_ebo_helper<0, _ExtractKey>,
661 private _Hashtable_ebo_helper<1, _H1>,
662 private _Hashtable_ebo_helper<2, _H2>
665 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
666 typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
667 typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
673 hash_function() const
677 // We need the default constructor for the local iterators.
678 _Hash_code_base() = default;
679 _Hash_code_base(const _ExtractKey& __ex,
680 const _H1& __h1, const _H2& __h2,
681 const _Default_ranged_hash&)
682 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
684 typedef std::size_t _Hash_code_type;
687 _M_hash_code(const _Key& __k) const
688 { return _M_h1()(__k); }
691 _M_bucket_index(const _Key&, _Hash_code_type __c,
692 std::size_t __n) const
693 { return _M_h2()(__c, __n); }
696 _M_bucket_index(const _Hash_node<_Value, false>* __p,
697 std::size_t __n) const
698 { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v)), __n); }
701 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
705 _M_copy_code(_Hash_node<_Value, false>*,
706 const _Hash_node<_Value, false>*) const
710 _M_swap(_Hash_code_base& __x)
712 std::swap(_M_extract(), __x._M_extract());
713 std::swap(_M_h1(), __x._M_h1());
714 std::swap(_M_h2(), __x._M_h2());
719 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
721 _M_extract() { return _EboExtractKey::_S_get(*this); }
723 _M_h1() const { return _EboH1::_S_cget(*this); }
725 _M_h1() { return _EboH1::_S_get(*this); }
727 _M_h2() const { return _EboH2::_S_cget(*this); }
729 _M_h2() { return _EboH2::_S_get(*this); }
732 // Specialization: hash function and range-hashing function,
733 // caching hash codes. H is provided but ignored. Provides
734 // typedef and accessor required by TR1.
735 template<typename _Key, typename _Value, typename _ExtractKey,
736 typename _H1, typename _H2>
737 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
738 _Default_ranged_hash, true>
739 : private _Hashtable_ebo_helper<0, _ExtractKey>,
740 private _Hashtable_ebo_helper<1, _H1>,
741 private _Hashtable_ebo_helper<2, _H2>
744 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
745 typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
746 typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
752 hash_function() const
756 _Hash_code_base(const _ExtractKey& __ex,
757 const _H1& __h1, const _H2& __h2,
758 const _Default_ranged_hash&)
759 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
761 typedef std::size_t _Hash_code_type;
764 _M_hash_code(const _Key& __k) const
765 { return _M_h1()(__k); }
768 _M_bucket_index(const _Key&, _Hash_code_type __c,
769 std::size_t __n) const
770 { return _M_h2()(__c, __n); }
773 _M_bucket_index(const _Hash_node<_Value, true>* __p,
774 std::size_t __n) const
775 { return _M_h2()(__p->_M_hash_code, __n); }
778 _M_store_code(_Hash_node<_Value, true>* __n, _Hash_code_type __c) const
779 { __n->_M_hash_code = __c; }
782 _M_copy_code(_Hash_node<_Value, true>* __to,
783 const _Hash_node<_Value, true>* __from) const
784 { __to->_M_hash_code = __from->_M_hash_code; }
787 _M_swap(_Hash_code_base& __x)
789 std::swap(_M_extract(), __x._M_extract());
790 std::swap(_M_h1(), __x._M_h1());
791 std::swap(_M_h2(), __x._M_h2());
796 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
798 _M_extract() { return _EboExtractKey::_S_get(*this); }
800 _M_h1() const { return _EboH1::_S_cget(*this); }
802 _M_h1() { return _EboH1::_S_get(*this); }
804 _M_h2() const { return _EboH2::_S_cget(*this); }
806 _M_h2() { return _EboH2::_S_get(*this); }
809 template <typename _Key, typename _Value, typename _ExtractKey,
810 typename _Equal, typename _HashCodeType,
811 bool __cache_hash_code>
812 struct _Equal_helper;
814 template<typename _Key, typename _Value, typename _ExtractKey,
815 typename _Equal, typename _HashCodeType>
816 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
819 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
820 const _Key& __k, _HashCodeType __c,
821 _Hash_node<_Value, true>* __n)
822 { return __c == __n->_M_hash_code
823 && __eq(__k, __extract(__n->_M_v)); }
826 template<typename _Key, typename _Value, typename _ExtractKey,
827 typename _Equal, typename _HashCodeType>
828 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
831 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
832 const _Key& __k, _HashCodeType,
833 _Hash_node<_Value, false>* __n)
834 { return __eq(__k, __extract(__n->_M_v)); }
837 // Helper class adding management of _Equal functor to _Hash_code_base
839 template<typename _Key, typename _Value,
840 typename _ExtractKey, typename _Equal,
841 typename _H1, typename _H2, typename _Hash,
842 bool __cache_hash_code>
843 struct _Hashtable_base
844 : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
846 private _Hashtable_ebo_helper<0, _Equal>
849 typedef _Hashtable_ebo_helper<0, _Equal> _EboEqual;
852 typedef _Hash_code_base<_Key, _Value, _ExtractKey,
853 _H1, _H2, _Hash, __cache_hash_code> _HCBase;
854 typedef typename _HCBase::_Hash_code_type _Hash_code_type;
856 _Hashtable_base(const _ExtractKey& __ex,
857 const _H1& __h1, const _H2& __h2,
858 const _Hash& __hash, const _Equal& __eq)
859 : _HCBase(__ex, __h1, __h2, __hash), _EboEqual(__eq) { }
862 _M_equals(const _Key& __k, _Hash_code_type __c,
863 _Hash_node<_Value, __cache_hash_code>* __n) const
865 typedef _Equal_helper<_Key, _Value, _ExtractKey,
866 _Equal, _Hash_code_type,
867 __cache_hash_code> _EqualHelper;
868 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
873 _M_swap(_Hashtable_base& __x)
875 _HCBase::_M_swap(__x);
876 std::swap(_M_eq(), __x._M_eq());
881 _M_eq() const { return _EboEqual::_S_cget(*this); }
883 _M_eq() { return _EboEqual::_S_get(*this); }
886 // Local iterators, used to iterate within a bucket but not between
888 template<typename _Key, typename _Value, typename _ExtractKey,
889 typename _H1, typename _H2, typename _Hash,
890 bool __cache_hash_code>
891 struct _Local_iterator_base;
893 template<typename _Key, typename _Value, typename _ExtractKey,
894 typename _H1, typename _H2, typename _Hash>
895 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
896 _H1, _H2, _Hash, true>
899 _Local_iterator_base() = default;
900 _Local_iterator_base(_Hash_node<_Value, true>* __p,
901 std::size_t __bkt, std::size_t __bkt_count)
902 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
907 _M_cur = _M_cur->_M_next;
910 std::size_t __bkt = _M_h2()(_M_cur->_M_hash_code, _M_bucket_count);
911 if (__bkt != _M_bucket)
916 const _H2& _M_h2() const
919 _Hash_node<_Value, true>* _M_cur;
920 std::size_t _M_bucket;
921 std::size_t _M_bucket_count;
924 template<typename _Key, typename _Value, typename _ExtractKey,
925 typename _H1, typename _H2, typename _Hash>
926 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
927 _H1, _H2, _Hash, false>
928 : private _Hash_code_base<_Key, _Value, _ExtractKey,
929 _H1, _H2, _Hash, false>
931 _Local_iterator_base() = default;
932 _Local_iterator_base(_Hash_node<_Value, false>* __p,
933 std::size_t __bkt, std::size_t __bkt_count)
934 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
939 _M_cur = _M_cur->_M_next;
942 std::size_t __bkt = this->_M_bucket_index(_M_cur, _M_bucket_count);
943 if (__bkt != _M_bucket)
948 _Hash_node<_Value, false>* _M_cur;
949 std::size_t _M_bucket;
950 std::size_t _M_bucket_count;
953 template<typename _Key, typename _Value, typename _ExtractKey,
954 typename _H1, typename _H2, typename _Hash, bool __cache>
956 operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
957 _H1, _H2, _Hash, __cache>& __x,
958 const _Local_iterator_base<_Key, _Value, _ExtractKey,
959 _H1, _H2, _Hash, __cache>& __y)
960 { return __x._M_cur == __y._M_cur; }
962 template<typename _Key, typename _Value, typename _ExtractKey,
963 typename _H1, typename _H2, typename _Hash, bool __cache>
965 operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
966 _H1, _H2, _Hash, __cache>& __x,
967 const _Local_iterator_base<_Key, _Value, _ExtractKey,
968 _H1, _H2, _Hash, __cache>& __y)
969 { return __x._M_cur != __y._M_cur; }
971 template<typename _Key, typename _Value, typename _ExtractKey,
972 typename _H1, typename _H2, typename _Hash,
973 bool __constant_iterators, bool __cache>
974 struct _Local_iterator
975 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
976 _H1, _H2, _Hash, __cache>
978 typedef _Value value_type;
979 typedef typename std::conditional<__constant_iterators,
980 const _Value*, _Value*>::type
982 typedef typename std::conditional<__constant_iterators,
983 const _Value&, _Value&>::type
985 typedef std::ptrdiff_t difference_type;
986 typedef std::forward_iterator_tag iterator_category;
988 _Local_iterator() = default;
991 _Local_iterator(_Hash_node<_Value, __cache>* __p,
992 std::size_t __bkt, std::size_t __bkt_count)
993 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
994 __cache>(__p, __bkt, __bkt_count)
999 { return this->_M_cur->_M_v; }
1003 { return std::__addressof(this->_M_cur->_M_v); }
1015 _Local_iterator __tmp(*this);
1021 template<typename _Key, typename _Value, typename _ExtractKey,
1022 typename _H1, typename _H2, typename _Hash,
1023 bool __constant_iterators, bool __cache>
1024 struct _Local_const_iterator
1025 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1026 _H1, _H2, _Hash, __cache>
1028 typedef _Value value_type;
1029 typedef const _Value* pointer;
1030 typedef const _Value& reference;
1031 typedef std::ptrdiff_t difference_type;
1032 typedef std::forward_iterator_tag iterator_category;
1034 _Local_const_iterator() = default;
1037 _Local_const_iterator(_Hash_node<_Value, __cache>* __p,
1038 std::size_t __bkt, std::size_t __bkt_count)
1039 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1040 __cache>(__p, __bkt, __bkt_count)
1043 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1045 __constant_iterators,
1047 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1048 __cache>(__x._M_cur, __x._M_bucket,
1049 __x._M_bucket_count)
1054 { return this->_M_cur->_M_v; }
1058 { return std::__addressof(this->_M_cur->_M_v); }
1060 _Local_const_iterator&
1067 _Local_const_iterator
1070 _Local_const_iterator __tmp(*this);
1077 // Class template _Equality_base. This is for implementing equality
1078 // comparison for unordered containers, per N3068, by John Lakos and
1079 // Pablo Halpern. Algorithmically, we follow closely the reference
1080 // implementations therein.
1081 template<typename _ExtractKey, bool __unique_keys,
1082 typename _Hashtable>
1083 struct _Equality_base;
1085 template<typename _ExtractKey, typename _Hashtable>
1086 struct _Equality_base<_ExtractKey, true, _Hashtable>
1088 bool _M_equal(const _Hashtable&) const;
1091 template<typename _ExtractKey, typename _Hashtable>
1093 _Equality_base<_ExtractKey, true, _Hashtable>::
1094 _M_equal(const _Hashtable& __other) const
1096 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1098 if (__this->size() != __other.size())
1101 for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
1103 const auto __ity = __other.find(_ExtractKey()(*__itx));
1104 if (__ity == __other.end() || *__ity != *__itx)
1110 template<typename _ExtractKey, typename _Hashtable>
1111 struct _Equality_base<_ExtractKey, false, _Hashtable>
1113 bool _M_equal(const _Hashtable&) const;
1116 template<typename _Uiterator>
1118 _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
1121 // See std::is_permutation in N3068.
1122 template<typename _ExtractKey, typename _Hashtable>
1123 template<typename _Uiterator>
1125 _Equality_base<_ExtractKey, false, _Hashtable>::
1126 _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
1127 _Uiterator __first2)
1129 for (; __first1 != __last1; ++__first1, ++__first2)
1130 if (!(*__first1 == *__first2))
1133 if (__first1 == __last1)
1136 _Uiterator __last2 = __first2;
1137 std::advance(__last2, std::distance(__first1, __last1));
1139 for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
1141 _Uiterator __tmp = __first1;
1142 while (__tmp != __it1 && !(*__tmp == *__it1))
1145 // We've seen this one before.
1149 std::ptrdiff_t __n2 = 0;
1150 for (__tmp = __first2; __tmp != __last2; ++__tmp)
1151 if (*__tmp == *__it1)
1157 std::ptrdiff_t __n1 = 0;
1158 for (__tmp = __it1; __tmp != __last1; ++__tmp)
1159 if (*__tmp == *__it1)
1168 template<typename _ExtractKey, typename _Hashtable>
1170 _Equality_base<_ExtractKey, false, _Hashtable>::
1171 _M_equal(const _Hashtable& __other) const
1173 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1175 if (__this->size() != __other.size())
1178 for (auto __itx = __this->begin(); __itx != __this->end();)
1180 const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
1181 const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
1183 if (std::distance(__xrange.first, __xrange.second)
1184 != std::distance(__yrange.first, __yrange.second))
1187 if (!_S_is_permutation(__xrange.first,
1192 __itx = __xrange.second;
1197 _GLIBCXX_END_NAMESPACE_VERSION
1198 } // namespace __detail
1201 #endif // _HASHTABLE_POLICY_H