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 // Auxiliary types used for all instantiations of _Hashtable: nodes
65 // Nodes, used to wrap elements stored in the hash table. A policy
66 // template parameter of class template _Hashtable controls whether
67 // nodes also store a hash code. In some cases (e.g. strings) this
68 // may be a performance win.
69 template<typename _Value
, bool __cache_hash_code
>
72 template<typename _Value
>
73 struct _Hash_node
<_Value
, true>
76 std::size_t _M_hash_code
;
79 template<typename
... _Args
>
80 _Hash_node(_Args
&&... __args
)
81 : _M_v(std::forward
<_Args
>(__args
)...),
82 _M_hash_code(), _M_next() { }
85 template<typename _Value
>
86 struct _Hash_node
<_Value
, false>
91 template<typename
... _Args
>
92 _Hash_node(_Args
&&... __args
)
93 : _M_v(std::forward
<_Args
>(__args
)...),
97 // Local iterators, used to iterate within a bucket but not between
99 template<typename _Value
, bool __cache
>
100 struct _Node_iterator_base
102 _Node_iterator_base(_Hash_node
<_Value
, __cache
>* __p
)
107 { _M_cur
= _M_cur
->_M_next
; }
109 _Hash_node
<_Value
, __cache
>* _M_cur
;
112 template<typename _Value
, bool __cache
>
114 operator==(const _Node_iterator_base
<_Value
, __cache
>& __x
,
115 const _Node_iterator_base
<_Value
, __cache
>& __y
)
116 { return __x
._M_cur
== __y
._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 __constant_iterators
, bool __cache
>
125 struct _Node_iterator
126 : public _Node_iterator_base
<_Value
, __cache
>
128 typedef _Value value_type
;
129 typedef typename
std::conditional
<__constant_iterators
,
130 const _Value
*, _Value
*>::type
132 typedef typename
std::conditional
<__constant_iterators
,
133 const _Value
&, _Value
&>::type
135 typedef std::ptrdiff_t difference_type
;
136 typedef std::forward_iterator_tag iterator_category
;
139 : _Node_iterator_base
<_Value
, __cache
>(0) { }
142 _Node_iterator(_Hash_node
<_Value
, __cache
>* __p
)
143 : _Node_iterator_base
<_Value
, __cache
>(__p
) { }
147 { return this->_M_cur
->_M_v
; }
151 { return std::__addressof(this->_M_cur
->_M_v
); }
163 _Node_iterator
__tmp(*this);
169 template<typename _Value
, bool __constant_iterators
, bool __cache
>
170 struct _Node_const_iterator
171 : public _Node_iterator_base
<_Value
, __cache
>
173 typedef _Value value_type
;
174 typedef const _Value
* pointer
;
175 typedef const _Value
& reference
;
176 typedef std::ptrdiff_t difference_type
;
177 typedef std::forward_iterator_tag iterator_category
;
179 _Node_const_iterator()
180 : _Node_iterator_base
<_Value
, __cache
>(0) { }
183 _Node_const_iterator(_Hash_node
<_Value
, __cache
>* __p
)
184 : _Node_iterator_base
<_Value
, __cache
>(__p
) { }
186 _Node_const_iterator(const _Node_iterator
<_Value
, __constant_iterators
,
188 : _Node_iterator_base
<_Value
, __cache
>(__x
._M_cur
) { }
192 { return this->_M_cur
->_M_v
; }
196 { return std::__addressof(this->_M_cur
->_M_v
); }
198 _Node_const_iterator
&
208 _Node_const_iterator
__tmp(*this);
214 template<typename _Value
, bool __cache
>
215 struct _Hashtable_iterator_base
217 _Hashtable_iterator_base(_Hash_node
<_Value
, __cache
>* __node
,
218 _Hash_node
<_Value
, __cache
>** __bucket
)
219 : _M_cur_node(__node
), _M_cur_bucket(__bucket
) { }
224 _M_cur_node
= _M_cur_node
->_M_next
;
232 _Hash_node
<_Value
, __cache
>* _M_cur_node
;
233 _Hash_node
<_Value
, __cache
>** _M_cur_bucket
;
236 // Global iterators, used for arbitrary iteration within a hash
237 // table. Larger and more expensive than local iterators.
238 template<typename _Value
, bool __cache
>
240 _Hashtable_iterator_base
<_Value
, __cache
>::
245 // This loop requires the bucket array to have a non-null sentinel.
246 while (!*_M_cur_bucket
)
248 _M_cur_node
= *_M_cur_bucket
;
251 template<typename _Value
, bool __cache
>
253 operator==(const _Hashtable_iterator_base
<_Value
, __cache
>& __x
,
254 const _Hashtable_iterator_base
<_Value
, __cache
>& __y
)
255 { return __x
._M_cur_node
== __y
._M_cur_node
; }
257 template<typename _Value
, bool __cache
>
259 operator!=(const _Hashtable_iterator_base
<_Value
, __cache
>& __x
,
260 const _Hashtable_iterator_base
<_Value
, __cache
>& __y
)
261 { return __x
._M_cur_node
!= __y
._M_cur_node
; }
263 template<typename _Value
, bool __constant_iterators
, bool __cache
>
264 struct _Hashtable_iterator
265 : public _Hashtable_iterator_base
<_Value
, __cache
>
267 typedef _Value value_type
;
268 typedef typename
std::conditional
<__constant_iterators
,
269 const _Value
*, _Value
*>::type
271 typedef typename
std::conditional
<__constant_iterators
,
272 const _Value
&, _Value
&>::type
274 typedef std::ptrdiff_t difference_type
;
275 typedef std::forward_iterator_tag iterator_category
;
277 _Hashtable_iterator()
278 : _Hashtable_iterator_base
<_Value
, __cache
>(0, 0) { }
280 _Hashtable_iterator(_Hash_node
<_Value
, __cache
>* __p
,
281 _Hash_node
<_Value
, __cache
>** __b
)
282 : _Hashtable_iterator_base
<_Value
, __cache
>(__p
, __b
) { }
285 _Hashtable_iterator(_Hash_node
<_Value
, __cache
>** __b
)
286 : _Hashtable_iterator_base
<_Value
, __cache
>(*__b
, __b
) { }
290 { return this->_M_cur_node
->_M_v
; }
294 { return std::__addressof(this->_M_cur_node
->_M_v
); }
306 _Hashtable_iterator
__tmp(*this);
312 template<typename _Value
, bool __constant_iterators
, bool __cache
>
313 struct _Hashtable_const_iterator
314 : public _Hashtable_iterator_base
<_Value
, __cache
>
316 typedef _Value value_type
;
317 typedef const _Value
* pointer
;
318 typedef const _Value
& reference
;
319 typedef std::ptrdiff_t difference_type
;
320 typedef std::forward_iterator_tag iterator_category
;
322 _Hashtable_const_iterator()
323 : _Hashtable_iterator_base
<_Value
, __cache
>(0, 0) { }
325 _Hashtable_const_iterator(_Hash_node
<_Value
, __cache
>* __p
,
326 _Hash_node
<_Value
, __cache
>** __b
)
327 : _Hashtable_iterator_base
<_Value
, __cache
>(__p
, __b
) { }
330 _Hashtable_const_iterator(_Hash_node
<_Value
, __cache
>** __b
)
331 : _Hashtable_iterator_base
<_Value
, __cache
>(*__b
, __b
) { }
333 _Hashtable_const_iterator(const _Hashtable_iterator
<_Value
,
334 __constant_iterators
, __cache
>& __x
)
335 : _Hashtable_iterator_base
<_Value
, __cache
>(__x
._M_cur_node
,
336 __x
._M_cur_bucket
) { }
340 { return this->_M_cur_node
->_M_v
; }
344 { return std::__addressof(this->_M_cur_node
->_M_v
); }
346 _Hashtable_const_iterator
&
353 _Hashtable_const_iterator
356 _Hashtable_const_iterator
__tmp(*this);
363 // Many of class template _Hashtable's template parameters are policy
364 // classes. These are defaults for the policies.
366 // Default range hashing function: use division to fold a large number
367 // into the range [0, N).
368 struct _Mod_range_hashing
370 typedef std::size_t first_argument_type
;
371 typedef std::size_t second_argument_type
;
372 typedef std::size_t result_type
;
375 operator()(first_argument_type __num
, second_argument_type __den
) const
376 { return __num
% __den
; }
379 // Default ranged hash function H. In principle it should be a
380 // function object composed from objects of type H1 and H2 such that
381 // h(k, N) = h2(h1(k), N), but that would mean making extra copies of
382 // h1 and h2. So instead we'll just use a tag to tell class template
383 // hashtable to do that composition.
384 struct _Default_ranged_hash
{ };
386 // Default value for rehash policy. Bucket size is (usually) the
387 // smallest prime that keeps the load factor small enough.
388 struct _Prime_rehash_policy
390 _Prime_rehash_policy(float __z
= 1.0)
391 : _M_max_load_factor(__z
), _M_growth_factor(2.f
), _M_next_resize(0) { }
394 max_load_factor() const noexcept
395 { return _M_max_load_factor
; }
397 // Return a bucket size no smaller than n.
399 _M_next_bkt(std::size_t __n
) const;
401 // Return a bucket count appropriate for n elements
403 _M_bkt_for_elements(std::size_t __n
) const;
405 // __n_bkt is current bucket count, __n_elt is current element count,
406 // and __n_ins is number of elements to be inserted. Do we need to
407 // increase bucket count? If so, return make_pair(true, n), where n
408 // is the new bucket count. If not, return make_pair(false, 0).
409 std::pair
<bool, std::size_t>
410 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
411 std::size_t __n_ins
) const;
413 enum { _S_n_primes
= sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
415 float _M_max_load_factor
;
416 float _M_growth_factor
;
417 mutable std::size_t _M_next_resize
;
420 extern const unsigned long __prime_list
[];
422 // XXX This is a hack. There's no good reason for any of
423 // _Prime_rehash_policy's member functions to be inline.
425 // Return a prime no smaller than n.
427 _Prime_rehash_policy::
428 _M_next_bkt(std::size_t __n
) const
430 const unsigned long __p
= *std::lower_bound(__prime_list
, __prime_list
433 static_cast<std::size_t>(__builtin_floor(__p
* _M_max_load_factor
));
437 // Return the smallest prime p such that alpha p >= n, where alpha
438 // is the load factor.
440 _Prime_rehash_policy::
441 _M_bkt_for_elements(std::size_t __n
) const
443 const float __min_bkts
= __n
/ _M_max_load_factor
;
444 const unsigned long __p
= *std::lower_bound(__prime_list
, __prime_list
445 + _S_n_primes
, __min_bkts
);
447 static_cast<std::size_t>(__builtin_floor(__p
* _M_max_load_factor
));
451 // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
452 // If p > __n_bkt, return make_pair(true, p); otherwise return
453 // make_pair(false, 0). In principle this isn't very different from
454 // _M_bkt_for_elements.
456 // The only tricky part is that we're caching the element count at
457 // which we need to rehash, so we don't have to do a floating-point
458 // multiply for every insertion.
460 inline std::pair
<bool, std::size_t>
461 _Prime_rehash_policy::
462 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
463 std::size_t __n_ins
) const
465 if (__n_elt
+ __n_ins
> _M_next_resize
)
467 float __min_bkts
= ((float(__n_ins
) + float(__n_elt
))
468 / _M_max_load_factor
);
469 if (__min_bkts
> __n_bkt
)
471 __min_bkts
= std::max(__min_bkts
, _M_growth_factor
* __n_bkt
);
472 const unsigned long __p
=
473 *std::lower_bound(__prime_list
, __prime_list
+ _S_n_primes
,
475 _M_next_resize
= static_cast<std::size_t>
476 (__builtin_floor(__p
* _M_max_load_factor
));
477 return std::make_pair(true, *__p
);
481 _M_next_resize
= static_cast<std::size_t>
482 (__builtin_floor(__n_bkt
* _M_max_load_factor
));
483 return std::make_pair(false, 0);
487 return std::make_pair(false, 0);
490 // Base classes for std::_Hashtable. We define these base classes
491 // because in some cases we want to do different things depending
492 // on the value of a policy class. In some cases the policy class
493 // affects which member functions and nested typedefs are defined;
494 // we handle that by specializing base class templates. Several of
495 // the base class templates need to access other members of class
496 // template _Hashtable, so we use the "curiously recurring template
497 // pattern" for them.
499 // class template _Map_base. If the hashtable has a value type of
500 // the form pair<T1, T2> and a key extraction policy that returns the
501 // first part of the pair, the hashtable gets a mapped_type typedef.
502 // If it satisfies those criteria and also has unique keys, then it
503 // also gets an operator[].
504 template<typename _Key
, typename _Value
, typename _Ex
, bool __unique
,
506 struct _Map_base
{ };
508 template<typename _Key
, typename _Pair
, typename _Hashtable
>
509 struct _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, false, _Hashtable
>
511 typedef typename
_Pair::second_type mapped_type
;
514 template<typename _Key
, typename _Pair
, typename _Hashtable
>
515 struct _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>
517 typedef typename
_Pair::second_type mapped_type
;
520 operator[](const _Key
& __k
);
523 operator[](_Key
&& __k
);
525 // _GLIBCXX_RESOLVE_LIB_DEFECTS
526 // DR 761. unordered_map needs an at() member function.
531 at(const _Key
& __k
) const;
534 template<typename _Key
, typename _Pair
, typename _Hashtable
>
535 typename _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>,
536 true, _Hashtable
>::mapped_type
&
537 _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>::
538 operator[](const _Key
& __k
)
540 _Hashtable
* __h
= static_cast<_Hashtable
*>(this);
541 typename
_Hashtable::_Hash_code_type __code
= __h
->_M_hash_code(__k
);
542 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
,
543 __h
->_M_bucket_count
);
545 typename
_Hashtable::_Node
* __p
=
546 __h
->_M_find_node(__h
->_M_buckets
[__n
], __k
, __code
);
548 return __h
->_M_insert_bucket(std::make_pair(__k
, mapped_type()),
549 __n
, __code
)->second
;
550 return (__p
->_M_v
).second
;
553 template<typename _Key
, typename _Pair
, typename _Hashtable
>
554 typename _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>,
555 true, _Hashtable
>::mapped_type
&
556 _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>::
557 operator[](_Key
&& __k
)
559 _Hashtable
* __h
= static_cast<_Hashtable
*>(this);
560 typename
_Hashtable::_Hash_code_type __code
= __h
->_M_hash_code(__k
);
561 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
,
562 __h
->_M_bucket_count
);
564 typename
_Hashtable::_Node
* __p
=
565 __h
->_M_find_node(__h
->_M_buckets
[__n
], __k
, __code
);
567 return __h
->_M_insert_bucket(std::make_pair(std::move(__k
),
569 __n
, __code
)->second
;
570 return (__p
->_M_v
).second
;
573 template<typename _Key
, typename _Pair
, typename _Hashtable
>
574 typename _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>,
575 true, _Hashtable
>::mapped_type
&
576 _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>::
579 _Hashtable
* __h
= static_cast<_Hashtable
*>(this);
580 typename
_Hashtable::_Hash_code_type __code
= __h
->_M_hash_code(__k
);
581 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
,
582 __h
->_M_bucket_count
);
584 typename
_Hashtable::_Node
* __p
=
585 __h
->_M_find_node(__h
->_M_buckets
[__n
], __k
, __code
);
587 __throw_out_of_range(__N("_Map_base::at"));
588 return (__p
->_M_v
).second
;
591 template<typename _Key
, typename _Pair
, typename _Hashtable
>
592 const typename _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>,
593 true, _Hashtable
>::mapped_type
&
594 _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>::
595 at(const _Key
& __k
) const
597 const _Hashtable
* __h
= static_cast<const _Hashtable
*>(this);
598 typename
_Hashtable::_Hash_code_type __code
= __h
->_M_hash_code(__k
);
599 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
,
600 __h
->_M_bucket_count
);
602 typename
_Hashtable::_Node
* __p
=
603 __h
->_M_find_node(__h
->_M_buckets
[__n
], __k
, __code
);
605 __throw_out_of_range(__N("_Map_base::at"));
606 return (__p
->_M_v
).second
;
609 // class template _Rehash_base. Give hashtable the max_load_factor
610 // functions and reserve iff the rehash policy is _Prime_rehash_policy.
611 template<typename _RehashPolicy
, typename _Hashtable
>
612 struct _Rehash_base
{ };
614 template<typename _Hashtable
>
615 struct _Rehash_base
<_Prime_rehash_policy
, _Hashtable
>
618 max_load_factor() const noexcept
620 const _Hashtable
* __this
= static_cast<const _Hashtable
*>(this);
621 return __this
->__rehash_policy().max_load_factor();
625 max_load_factor(float __z
)
627 _Hashtable
* __this
= static_cast<_Hashtable
*>(this);
628 __this
->__rehash_policy(_Prime_rehash_policy(__z
));
632 reserve(std::size_t __n
)
634 _Hashtable
* __this
= static_cast<_Hashtable
*>(this);
635 __this
->rehash(__builtin_ceil(__n
/ max_load_factor()));
639 // Class template _Hash_code_base. Encapsulates two policy issues that
640 // aren't quite orthogonal.
641 // (1) the difference between using a ranged hash function and using
642 // the combination of a hash function and a range-hashing function.
643 // In the former case we don't have such things as hash codes, so
644 // we have a dummy type as placeholder.
645 // (2) Whether or not we cache hash codes. Caching hash codes is
646 // meaningless if we have a ranged hash function.
647 // We also put the key extraction and equality comparison function
648 // objects here, for convenience.
650 // Primary template: unused except as a hook for specializations.
651 template<typename _Key
, typename _Value
,
652 typename _ExtractKey
, typename _Equal
,
653 typename _H1
, typename _H2
, typename _Hash
,
654 bool __cache_hash_code
>
655 struct _Hash_code_base
;
657 // Specialization: ranged hash function, no caching hash codes. H1
658 // and H2 are provided but ignored. We define a dummy hash code type.
659 template<typename _Key
, typename _Value
,
660 typename _ExtractKey
, typename _Equal
,
661 typename _H1
, typename _H2
, typename _Hash
>
662 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
666 _Hash_code_base(const _ExtractKey
& __ex
, const _Equal
& __eq
,
667 const _H1
&, const _H2
&, const _Hash
& __h
)
668 : _M_extract(__ex
), _M_eq(__eq
), _M_ranged_hash(__h
) { }
670 typedef void* _Hash_code_type
;
673 _M_hash_code(const _Key
& __key
) const
677 _M_bucket_index(const _Key
& __k
, _Hash_code_type
,
678 std::size_t __n
) const
679 { return _M_ranged_hash(__k
, __n
); }
682 _M_bucket_index(const _Hash_node
<_Value
, false>* __p
,
683 std::size_t __n
) const
684 { return _M_ranged_hash(_M_extract(__p
->_M_v
), __n
); }
687 _M_compare(const _Key
& __k
, _Hash_code_type
,
688 _Hash_node
<_Value
, false>* __n
) const
689 { return _M_eq(__k
, _M_extract(__n
->_M_v
)); }
692 _M_store_code(_Hash_node
<_Value
, false>*, _Hash_code_type
) const
696 _M_copy_code(_Hash_node
<_Value
, false>*,
697 const _Hash_node
<_Value
, false>*) const
701 _M_swap(_Hash_code_base
& __x
)
703 std::swap(_M_extract
, __x
._M_extract
);
704 std::swap(_M_eq
, __x
._M_eq
);
705 std::swap(_M_ranged_hash
, __x
._M_ranged_hash
);
709 _ExtractKey _M_extract
;
711 _Hash _M_ranged_hash
;
715 // No specialization for ranged hash function while caching hash codes.
716 // That combination is meaningless, and trying to do it is an error.
719 // Specialization: ranged hash function, cache hash codes. This
720 // combination is meaningless, so we provide only a declaration
721 // and no definition.
722 template<typename _Key
, typename _Value
,
723 typename _ExtractKey
, typename _Equal
,
724 typename _H1
, typename _H2
, typename _Hash
>
725 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
728 // Specialization: hash function and range-hashing function, no
729 // caching of hash codes. H is provided but ignored. Provides
730 // typedef and accessor required by TR1.
731 template<typename _Key
, typename _Value
,
732 typename _ExtractKey
, typename _Equal
,
733 typename _H1
, typename _H2
>
734 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
735 _Default_ranged_hash
, false>
740 hash_function() const
744 _Hash_code_base(const _ExtractKey
& __ex
, const _Equal
& __eq
,
745 const _H1
& __h1
, const _H2
& __h2
,
746 const _Default_ranged_hash
&)
747 : _M_extract(__ex
), _M_eq(__eq
), _M_h1(__h1
), _M_h2(__h2
) { }
749 typedef std::size_t _Hash_code_type
;
752 _M_hash_code(const _Key
& __k
) const
753 { return _M_h1(__k
); }
756 _M_bucket_index(const _Key
&, _Hash_code_type __c
,
757 std::size_t __n
) const
758 { return _M_h2(__c
, __n
); }
761 _M_bucket_index(const _Hash_node
<_Value
, false>* __p
,
762 std::size_t __n
) const
763 { return _M_h2(_M_h1(_M_extract(__p
->_M_v
)), __n
); }
766 _M_compare(const _Key
& __k
, _Hash_code_type
,
767 _Hash_node
<_Value
, false>* __n
) const
768 { return _M_eq(__k
, _M_extract(__n
->_M_v
)); }
771 _M_store_code(_Hash_node
<_Value
, false>*, _Hash_code_type
) const
775 _M_copy_code(_Hash_node
<_Value
, false>*,
776 const _Hash_node
<_Value
, false>*) const
780 _M_swap(_Hash_code_base
& __x
)
782 std::swap(_M_extract
, __x
._M_extract
);
783 std::swap(_M_eq
, __x
._M_eq
);
784 std::swap(_M_h1
, __x
._M_h1
);
785 std::swap(_M_h2
, __x
._M_h2
);
789 _ExtractKey _M_extract
;
795 // Specialization: hash function and range-hashing function,
796 // caching hash codes. H is provided but ignored. Provides
797 // typedef and accessor required by TR1.
798 template<typename _Key
, typename _Value
,
799 typename _ExtractKey
, typename _Equal
,
800 typename _H1
, typename _H2
>
801 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
802 _Default_ranged_hash
, true>
807 hash_function() const
811 _Hash_code_base(const _ExtractKey
& __ex
, const _Equal
& __eq
,
812 const _H1
& __h1
, const _H2
& __h2
,
813 const _Default_ranged_hash
&)
814 : _M_extract(__ex
), _M_eq(__eq
), _M_h1(__h1
), _M_h2(__h2
) { }
816 typedef std::size_t _Hash_code_type
;
819 _M_hash_code(const _Key
& __k
) const
820 { return _M_h1(__k
); }
823 _M_bucket_index(const _Key
&, _Hash_code_type __c
,
824 std::size_t __n
) const
825 { return _M_h2(__c
, __n
); }
828 _M_bucket_index(const _Hash_node
<_Value
, true>* __p
,
829 std::size_t __n
) const
830 { return _M_h2(__p
->_M_hash_code
, __n
); }
833 _M_compare(const _Key
& __k
, _Hash_code_type __c
,
834 _Hash_node
<_Value
, true>* __n
) const
835 { return __c
== __n
->_M_hash_code
&& _M_eq(__k
, _M_extract(__n
->_M_v
)); }
838 _M_store_code(_Hash_node
<_Value
, true>* __n
, _Hash_code_type __c
) const
839 { __n
->_M_hash_code
= __c
; }
842 _M_copy_code(_Hash_node
<_Value
, true>* __to
,
843 const _Hash_node
<_Value
, true>* __from
) const
844 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
847 _M_swap(_Hash_code_base
& __x
)
849 std::swap(_M_extract
, __x
._M_extract
);
850 std::swap(_M_eq
, __x
._M_eq
);
851 std::swap(_M_h1
, __x
._M_h1
);
852 std::swap(_M_h2
, __x
._M_h2
);
856 _ExtractKey _M_extract
;
863 // Class template _Equality_base. This is for implementing equality
864 // comparison for unordered containers, per N3068, by John Lakos and
865 // Pablo Halpern. Algorithmically, we follow closely the reference
866 // implementations therein.
867 template<typename _ExtractKey
, bool __unique_keys
,
869 struct _Equality_base
;
871 template<typename _ExtractKey
, typename _Hashtable
>
872 struct _Equality_base
<_ExtractKey
, true, _Hashtable
>
874 bool _M_equal(const _Hashtable
&) const;
877 template<typename _ExtractKey
, typename _Hashtable
>
879 _Equality_base
<_ExtractKey
, true, _Hashtable
>::
880 _M_equal(const _Hashtable
& __other
) const
882 const _Hashtable
* __this
= static_cast<const _Hashtable
*>(this);
884 if (__this
->size() != __other
.size())
887 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
889 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
890 if (__ity
== __other
.end() || *__ity
!= *__itx
)
896 template<typename _ExtractKey
, typename _Hashtable
>
897 struct _Equality_base
<_ExtractKey
, false, _Hashtable
>
899 bool _M_equal(const _Hashtable
&) const;
902 template<typename _Uiterator
>
904 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
907 // See std::is_permutation in N3068.
908 template<typename _ExtractKey
, typename _Hashtable
>
909 template<typename _Uiterator
>
911 _Equality_base
<_ExtractKey
, false, _Hashtable
>::
912 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
915 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
916 if (!(*__first1
== *__first2
))
919 if (__first1
== __last1
)
922 _Uiterator __last2
= __first2
;
923 std::advance(__last2
, std::distance(__first1
, __last1
));
925 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
927 _Uiterator __tmp
= __first1
;
928 while (__tmp
!= __it1
&& !(*__tmp
== *__it1
))
931 // We've seen this one before.
935 std::ptrdiff_t __n2
= 0;
936 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
937 if (*__tmp
== *__it1
)
943 std::ptrdiff_t __n1
= 0;
944 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
945 if (*__tmp
== *__it1
)
954 template<typename _ExtractKey
, typename _Hashtable
>
956 _Equality_base
<_ExtractKey
, false, _Hashtable
>::
957 _M_equal(const _Hashtable
& __other
) const
959 const _Hashtable
* __this
= static_cast<const _Hashtable
*>(this);
961 if (__this
->size() != __other
.size())
964 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
966 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
967 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
969 if (std::distance(__xrange
.first
, __xrange
.second
)
970 != std::distance(__yrange
.first
, __yrange
.second
))
973 if (!_S_is_permutation(__xrange
.first
,
978 __itx
= __xrange
.second
;
983 _GLIBCXX_END_NAMESPACE_VERSION
984 } // namespace __detail
987 #endif // _HASHTABLE_POLICY_H