31 #ifndef _HASHTABLE_POLICY_H
32 #define _HASHTABLE_POLICY_H 1
34 namespace std _GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 template<
class _Iterator>
43 inline typename std::iterator_traits<_Iterator>::difference_type
44 __distance_fw(_Iterator __first, _Iterator __last,
48 template<
class _Iterator>
49 inline typename std::iterator_traits<_Iterator>::difference_type
50 __distance_fw(_Iterator __first, _Iterator __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());
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)...),
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;
130 const _Value*, _Value*>::type
133 const _Value&, _Value&>::type
135 typedef std::ptrdiff_t difference_type;
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;
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;
238 template<
typename _Value,
bool __cache>
240 _Hashtable_iterator_base<_Value, __cache>::
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;
269 const _Value*, _Value*>::type
272 const _Value&, _Value&>::type
274 typedef std::ptrdiff_t difference_type;
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;
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);
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; }
384 struct _Default_ranged_hash { };
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
395 {
return _M_max_load_factor; }
399 _M_next_bkt(std::size_t __n)
const;
403 _M_bkt_for_elements(std::size_t __n)
const;
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[];
427 _Prime_rehash_policy::
428 _M_next_bkt(std::size_t __n)
const
433 static_cast<std::size_t
>(__builtin_ceil(*__p * _M_max_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;
445 + _S_n_primes, __min_bkts);
447 static_cast<std::size_t
>(__builtin_ceil(*__p * _M_max_load_factor));
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 =
475 _M_next_resize =
static_cast<std::size_t
>
476 (__builtin_ceil(*__p * _M_max_load_factor));
481 _M_next_resize =
static_cast<std::size_t
>
482 (__builtin_ceil(__n_bkt * _M_max_load_factor));
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;
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);
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);
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;
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
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()));
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;
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;
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,
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;
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;
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() || !bool(*__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);
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;
925 for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
927 _Uiterator __tmp = __first1;
928 while (__tmp != __it1 && !
bool(*__tmp == *__it1))
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));
973 if (!_S_is_permutation(__xrange.first,
978 __itx = __xrange.second;
983 _GLIBCXX_END_NAMESPACE_VERSION
987 #endif // _HASHTABLE_POLICY_H
reference operator[](size_t __position)
Array-indexing support.
Struct holding two objects of arbitrary type.
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Forward iterators support a superset of input iterator operations.
_ForwardIterator lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__val)
Finds the first position in which val could be inserted without changing the ordering.
pair< typename __decay_and_strip< _T1 >::__type, typename __decay_and_strip< _T2 >::__type > make_pair(_T1 &&__x, _T2 &&__y)
A convenience wrapper for creating a pair from two objects.
iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
_Tp && forward(typename std::remove_reference< _Tp >::type &__t)
forward (as per N3143)