1 // Internal policy header for unordered_set and unordered_map -*- C++ -*-
3 // Copyright (C) 2010-2014 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/hashtable_policy.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly.
28 * @headername{unordered_map,unordered_set}
31 #ifndef _HASHTABLE_POLICY_H
32 #define _HASHTABLE_POLICY_H 1
34 namespace std
_GLIBCXX_VISIBILITY(default)
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
>
44 _GLIBCXX_END_NAMESPACE_VERSION
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 * @defgroup hashtable-detail Base and Implementation Classes
52 * @ingroup unordered_associative_containers
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
)
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
&>()))>
90 template<typename _Tp
>
92 operator()(_Tp
&& __x
) const
93 { return std::forward
<_Tp
>(__x
); }
98 template<typename _Tp
>
100 operator()(_Tp
&& __x
) const
101 -> decltype(std::get
<0>(std::forward
<_Tp
>(__x
)))
102 { return std::get
<0>(std::forward
<_Tp
>(__x
)); }
105 template<typename _NodeAlloc
>
106 struct _Hashtable_alloc
;
108 // Functor recycling a pool of nodes and using allocation once the pool is
110 template<typename _NodeAlloc
>
111 struct _ReuseOrAllocNode
114 using __node_alloc_type
= _NodeAlloc
;
115 using __hashtable_alloc
= _Hashtable_alloc
<__node_alloc_type
>;
116 using __value_alloc_type
= typename
__hashtable_alloc::__value_alloc_type
;
117 using __value_alloc_traits
=
118 typename
__hashtable_alloc::__value_alloc_traits
;
119 using __node_alloc_traits
=
120 typename
__hashtable_alloc::__node_alloc_traits
;
121 using __node_type
= typename
__hashtable_alloc::__node_type
;
124 _ReuseOrAllocNode(__node_type
* __nodes
, __hashtable_alloc
& __h
)
125 : _M_nodes(__nodes
), _M_h(__h
) { }
126 _ReuseOrAllocNode(const _ReuseOrAllocNode
&) = delete;
129 { _M_h
._M_deallocate_nodes(_M_nodes
); }
131 template<typename _Arg
>
133 operator()(_Arg
&& __arg
) const
137 __node_type
* __node
= _M_nodes
;
138 _M_nodes
= _M_nodes
->_M_next();
139 __node
->_M_nxt
= nullptr;
140 __value_alloc_type
__a(_M_h
._M_node_allocator());
141 __value_alloc_traits::destroy(__a
, __node
->_M_valptr());
144 __value_alloc_traits::construct(__a
, __node
->_M_valptr(),
145 std::forward
<_Arg
>(__arg
));
149 __node
->~__node_type();
150 __node_alloc_traits::deallocate(_M_h
._M_node_allocator(),
152 __throw_exception_again
;
156 return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
));
160 mutable __node_type
* _M_nodes
;
161 __hashtable_alloc
& _M_h
;
164 // Functor similar to the previous one but without any pool of nodes to
166 template<typename _NodeAlloc
>
170 using __hashtable_alloc
= _Hashtable_alloc
<_NodeAlloc
>;
171 using __node_type
= typename
__hashtable_alloc::__node_type
;
174 _AllocNode(__hashtable_alloc
& __h
)
177 template<typename _Arg
>
179 operator()(_Arg
&& __arg
) const
180 { return _M_h
._M_allocate_node(std::forward
<_Arg
>(__arg
)); }
183 __hashtable_alloc
& _M_h
;
186 // Auxiliary types used for all instantiations of _Hashtable nodes
190 * struct _Hashtable_traits
192 * Important traits for hash tables.
194 * @tparam _Cache_hash_code Boolean value. True if the value of
195 * the hash function is stored along with the value. This is a
196 * time-space tradeoff. Storing it may improve lookup speed by
197 * reducing the number of times we need to call the _Equal
200 * @tparam _Constant_iterators Boolean value. True if iterator and
201 * const_iterator are both constant iterator types. This is true
202 * for unordered_set and unordered_multiset, false for
203 * unordered_map and unordered_multimap.
205 * @tparam _Unique_keys Boolean value. True if the return value
206 * of _Hashtable::count(k) is always at most one, false if it may
207 * be an arbitrary number. This is true for unordered_set and
208 * unordered_map, false for unordered_multiset and
209 * unordered_multimap.
211 template<bool _Cache_hash_code
, bool _Constant_iterators
, bool _Unique_keys
>
212 struct _Hashtable_traits
215 using __bool_constant
= integral_constant
<bool, _Cond
>;
217 using __hash_cached
= __bool_constant
<_Cache_hash_code
>;
218 using __constant_iterators
= __bool_constant
<_Constant_iterators
>;
219 using __unique_keys
= __bool_constant
<_Unique_keys
>;
223 * struct _Hash_node_base
225 * Nodes, used to wrap elements stored in the hash table. A policy
226 * template parameter of class template _Hashtable controls whether
227 * nodes also store a hash code. In some cases (e.g. strings) this
228 * may be a performance win.
230 struct _Hash_node_base
232 _Hash_node_base
* _M_nxt
;
234 _Hash_node_base() noexcept
: _M_nxt() { }
236 _Hash_node_base(_Hash_node_base
* __next
) noexcept
: _M_nxt(__next
) { }
240 * struct _Hash_node_value_base
242 * Node type with the value to store.
244 template<typename _Value
>
245 struct _Hash_node_value_base
: _Hash_node_base
247 typedef _Value value_type
;
249 __gnu_cxx::__aligned_buffer
<_Value
> _M_storage
;
253 { return _M_storage
._M_ptr(); }
256 _M_valptr() const noexcept
257 { return _M_storage
._M_ptr(); }
261 { return *_M_valptr(); }
264 _M_v() const noexcept
265 { return *_M_valptr(); }
269 * Primary template struct _Hash_node.
271 template<typename _Value
, bool _Cache_hash_code
>
275 * Specialization for nodes with caches, struct _Hash_node.
277 * Base class is __detail::_Hash_node_value_base.
279 template<typename _Value
>
280 struct _Hash_node
<_Value
, true> : _Hash_node_value_base
<_Value
>
282 std::size_t _M_hash_code
;
285 _M_next() const noexcept
286 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
290 * Specialization for nodes without caches, struct _Hash_node.
292 * Base class is __detail::_Hash_node_value_base.
294 template<typename _Value
>
295 struct _Hash_node
<_Value
, false> : _Hash_node_value_base
<_Value
>
298 _M_next() const noexcept
299 { return static_cast<_Hash_node
*>(this->_M_nxt
); }
302 /// Base class for node iterators.
303 template<typename _Value
, bool _Cache_hash_code
>
304 struct _Node_iterator_base
306 using __node_type
= _Hash_node
<_Value
, _Cache_hash_code
>;
310 _Node_iterator_base(__node_type
* __p
) noexcept
315 { _M_cur
= _M_cur
->_M_next(); }
318 template<typename _Value
, bool _Cache_hash_code
>
320 operator==(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
321 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
323 { return __x
._M_cur
== __y
._M_cur
; }
325 template<typename _Value
, bool _Cache_hash_code
>
327 operator!=(const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __x
,
328 const _Node_iterator_base
<_Value
, _Cache_hash_code
>& __y
)
330 { return __x
._M_cur
!= __y
._M_cur
; }
332 /// Node iterators, used to iterate through all the hashtable.
333 template<typename _Value
, bool __constant_iterators
, bool __cache
>
334 struct _Node_iterator
335 : public _Node_iterator_base
<_Value
, __cache
>
338 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
339 using __node_type
= typename
__base_type::__node_type
;
342 typedef _Value value_type
;
343 typedef std::ptrdiff_t difference_type
;
344 typedef std::forward_iterator_tag iterator_category
;
346 using pointer
= typename
std::conditional
<__constant_iterators
,
347 const _Value
*, _Value
*>::type
;
349 using reference
= typename
std::conditional
<__constant_iterators
,
350 const _Value
&, _Value
&>::type
;
352 _Node_iterator() noexcept
356 _Node_iterator(__node_type
* __p
) noexcept
357 : __base_type(__p
) { }
360 operator*() const noexcept
361 { return this->_M_cur
->_M_v(); }
364 operator->() const noexcept
365 { return this->_M_cur
->_M_valptr(); }
368 operator++() noexcept
375 operator++(int) noexcept
377 _Node_iterator
__tmp(*this);
383 /// Node const_iterators, used to iterate through all the hashtable.
384 template<typename _Value
, bool __constant_iterators
, bool __cache
>
385 struct _Node_const_iterator
386 : public _Node_iterator_base
<_Value
, __cache
>
389 using __base_type
= _Node_iterator_base
<_Value
, __cache
>;
390 using __node_type
= typename
__base_type::__node_type
;
393 typedef _Value value_type
;
394 typedef std::ptrdiff_t difference_type
;
395 typedef std::forward_iterator_tag iterator_category
;
397 typedef const _Value
* pointer
;
398 typedef const _Value
& reference
;
400 _Node_const_iterator() noexcept
404 _Node_const_iterator(__node_type
* __p
) noexcept
405 : __base_type(__p
) { }
407 _Node_const_iterator(const _Node_iterator
<_Value
, __constant_iterators
,
408 __cache
>& __x
) noexcept
409 : __base_type(__x
._M_cur
) { }
412 operator*() const noexcept
413 { return this->_M_cur
->_M_v(); }
416 operator->() const noexcept
417 { return this->_M_cur
->_M_valptr(); }
419 _Node_const_iterator
&
420 operator++() noexcept
427 operator++(int) noexcept
429 _Node_const_iterator
__tmp(*this);
435 // Many of class template _Hashtable's template parameters are policy
436 // classes. These are defaults for the policies.
438 /// Default range hashing function: use division to fold a large number
439 /// into the range [0, N).
440 struct _Mod_range_hashing
442 typedef std::size_t first_argument_type
;
443 typedef std::size_t second_argument_type
;
444 typedef std::size_t result_type
;
447 operator()(first_argument_type __num
,
448 second_argument_type __den
) const noexcept
449 { return __num
% __den
; }
452 /// Default ranged hash function H. In principle it should be a
453 /// function object composed from objects of type H1 and H2 such that
454 /// h(k, N) = h2(h1(k), N), but that would mean making extra copies of
455 /// h1 and h2. So instead we'll just use a tag to tell class template
456 /// hashtable to do that composition.
457 struct _Default_ranged_hash
{ };
459 /// Default value for rehash policy. Bucket size is (usually) the
460 /// smallest prime that keeps the load factor small enough.
461 struct _Prime_rehash_policy
463 _Prime_rehash_policy(float __z
= 1.0)
464 : _M_max_load_factor(__z
), _M_next_resize(0) { }
467 max_load_factor() const noexcept
468 { return _M_max_load_factor
; }
470 // Return a bucket size no smaller than n.
472 _M_next_bkt(std::size_t __n
) const;
474 // Return a bucket count appropriate for n elements
476 _M_bkt_for_elements(std::size_t __n
) const
477 { return __builtin_ceil(__n
/ (long double)_M_max_load_factor
); }
479 // __n_bkt is current bucket count, __n_elt is current element count,
480 // and __n_ins is number of elements to be inserted. Do we need to
481 // increase bucket count? If so, return make_pair(true, n), where n
482 // is the new bucket count. If not, return make_pair(false, 0).
483 std::pair
<bool, std::size_t>
484 _M_need_rehash(std::size_t __n_bkt
, std::size_t __n_elt
,
485 std::size_t __n_ins
) const;
487 typedef std::size_t _State
;
491 { return _M_next_resize
; }
495 { _M_next_resize
= 0; }
498 _M_reset(_State __state
)
499 { _M_next_resize
= __state
; }
501 enum { _S_n_primes
= sizeof(unsigned long) != 8 ? 256 : 256 + 48 };
503 static const std::size_t _S_growth_factor
= 2;
505 float _M_max_load_factor
;
506 mutable std::size_t _M_next_resize
;
509 // Base classes for std::_Hashtable. We define these base classes
510 // because in some cases we want to do different things depending on
511 // the value of a policy class. In some cases the policy class
512 // affects which member functions and nested typedefs are defined;
513 // we handle that by specializing base class templates. Several of
514 // the base class templates need to access other members of class
515 // template _Hashtable, so we use a variant of the "Curiously
516 // Recurring Template Pattern" (CRTP) technique.
519 * Primary class template _Map_base.
521 * If the hashtable has a value type of the form pair<T1, T2> and a
522 * key extraction policy (_ExtractKey) that returns the first part
523 * of the pair, the hashtable gets a mapped_type typedef. If it
524 * satisfies those criteria and also has unique keys, then it also
525 * gets an operator[].
527 template<typename _Key
, typename _Value
, typename _Alloc
,
528 typename _ExtractKey
, typename _Equal
,
529 typename _H1
, typename _H2
, typename _Hash
,
530 typename _RehashPolicy
, typename _Traits
,
531 bool _Unique_keys
= _Traits::__unique_keys::value
>
532 struct _Map_base
{ };
534 /// Partial specialization, __unique_keys set to false.
535 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
536 typename _H1
, typename _H2
, typename _Hash
,
537 typename _RehashPolicy
, typename _Traits
>
538 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
539 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
541 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
544 /// Partial specialization, __unique_keys set to true.
545 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
546 typename _H1
, typename _H2
, typename _Hash
,
547 typename _RehashPolicy
, typename _Traits
>
548 struct _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
549 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
552 using __hashtable_base
= __detail::_Hashtable_base
<_Key
, _Pair
,
554 _Equal
, _H1
, _H2
, _Hash
,
557 using __hashtable
= _Hashtable
<_Key
, _Pair
, _Alloc
,
559 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
561 using __hash_code
= typename
__hashtable_base::__hash_code
;
562 using __node_type
= typename
__hashtable_base::__node_type
;
565 using key_type
= typename
__hashtable_base::key_type
;
566 using iterator
= typename
__hashtable_base::iterator
;
567 using mapped_type
= typename
std::tuple_element
<1, _Pair
>::type
;
570 operator[](const key_type
& __k
);
573 operator[](key_type
&& __k
);
575 // _GLIBCXX_RESOLVE_LIB_DEFECTS
576 // DR 761. unordered_map needs an at() member function.
578 at(const key_type
& __k
);
581 at(const key_type
& __k
) const;
584 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
585 typename _H1
, typename _H2
, typename _Hash
,
586 typename _RehashPolicy
, typename _Traits
>
587 typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
588 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
590 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
591 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
592 operator[](const key_type
& __k
)
594 __hashtable
* __h
= static_cast<__hashtable
*>(this);
595 __hash_code __code
= __h
->_M_hash_code(__k
);
596 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
597 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
601 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
602 std::tuple
<const key_type
&>(__k
),
604 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
607 return __p
->_M_v().second
;
610 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
611 typename _H1
, typename _H2
, typename _Hash
,
612 typename _RehashPolicy
, typename _Traits
>
613 typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
614 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
616 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
617 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
618 operator[](key_type
&& __k
)
620 __hashtable
* __h
= static_cast<__hashtable
*>(this);
621 __hash_code __code
= __h
->_M_hash_code(__k
);
622 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
623 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
627 __p
= __h
->_M_allocate_node(std::piecewise_construct
,
628 std::forward_as_tuple(std::move(__k
)),
630 return __h
->_M_insert_unique_node(__n
, __code
, __p
)->second
;
633 return __p
->_M_v().second
;
636 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
637 typename _H1
, typename _H2
, typename _Hash
,
638 typename _RehashPolicy
, typename _Traits
>
639 typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
640 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
642 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
643 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
644 at(const key_type
& __k
)
646 __hashtable
* __h
= static_cast<__hashtable
*>(this);
647 __hash_code __code
= __h
->_M_hash_code(__k
);
648 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
649 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
652 __throw_out_of_range(__N("_Map_base::at"));
653 return __p
->_M_v().second
;
656 template<typename _Key
, typename _Pair
, typename _Alloc
, typename _Equal
,
657 typename _H1
, typename _H2
, typename _Hash
,
658 typename _RehashPolicy
, typename _Traits
>
659 const typename _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
,
660 _Equal
, _H1
, _H2
, _Hash
, _RehashPolicy
,
661 _Traits
, true>::mapped_type
&
662 _Map_base
<_Key
, _Pair
, _Alloc
, _Select1st
, _Equal
,
663 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
664 at(const key_type
& __k
) const
666 const __hashtable
* __h
= static_cast<const __hashtable
*>(this);
667 __hash_code __code
= __h
->_M_hash_code(__k
);
668 std::size_t __n
= __h
->_M_bucket_index(__k
, __code
);
669 __node_type
* __p
= __h
->_M_find_node(__n
, __k
, __code
);
672 __throw_out_of_range(__N("_Map_base::at"));
673 return __p
->_M_v().second
;
677 * Primary class template _Insert_base.
679 * insert member functions appropriate to all _Hashtables.
681 template<typename _Key
, typename _Value
, typename _Alloc
,
682 typename _ExtractKey
, typename _Equal
,
683 typename _H1
, typename _H2
, typename _Hash
,
684 typename _RehashPolicy
, typename _Traits
>
688 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
689 _Equal
, _H1
, _H2
, _Hash
,
690 _RehashPolicy
, _Traits
>;
692 using __hashtable_base
= _Hashtable_base
<_Key
, _Value
, _ExtractKey
,
693 _Equal
, _H1
, _H2
, _Hash
,
696 using value_type
= typename
__hashtable_base::value_type
;
697 using iterator
= typename
__hashtable_base::iterator
;
698 using const_iterator
= typename
__hashtable_base::const_iterator
;
699 using size_type
= typename
__hashtable_base::size_type
;
701 using __unique_keys
= typename
__hashtable_base::__unique_keys
;
702 using __ireturn_type
= typename
__hashtable_base::__ireturn_type
;
703 using __node_type
= _Hash_node
<_Value
, _Traits::__hash_cached::value
>;
704 using __node_alloc_type
= __alloc_rebind
<_Alloc
, __node_type
>;
705 using __node_gen_type
= _AllocNode
<__node_alloc_type
>;
708 _M_conjure_hashtable()
709 { return *(static_cast<__hashtable
*>(this)); }
711 template<typename _InputIterator
, typename _NodeGetter
>
713 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
718 insert(const value_type
& __v
)
720 __hashtable
& __h
= _M_conjure_hashtable();
721 __node_gen_type
__node_gen(__h
);
722 return __h
._M_insert(__v
, __node_gen
, __unique_keys());
726 insert(const_iterator __hint
, const value_type
& __v
)
728 __hashtable
& __h
= _M_conjure_hashtable();
729 __node_gen_type
__node_gen(__h
);
730 return __h
._M_insert(__hint
, __v
, __node_gen
, __unique_keys());
734 insert(initializer_list
<value_type
> __l
)
735 { this->insert(__l
.begin(), __l
.end()); }
737 template<typename _InputIterator
>
739 insert(_InputIterator __first
, _InputIterator __last
)
741 __hashtable
& __h
= _M_conjure_hashtable();
742 __node_gen_type
__node_gen(__h
);
743 return _M_insert_range(__first
, __last
, __node_gen
);
747 template<typename _Key
, typename _Value
, typename _Alloc
,
748 typename _ExtractKey
, typename _Equal
,
749 typename _H1
, typename _H2
, typename _Hash
,
750 typename _RehashPolicy
, typename _Traits
>
751 template<typename _InputIterator
, typename _NodeGetter
>
753 _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
754 _RehashPolicy
, _Traits
>::
755 _M_insert_range(_InputIterator __first
, _InputIterator __last
,
756 const _NodeGetter
& __node_gen
)
758 using __rehash_type
= typename
__hashtable::__rehash_type
;
759 using __rehash_state
= typename
__hashtable::__rehash_state
;
760 using pair_type
= std::pair
<bool, std::size_t>;
762 size_type __n_elt
= __detail::__distance_fw(__first
, __last
);
764 __hashtable
& __h
= _M_conjure_hashtable();
765 __rehash_type
& __rehash
= __h
._M_rehash_policy
;
766 const __rehash_state
& __saved_state
= __rehash
._M_state();
767 pair_type __do_rehash
= __rehash
._M_need_rehash(__h
._M_bucket_count
,
768 __h
._M_element_count
,
771 if (__do_rehash
.first
)
772 __h
._M_rehash(__do_rehash
.second
, __saved_state
);
774 for (; __first
!= __last
; ++__first
)
775 __h
._M_insert(*__first
, __node_gen
, __unique_keys());
779 * Primary class template _Insert.
781 * Select insert member functions appropriate to _Hashtable policy choices.
783 template<typename _Key
, typename _Value
, typename _Alloc
,
784 typename _ExtractKey
, typename _Equal
,
785 typename _H1
, typename _H2
, typename _Hash
,
786 typename _RehashPolicy
, typename _Traits
,
787 bool _Constant_iterators
= _Traits::__constant_iterators::value
,
788 bool _Unique_keys
= _Traits::__unique_keys::value
>
792 template<typename _Key
, typename _Value
, typename _Alloc
,
793 typename _ExtractKey
, typename _Equal
,
794 typename _H1
, typename _H2
, typename _Hash
,
795 typename _RehashPolicy
, typename _Traits
>
796 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
797 _RehashPolicy
, _Traits
, true, true>
798 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
799 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
801 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
802 _Equal
, _H1
, _H2
, _Hash
,
803 _RehashPolicy
, _Traits
>;
804 using value_type
= typename
__base_type::value_type
;
805 using iterator
= typename
__base_type::iterator
;
806 using const_iterator
= typename
__base_type::const_iterator
;
808 using __unique_keys
= typename
__base_type::__unique_keys
;
809 using __hashtable
= typename
__base_type::__hashtable
;
810 using __node_gen_type
= typename
__base_type::__node_gen_type
;
812 using __base_type::insert
;
814 std::pair
<iterator
, bool>
815 insert(value_type
&& __v
)
817 __hashtable
& __h
= this->_M_conjure_hashtable();
818 __node_gen_type
__node_gen(__h
);
819 return __h
._M_insert(std::move(__v
), __node_gen
, __unique_keys());
823 insert(const_iterator __hint
, value_type
&& __v
)
825 __hashtable
& __h
= this->_M_conjure_hashtable();
826 __node_gen_type
__node_gen(__h
);
827 return __h
._M_insert(__hint
, std::move(__v
), __node_gen
,
833 template<typename _Key
, typename _Value
, typename _Alloc
,
834 typename _ExtractKey
, typename _Equal
,
835 typename _H1
, typename _H2
, typename _Hash
,
836 typename _RehashPolicy
, typename _Traits
>
837 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
838 _RehashPolicy
, _Traits
, true, false>
839 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
840 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
842 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
843 _Equal
, _H1
, _H2
, _Hash
,
844 _RehashPolicy
, _Traits
>;
845 using value_type
= typename
__base_type::value_type
;
846 using iterator
= typename
__base_type::iterator
;
847 using const_iterator
= typename
__base_type::const_iterator
;
849 using __unique_keys
= typename
__base_type::__unique_keys
;
850 using __hashtable
= typename
__base_type::__hashtable
;
851 using __node_gen_type
= typename
__base_type::__node_gen_type
;
853 using __base_type::insert
;
856 insert(value_type
&& __v
)
858 __hashtable
& __h
= this->_M_conjure_hashtable();
859 __node_gen_type
__node_gen(__h
);
860 return __h
._M_insert(std::move(__v
), __node_gen
, __unique_keys());
864 insert(const_iterator __hint
, value_type
&& __v
)
866 __hashtable
& __h
= this->_M_conjure_hashtable();
867 __node_gen_type
__node_gen(__h
);
868 return __h
._M_insert(__hint
, std::move(__v
), __node_gen
,
874 template<typename _Key
, typename _Value
, typename _Alloc
,
875 typename _ExtractKey
, typename _Equal
,
876 typename _H1
, typename _H2
, typename _Hash
,
877 typename _RehashPolicy
, typename _Traits
, bool _Unique_keys
>
878 struct _Insert
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
, _H1
, _H2
, _Hash
,
879 _RehashPolicy
, _Traits
, false, _Unique_keys
>
880 : public _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
881 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>
883 using __base_type
= _Insert_base
<_Key
, _Value
, _Alloc
, _ExtractKey
,
884 _Equal
, _H1
, _H2
, _Hash
,
885 _RehashPolicy
, _Traits
>;
886 using value_type
= typename
__base_type::value_type
;
887 using iterator
= typename
__base_type::iterator
;
888 using const_iterator
= typename
__base_type::const_iterator
;
890 using __unique_keys
= typename
__base_type::__unique_keys
;
891 using __hashtable
= typename
__base_type::__hashtable
;
892 using __ireturn_type
= typename
__base_type::__ireturn_type
;
894 using __base_type::insert
;
896 template<typename _Pair
>
897 using __is_cons
= std::is_constructible
<value_type
, _Pair
&&>;
899 template<typename _Pair
>
900 using _IFcons
= std::enable_if
<__is_cons
<_Pair
>::value
>;
902 template<typename _Pair
>
903 using _IFconsp
= typename _IFcons
<_Pair
>::type
;
905 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
909 __hashtable
& __h
= this->_M_conjure_hashtable();
910 return __h
._M_emplace(__unique_keys(), std::forward
<_Pair
>(__v
));
913 template<typename _Pair
, typename
= _IFconsp
<_Pair
>>
915 insert(const_iterator __hint
, _Pair
&& __v
)
917 __hashtable
& __h
= this->_M_conjure_hashtable();
918 return __h
._M_emplace(__hint
, __unique_keys(),
919 std::forward
<_Pair
>(__v
));
924 * Primary class template _Rehash_base.
926 * Give hashtable the max_load_factor functions and reserve iff the
927 * rehash policy is _Prime_rehash_policy.
929 template<typename _Key
, typename _Value
, typename _Alloc
,
930 typename _ExtractKey
, typename _Equal
,
931 typename _H1
, typename _H2
, typename _Hash
,
932 typename _RehashPolicy
, typename _Traits
>
936 template<typename _Key
, typename _Value
, typename _Alloc
,
937 typename _ExtractKey
, typename _Equal
,
938 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
939 struct _Rehash_base
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
940 _H1
, _H2
, _Hash
, _Prime_rehash_policy
, _Traits
>
942 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
,
943 _Equal
, _H1
, _H2
, _Hash
,
944 _Prime_rehash_policy
, _Traits
>;
947 max_load_factor() const noexcept
949 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
950 return __this
->__rehash_policy().max_load_factor();
954 max_load_factor(float __z
)
956 __hashtable
* __this
= static_cast<__hashtable
*>(this);
957 __this
->__rehash_policy(_Prime_rehash_policy(__z
));
961 reserve(std::size_t __n
)
963 __hashtable
* __this
= static_cast<__hashtable
*>(this);
964 __this
->rehash(__builtin_ceil(__n
/ max_load_factor()));
969 * Primary class template _Hashtable_ebo_helper.
971 * Helper class using EBO when it is not forbidden (the type is not
972 * final) and when it is worth it (the type is empty.)
974 template<int _Nm
, typename _Tp
,
975 bool __use_ebo
= !__is_final(_Tp
) && __is_empty(_Tp
)>
976 struct _Hashtable_ebo_helper
;
978 /// Specialization using EBO.
979 template<int _Nm
, typename _Tp
>
980 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, true>
983 _Hashtable_ebo_helper() = default;
985 template<typename _OtherTp
>
986 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
987 : _Tp(std::forward
<_OtherTp
>(__tp
))
991 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
992 { return static_cast<const _Tp
&>(__eboh
); }
995 _S_get(_Hashtable_ebo_helper
& __eboh
)
996 { return static_cast<_Tp
&>(__eboh
); }
999 /// Specialization not using EBO.
1000 template<int _Nm
, typename _Tp
>
1001 struct _Hashtable_ebo_helper
<_Nm
, _Tp
, false>
1003 _Hashtable_ebo_helper() = default;
1005 template<typename _OtherTp
>
1006 _Hashtable_ebo_helper(_OtherTp
&& __tp
)
1007 : _M_tp(std::forward
<_OtherTp
>(__tp
))
1011 _S_cget(const _Hashtable_ebo_helper
& __eboh
)
1012 { return __eboh
._M_tp
; }
1015 _S_get(_Hashtable_ebo_helper
& __eboh
)
1016 { return __eboh
._M_tp
; }
1023 * Primary class template _Local_iterator_base.
1025 * Base class for local iterators, used to iterate within a bucket
1026 * but not between buckets.
1028 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1029 typename _H1
, typename _H2
, typename _Hash
,
1030 bool __cache_hash_code
>
1031 struct _Local_iterator_base
;
1034 * Primary class template _Hash_code_base.
1036 * Encapsulates two policy issues that aren't quite orthogonal.
1037 * (1) the difference between using a ranged hash function and using
1038 * the combination of a hash function and a range-hashing function.
1039 * In the former case we don't have such things as hash codes, so
1040 * we have a dummy type as placeholder.
1041 * (2) Whether or not we cache hash codes. Caching hash codes is
1042 * meaningless if we have a ranged hash function.
1044 * We also put the key extraction objects here, for convenience.
1045 * Each specialization derives from one or more of the template
1046 * parameters to benefit from Ebo. This is important as this type
1047 * is inherited in some cases by the _Local_iterator_base type used
1048 * to implement local_iterator and const_local_iterator. As with
1049 * any iterator type we prefer to make it as small as possible.
1051 * Primary template is unused except as a hook for specializations.
1053 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1054 typename _H1
, typename _H2
, typename _Hash
,
1055 bool __cache_hash_code
>
1056 struct _Hash_code_base
;
1058 /// Specialization: ranged hash function, no caching hash codes. H1
1059 /// and H2 are provided but ignored. We define a dummy hash code type.
1060 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1061 typename _H1
, typename _H2
, typename _Hash
>
1062 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, false>
1063 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1064 private _Hashtable_ebo_helper
<1, _Hash
>
1067 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1068 using __ebo_hash
= _Hashtable_ebo_helper
<1, _Hash
>;
1071 typedef void* __hash_code
;
1072 typedef _Hash_node
<_Value
, false> __node_type
;
1074 // We need the default constructor for the local iterators.
1075 _Hash_code_base() = default;
1077 _Hash_code_base(const _ExtractKey
& __ex
, const _H1
&, const _H2
&,
1079 : __ebo_extract_key(__ex
), __ebo_hash(__h
) { }
1082 _M_hash_code(const _Key
& __key
) const
1086 _M_bucket_index(const _Key
& __k
, __hash_code
, std::size_t __n
) const
1087 { return _M_ranged_hash()(__k
, __n
); }
1090 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1091 noexcept( noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>(),
1093 { return _M_ranged_hash()(_M_extract()(__p
->_M_v()), __n
); }
1096 _M_store_code(__node_type
*, __hash_code
) const
1100 _M_copy_code(__node_type
*, const __node_type
*) const
1104 _M_swap(_Hash_code_base
& __x
)
1106 std::swap(_M_extract(), __x
._M_extract());
1107 std::swap(_M_ranged_hash(), __x
._M_ranged_hash());
1111 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1114 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1117 _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
1120 _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
1123 // No specialization for ranged hash function while caching hash codes.
1124 // That combination is meaningless, and trying to do it is an error.
1126 /// Specialization: ranged hash function, cache hash codes. This
1127 /// combination is meaningless, so we provide only a declaration
1128 /// and no definition.
1129 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1130 typename _H1
, typename _H2
, typename _Hash
>
1131 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, true>;
1133 /// Specialization: hash function and range-hashing function, no
1134 /// caching of hash codes.
1135 /// Provides typedef and accessor required by C++ 11.
1136 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1137 typename _H1
, typename _H2
>
1138 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1139 _Default_ranged_hash
, false>
1140 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1141 private _Hashtable_ebo_helper
<1, _H1
>,
1142 private _Hashtable_ebo_helper
<2, _H2
>
1145 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1146 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1147 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1149 // Gives the local iterator implementation access to _M_bucket_index().
1150 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1151 _Default_ranged_hash
, false>;
1157 hash_function() const
1161 typedef std::size_t __hash_code
;
1162 typedef _Hash_node
<_Value
, false> __node_type
;
1164 // We need the default constructor for the local iterators.
1165 _Hash_code_base() = default;
1167 _Hash_code_base(const _ExtractKey
& __ex
,
1168 const _H1
& __h1
, const _H2
& __h2
,
1169 const _Default_ranged_hash
&)
1170 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1173 _M_hash_code(const _Key
& __k
) const
1174 { return _M_h1()(__k
); }
1177 _M_bucket_index(const _Key
&, __hash_code __c
, std::size_t __n
) const
1178 { return _M_h2()(__c
, __n
); }
1181 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1182 noexcept( noexcept(declval
<const _H1
&>()(declval
<const _Key
&>()))
1183 && noexcept(declval
<const _H2
&>()((__hash_code
)0,
1185 { return _M_h2()(_M_h1()(_M_extract()(__p
->_M_v())), __n
); }
1188 _M_store_code(__node_type
*, __hash_code
) const
1192 _M_copy_code(__node_type
*, const __node_type
*) const
1196 _M_swap(_Hash_code_base
& __x
)
1198 std::swap(_M_extract(), __x
._M_extract());
1199 std::swap(_M_h1(), __x
._M_h1());
1200 std::swap(_M_h2(), __x
._M_h2());
1204 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1207 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1210 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1213 _M_h1() { return __ebo_h1::_S_get(*this); }
1216 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1219 _M_h2() { return __ebo_h2::_S_get(*this); }
1222 /// Specialization: hash function and range-hashing function,
1223 /// caching hash codes. H is provided but ignored. Provides
1224 /// typedef and accessor required by C++ 11.
1225 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1226 typename _H1
, typename _H2
>
1227 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1228 _Default_ranged_hash
, true>
1229 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1230 private _Hashtable_ebo_helper
<1, _H1
>,
1231 private _Hashtable_ebo_helper
<2, _H2
>
1234 // Gives the local iterator implementation access to _M_h2().
1235 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1236 _Default_ranged_hash
, true>;
1238 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1239 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1240 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1246 hash_function() const
1250 typedef std::size_t __hash_code
;
1251 typedef _Hash_node
<_Value
, true> __node_type
;
1253 _Hash_code_base(const _ExtractKey
& __ex
,
1254 const _H1
& __h1
, const _H2
& __h2
,
1255 const _Default_ranged_hash
&)
1256 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1259 _M_hash_code(const _Key
& __k
) const
1260 { return _M_h1()(__k
); }
1263 _M_bucket_index(const _Key
&, __hash_code __c
,
1264 std::size_t __n
) const
1265 { return _M_h2()(__c
, __n
); }
1268 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1269 noexcept( noexcept(declval
<const _H2
&>()((__hash_code
)0,
1271 { return _M_h2()(__p
->_M_hash_code
, __n
); }
1274 _M_store_code(__node_type
* __n
, __hash_code __c
) const
1275 { __n
->_M_hash_code
= __c
; }
1278 _M_copy_code(__node_type
* __to
, const __node_type
* __from
) const
1279 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
1282 _M_swap(_Hash_code_base
& __x
)
1284 std::swap(_M_extract(), __x
._M_extract());
1285 std::swap(_M_h1(), __x
._M_h1());
1286 std::swap(_M_h2(), __x
._M_h2());
1290 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1293 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1296 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1299 _M_h1() { return __ebo_h1::_S_get(*this); }
1302 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1305 _M_h2() { return __ebo_h2::_S_get(*this); }
1309 * Primary class template _Equal_helper.
1312 template <typename _Key
, typename _Value
, typename _ExtractKey
,
1313 typename _Equal
, typename _HashCodeType
,
1314 bool __cache_hash_code
>
1315 struct _Equal_helper
;
1318 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1319 typename _Equal
, typename _HashCodeType
>
1320 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, true>
1323 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1324 const _Key
& __k
, _HashCodeType __c
, _Hash_node
<_Value
, true>* __n
)
1325 { return __c
== __n
->_M_hash_code
&& __eq(__k
, __extract(__n
->_M_v())); }
1329 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1330 typename _Equal
, typename _HashCodeType
>
1331 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, false>
1334 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1335 const _Key
& __k
, _HashCodeType
, _Hash_node
<_Value
, false>* __n
)
1336 { return __eq(__k
, __extract(__n
->_M_v())); }
1340 /// Partial specialization used when nodes contain a cached hash code.
1341 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1342 typename _H1
, typename _H2
, typename _Hash
>
1343 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1344 _H1
, _H2
, _Hash
, true>
1345 : private _Hashtable_ebo_helper
<0, _H2
>
1348 using __base_type
= _Hashtable_ebo_helper
<0, _H2
>;
1349 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1350 _H1
, _H2
, _Hash
, true>;
1352 _Local_iterator_base() = default;
1353 _Local_iterator_base(const __hash_code_base
& __base
,
1354 _Hash_node
<_Value
, true>* __p
,
1355 std::size_t __bkt
, std::size_t __bkt_count
)
1356 : __base_type(__base
._M_h2()),
1357 _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1362 _M_cur
= _M_cur
->_M_next();
1366 = __base_type::_S_get(*this)(_M_cur
->_M_hash_code
,
1368 if (__bkt
!= _M_bucket
)
1373 _Hash_node
<_Value
, true>* _M_cur
;
1374 std::size_t _M_bucket
;
1375 std::size_t _M_bucket_count
;
1379 _M_curr() const { return _M_cur
; } // for equality ops
1382 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1385 // Uninitialized storage for a _Hash_code_base.
1386 // This type is DefaultConstructible and Assignable even if the
1387 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1388 // can be DefaultConstructible and Assignable.
1389 template<typename _Tp
, bool _IsEmpty
= std::is_empty
<_Tp
>::value
>
1390 struct _Hash_code_storage
1392 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
1395 _M_h() { return _M_storage
._M_ptr(); }
1398 _M_h() const { return _M_storage
._M_ptr(); }
1401 // Empty partial specialization for empty _Hash_code_base types.
1402 template<typename _Tp
>
1403 struct _Hash_code_storage
<_Tp
, true>
1405 static_assert( std::is_empty
<_Tp
>::value
, "Type must be empty" );
1407 // As _Tp is an empty type there will be no bytes written/read through
1408 // the cast pointer, so no strict-aliasing violation.
1410 _M_h() { return reinterpret_cast<_Tp
*>(this); }
1413 _M_h() const { return reinterpret_cast<const _Tp
*>(this); }
1416 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1417 typename _H1
, typename _H2
, typename _Hash
>
1418 using __hash_code_for_local_iter
1419 = _Hash_code_storage
<_Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1420 _H1
, _H2
, _Hash
, false>>;
1422 // Partial specialization used when hash codes are not cached
1423 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1424 typename _H1
, typename _H2
, typename _Hash
>
1425 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1426 _H1
, _H2
, _Hash
, false>
1427 : __hash_code_for_local_iter
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
>
1430 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1431 _H1
, _H2
, _Hash
, false>;
1433 _Local_iterator_base() : _M_bucket_count(-1) { }
1435 _Local_iterator_base(const __hash_code_base
& __base
,
1436 _Hash_node
<_Value
, false>* __p
,
1437 std::size_t __bkt
, std::size_t __bkt_count
)
1438 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
)
1439 { _M_init(__base
); }
1441 ~_Local_iterator_base()
1443 if (_M_bucket_count
!= -1)
1447 _Local_iterator_base(const _Local_iterator_base
& __iter
)
1448 : _M_cur(__iter
._M_cur
), _M_bucket(__iter
._M_bucket
),
1449 _M_bucket_count(__iter
._M_bucket_count
)
1451 if (_M_bucket_count
!= -1)
1452 _M_init(*__iter
._M_h());
1455 _Local_iterator_base
&
1456 operator=(const _Local_iterator_base
& __iter
)
1458 if (_M_bucket_count
!= -1)
1460 _M_cur
= __iter
._M_cur
;
1461 _M_bucket
= __iter
._M_bucket
;
1462 _M_bucket_count
= __iter
._M_bucket_count
;
1463 if (_M_bucket_count
!= -1)
1464 _M_init(*__iter
._M_h());
1471 _M_cur
= _M_cur
->_M_next();
1474 std::size_t __bkt
= this->_M_h()->_M_bucket_index(_M_cur
,
1476 if (__bkt
!= _M_bucket
)
1481 _Hash_node
<_Value
, false>* _M_cur
;
1482 std::size_t _M_bucket
;
1483 std::size_t _M_bucket_count
;
1486 _M_init(const __hash_code_base
& __base
)
1487 { ::new(this->_M_h()) __hash_code_base(__base
); }
1490 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1494 _M_curr() const { return _M_cur
; } // for equality ops and debug mode
1497 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1500 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1501 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1503 operator==(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1504 _H1
, _H2
, _Hash
, __cache
>& __x
,
1505 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1506 _H1
, _H2
, _Hash
, __cache
>& __y
)
1507 { return __x
._M_curr() == __y
._M_curr(); }
1509 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1510 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1512 operator!=(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1513 _H1
, _H2
, _Hash
, __cache
>& __x
,
1514 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1515 _H1
, _H2
, _Hash
, __cache
>& __y
)
1516 { return __x
._M_curr() != __y
._M_curr(); }
1519 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1520 typename _H1
, typename _H2
, typename _Hash
,
1521 bool __constant_iterators
, bool __cache
>
1522 struct _Local_iterator
1523 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1524 _H1
, _H2
, _Hash
, __cache
>
1527 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1528 _H1
, _H2
, _Hash
, __cache
>;
1529 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1531 typedef _Value value_type
;
1532 typedef typename
std::conditional
<__constant_iterators
,
1533 const _Value
*, _Value
*>::type
1535 typedef typename
std::conditional
<__constant_iterators
,
1536 const _Value
&, _Value
&>::type
1538 typedef std::ptrdiff_t difference_type
;
1539 typedef std::forward_iterator_tag iterator_category
;
1541 _Local_iterator() = default;
1543 _Local_iterator(const __hash_code_base
& __base
,
1544 _Hash_node
<_Value
, __cache
>* __p
,
1545 std::size_t __bkt
, std::size_t __bkt_count
)
1546 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1551 { return this->_M_cur
->_M_v(); }
1555 { return this->_M_cur
->_M_valptr(); }
1567 _Local_iterator
__tmp(*this);
1573 /// local const_iterators
1574 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1575 typename _H1
, typename _H2
, typename _Hash
,
1576 bool __constant_iterators
, bool __cache
>
1577 struct _Local_const_iterator
1578 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1579 _H1
, _H2
, _Hash
, __cache
>
1582 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1583 _H1
, _H2
, _Hash
, __cache
>;
1584 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1587 typedef _Value value_type
;
1588 typedef const _Value
* pointer
;
1589 typedef const _Value
& reference
;
1590 typedef std::ptrdiff_t difference_type
;
1591 typedef std::forward_iterator_tag iterator_category
;
1593 _Local_const_iterator() = default;
1595 _Local_const_iterator(const __hash_code_base
& __base
,
1596 _Hash_node
<_Value
, __cache
>* __p
,
1597 std::size_t __bkt
, std::size_t __bkt_count
)
1598 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1601 _Local_const_iterator(const _Local_iterator
<_Key
, _Value
, _ExtractKey
,
1603 __constant_iterators
,
1610 { return this->_M_cur
->_M_v(); }
1614 { return this->_M_cur
->_M_valptr(); }
1616 _Local_const_iterator
&
1623 _Local_const_iterator
1626 _Local_const_iterator
__tmp(*this);
1633 * Primary class template _Hashtable_base.
1635 * Helper class adding management of _Equal functor to
1636 * _Hash_code_base type.
1638 * Base class templates are:
1639 * - __detail::_Hash_code_base
1640 * - __detail::_Hashtable_ebo_helper
1642 template<typename _Key
, typename _Value
,
1643 typename _ExtractKey
, typename _Equal
,
1644 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
1645 struct _Hashtable_base
1646 : public _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1647 _Traits::__hash_cached::value
>,
1648 private _Hashtable_ebo_helper
<0, _Equal
>
1651 typedef _Key key_type
;
1652 typedef _Value value_type
;
1653 typedef _Equal key_equal
;
1654 typedef std::size_t size_type
;
1655 typedef std::ptrdiff_t difference_type
;
1657 using __traits_type
= _Traits
;
1658 using __hash_cached
= typename
__traits_type::__hash_cached
;
1659 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
1660 using __unique_keys
= typename
__traits_type::__unique_keys
;
1662 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1664 __hash_cached::value
>;
1666 using __hash_code
= typename
__hash_code_base::__hash_code
;
1667 using __node_type
= typename
__hash_code_base::__node_type
;
1669 using iterator
= __detail::_Node_iterator
<value_type
,
1670 __constant_iterators::value
,
1671 __hash_cached::value
>;
1673 using const_iterator
= __detail::_Node_const_iterator
<value_type
,
1674 __constant_iterators::value
,
1675 __hash_cached::value
>;
1677 using local_iterator
= __detail::_Local_iterator
<key_type
, value_type
,
1678 _ExtractKey
, _H1
, _H2
, _Hash
,
1679 __constant_iterators::value
,
1680 __hash_cached::value
>;
1682 using const_local_iterator
= __detail::_Local_const_iterator
<key_type
,
1684 _ExtractKey
, _H1
, _H2
, _Hash
,
1685 __constant_iterators::value
,
1686 __hash_cached::value
>;
1688 using __ireturn_type
= typename
std::conditional
<__unique_keys::value
,
1689 std::pair
<iterator
, bool>,
1692 using _EqualEBO
= _Hashtable_ebo_helper
<0, _Equal
>;
1693 using _EqualHelper
= _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
,
1694 __hash_code
, __hash_cached::value
>;
1697 _Hashtable_base(const _ExtractKey
& __ex
, const _H1
& __h1
, const _H2
& __h2
,
1698 const _Hash
& __hash
, const _Equal
& __eq
)
1699 : __hash_code_base(__ex
, __h1
, __h2
, __hash
), _EqualEBO(__eq
)
1703 _M_equals(const _Key
& __k
, __hash_code __c
, __node_type
* __n
) const
1705 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
1710 _M_swap(_Hashtable_base
& __x
)
1712 __hash_code_base::_M_swap(__x
);
1713 std::swap(_M_eq(), __x
._M_eq());
1717 _M_eq() const { return _EqualEBO::_S_cget(*this); }
1720 _M_eq() { return _EqualEBO::_S_get(*this); }
1724 * struct _Equality_base.
1726 * Common types and functions for class _Equality.
1728 struct _Equality_base
1731 template<typename _Uiterator
>
1733 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
1736 // See std::is_permutation in N3068.
1737 template<typename _Uiterator
>
1740 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
1741 _Uiterator __first2
)
1743 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1744 if (!(*__first1
== *__first2
))
1747 if (__first1
== __last1
)
1750 _Uiterator __last2
= __first2
;
1751 std::advance(__last2
, std::distance(__first1
, __last1
));
1753 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
1755 _Uiterator __tmp
= __first1
;
1756 while (__tmp
!= __it1
&& !bool(*__tmp
== *__it1
))
1759 // We've seen this one before.
1763 std::ptrdiff_t __n2
= 0;
1764 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
1765 if (*__tmp
== *__it1
)
1771 std::ptrdiff_t __n1
= 0;
1772 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
1773 if (*__tmp
== *__it1
)
1783 * Primary class template _Equality.
1785 * This is for implementing equality comparison for unordered
1786 * containers, per N3068, by John Lakos and Pablo Halpern.
1787 * Algorithmically, we follow closely the reference implementations
1790 template<typename _Key
, typename _Value
, typename _Alloc
,
1791 typename _ExtractKey
, typename _Equal
,
1792 typename _H1
, typename _H2
, typename _Hash
,
1793 typename _RehashPolicy
, typename _Traits
,
1794 bool _Unique_keys
= _Traits::__unique_keys::value
>
1798 template<typename _Key
, typename _Value
, typename _Alloc
,
1799 typename _ExtractKey
, typename _Equal
,
1800 typename _H1
, typename _H2
, typename _Hash
,
1801 typename _RehashPolicy
, typename _Traits
>
1802 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1803 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
1805 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1806 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1809 _M_equal(const __hashtable
&) const;
1812 template<typename _Key
, typename _Value
, typename _Alloc
,
1813 typename _ExtractKey
, typename _Equal
,
1814 typename _H1
, typename _H2
, typename _Hash
,
1815 typename _RehashPolicy
, typename _Traits
>
1817 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1818 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
1819 _M_equal(const __hashtable
& __other
) const
1821 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1823 if (__this
->size() != __other
.size())
1826 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
1828 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
1829 if (__ity
== __other
.end() || !bool(*__ity
== *__itx
))
1836 template<typename _Key
, typename _Value
, typename _Alloc
,
1837 typename _ExtractKey
, typename _Equal
,
1838 typename _H1
, typename _H2
, typename _Hash
,
1839 typename _RehashPolicy
, typename _Traits
>
1840 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1841 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
1842 : public _Equality_base
1844 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1845 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1848 _M_equal(const __hashtable
&) const;
1851 template<typename _Key
, typename _Value
, typename _Alloc
,
1852 typename _ExtractKey
, typename _Equal
,
1853 typename _H1
, typename _H2
, typename _Hash
,
1854 typename _RehashPolicy
, typename _Traits
>
1856 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1857 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>::
1858 _M_equal(const __hashtable
& __other
) const
1860 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1862 if (__this
->size() != __other
.size())
1865 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
1867 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
1868 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
1870 if (std::distance(__xrange
.first
, __xrange
.second
)
1871 != std::distance(__yrange
.first
, __yrange
.second
))
1874 if (!_S_is_permutation(__xrange
.first
, __xrange
.second
,
1878 __itx
= __xrange
.second
;
1884 * This type deals with all allocation and keeps an allocator instance through
1885 * inheritance to benefit from EBO when possible.
1887 template<typename _NodeAlloc
>
1888 struct _Hashtable_alloc
: private _Hashtable_ebo_helper
<0, _NodeAlloc
>
1891 using __ebo_node_alloc
= _Hashtable_ebo_helper
<0, _NodeAlloc
>;
1893 using __node_type
= typename
_NodeAlloc::value_type
;
1894 using __node_alloc_type
= _NodeAlloc
;
1895 // Use __gnu_cxx to benefit from _S_always_equal and al.
1896 using __node_alloc_traits
= __gnu_cxx::__alloc_traits
<__node_alloc_type
>;
1898 using __value_type
= typename
__node_type::value_type
;
1899 using __value_alloc_type
=
1900 __alloc_rebind
<__node_alloc_type
, __value_type
>;
1901 using __value_alloc_traits
= std::allocator_traits
<__value_alloc_type
>;
1903 using __node_base
= __detail::_Hash_node_base
;
1904 using __bucket_type
= __node_base
*;
1905 using __bucket_alloc_type
=
1906 __alloc_rebind
<__node_alloc_type
, __bucket_type
>;
1907 using __bucket_alloc_traits
= std::allocator_traits
<__bucket_alloc_type
>;
1909 _Hashtable_alloc(const _Hashtable_alloc
&) = default;
1910 _Hashtable_alloc(_Hashtable_alloc
&&) = default;
1912 template<typename _Alloc
>
1913 _Hashtable_alloc(_Alloc
&& __a
)
1914 : __ebo_node_alloc(std::forward
<_Alloc
>(__a
))
1919 { return __ebo_node_alloc::_S_get(*this); }
1921 const __node_alloc_type
&
1922 _M_node_allocator() const
1923 { return __ebo_node_alloc::_S_cget(*this); }
1925 template<typename
... _Args
>
1927 _M_allocate_node(_Args
&&... __args
);
1930 _M_deallocate_node(__node_type
* __n
);
1932 // Deallocate the linked list of nodes pointed to by __n
1934 _M_deallocate_nodes(__node_type
* __n
);
1937 _M_allocate_buckets(std::size_t __n
);
1940 _M_deallocate_buckets(__bucket_type
*, std::size_t __n
);
1943 // Definitions of class template _Hashtable_alloc's out-of-line member
1945 template<typename _NodeAlloc
>
1946 template<typename
... _Args
>
1947 typename _Hashtable_alloc
<_NodeAlloc
>::__node_type
*
1948 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_node(_Args
&&... __args
)
1950 auto __nptr
= __node_alloc_traits::allocate(_M_node_allocator(), 1);
1951 __node_type
* __n
= std::__addressof(*__nptr
);
1954 __value_alloc_type
__a(_M_node_allocator());
1955 ::new ((void*)__n
) __node_type
;
1956 __value_alloc_traits::construct(__a
, __n
->_M_valptr(),
1957 std::forward
<_Args
>(__args
)...);
1962 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr
, 1);
1963 __throw_exception_again
;
1967 template<typename _NodeAlloc
>
1969 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_node(__node_type
* __n
)
1971 typedef typename
__node_alloc_traits::pointer _Ptr
;
1972 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__n
);
1973 __value_alloc_type
__a(_M_node_allocator());
1974 __value_alloc_traits::destroy(__a
, __n
->_M_valptr());
1975 __n
->~__node_type();
1976 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr
, 1);
1979 template<typename _NodeAlloc
>
1981 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_nodes(__node_type
* __n
)
1985 __node_type
* __tmp
= __n
;
1986 __n
= __n
->_M_next();
1987 _M_deallocate_node(__tmp
);
1991 template<typename _NodeAlloc
>
1992 typename _Hashtable_alloc
<_NodeAlloc
>::__bucket_type
*
1993 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_buckets(std::size_t __n
)
1995 __bucket_alloc_type
__alloc(_M_node_allocator());
1997 auto __ptr
= __bucket_alloc_traits::allocate(__alloc
, __n
);
1998 __bucket_type
* __p
= std::__addressof(*__ptr
);
1999 __builtin_memset(__p
, 0, __n
* sizeof(__bucket_type
));
2003 template<typename _NodeAlloc
>
2005 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_buckets(__bucket_type
* __bkts
,
2008 typedef typename
__bucket_alloc_traits::pointer _Ptr
;
2009 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__bkts
);
2010 __bucket_alloc_type
__alloc(_M_node_allocator());
2011 __bucket_alloc_traits::deallocate(__alloc
, __ptr
, __n
);
2014 //@} hashtable-detail
2015 _GLIBCXX_END_NAMESPACE_VERSION
2016 } // namespace __detail
2019 #endif // _HASHTABLE_POLICY_H