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 struct _Hash_node_base
78 _Hash_node_base* _M_nxt;
82 _Hash_node_base(_Hash_node_base* __next)
86 template<typename _Value, bool __cache_hash_code>
89 template<typename _Value>
90 struct _Hash_node<_Value, true> : _Hash_node_base
93 std::size_t _M_hash_code;
95 template<typename... _Args>
96 _Hash_node(_Args&&... __args)
97 : _M_v(std::forward<_Args>(__args)...), _M_hash_code() { }
99 _Hash_node* _M_next() const
100 { return static_cast<_Hash_node*>(_M_nxt); }
103 template<typename _Value>
104 struct _Hash_node<_Value, false> : _Hash_node_base
108 template<typename... _Args>
109 _Hash_node(_Args&&... __args)
110 : _M_v(std::forward<_Args>(__args)...) { }
112 _Hash_node* _M_next() const
113 { return static_cast<_Hash_node*>(_M_nxt); }
116 // Node iterators, used to iterate through all the hashtable.
117 template<typename _Value, bool __cache>
118 struct _Node_iterator_base
120 _Node_iterator_base(_Hash_node<_Value, __cache>* __p)
125 { _M_cur = _M_cur->_M_next(); }
127 _Hash_node<_Value, __cache>* _M_cur;
130 template<typename _Value, bool __cache>
132 operator==(const _Node_iterator_base<_Value, __cache>& __x,
133 const _Node_iterator_base<_Value, __cache>& __y)
134 { return __x._M_cur == __y._M_cur; }
136 template<typename _Value, bool __cache>
138 operator!=(const _Node_iterator_base<_Value, __cache>& __x,
139 const _Node_iterator_base<_Value, __cache>& __y)
140 { return __x._M_cur != __y._M_cur; }
142 template<typename _Value, bool __constant_iterators, bool __cache>
143 struct _Node_iterator
144 : public _Node_iterator_base<_Value, __cache>
146 typedef _Value value_type;
147 typedef typename std::conditional<__constant_iterators,
148 const _Value*, _Value*>::type
150 typedef typename std::conditional<__constant_iterators,
151 const _Value&, _Value&>::type
153 typedef std::ptrdiff_t difference_type;
154 typedef std::forward_iterator_tag iterator_category;
157 : _Node_iterator_base<_Value, __cache>(0) { }
160 _Node_iterator(_Hash_node<_Value, __cache>* __p)
161 : _Node_iterator_base<_Value, __cache>(__p) { }
165 { return this->_M_cur->_M_v; }
169 { return std::__addressof(this->_M_cur->_M_v); }
181 _Node_iterator __tmp(*this);
187 template<typename _Value, bool __constant_iterators, bool __cache>
188 struct _Node_const_iterator
189 : public _Node_iterator_base<_Value, __cache>
191 typedef _Value value_type;
192 typedef const _Value* pointer;
193 typedef const _Value& reference;
194 typedef std::ptrdiff_t difference_type;
195 typedef std::forward_iterator_tag iterator_category;
197 _Node_const_iterator()
198 : _Node_iterator_base<_Value, __cache>(0) { }
201 _Node_const_iterator(_Hash_node<_Value, __cache>* __p)
202 : _Node_iterator_base<_Value, __cache>(__p) { }
204 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
206 : _Node_iterator_base<_Value, __cache>(__x._M_cur) { }
210 { return this->_M_cur->_M_v; }
214 { return std::__addressof(this->_M_cur->_M_v); }
216 _Node_const_iterator&
226 _Node_const_iterator __tmp(*this);
232 // Many of class template _Hashtable's template parameters are policy
233 // classes. These are defaults for the policies.
235 // Default range hashing function: use division to fold a large number
236 // into the range [0, N).
237 struct _Mod_range_hashing
239 typedef std::size_t first_argument_type;
240 typedef std::size_t second_argument_type;
241 typedef std::size_t result_type;
244 operator()(first_argument_type __num, second_argument_type __den) const
245 { return __num % __den; }
248 // Default ranged hash function H. In principle it should be a
249 // function object composed from objects of type H1 and H2 such that
250 // h(k, N) = h2(h1(k), N), but that would mean making extra copies of
251 // h1 and h2. So instead we'll just use a tag to tell class template
252 // hashtable to do that composition.
253 struct _Default_ranged_hash { };
255 // Default value for rehash policy. Bucket size is (usually) the
256 // smallest prime that keeps the load factor small enough.
257 struct _Prime_rehash_policy
259 _Prime_rehash_policy(float __z = 1.0)
260 : _M_max_load_factor(__z), _M_prev_resize(0), _M_next_resize(0) { }
263 max_load_factor() const noexcept
264 { return _M_max_load_factor; }
266 // Return a bucket size no smaller than n.
268 _M_next_bkt(std::size_t __n) const;
270 // Return a bucket count appropriate for n elements
272 _M_bkt_for_elements(std::size_t __n) const;
274 // __n_bkt is current bucket count, __n_elt is current element count,
275 // and __n_ins is number of elements to be inserted. Do we need to
276 // increase bucket count? If so, return make_pair(true, n), where n
277 // is the new bucket count. If not, return make_pair(false, 0).
278 std::pair<bool, std::size_t>
279 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
280 std::size_t __n_ins) const;
282 typedef std::pair<std::size_t, std::size_t> _State;
286 { return std::make_pair(_M_prev_resize, _M_next_resize); }
289 _M_reset(const _State& __state)
291 _M_prev_resize = __state.first;
292 _M_next_resize = __state.second;
295 enum { _S_n_primes = sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
297 static const std::size_t _S_growth_factor = 2;
299 float _M_max_load_factor;
300 mutable std::size_t _M_prev_resize;
301 mutable std::size_t _M_next_resize;
304 extern const unsigned long __prime_list[];
306 // XXX This is a hack. There's no good reason for any of
307 // _Prime_rehash_policy's member functions to be inline.
309 // Return a prime no smaller than n.
311 _Prime_rehash_policy::
312 _M_next_bkt(std::size_t __n) const
314 // Optimize lookups involving the first elements of __prime_list.
315 // (useful to speed-up, eg, constructors)
316 static const unsigned char __fast_bkt[12]
317 = { 2, 2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11 };
319 const std::size_t __grown_n = __n * _S_growth_factor;
324 = __builtin_ceil(__fast_bkt[__grown_n]
325 * (long double)_M_max_load_factor);
326 return __fast_bkt[__grown_n];
329 const unsigned long* __next_bkt
330 = std::lower_bound(__prime_list + 5, __prime_list + _S_n_primes,
332 const unsigned long* __prev_bkt
333 = std::lower_bound(__prime_list + 1, __next_bkt, __n / _S_growth_factor);
336 = __builtin_floor(*(__prev_bkt - 1) * (long double)_M_max_load_factor);
338 = __builtin_ceil(*__next_bkt * (long double)_M_max_load_factor);
342 // Return the smallest prime p such that alpha p >= n, where alpha
343 // is the load factor.
345 _Prime_rehash_policy::
346 _M_bkt_for_elements(std::size_t __n) const
347 { return _M_next_bkt(__builtin_ceil(__n / (long double)_M_max_load_factor)); }
349 // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
350 // If p > __n_bkt, return make_pair(true, p); otherwise return
351 // make_pair(false, 0). In principle this isn't very different from
352 // _M_bkt_for_elements.
354 // The only tricky part is that we're caching the element count at
355 // which we need to rehash, so we don't have to do a floating-point
356 // multiply for every insertion.
358 inline std::pair<bool, std::size_t>
359 _Prime_rehash_policy::
360 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
361 std::size_t __n_ins) const
363 if (__n_elt + __n_ins >= _M_next_resize)
365 long double __min_bkts = (__n_elt + __n_ins)
366 / (long double)_M_max_load_factor;
367 if (__min_bkts >= __n_bkt)
368 return std::make_pair(true,
369 _M_next_bkt(__builtin_floor(__min_bkts) + 1));
373 = __builtin_floor(__n_bkt * (long double)_M_max_load_factor);
374 return std::make_pair(false, 0);
377 else if (__n_elt + __n_ins < _M_prev_resize)
379 long double __min_bkts = (__n_elt + __n_ins)
380 / (long double)_M_max_load_factor;
381 return std::make_pair(true,
382 _M_next_bkt(__builtin_floor(__min_bkts) + 1));
385 return std::make_pair(false, 0);
388 // Base classes for std::_Hashtable. We define these base classes
389 // because in some cases we want to do different things depending
390 // on the value of a policy class. In some cases the policy class
391 // affects which member functions and nested typedefs are defined;
392 // we handle that by specializing base class templates. Several of
393 // the base class templates need to access other members of class
394 // template _Hashtable, so we use the "curiously recurring template
395 // pattern" for them.
397 // class template _Map_base. If the hashtable has a value type of
398 // the form pair<T1, T2> and a key extraction policy that returns the
399 // first part of the pair, the hashtable gets a mapped_type typedef.
400 // If it satisfies those criteria and also has unique keys, then it
401 // also gets an operator[].
402 template<typename _Key, typename _Value, typename _Ex, bool __unique,
404 struct _Map_base { };
406 template<typename _Key, typename _Pair, typename _Hashtable>
407 struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, false, _Hashtable>
409 typedef typename _Pair::second_type mapped_type;
412 template<typename _Key, typename _Pair, typename _Hashtable>
413 struct _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>
415 typedef typename _Pair::second_type mapped_type;
418 operator[](const _Key& __k);
421 operator[](_Key&& __k);
423 // _GLIBCXX_RESOLVE_LIB_DEFECTS
424 // DR 761. unordered_map needs an at() member function.
429 at(const _Key& __k) const;
432 template<typename _Key, typename _Pair, typename _Hashtable>
433 typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
434 true, _Hashtable>::mapped_type&
435 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
436 operator[](const _Key& __k)
438 _Hashtable* __h = static_cast<_Hashtable*>(this);
439 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
440 std::size_t __n = __h->_M_bucket_index(__k, __code);
442 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
444 return __h->_M_insert_bucket(std::make_pair(__k, mapped_type()),
445 __n, __code)->second;
446 return (__p->_M_v).second;
449 template<typename _Key, typename _Pair, typename _Hashtable>
450 typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
451 true, _Hashtable>::mapped_type&
452 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
453 operator[](_Key&& __k)
455 _Hashtable* __h = static_cast<_Hashtable*>(this);
456 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
457 std::size_t __n = __h->_M_bucket_index(__k, __code);
459 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
461 return __h->_M_insert_bucket(std::make_pair(std::move(__k),
463 __n, __code)->second;
464 return (__p->_M_v).second;
467 template<typename _Key, typename _Pair, typename _Hashtable>
468 typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
469 true, _Hashtable>::mapped_type&
470 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
473 _Hashtable* __h = static_cast<_Hashtable*>(this);
474 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
475 std::size_t __n = __h->_M_bucket_index(__k, __code);
477 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
479 __throw_out_of_range(__N("_Map_base::at"));
480 return (__p->_M_v).second;
483 template<typename _Key, typename _Pair, typename _Hashtable>
484 const typename _Map_base<_Key, _Pair, std::_Select1st<_Pair>,
485 true, _Hashtable>::mapped_type&
486 _Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::
487 at(const _Key& __k) const
489 const _Hashtable* __h = static_cast<const _Hashtable*>(this);
490 typename _Hashtable::_Hash_code_type __code = __h->_M_hash_code(__k);
491 std::size_t __n = __h->_M_bucket_index(__k, __code);
493 typename _Hashtable::_Node* __p = __h->_M_find_node(__n, __k, __code);
495 __throw_out_of_range(__N("_Map_base::at"));
496 return (__p->_M_v).second;
499 // class template _Rehash_base. Give hashtable the max_load_factor
500 // functions and reserve iff the rehash policy is _Prime_rehash_policy.
501 template<typename _RehashPolicy, typename _Hashtable>
502 struct _Rehash_base { };
504 template<typename _Hashtable>
505 struct _Rehash_base<_Prime_rehash_policy, _Hashtable>
508 max_load_factor() const noexcept
510 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
511 return __this->__rehash_policy().max_load_factor();
515 max_load_factor(float __z)
517 _Hashtable* __this = static_cast<_Hashtable*>(this);
518 __this->__rehash_policy(_Prime_rehash_policy(__z));
522 reserve(std::size_t __n)
524 _Hashtable* __this = static_cast<_Hashtable*>(this);
525 __this->rehash(__builtin_ceil(__n / max_load_factor()));
529 // Helper class using EBO when it is not forbidden, type is not final,
530 // and when it worth it, type is empty.
531 template<int _Nm, typename _Tp,
532 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
533 struct _Hashtable_ebo_helper;
535 // Specialization using EBO.
536 template<int _Nm, typename _Tp>
537 struct _Hashtable_ebo_helper<_Nm, _Tp, true>
541 _Hashtable_ebo_helper() = default;
542 _Hashtable_ebo_helper(const _Tp& __tp) : _Tp(__tp)
546 _S_cget(const _Hashtable_ebo_helper& __eboh)
547 { return static_cast<const _Tp&>(__eboh); }
550 _S_get(_Hashtable_ebo_helper& __eboh)
551 { return static_cast<_Tp&>(__eboh); }
554 // Specialization not using EBO.
555 template<int _Nm, typename _Tp>
556 struct _Hashtable_ebo_helper<_Nm, _Tp, false>
558 _Hashtable_ebo_helper() = default;
559 _Hashtable_ebo_helper(const _Tp& __tp) : _M_tp(__tp)
563 _S_cget(const _Hashtable_ebo_helper& __eboh)
564 { return __eboh._M_tp; }
567 _S_get(_Hashtable_ebo_helper& __eboh)
568 { return __eboh._M_tp; }
574 // Class template _Hash_code_base. Encapsulates two policy issues that
575 // aren't quite orthogonal.
576 // (1) the difference between using a ranged hash function and using
577 // the combination of a hash function and a range-hashing function.
578 // In the former case we don't have such things as hash codes, so
579 // we have a dummy type as placeholder.
580 // (2) Whether or not we cache hash codes. Caching hash codes is
581 // meaningless if we have a ranged hash function.
582 // We also put the key extraction objects here, for convenience.
584 // Each specialization derives from one or more of the template parameters to
585 // benefit from Ebo. This is important as this type is inherited in some cases
586 // by the _Local_iterator_base type used to implement local_iterator and
587 // const_local_iterator. As with any iterator type we prefer to make it as
588 // small as possible.
590 // Primary template: unused except as a hook for specializations.
591 template<typename _Key, typename _Value, typename _ExtractKey,
592 typename _H1, typename _H2, typename _Hash,
593 bool __cache_hash_code>
594 struct _Hash_code_base;
596 // Specialization: ranged hash function, no caching hash codes. H1
597 // and H2 are provided but ignored. We define a dummy hash code type.
598 template<typename _Key, typename _Value, typename _ExtractKey,
599 typename _H1, typename _H2, typename _Hash>
600 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
602 : public _Hashtable_ebo_helper<0, _ExtractKey>,
603 public _Hashtable_ebo_helper<1, _Hash>
606 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
607 typedef _Hashtable_ebo_helper<1, _Hash> _EboHash;
610 // We need the default constructor for the local iterators.
611 _Hash_code_base() = default;
612 _Hash_code_base(const _ExtractKey& __ex,
613 const _H1&, const _H2&, const _Hash& __h)
614 : _EboExtractKey(__ex), _EboHash(__h) { }
616 typedef void* _Hash_code_type;
619 _M_hash_code(const _Key& __key) const
623 _M_bucket_index(const _Key& __k, _Hash_code_type,
624 std::size_t __n) const
625 { return _M_ranged_hash()(__k, __n); }
628 _M_bucket_index(const _Hash_node<_Value, false>* __p,
629 std::size_t __n) const
630 { return _M_ranged_hash()(_M_extract()(__p->_M_v), __n); }
633 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
637 _M_copy_code(_Hash_node<_Value, false>*,
638 const _Hash_node<_Value, false>*) const
642 _M_swap(_Hash_code_base& __x)
644 std::swap(_M_extract(), __x._M_extract());
645 std::swap(_M_ranged_hash(), __x._M_ranged_hash());
650 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
652 _M_extract() { return _EboExtractKey::_S_get(*this); }
654 _M_ranged_hash() const { return _EboHash::_S_cget(*this); }
656 _M_ranged_hash() { return _EboHash::_S_get(*this); }
659 // No specialization for ranged hash function while caching hash codes.
660 // That combination is meaningless, and trying to do it is an error.
662 // Specialization: ranged hash function, cache hash codes. This
663 // combination is meaningless, so we provide only a declaration
664 // and no definition.
665 template<typename _Key, typename _Value, typename _ExtractKey,
666 typename _H1, typename _H2, typename _Hash>
667 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
669 // Specialization: hash function and range-hashing function, no
670 // caching of hash codes.
671 // Provides typedef and accessor required by TR1.
672 template<typename _Key, typename _Value, typename _ExtractKey,
673 typename _H1, typename _H2>
674 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
675 _Default_ranged_hash, false>
677 : public _Hashtable_ebo_helper<0, _ExtractKey>,
678 public _Hashtable_ebo_helper<1, _H1>,
679 public _Hashtable_ebo_helper<2, _H2>
682 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
683 typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
684 typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
690 hash_function() const
694 // We need the default constructor for the local iterators.
695 _Hash_code_base() = default;
696 _Hash_code_base(const _ExtractKey& __ex,
697 const _H1& __h1, const _H2& __h2,
698 const _Default_ranged_hash&)
699 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
701 typedef std::size_t _Hash_code_type;
704 _M_hash_code(const _Key& __k) const
705 { return _M_h1()(__k); }
708 _M_bucket_index(const _Key&, _Hash_code_type __c,
709 std::size_t __n) const
710 { return _M_h2()(__c, __n); }
713 _M_bucket_index(const _Hash_node<_Value, false>* __p,
714 std::size_t __n) const
715 { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v)), __n); }
718 _M_store_code(_Hash_node<_Value, false>*, _Hash_code_type) const
722 _M_copy_code(_Hash_node<_Value, false>*,
723 const _Hash_node<_Value, false>*) const
727 _M_swap(_Hash_code_base& __x)
729 std::swap(_M_extract(), __x._M_extract());
730 std::swap(_M_h1(), __x._M_h1());
731 std::swap(_M_h2(), __x._M_h2());
736 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
738 _M_extract() { return _EboExtractKey::_S_get(*this); }
740 _M_h1() const { return _EboH1::_S_cget(*this); }
742 _M_h1() { return _EboH1::_S_get(*this); }
744 _M_h2() const { return _EboH2::_S_cget(*this); }
746 _M_h2() { return _EboH2::_S_get(*this); }
749 // Specialization: hash function and range-hashing function,
750 // caching hash codes. H is provided but ignored. Provides
751 // typedef and accessor required by TR1.
752 template<typename _Key, typename _Value, typename _ExtractKey,
753 typename _H1, typename _H2>
754 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
755 _Default_ranged_hash, true>
757 : public _Hashtable_ebo_helper<0, _ExtractKey>,
758 public _Hashtable_ebo_helper<1, _H1>,
759 public _Hashtable_ebo_helper<2, _H2>
762 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
763 typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
764 typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
770 hash_function() const
774 _Hash_code_base(const _ExtractKey& __ex,
775 const _H1& __h1, const _H2& __h2,
776 const _Default_ranged_hash&)
777 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
779 typedef std::size_t _Hash_code_type;
782 _M_hash_code(const _Key& __k) const
783 { return _M_h1()(__k); }
786 _M_bucket_index(const _Key&, _Hash_code_type __c,
787 std::size_t __n) const
788 { return _M_h2()(__c, __n); }
791 _M_bucket_index(const _Hash_node<_Value, true>* __p,
792 std::size_t __n) const
793 { return _M_h2()(__p->_M_hash_code, __n); }
796 _M_store_code(_Hash_node<_Value, true>* __n, _Hash_code_type __c) const
797 { __n->_M_hash_code = __c; }
800 _M_copy_code(_Hash_node<_Value, true>* __to,
801 const _Hash_node<_Value, true>* __from) const
802 { __to->_M_hash_code = __from->_M_hash_code; }
805 _M_swap(_Hash_code_base& __x)
807 std::swap(_M_extract(), __x._M_extract());
808 std::swap(_M_h1(), __x._M_h1());
809 std::swap(_M_h2(), __x._M_h2());
814 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
816 _M_extract() { return _EboExtractKey::_S_get(*this); }
818 _M_h1() const { return _EboH1::_S_cget(*this); }
820 _M_h1() { return _EboH1::_S_get(*this); }
822 _M_h2() const { return _EboH2::_S_cget(*this); }
824 _M_h2() { return _EboH2::_S_get(*this); }
827 template <typename _Key, typename _Value, typename _ExtractKey,
828 typename _Equal, typename _HashCodeType,
829 bool __cache_hash_code>
830 struct _Equal_helper;
832 template<typename _Key, typename _Value, typename _ExtractKey,
833 typename _Equal, typename _HashCodeType>
834 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
837 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
838 const _Key& __k, _HashCodeType __c,
839 _Hash_node<_Value, true>* __n)
840 { return __c == __n->_M_hash_code
841 && __eq(__k, __extract(__n->_M_v)); }
844 template<typename _Key, typename _Value, typename _ExtractKey,
845 typename _Equal, typename _HashCodeType>
846 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
849 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
850 const _Key& __k, _HashCodeType,
851 _Hash_node<_Value, false>* __n)
852 { return __eq(__k, __extract(__n->_M_v)); }
855 // Helper class adding management of _Equal functor to _Hash_code_base
857 template<typename _Key, typename _Value,
858 typename _ExtractKey, typename _Equal,
859 typename _H1, typename _H2, typename _Hash,
860 bool __cache_hash_code>
861 struct _Hashtable_base
863 : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
865 public _Hashtable_ebo_helper<0, _Equal>
868 typedef _Hashtable_ebo_helper<0, _Equal> _EboEqual;
871 typedef _Hash_code_base<_Key, _Value, _ExtractKey,
872 _H1, _H2, _Hash, __cache_hash_code> _HCBase;
873 typedef typename _HCBase::_Hash_code_type _Hash_code_type;
875 _Hashtable_base(const _ExtractKey& __ex,
876 const _H1& __h1, const _H2& __h2,
877 const _Hash& __hash, const _Equal& __eq)
878 : _HCBase(__ex, __h1, __h2, __hash), _EboEqual(__eq) { }
881 _M_equals(const _Key& __k, _Hash_code_type __c,
882 _Hash_node<_Value, __cache_hash_code>* __n) const
884 typedef _Equal_helper<_Key, _Value, _ExtractKey,
885 _Equal, _Hash_code_type,
886 __cache_hash_code> _EqualHelper;
887 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
892 _M_swap(_Hashtable_base& __x)
894 _HCBase::_M_swap(__x);
895 std::swap(_M_eq(), __x._M_eq());
900 _M_eq() const { return _EboEqual::_S_cget(*this); }
902 _M_eq() { return _EboEqual::_S_get(*this); }
905 // Local iterators, used to iterate within a bucket but not between
907 template<typename _Key, typename _Value, typename _ExtractKey,
908 typename _H1, typename _H2, typename _Hash,
909 bool __cache_hash_code>
910 struct _Local_iterator_base;
912 template<typename _Key, typename _Value, typename _ExtractKey,
913 typename _H1, typename _H2, typename _Hash>
914 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
915 _H1, _H2, _Hash, true>
919 _Local_iterator_base() = default;
920 _Local_iterator_base(_Hash_node<_Value, true>* __p,
921 std::size_t __bkt, std::size_t __bkt_count)
922 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
927 _M_cur = _M_cur->_M_next();
930 std::size_t __bkt = _M_h2()(_M_cur->_M_hash_code, _M_bucket_count);
931 if (__bkt != _M_bucket)
936 const _H2& _M_h2() const
939 _Hash_node<_Value, true>* _M_cur;
940 std::size_t _M_bucket;
941 std::size_t _M_bucket_count;
944 template<typename _Key, typename _Value, typename _ExtractKey,
945 typename _H1, typename _H2, typename _Hash>
946 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
947 _H1, _H2, _Hash, false>
949 : public _Hash_code_base<_Key, _Value, _ExtractKey,
950 _H1, _H2, _Hash, false>
952 _Local_iterator_base() = default;
953 _Local_iterator_base(_Hash_node<_Value, false>* __p,
954 std::size_t __bkt, std::size_t __bkt_count)
955 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
960 _M_cur = _M_cur->_M_next();
963 std::size_t __bkt = this->_M_bucket_index(_M_cur, _M_bucket_count);
964 if (__bkt != _M_bucket)
969 _Hash_node<_Value, false>* _M_cur;
970 std::size_t _M_bucket;
971 std::size_t _M_bucket_count;
974 template<typename _Key, typename _Value, typename _ExtractKey,
975 typename _H1, typename _H2, typename _Hash, bool __cache>
977 operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
978 _H1, _H2, _Hash, __cache>& __x,
979 const _Local_iterator_base<_Key, _Value, _ExtractKey,
980 _H1, _H2, _Hash, __cache>& __y)
981 { return __x._M_cur == __y._M_cur; }
983 template<typename _Key, typename _Value, typename _ExtractKey,
984 typename _H1, typename _H2, typename _Hash, bool __cache>
986 operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
987 _H1, _H2, _Hash, __cache>& __x,
988 const _Local_iterator_base<_Key, _Value, _ExtractKey,
989 _H1, _H2, _Hash, __cache>& __y)
990 { return __x._M_cur != __y._M_cur; }
992 template<typename _Key, typename _Value, typename _ExtractKey,
993 typename _H1, typename _H2, typename _Hash,
994 bool __constant_iterators, bool __cache>
995 struct _Local_iterator
996 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
997 _H1, _H2, _Hash, __cache>
999 typedef _Value value_type;
1000 typedef typename std::conditional<__constant_iterators,
1001 const _Value*, _Value*>::type
1003 typedef typename std::conditional<__constant_iterators,
1004 const _Value&, _Value&>::type
1006 typedef std::ptrdiff_t difference_type;
1007 typedef std::forward_iterator_tag iterator_category;
1009 _Local_iterator() = default;
1012 _Local_iterator(_Hash_node<_Value, __cache>* __p,
1013 std::size_t __bkt, std::size_t __bkt_count)
1014 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1015 __cache>(__p, __bkt, __bkt_count)
1020 { return this->_M_cur->_M_v; }
1024 { return std::__addressof(this->_M_cur->_M_v); }
1036 _Local_iterator __tmp(*this);
1042 template<typename _Key, typename _Value, typename _ExtractKey,
1043 typename _H1, typename _H2, typename _Hash,
1044 bool __constant_iterators, bool __cache>
1045 struct _Local_const_iterator
1046 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1047 _H1, _H2, _Hash, __cache>
1049 typedef _Value value_type;
1050 typedef const _Value* pointer;
1051 typedef const _Value& reference;
1052 typedef std::ptrdiff_t difference_type;
1053 typedef std::forward_iterator_tag iterator_category;
1055 _Local_const_iterator() = default;
1058 _Local_const_iterator(_Hash_node<_Value, __cache>* __p,
1059 std::size_t __bkt, std::size_t __bkt_count)
1060 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1061 __cache>(__p, __bkt, __bkt_count)
1064 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1066 __constant_iterators,
1068 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1069 __cache>(__x._M_cur, __x._M_bucket,
1070 __x._M_bucket_count)
1075 { return this->_M_cur->_M_v; }
1079 { return std::__addressof(this->_M_cur->_M_v); }
1081 _Local_const_iterator&
1088 _Local_const_iterator
1091 _Local_const_iterator __tmp(*this);
1098 // Class template _Equality_base. This is for implementing equality
1099 // comparison for unordered containers, per N3068, by John Lakos and
1100 // Pablo Halpern. Algorithmically, we follow closely the reference
1101 // implementations therein.
1102 template<typename _ExtractKey, bool __unique_keys,
1103 typename _Hashtable>
1104 struct _Equality_base;
1106 template<typename _ExtractKey, typename _Hashtable>
1107 struct _Equality_base<_ExtractKey, true, _Hashtable>
1109 bool _M_equal(const _Hashtable&) const;
1112 template<typename _ExtractKey, typename _Hashtable>
1114 _Equality_base<_ExtractKey, true, _Hashtable>::
1115 _M_equal(const _Hashtable& __other) const
1117 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1119 if (__this->size() != __other.size())
1122 for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
1124 const auto __ity = __other.find(_ExtractKey()(*__itx));
1125 if (__ity == __other.end() || !bool(*__ity == *__itx))
1131 template<typename _ExtractKey, typename _Hashtable>
1132 struct _Equality_base<_ExtractKey, false, _Hashtable>
1134 bool _M_equal(const _Hashtable&) const;
1137 template<typename _Uiterator>
1139 _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
1142 // See std::is_permutation in N3068.
1143 template<typename _ExtractKey, typename _Hashtable>
1144 template<typename _Uiterator>
1146 _Equality_base<_ExtractKey, false, _Hashtable>::
1147 _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
1148 _Uiterator __first2)
1150 for (; __first1 != __last1; ++__first1, ++__first2)
1151 if (!(*__first1 == *__first2))
1154 if (__first1 == __last1)
1157 _Uiterator __last2 = __first2;
1158 std::advance(__last2, std::distance(__first1, __last1));
1160 for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
1162 _Uiterator __tmp = __first1;
1163 while (__tmp != __it1 && !bool(*__tmp == *__it1))
1166 // We've seen this one before.
1170 std::ptrdiff_t __n2 = 0;
1171 for (__tmp = __first2; __tmp != __last2; ++__tmp)
1172 if (*__tmp == *__it1)
1178 std::ptrdiff_t __n1 = 0;
1179 for (__tmp = __it1; __tmp != __last1; ++__tmp)
1180 if (*__tmp == *__it1)
1189 template<typename _ExtractKey, typename _Hashtable>
1191 _Equality_base<_ExtractKey, false, _Hashtable>::
1192 _M_equal(const _Hashtable& __other) const
1194 const _Hashtable* __this = static_cast<const _Hashtable*>(this);
1196 if (__this->size() != __other.size())
1199 for (auto __itx = __this->begin(); __itx != __this->end();)
1201 const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
1202 const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
1204 if (std::distance(__xrange.first, __xrange.second)
1205 != std::distance(__yrange.first, __yrange.second))
1208 if (!_S_is_permutation(__xrange.first,
1213 __itx = __xrange.second;
1218 _GLIBCXX_END_NAMESPACE_VERSION
1219 } // namespace __detail
1222 #endif // _HASHTABLE_POLICY_H