* include/bits/hashtable.h: Improve comments.
[official-gcc.git] / libstdc++-v3 / include / bits / hashtable_policy.h
blob023f46df89aa5849327bc58d98e68c6242dda413
1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010, 2011, 2012 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
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)
36 _GLIBCXX_BEGIN_NAMESPACE_VERSION
38 template<typename _Key, typename _Value, typename _Alloc,
39 typename _ExtractKey, typename _Equal,
40 typename _H1, typename _H2, typename _Hash,
41 typename _RehashPolicy, typename _Traits>
42 class _Hashtable;
44 _GLIBCXX_END_NAMESPACE_VERSION
46 namespace __detail
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 /**
51 * @defgroup hashtable-detail Base and Implementation Classes
52 * @ingroup unordered_associative_containers
53 * @{
55 template<typename _Key, typename _Value,
56 typename _ExtractKey, typename _Equal,
57 typename _H1, typename _H2, typename _Hash, typename _Traits>
58 struct _Hashtable_base;
60 // Helper function: return distance(first, last) for forward
61 // iterators, or 0 for input iterators.
62 template<class _Iterator>
63 inline typename std::iterator_traits<_Iterator>::difference_type
64 __distance_fw(_Iterator __first, _Iterator __last,
65 std::input_iterator_tag)
66 { return 0; }
68 template<class _Iterator>
69 inline typename std::iterator_traits<_Iterator>::difference_type
70 __distance_fw(_Iterator __first, _Iterator __last,
71 std::forward_iterator_tag)
72 { return std::distance(__first, __last); }
74 template<class _Iterator>
75 inline typename std::iterator_traits<_Iterator>::difference_type
76 __distance_fw(_Iterator __first, _Iterator __last)
78 typedef typename std::iterator_traits<_Iterator>::iterator_category _Tag;
79 return __distance_fw(__first, __last, _Tag());
82 // Helper type used to detect whether the hash functor is noexcept.
83 template <typename _Key, typename _Hash>
84 struct __is_noexcept_hash : std::integral_constant<bool,
85 noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
86 { };
88 struct _Identity
90 template<typename _Tp>
91 _Tp&&
92 operator()(_Tp&& __x) const
93 { return std::forward<_Tp>(__x); }
96 struct _Select1st
98 template<typename _Tp>
99 auto
100 operator()(_Tp&& __x) const
101 -> decltype(std::get<0>(std::forward<_Tp>(__x)))
102 { return std::get<0>(std::forward<_Tp>(__x)); }
105 // Auxiliary types used for all instantiations of _Hashtable nodes
106 // and iterators.
109 * struct _Hashtable_traits
111 * Important traits for hash tables.
113 * @tparam _Cache_hash_code Boolean value. True if the value of
114 * the hash function is stored along with the value. This is a
115 * time-space tradeoff. Storing it may improve lookup speed by
116 * reducing the number of times we need to call the _Equal
117 * function.
119 * @tparam _Constant_iterators Boolean value. True if iterator and
120 * const_iterator are both constant iterator types. This is true
121 * for unordered_set and unordered_multiset, false for
122 * unordered_map and unordered_multimap.
124 * @tparam _Unique_keys Boolean value. True if the return value
125 * of _Hashtable::count(k) is always at most one, false if it may
126 * be an arbitrary number. This is true for unordered_set and
127 * unordered_map, false for unordered_multiset and
128 * unordered_multimap.
130 template<bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys>
131 struct _Hashtable_traits
133 template<bool _Cond>
134 using __bool_constant = integral_constant<bool, _Cond>;
136 using __hash_cached = __bool_constant<_Cache_hash_code>;
137 using __constant_iterators = __bool_constant<_Constant_iterators>;
138 using __unique_keys = __bool_constant<_Unique_keys>;
142 * struct _Hash_node_base
144 * Nodes, used to wrap elements stored in the hash table. A policy
145 * template parameter of class template _Hashtable controls whether
146 * nodes also store a hash code. In some cases (e.g. strings) this
147 * may be a performance win.
149 struct _Hash_node_base
151 _Hash_node_base* _M_nxt;
153 _Hash_node_base() : _M_nxt() { }
155 _Hash_node_base(_Hash_node_base* __next) : _M_nxt(__next) { }
159 * Primary template struct _Hash_node.
161 template<typename _Value, bool _Cache_hash_code>
162 struct _Hash_node;
165 * Specialization for nodes with caches, struct _Hash_node.
167 * Base class is __detail::_Hash_node_base.
169 template<typename _Value>
170 struct _Hash_node<_Value, true> : _Hash_node_base
172 _Value _M_v;
173 std::size_t _M_hash_code;
175 template<typename... _Args>
176 _Hash_node(_Args&&... __args)
177 : _M_v(std::forward<_Args>(__args)...), _M_hash_code() { }
179 _Hash_node*
180 _M_next() const { return static_cast<_Hash_node*>(_M_nxt); }
184 * Specialization for nodes without caches, struct _Hash_node.
186 * Base class is __detail::_Hash_node_base.
188 template<typename _Value>
189 struct _Hash_node<_Value, false> : _Hash_node_base
191 _Value _M_v;
193 template<typename... _Args>
194 _Hash_node(_Args&&... __args)
195 : _M_v(std::forward<_Args>(__args)...) { }
197 _Hash_node*
198 _M_next() const { return static_cast<_Hash_node*>(_M_nxt); }
201 /// Base class for node iterators.
202 template<typename _Value, bool _Cache_hash_code>
203 struct _Node_iterator_base
205 typedef _Hash_node<_Value, _Cache_hash_code> __node_type;
207 __node_type* _M_cur;
209 _Node_iterator_base(__node_type* __p)
210 : _M_cur(__p) { }
212 void
213 _M_incr()
214 { _M_cur = _M_cur->_M_next(); }
217 template<typename _Value, bool _Cache_hash_code>
218 inline bool
219 operator==(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
220 const _Node_iterator_base<_Value, _Cache_hash_code >& __y)
221 { return __x._M_cur == __y._M_cur; }
223 template<typename _Value, bool _Cache_hash_code>
224 inline bool
225 operator!=(const _Node_iterator_base<_Value, _Cache_hash_code>& __x,
226 const _Node_iterator_base<_Value, _Cache_hash_code>& __y)
227 { return __x._M_cur != __y._M_cur; }
229 /// Node iterators, used to iterate through all the hashtable.
230 template<typename _Value, bool __constant_iterators, bool __cache>
231 struct _Node_iterator
232 : public _Node_iterator_base<_Value, __cache>
234 private:
235 using __base_type = _Node_iterator_base<_Value, __cache>;
236 using __node_type = typename __base_type::__node_type;
238 public:
239 typedef _Value value_type;
240 typedef std::ptrdiff_t difference_type;
241 typedef std::forward_iterator_tag iterator_category;
243 using pointer = typename std::conditional<__constant_iterators,
244 const _Value*, _Value*>::type;
246 using reference = typename std::conditional<__constant_iterators,
247 const _Value&, _Value&>::type;
249 _Node_iterator()
250 : __base_type(0) { }
252 explicit
253 _Node_iterator(__node_type* __p)
254 : __base_type(__p) { }
256 reference
257 operator*() const
258 { return this->_M_cur->_M_v; }
260 pointer
261 operator->() const
262 { return std::__addressof(this->_M_cur->_M_v); }
264 _Node_iterator&
265 operator++()
267 this->_M_incr();
268 return *this;
271 _Node_iterator
272 operator++(int)
274 _Node_iterator __tmp(*this);
275 this->_M_incr();
276 return __tmp;
280 /// Node const_iterators, used to iterate through all the hashtable.
281 template<typename _Value, bool __constant_iterators, bool __cache>
282 struct _Node_const_iterator
283 : public _Node_iterator_base<_Value, __cache>
285 private:
286 using __base_type = _Node_iterator_base<_Value, __cache>;
287 using __node_type = typename __base_type::__node_type;
289 public:
290 typedef _Value value_type;
291 typedef std::ptrdiff_t difference_type;
292 typedef std::forward_iterator_tag iterator_category;
294 typedef const _Value* pointer;
295 typedef const _Value& reference;
297 _Node_const_iterator()
298 : __base_type(0) { }
300 explicit
301 _Node_const_iterator(__node_type* __p)
302 : __base_type(__p) { }
304 _Node_const_iterator(const _Node_iterator<_Value, __constant_iterators,
305 __cache>& __x)
306 : __base_type(__x._M_cur) { }
308 reference
309 operator*() const
310 { return this->_M_cur->_M_v; }
312 pointer
313 operator->() const
314 { return std::__addressof(this->_M_cur->_M_v); }
316 _Node_const_iterator&
317 operator++()
319 this->_M_incr();
320 return *this;
323 _Node_const_iterator
324 operator++(int)
326 _Node_const_iterator __tmp(*this);
327 this->_M_incr();
328 return __tmp;
332 // Many of class template _Hashtable's template parameters are policy
333 // classes. These are defaults for the policies.
335 /// Default range hashing function: use division to fold a large number
336 /// into the range [0, N).
337 struct _Mod_range_hashing
339 typedef std::size_t first_argument_type;
340 typedef std::size_t second_argument_type;
341 typedef std::size_t result_type;
343 result_type
344 operator()(first_argument_type __num, second_argument_type __den) const
345 { return __num % __den; }
348 /// Default ranged hash function H. In principle it should be a
349 /// function object composed from objects of type H1 and H2 such that
350 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
351 /// h1 and h2. So instead we'll just use a tag to tell class template
352 /// hashtable to do that composition.
353 struct _Default_ranged_hash { };
355 /// Default value for rehash policy. Bucket size is (usually) the
356 /// smallest prime that keeps the load factor small enough.
357 struct _Prime_rehash_policy
359 _Prime_rehash_policy(float __z = 1.0)
360 : _M_max_load_factor(__z), _M_next_resize(0) { }
362 float
363 max_load_factor() const noexcept
364 { return _M_max_load_factor; }
366 // Return a bucket size no smaller than n.
367 std::size_t
368 _M_next_bkt(std::size_t __n) const;
370 // Return a bucket count appropriate for n elements
371 std::size_t
372 _M_bkt_for_elements(std::size_t __n) const;
374 // __n_bkt is current bucket count, __n_elt is current element count,
375 // and __n_ins is number of elements to be inserted. Do we need to
376 // increase bucket count? If so, return make_pair(true, n), where n
377 // is the new bucket count. If not, return make_pair(false, 0).
378 std::pair<bool, std::size_t>
379 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
380 std::size_t __n_ins) const;
382 typedef std::size_t _State;
384 _State
385 _M_state() const
386 { return _M_next_resize; }
388 void
389 _M_reset(_State __state)
390 { _M_next_resize = __state; }
392 enum { _S_n_primes = sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
394 static const std::size_t _S_growth_factor = 2;
396 float _M_max_load_factor;
397 mutable std::size_t _M_next_resize;
400 extern const unsigned long __prime_list[];
402 // XXX This is a hack. There's no good reason for any of
403 // _Prime_rehash_policy's member functions to be inline.
405 // Return a prime no smaller than n.
406 inline std::size_t
407 _Prime_rehash_policy::
408 _M_next_bkt(std::size_t __n) const
410 // Optimize lookups involving the first elements of __prime_list.
411 // (useful to speed-up, eg, constructors)
412 static const unsigned char __fast_bkt[12]
413 = { 2, 2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11 };
415 if (__n <= 11)
417 _M_next_resize
418 = __builtin_ceil(__fast_bkt[__n]
419 * (long double)_M_max_load_factor);
420 return __fast_bkt[__n];
423 const unsigned long* __next_bkt
424 = std::lower_bound(__prime_list + 5, __prime_list + _S_n_primes,
425 __n);
426 _M_next_resize
427 = __builtin_ceil(*__next_bkt * (long double)_M_max_load_factor);
428 return *__next_bkt;
431 // Return the smallest integer p such that alpha p >= n, where alpha
432 // is the load factor.
433 inline std::size_t
434 _Prime_rehash_policy::
435 _M_bkt_for_elements(std::size_t __n) const
436 { return __builtin_ceil(__n / (long double)_M_max_load_factor); }
438 // Finds the smallest prime p such that alpha p > __n_elt + __n_ins.
439 // If p > __n_bkt, return make_pair(true, p); otherwise return
440 // make_pair(false, 0). In principle this isn't very different from
441 // _M_bkt_for_elements.
443 // The only tricky part is that we're caching the element count at
444 // which we need to rehash, so we don't have to do a floating-point
445 // multiply for every insertion.
447 inline std::pair<bool, std::size_t>
448 _Prime_rehash_policy::
449 _M_need_rehash(std::size_t __n_bkt, std::size_t __n_elt,
450 std::size_t __n_ins) const
452 if (__n_elt + __n_ins >= _M_next_resize)
454 long double __min_bkts = (__n_elt + __n_ins)
455 / (long double)_M_max_load_factor;
456 if (__min_bkts >= __n_bkt)
457 return std::make_pair(true,
458 _M_next_bkt(std::max<std::size_t>(__builtin_floor(__min_bkts) + 1,
459 __n_bkt * _S_growth_factor)));
460 else
462 _M_next_resize
463 = __builtin_floor(__n_bkt * (long double)_M_max_load_factor);
464 return std::make_pair(false, 0);
467 else
468 return std::make_pair(false, 0);
471 // Base classes for std::_Hashtable. We define these base classes
472 // because in some cases we want to do different things depending on
473 // the value of a policy class. In some cases the policy class
474 // affects which member functions and nested typedefs are defined;
475 // we handle that by specializing base class templates. Several of
476 // the base class templates need to access other members of class
477 // template _Hashtable, so we use a variant of the "Curiously
478 // Recurring Template Pattern" (CRTP) technique.
481 * Primary class template _Map_base.
483 * If the hashtable has a value type of the form pair<T1, T2> and a
484 * key extraction policy (_ExtractKey) that returns the first part
485 * of the pair, the hashtable gets a mapped_type typedef. If it
486 * satisfies those criteria and also has unique keys, then it also
487 * gets an operator[].
489 template<typename _Key, typename _Value, typename _Alloc,
490 typename _ExtractKey, typename _Equal,
491 typename _H1, typename _H2, typename _Hash,
492 typename _RehashPolicy, typename _Traits,
493 bool _Unique_keys = _Traits::__unique_keys::value>
494 struct _Map_base { };
496 /// Partial specialization, __unique_keys set to false.
497 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
498 typename _H1, typename _H2, typename _Hash,
499 typename _RehashPolicy, typename _Traits>
500 struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
501 _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
503 using mapped_type = typename std::tuple_element<1, _Pair>::type;
506 /// Partial specialization, __unique_keys set to true.
507 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
508 typename _H1, typename _H2, typename _Hash,
509 typename _RehashPolicy, typename _Traits>
510 struct _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
511 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
513 private:
514 using __hashtable_base = __detail::_Hashtable_base<_Key, _Pair,
515 _Select1st,
516 _Equal, _H1, _H2, _Hash,
517 _Traits>;
519 using __hashtable = _Hashtable<_Key, _Pair, _Alloc,
520 _Select1st, _Equal,
521 _H1, _H2, _Hash, _RehashPolicy, _Traits>;
523 using __hash_code = typename __hashtable_base::__hash_code;
524 using __node_type = typename __hashtable_base::__node_type;
526 public:
527 using key_type = typename __hashtable_base::key_type;
528 using iterator = typename __hashtable_base::iterator;
529 using mapped_type = typename std::tuple_element<1, _Pair>::type;
531 mapped_type&
532 operator[](const key_type& __k);
534 mapped_type&
535 operator[](key_type&& __k);
537 // _GLIBCXX_RESOLVE_LIB_DEFECTS
538 // DR 761. unordered_map needs an at() member function.
539 mapped_type&
540 at(const key_type& __k);
542 const mapped_type&
543 at(const key_type& __k) const;
546 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
547 typename _H1, typename _H2, typename _Hash,
548 typename _RehashPolicy, typename _Traits>
549 typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
550 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
551 ::mapped_type&
552 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
553 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
554 operator[](const key_type& __k)
556 __hashtable* __h = static_cast<__hashtable*>(this);
557 __hash_code __code = __h->_M_hash_code(__k);
558 std::size_t __n = __h->_M_bucket_index(__k, __code);
559 __node_type* __p = __h->_M_find_node(__n, __k, __code);
561 if (!__p)
563 __p = __h->_M_allocate_node(std::piecewise_construct,
564 std::tuple<const key_type&>(__k),
565 std::tuple<>());
566 return __h->_M_insert_unique_node(__n, __code, __p)->second;
569 return (__p->_M_v).second;
572 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
573 typename _H1, typename _H2, typename _Hash,
574 typename _RehashPolicy, typename _Traits>
575 typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
576 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
577 ::mapped_type&
578 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
579 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
580 operator[](key_type&& __k)
582 __hashtable* __h = static_cast<__hashtable*>(this);
583 __hash_code __code = __h->_M_hash_code(__k);
584 std::size_t __n = __h->_M_bucket_index(__k, __code);
585 __node_type* __p = __h->_M_find_node(__n, __k, __code);
587 if (!__p)
589 __p = __h->_M_allocate_node(std::piecewise_construct,
590 std::forward_as_tuple(std::move(__k)),
591 std::tuple<>());
592 return __h->_M_insert_unique_node(__n, __code, __p)->second;
595 return (__p->_M_v).second;
598 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
599 typename _H1, typename _H2, typename _Hash,
600 typename _RehashPolicy, typename _Traits>
601 typename _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
602 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
603 ::mapped_type&
604 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
605 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
606 at(const key_type& __k)
608 __hashtable* __h = static_cast<__hashtable*>(this);
609 __hash_code __code = __h->_M_hash_code(__k);
610 std::size_t __n = __h->_M_bucket_index(__k, __code);
611 __node_type* __p = __h->_M_find_node(__n, __k, __code);
613 if (!__p)
614 __throw_out_of_range(__N("_Map_base::at"));
615 return (__p->_M_v).second;
618 template<typename _Key, typename _Pair, typename _Alloc, typename _Equal,
619 typename _H1, typename _H2, typename _Hash,
620 typename _RehashPolicy, typename _Traits>
621 const typename _Map_base<_Key, _Pair, _Alloc, _Select1st,
622 _Equal, _H1, _H2, _Hash, _RehashPolicy,
623 _Traits, true>::mapped_type&
624 _Map_base<_Key, _Pair, _Alloc, _Select1st, _Equal,
625 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
626 at(const key_type& __k) const
628 const __hashtable* __h = static_cast<const __hashtable*>(this);
629 __hash_code __code = __h->_M_hash_code(__k);
630 std::size_t __n = __h->_M_bucket_index(__k, __code);
631 __node_type* __p = __h->_M_find_node(__n, __k, __code);
633 if (!__p)
634 __throw_out_of_range(__N("_Map_base::at"));
635 return (__p->_M_v).second;
639 * Primary class template _Insert_base.
641 * insert member functions appropriate to all _Hashtables.
643 template<typename _Key, typename _Value, typename _Alloc,
644 typename _ExtractKey, typename _Equal,
645 typename _H1, typename _H2, typename _Hash,
646 typename _RehashPolicy, typename _Traits>
647 struct _Insert_base
649 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
650 _Equal, _H1, _H2, _Hash,
651 _RehashPolicy, _Traits>;
653 using __hashtable_base = _Hashtable_base<_Key, _Value, _ExtractKey,
654 _Equal, _H1, _H2, _Hash,
655 _Traits>;
657 using value_type = typename __hashtable_base::value_type;
658 using iterator = typename __hashtable_base::iterator;
659 using const_iterator = typename __hashtable_base::const_iterator;
660 using size_type = typename __hashtable_base::size_type;
662 using __unique_keys = typename __hashtable_base::__unique_keys;
663 using __ireturn_type = typename __hashtable_base::__ireturn_type;
664 using __iconv_type = typename __hashtable_base::__iconv_type;
666 __hashtable&
667 _M_conjure_hashtable()
668 { return *(static_cast<__hashtable*>(this)); }
670 __ireturn_type
671 insert(const value_type& __v)
673 __hashtable& __h = _M_conjure_hashtable();
674 return __h._M_insert(__v, __unique_keys());
677 iterator
678 insert(const_iterator, const value_type& __v)
679 { return __iconv_type()(insert(__v)); }
681 void
682 insert(initializer_list<value_type> __l)
683 { this->insert(__l.begin(), __l.end()); }
685 template<typename _InputIterator>
686 void
687 insert(_InputIterator __first, _InputIterator __last);
690 template<typename _Key, typename _Value, typename _Alloc,
691 typename _ExtractKey, typename _Equal,
692 typename _H1, typename _H2, typename _Hash,
693 typename _RehashPolicy, typename _Traits>
694 template<typename _InputIterator>
695 void
696 _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
697 _RehashPolicy, _Traits>::
698 insert(_InputIterator __first, _InputIterator __last)
700 using __rehash_type = typename __hashtable::__rehash_type;
701 using __rehash_state = typename __hashtable::__rehash_state;
702 using pair_type = std::pair<bool, std::size_t>;
704 size_type __n_elt = __detail::__distance_fw(__first, __last);
706 __hashtable& __h = _M_conjure_hashtable();
707 __rehash_type& __rehash = __h._M_rehash_policy;
708 const __rehash_state& __saved_state = __rehash._M_state();
709 pair_type __do_rehash = __rehash._M_need_rehash(__h._M_bucket_count,
710 __h._M_element_count,
711 __n_elt);
713 if (__do_rehash.first)
714 __h._M_rehash(__do_rehash.second, __saved_state);
716 for (; __first != __last; ++__first)
717 this->insert(*__first);
721 * Primary class template _Insert.
723 * Select insert member functions appropriate to _Hashtable policy choices.
725 template<typename _Key, typename _Value, typename _Alloc,
726 typename _ExtractKey, typename _Equal,
727 typename _H1, typename _H2, typename _Hash,
728 typename _RehashPolicy, typename _Traits,
729 bool _Constant_iterators = _Traits::__constant_iterators::value,
730 bool _Unique_keys = _Traits::__unique_keys::value>
731 struct _Insert;
733 /// Specialization.
734 template<typename _Key, typename _Value, typename _Alloc,
735 typename _ExtractKey, typename _Equal,
736 typename _H1, typename _H2, typename _Hash,
737 typename _RehashPolicy, typename _Traits>
738 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
739 _RehashPolicy, _Traits, true, true>
740 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
741 _H1, _H2, _Hash, _RehashPolicy, _Traits>
743 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
744 _Equal, _H1, _H2, _Hash,
745 _RehashPolicy, _Traits>;
746 using value_type = typename __base_type::value_type;
747 using iterator = typename __base_type::iterator;
748 using const_iterator = typename __base_type::const_iterator;
750 using __unique_keys = typename __base_type::__unique_keys;
751 using __hashtable = typename __base_type::__hashtable;
753 using __base_type::insert;
755 std::pair<iterator, bool>
756 insert(value_type&& __v)
758 __hashtable& __h = this->_M_conjure_hashtable();
759 return __h._M_insert(std::move(__v), __unique_keys());
762 iterator
763 insert(const_iterator, value_type&& __v)
764 { return insert(std::move(__v)).first; }
767 /// Specialization.
768 template<typename _Key, typename _Value, typename _Alloc,
769 typename _ExtractKey, typename _Equal,
770 typename _H1, typename _H2, typename _Hash,
771 typename _RehashPolicy, typename _Traits>
772 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
773 _RehashPolicy, _Traits, true, false>
774 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
775 _H1, _H2, _Hash, _RehashPolicy, _Traits>
777 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
778 _Equal, _H1, _H2, _Hash,
779 _RehashPolicy, _Traits>;
780 using value_type = typename __base_type::value_type;
781 using iterator = typename __base_type::iterator;
782 using const_iterator = typename __base_type::const_iterator;
784 using __unique_keys = typename __base_type::__unique_keys;
785 using __hashtable = typename __base_type::__hashtable;
787 using __base_type::insert;
789 iterator
790 insert(value_type&& __v)
792 __hashtable& __h = this->_M_conjure_hashtable();
793 return __h._M_insert(std::move(__v), __unique_keys());
796 iterator
797 insert(const_iterator, value_type&& __v)
798 { return insert(std::move(__v)); }
801 /// Specialization.
802 template<typename _Key, typename _Value, typename _Alloc,
803 typename _ExtractKey, typename _Equal,
804 typename _H1, typename _H2, typename _Hash,
805 typename _RehashPolicy, typename _Traits, bool _Unique_keys>
806 struct _Insert<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash,
807 _RehashPolicy, _Traits, false, _Unique_keys>
808 : public _Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
809 _H1, _H2, _Hash, _RehashPolicy, _Traits>
811 using __base_type = _Insert_base<_Key, _Value, _Alloc, _ExtractKey,
812 _Equal, _H1, _H2, _Hash,
813 _RehashPolicy, _Traits>;
814 using value_type = typename __base_type::value_type;
815 using iterator = typename __base_type::iterator;
816 using const_iterator = typename __base_type::const_iterator;
818 using __unique_keys = typename __base_type::__unique_keys;
819 using __hashtable = typename __base_type::__hashtable;
820 using __ireturn_type = typename __base_type::__ireturn_type;
821 using __iconv_type = typename __base_type::__iconv_type;
823 using __base_type::insert;
825 template<typename _Pair>
826 using __is_cons = std::is_constructible<value_type, _Pair&&>;
828 template<typename _Pair>
829 using _IFcons = std::enable_if<__is_cons<_Pair>::value>;
831 template<typename _Pair>
832 using _IFconsp = typename _IFcons<_Pair>::type;
834 template<typename _Pair, typename = _IFconsp<_Pair>>
835 __ireturn_type
836 insert(_Pair&& __v)
838 __hashtable& __h = this->_M_conjure_hashtable();
839 return __h._M_insert(std::forward<_Pair>(__v), __unique_keys());
842 template<typename _Pair, typename = _IFconsp<_Pair>>
843 iterator
844 insert(const_iterator, _Pair&& __v)
845 { return __iconv_type()(insert(std::forward<_Pair>(__v))); }
849 * Primary class template _Rehash_base.
851 * Give hashtable the max_load_factor functions and reserve iff the
852 * rehash policy is _Prime_rehash_policy.
854 template<typename _Key, typename _Value, typename _Alloc,
855 typename _ExtractKey, typename _Equal,
856 typename _H1, typename _H2, typename _Hash,
857 typename _RehashPolicy, typename _Traits>
858 struct _Rehash_base;
860 /// Specialization.
861 template<typename _Key, typename _Value, typename _Alloc,
862 typename _ExtractKey, typename _Equal,
863 typename _H1, typename _H2, typename _Hash, typename _Traits>
864 struct _Rehash_base<_Key, _Value, _Alloc, _ExtractKey, _Equal,
865 _H1, _H2, _Hash, _Prime_rehash_policy, _Traits>
867 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey,
868 _Equal, _H1, _H2, _Hash,
869 _Prime_rehash_policy, _Traits>;
871 float
872 max_load_factor() const noexcept
874 const __hashtable* __this = static_cast<const __hashtable*>(this);
875 return __this->__rehash_policy().max_load_factor();
878 void
879 max_load_factor(float __z)
881 __hashtable* __this = static_cast<__hashtable*>(this);
882 __this->__rehash_policy(_Prime_rehash_policy(__z));
885 void
886 reserve(std::size_t __n)
888 __hashtable* __this = static_cast<__hashtable*>(this);
889 __this->rehash(__builtin_ceil(__n / max_load_factor()));
894 * Primary class template _Hashtable_ebo_helper.
896 * Helper class using EBO when it is not forbidden, type is not
897 * final, and when it worth it, type is empty.
899 template<int _Nm, typename _Tp,
900 bool __use_ebo = !__is_final(_Tp) && __is_empty(_Tp)>
901 struct _Hashtable_ebo_helper;
903 /// Specialization using EBO.
904 template<int _Nm, typename _Tp>
905 struct _Hashtable_ebo_helper<_Nm, _Tp, true>
906 : private _Tp
908 _Hashtable_ebo_helper() = default;
910 _Hashtable_ebo_helper(const _Tp& __tp) : _Tp(__tp)
913 static const _Tp&
914 _S_cget(const _Hashtable_ebo_helper& __eboh)
915 { return static_cast<const _Tp&>(__eboh); }
917 static _Tp&
918 _S_get(_Hashtable_ebo_helper& __eboh)
919 { return static_cast<_Tp&>(__eboh); }
922 /// Specialization not using EBO.
923 template<int _Nm, typename _Tp>
924 struct _Hashtable_ebo_helper<_Nm, _Tp, false>
926 _Hashtable_ebo_helper() = default;
928 _Hashtable_ebo_helper(const _Tp& __tp) : _M_tp(__tp)
931 static const _Tp&
932 _S_cget(const _Hashtable_ebo_helper& __eboh)
933 { return __eboh._M_tp; }
935 static _Tp&
936 _S_get(_Hashtable_ebo_helper& __eboh)
937 { return __eboh._M_tp; }
939 private:
940 _Tp _M_tp;
944 * Primary class template _Hash_code_base.
946 * Encapsulates two policy issues that aren't quite orthogonal.
947 * (1) the difference between using a ranged hash function and using
948 * the combination of a hash function and a range-hashing function.
949 * In the former case we don't have such things as hash codes, so
950 * we have a dummy type as placeholder.
951 * (2) Whether or not we cache hash codes. Caching hash codes is
952 * meaningless if we have a ranged hash function.
954 * We also put the key extraction objects here, for convenience.
955 * Each specialization derives from one or more of the template
956 * parameters to benefit from Ebo. This is important as this type
957 * is inherited in some cases by the _Local_iterator_base type used
958 * to implement local_iterator and const_local_iterator. As with
959 * any iterator type we prefer to make it as small as possible.
961 * Primary template is unused except as a hook for specializations.
963 template<typename _Key, typename _Value, typename _ExtractKey,
964 typename _H1, typename _H2, typename _Hash,
965 bool __cache_hash_code>
966 struct _Hash_code_base;
968 /// Specialization: ranged hash function, no caching hash codes. H1
969 /// and H2 are provided but ignored. We define a dummy hash code type.
970 template<typename _Key, typename _Value, typename _ExtractKey,
971 typename _H1, typename _H2, typename _Hash>
972 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, false>
973 : private _Hashtable_ebo_helper<0, _ExtractKey>,
974 private _Hashtable_ebo_helper<1, _Hash>
976 private:
977 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
978 typedef _Hashtable_ebo_helper<1, _Hash> _EboHash;
980 protected:
981 typedef void* __hash_code;
982 typedef _Hash_node<_Value, false> __node_type;
984 // We need the default constructor for the local iterators.
985 _Hash_code_base() = default;
987 _Hash_code_base(const _ExtractKey& __ex, const _H1&, const _H2&,
988 const _Hash& __h)
989 : _EboExtractKey(__ex), _EboHash(__h) { }
991 __hash_code
992 _M_hash_code(const _Key& __key) const
993 { return 0; }
995 std::size_t
996 _M_bucket_index(const _Key& __k, __hash_code, std::size_t __n) const
997 { return _M_ranged_hash()(__k, __n); }
999 std::size_t
1000 _M_bucket_index(const __node_type* __p, std::size_t __n) const
1001 { return _M_ranged_hash()(_M_extract()(__p->_M_v), __n); }
1003 void
1004 _M_store_code(__node_type*, __hash_code) const
1007 void
1008 _M_copy_code(__node_type*, const __node_type*) const
1011 void
1012 _M_swap(_Hash_code_base& __x)
1014 std::swap(_M_extract(), __x._M_extract());
1015 std::swap(_M_ranged_hash(), __x._M_ranged_hash());
1018 protected:
1019 const _ExtractKey&
1020 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
1022 _ExtractKey&
1023 _M_extract() { return _EboExtractKey::_S_get(*this); }
1025 const _Hash&
1026 _M_ranged_hash() const { return _EboHash::_S_cget(*this); }
1028 _Hash&
1029 _M_ranged_hash() { return _EboHash::_S_get(*this); }
1032 // No specialization for ranged hash function while caching hash codes.
1033 // That combination is meaningless, and trying to do it is an error.
1035 /// Specialization: ranged hash function, cache hash codes. This
1036 /// combination is meaningless, so we provide only a declaration
1037 /// and no definition.
1038 template<typename _Key, typename _Value, typename _ExtractKey,
1039 typename _H1, typename _H2, typename _Hash>
1040 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash, true>;
1042 /// Specialization: hash function and range-hashing function, no
1043 /// caching of hash codes.
1044 /// Provides typedef and accessor required by TR1.
1045 template<typename _Key, typename _Value, typename _ExtractKey,
1046 typename _H1, typename _H2>
1047 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
1048 _Default_ranged_hash, false>
1049 : private _Hashtable_ebo_helper<0, _ExtractKey>,
1050 private _Hashtable_ebo_helper<1, _H1>,
1051 private _Hashtable_ebo_helper<2, _H2>
1053 private:
1054 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
1055 typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
1056 typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
1058 public:
1059 typedef _H1 hasher;
1061 hasher
1062 hash_function() const
1063 { return _M_h1(); }
1065 typedef std::size_t __hash_code;
1066 typedef _Hash_node<_Value, false> __node_type;
1068 protected:
1069 // We need the default constructor for the local iterators.
1070 _Hash_code_base() = default;
1072 _Hash_code_base(const _ExtractKey& __ex,
1073 const _H1& __h1, const _H2& __h2,
1074 const _Default_ranged_hash&)
1075 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
1077 __hash_code
1078 _M_hash_code(const _Key& __k) const
1079 { return _M_h1()(__k); }
1081 std::size_t
1082 _M_bucket_index(const _Key&, __hash_code __c, std::size_t __n) const
1083 { return _M_h2()(__c, __n); }
1085 std::size_t
1086 _M_bucket_index(const __node_type* __p,
1087 std::size_t __n) const
1088 { return _M_h2()(_M_h1()(_M_extract()(__p->_M_v)), __n); }
1090 void
1091 _M_store_code(__node_type*, __hash_code) const
1094 void
1095 _M_copy_code(__node_type*, const __node_type*) const
1098 void
1099 _M_swap(_Hash_code_base& __x)
1101 std::swap(_M_extract(), __x._M_extract());
1102 std::swap(_M_h1(), __x._M_h1());
1103 std::swap(_M_h2(), __x._M_h2());
1106 const _ExtractKey&
1107 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
1109 _ExtractKey&
1110 _M_extract() { return _EboExtractKey::_S_get(*this); }
1112 const _H1&
1113 _M_h1() const { return _EboH1::_S_cget(*this); }
1115 _H1&
1116 _M_h1() { return _EboH1::_S_get(*this); }
1118 const _H2&
1119 _M_h2() const { return _EboH2::_S_cget(*this); }
1121 _H2&
1122 _M_h2() { return _EboH2::_S_get(*this); }
1125 /// Specialization: hash function and range-hashing function,
1126 /// caching hash codes. H is provided but ignored. Provides
1127 /// typedef and accessor required by TR1.
1128 template<typename _Key, typename _Value, typename _ExtractKey,
1129 typename _H1, typename _H2>
1130 struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
1131 _Default_ranged_hash, true>
1132 : private _Hashtable_ebo_helper<0, _ExtractKey>,
1133 private _Hashtable_ebo_helper<1, _H1>,
1134 private _Hashtable_ebo_helper<2, _H2>
1136 private:
1137 typedef _Hashtable_ebo_helper<0, _ExtractKey> _EboExtractKey;
1138 typedef _Hashtable_ebo_helper<1, _H1> _EboH1;
1139 typedef _Hashtable_ebo_helper<2, _H2> _EboH2;
1141 public:
1142 typedef _H1 hasher;
1144 hasher
1145 hash_function() const
1146 { return _M_h1(); }
1148 typedef std::size_t __hash_code;
1149 typedef _Hash_node<_Value, true> __node_type;
1151 protected:
1152 _Hash_code_base(const _ExtractKey& __ex,
1153 const _H1& __h1, const _H2& __h2,
1154 const _Default_ranged_hash&)
1155 : _EboExtractKey(__ex), _EboH1(__h1), _EboH2(__h2) { }
1157 __hash_code
1158 _M_hash_code(const _Key& __k) const
1159 { return _M_h1()(__k); }
1161 std::size_t
1162 _M_bucket_index(const _Key&, __hash_code __c,
1163 std::size_t __n) const
1164 { return _M_h2()(__c, __n); }
1166 std::size_t
1167 _M_bucket_index(const __node_type* __p, std::size_t __n) const
1168 { return _M_h2()(__p->_M_hash_code, __n); }
1170 void
1171 _M_store_code(__node_type* __n, __hash_code __c) const
1172 { __n->_M_hash_code = __c; }
1174 void
1175 _M_copy_code(__node_type* __to, const __node_type* __from) const
1176 { __to->_M_hash_code = __from->_M_hash_code; }
1178 void
1179 _M_swap(_Hash_code_base& __x)
1181 std::swap(_M_extract(), __x._M_extract());
1182 std::swap(_M_h1(), __x._M_h1());
1183 std::swap(_M_h2(), __x._M_h2());
1186 const _ExtractKey&
1187 _M_extract() const { return _EboExtractKey::_S_cget(*this); }
1189 _ExtractKey&
1190 _M_extract() { return _EboExtractKey::_S_get(*this); }
1192 const _H1&
1193 _M_h1() const { return _EboH1::_S_cget(*this); }
1195 _H1&
1196 _M_h1() { return _EboH1::_S_get(*this); }
1198 const _H2&
1199 _M_h2() const { return _EboH2::_S_cget(*this); }
1201 _H2&
1202 _M_h2() { return _EboH2::_S_get(*this); }
1206 * Primary class template _Equal_helper.
1209 template <typename _Key, typename _Value, typename _ExtractKey,
1210 typename _Equal, typename _HashCodeType,
1211 bool __cache_hash_code>
1212 struct _Equal_helper;
1214 /// Specialization.
1215 template<typename _Key, typename _Value, typename _ExtractKey,
1216 typename _Equal, typename _HashCodeType>
1217 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, true>
1219 static bool
1220 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
1221 const _Key& __k, _HashCodeType __c, _Hash_node<_Value, true>* __n)
1222 { return __c == __n->_M_hash_code && __eq(__k, __extract(__n->_M_v)); }
1225 /// Specialization.
1226 template<typename _Key, typename _Value, typename _ExtractKey,
1227 typename _Equal, typename _HashCodeType>
1228 struct _Equal_helper<_Key, _Value, _ExtractKey, _Equal, _HashCodeType, false>
1230 static bool
1231 _S_equals(const _Equal& __eq, const _ExtractKey& __extract,
1232 const _Key& __k, _HashCodeType, _Hash_node<_Value, false>* __n)
1233 { return __eq(__k, __extract(__n->_M_v)); }
1238 * Primary class template _Local_iterator_base.
1240 * Base class for local iterators, used to iterate within a bucket
1241 * but not between buckets.
1243 template<typename _Key, typename _Value, typename _ExtractKey,
1244 typename _H1, typename _H2, typename _Hash,
1245 bool __cache_hash_code>
1246 struct _Local_iterator_base;
1248 /// Specialization.
1249 template<typename _Key, typename _Value, typename _ExtractKey,
1250 typename _H1, typename _H2, typename _Hash>
1251 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1252 _H1, _H2, _Hash, true>
1253 : private _H2
1255 _Local_iterator_base() = default;
1256 _Local_iterator_base(_Hash_node<_Value, true>* __p,
1257 std::size_t __bkt, std::size_t __bkt_count)
1258 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
1260 void
1261 _M_incr()
1263 _M_cur = _M_cur->_M_next();
1264 if (_M_cur)
1266 std::size_t __bkt = _M_h2()(_M_cur->_M_hash_code, _M_bucket_count);
1267 if (__bkt != _M_bucket)
1268 _M_cur = nullptr;
1272 const _H2& _M_h2() const
1273 { return *this; }
1275 _Hash_node<_Value, true>* _M_cur;
1276 std::size_t _M_bucket;
1277 std::size_t _M_bucket_count;
1280 /// Specialization.
1281 template<typename _Key, typename _Value, typename _ExtractKey,
1282 typename _H1, typename _H2, typename _Hash>
1283 struct _Local_iterator_base<_Key, _Value, _ExtractKey,
1284 _H1, _H2, _Hash, false>
1285 : private _Hash_code_base<_Key, _Value, _ExtractKey,
1286 _H1, _H2, _Hash, false>
1288 _Local_iterator_base() = default;
1289 _Local_iterator_base(_Hash_node<_Value, false>* __p,
1290 std::size_t __bkt, std::size_t __bkt_count)
1291 : _M_cur(__p), _M_bucket(__bkt), _M_bucket_count(__bkt_count) { }
1293 void
1294 _M_incr()
1296 _M_cur = _M_cur->_M_next();
1297 if (_M_cur)
1299 std::size_t __bkt = this->_M_bucket_index(_M_cur, _M_bucket_count);
1300 if (__bkt != _M_bucket)
1301 _M_cur = nullptr;
1305 _Hash_node<_Value, false>* _M_cur;
1306 std::size_t _M_bucket;
1307 std::size_t _M_bucket_count;
1310 template<typename _Key, typename _Value, typename _ExtractKey,
1311 typename _H1, typename _H2, typename _Hash, bool __cache>
1312 inline bool
1313 operator==(const _Local_iterator_base<_Key, _Value, _ExtractKey,
1314 _H1, _H2, _Hash, __cache>& __x,
1315 const _Local_iterator_base<_Key, _Value, _ExtractKey,
1316 _H1, _H2, _Hash, __cache>& __y)
1317 { return __x._M_cur == __y._M_cur; }
1319 template<typename _Key, typename _Value, typename _ExtractKey,
1320 typename _H1, typename _H2, typename _Hash, bool __cache>
1321 inline bool
1322 operator!=(const _Local_iterator_base<_Key, _Value, _ExtractKey,
1323 _H1, _H2, _Hash, __cache>& __x,
1324 const _Local_iterator_base<_Key, _Value, _ExtractKey,
1325 _H1, _H2, _Hash, __cache>& __y)
1326 { return __x._M_cur != __y._M_cur; }
1328 /// local iterators
1329 template<typename _Key, typename _Value, typename _ExtractKey,
1330 typename _H1, typename _H2, typename _Hash,
1331 bool __constant_iterators, bool __cache>
1332 struct _Local_iterator
1333 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1334 _H1, _H2, _Hash, __cache>
1336 typedef _Value value_type;
1337 typedef typename std::conditional<__constant_iterators,
1338 const _Value*, _Value*>::type
1339 pointer;
1340 typedef typename std::conditional<__constant_iterators,
1341 const _Value&, _Value&>::type
1342 reference;
1343 typedef std::ptrdiff_t difference_type;
1344 typedef std::forward_iterator_tag iterator_category;
1346 _Local_iterator() = default;
1348 explicit
1349 _Local_iterator(_Hash_node<_Value, __cache>* __p,
1350 std::size_t __bkt, std::size_t __bkt_count)
1351 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1352 __cache>(__p, __bkt, __bkt_count)
1355 reference
1356 operator*() const
1357 { return this->_M_cur->_M_v; }
1359 pointer
1360 operator->() const
1361 { return std::__addressof(this->_M_cur->_M_v); }
1363 _Local_iterator&
1364 operator++()
1366 this->_M_incr();
1367 return *this;
1370 _Local_iterator
1371 operator++(int)
1373 _Local_iterator __tmp(*this);
1374 this->_M_incr();
1375 return __tmp;
1379 /// local const_iterators
1380 template<typename _Key, typename _Value, typename _ExtractKey,
1381 typename _H1, typename _H2, typename _Hash,
1382 bool __constant_iterators, bool __cache>
1383 struct _Local_const_iterator
1384 : public _Local_iterator_base<_Key, _Value, _ExtractKey,
1385 _H1, _H2, _Hash, __cache>
1387 typedef _Value value_type;
1388 typedef const _Value* pointer;
1389 typedef const _Value& reference;
1390 typedef std::ptrdiff_t difference_type;
1391 typedef std::forward_iterator_tag iterator_category;
1393 _Local_const_iterator() = default;
1395 explicit
1396 _Local_const_iterator(_Hash_node<_Value, __cache>* __p,
1397 std::size_t __bkt, std::size_t __bkt_count)
1398 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1399 __cache>(__p, __bkt, __bkt_count)
1402 _Local_const_iterator(const _Local_iterator<_Key, _Value, _ExtractKey,
1403 _H1, _H2, _Hash,
1404 __constant_iterators,
1405 __cache>& __x)
1406 : _Local_iterator_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1407 __cache>(__x._M_cur, __x._M_bucket,
1408 __x._M_bucket_count)
1411 reference
1412 operator*() const
1413 { return this->_M_cur->_M_v; }
1415 pointer
1416 operator->() const
1417 { return std::__addressof(this->_M_cur->_M_v); }
1419 _Local_const_iterator&
1420 operator++()
1422 this->_M_incr();
1423 return *this;
1426 _Local_const_iterator
1427 operator++(int)
1429 _Local_const_iterator __tmp(*this);
1430 this->_M_incr();
1431 return __tmp;
1436 * Primary class template _Hashtable_base.
1438 * Helper class adding management of _Equal functor to
1439 * _Hash_code_base type.
1441 * Base class templates are:
1442 * - __detail::_Hash_code_base
1443 * - __detail::_Hashtable_ebo_helper
1445 template<typename _Key, typename _Value,
1446 typename _ExtractKey, typename _Equal,
1447 typename _H1, typename _H2, typename _Hash, typename _Traits>
1448 struct _Hashtable_base
1449 : public _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2, _Hash,
1450 _Traits::__hash_cached::value>,
1451 private _Hashtable_ebo_helper<0, _Equal>
1453 public:
1454 typedef _Key key_type;
1455 typedef _Value value_type;
1456 typedef _Equal key_equal;
1457 typedef std::size_t size_type;
1458 typedef std::ptrdiff_t difference_type;
1460 using __traits_type = _Traits;
1461 using __hash_cached = typename __traits_type::__hash_cached;
1462 using __constant_iterators = typename __traits_type::__constant_iterators;
1463 using __unique_keys = typename __traits_type::__unique_keys;
1465 using __hash_code_base = _Hash_code_base<_Key, _Value, _ExtractKey,
1466 _H1, _H2, _Hash,
1467 __hash_cached::value>;
1469 using __hash_code = typename __hash_code_base::__hash_code;
1470 using __node_type = typename __hash_code_base::__node_type;
1472 using iterator = __detail::_Node_iterator<value_type,
1473 __constant_iterators::value,
1474 __hash_cached::value>;
1476 using const_iterator = __detail::_Node_const_iterator<value_type,
1477 __constant_iterators::value,
1478 __hash_cached::value>;
1480 using local_iterator = __detail::_Local_iterator<key_type, value_type,
1481 _ExtractKey, _H1, _H2, _Hash,
1482 __constant_iterators::value,
1483 __hash_cached::value>;
1485 using const_local_iterator = __detail::_Local_const_iterator<key_type,
1486 value_type,
1487 _ExtractKey, _H1, _H2, _Hash,
1488 __constant_iterators::value,
1489 __hash_cached::value>;
1491 using __ireturn_type = typename std::conditional<__unique_keys::value,
1492 std::pair<iterator, bool>,
1493 iterator>::type;
1495 using __iconv_type = typename std::conditional<__unique_keys::value,
1496 _Select1st, _Identity
1497 >::type;
1498 private:
1499 using _EqualEBO = _Hashtable_ebo_helper<0, _Equal>;
1500 using _EqualHelper = _Equal_helper<_Key, _Value, _ExtractKey, _Equal,
1501 __hash_code, __hash_cached::value>;
1503 protected:
1504 using __node_base = __detail::_Hash_node_base;
1505 using __bucket_type = __node_base*;
1507 _Hashtable_base(const _ExtractKey& __ex, const _H1& __h1, const _H2& __h2,
1508 const _Hash& __hash, const _Equal& __eq)
1509 : __hash_code_base(__ex, __h1, __h2, __hash), _EqualEBO(__eq)
1512 bool
1513 _M_equals(const _Key& __k, __hash_code __c, __node_type* __n) const
1515 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
1516 __k, __c, __n);
1519 void
1520 _M_swap(_Hashtable_base& __x)
1522 __hash_code_base::_M_swap(__x);
1523 std::swap(_M_eq(), __x._M_eq());
1526 const _Equal&
1527 _M_eq() const { return _EqualEBO::_S_cget(*this); }
1529 _Equal&
1530 _M_eq() { return _EqualEBO::_S_get(*this); }
1534 * struct _Equality_base.
1536 * Common types and functions for class _Equality.
1538 struct _Equality_base
1540 protected:
1541 template<typename _Uiterator>
1542 static bool
1543 _S_is_permutation(_Uiterator, _Uiterator, _Uiterator);
1546 // See std::is_permutation in N3068.
1547 template<typename _Uiterator>
1548 bool
1549 _Equality_base::
1550 _S_is_permutation(_Uiterator __first1, _Uiterator __last1,
1551 _Uiterator __first2)
1553 for (; __first1 != __last1; ++__first1, ++__first2)
1554 if (!(*__first1 == *__first2))
1555 break;
1557 if (__first1 == __last1)
1558 return true;
1560 _Uiterator __last2 = __first2;
1561 std::advance(__last2, std::distance(__first1, __last1));
1563 for (_Uiterator __it1 = __first1; __it1 != __last1; ++__it1)
1565 _Uiterator __tmp = __first1;
1566 while (__tmp != __it1 && !bool(*__tmp == *__it1))
1567 ++__tmp;
1569 // We've seen this one before.
1570 if (__tmp != __it1)
1571 continue;
1573 std::ptrdiff_t __n2 = 0;
1574 for (__tmp = __first2; __tmp != __last2; ++__tmp)
1575 if (*__tmp == *__it1)
1576 ++__n2;
1578 if (!__n2)
1579 return false;
1581 std::ptrdiff_t __n1 = 0;
1582 for (__tmp = __it1; __tmp != __last1; ++__tmp)
1583 if (*__tmp == *__it1)
1584 ++__n1;
1586 if (__n1 != __n2)
1587 return false;
1589 return true;
1593 * Primary class template _Equality.
1595 * This is for implementing equality comparison for unordered
1596 * containers, per N3068, by John Lakos and Pablo Halpern.
1597 * Algorithmically, we follow closely the reference implementations
1598 * therein.
1600 template<typename _Key, typename _Value, typename _Alloc,
1601 typename _ExtractKey, typename _Equal,
1602 typename _H1, typename _H2, typename _Hash,
1603 typename _RehashPolicy, typename _Traits,
1604 bool _Unique_keys = _Traits::__unique_keys::value>
1605 struct _Equality;
1607 /// Specialization.
1608 template<typename _Key, typename _Value, typename _Alloc,
1609 typename _ExtractKey, typename _Equal,
1610 typename _H1, typename _H2, typename _Hash,
1611 typename _RehashPolicy, typename _Traits>
1612 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1613 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>
1615 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1616 _H1, _H2, _Hash, _RehashPolicy, _Traits>;
1618 bool
1619 _M_equal(const __hashtable&) const;
1622 template<typename _Key, typename _Value, typename _Alloc,
1623 typename _ExtractKey, typename _Equal,
1624 typename _H1, typename _H2, typename _Hash,
1625 typename _RehashPolicy, typename _Traits>
1626 bool
1627 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1628 _H1, _H2, _Hash, _RehashPolicy, _Traits, true>::
1629 _M_equal(const __hashtable& __other) const
1631 const __hashtable* __this = static_cast<const __hashtable*>(this);
1633 if (__this->size() != __other.size())
1634 return false;
1636 for (auto __itx = __this->begin(); __itx != __this->end(); ++__itx)
1638 const auto __ity = __other.find(_ExtractKey()(*__itx));
1639 if (__ity == __other.end() || !bool(*__ity == *__itx))
1640 return false;
1642 return true;
1645 /// Specialization.
1646 template<typename _Key, typename _Value, typename _Alloc,
1647 typename _ExtractKey, typename _Equal,
1648 typename _H1, typename _H2, typename _Hash,
1649 typename _RehashPolicy, typename _Traits>
1650 struct _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1651 _H1, _H2, _Hash, _RehashPolicy, _Traits, false>
1652 : public _Equality_base
1654 using __hashtable = _Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1655 _H1, _H2, _Hash, _RehashPolicy, _Traits>;
1657 bool
1658 _M_equal(const __hashtable&) const;
1661 template<typename _Key, typename _Value, typename _Alloc,
1662 typename _ExtractKey, typename _Equal,
1663 typename _H1, typename _H2, typename _Hash,
1664 typename _RehashPolicy, typename _Traits>
1665 bool
1666 _Equality<_Key, _Value, _Alloc, _ExtractKey, _Equal,
1667 _H1, _H2, _Hash, _RehashPolicy, _Traits, false>::
1668 _M_equal(const __hashtable& __other) const
1670 const __hashtable* __this = static_cast<const __hashtable*>(this);
1672 if (__this->size() != __other.size())
1673 return false;
1675 for (auto __itx = __this->begin(); __itx != __this->end();)
1677 const auto __xrange = __this->equal_range(_ExtractKey()(*__itx));
1678 const auto __yrange = __other.equal_range(_ExtractKey()(*__itx));
1680 if (std::distance(__xrange.first, __xrange.second)
1681 != std::distance(__yrange.first, __yrange.second))
1682 return false;
1684 if (!_S_is_permutation(__xrange.first, __xrange.second,
1685 __yrange.first))
1686 return false;
1688 __itx = __xrange.second;
1690 return true;
1694 * This type is to combine a _Hash_node_base instance with an allocator
1695 * instance through inheritance to benefit from EBO when possible.
1697 template<typename _NodeAlloc>
1698 struct _Before_begin : public _NodeAlloc
1700 _Hash_node_base _M_node;
1702 _Before_begin(const _Before_begin&) = default;
1703 _Before_begin(_Before_begin&&) = default;
1705 template<typename _Alloc>
1706 _Before_begin(_Alloc&& __a)
1707 : _NodeAlloc(std::forward<_Alloc>(__a))
1711 //@} hashtable-detail
1712 _GLIBCXX_END_NAMESPACE_VERSION
1713 } // namespace __detail
1714 } // namespace std
1716 #endif // _HASHTABLE_POLICY_H