1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010 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 * You should not attempt to use it directly.
30 #ifndef _HASHTABLE_POLICY_H
31 #define _HASHTABLE_POLICY_H 1
37 // Helper function: return distance(first, last) for forward
38 // iterators, or 0 for input iterators.
39 template<class _Iterator
>
40 inline typename
std::iterator_traits
<_Iterator
>::difference_type
41 __distance_fw(_Iterator __first
, _Iterator __last
,
42 std::input_iterator_tag
)
45 template<class _Iterator
>
46 inline typename
std::iterator_traits
<_Iterator
>::difference_type
47 __distance_fw(_Iterator __first
, _Iterator __last
,
48 std::forward_iterator_tag
)
49 { return std::distance(__first
, __last
); }
51 template<class _Iterator
>
52 inline typename
std::iterator_traits
<_Iterator
>::difference_type
53 __distance_fw(_Iterator __first
, _Iterator __last
)
55 typedef typename
std::iterator_traits
<_Iterator
>::iterator_category _Tag
;
56 return __distance_fw(__first
, __last
, _Tag());
59 template<typename _RAIter
, typename _Tp
>
61 __lower_bound(_RAIter __first
, _RAIter __last
, const _Tp
& __val
)
63 typedef typename
std::iterator_traits
<_RAIter
>::difference_type _DType
;
65 _DType __len
= __last
- __first
;
68 _DType __half
= __len
>> 1;
69 _RAIter __middle
= __first
+ __half
;
70 if (*__middle
< __val
)
74 __len
= __len
- __half
- 1;
82 // Auxiliary types used for all instantiations of _Hashtable: nodes
85 // Nodes, used to wrap elements stored in the hash table. A policy
86 // template parameter of class template _Hashtable controls whether
87 // nodes also store a hash code. In some cases (e.g. strings) this
88 // may be a performance win.
89 template<typename _Value
, bool __cache_hash_code
>
92 template<typename _Value
>
93 struct _Hash_node
<_Value
, true>
96 std::size_t _M_hash_code
;
99 template<typename
... _Args
>
100 _Hash_node(_Args
&&... __args
)
101 : _M_v(std::forward
<_Args
>(__args
)...),
102 _M_hash_code(), _M_next() { }
105 template<typename _Value
>
106 struct _Hash_node
<_Value
, false>
111 template<typename
... _Args
>
112 _Hash_node(_Args
&&... __args
)
113 : _M_v(std::forward
<_Args
>(__args
)...),
117 // Local iterators, used to iterate within a bucket but not between
119 template<typename _Value
, bool __cache
>
120 struct _Node_iterator_base
122 _Node_iterator_base(_Hash_node
<_Value
, __cache
>* __p
)
127 { _M_cur
= _M_cur
->_M_next
; }
129 _Hash_node
<_Value
, __cache
>* _M_cur
;
132 template<typename _Value
, bool __cache
>
134 operator==(const _Node_iterator_base
<_Value
, __cache
>& __x
,
135 const _Node_iterator_base
<_Value
, __cache
>& __y
)
136 { return __x
._M_cur
== __y
._M_cur
; }
138 template<typename _Value
, bool __cache
>
140 operator!=(const _Node_iterator_base
<_Value
, __cache
>& __x
,
141 const _Node_iterator_base
<_Value
, __cache
>& __y
)
142 { return __x
._M_cur
!= __y
._M_cur
; }
144 template<typename _Value
, bool __constant_iterators
, bool __cache
>
145 struct _Node_iterator
146 : public _Node_iterator_base
<_Value
, __cache
>
148 typedef _Value value_type
;
149 typedef typename
std::conditional
<__constant_iterators
,
150 const _Value
*, _Value
*>::type
152 typedef typename
std::conditional
<__constant_iterators
,
153 const _Value
&, _Value
&>::type
155 typedef std::ptrdiff_t difference_type
;
156 typedef std::forward_iterator_tag iterator_category
;
159 : _Node_iterator_base
<_Value
, __cache
>(0) { }
162 _Node_iterator(_Hash_node
<_Value
, __cache
>* __p
)
163 : _Node_iterator_base
<_Value
, __cache
>(__p
) { }
167 { return this->_M_cur
->_M_v
; }
171 { return &this->_M_cur
->_M_v
; }
183 _Node_iterator
__tmp(*this);
189 template<typename _Value
, bool __constant_iterators
, bool __cache
>
190 struct _Node_const_iterator
191 : public _Node_iterator_base
<_Value
, __cache
>
193 typedef _Value value_type
;
194 typedef const _Value
* pointer
;
195 typedef const _Value
& reference
;
196 typedef std::ptrdiff_t difference_type
;
197 typedef std::forward_iterator_tag iterator_category
;
199 _Node_const_iterator()
200 : _Node_iterator_base
<_Value
, __cache
>(0) { }
203 _Node_const_iterator(_Hash_node
<_Value
, __cache
>* __p
)
204 : _Node_iterator_base
<_Value
, __cache
>(__p
) { }
206 _Node_const_iterator(const _Node_iterator
<_Value
, __constant_iterators
,
208 : _Node_iterator_base
<_Value
, __cache
>(__x
._M_cur
) { }
212 { return this->_M_cur
->_M_v
; }
216 { return &this->_M_cur
->_M_v
; }
218 _Node_const_iterator
&
228 _Node_const_iterator
__tmp(*this);
234 template<typename _Value
, bool __cache
>
235 struct _Hashtable_iterator_base
237 _Hashtable_iterator_base(_Hash_node
<_Value
, __cache
>* __node
,
238 _Hash_node
<_Value
, __cache
>** __bucket
)
239 : _M_cur_node(__node
), _M_cur_bucket(__bucket
) { }
244 _M_cur_node
= _M_cur_node
->_M_next
;
252 _Hash_node
<_Value
, __cache
>* _M_cur_node
;
253 _Hash_node
<_Value
, __cache
>** _M_cur_bucket
;
256 // Global iterators, used for arbitrary iteration within a hash
257 // table. Larger and more expensive than local iterators.
258 template<typename _Value
, bool __cache
>
260 _Hashtable_iterator_base
<_Value
, __cache
>::
265 // This loop requires the bucket array to have a non-null sentinel.
266 while (!*_M_cur_bucket
)
268 _M_cur_node
= *_M_cur_bucket
;
271 template<typename _Value
, bool __cache
>
273 operator==(const _Hashtable_iterator_base
<_Value
, __cache
>& __x
,
274 const _Hashtable_iterator_base
<_Value
, __cache
>& __y
)
275 { return __x
._M_cur_node
== __y
._M_cur_node
; }
277 template<typename _Value
, bool __cache
>
279 operator!=(const _Hashtable_iterator_base
<_Value
, __cache
>& __x
,
280 const _Hashtable_iterator_base
<_Value
, __cache
>& __y
)
281 { return __x
._M_cur_node
!= __y
._M_cur_node
; }
283 template<typename _Value
, bool __constant_iterators
, bool __cache
>
284 struct _Hashtable_iterator
285 : public _Hashtable_iterator_base
<_Value
, __cache
>
287 typedef _Value value_type
;
288 typedef typename
std::conditional
<__constant_iterators
,
289 const _Value
*, _Value
*>::type
291 typedef typename
std::conditional
<__constant_iterators
,
292 const _Value
&, _Value
&>::type
294 typedef std::ptrdiff_t difference_type
;
295 typedef std::forward_iterator_tag iterator_category
;
297 _Hashtable_iterator()
298 : _Hashtable_iterator_base
<_Value
, __cache
>(0, 0) { }
300 _Hashtable_iterator(_Hash_node
<_Value
, __cache
>* __p
,
301 _Hash_node
<_Value
, __cache
>** __b
)
302 : _Hashtable_iterator_base
<_Value
, __cache
>(__p
, __b
) { }
305 _Hashtable_iterator(_Hash_node
<_Value
, __cache
>** __b
)
306 : _Hashtable_iterator_base
<_Value
, __cache
>(*__b
, __b
) { }
310 { return this->_M_cur_node
->_M_v
; }
314 { return &this->_M_cur_node
->_M_v
; }
326 _Hashtable_iterator
__tmp(*this);
332 template<typename _Value
, bool __constant_iterators
, bool __cache
>
333 struct _Hashtable_const_iterator
334 : public _Hashtable_iterator_base
<_Value
, __cache
>
336 typedef _Value value_type
;
337 typedef const _Value
* pointer
;
338 typedef const _Value
& reference
;
339 typedef std::ptrdiff_t difference_type
;
340 typedef std::forward_iterator_tag iterator_category
;
342 _Hashtable_const_iterator()
343 : _Hashtable_iterator_base
<_Value
, __cache
>(0, 0) { }
345 _Hashtable_const_iterator(_Hash_node
<_Value
, __cache
>* __p
,
346 _Hash_node
<_Value
, __cache
>** __b
)
347 : _Hashtable_iterator_base
<_Value
, __cache
>(__p
, __b
) { }
350 _Hashtable_const_iterator(_Hash_node
<_Value
, __cache
>** __b
)
351 : _Hashtable_iterator_base
<_Value
, __cache
>(*__b
, __b
) { }
353 _Hashtable_const_iterator(const _Hashtable_iterator
<_Value
,
354 __constant_iterators
, __cache
>& __x
)
355 : _Hashtable_iterator_base
<_Value
, __cache
>(__x
._M_cur_node
,
356 __x
._M_cur_bucket
) { }
360 { return this->_M_cur_node
->_M_v
; }
364 { return &this->_M_cur_node
->_M_v
; }
366 _Hashtable_const_iterator
&
373 _Hashtable_const_iterator
376 _Hashtable_const_iterator
__tmp(*this);
383 // Many of class template _Hashtable's template parameters are policy
384 // classes. These are defaults for the policies.
386 // Default range hashing function: use division to fold a large number
387 // into the range [0, N).
388 struct _Mod_range_hashing
390 typedef std::size_t first_argument_type
;
391 typedef std::size_t second_argument_type
;
392 typedef std::size_t result_type
;
395 operator()(first_argument_type __num
, second_argument_type __den
) const
396 { return __num
% __den
; }
399 // Default ranged hash function H. In principle it should be a
400 // function object composed from objects of type H1 and H2 such that
401 // h(k, N) = h2(h1(k), N), but that would mean making extra copies of
402 // h1 and h2. So instead we'll just use a tag to tell class template
403 // hashtable to do that composition.
404 struct _Default_ranged_hash
{ };
406 // Default value for rehash policy. Bucket size is (usually) the
407 // smallest prime that keeps the load factor small enough.
408 struct _Prime_rehash_policy
410 _Prime_rehash_policy(float __z
= 1.0)
411 : _M_max_load_factor(__z
), _M_growth_factor(2.f
), _M_next_resize(0) { }
414 max_load_factor() const
415 { return _M_max_load_factor
; }
417 // Return a bucket size no smaller than n.
419 _M_next_bkt(std::size_t __n
) const;
421 // Return a bucket count appropriate for n elements
423 _M_bkt_for_elements(std::size_t __n
) const;
425 // __n_bkt is current bucket count, __n_elt is current element count,
426 // and __n_ins is number of elements to be inserted. Do we need to
427 // increase bucket count? If so, return make_pair(true, n), where n
428 // is the new bucket count. If not, return make_pair(false, 0).
429 std::pair
<bool, std::size_t>
430 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
431 std::size_t __n_ins
) const;
433 enum { _S_n_primes
= sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
435 float _M_max_load_factor
;
436 float _M_growth_factor
;
437 mutable std::size_t _M_next_resize
;
440 extern const unsigned long __prime_list
[];
442 // XXX This is a hack. There's no good reason for any of
443 // _Prime_rehash_policy's member functions to be inline.
445 // Return a prime no smaller than n.
447 _Prime_rehash_policy::
448 _M_next_bkt(std::size_t __n
) const
450 const unsigned long* __p
= __lower_bound(__prime_list
, __prime_list
453 static_cast<std::size_t>(__builtin_ceil(*__p
* _M_max_load_factor
));
457 // Return the smallest prime p such that alpha p >= n, where alpha
458 // is the load factor.
460 _Prime_rehash_policy::
461 _M_bkt_for_elements(std::size_t __n
) const
463 const float __min_bkts
= __n
/ _M_max_load_factor
;
464 const unsigned long* __p
= __lower_bound(__prime_list
, __prime_list
465 + _S_n_primes
, __min_bkts
);
467 static_cast<std::size_t>(__builtin_ceil(*__p
* _M_max_load_factor
));
471 // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
472 // If p > __n_bkt, return make_pair(true, p); otherwise return
473 // make_pair(false, 0). In principle this isn't very different from
474 // _M_bkt_for_elements.
476 // The only tricky part is that we're caching the element count at
477 // which we need to rehash, so we don't have to do a floating-point
478 // multiply for every insertion.
480 inline std::pair
<bool, std::size_t>
481 _Prime_rehash_policy::
482 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
483 std::size_t __n_ins
) const
485 if (__n_elt
+ __n_ins
> _M_next_resize
)
487 float __min_bkts
= ((float(__n_ins
) + float(__n_elt
))
488 / _M_max_load_factor
);
489 if (__min_bkts
> __n_bkt
)
491 __min_bkts
= std::max(__min_bkts
, _M_growth_factor
* __n_bkt
);
492 const unsigned long* __p
=
493 __lower_bound(__prime_list
, __prime_list
+ _S_n_primes
,
495 _M_next_resize
= static_cast<std::size_t>
496 (__builtin_ceil(*__p
* _M_max_load_factor
));
497 return std::make_pair(true, *__p
);
501 _M_next_resize
= static_cast<std::size_t>
502 (__builtin_ceil(__n_bkt
* _M_max_load_factor
));
503 return std::make_pair(false, 0);
507 return std::make_pair(false, 0);
510 // Base classes for std::tr1::_Hashtable. We define these base
511 // classes because in some cases we want to do different things
512 // depending on the value of a policy class. In some cases the
513 // policy class affects which member functions and nested typedefs
514 // are defined; we handle that by specializing base class templates.
515 // Several of the base class templates need to access other members
516 // of class template _Hashtable, so we use the "curiously recurring
517 // template pattern" for them.
519 // class template _Map_base. If the hashtable has a value type of the
520 // form pair<T1, T2> and a key extraction policy that returns the
521 // first part of the pair, the hashtable gets a mapped_type typedef.
522 // If it satisfies those criteria and also has unique keys, then it
523 // also gets an operator[].
524 template<typename _Key
, typename _Value
, typename _Ex
, bool __unique
,
526 struct _Map_base
{ };
528 template<typename _Key
, typename _Pair
, typename _Hashtable
>
529 struct _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, false, _Hashtable
>
531 typedef typename
_Pair::second_type mapped_type
;
534 template<typename _Key
, typename _Pair
, typename _Hashtable
>
535 struct _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>
537 typedef typename
_Pair::second_type mapped_type
;
540 operator[](const _Key
& __k
);
542 // _GLIBCXX_RESOLVE_LIB_DEFECTS
543 // DR 761. unordered_map needs an at() member function.
548 at(const _Key
& __k
) const;
551 template<typename _Key
, typename _Pair
, typename _Hashtable
>
552 typename _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>,
553 true, _Hashtable
>::mapped_type
&
554 _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>::
555 operator[](const _Key
& __k
)
557 _Hashtable
* __h
= static_cast<_Hashtable
*>(this);
558 typename
_Hashtable::_Hash_code_type __code
= __h
->_M_hash_code(__k
);
559 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
,
560 __h
->_M_bucket_count
);
562 typename
_Hashtable::_Node
* __p
=
563 __h
->_M_find_node(__h
->_M_buckets
[__n
], __k
, __code
);
565 return __h
->_M_insert_bucket(std::make_pair(__k
, mapped_type()),
566 __n
, __code
)->second
;
567 return (__p
->_M_v
).second
;
570 template<typename _Key
, typename _Pair
, typename _Hashtable
>
571 typename _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>,
572 true, _Hashtable
>::mapped_type
&
573 _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>::
576 _Hashtable
* __h
= static_cast<_Hashtable
*>(this);
577 typename
_Hashtable::_Hash_code_type __code
= __h
->_M_hash_code(__k
);
578 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
,
579 __h
->_M_bucket_count
);
581 typename
_Hashtable::_Node
* __p
=
582 __h
->_M_find_node(__h
->_M_buckets
[__n
], __k
, __code
);
584 __throw_out_of_range(__N("_Map_base::at"));
585 return (__p
->_M_v
).second
;
588 template<typename _Key
, typename _Pair
, typename _Hashtable
>
589 const typename _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>,
590 true, _Hashtable
>::mapped_type
&
591 _Map_base
<_Key
, _Pair
, std::_Select1st
<_Pair
>, true, _Hashtable
>::
592 at(const _Key
& __k
) const
594 const _Hashtable
* __h
= static_cast<const _Hashtable
*>(this);
595 typename
_Hashtable::_Hash_code_type __code
= __h
->_M_hash_code(__k
);
596 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
,
597 __h
->_M_bucket_count
);
599 typename
_Hashtable::_Node
* __p
=
600 __h
->_M_find_node(__h
->_M_buckets
[__n
], __k
, __code
);
602 __throw_out_of_range(__N("_Map_base::at"));
603 return (__p
->_M_v
).second
;
606 // class template _Rehash_base. Give hashtable the max_load_factor
607 // functions iff the rehash policy is _Prime_rehash_policy.
608 template<typename _RehashPolicy
, typename _Hashtable
>
609 struct _Rehash_base
{ };
611 template<typename _Hashtable
>
612 struct _Rehash_base
<_Prime_rehash_policy
, _Hashtable
>
615 max_load_factor() const
617 const _Hashtable
* __this
= static_cast<const _Hashtable
*>(this);
618 return __this
->__rehash_policy().max_load_factor();
622 max_load_factor(float __z
)
624 _Hashtable
* __this
= static_cast<_Hashtable
*>(this);
625 __this
->__rehash_policy(_Prime_rehash_policy(__z
));
629 // Class template _Hash_code_base. Encapsulates two policy issues that
630 // aren't quite orthogonal.
631 // (1) the difference between using a ranged hash function and using
632 // the combination of a hash function and a range-hashing function.
633 // In the former case we don't have such things as hash codes, so
634 // we have a dummy type as placeholder.
635 // (2) Whether or not we cache hash codes. Caching hash codes is
636 // meaningless if we have a ranged hash function.
637 // We also put the key extraction and equality comparison function
638 // objects here, for convenience.
640 // Primary template: unused except as a hook for specializations.
641 template<typename _Key
, typename _Value
,
642 typename _ExtractKey
, typename _Equal
,
643 typename _H1
, typename _H2
, typename _Hash
,
644 bool __cache_hash_code
>
645 struct _Hash_code_base
;
647 // Specialization: ranged hash function, no caching hash codes. H1
648 // and H2 are provided but ignored. We define a dummy hash code type.
649 template<typename _Key
, typename _Value
,
650 typename _ExtractKey
, typename _Equal
,
651 typename _H1
, typename _H2
, typename _Hash
>
652 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
656 _Hash_code_base(const _ExtractKey
& __ex
, const _Equal
& __eq
,
657 const _H1
&, const _H2
&, const _Hash
& __h
)
658 : _M_extract(__ex
), _M_eq(__eq
), _M_ranged_hash(__h
) { }
660 typedef void* _Hash_code_type
;
663 _M_hash_code(const _Key
& __key
) const
667 _M_bucket_index(const _Key
& __k
, _Hash_code_type
,
668 std::size_t __n
) const
669 { return _M_ranged_hash(__k
, __n
); }
672 _M_bucket_index(const _Hash_node
<_Value
, false>* __p
,
673 std::size_t __n
) const
674 { return _M_ranged_hash(_M_extract(__p
->_M_v
), __n
); }
677 _M_compare(const _Key
& __k
, _Hash_code_type
,
678 _Hash_node
<_Value
, false>* __n
) const
679 { return _M_eq(__k
, _M_extract(__n
->_M_v
)); }
682 _M_store_code(_Hash_node
<_Value
, false>*, _Hash_code_type
) const
686 _M_copy_code(_Hash_node
<_Value
, false>*,
687 const _Hash_node
<_Value
, false>*) const
691 _M_swap(_Hash_code_base
& __x
)
693 std::swap(_M_extract
, __x
._M_extract
);
694 std::swap(_M_eq
, __x
._M_eq
);
695 std::swap(_M_ranged_hash
, __x
._M_ranged_hash
);
699 _ExtractKey _M_extract
;
701 _Hash _M_ranged_hash
;
705 // No specialization for ranged hash function while caching hash codes.
706 // That combination is meaningless, and trying to do it is an error.
709 // Specialization: ranged hash function, cache hash codes. This
710 // combination is meaningless, so we provide only a declaration
711 // and no definition.
712 template<typename _Key
, typename _Value
,
713 typename _ExtractKey
, typename _Equal
,
714 typename _H1
, typename _H2
, typename _Hash
>
715 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
718 // Specialization: hash function and range-hashing function, no
719 // caching of hash codes. H is provided but ignored. Provides
720 // typedef and accessor required by TR1.
721 template<typename _Key
, typename _Value
,
722 typename _ExtractKey
, typename _Equal
,
723 typename _H1
, typename _H2
>
724 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
725 _Default_ranged_hash
, false>
730 hash_function() const
734 _Hash_code_base(const _ExtractKey
& __ex
, const _Equal
& __eq
,
735 const _H1
& __h1
, const _H2
& __h2
,
736 const _Default_ranged_hash
&)
737 : _M_extract(__ex
), _M_eq(__eq
), _M_h1(__h1
), _M_h2(__h2
) { }
739 typedef std::size_t _Hash_code_type
;
742 _M_hash_code(const _Key
& __k
) const
743 { return _M_h1(__k
); }
746 _M_bucket_index(const _Key
&, _Hash_code_type __c
,
747 std::size_t __n
) const
748 { return _M_h2(__c
, __n
); }
751 _M_bucket_index(const _Hash_node
<_Value
, false>* __p
,
752 std::size_t __n
) const
753 { return _M_h2(_M_h1(_M_extract(__p
->_M_v
)), __n
); }
756 _M_compare(const _Key
& __k
, _Hash_code_type
,
757 _Hash_node
<_Value
, false>* __n
) const
758 { return _M_eq(__k
, _M_extract(__n
->_M_v
)); }
761 _M_store_code(_Hash_node
<_Value
, false>*, _Hash_code_type
) const
765 _M_copy_code(_Hash_node
<_Value
, false>*,
766 const _Hash_node
<_Value
, false>*) const
770 _M_swap(_Hash_code_base
& __x
)
772 std::swap(_M_extract
, __x
._M_extract
);
773 std::swap(_M_eq
, __x
._M_eq
);
774 std::swap(_M_h1
, __x
._M_h1
);
775 std::swap(_M_h2
, __x
._M_h2
);
779 _ExtractKey _M_extract
;
785 // Specialization: hash function and range-hashing function,
786 // caching hash codes. H is provided but ignored. Provides
787 // typedef and accessor required by TR1.
788 template<typename _Key
, typename _Value
,
789 typename _ExtractKey
, typename _Equal
,
790 typename _H1
, typename _H2
>
791 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _Equal
, _H1
, _H2
,
792 _Default_ranged_hash
, true>
797 hash_function() const
801 _Hash_code_base(const _ExtractKey
& __ex
, const _Equal
& __eq
,
802 const _H1
& __h1
, const _H2
& __h2
,
803 const _Default_ranged_hash
&)
804 : _M_extract(__ex
), _M_eq(__eq
), _M_h1(__h1
), _M_h2(__h2
) { }
806 typedef std::size_t _Hash_code_type
;
809 _M_hash_code(const _Key
& __k
) const
810 { return _M_h1(__k
); }
813 _M_bucket_index(const _Key
&, _Hash_code_type __c
,
814 std::size_t __n
) const
815 { return _M_h2(__c
, __n
); }
818 _M_bucket_index(const _Hash_node
<_Value
, true>* __p
,
819 std::size_t __n
) const
820 { return _M_h2(__p
->_M_hash_code
, __n
); }
823 _M_compare(const _Key
& __k
, _Hash_code_type __c
,
824 _Hash_node
<_Value
, true>* __n
) const
825 { return __c
== __n
->_M_hash_code
&& _M_eq(__k
, _M_extract(__n
->_M_v
)); }
828 _M_store_code(_Hash_node
<_Value
, true>* __n
, _Hash_code_type __c
) const
829 { __n
->_M_hash_code
= __c
; }
832 _M_copy_code(_Hash_node
<_Value
, true>* __to
,
833 const _Hash_node
<_Value
, true>* __from
) const
834 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
837 _M_swap(_Hash_code_base
& __x
)
839 std::swap(_M_extract
, __x
._M_extract
);
840 std::swap(_M_eq
, __x
._M_eq
);
841 std::swap(_M_h1
, __x
._M_h1
);
842 std::swap(_M_h2
, __x
._M_h2
);
846 _ExtractKey _M_extract
;
851 } // namespace __detail
854 #endif // _HASHTABLE_POLICY_H