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) noexcept
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 and _Hashtable
1075 // default constructor.
1076 _Hash_code_base() = default;
1078 _Hash_code_base(const _ExtractKey
& __ex
, const _H1
&, const _H2
&,
1080 : __ebo_extract_key(__ex
), __ebo_hash(__h
) { }
1083 _M_hash_code(const _Key
& __key
) const
1087 _M_bucket_index(const _Key
& __k
, __hash_code
, std::size_t __n
) const
1088 { return _M_ranged_hash()(__k
, __n
); }
1091 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1092 noexcept( noexcept(declval
<const _Hash
&>()(declval
<const _Key
&>(),
1094 { return _M_ranged_hash()(_M_extract()(__p
->_M_v()), __n
); }
1097 _M_store_code(__node_type
*, __hash_code
) const
1101 _M_copy_code(__node_type
*, const __node_type
*) const
1105 _M_swap(_Hash_code_base
& __x
)
1107 std::swap(_M_extract(), __x
._M_extract());
1108 std::swap(_M_ranged_hash(), __x
._M_ranged_hash());
1112 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1115 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1118 _M_ranged_hash() const { return __ebo_hash::_S_cget(*this); }
1121 _M_ranged_hash() { return __ebo_hash::_S_get(*this); }
1124 // No specialization for ranged hash function while caching hash codes.
1125 // That combination is meaningless, and trying to do it is an error.
1127 /// Specialization: ranged hash function, cache hash codes. This
1128 /// combination is meaningless, so we provide only a declaration
1129 /// and no definition.
1130 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1131 typename _H1
, typename _H2
, typename _Hash
>
1132 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
, true>;
1134 /// Specialization: hash function and range-hashing function, no
1135 /// caching of hash codes.
1136 /// Provides typedef and accessor required by C++ 11.
1137 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1138 typename _H1
, typename _H2
>
1139 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1140 _Default_ranged_hash
, false>
1141 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1142 private _Hashtable_ebo_helper
<1, _H1
>,
1143 private _Hashtable_ebo_helper
<2, _H2
>
1146 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1147 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1148 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1150 // Gives the local iterator implementation access to _M_bucket_index().
1151 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1152 _Default_ranged_hash
, false>;
1158 hash_function() const
1162 typedef std::size_t __hash_code
;
1163 typedef _Hash_node
<_Value
, false> __node_type
;
1165 // We need the default constructor for the local iterators and _Hashtable
1166 // default constructor.
1167 _Hash_code_base() = default;
1169 _Hash_code_base(const _ExtractKey
& __ex
,
1170 const _H1
& __h1
, const _H2
& __h2
,
1171 const _Default_ranged_hash
&)
1172 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1175 _M_hash_code(const _Key
& __k
) const
1176 { return _M_h1()(__k
); }
1179 _M_bucket_index(const _Key
&, __hash_code __c
, std::size_t __n
) const
1180 { return _M_h2()(__c
, __n
); }
1183 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1184 noexcept( noexcept(declval
<const _H1
&>()(declval
<const _Key
&>()))
1185 && noexcept(declval
<const _H2
&>()((__hash_code
)0,
1187 { return _M_h2()(_M_h1()(_M_extract()(__p
->_M_v())), __n
); }
1190 _M_store_code(__node_type
*, __hash_code
) const
1194 _M_copy_code(__node_type
*, const __node_type
*) const
1198 _M_swap(_Hash_code_base
& __x
)
1200 std::swap(_M_extract(), __x
._M_extract());
1201 std::swap(_M_h1(), __x
._M_h1());
1202 std::swap(_M_h2(), __x
._M_h2());
1206 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1209 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1212 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1215 _M_h1() { return __ebo_h1::_S_get(*this); }
1218 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1221 _M_h2() { return __ebo_h2::_S_get(*this); }
1224 /// Specialization: hash function and range-hashing function,
1225 /// caching hash codes. H is provided but ignored. Provides
1226 /// typedef and accessor required by C++ 11.
1227 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1228 typename _H1
, typename _H2
>
1229 struct _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1230 _Default_ranged_hash
, true>
1231 : private _Hashtable_ebo_helper
<0, _ExtractKey
>,
1232 private _Hashtable_ebo_helper
<1, _H1
>,
1233 private _Hashtable_ebo_helper
<2, _H2
>
1236 // Gives the local iterator implementation access to _M_h2().
1237 friend struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
,
1238 _Default_ranged_hash
, true>;
1240 using __ebo_extract_key
= _Hashtable_ebo_helper
<0, _ExtractKey
>;
1241 using __ebo_h1
= _Hashtable_ebo_helper
<1, _H1
>;
1242 using __ebo_h2
= _Hashtable_ebo_helper
<2, _H2
>;
1248 hash_function() const
1252 typedef std::size_t __hash_code
;
1253 typedef _Hash_node
<_Value
, true> __node_type
;
1255 // We need the default constructor for _Hashtable default constructor.
1256 _Hash_code_base() = default;
1257 _Hash_code_base(const _ExtractKey
& __ex
,
1258 const _H1
& __h1
, const _H2
& __h2
,
1259 const _Default_ranged_hash
&)
1260 : __ebo_extract_key(__ex
), __ebo_h1(__h1
), __ebo_h2(__h2
) { }
1263 _M_hash_code(const _Key
& __k
) const
1264 { return _M_h1()(__k
); }
1267 _M_bucket_index(const _Key
&, __hash_code __c
,
1268 std::size_t __n
) const
1269 { return _M_h2()(__c
, __n
); }
1272 _M_bucket_index(const __node_type
* __p
, std::size_t __n
) const
1273 noexcept( noexcept(declval
<const _H2
&>()((__hash_code
)0,
1275 { return _M_h2()(__p
->_M_hash_code
, __n
); }
1278 _M_store_code(__node_type
* __n
, __hash_code __c
) const
1279 { __n
->_M_hash_code
= __c
; }
1282 _M_copy_code(__node_type
* __to
, const __node_type
* __from
) const
1283 { __to
->_M_hash_code
= __from
->_M_hash_code
; }
1286 _M_swap(_Hash_code_base
& __x
)
1288 std::swap(_M_extract(), __x
._M_extract());
1289 std::swap(_M_h1(), __x
._M_h1());
1290 std::swap(_M_h2(), __x
._M_h2());
1294 _M_extract() const { return __ebo_extract_key::_S_cget(*this); }
1297 _M_extract() { return __ebo_extract_key::_S_get(*this); }
1300 _M_h1() const { return __ebo_h1::_S_cget(*this); }
1303 _M_h1() { return __ebo_h1::_S_get(*this); }
1306 _M_h2() const { return __ebo_h2::_S_cget(*this); }
1309 _M_h2() { return __ebo_h2::_S_get(*this); }
1313 * Primary class template _Equal_helper.
1316 template <typename _Key
, typename _Value
, typename _ExtractKey
,
1317 typename _Equal
, typename _HashCodeType
,
1318 bool __cache_hash_code
>
1319 struct _Equal_helper
;
1322 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1323 typename _Equal
, typename _HashCodeType
>
1324 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, true>
1327 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1328 const _Key
& __k
, _HashCodeType __c
, _Hash_node
<_Value
, true>* __n
)
1329 { return __c
== __n
->_M_hash_code
&& __eq(__k
, __extract(__n
->_M_v())); }
1333 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1334 typename _Equal
, typename _HashCodeType
>
1335 struct _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
, _HashCodeType
, false>
1338 _S_equals(const _Equal
& __eq
, const _ExtractKey
& __extract
,
1339 const _Key
& __k
, _HashCodeType
, _Hash_node
<_Value
, false>* __n
)
1340 { return __eq(__k
, __extract(__n
->_M_v())); }
1344 /// Partial specialization used when nodes contain a cached hash code.
1345 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1346 typename _H1
, typename _H2
, typename _Hash
>
1347 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1348 _H1
, _H2
, _Hash
, true>
1349 : private _Hashtable_ebo_helper
<0, _H2
>
1352 using __base_type
= _Hashtable_ebo_helper
<0, _H2
>;
1353 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1354 _H1
, _H2
, _Hash
, true>;
1356 _Local_iterator_base() = default;
1357 _Local_iterator_base(const __hash_code_base
& __base
,
1358 _Hash_node
<_Value
, true>* __p
,
1359 std::size_t __bkt
, std::size_t __bkt_count
)
1360 : __base_type(__base
._M_h2()),
1361 _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
) { }
1366 _M_cur
= _M_cur
->_M_next();
1370 = __base_type::_S_get(*this)(_M_cur
->_M_hash_code
,
1372 if (__bkt
!= _M_bucket
)
1377 _Hash_node
<_Value
, true>* _M_cur
;
1378 std::size_t _M_bucket
;
1379 std::size_t _M_bucket_count
;
1383 _M_curr() const { return _M_cur
; } // for equality ops
1386 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1389 // Uninitialized storage for a _Hash_code_base.
1390 // This type is DefaultConstructible and Assignable even if the
1391 // _Hash_code_base type isn't, so that _Local_iterator_base<..., false>
1392 // can be DefaultConstructible and Assignable.
1393 template<typename _Tp
, bool _IsEmpty
= std::is_empty
<_Tp
>::value
>
1394 struct _Hash_code_storage
1396 __gnu_cxx::__aligned_buffer
<_Tp
> _M_storage
;
1399 _M_h() { return _M_storage
._M_ptr(); }
1402 _M_h() const { return _M_storage
._M_ptr(); }
1405 // Empty partial specialization for empty _Hash_code_base types.
1406 template<typename _Tp
>
1407 struct _Hash_code_storage
<_Tp
, true>
1409 static_assert( std::is_empty
<_Tp
>::value
, "Type must be empty" );
1411 // As _Tp is an empty type there will be no bytes written/read through
1412 // the cast pointer, so no strict-aliasing violation.
1414 _M_h() { return reinterpret_cast<_Tp
*>(this); }
1417 _M_h() const { return reinterpret_cast<const _Tp
*>(this); }
1420 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1421 typename _H1
, typename _H2
, typename _Hash
>
1422 using __hash_code_for_local_iter
1423 = _Hash_code_storage
<_Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1424 _H1
, _H2
, _Hash
, false>>;
1426 // Partial specialization used when hash codes are not cached
1427 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1428 typename _H1
, typename _H2
, typename _Hash
>
1429 struct _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1430 _H1
, _H2
, _Hash
, false>
1431 : __hash_code_for_local_iter
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
>
1434 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1435 _H1
, _H2
, _Hash
, false>;
1437 _Local_iterator_base() : _M_bucket_count(-1) { }
1439 _Local_iterator_base(const __hash_code_base
& __base
,
1440 _Hash_node
<_Value
, false>* __p
,
1441 std::size_t __bkt
, std::size_t __bkt_count
)
1442 : _M_cur(__p
), _M_bucket(__bkt
), _M_bucket_count(__bkt_count
)
1443 { _M_init(__base
); }
1445 ~_Local_iterator_base()
1447 if (_M_bucket_count
!= -1)
1451 _Local_iterator_base(const _Local_iterator_base
& __iter
)
1452 : _M_cur(__iter
._M_cur
), _M_bucket(__iter
._M_bucket
),
1453 _M_bucket_count(__iter
._M_bucket_count
)
1455 if (_M_bucket_count
!= -1)
1456 _M_init(*__iter
._M_h());
1459 _Local_iterator_base
&
1460 operator=(const _Local_iterator_base
& __iter
)
1462 if (_M_bucket_count
!= -1)
1464 _M_cur
= __iter
._M_cur
;
1465 _M_bucket
= __iter
._M_bucket
;
1466 _M_bucket_count
= __iter
._M_bucket_count
;
1467 if (_M_bucket_count
!= -1)
1468 _M_init(*__iter
._M_h());
1475 _M_cur
= _M_cur
->_M_next();
1478 std::size_t __bkt
= this->_M_h()->_M_bucket_index(_M_cur
,
1480 if (__bkt
!= _M_bucket
)
1485 _Hash_node
<_Value
, false>* _M_cur
;
1486 std::size_t _M_bucket
;
1487 std::size_t _M_bucket_count
;
1490 _M_init(const __hash_code_base
& __base
)
1491 { ::new(this->_M_h()) __hash_code_base(__base
); }
1494 _M_destroy() { this->_M_h()->~__hash_code_base(); }
1498 _M_curr() const { return _M_cur
; } // for equality ops and debug mode
1501 _M_get_bucket() const { return _M_bucket
; } // for debug mode
1504 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1505 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1507 operator==(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1508 _H1
, _H2
, _Hash
, __cache
>& __x
,
1509 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1510 _H1
, _H2
, _Hash
, __cache
>& __y
)
1511 { return __x
._M_curr() == __y
._M_curr(); }
1513 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1514 typename _H1
, typename _H2
, typename _Hash
, bool __cache
>
1516 operator!=(const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1517 _H1
, _H2
, _Hash
, __cache
>& __x
,
1518 const _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1519 _H1
, _H2
, _Hash
, __cache
>& __y
)
1520 { return __x
._M_curr() != __y
._M_curr(); }
1523 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1524 typename _H1
, typename _H2
, typename _Hash
,
1525 bool __constant_iterators
, bool __cache
>
1526 struct _Local_iterator
1527 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1528 _H1
, _H2
, _Hash
, __cache
>
1531 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1532 _H1
, _H2
, _Hash
, __cache
>;
1533 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1535 typedef _Value value_type
;
1536 typedef typename
std::conditional
<__constant_iterators
,
1537 const _Value
*, _Value
*>::type
1539 typedef typename
std::conditional
<__constant_iterators
,
1540 const _Value
&, _Value
&>::type
1542 typedef std::ptrdiff_t difference_type
;
1543 typedef std::forward_iterator_tag iterator_category
;
1545 _Local_iterator() = default;
1547 _Local_iterator(const __hash_code_base
& __base
,
1548 _Hash_node
<_Value
, __cache
>* __p
,
1549 std::size_t __bkt
, std::size_t __bkt_count
)
1550 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1555 { return this->_M_cur
->_M_v(); }
1559 { return this->_M_cur
->_M_valptr(); }
1571 _Local_iterator
__tmp(*this);
1577 /// local const_iterators
1578 template<typename _Key
, typename _Value
, typename _ExtractKey
,
1579 typename _H1
, typename _H2
, typename _Hash
,
1580 bool __constant_iterators
, bool __cache
>
1581 struct _Local_const_iterator
1582 : public _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1583 _H1
, _H2
, _Hash
, __cache
>
1586 using __base_type
= _Local_iterator_base
<_Key
, _Value
, _ExtractKey
,
1587 _H1
, _H2
, _Hash
, __cache
>;
1588 using __hash_code_base
= typename
__base_type::__hash_code_base
;
1591 typedef _Value value_type
;
1592 typedef const _Value
* pointer
;
1593 typedef const _Value
& reference
;
1594 typedef std::ptrdiff_t difference_type
;
1595 typedef std::forward_iterator_tag iterator_category
;
1597 _Local_const_iterator() = default;
1599 _Local_const_iterator(const __hash_code_base
& __base
,
1600 _Hash_node
<_Value
, __cache
>* __p
,
1601 std::size_t __bkt
, std::size_t __bkt_count
)
1602 : __base_type(__base
, __p
, __bkt
, __bkt_count
)
1605 _Local_const_iterator(const _Local_iterator
<_Key
, _Value
, _ExtractKey
,
1607 __constant_iterators
,
1614 { return this->_M_cur
->_M_v(); }
1618 { return this->_M_cur
->_M_valptr(); }
1620 _Local_const_iterator
&
1627 _Local_const_iterator
1630 _Local_const_iterator
__tmp(*this);
1637 * Primary class template _Hashtable_base.
1639 * Helper class adding management of _Equal functor to
1640 * _Hash_code_base type.
1642 * Base class templates are:
1643 * - __detail::_Hash_code_base
1644 * - __detail::_Hashtable_ebo_helper
1646 template<typename _Key
, typename _Value
,
1647 typename _ExtractKey
, typename _Equal
,
1648 typename _H1
, typename _H2
, typename _Hash
, typename _Traits
>
1649 struct _Hashtable_base
1650 : public _Hash_code_base
<_Key
, _Value
, _ExtractKey
, _H1
, _H2
, _Hash
,
1651 _Traits::__hash_cached::value
>,
1652 private _Hashtable_ebo_helper
<0, _Equal
>
1655 typedef _Key key_type
;
1656 typedef _Value value_type
;
1657 typedef _Equal key_equal
;
1658 typedef std::size_t size_type
;
1659 typedef std::ptrdiff_t difference_type
;
1661 using __traits_type
= _Traits
;
1662 using __hash_cached
= typename
__traits_type::__hash_cached
;
1663 using __constant_iterators
= typename
__traits_type::__constant_iterators
;
1664 using __unique_keys
= typename
__traits_type::__unique_keys
;
1666 using __hash_code_base
= _Hash_code_base
<_Key
, _Value
, _ExtractKey
,
1668 __hash_cached::value
>;
1670 using __hash_code
= typename
__hash_code_base::__hash_code
;
1671 using __node_type
= typename
__hash_code_base::__node_type
;
1673 using iterator
= __detail::_Node_iterator
<value_type
,
1674 __constant_iterators::value
,
1675 __hash_cached::value
>;
1677 using const_iterator
= __detail::_Node_const_iterator
<value_type
,
1678 __constant_iterators::value
,
1679 __hash_cached::value
>;
1681 using local_iterator
= __detail::_Local_iterator
<key_type
, value_type
,
1682 _ExtractKey
, _H1
, _H2
, _Hash
,
1683 __constant_iterators::value
,
1684 __hash_cached::value
>;
1686 using const_local_iterator
= __detail::_Local_const_iterator
<key_type
,
1688 _ExtractKey
, _H1
, _H2
, _Hash
,
1689 __constant_iterators::value
,
1690 __hash_cached::value
>;
1692 using __ireturn_type
= typename
std::conditional
<__unique_keys::value
,
1693 std::pair
<iterator
, bool>,
1696 using _EqualEBO
= _Hashtable_ebo_helper
<0, _Equal
>;
1697 using _EqualHelper
= _Equal_helper
<_Key
, _Value
, _ExtractKey
, _Equal
,
1698 __hash_code
, __hash_cached::value
>;
1701 _Hashtable_base() = default;
1702 _Hashtable_base(const _ExtractKey
& __ex
, const _H1
& __h1
, const _H2
& __h2
,
1703 const _Hash
& __hash
, const _Equal
& __eq
)
1704 : __hash_code_base(__ex
, __h1
, __h2
, __hash
), _EqualEBO(__eq
)
1708 _M_equals(const _Key
& __k
, __hash_code __c
, __node_type
* __n
) const
1710 return _EqualHelper::_S_equals(_M_eq(), this->_M_extract(),
1715 _M_swap(_Hashtable_base
& __x
)
1717 __hash_code_base::_M_swap(__x
);
1718 std::swap(_M_eq(), __x
._M_eq());
1722 _M_eq() const { return _EqualEBO::_S_cget(*this); }
1725 _M_eq() { return _EqualEBO::_S_get(*this); }
1729 * struct _Equality_base.
1731 * Common types and functions for class _Equality.
1733 struct _Equality_base
1736 template<typename _Uiterator
>
1738 _S_is_permutation(_Uiterator
, _Uiterator
, _Uiterator
);
1741 // See std::is_permutation in N3068.
1742 template<typename _Uiterator
>
1745 _S_is_permutation(_Uiterator __first1
, _Uiterator __last1
,
1746 _Uiterator __first2
)
1748 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1749 if (!(*__first1
== *__first2
))
1752 if (__first1
== __last1
)
1755 _Uiterator __last2
= __first2
;
1756 std::advance(__last2
, std::distance(__first1
, __last1
));
1758 for (_Uiterator __it1
= __first1
; __it1
!= __last1
; ++__it1
)
1760 _Uiterator __tmp
= __first1
;
1761 while (__tmp
!= __it1
&& !bool(*__tmp
== *__it1
))
1764 // We've seen this one before.
1768 std::ptrdiff_t __n2
= 0;
1769 for (__tmp
= __first2
; __tmp
!= __last2
; ++__tmp
)
1770 if (*__tmp
== *__it1
)
1776 std::ptrdiff_t __n1
= 0;
1777 for (__tmp
= __it1
; __tmp
!= __last1
; ++__tmp
)
1778 if (*__tmp
== *__it1
)
1788 * Primary class template _Equality.
1790 * This is for implementing equality comparison for unordered
1791 * containers, per N3068, by John Lakos and Pablo Halpern.
1792 * Algorithmically, we follow closely the reference implementations
1795 template<typename _Key
, typename _Value
, typename _Alloc
,
1796 typename _ExtractKey
, typename _Equal
,
1797 typename _H1
, typename _H2
, typename _Hash
,
1798 typename _RehashPolicy
, typename _Traits
,
1799 bool _Unique_keys
= _Traits::__unique_keys::value
>
1803 template<typename _Key
, typename _Value
, typename _Alloc
,
1804 typename _ExtractKey
, typename _Equal
,
1805 typename _H1
, typename _H2
, typename _Hash
,
1806 typename _RehashPolicy
, typename _Traits
>
1807 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1808 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>
1810 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1811 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1814 _M_equal(const __hashtable
&) const;
1817 template<typename _Key
, typename _Value
, typename _Alloc
,
1818 typename _ExtractKey
, typename _Equal
,
1819 typename _H1
, typename _H2
, typename _Hash
,
1820 typename _RehashPolicy
, typename _Traits
>
1822 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1823 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, true>::
1824 _M_equal(const __hashtable
& __other
) const
1826 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1828 if (__this
->size() != __other
.size())
1831 for (auto __itx
= __this
->begin(); __itx
!= __this
->end(); ++__itx
)
1833 const auto __ity
= __other
.find(_ExtractKey()(*__itx
));
1834 if (__ity
== __other
.end() || !bool(*__ity
== *__itx
))
1841 template<typename _Key
, typename _Value
, typename _Alloc
,
1842 typename _ExtractKey
, typename _Equal
,
1843 typename _H1
, typename _H2
, typename _Hash
,
1844 typename _RehashPolicy
, typename _Traits
>
1845 struct _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1846 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>
1847 : public _Equality_base
1849 using __hashtable
= _Hashtable
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1850 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
>;
1853 _M_equal(const __hashtable
&) const;
1856 template<typename _Key
, typename _Value
, typename _Alloc
,
1857 typename _ExtractKey
, typename _Equal
,
1858 typename _H1
, typename _H2
, typename _Hash
,
1859 typename _RehashPolicy
, typename _Traits
>
1861 _Equality
<_Key
, _Value
, _Alloc
, _ExtractKey
, _Equal
,
1862 _H1
, _H2
, _Hash
, _RehashPolicy
, _Traits
, false>::
1863 _M_equal(const __hashtable
& __other
) const
1865 const __hashtable
* __this
= static_cast<const __hashtable
*>(this);
1867 if (__this
->size() != __other
.size())
1870 for (auto __itx
= __this
->begin(); __itx
!= __this
->end();)
1872 const auto __xrange
= __this
->equal_range(_ExtractKey()(*__itx
));
1873 const auto __yrange
= __other
.equal_range(_ExtractKey()(*__itx
));
1875 if (std::distance(__xrange
.first
, __xrange
.second
)
1876 != std::distance(__yrange
.first
, __yrange
.second
))
1879 if (!_S_is_permutation(__xrange
.first
, __xrange
.second
,
1883 __itx
= __xrange
.second
;
1889 * This type deals with all allocation and keeps an allocator instance through
1890 * inheritance to benefit from EBO when possible.
1892 template<typename _NodeAlloc
>
1893 struct _Hashtable_alloc
: private _Hashtable_ebo_helper
<0, _NodeAlloc
>
1896 using __ebo_node_alloc
= _Hashtable_ebo_helper
<0, _NodeAlloc
>;
1898 using __node_type
= typename
_NodeAlloc::value_type
;
1899 using __node_alloc_type
= _NodeAlloc
;
1900 // Use __gnu_cxx to benefit from _S_always_equal and al.
1901 using __node_alloc_traits
= __gnu_cxx::__alloc_traits
<__node_alloc_type
>;
1903 using __value_type
= typename
__node_type::value_type
;
1904 using __value_alloc_type
=
1905 __alloc_rebind
<__node_alloc_type
, __value_type
>;
1906 using __value_alloc_traits
= std::allocator_traits
<__value_alloc_type
>;
1908 using __node_base
= __detail::_Hash_node_base
;
1909 using __bucket_type
= __node_base
*;
1910 using __bucket_alloc_type
=
1911 __alloc_rebind
<__node_alloc_type
, __bucket_type
>;
1912 using __bucket_alloc_traits
= std::allocator_traits
<__bucket_alloc_type
>;
1914 _Hashtable_alloc() = default;
1915 _Hashtable_alloc(const _Hashtable_alloc
&) = default;
1916 _Hashtable_alloc(_Hashtable_alloc
&&) = default;
1918 template<typename _Alloc
>
1919 _Hashtable_alloc(_Alloc
&& __a
)
1920 : __ebo_node_alloc(std::forward
<_Alloc
>(__a
))
1925 { return __ebo_node_alloc::_S_get(*this); }
1927 const __node_alloc_type
&
1928 _M_node_allocator() const
1929 { return __ebo_node_alloc::_S_cget(*this); }
1931 template<typename
... _Args
>
1933 _M_allocate_node(_Args
&&... __args
);
1936 _M_deallocate_node(__node_type
* __n
);
1938 // Deallocate the linked list of nodes pointed to by __n
1940 _M_deallocate_nodes(__node_type
* __n
);
1943 _M_allocate_buckets(std::size_t __n
);
1946 _M_deallocate_buckets(__bucket_type
*, std::size_t __n
);
1949 // Definitions of class template _Hashtable_alloc's out-of-line member
1951 template<typename _NodeAlloc
>
1952 template<typename
... _Args
>
1953 typename _Hashtable_alloc
<_NodeAlloc
>::__node_type
*
1954 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_node(_Args
&&... __args
)
1956 auto __nptr
= __node_alloc_traits::allocate(_M_node_allocator(), 1);
1957 __node_type
* __n
= std::__addressof(*__nptr
);
1960 __value_alloc_type
__a(_M_node_allocator());
1961 ::new ((void*)__n
) __node_type
;
1962 __value_alloc_traits::construct(__a
, __n
->_M_valptr(),
1963 std::forward
<_Args
>(__args
)...);
1968 __node_alloc_traits::deallocate(_M_node_allocator(), __nptr
, 1);
1969 __throw_exception_again
;
1973 template<typename _NodeAlloc
>
1975 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_node(__node_type
* __n
)
1977 typedef typename
__node_alloc_traits::pointer _Ptr
;
1978 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__n
);
1979 __value_alloc_type
__a(_M_node_allocator());
1980 __value_alloc_traits::destroy(__a
, __n
->_M_valptr());
1981 __n
->~__node_type();
1982 __node_alloc_traits::deallocate(_M_node_allocator(), __ptr
, 1);
1985 template<typename _NodeAlloc
>
1987 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_nodes(__node_type
* __n
)
1991 __node_type
* __tmp
= __n
;
1992 __n
= __n
->_M_next();
1993 _M_deallocate_node(__tmp
);
1997 template<typename _NodeAlloc
>
1998 typename _Hashtable_alloc
<_NodeAlloc
>::__bucket_type
*
1999 _Hashtable_alloc
<_NodeAlloc
>::_M_allocate_buckets(std::size_t __n
)
2001 __bucket_alloc_type
__alloc(_M_node_allocator());
2003 auto __ptr
= __bucket_alloc_traits::allocate(__alloc
, __n
);
2004 __bucket_type
* __p
= std::__addressof(*__ptr
);
2005 __builtin_memset(__p
, 0, __n
* sizeof(__bucket_type
));
2009 template<typename _NodeAlloc
>
2011 _Hashtable_alloc
<_NodeAlloc
>::_M_deallocate_buckets(__bucket_type
* __bkts
,
2014 typedef typename
__bucket_alloc_traits::pointer _Ptr
;
2015 auto __ptr
= std::pointer_traits
<_Ptr
>::pointer_to(*__bkts
);
2016 __bucket_alloc_type
__alloc(_M_node_allocator());
2017 __bucket_alloc_traits::deallocate(__alloc
, __ptr
, __n
);
2020 //@} hashtable-detail
2021 _GLIBCXX_END_NAMESPACE_VERSION
2022 } // namespace __detail
2025 #endif // _HASHTABLE_POLICY_H