blob: c77de961be6b609e1b26804c5e9b10d83a995a52 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP__HASH_TABLE
12#define _LIBCPP__HASH_TABLE
13
14#include <__config>
15#include <initializer_list>
16#include <memory>
17#include <iterator>
18#include <algorithm>
19#include <cmath>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +000020#include <utility>
Eric Fiselier04333f92017-01-13 22:42:53 +000021#include <type_traits>
Howard Hinnant3e519522010-05-11 19:42:16 +000022
Eric Fiselierc1bd9192014-08-10 23:53:08 +000023#include <__debug>
Howard Hinnant42a30462013-08-02 00:26:35 +000024
Howard Hinnant073458b2011-10-17 20:05:10 +000025#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnant3e519522010-05-11 19:42:16 +000026#pragma GCC system_header
Howard Hinnant073458b2011-10-17 20:05:10 +000027#endif
Howard Hinnant3e519522010-05-11 19:42:16 +000028
Eric Fiseliera016efb2017-05-31 22:07:49 +000029_LIBCPP_PUSH_MACROS
30#include <__undef_macros>
Howard Hinnant3e519522010-05-11 19:42:16 +000031
Eric Fiselierfcd02212016-02-11 11:59:44 +000032
Eric Fiseliera016efb2017-05-31 22:07:49 +000033_LIBCPP_BEGIN_NAMESPACE_STD
34
Eric Fiselierfcd02212016-02-11 11:59:44 +000035template <class _Key, class _Tp>
36struct __hash_value_type;
Eric Fiselierfcd02212016-02-11 11:59:44 +000037
Eric Fiselier04333f92017-01-13 22:42:53 +000038template <class _Key, class _Cp, class _Hash,
39 bool = is_empty<_Hash>::value && !__libcpp_is_final<_Hash>::value>
40class __unordered_map_hasher;
41
42template <class _Key, class _Cp, class _Pred,
43 bool = is_empty<_Pred>::value && !__libcpp_is_final<_Pred>::value
44 >
45class __unordered_map_equal;
46
Eric Fiselierfcd02212016-02-11 11:59:44 +000047#ifndef _LIBCPP_CXX03_LANG
48template <class _Tp>
49struct __is_hash_value_type_imp : false_type {};
50
51template <class _Key, class _Value>
52struct __is_hash_value_type_imp<__hash_value_type<_Key, _Value>> : true_type {};
53
54template <class ..._Args>
55struct __is_hash_value_type : false_type {};
56
57template <class _One>
58struct __is_hash_value_type<_One> : __is_hash_value_type_imp<typename __uncvref<_One>::type> {};
59#endif
60
Howard Hinnant6e412562013-03-06 23:30:19 +000061_LIBCPP_FUNC_VIS
Howard Hinnantce534202011-06-14 19:58:17 +000062size_t __next_prime(size_t __n);
Howard Hinnant3e519522010-05-11 19:42:16 +000063
64template <class _NodePtr>
65struct __hash_node_base
66{
Eric Fiselier40492ba2016-07-23 20:36:55 +000067 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
Howard Hinnant3e519522010-05-11 19:42:16 +000068 typedef __hash_node_base __first_node;
Eric Fiselier40492ba2016-07-23 20:36:55 +000069 typedef typename __rebind_pointer<_NodePtr, __first_node>::type __node_base_pointer;
70 typedef _NodePtr __node_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +000071
Eric Fiselier40492ba2016-07-23 20:36:55 +000072#if defined(_LIBCPP_ABI_FIX_UNORDERED_NODE_POINTER_UB)
73 typedef __node_base_pointer __next_pointer;
74#else
75 typedef typename conditional<
76 is_pointer<__node_pointer>::value,
77 __node_base_pointer,
78 __node_pointer>::type __next_pointer;
79#endif
80
81 __next_pointer __next_;
82
83 _LIBCPP_INLINE_VISIBILITY
84 __next_pointer __ptr() _NOEXCEPT {
85 return static_cast<__next_pointer>(
86 pointer_traits<__node_base_pointer>::pointer_to(*this));
87 }
88
89 _LIBCPP_INLINE_VISIBILITY
90 __node_pointer __upcast() _NOEXCEPT {
91 return static_cast<__node_pointer>(
92 pointer_traits<__node_base_pointer>::pointer_to(*this));
93 }
94
95 _LIBCPP_INLINE_VISIBILITY
96 size_t __hash() const _NOEXCEPT {
97 return static_cast<__node_type const&>(*this).__hash_;
98 }
Howard Hinnant3e519522010-05-11 19:42:16 +000099
Howard Hinnant37141072011-06-04 18:54:24 +0000100 _LIBCPP_INLINE_VISIBILITY __hash_node_base() _NOEXCEPT : __next_(nullptr) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000101};
102
103template <class _Tp, class _VoidPtr>
104struct __hash_node
105 : public __hash_node_base
106 <
Eric Fiselier934b0922015-12-30 21:52:00 +0000107 typename __rebind_pointer<_VoidPtr, __hash_node<_Tp, _VoidPtr> >::type
Howard Hinnant3e519522010-05-11 19:42:16 +0000108 >
109{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000110 typedef _Tp __node_value_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000111
Eric Fiselier40492ba2016-07-23 20:36:55 +0000112 size_t __hash_;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000113 __node_value_type __value_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000114};
115
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000116inline _LIBCPP_INLINE_VISIBILITY
117bool
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000118__is_hash_power2(size_t __bc)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000119{
120 return __bc > 2 && !(__bc & (__bc - 1));
121}
122
123inline _LIBCPP_INLINE_VISIBILITY
124size_t
125__constrain_hash(size_t __h, size_t __bc)
126{
Eric Fiselier118cb412016-07-11 22:02:02 +0000127 return !(__bc & (__bc - 1)) ? __h & (__bc - 1) :
128 (__h < __bc ? __h : __h % __bc);
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000129}
130
131inline _LIBCPP_INLINE_VISIBILITY
132size_t
Eric Fiselier9ba5c112015-02-02 21:31:48 +0000133__next_hash_pow2(size_t __n)
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000134{
Marshall Clow87af6462017-06-03 00:08:32 +0000135 return __n < 2 ? __n : (size_t(1) << (std::numeric_limits<size_t>::digits - __clz(__n-1)));
Howard Hinnant4cb38a82012-07-06 17:31:14 +0000136}
137
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000138
Howard Hinnantce534202011-06-14 19:58:17 +0000139template <class _Tp, class _Hash, class _Equal, class _Alloc> class __hash_table;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000140
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000141template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_iterator;
142template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
143template <class _NodePtr> class _LIBCPP_TEMPLATE_VIS __hash_local_iterator;
144template <class _ConstNodePtr> class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
145template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
146template <class _HashIterator> class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000147
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000148template <class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000149struct __hash_key_value_types {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000150 static_assert(!is_reference<_Tp>::value && !is_const<_Tp>::value, "");
151 typedef _Tp key_type;
152 typedef _Tp __node_value_type;
153 typedef _Tp __container_value_type;
154 static const bool __is_map = false;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000155
156 _LIBCPP_INLINE_VISIBILITY
157 static key_type const& __get_key(_Tp const& __v) {
158 return __v;
159 }
160 _LIBCPP_INLINE_VISIBILITY
161 static __container_value_type const& __get_value(__node_value_type const& __v) {
162 return __v;
163 }
164 _LIBCPP_INLINE_VISIBILITY
165 static __container_value_type* __get_ptr(__node_value_type& __n) {
166 return _VSTD::addressof(__n);
167 }
168#ifndef _LIBCPP_CXX03_LANG
169 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000170 static __container_value_type&& __move(__node_value_type& __v) {
Eric Fiselierfcd02212016-02-11 11:59:44 +0000171 return _VSTD::move(__v);
172 }
173#endif
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000174};
175
176template <class _Key, class _Tp>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000177struct __hash_key_value_types<__hash_value_type<_Key, _Tp> > {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000178 typedef _Key key_type;
179 typedef _Tp mapped_type;
180 typedef __hash_value_type<_Key, _Tp> __node_value_type;
181 typedef pair<const _Key, _Tp> __container_value_type;
182 typedef __container_value_type __map_value_type;
183 static const bool __is_map = true;
Eric Fiselierfcd02212016-02-11 11:59:44 +0000184
185 _LIBCPP_INLINE_VISIBILITY
186 static key_type const& __get_key(__container_value_type const& __v) {
187 return __v.first;
188 }
189
190 template <class _Up>
191 _LIBCPP_INLINE_VISIBILITY
192 static typename enable_if<__is_same_uncvref<_Up, __node_value_type>::value,
193 __container_value_type const&>::type
194 __get_value(_Up& __t) {
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000195 return __t.__get_value();
Eric Fiselierfcd02212016-02-11 11:59:44 +0000196 }
197
198 template <class _Up>
199 _LIBCPP_INLINE_VISIBILITY
200 static typename enable_if<__is_same_uncvref<_Up, __container_value_type>::value,
201 __container_value_type const&>::type
202 __get_value(_Up& __t) {
203 return __t;
204 }
205
206 _LIBCPP_INLINE_VISIBILITY
207 static __container_value_type* __get_ptr(__node_value_type& __n) {
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000208 return _VSTD::addressof(__n.__get_value());
Eric Fiselierfcd02212016-02-11 11:59:44 +0000209 }
210#ifndef _LIBCPP_CXX03_LANG
211 _LIBCPP_INLINE_VISIBILITY
Erik Pilkingtonf52318b2018-06-04 20:38:23 +0000212 static pair<key_type&&, mapped_type&&> __move(__node_value_type& __v) {
213 return __v.__move();
Eric Fiselierfcd02212016-02-11 11:59:44 +0000214 }
215#endif
216
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000217};
218
Eric Fiselier43b121d2016-02-20 07:59:16 +0000219template <class _Tp, class _AllocPtr, class _KVTypes = __hash_key_value_types<_Tp>,
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000220 bool = _KVTypes::__is_map>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000221struct __hash_map_pointer_types {};
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000222
223template <class _Tp, class _AllocPtr, class _KVTypes>
Eric Fiselier43b121d2016-02-20 07:59:16 +0000224struct __hash_map_pointer_types<_Tp, _AllocPtr, _KVTypes, true> {
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000225 typedef typename _KVTypes::__map_value_type _Mv;
226 typedef typename __rebind_pointer<_AllocPtr, _Mv>::type
227 __map_value_type_pointer;
228 typedef typename __rebind_pointer<_AllocPtr, const _Mv>::type
229 __const_map_value_type_pointer;
230};
231
232template <class _NodePtr, class _NodeT = typename pointer_traits<_NodePtr>::element_type>
233struct __hash_node_types;
234
235template <class _NodePtr, class _Tp, class _VoidPtr>
236struct __hash_node_types<_NodePtr, __hash_node<_Tp, _VoidPtr> >
Eric Fiselier43b121d2016-02-20 07:59:16 +0000237 : public __hash_key_value_types<_Tp>, __hash_map_pointer_types<_Tp, _VoidPtr>
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000238
239{
Eric Fiselier43b121d2016-02-20 07:59:16 +0000240 typedef __hash_key_value_types<_Tp> __base;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000241
242public:
243 typedef ptrdiff_t difference_type;
244 typedef size_t size_type;
245
246 typedef typename __rebind_pointer<_NodePtr, void>::type __void_pointer;
247
248 typedef typename pointer_traits<_NodePtr>::element_type __node_type;
249 typedef _NodePtr __node_pointer;
250
251 typedef __hash_node_base<__node_pointer> __node_base_type;
252 typedef typename __rebind_pointer<_NodePtr, __node_base_type>::type
253 __node_base_pointer;
254
Eric Fiselier40492ba2016-07-23 20:36:55 +0000255 typedef typename __node_base_type::__next_pointer __next_pointer;
256
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000257 typedef _Tp __node_value_type;
258 typedef typename __rebind_pointer<_VoidPtr, __node_value_type>::type
259 __node_value_type_pointer;
260 typedef typename __rebind_pointer<_VoidPtr, const __node_value_type>::type
261 __const_node_value_type_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000262
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000263private:
264 static_assert(!is_const<__node_type>::value,
265 "_NodePtr should never be a pointer to const");
266 static_assert((is_same<typename pointer_traits<_VoidPtr>::element_type, void>::value),
267 "_VoidPtr does not point to unqualified void type");
268 static_assert((is_same<typename __rebind_pointer<_VoidPtr, __node_type>::type,
269 _NodePtr>::value), "_VoidPtr does not rebind to _NodePtr.");
270};
271
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000272template <class _HashIterator>
273struct __hash_node_types_from_iterator;
274template <class _NodePtr>
275struct __hash_node_types_from_iterator<__hash_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
276template <class _NodePtr>
277struct __hash_node_types_from_iterator<__hash_const_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
278template <class _NodePtr>
279struct __hash_node_types_from_iterator<__hash_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
280template <class _NodePtr>
281struct __hash_node_types_from_iterator<__hash_const_local_iterator<_NodePtr> > : __hash_node_types<_NodePtr> {};
282
283
284template <class _NodeValueTp, class _VoidPtr>
285struct __make_hash_node_types {
286 typedef __hash_node<_NodeValueTp, _VoidPtr> _NodeTp;
287 typedef typename __rebind_pointer<_VoidPtr, _NodeTp>::type _NodePtr;
288 typedef __hash_node_types<_NodePtr> type;
289};
290
Howard Hinnant3e519522010-05-11 19:42:16 +0000291template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000292class _LIBCPP_TEMPLATE_VIS __hash_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000293{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000294 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000295 typedef _NodePtr __node_pointer;
296 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000297
Eric Fiselier40492ba2016-07-23 20:36:55 +0000298 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000299
300public:
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000301 typedef forward_iterator_tag iterator_category;
302 typedef typename _NodeTypes::__node_value_type value_type;
303 typedef typename _NodeTypes::difference_type difference_type;
304 typedef value_type& reference;
305 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000306
Eric Fiselier40492ba2016-07-23 20:36:55 +0000307 _LIBCPP_INLINE_VISIBILITY __hash_iterator() _NOEXCEPT : __node_(nullptr) {
308 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000309 }
310
311#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000312 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000313 __hash_iterator(const __hash_iterator& __i)
314 : __node_(__i.__node_)
315 {
316 __get_db()->__iterator_copy(this, &__i);
317 }
318
Howard Hinnant43d99232010-09-21 17:32:39 +0000319 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000320 ~__hash_iterator()
321 {
322 __get_db()->__erase_i(this);
323 }
324
325 _LIBCPP_INLINE_VISIBILITY
326 __hash_iterator& operator=(const __hash_iterator& __i)
327 {
328 if (this != &__i)
329 {
330 __get_db()->__iterator_copy(this, &__i);
331 __node_ = __i.__node_;
332 }
333 return *this;
334 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000335#endif // _LIBCPP_DEBUG_LEVEL >= 2
336
337 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000338 reference operator*() const {
339 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
340 "Attempted to dereference a non-dereferenceable unordered container iterator");
341 return __node_->__upcast()->__value_;
342 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000343
Howard Hinnant43d99232010-09-21 17:32:39 +0000344 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000345 pointer operator->() const {
346 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
347 "Attempted to dereference a non-dereferenceable unordered container iterator");
348 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
349 }
350
351 _LIBCPP_INLINE_VISIBILITY
352 __hash_iterator& operator++() {
353 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000354 "Attempted to increment non-incrementable unordered container iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000355 __node_ = __node_->__next_;
356 return *this;
357 }
358
Howard Hinnant43d99232010-09-21 17:32:39 +0000359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000360 __hash_iterator operator++(int)
361 {
362 __hash_iterator __t(*this);
363 ++(*this);
364 return __t;
365 }
366
Howard Hinnant43d99232010-09-21 17:32:39 +0000367 friend _LIBCPP_INLINE_VISIBILITY
368 bool operator==(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000369 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000370 return __x.__node_ == __y.__node_;
371 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000372 friend _LIBCPP_INLINE_VISIBILITY
373 bool operator!=(const __hash_iterator& __x, const __hash_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000374 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000375
376private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000377#if _LIBCPP_DEBUG_LEVEL >= 2
378 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000379 __hash_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000380 : __node_(__node)
381 {
382 __get_db()->__insert_ic(this, __c);
383 }
384#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000385 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000386 __hash_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000387 : __node_(__node)
388 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000389#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000390 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000391 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
392 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
393 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
394 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000395};
396
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000397template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000398class _LIBCPP_TEMPLATE_VIS __hash_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000399{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000400 static_assert(!is_const<typename pointer_traits<_NodePtr>::element_type>::value, "");
401 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000402 typedef _NodePtr __node_pointer;
403 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000404
Eric Fiselier40492ba2016-07-23 20:36:55 +0000405 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000406
407public:
Eric Fiselier757373e2016-02-18 00:20:34 +0000408 typedef __hash_iterator<_NodePtr> __non_const_iterator;
409
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000410 typedef forward_iterator_tag iterator_category;
411 typedef typename _NodeTypes::__node_value_type value_type;
412 typedef typename _NodeTypes::difference_type difference_type;
413 typedef const value_type& reference;
414 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
415
Howard Hinnant3e519522010-05-11 19:42:16 +0000416
Eric Fiselier40492ba2016-07-23 20:36:55 +0000417 _LIBCPP_INLINE_VISIBILITY __hash_const_iterator() _NOEXCEPT : __node_(nullptr) {
418 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000419 }
Eric Fiselier40492ba2016-07-23 20:36:55 +0000420
Howard Hinnant43d99232010-09-21 17:32:39 +0000421 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000422 __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000423 : __node_(__x.__node_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000424 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000425 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000426 }
427
428#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000429 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000430 __hash_const_iterator(const __hash_const_iterator& __i)
431 : __node_(__i.__node_)
432 {
433 __get_db()->__iterator_copy(this, &__i);
434 }
435
Howard Hinnant43d99232010-09-21 17:32:39 +0000436 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000437 ~__hash_const_iterator()
438 {
439 __get_db()->__erase_i(this);
440 }
441
442 _LIBCPP_INLINE_VISIBILITY
443 __hash_const_iterator& operator=(const __hash_const_iterator& __i)
444 {
445 if (this != &__i)
446 {
447 __get_db()->__iterator_copy(this, &__i);
448 __node_ = __i.__node_;
449 }
450 return *this;
451 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000452#endif // _LIBCPP_DEBUG_LEVEL >= 2
453
454 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000455 reference operator*() const {
456 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000457 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000458 return __node_->__upcast()->__value_;
459 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000460 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000461 pointer operator->() const {
462 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000463 "Attempted to dereference a non-dereferenceable unordered container const_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000464 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
465 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000466
Howard Hinnant43d99232010-09-21 17:32:39 +0000467 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000468 __hash_const_iterator& operator++() {
469 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
470 "Attempted to increment non-incrementable unordered container const_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000471 __node_ = __node_->__next_;
472 return *this;
473 }
474
Howard Hinnant43d99232010-09-21 17:32:39 +0000475 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000476 __hash_const_iterator operator++(int)
477 {
478 __hash_const_iterator __t(*this);
479 ++(*this);
480 return __t;
481 }
482
Howard Hinnant43d99232010-09-21 17:32:39 +0000483 friend _LIBCPP_INLINE_VISIBILITY
484 bool operator==(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000485 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000486 return __x.__node_ == __y.__node_;
487 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000488 friend _LIBCPP_INLINE_VISIBILITY
489 bool operator!=(const __hash_const_iterator& __x, const __hash_const_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000490 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000491
492private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000493#if _LIBCPP_DEBUG_LEVEL >= 2
494 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000495 __hash_const_iterator(__next_pointer __node, const void* __c) _NOEXCEPT
Howard Hinnantb24c8022013-07-23 22:01:58 +0000496 : __node_(__node)
497 {
498 __get_db()->__insert_ic(this, __c);
499 }
500#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000501 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000502 __hash_const_iterator(__next_pointer __node) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000503 : __node_(__node)
504 {}
Howard Hinnantb24c8022013-07-23 22:01:58 +0000505#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000506 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000507 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
508 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
509 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +0000510};
511
Howard Hinnant3e519522010-05-11 19:42:16 +0000512template <class _NodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000513class _LIBCPP_TEMPLATE_VIS __hash_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000514{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000515 typedef __hash_node_types<_NodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000516 typedef _NodePtr __node_pointer;
517 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000518
Eric Fiselier40492ba2016-07-23 20:36:55 +0000519 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000520 size_t __bucket_;
521 size_t __bucket_count_;
522
Howard Hinnant3e519522010-05-11 19:42:16 +0000523public:
524 typedef forward_iterator_tag iterator_category;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000525 typedef typename _NodeTypes::__node_value_type value_type;
526 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000527 typedef value_type& reference;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000528 typedef typename _NodeTypes::__node_value_type_pointer pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000529
Eric Fiselier40492ba2016-07-23 20:36:55 +0000530 _LIBCPP_INLINE_VISIBILITY __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {
531 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000532 }
533
534#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000536 __hash_local_iterator(const __hash_local_iterator& __i)
537 : __node_(__i.__node_),
538 __bucket_(__i.__bucket_),
539 __bucket_count_(__i.__bucket_count_)
540 {
541 __get_db()->__iterator_copy(this, &__i);
542 }
543
Howard Hinnant43d99232010-09-21 17:32:39 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000545 ~__hash_local_iterator()
546 {
547 __get_db()->__erase_i(this);
548 }
549
550 _LIBCPP_INLINE_VISIBILITY
551 __hash_local_iterator& operator=(const __hash_local_iterator& __i)
552 {
553 if (this != &__i)
554 {
555 __get_db()->__iterator_copy(this, &__i);
556 __node_ = __i.__node_;
557 __bucket_ = __i.__bucket_;
558 __bucket_count_ = __i.__bucket_count_;
559 }
560 return *this;
561 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000562#endif // _LIBCPP_DEBUG_LEVEL >= 2
563
564 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000565 reference operator*() const {
566 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000567 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000568 return __node_->__upcast()->__value_;
569 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000570
Howard Hinnant43d99232010-09-21 17:32:39 +0000571 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000572 pointer operator->() const {
573 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
574 "Attempted to dereference a non-dereferenceable unordered container local_iterator");
575 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
576 }
577
578 _LIBCPP_INLINE_VISIBILITY
579 __hash_local_iterator& operator++() {
580 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000581 "Attempted to increment non-incrementable unordered container local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000582 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000583 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000584 __node_ = nullptr;
585 return *this;
586 }
587
Howard Hinnant43d99232010-09-21 17:32:39 +0000588 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000589 __hash_local_iterator operator++(int)
590 {
591 __hash_local_iterator __t(*this);
592 ++(*this);
593 return __t;
594 }
595
Howard Hinnant43d99232010-09-21 17:32:39 +0000596 friend _LIBCPP_INLINE_VISIBILITY
597 bool operator==(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000598 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000599 return __x.__node_ == __y.__node_;
600 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000601 friend _LIBCPP_INLINE_VISIBILITY
602 bool operator!=(const __hash_local_iterator& __x, const __hash_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000603 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000604
605private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000606#if _LIBCPP_DEBUG_LEVEL >= 2
607 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000608 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000609 size_t __bucket_count, const void* __c) _NOEXCEPT
610 : __node_(__node),
611 __bucket_(__bucket),
612 __bucket_count_(__bucket_count)
613 {
614 __get_db()->__insert_ic(this, __c);
615 if (__node_ != nullptr)
616 __node_ = __node_->__next_;
617 }
618#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000619 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000620 __hash_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000621 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000622 : __node_(__node),
623 __bucket_(__bucket),
624 __bucket_count_(__bucket_count)
625 {
626 if (__node_ != nullptr)
627 __node_ = __node_->__next_;
628 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000629#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000630 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000631 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
632 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000633};
634
635template <class _ConstNodePtr>
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000636class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000637{
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000638 typedef __hash_node_types<_ConstNodePtr> _NodeTypes;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000639 typedef _ConstNodePtr __node_pointer;
640 typedef typename _NodeTypes::__next_pointer __next_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000641
Eric Fiselier40492ba2016-07-23 20:36:55 +0000642 __next_pointer __node_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000643 size_t __bucket_;
644 size_t __bucket_count_;
645
646 typedef pointer_traits<__node_pointer> __pointer_traits;
647 typedef typename __pointer_traits::element_type __node;
648 typedef typename remove_const<__node>::type __non_const_node;
Eric Fiselier934b0922015-12-30 21:52:00 +0000649 typedef typename __rebind_pointer<__node_pointer, __non_const_node>::type
650 __non_const_node_pointer;
Eric Fiselier757373e2016-02-18 00:20:34 +0000651public:
Howard Hinnant3e519522010-05-11 19:42:16 +0000652 typedef __hash_local_iterator<__non_const_node_pointer>
653 __non_const_iterator;
Eric Fiselier757373e2016-02-18 00:20:34 +0000654
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000655 typedef forward_iterator_tag iterator_category;
656 typedef typename _NodeTypes::__node_value_type value_type;
657 typedef typename _NodeTypes::difference_type difference_type;
658 typedef const value_type& reference;
659 typedef typename _NodeTypes::__const_node_value_type_pointer pointer;
Eric Fiselier934b0922015-12-30 21:52:00 +0000660
Howard Hinnant3e519522010-05-11 19:42:16 +0000661
Eric Fiselier40492ba2016-07-23 20:36:55 +0000662 _LIBCPP_INLINE_VISIBILITY __hash_const_local_iterator() _NOEXCEPT : __node_(nullptr) {
663 _LIBCPP_DEBUG_MODE(__get_db()->__insert_i(this));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000664 }
665
Howard Hinnant43d99232010-09-21 17:32:39 +0000666 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000667 __hash_const_local_iterator(const __non_const_iterator& __x) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000668 : __node_(__x.__node_),
669 __bucket_(__x.__bucket_),
670 __bucket_count_(__x.__bucket_count_)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000671 {
Eric Fiselier40492ba2016-07-23 20:36:55 +0000672 _LIBCPP_DEBUG_MODE(__get_db()->__iterator_copy(this, &__x));
Howard Hinnantb24c8022013-07-23 22:01:58 +0000673 }
674
675#if _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant43d99232010-09-21 17:32:39 +0000676 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000677 __hash_const_local_iterator(const __hash_const_local_iterator& __i)
678 : __node_(__i.__node_),
679 __bucket_(__i.__bucket_),
680 __bucket_count_(__i.__bucket_count_)
681 {
682 __get_db()->__iterator_copy(this, &__i);
683 }
684
Howard Hinnant43d99232010-09-21 17:32:39 +0000685 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb24c8022013-07-23 22:01:58 +0000686 ~__hash_const_local_iterator()
687 {
688 __get_db()->__erase_i(this);
689 }
690
691 _LIBCPP_INLINE_VISIBILITY
692 __hash_const_local_iterator& operator=(const __hash_const_local_iterator& __i)
693 {
694 if (this != &__i)
695 {
696 __get_db()->__iterator_copy(this, &__i);
697 __node_ = __i.__node_;
698 __bucket_ = __i.__bucket_;
699 __bucket_count_ = __i.__bucket_count_;
700 }
701 return *this;
702 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000703#endif // _LIBCPP_DEBUG_LEVEL >= 2
704
705 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000706 reference operator*() const {
707 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000708 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
Eric Fiselier40492ba2016-07-23 20:36:55 +0000709 return __node_->__upcast()->__value_;
710 }
Howard Hinnant3e519522010-05-11 19:42:16 +0000711
Howard Hinnant43d99232010-09-21 17:32:39 +0000712 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000713 pointer operator->() const {
714 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
715 "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
716 return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__value_);
717 }
718
719 _LIBCPP_INLINE_VISIBILITY
720 __hash_const_local_iterator& operator++() {
721 _LIBCPP_DEBUG_ASSERT(__get_const_db()->__dereferenceable(this),
Howard Hinnantb24c8022013-07-23 22:01:58 +0000722 "Attempted to increment non-incrementable unordered container const_local_iterator");
Howard Hinnant3e519522010-05-11 19:42:16 +0000723 __node_ = __node_->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000724 if (__node_ != nullptr && __constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
Howard Hinnant3e519522010-05-11 19:42:16 +0000725 __node_ = nullptr;
726 return *this;
727 }
728
Howard Hinnant43d99232010-09-21 17:32:39 +0000729 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000730 __hash_const_local_iterator operator++(int)
731 {
732 __hash_const_local_iterator __t(*this);
733 ++(*this);
734 return __t;
735 }
736
Howard Hinnant43d99232010-09-21 17:32:39 +0000737 friend _LIBCPP_INLINE_VISIBILITY
738 bool operator==(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000739 {
Howard Hinnantb24c8022013-07-23 22:01:58 +0000740 return __x.__node_ == __y.__node_;
741 }
Howard Hinnant43d99232010-09-21 17:32:39 +0000742 friend _LIBCPP_INLINE_VISIBILITY
743 bool operator!=(const __hash_const_local_iterator& __x, const __hash_const_local_iterator& __y)
Howard Hinnantb24c8022013-07-23 22:01:58 +0000744 {return !(__x == __y);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000745
746private:
Howard Hinnantb24c8022013-07-23 22:01:58 +0000747#if _LIBCPP_DEBUG_LEVEL >= 2
748 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000749 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnantb24c8022013-07-23 22:01:58 +0000750 size_t __bucket_count, const void* __c) _NOEXCEPT
751 : __node_(__node),
752 __bucket_(__bucket),
753 __bucket_count_(__bucket_count)
754 {
755 __get_db()->__insert_ic(this, __c);
756 if (__node_ != nullptr)
757 __node_ = __node_->__next_;
758 }
759#else
Howard Hinnant43d99232010-09-21 17:32:39 +0000760 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier40492ba2016-07-23 20:36:55 +0000761 __hash_const_local_iterator(__next_pointer __node, size_t __bucket,
Howard Hinnant37141072011-06-04 18:54:24 +0000762 size_t __bucket_count) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000763 : __node_(__node),
764 __bucket_(__bucket),
765 __bucket_count_(__bucket_count)
766 {
767 if (__node_ != nullptr)
768 __node_ = __node_->__next_;
769 }
Howard Hinnantb24c8022013-07-23 22:01:58 +0000770#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000771 template <class, class, class, class> friend class __hash_table;
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +0000772 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000773};
774
775template <class _Alloc>
776class __bucket_list_deallocator
777{
778 typedef _Alloc allocator_type;
779 typedef allocator_traits<allocator_type> __alloc_traits;
780 typedef typename __alloc_traits::size_type size_type;
781
782 __compressed_pair<size_type, allocator_type> __data_;
783public:
784 typedef typename __alloc_traits::pointer pointer;
785
Howard Hinnant43d99232010-09-21 17:32:39 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000787 __bucket_list_deallocator()
Howard Hinnant37141072011-06-04 18:54:24 +0000788 _NOEXCEPT_(is_nothrow_default_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000789 : __data_(0) {}
Howard Hinnant43d99232010-09-21 17:32:39 +0000790
791 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000792 __bucket_list_deallocator(const allocator_type& __a, size_type __size)
Howard Hinnant37141072011-06-04 18:54:24 +0000793 _NOEXCEPT_(is_nothrow_copy_constructible<allocator_type>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +0000794 : __data_(__size, __a) {}
795
Eric Fiselierafa7a952017-04-19 01:23:04 +0000796#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant43d99232010-09-21 17:32:39 +0000797 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000798 __bucket_list_deallocator(__bucket_list_deallocator&& __x)
Howard Hinnant37141072011-06-04 18:54:24 +0000799 _NOEXCEPT_(is_nothrow_move_constructible<allocator_type>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +0000800 : __data_(_VSTD::move(__x.__data_))
Howard Hinnant3e519522010-05-11 19:42:16 +0000801 {
802 __x.size() = 0;
803 }
Eric Fiselierafa7a952017-04-19 01:23:04 +0000804#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000805
Howard Hinnant37141072011-06-04 18:54:24 +0000806 _LIBCPP_INLINE_VISIBILITY
807 size_type& size() _NOEXCEPT {return __data_.first();}
808 _LIBCPP_INLINE_VISIBILITY
809 size_type size() const _NOEXCEPT {return __data_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000810
Howard Hinnant43d99232010-09-21 17:32:39 +0000811 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000812 allocator_type& __alloc() _NOEXCEPT {return __data_.second();}
813 _LIBCPP_INLINE_VISIBILITY
814 const allocator_type& __alloc() const _NOEXCEPT {return __data_.second();}
815
816 _LIBCPP_INLINE_VISIBILITY
817 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000818 {
819 __alloc_traits::deallocate(__alloc(), __p, size());
820 }
821};
822
Howard Hinnantce534202011-06-14 19:58:17 +0000823template <class _Alloc> class __hash_map_node_destructor;
Howard Hinnant3e519522010-05-11 19:42:16 +0000824
825template <class _Alloc>
826class __hash_node_destructor
827{
828 typedef _Alloc allocator_type;
829 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000830
Howard Hinnant3e519522010-05-11 19:42:16 +0000831public:
832 typedef typename __alloc_traits::pointer pointer;
833private:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000834 typedef __hash_node_types<pointer> _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000835
836 allocator_type& __na_;
837
838 __hash_node_destructor& operator=(const __hash_node_destructor&);
839
840public:
841 bool __value_constructed;
842
Howard Hinnant43d99232010-09-21 17:32:39 +0000843 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantf622b582011-07-31 17:04:30 +0000844 explicit __hash_node_destructor(allocator_type& __na,
845 bool __constructed = false) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000846 : __na_(__na),
Howard Hinnantf622b582011-07-31 17:04:30 +0000847 __value_constructed(__constructed)
Howard Hinnant3e519522010-05-11 19:42:16 +0000848 {}
849
Howard Hinnant43d99232010-09-21 17:32:39 +0000850 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +0000851 void operator()(pointer __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000852 {
853 if (__value_constructed)
Eric Fiselierfcd02212016-02-11 11:59:44 +0000854 __alloc_traits::destroy(__na_, _NodeTypes::__get_ptr(__p->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +0000855 if (__p)
856 __alloc_traits::deallocate(__na_, __p, 1);
857 }
858
859 template <class> friend class __hash_map_node_destructor;
860};
861
Erik Pilkingtonb0386a52018-08-01 01:33:38 +0000862#if _LIBCPP_STD_VER > 14
863template <class _NodeType, class _Alloc>
864struct __generic_container_node_destructor;
865
866template <class _Tp, class _VoidPtr, class _Alloc>
867struct __generic_container_node_destructor<__hash_node<_Tp, _VoidPtr>, _Alloc>
868 : __hash_node_destructor<_Alloc>
869{
870 using __hash_node_destructor<_Alloc>::__hash_node_destructor;
871};
872#endif
Eric Fiselier04333f92017-01-13 22:42:53 +0000873
874#ifndef _LIBCPP_CXX03_LANG
875template <class _Key, class _Hash, class _Equal, class _Alloc>
876struct __diagnose_hash_table_helper {
877 static constexpr bool __trigger_diagnostics()
Eric Fiselierbd6a2d82017-03-03 22:35:58 +0000878 _LIBCPP_DIAGNOSE_WARNING(__check_hash_requirements<_Key, _Hash>::value
Eric Fiselieracb21582017-03-01 02:02:28 +0000879 && !__invokable<_Hash const&, _Key const&>::value,
880 "the specified hash functor does not provide a const call operator")
881 _LIBCPP_DIAGNOSE_WARNING(is_copy_constructible<_Equal>::value
882 && !__invokable<_Equal const&, _Key const&, _Key const&>::value,
883 "the specified comparator type does not provide a const call operator")
884 {
Eric Fiselierbd6a2d82017-03-03 22:35:58 +0000885 static_assert(__check_hash_requirements<_Key, _Hash>::value,
886 "the specified hash does not meet the Hash requirements");
Eric Fiselieracb21582017-03-01 02:02:28 +0000887 static_assert(is_copy_constructible<_Equal>::value,
888 "the specified comparator is required to be copy constructible");
889 return true;
890 }
Eric Fiselier04333f92017-01-13 22:42:53 +0000891};
892
893template <class _Key, class _Value, class _Hash, class _Equal, class _Alloc>
894struct __diagnose_hash_table_helper<
895 __hash_value_type<_Key, _Value>,
896 __unordered_map_hasher<_Key, __hash_value_type<_Key, _Value>, _Hash>,
897 __unordered_map_equal<_Key, __hash_value_type<_Key, _Value>, _Equal>,
898 _Alloc>
899: __diagnose_hash_table_helper<_Key, _Hash, _Equal, _Alloc>
900{
901};
902#endif // _LIBCPP_CXX03_LANG
903
Howard Hinnant3e519522010-05-11 19:42:16 +0000904template <class _Tp, class _Hash, class _Equal, class _Alloc>
905class __hash_table
906{
907public:
908 typedef _Tp value_type;
909 typedef _Hash hasher;
910 typedef _Equal key_equal;
911 typedef _Alloc allocator_type;
912
913private:
914 typedef allocator_traits<allocator_type> __alloc_traits;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000915 typedef typename
916 __make_hash_node_types<value_type, typename __alloc_traits::void_pointer>::type
917 _NodeTypes;
Howard Hinnant3e519522010-05-11 19:42:16 +0000918public:
Eric Fiselierfcd02212016-02-11 11:59:44 +0000919
920 typedef typename _NodeTypes::__node_value_type __node_value_type;
921 typedef typename _NodeTypes::__container_value_type __container_value_type;
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +0000922 typedef typename _NodeTypes::key_type key_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000923 typedef value_type& reference;
924 typedef const value_type& const_reference;
925 typedef typename __alloc_traits::pointer pointer;
926 typedef typename __alloc_traits::const_pointer const_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000927#ifndef _LIBCPP_ABI_FIX_UNORDERED_CONTAINER_SIZE_TYPE
Howard Hinnant3e519522010-05-11 19:42:16 +0000928 typedef typename __alloc_traits::size_type size_type;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000929#else
930 typedef typename _NodeTypes::size_type size_type;
931#endif
932 typedef typename _NodeTypes::difference_type difference_type;
Howard Hinnant3e519522010-05-11 19:42:16 +0000933public:
934 // Create __node
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000935
936 typedef typename _NodeTypes::__node_type __node;
Marshall Clow1f508012015-04-07 05:21:38 +0000937 typedef typename __rebind_alloc_helper<__alloc_traits, __node>::type __node_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000938 typedef allocator_traits<__node_allocator> __node_traits;
Eric Fiselier8e397682016-02-11 15:22:37 +0000939 typedef typename _NodeTypes::__void_pointer __void_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000940 typedef typename _NodeTypes::__node_pointer __node_pointer;
941 typedef typename _NodeTypes::__node_pointer __node_const_pointer;
942 typedef typename _NodeTypes::__node_base_type __first_node;
943 typedef typename _NodeTypes::__node_base_pointer __node_base_pointer;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000944 typedef typename _NodeTypes::__next_pointer __next_pointer;
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000945
946private:
947 // check for sane allocator pointer rebinding semantics. Rebinding the
948 // allocator for a new pointer type should be exactly the same as rebinding
949 // the pointer using 'pointer_traits'.
950 static_assert((is_same<__node_pointer, typename __node_traits::pointer>::value),
951 "Allocator does not rebind pointers in a sane manner.");
952 typedef typename __rebind_alloc_helper<__node_traits, __first_node>::type
953 __node_base_allocator;
954 typedef allocator_traits<__node_base_allocator> __node_base_traits;
955 static_assert((is_same<__node_base_pointer, typename __node_base_traits::pointer>::value),
956 "Allocator does not rebind pointers in a sane manner.");
Howard Hinnant3e519522010-05-11 19:42:16 +0000957
958private:
959
Eric Fiselier40492ba2016-07-23 20:36:55 +0000960 typedef typename __rebind_alloc_helper<__node_traits, __next_pointer>::type __pointer_allocator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000961 typedef __bucket_list_deallocator<__pointer_allocator> __bucket_list_deleter;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000962 typedef unique_ptr<__next_pointer[], __bucket_list_deleter> __bucket_list;
Howard Hinnant3e519522010-05-11 19:42:16 +0000963 typedef allocator_traits<__pointer_allocator> __pointer_alloc_traits;
Eric Fiselier40492ba2016-07-23 20:36:55 +0000964 typedef typename __bucket_list_deleter::pointer __node_pointer_pointer;
Howard Hinnant3e519522010-05-11 19:42:16 +0000965
Eric Fiselieracb21582017-03-01 02:02:28 +0000966#ifndef _LIBCPP_CXX03_LANG
967 static_assert(__diagnose_hash_table_helper<_Tp, _Hash, _Equal, _Alloc>::__trigger_diagnostics(), "");
968#endif
969
Howard Hinnant3e519522010-05-11 19:42:16 +0000970 // --- Member data begin ---
Eric Fiselier75d0dcf2016-02-10 20:46:23 +0000971 __bucket_list __bucket_list_;
972 __compressed_pair<__first_node, __node_allocator> __p1_;
973 __compressed_pair<size_type, hasher> __p2_;
974 __compressed_pair<float, key_equal> __p3_;
Howard Hinnant3e519522010-05-11 19:42:16 +0000975 // --- Member data end ---
976
Howard Hinnant37141072011-06-04 18:54:24 +0000977 _LIBCPP_INLINE_VISIBILITY
978 size_type& size() _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000979public:
Howard Hinnant37141072011-06-04 18:54:24 +0000980 _LIBCPP_INLINE_VISIBILITY
981 size_type size() const _NOEXCEPT {return __p2_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000982
Howard Hinnant37141072011-06-04 18:54:24 +0000983 _LIBCPP_INLINE_VISIBILITY
984 hasher& hash_function() _NOEXCEPT {return __p2_.second();}
985 _LIBCPP_INLINE_VISIBILITY
986 const hasher& hash_function() const _NOEXCEPT {return __p2_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000987
Howard Hinnant37141072011-06-04 18:54:24 +0000988 _LIBCPP_INLINE_VISIBILITY
989 float& max_load_factor() _NOEXCEPT {return __p3_.first();}
990 _LIBCPP_INLINE_VISIBILITY
991 float max_load_factor() const _NOEXCEPT {return __p3_.first();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000992
Howard Hinnant37141072011-06-04 18:54:24 +0000993 _LIBCPP_INLINE_VISIBILITY
994 key_equal& key_eq() _NOEXCEPT {return __p3_.second();}
995 _LIBCPP_INLINE_VISIBILITY
996 const key_equal& key_eq() const _NOEXCEPT {return __p3_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000997
Howard Hinnant37141072011-06-04 18:54:24 +0000998 _LIBCPP_INLINE_VISIBILITY
999 __node_allocator& __node_alloc() _NOEXCEPT {return __p1_.second();}
1000 _LIBCPP_INLINE_VISIBILITY
1001 const __node_allocator& __node_alloc() const _NOEXCEPT
1002 {return __p1_.second();}
Howard Hinnant3e519522010-05-11 19:42:16 +00001003
1004public:
1005 typedef __hash_iterator<__node_pointer> iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +00001006 typedef __hash_const_iterator<__node_pointer> const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +00001007 typedef __hash_local_iterator<__node_pointer> local_iterator;
Howard Hinnant307f8142013-06-22 15:21:29 +00001008 typedef __hash_const_local_iterator<__node_pointer> const_local_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +00001009
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001010 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001011 __hash_table()
1012 _NOEXCEPT_(
1013 is_nothrow_default_constructible<__bucket_list>::value &&
1014 is_nothrow_default_constructible<__first_node>::value &&
1015 is_nothrow_default_constructible<__node_allocator>::value &&
1016 is_nothrow_default_constructible<hasher>::value &&
1017 is_nothrow_default_constructible<key_equal>::value);
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001018 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001019 __hash_table(const hasher& __hf, const key_equal& __eql);
1020 __hash_table(const hasher& __hf, const key_equal& __eql,
1021 const allocator_type& __a);
1022 explicit __hash_table(const allocator_type& __a);
1023 __hash_table(const __hash_table& __u);
1024 __hash_table(const __hash_table& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001025#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant37141072011-06-04 18:54:24 +00001026 __hash_table(__hash_table&& __u)
1027 _NOEXCEPT_(
1028 is_nothrow_move_constructible<__bucket_list>::value &&
1029 is_nothrow_move_constructible<__first_node>::value &&
1030 is_nothrow_move_constructible<__node_allocator>::value &&
1031 is_nothrow_move_constructible<hasher>::value &&
1032 is_nothrow_move_constructible<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001033 __hash_table(__hash_table&& __u, const allocator_type& __a);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001034#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001035 ~__hash_table();
1036
1037 __hash_table& operator=(const __hash_table& __u);
Eric Fiselierfcd02212016-02-11 11:59:44 +00001038#ifndef _LIBCPP_CXX03_LANG
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001039 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001040 __hash_table& operator=(__hash_table&& __u)
1041 _NOEXCEPT_(
1042 __node_traits::propagate_on_container_move_assignment::value &&
1043 is_nothrow_move_assignable<__node_allocator>::value &&
1044 is_nothrow_move_assignable<hasher>::value &&
1045 is_nothrow_move_assignable<key_equal>::value);
Howard Hinnant3e519522010-05-11 19:42:16 +00001046#endif
1047 template <class _InputIterator>
1048 void __assign_unique(_InputIterator __first, _InputIterator __last);
1049 template <class _InputIterator>
1050 void __assign_multi(_InputIterator __first, _InputIterator __last);
1051
Howard Hinnant43d99232010-09-21 17:32:39 +00001052 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001053 size_type max_size() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001054 {
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001055 return std::min<size_type>(
Eric Fiselier341c9dd2016-11-23 09:16:12 +00001056 __node_traits::max_size(__node_alloc()),
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001057 numeric_limits<difference_type >::max()
1058 );
Howard Hinnant3e519522010-05-11 19:42:16 +00001059 }
1060
1061 pair<iterator, bool> __node_insert_unique(__node_pointer __nd);
1062 iterator __node_insert_multi(__node_pointer __nd);
1063 iterator __node_insert_multi(const_iterator __p,
1064 __node_pointer __nd);
1065
Eric Fiselierfcd02212016-02-11 11:59:44 +00001066#ifndef _LIBCPP_CXX03_LANG
1067 template <class _Key, class ..._Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001068 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001069 pair<iterator, bool> __emplace_unique_key_args(_Key const& __k, _Args&&... __args);
Howard Hinnant3e519522010-05-11 19:42:16 +00001070
Eric Fiselierfcd02212016-02-11 11:59:44 +00001071 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001072 _LIBCPP_INLINE_VISIBILITY
1073 pair<iterator, bool> __emplace_unique_impl(_Args&&... __args);
1074
1075 template <class _Pp>
1076 _LIBCPP_INLINE_VISIBILITY
1077 pair<iterator, bool> __emplace_unique(_Pp&& __x) {
1078 return __emplace_unique_extract_key(_VSTD::forward<_Pp>(__x),
1079 __can_extract_key<_Pp, key_type>());
1080 }
Eric Fiselier50088682016-04-16 00:23:12 +00001081
1082 template <class _First, class _Second>
1083 _LIBCPP_INLINE_VISIBILITY
1084 typename enable_if<
1085 __can_extract_map_key<_First, key_type, __container_value_type>::value,
1086 pair<iterator, bool>
1087 >::type __emplace_unique(_First&& __f, _Second&& __s) {
1088 return __emplace_unique_key_args(__f, _VSTD::forward<_First>(__f),
1089 _VSTD::forward<_Second>(__s));
1090 }
1091
Eric Fiselierfcd02212016-02-11 11:59:44 +00001092 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001093 _LIBCPP_INLINE_VISIBILITY
1094 pair<iterator, bool> __emplace_unique(_Args&&... __args) {
1095 return __emplace_unique_impl(_VSTD::forward<_Args>(__args)...);
1096 }
1097
1098 template <class _Pp>
1099 _LIBCPP_INLINE_VISIBILITY
1100 pair<iterator, bool>
1101 __emplace_unique_extract_key(_Pp&& __x, __extract_key_fail_tag) {
1102 return __emplace_unique_impl(_VSTD::forward<_Pp>(__x));
1103 }
1104 template <class _Pp>
1105 _LIBCPP_INLINE_VISIBILITY
1106 pair<iterator, bool>
1107 __emplace_unique_extract_key(_Pp&& __x, __extract_key_self_tag) {
1108 return __emplace_unique_key_args(__x, _VSTD::forward<_Pp>(__x));
1109 }
1110 template <class _Pp>
1111 _LIBCPP_INLINE_VISIBILITY
1112 pair<iterator, bool>
1113 __emplace_unique_extract_key(_Pp&& __x, __extract_key_first_tag) {
1114 return __emplace_unique_key_args(__x.first, _VSTD::forward<_Pp>(__x));
1115 }
1116
1117 template <class... _Args>
1118 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001119 iterator __emplace_multi(_Args&&... __args);
1120 template <class... _Args>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00001121 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001122 iterator __emplace_hint_multi(const_iterator __p, _Args&&... __args);
1123
1124
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001125 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001126 pair<iterator, bool>
1127 __insert_unique(__container_value_type&& __x) {
1128 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), _VSTD::move(__x));
1129 }
1130
1131 template <class _Pp, class = typename enable_if<
1132 !__is_same_uncvref<_Pp, __container_value_type>::value
1133 >::type>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001134 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001135 pair<iterator, bool> __insert_unique(_Pp&& __x) {
1136 return __emplace_unique(_VSTD::forward<_Pp>(__x));
1137 }
1138
1139 template <class _Pp>
1140 _LIBCPP_INLINE_VISIBILITY
1141 iterator __insert_multi(_Pp&& __x) {
1142 return __emplace_multi(_VSTD::forward<_Pp>(__x));
1143 }
1144
1145 template <class _Pp>
1146 _LIBCPP_INLINE_VISIBILITY
1147 iterator __insert_multi(const_iterator __p, _Pp&& __x) {
1148 return __emplace_hint_multi(__p, _VSTD::forward<_Pp>(__x));
1149 }
1150
1151#else // !defined(_LIBCPP_CXX03_LANG)
1152 template <class _Key, class _Args>
Eric Fiselier4271d012016-09-25 04:05:46 +00001153 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierfcd02212016-02-11 11:59:44 +00001154 pair<iterator, bool> __emplace_unique_key_args(_Key const&, _Args& __args);
1155
1156 iterator __insert_multi(const __container_value_type& __x);
1157 iterator __insert_multi(const_iterator __p, const __container_value_type& __x);
Eric Fiselierde3f2b32015-06-13 07:18:32 +00001158#endif
1159
Eric Fiselierfcd02212016-02-11 11:59:44 +00001160 _LIBCPP_INLINE_VISIBILITY
1161 pair<iterator, bool> __insert_unique(const __container_value_type& __x) {
1162 return __emplace_unique_key_args(_NodeTypes::__get_key(__x), __x);
1163 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001164
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00001165#if _LIBCPP_STD_VER > 14
1166 template <class _NodeHandle, class _InsertReturnType>
1167 _LIBCPP_INLINE_VISIBILITY
1168 _InsertReturnType __node_handle_insert_unique(_NodeHandle&& __nh);
1169 template <class _NodeHandle>
1170 _LIBCPP_INLINE_VISIBILITY
1171 iterator __node_handle_insert_unique(const_iterator __hint,
1172 _NodeHandle&& __nh);
1173
1174 template <class _NodeHandle>
1175 _LIBCPP_INLINE_VISIBILITY
1176 iterator __node_handle_insert_multi(_NodeHandle&& __nh);
1177 template <class _NodeHandle>
1178 _LIBCPP_INLINE_VISIBILITY
1179 iterator __node_handle_insert_multi(const_iterator __hint, _NodeHandle&& __nh);
1180
1181 template <class _NodeHandle>
1182 _LIBCPP_INLINE_VISIBILITY
1183 _NodeHandle __node_handle_extract(key_type const& __key);
1184 template <class _NodeHandle>
1185 _LIBCPP_INLINE_VISIBILITY
1186 _NodeHandle __node_handle_extract(const_iterator __it);
1187#endif
1188
Howard Hinnant37141072011-06-04 18:54:24 +00001189 void clear() _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001190 void rehash(size_type __n);
Howard Hinnant43d99232010-09-21 17:32:39 +00001191 _LIBCPP_INLINE_VISIBILITY void reserve(size_type __n)
Howard Hinnant3e519522010-05-11 19:42:16 +00001192 {rehash(static_cast<size_type>(ceil(__n / max_load_factor())));}
Howard Hinnant43d99232010-09-21 17:32:39 +00001193
1194 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001195 size_type bucket_count() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001196 {
1197 return __bucket_list_.get_deleter().size();
1198 }
1199
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001200 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001201 iterator begin() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001202 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001203 iterator end() _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001204 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001205 const_iterator begin() const _NOEXCEPT;
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001206 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001207 const_iterator end() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001208
1209 template <class _Key>
Howard Hinnant43d99232010-09-21 17:32:39 +00001210 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001211 size_type bucket(const _Key& __k) const
Howard Hinnante5c13de2013-07-29 19:05:47 +00001212 {
1213 _LIBCPP_ASSERT(bucket_count() > 0,
1214 "unordered container::bucket(key) called when bucket_count() == 0");
1215 return __constrain_hash(hash_function()(__k), bucket_count());
1216 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001217
1218 template <class _Key>
1219 iterator find(const _Key& __x);
1220 template <class _Key>
1221 const_iterator find(const _Key& __x) const;
1222
Howard Hinnantc003db12011-11-29 18:15:50 +00001223 typedef __hash_node_destructor<__node_allocator> _Dp;
1224 typedef unique_ptr<__node, _Dp> __node_holder;
Howard Hinnant3e519522010-05-11 19:42:16 +00001225
1226 iterator erase(const_iterator __p);
1227 iterator erase(const_iterator __first, const_iterator __last);
1228 template <class _Key>
1229 size_type __erase_unique(const _Key& __k);
1230 template <class _Key>
1231 size_type __erase_multi(const _Key& __k);
Howard Hinnant37141072011-06-04 18:54:24 +00001232 __node_holder remove(const_iterator __p) _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +00001233
1234 template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001236 size_type __count_unique(const _Key& __k) const;
1237 template <class _Key>
1238 size_type __count_multi(const _Key& __k) const;
1239
1240 template <class _Key>
1241 pair<iterator, iterator>
1242 __equal_range_unique(const _Key& __k);
1243 template <class _Key>
1244 pair<const_iterator, const_iterator>
1245 __equal_range_unique(const _Key& __k) const;
1246
1247 template <class _Key>
1248 pair<iterator, iterator>
1249 __equal_range_multi(const _Key& __k);
1250 template <class _Key>
1251 pair<const_iterator, const_iterator>
1252 __equal_range_multi(const _Key& __k) const;
1253
Howard Hinnant37141072011-06-04 18:54:24 +00001254 void swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00001255#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00001256 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00001257 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00001258 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
1259 || __is_nothrow_swappable<__pointer_allocator>::value)
1260 && (!__node_traits::propagate_on_container_swap::value
1261 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00001262 );
Eric Fiselier87a82492015-07-18 20:40:46 +00001263#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00001264 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value);
Eric Fiselier87a82492015-07-18 20:40:46 +00001265#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001266
Howard Hinnant43d99232010-09-21 17:32:39 +00001267 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001268 size_type max_bucket_count() const _NOEXCEPT
Eric Fiselier55b31b4e2016-11-23 01:18:56 +00001269 {return max_size(); }
Howard Hinnant3e519522010-05-11 19:42:16 +00001270 size_type bucket_size(size_type __n) const;
Howard Hinnant37141072011-06-04 18:54:24 +00001271 _LIBCPP_INLINE_VISIBILITY float load_factor() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001272 {
1273 size_type __bc = bucket_count();
1274 return __bc != 0 ? (float)size() / __bc : 0.f;
1275 }
Howard Hinnant37141072011-06-04 18:54:24 +00001276 _LIBCPP_INLINE_VISIBILITY void max_load_factor(float __mlf) _NOEXCEPT
Howard Hinnante5c13de2013-07-29 19:05:47 +00001277 {
1278 _LIBCPP_ASSERT(__mlf > 0,
1279 "unordered container::max_load_factor(lf) called with lf <= 0");
1280 max_load_factor() = _VSTD::max(__mlf, load_factor());
1281 }
Howard Hinnant3e519522010-05-11 19:42:16 +00001282
Howard Hinnantb24c8022013-07-23 22:01:58 +00001283 _LIBCPP_INLINE_VISIBILITY
1284 local_iterator
1285 begin(size_type __n)
1286 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001287 _LIBCPP_ASSERT(__n < bucket_count(),
1288 "unordered container::begin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001289#if _LIBCPP_DEBUG_LEVEL >= 2
1290 return local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1291#else
1292 return local_iterator(__bucket_list_[__n], __n, bucket_count());
1293#endif
1294 }
1295
1296 _LIBCPP_INLINE_VISIBILITY
1297 local_iterator
1298 end(size_type __n)
1299 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001300 _LIBCPP_ASSERT(__n < bucket_count(),
1301 "unordered container::end(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001302#if _LIBCPP_DEBUG_LEVEL >= 2
1303 return local_iterator(nullptr, __n, bucket_count(), this);
1304#else
1305 return local_iterator(nullptr, __n, bucket_count());
1306#endif
1307 }
1308
1309 _LIBCPP_INLINE_VISIBILITY
1310 const_local_iterator
1311 cbegin(size_type __n) const
1312 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001313 _LIBCPP_ASSERT(__n < bucket_count(),
1314 "unordered container::cbegin(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001315#if _LIBCPP_DEBUG_LEVEL >= 2
1316 return const_local_iterator(__bucket_list_[__n], __n, bucket_count(), this);
1317#else
1318 return const_local_iterator(__bucket_list_[__n], __n, bucket_count());
1319#endif
1320 }
1321
1322 _LIBCPP_INLINE_VISIBILITY
1323 const_local_iterator
1324 cend(size_type __n) const
1325 {
Howard Hinnante5c13de2013-07-29 19:05:47 +00001326 _LIBCPP_ASSERT(__n < bucket_count(),
1327 "unordered container::cend(n) called with n >= bucket_count()");
Howard Hinnantb24c8022013-07-23 22:01:58 +00001328#if _LIBCPP_DEBUG_LEVEL >= 2
1329 return const_local_iterator(nullptr, __n, bucket_count(), this);
1330#else
1331 return const_local_iterator(nullptr, __n, bucket_count());
1332#endif
1333 }
1334
1335#if _LIBCPP_DEBUG_LEVEL >= 2
1336
1337 bool __dereferenceable(const const_iterator* __i) const;
1338 bool __decrementable(const const_iterator* __i) const;
1339 bool __addable(const const_iterator* __i, ptrdiff_t __n) const;
1340 bool __subscriptable(const const_iterator* __i, ptrdiff_t __n) const;
1341
1342#endif // _LIBCPP_DEBUG_LEVEL >= 2
1343
Howard Hinnant3e519522010-05-11 19:42:16 +00001344private:
1345 void __rehash(size_type __n);
1346
Eric Fiselierfcd02212016-02-11 11:59:44 +00001347#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001348 template <class ..._Args>
Eric Fiselierfcd02212016-02-11 11:59:44 +00001349 __node_holder __construct_node(_Args&& ...__args);
1350
1351 template <class _First, class ..._Rest>
1352 __node_holder __construct_node_hash(size_t __hash, _First&& __f, _Rest&&... __rest);
1353#else // _LIBCPP_CXX03_LANG
1354 __node_holder __construct_node(const __container_value_type& __v);
1355 __node_holder __construct_node_hash(size_t __hash, const __container_value_type& __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00001356#endif
Eric Fiselierfcd02212016-02-11 11:59:44 +00001357
Howard Hinnant3e519522010-05-11 19:42:16 +00001358
Howard Hinnant43d99232010-09-21 17:32:39 +00001359 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001360 void __copy_assign_alloc(const __hash_table& __u)
1361 {__copy_assign_alloc(__u, integral_constant<bool,
1362 __node_traits::propagate_on_container_copy_assignment::value>());}
1363 void __copy_assign_alloc(const __hash_table& __u, true_type);
Howard Hinnant43d99232010-09-21 17:32:39 +00001364 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc2063662011-12-01 20:21:04 +00001365 void __copy_assign_alloc(const __hash_table&, false_type) {}
Howard Hinnant3e519522010-05-11 19:42:16 +00001366
Eric Fiselierfcd02212016-02-11 11:59:44 +00001367#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001368 void __move_assign(__hash_table& __u, false_type);
Howard Hinnant37141072011-06-04 18:54:24 +00001369 void __move_assign(__hash_table& __u, true_type)
1370 _NOEXCEPT_(
1371 is_nothrow_move_assignable<__node_allocator>::value &&
1372 is_nothrow_move_assignable<hasher>::value &&
1373 is_nothrow_move_assignable<key_equal>::value);
1374 _LIBCPP_INLINE_VISIBILITY
1375 void __move_assign_alloc(__hash_table& __u)
1376 _NOEXCEPT_(
1377 !__node_traits::propagate_on_container_move_assignment::value ||
1378 (is_nothrow_move_assignable<__pointer_allocator>::value &&
1379 is_nothrow_move_assignable<__node_allocator>::value))
Howard Hinnant3e519522010-05-11 19:42:16 +00001380 {__move_assign_alloc(__u, integral_constant<bool,
1381 __node_traits::propagate_on_container_move_assignment::value>());}
Howard Hinnant43d99232010-09-21 17:32:39 +00001382 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +00001383 void __move_assign_alloc(__hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001384 _NOEXCEPT_(
1385 is_nothrow_move_assignable<__pointer_allocator>::value &&
1386 is_nothrow_move_assignable<__node_allocator>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001387 {
1388 __bucket_list_.get_deleter().__alloc() =
Howard Hinnantce48a112011-06-30 21:18:19 +00001389 _VSTD::move(__u.__bucket_list_.get_deleter().__alloc());
1390 __node_alloc() = _VSTD::move(__u.__node_alloc());
Howard Hinnant3e519522010-05-11 19:42:16 +00001391 }
Howard Hinnant43d99232010-09-21 17:32:39 +00001392 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant37141072011-06-04 18:54:24 +00001393 void __move_assign_alloc(__hash_table&, false_type) _NOEXCEPT {}
Eric Fiselierfcd02212016-02-11 11:59:44 +00001394#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001395
Eric Fiseliercd71f442017-01-07 03:01:24 +00001396 void __deallocate_node(__next_pointer __np) _NOEXCEPT;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001397 __next_pointer __detach() _NOEXCEPT;
Howard Hinnant307f8142013-06-22 15:21:29 +00001398
Eric Fiseliere2f2d1e2017-01-04 23:56:00 +00001399 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_map;
1400 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS unordered_multimap;
Howard Hinnant3e519522010-05-11 19:42:16 +00001401};
1402
1403template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001404inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001405__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table()
Howard Hinnant37141072011-06-04 18:54:24 +00001406 _NOEXCEPT_(
1407 is_nothrow_default_constructible<__bucket_list>::value &&
1408 is_nothrow_default_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001409 is_nothrow_default_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001410 is_nothrow_default_constructible<hasher>::value &&
1411 is_nothrow_default_constructible<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001412 : __p2_(0),
1413 __p3_(1.0f)
1414{
1415}
1416
1417template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001418inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001419__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1420 const key_equal& __eql)
1421 : __bucket_list_(nullptr, __bucket_list_deleter()),
1422 __p1_(),
1423 __p2_(0, __hf),
1424 __p3_(1.0f, __eql)
1425{
1426}
1427
1428template <class _Tp, class _Hash, class _Equal, class _Alloc>
1429__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const hasher& __hf,
1430 const key_equal& __eql,
1431 const allocator_type& __a)
1432 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001433 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001434 __p2_(0, __hf),
1435 __p3_(1.0f, __eql)
1436{
1437}
1438
1439template <class _Tp, class _Hash, class _Equal, class _Alloc>
1440__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const allocator_type& __a)
1441 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001442 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001443 __p2_(0),
1444 __p3_(1.0f)
1445{
1446}
1447
1448template <class _Tp, class _Hash, class _Equal, class _Alloc>
1449__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u)
1450 : __bucket_list_(nullptr,
1451 __bucket_list_deleter(allocator_traits<__pointer_allocator>::
1452 select_on_container_copy_construction(
1453 __u.__bucket_list_.get_deleter().__alloc()), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001454 __p1_(__second_tag(), allocator_traits<__node_allocator>::
Howard Hinnant3e519522010-05-11 19:42:16 +00001455 select_on_container_copy_construction(__u.__node_alloc())),
1456 __p2_(0, __u.hash_function()),
1457 __p3_(__u.__p3_)
1458{
1459}
1460
1461template <class _Tp, class _Hash, class _Equal, class _Alloc>
1462__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(const __hash_table& __u,
1463 const allocator_type& __a)
1464 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001465 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnant3e519522010-05-11 19:42:16 +00001466 __p2_(0, __u.hash_function()),
1467 __p3_(__u.__p3_)
1468{
1469}
1470
Eric Fiselierfcd02212016-02-11 11:59:44 +00001471#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001472
1473template <class _Tp, class _Hash, class _Equal, class _Alloc>
1474__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001475 _NOEXCEPT_(
1476 is_nothrow_move_constructible<__bucket_list>::value &&
1477 is_nothrow_move_constructible<__first_node>::value &&
Eric Fiseliere2e332a2015-12-16 00:53:04 +00001478 is_nothrow_move_constructible<__node_allocator>::value &&
Howard Hinnant37141072011-06-04 18:54:24 +00001479 is_nothrow_move_constructible<hasher>::value &&
1480 is_nothrow_move_constructible<key_equal>::value)
Howard Hinnantce48a112011-06-30 21:18:19 +00001481 : __bucket_list_(_VSTD::move(__u.__bucket_list_)),
1482 __p1_(_VSTD::move(__u.__p1_)),
1483 __p2_(_VSTD::move(__u.__p2_)),
1484 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001485{
1486 if (size() > 0)
1487 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001488 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1489 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001490 __u.__p1_.first().__next_ = nullptr;
1491 __u.size() = 0;
1492 }
1493}
1494
1495template <class _Tp, class _Hash, class _Equal, class _Alloc>
1496__hash_table<_Tp, _Hash, _Equal, _Alloc>::__hash_table(__hash_table&& __u,
1497 const allocator_type& __a)
1498 : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
Eric Fiselierc88580c2017-04-12 23:45:53 +00001499 __p1_(__second_tag(), __node_allocator(__a)),
Howard Hinnantce48a112011-06-30 21:18:19 +00001500 __p2_(0, _VSTD::move(__u.hash_function())),
1501 __p3_(_VSTD::move(__u.__p3_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001502{
1503 if (__a == allocator_type(__u.__node_alloc()))
1504 {
1505 __bucket_list_.reset(__u.__bucket_list_.release());
1506 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1507 __u.__bucket_list_.get_deleter().size() = 0;
1508 if (__u.size() > 0)
1509 {
1510 __p1_.first().__next_ = __u.__p1_.first().__next_;
1511 __u.__p1_.first().__next_ = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001512 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1513 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001514 size() = __u.size();
1515 __u.size() = 0;
1516 }
1517 }
1518}
1519
Eric Fiselierfcd02212016-02-11 11:59:44 +00001520#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001521
1522template <class _Tp, class _Hash, class _Equal, class _Alloc>
1523__hash_table<_Tp, _Hash, _Equal, _Alloc>::~__hash_table()
1524{
Eric Fiselieracb21582017-03-01 02:02:28 +00001525#if defined(_LIBCPP_CXX03_LANG)
Marshall Clow3b8669e2016-06-30 22:05:45 +00001526 static_assert((is_copy_constructible<key_equal>::value),
1527 "Predicate must be copy-constructible.");
1528 static_assert((is_copy_constructible<hasher>::value),
1529 "Hasher must be copy-constructible.");
Eric Fiselier04333f92017-01-13 22:42:53 +00001530#endif
Eric Fiselieracb21582017-03-01 02:02:28 +00001531
Eric Fiseliercd71f442017-01-07 03:01:24 +00001532 __deallocate_node(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001533#if _LIBCPP_DEBUG_LEVEL >= 2
1534 __get_db()->__erase_c(this);
1535#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001536}
1537
1538template <class _Tp, class _Hash, class _Equal, class _Alloc>
1539void
1540__hash_table<_Tp, _Hash, _Equal, _Alloc>::__copy_assign_alloc(
1541 const __hash_table& __u, true_type)
1542{
1543 if (__node_alloc() != __u.__node_alloc())
1544 {
1545 clear();
1546 __bucket_list_.reset();
1547 __bucket_list_.get_deleter().size() = 0;
1548 }
1549 __bucket_list_.get_deleter().__alloc() = __u.__bucket_list_.get_deleter().__alloc();
1550 __node_alloc() = __u.__node_alloc();
1551}
1552
1553template <class _Tp, class _Hash, class _Equal, class _Alloc>
1554__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1555__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(const __hash_table& __u)
1556{
1557 if (this != &__u)
1558 {
1559 __copy_assign_alloc(__u);
1560 hash_function() = __u.hash_function();
1561 key_eq() = __u.key_eq();
1562 max_load_factor() = __u.max_load_factor();
1563 __assign_multi(__u.begin(), __u.end());
1564 }
1565 return *this;
1566}
1567
1568template <class _Tp, class _Hash, class _Equal, class _Alloc>
1569void
Eric Fiseliercd71f442017-01-07 03:01:24 +00001570__hash_table<_Tp, _Hash, _Equal, _Alloc>::__deallocate_node(__next_pointer __np)
Howard Hinnant37141072011-06-04 18:54:24 +00001571 _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001572{
1573 __node_allocator& __na = __node_alloc();
1574 while (__np != nullptr)
1575 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001576 __next_pointer __next = __np->__next_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00001577#if _LIBCPP_DEBUG_LEVEL >= 2
1578 __c_node* __c = __get_db()->__find_c_and_lock(this);
1579 for (__i_node** __p = __c->end_; __p != __c->beg_; )
1580 {
1581 --__p;
1582 iterator* __i = static_cast<iterator*>((*__p)->__i_);
1583 if (__i->__node_ == __np)
1584 {
1585 (*__p)->__c_ = nullptr;
1586 if (--__c->end_ != __p)
1587 memmove(__p, __p+1, (__c->end_ - __p)*sizeof(__i_node*));
1588 }
1589 }
1590 __get_db()->unlock();
1591#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00001592 __node_pointer __real_np = __np->__upcast();
1593 __node_traits::destroy(__na, _NodeTypes::__get_ptr(__real_np->__value_));
1594 __node_traits::deallocate(__na, __real_np, 1);
Howard Hinnant3e519522010-05-11 19:42:16 +00001595 __np = __next;
1596 }
1597}
1598
1599template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselier40492ba2016-07-23 20:36:55 +00001600typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__next_pointer
Howard Hinnant37141072011-06-04 18:54:24 +00001601__hash_table<_Tp, _Hash, _Equal, _Alloc>::__detach() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001602{
1603 size_type __bc = bucket_count();
1604 for (size_type __i = 0; __i < __bc; ++__i)
1605 __bucket_list_[__i] = nullptr;
1606 size() = 0;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001607 __next_pointer __cache = __p1_.first().__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00001608 __p1_.first().__next_ = nullptr;
1609 return __cache;
1610}
1611
Eric Fiselierfcd02212016-02-11 11:59:44 +00001612#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001613
1614template <class _Tp, class _Hash, class _Equal, class _Alloc>
1615void
1616__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1617 __hash_table& __u, true_type)
Howard Hinnant37141072011-06-04 18:54:24 +00001618 _NOEXCEPT_(
1619 is_nothrow_move_assignable<__node_allocator>::value &&
1620 is_nothrow_move_assignable<hasher>::value &&
1621 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001622{
1623 clear();
1624 __bucket_list_.reset(__u.__bucket_list_.release());
1625 __bucket_list_.get_deleter().size() = __u.__bucket_list_.get_deleter().size();
1626 __u.__bucket_list_.get_deleter().size() = 0;
1627 __move_assign_alloc(__u);
1628 size() = __u.size();
Howard Hinnantce48a112011-06-30 21:18:19 +00001629 hash_function() = _VSTD::move(__u.hash_function());
Howard Hinnant3e519522010-05-11 19:42:16 +00001630 max_load_factor() = __u.max_load_factor();
Howard Hinnantce48a112011-06-30 21:18:19 +00001631 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001632 __p1_.first().__next_ = __u.__p1_.first().__next_;
1633 if (size() > 0)
1634 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001635 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
1636 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001637 __u.__p1_.first().__next_ = nullptr;
1638 __u.size() = 0;
1639 }
Howard Hinnantb24c8022013-07-23 22:01:58 +00001640#if _LIBCPP_DEBUG_LEVEL >= 2
1641 __get_db()->swap(this, &__u);
1642#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001643}
1644
1645template <class _Tp, class _Hash, class _Equal, class _Alloc>
1646void
1647__hash_table<_Tp, _Hash, _Equal, _Alloc>::__move_assign(
1648 __hash_table& __u, false_type)
1649{
1650 if (__node_alloc() == __u.__node_alloc())
1651 __move_assign(__u, true_type());
1652 else
1653 {
Howard Hinnantce48a112011-06-30 21:18:19 +00001654 hash_function() = _VSTD::move(__u.hash_function());
1655 key_eq() = _VSTD::move(__u.key_eq());
Howard Hinnant3e519522010-05-11 19:42:16 +00001656 max_load_factor() = __u.max_load_factor();
1657 if (bucket_count() != 0)
1658 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001659 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001660#ifndef _LIBCPP_NO_EXCEPTIONS
1661 try
1662 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001663#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001664 const_iterator __i = __u.begin();
1665 while (__cache != nullptr && __u.size() != 0)
1666 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001667 __cache->__upcast()->__value_ =
1668 _VSTD::move(__u.remove(__i++)->__value_);
1669 __next_pointer __next = __cache->__next_;
1670 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001671 __cache = __next;
1672 }
1673#ifndef _LIBCPP_NO_EXCEPTIONS
1674 }
1675 catch (...)
1676 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001677 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001678 throw;
1679 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001680#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001681 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001682 }
1683 const_iterator __i = __u.begin();
1684 while (__u.size() != 0)
1685 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00001686 __node_holder __h = __construct_node(_NodeTypes::__move(__u.remove(__i++)->__value_));
Howard Hinnant3e519522010-05-11 19:42:16 +00001687 __node_insert_multi(__h.get());
1688 __h.release();
1689 }
1690 }
1691}
1692
1693template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001694inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001695__hash_table<_Tp, _Hash, _Equal, _Alloc>&
1696__hash_table<_Tp, _Hash, _Equal, _Alloc>::operator=(__hash_table&& __u)
Howard Hinnant37141072011-06-04 18:54:24 +00001697 _NOEXCEPT_(
1698 __node_traits::propagate_on_container_move_assignment::value &&
1699 is_nothrow_move_assignable<__node_allocator>::value &&
1700 is_nothrow_move_assignable<hasher>::value &&
1701 is_nothrow_move_assignable<key_equal>::value)
Howard Hinnant3e519522010-05-11 19:42:16 +00001702{
1703 __move_assign(__u, integral_constant<bool,
1704 __node_traits::propagate_on_container_move_assignment::value>());
1705 return *this;
1706}
1707
Eric Fiselierfcd02212016-02-11 11:59:44 +00001708#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00001709
1710template <class _Tp, class _Hash, class _Equal, class _Alloc>
1711template <class _InputIterator>
1712void
1713__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_unique(_InputIterator __first,
1714 _InputIterator __last)
1715{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001716 typedef iterator_traits<_InputIterator> _ITraits;
1717 typedef typename _ITraits::value_type _ItValueType;
1718 static_assert((is_same<_ItValueType, __container_value_type>::value),
1719 "__assign_unique may only be called with the containers value type");
1720
Howard Hinnant3e519522010-05-11 19:42:16 +00001721 if (bucket_count() != 0)
1722 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001723 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001724#ifndef _LIBCPP_NO_EXCEPTIONS
1725 try
1726 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001727#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001728 for (; __cache != nullptr && __first != __last; ++__first)
1729 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001730 __cache->__upcast()->__value_ = *__first;
1731 __next_pointer __next = __cache->__next_;
1732 __node_insert_unique(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001733 __cache = __next;
1734 }
1735#ifndef _LIBCPP_NO_EXCEPTIONS
1736 }
1737 catch (...)
1738 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001739 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001740 throw;
1741 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001742#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001743 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001744 }
1745 for (; __first != __last; ++__first)
1746 __insert_unique(*__first);
1747}
1748
1749template <class _Tp, class _Hash, class _Equal, class _Alloc>
1750template <class _InputIterator>
1751void
1752__hash_table<_Tp, _Hash, _Equal, _Alloc>::__assign_multi(_InputIterator __first,
1753 _InputIterator __last)
1754{
Eric Fiselierfcd02212016-02-11 11:59:44 +00001755 typedef iterator_traits<_InputIterator> _ITraits;
1756 typedef typename _ITraits::value_type _ItValueType;
1757 static_assert((is_same<_ItValueType, __container_value_type>::value ||
1758 is_same<_ItValueType, __node_value_type>::value),
1759 "__assign_multi may only be called with the containers value type"
1760 " or the nodes value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00001761 if (bucket_count() != 0)
1762 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001763 __next_pointer __cache = __detach();
Howard Hinnant3e519522010-05-11 19:42:16 +00001764#ifndef _LIBCPP_NO_EXCEPTIONS
1765 try
1766 {
Howard Hinnantb3371f62010-08-22 00:02:43 +00001767#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +00001768 for (; __cache != nullptr && __first != __last; ++__first)
1769 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001770 __cache->__upcast()->__value_ = *__first;
1771 __next_pointer __next = __cache->__next_;
1772 __node_insert_multi(__cache->__upcast());
Howard Hinnant3e519522010-05-11 19:42:16 +00001773 __cache = __next;
1774 }
1775#ifndef _LIBCPP_NO_EXCEPTIONS
1776 }
1777 catch (...)
1778 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001779 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001780 throw;
1781 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00001782#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiseliercd71f442017-01-07 03:01:24 +00001783 __deallocate_node(__cache);
Howard Hinnant3e519522010-05-11 19:42:16 +00001784 }
1785 for (; __first != __last; ++__first)
Eric Fiselierfcd02212016-02-11 11:59:44 +00001786 __insert_multi(_NodeTypes::__get_value(*__first));
Howard Hinnant3e519522010-05-11 19:42:16 +00001787}
1788
1789template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001790inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001791typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001792__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001793{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001794#if _LIBCPP_DEBUG_LEVEL >= 2
1795 return iterator(__p1_.first().__next_, this);
1796#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001797 return iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001798#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001799}
1800
1801template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001802inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001803typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001804__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001805{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001806#if _LIBCPP_DEBUG_LEVEL >= 2
1807 return iterator(nullptr, this);
1808#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001809 return iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001810#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001811}
1812
1813template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001814inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001815typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001816__hash_table<_Tp, _Hash, _Equal, _Alloc>::begin() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001817{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001818#if _LIBCPP_DEBUG_LEVEL >= 2
1819 return const_iterator(__p1_.first().__next_, this);
1820#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001821 return const_iterator(__p1_.first().__next_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001822#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001823}
1824
1825template <class _Tp, class _Hash, class _Equal, class _Alloc>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00001826inline
Howard Hinnant3e519522010-05-11 19:42:16 +00001827typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
Howard Hinnant37141072011-06-04 18:54:24 +00001828__hash_table<_Tp, _Hash, _Equal, _Alloc>::end() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001829{
Howard Hinnantb24c8022013-07-23 22:01:58 +00001830#if _LIBCPP_DEBUG_LEVEL >= 2
1831 return const_iterator(nullptr, this);
1832#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001833 return const_iterator(nullptr);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001834#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001835}
1836
1837template <class _Tp, class _Hash, class _Equal, class _Alloc>
1838void
Howard Hinnant37141072011-06-04 18:54:24 +00001839__hash_table<_Tp, _Hash, _Equal, _Alloc>::clear() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00001840{
1841 if (size() > 0)
1842 {
Eric Fiseliercd71f442017-01-07 03:01:24 +00001843 __deallocate_node(__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00001844 __p1_.first().__next_ = nullptr;
1845 size_type __bc = bucket_count();
Howard Hinnant1f8da842011-07-05 14:14:17 +00001846 for (size_type __i = 0; __i < __bc; ++__i)
Howard Hinnant3e519522010-05-11 19:42:16 +00001847 __bucket_list_[__i] = nullptr;
1848 size() = 0;
1849 }
1850}
1851
1852template <class _Tp, class _Hash, class _Equal, class _Alloc>
1853pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
1854__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_unique(__node_pointer __nd)
1855{
1856 __nd->__hash_ = hash_function()(__nd->__value_);
1857 size_type __bc = bucket_count();
1858 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001859 __next_pointer __ndptr;
Howard Hinnant3e519522010-05-11 19:42:16 +00001860 size_t __chash;
1861 if (__bc != 0)
1862 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001863 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001864 __ndptr = __bucket_list_[__chash];
1865 if (__ndptr != nullptr)
1866 {
1867 for (__ndptr = __ndptr->__next_; __ndptr != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001868 __constrain_hash(__ndptr->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001869 __ndptr = __ndptr->__next_)
1870 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001871 if (key_eq()(__ndptr->__upcast()->__value_, __nd->__value_))
Howard Hinnant3e519522010-05-11 19:42:16 +00001872 goto __done;
1873 }
1874 }
1875 }
1876 {
1877 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1878 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001879 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001880 size_type(ceil(float(size() + 1) / max_load_factor()))));
1881 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001882 __chash = __constrain_hash(__nd->__hash_, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001883 }
1884 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00001885 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001886 if (__pn == nullptr)
1887 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001888 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001889 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001890 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001891 // fix up __bucket_list_
1892 __bucket_list_[__chash] = __pn;
1893 if (__nd->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001894 __bucket_list_[__constrain_hash(__nd->__next_->__hash(), __bc)] = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001895 }
1896 else
1897 {
1898 __nd->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001899 __pn->__next_ = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001900 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00001901 __ndptr = __nd->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001902 // increment size
1903 ++size();
1904 __inserted = true;
1905 }
1906__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00001907#if _LIBCPP_DEBUG_LEVEL >= 2
1908 return pair<iterator, bool>(iterator(__ndptr, this), __inserted);
1909#else
Howard Hinnant3e519522010-05-11 19:42:16 +00001910 return pair<iterator, bool>(iterator(__ndptr), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001911#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001912}
1913
1914template <class _Tp, class _Hash, class _Equal, class _Alloc>
1915typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1916__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(__node_pointer __cp)
1917{
1918 __cp->__hash_ = hash_function()(__cp->__value_);
1919 size_type __bc = bucket_count();
1920 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1921 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001922 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001923 size_type(ceil(float(size() + 1) / max_load_factor()))));
1924 __bc = bucket_count();
1925 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001926 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001927 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001928 if (__pn == nullptr)
1929 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001930 __pn =__p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001931 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001932 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001933 // fix up __bucket_list_
1934 __bucket_list_[__chash] = __pn;
1935 if (__cp->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001936 __bucket_list_[__constrain_hash(__cp->__next_->__hash(), __bc)]
1937 = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001938 }
1939 else
1940 {
1941 for (bool __found = false; __pn->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00001942 __constrain_hash(__pn->__next_->__hash(), __bc) == __chash;
Howard Hinnant3e519522010-05-11 19:42:16 +00001943 __pn = __pn->__next_)
Howard Hinnantb3371f62010-08-22 00:02:43 +00001944 {
Howard Hinnant3e519522010-05-11 19:42:16 +00001945 // __found key_eq() action
1946 // false false loop
1947 // true true loop
1948 // false true set __found to true
1949 // true false break
Eric Fiselier40492ba2016-07-23 20:36:55 +00001950 if (__found != (__pn->__next_->__hash() == __cp->__hash_ &&
1951 key_eq()(__pn->__next_->__upcast()->__value_, __cp->__value_)))
Howard Hinnant3e519522010-05-11 19:42:16 +00001952 {
1953 if (!__found)
1954 __found = true;
1955 else
1956 break;
1957 }
1958 }
1959 __cp->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00001960 __pn->__next_ = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001961 if (__cp->__next_ != nullptr)
1962 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001963 size_t __nhash = __constrain_hash(__cp->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00001964 if (__nhash != __chash)
Eric Fiselier40492ba2016-07-23 20:36:55 +00001965 __bucket_list_[__nhash] = __cp->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00001966 }
1967 }
1968 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00001969#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00001970 return iterator(__cp->__ptr(), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00001971#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00001972 return iterator(__cp->__ptr());
Howard Hinnantb24c8022013-07-23 22:01:58 +00001973#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001974}
1975
1976template <class _Tp, class _Hash, class _Equal, class _Alloc>
1977typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
1978__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_insert_multi(
1979 const_iterator __p, __node_pointer __cp)
1980{
Howard Hinnant2f51de52013-08-02 17:50:49 +00001981#if _LIBCPP_DEBUG_LEVEL >= 2
1982 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
1983 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
1984 " referring to this unordered container");
1985#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00001986 if (__p != end() && key_eq()(*__p, __cp->__value_))
1987 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00001988 __next_pointer __np = __p.__node_;
1989 __cp->__hash_ = __np->__hash();
Howard Hinnant3e519522010-05-11 19:42:16 +00001990 size_type __bc = bucket_count();
1991 if (size()+1 > __bc * max_load_factor() || __bc == 0)
1992 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00001993 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00001994 size_type(ceil(float(size() + 1) / max_load_factor()))));
1995 __bc = bucket_count();
1996 }
Howard Hinnant4cb38a82012-07-06 17:31:14 +00001997 size_t __chash = __constrain_hash(__cp->__hash_, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00001998 __next_pointer __pp = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00001999 while (__pp->__next_ != __np)
2000 __pp = __pp->__next_;
2001 __cp->__next_ = __np;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002002 __pp->__next_ = static_cast<__next_pointer>(__cp);
Howard Hinnant3e519522010-05-11 19:42:16 +00002003 ++size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002004#if _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiselier40492ba2016-07-23 20:36:55 +00002005 return iterator(static_cast<__next_pointer>(__cp), this);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002006#else
Eric Fiselier40492ba2016-07-23 20:36:55 +00002007 return iterator(static_cast<__next_pointer>(__cp));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002008#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002009 }
2010 return __node_insert_multi(__cp);
2011}
2012
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002013
2014
Eric Fiselierfcd02212016-02-11 11:59:44 +00002015#ifndef _LIBCPP_CXX03_LANG
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002016template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002017template <class _Key, class ..._Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002018pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002019__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args&&... __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002020#else
2021template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002022template <class _Key, class _Args>
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002023pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002024__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
Eric Fiselierde3f2b32015-06-13 07:18:32 +00002025#endif
2026{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002027
2028 size_t __hash = hash_function()(__k);
Howard Hinnant3e519522010-05-11 19:42:16 +00002029 size_type __bc = bucket_count();
2030 bool __inserted = false;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002031 __next_pointer __nd;
Howard Hinnant3e519522010-05-11 19:42:16 +00002032 size_t __chash;
2033 if (__bc != 0)
2034 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002035 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002036 __nd = __bucket_list_[__chash];
2037 if (__nd != nullptr)
2038 {
2039 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier1a06fe52016-07-24 06:22:25 +00002040 (__nd->__hash() == __hash || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002041 __nd = __nd->__next_)
2042 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002043 if (key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnant3e519522010-05-11 19:42:16 +00002044 goto __done;
2045 }
2046 }
2047 }
2048 {
Eric Fiselierfcd02212016-02-11 11:59:44 +00002049#ifndef _LIBCPP_CXX03_LANG
2050 __node_holder __h = __construct_node_hash(__hash, _VSTD::forward<_Args>(__args)...);
2051#else
2052 __node_holder __h = __construct_node_hash(__hash, __args);
2053#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002054 if (size()+1 > __bc * max_load_factor() || __bc == 0)
2055 {
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002056 rehash(_VSTD::max<size_type>(2 * __bc + !__is_hash_power2(__bc),
Howard Hinnant3e519522010-05-11 19:42:16 +00002057 size_type(ceil(float(size() + 1) / max_load_factor()))));
2058 __bc = bucket_count();
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002059 __chash = __constrain_hash(__hash, __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002060 }
2061 // insert_after __bucket_list_[__chash], or __first_node if bucket is null
Eric Fiselier40492ba2016-07-23 20:36:55 +00002062 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002063 if (__pn == nullptr)
2064 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002065 __pn = __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002066 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002067 __pn->__next_ = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002068 // fix up __bucket_list_
2069 __bucket_list_[__chash] = __pn;
2070 if (__h->__next_ != nullptr)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002071 __bucket_list_[__constrain_hash(__h->__next_->__hash(), __bc)]
2072 = __h.get()->__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002073 }
2074 else
2075 {
2076 __h->__next_ = __pn->__next_;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002077 __pn->__next_ = static_cast<__next_pointer>(__h.get());
Howard Hinnant3e519522010-05-11 19:42:16 +00002078 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002079 __nd = static_cast<__next_pointer>(__h.release());
Howard Hinnant3e519522010-05-11 19:42:16 +00002080 // increment size
2081 ++size();
2082 __inserted = true;
2083 }
2084__done:
Howard Hinnantb24c8022013-07-23 22:01:58 +00002085#if _LIBCPP_DEBUG_LEVEL >= 2
2086 return pair<iterator, bool>(iterator(__nd, this), __inserted);
2087#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002088 return pair<iterator, bool>(iterator(__nd), __inserted);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002089#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002090}
2091
Eric Fiselierfcd02212016-02-11 11:59:44 +00002092#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002093
2094template <class _Tp, class _Hash, class _Equal, class _Alloc>
2095template <class... _Args>
2096pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
Duncan P. N. Exon Smithfde79b42016-03-17 20:45:20 +00002097__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_impl(_Args&&... __args)
Howard Hinnant3e519522010-05-11 19:42:16 +00002098{
Howard Hinnantce48a112011-06-30 21:18:19 +00002099 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002100 pair<iterator, bool> __r = __node_insert_unique(__h.get());
2101 if (__r.second)
2102 __h.release();
2103 return __r;
2104}
2105
2106template <class _Tp, class _Hash, class _Equal, class _Alloc>
2107template <class... _Args>
2108typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2109__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_multi(_Args&&... __args)
2110{
Howard Hinnantce48a112011-06-30 21:18:19 +00002111 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002112 iterator __r = __node_insert_multi(__h.get());
2113 __h.release();
2114 return __r;
2115}
2116
2117template <class _Tp, class _Hash, class _Equal, class _Alloc>
2118template <class... _Args>
2119typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2120__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_hint_multi(
2121 const_iterator __p, _Args&&... __args)
2122{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002123#if _LIBCPP_DEBUG_LEVEL >= 2
2124 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2125 "unordered container::emplace_hint(const_iterator, args...) called with an iterator not"
2126 " referring to this unordered container");
2127#endif
Howard Hinnantce48a112011-06-30 21:18:19 +00002128 __node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002129 iterator __r = __node_insert_multi(__p, __h.get());
2130 __h.release();
2131 return __r;
2132}
2133
Eric Fiselierfcd02212016-02-11 11:59:44 +00002134#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002135
2136template <class _Tp, class _Hash, class _Equal, class _Alloc>
2137typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
Eric Fiselierfcd02212016-02-11 11:59:44 +00002138__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002139{
2140 __node_holder __h = __construct_node(__x);
2141 iterator __r = __node_insert_multi(__h.get());
2142 __h.release();
2143 return __r;
2144}
2145
2146template <class _Tp, class _Hash, class _Equal, class _Alloc>
2147typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2148__hash_table<_Tp, _Hash, _Equal, _Alloc>::__insert_multi(const_iterator __p,
Eric Fiselierfcd02212016-02-11 11:59:44 +00002149 const __container_value_type& __x)
Howard Hinnant3e519522010-05-11 19:42:16 +00002150{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002151#if _LIBCPP_DEBUG_LEVEL >= 2
2152 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2153 "unordered container::insert(const_iterator, lvalue) called with an iterator not"
2154 " referring to this unordered container");
2155#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002156 __node_holder __h = __construct_node(__x);
2157 iterator __r = __node_insert_multi(__p, __h.get());
2158 __h.release();
2159 return __r;
2160}
2161
Eric Fiselierfcd02212016-02-11 11:59:44 +00002162#endif // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002163
Erik Pilkingtonb0386a52018-08-01 01:33:38 +00002164#if _LIBCPP_STD_VER > 14
2165template <class _Tp, class _Hash, class _Equal, class _Alloc>
2166template <class _NodeHandle, class _InsertReturnType>
2167_LIBCPP_INLINE_VISIBILITY
2168_InsertReturnType
2169__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2170 _NodeHandle&& __nh)
2171{
2172 if (__nh.empty())
2173 return _InsertReturnType{end(), false, _NodeHandle()};
2174 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2175 if (__result.second)
2176 __nh.__release();
2177 return _InsertReturnType{__result.first, __result.second, _VSTD::move(__nh)};
2178}
2179
2180template <class _Tp, class _Hash, class _Equal, class _Alloc>
2181template <class _NodeHandle>
2182_LIBCPP_INLINE_VISIBILITY
2183typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2184__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_unique(
2185 const_iterator, _NodeHandle&& __nh)
2186{
2187 if (__nh.empty())
2188 return end();
2189 pair<iterator, bool> __result = __node_insert_unique(__nh.__ptr_);
2190 if (__result.second)
2191 __nh.__release();
2192 return __result.first;
2193}
2194
2195template <class _Tp, class _Hash, class _Equal, class _Alloc>
2196template <class _NodeHandle>
2197_LIBCPP_INLINE_VISIBILITY
2198_NodeHandle
2199__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2200 key_type const& __key)
2201{
2202 iterator __i = find(__key);
2203 if (__i == end())
2204 return _NodeHandle();
2205 return __node_handle_extract<_NodeHandle>(__i);
2206}
2207
2208template <class _Tp, class _Hash, class _Equal, class _Alloc>
2209template <class _NodeHandle>
2210_LIBCPP_INLINE_VISIBILITY
2211_NodeHandle
2212__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(
2213 const_iterator __p)
2214{
2215 allocator_type __alloc(__node_alloc());
2216 return _NodeHandle(remove(__p).release(), __alloc);
2217}
2218
2219template <class _Tp, class _Hash, class _Equal, class _Alloc>
2220template <class _NodeHandle>
2221_LIBCPP_INLINE_VISIBILITY
2222typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2223__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2224 _NodeHandle&& __nh)
2225{
2226 if (__nh.empty())
2227 return end();
2228 iterator __result = __node_insert_multi(__nh.__ptr_);
2229 __nh.__release();
2230 return __result;
2231}
2232
2233template <class _Tp, class _Hash, class _Equal, class _Alloc>
2234template <class _NodeHandle>
2235_LIBCPP_INLINE_VISIBILITY
2236typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2237__hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_insert_multi(
2238 const_iterator __hint, _NodeHandle&& __nh)
2239{
2240 if (__nh.empty())
2241 return end();
2242 iterator __result = __node_insert_multi(__hint, __nh.__ptr_);
2243 __nh.__release();
2244 return __result;
2245}
2246
2247#endif // _LIBCPP_STD_VER > 14
2248
Howard Hinnant3e519522010-05-11 19:42:16 +00002249template <class _Tp, class _Hash, class _Equal, class _Alloc>
2250void
2251__hash_table<_Tp, _Hash, _Equal, _Alloc>::rehash(size_type __n)
2252{
Dan Albert553b09b2018-01-08 22:57:12 +00002253 if (__n == 1)
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002254 __n = 2;
2255 else if (__n & (__n - 1))
2256 __n = __next_prime(__n);
Howard Hinnant3e519522010-05-11 19:42:16 +00002257 size_type __bc = bucket_count();
2258 if (__n > __bc)
2259 __rehash(__n);
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002260 else if (__n < __bc)
Howard Hinnant3e519522010-05-11 19:42:16 +00002261 {
Howard Hinnantce48a112011-06-30 21:18:19 +00002262 __n = _VSTD::max<size_type>
Howard Hinnant3e519522010-05-11 19:42:16 +00002263 (
2264 __n,
Eric Fiselier9ba5c112015-02-02 21:31:48 +00002265 __is_hash_power2(__bc) ? __next_hash_pow2(size_t(ceil(float(size()) / max_load_factor()))) :
2266 __next_prime(size_t(ceil(float(size()) / max_load_factor())))
Howard Hinnant3e519522010-05-11 19:42:16 +00002267 );
2268 if (__n < __bc)
2269 __rehash(__n);
2270 }
2271}
2272
2273template <class _Tp, class _Hash, class _Equal, class _Alloc>
2274void
2275__hash_table<_Tp, _Hash, _Equal, _Alloc>::__rehash(size_type __nbc)
2276{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002277#if _LIBCPP_DEBUG_LEVEL >= 2
2278 __get_db()->__invalidate_all(this);
2279#endif // _LIBCPP_DEBUG_LEVEL >= 2
Howard Hinnant3e519522010-05-11 19:42:16 +00002280 __pointer_allocator& __npa = __bucket_list_.get_deleter().__alloc();
2281 __bucket_list_.reset(__nbc > 0 ?
2282 __pointer_alloc_traits::allocate(__npa, __nbc) : nullptr);
2283 __bucket_list_.get_deleter().size() = __nbc;
2284 if (__nbc > 0)
2285 {
2286 for (size_type __i = 0; __i < __nbc; ++__i)
2287 __bucket_list_[__i] = nullptr;
Eric Fiselier40492ba2016-07-23 20:36:55 +00002288 __next_pointer __pp = __p1_.first().__ptr();
2289 __next_pointer __cp = __pp->__next_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002290 if (__cp != nullptr)
2291 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002292 size_type __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002293 __bucket_list_[__chash] = __pp;
2294 size_type __phash = __chash;
2295 for (__pp = __cp, __cp = __cp->__next_; __cp != nullptr;
2296 __cp = __pp->__next_)
2297 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002298 __chash = __constrain_hash(__cp->__hash(), __nbc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002299 if (__chash == __phash)
2300 __pp = __cp;
2301 else
2302 {
2303 if (__bucket_list_[__chash] == nullptr)
2304 {
2305 __bucket_list_[__chash] = __pp;
2306 __pp = __cp;
2307 __phash = __chash;
2308 }
2309 else
2310 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002311 __next_pointer __np = __cp;
Howard Hinnant3e519522010-05-11 19:42:16 +00002312 for (; __np->__next_ != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002313 key_eq()(__cp->__upcast()->__value_,
2314 __np->__next_->__upcast()->__value_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002315 __np = __np->__next_)
2316 ;
2317 __pp->__next_ = __np->__next_;
2318 __np->__next_ = __bucket_list_[__chash]->__next_;
2319 __bucket_list_[__chash]->__next_ = __cp;
Howard Hinnantb3371f62010-08-22 00:02:43 +00002320
Howard Hinnant3e519522010-05-11 19:42:16 +00002321 }
2322 }
2323 }
2324 }
2325 }
2326}
2327
2328template <class _Tp, class _Hash, class _Equal, class _Alloc>
2329template <class _Key>
2330typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2331__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k)
2332{
2333 size_t __hash = hash_function()(__k);
2334 size_type __bc = bucket_count();
2335 if (__bc != 0)
2336 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002337 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002338 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002339 if (__nd != nullptr)
2340 {
2341 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002342 (__nd->__hash() == __hash
2343 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002344 __nd = __nd->__next_)
2345 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002346 if ((__nd->__hash() == __hash)
2347 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002348#if _LIBCPP_DEBUG_LEVEL >= 2
2349 return iterator(__nd, this);
2350#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002351 return iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002352#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002353 }
2354 }
2355 }
2356 return end();
2357}
2358
2359template <class _Tp, class _Hash, class _Equal, class _Alloc>
2360template <class _Key>
2361typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator
2362__hash_table<_Tp, _Hash, _Equal, _Alloc>::find(const _Key& __k) const
2363{
2364 size_t __hash = hash_function()(__k);
2365 size_type __bc = bucket_count();
2366 if (__bc != 0)
2367 {
Howard Hinnant4cb38a82012-07-06 17:31:14 +00002368 size_t __chash = __constrain_hash(__hash, __bc);
Eric Fiselier40492ba2016-07-23 20:36:55 +00002369 __next_pointer __nd = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002370 if (__nd != nullptr)
2371 {
2372 for (__nd = __nd->__next_; __nd != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002373 (__hash == __nd->__hash()
2374 || __constrain_hash(__nd->__hash(), __bc) == __chash);
Howard Hinnant3e519522010-05-11 19:42:16 +00002375 __nd = __nd->__next_)
2376 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002377 if ((__nd->__hash() == __hash)
2378 && key_eq()(__nd->__upcast()->__value_, __k))
Howard Hinnantb24c8022013-07-23 22:01:58 +00002379#if _LIBCPP_DEBUG_LEVEL >= 2
2380 return const_iterator(__nd, this);
2381#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002382 return const_iterator(__nd);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002383#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002384 }
2385 }
Howard Hinnantb3371f62010-08-22 00:02:43 +00002386
Howard Hinnant3e519522010-05-11 19:42:16 +00002387 }
2388 return end();
2389}
2390
Eric Fiselierfcd02212016-02-11 11:59:44 +00002391#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002392
2393template <class _Tp, class _Hash, class _Equal, class _Alloc>
2394template <class ..._Args>
2395typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
2396__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(_Args&& ...__args)
2397{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002398 static_assert(!__is_hash_value_type<_Args...>::value,
2399 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002400 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002401 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002402 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002403 __h.get_deleter().__value_constructed = true;
2404 __h->__hash_ = hash_function()(__h->__value_);
2405 __h->__next_ = nullptr;
2406 return __h;
2407}
2408
2409template <class _Tp, class _Hash, class _Equal, class _Alloc>
Eric Fiselierfcd02212016-02-11 11:59:44 +00002410template <class _First, class ..._Rest>
Howard Hinnant3e519522010-05-11 19:42:16 +00002411typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002412__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(
2413 size_t __hash, _First&& __f, _Rest&& ...__rest)
Howard Hinnant3e519522010-05-11 19:42:16 +00002414{
Eric Fiselierfcd02212016-02-11 11:59:44 +00002415 static_assert(!__is_hash_value_type<_First, _Rest...>::value,
2416 "Construct cannot be called with a hash value type");
Howard Hinnant3e519522010-05-11 19:42:16 +00002417 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002418 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002419 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_),
2420 _VSTD::forward<_First>(__f),
2421 _VSTD::forward<_Rest>(__rest)...);
Howard Hinnant3e519522010-05-11 19:42:16 +00002422 __h.get_deleter().__value_constructed = true;
2423 __h->__hash_ = __hash;
2424 __h->__next_ = nullptr;
Howard Hinnant179b1f82013-08-22 18:29:50 +00002425 return __h;
Howard Hinnant3e519522010-05-11 19:42:16 +00002426}
2427
Eric Fiselierfcd02212016-02-11 11:59:44 +00002428#else // _LIBCPP_CXX03_LANG
Howard Hinnant3e519522010-05-11 19:42:16 +00002429
2430template <class _Tp, class _Hash, class _Equal, class _Alloc>
2431typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002432__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node(const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002433{
2434 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002435 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002436 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002437 __h.get_deleter().__value_constructed = true;
2438 __h->__hash_ = hash_function()(__h->__value_);
2439 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002440 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002441}
2442
Howard Hinnant3e519522010-05-11 19:42:16 +00002443template <class _Tp, class _Hash, class _Equal, class _Alloc>
2444typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Eric Fiselierfcd02212016-02-11 11:59:44 +00002445__hash_table<_Tp, _Hash, _Equal, _Alloc>::__construct_node_hash(size_t __hash,
2446 const __container_value_type& __v)
Howard Hinnant3e519522010-05-11 19:42:16 +00002447{
2448 __node_allocator& __na = __node_alloc();
Howard Hinnantc003db12011-11-29 18:15:50 +00002449 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
Eric Fiselierfcd02212016-02-11 11:59:44 +00002450 __node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), __v);
Howard Hinnant3e519522010-05-11 19:42:16 +00002451 __h.get_deleter().__value_constructed = true;
2452 __h->__hash_ = __hash;
2453 __h->__next_ = nullptr;
Dimitry Andric251c6292015-08-19 06:43:33 +00002454 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
Howard Hinnant3e519522010-05-11 19:42:16 +00002455}
2456
Eric Fiselierfcd02212016-02-11 11:59:44 +00002457#endif // _LIBCPP_CXX03_LANG
2458
Howard Hinnant3e519522010-05-11 19:42:16 +00002459template <class _Tp, class _Hash, class _Equal, class _Alloc>
2460typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2461__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __p)
2462{
Eric Fiselier40492ba2016-07-23 20:36:55 +00002463 __next_pointer __np = __p.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002464#if _LIBCPP_DEBUG_LEVEL >= 2
2465 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__p) == this,
2466 "unordered container erase(iterator) called with an iterator not"
2467 " referring to this container");
2468 _LIBCPP_ASSERT(__p != end(),
2469 "unordered container erase(iterator) called with a non-dereferenceable iterator");
2470 iterator __r(__np, this);
2471#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002472 iterator __r(__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002473#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002474 ++__r;
2475 remove(__p);
2476 return __r;
2477}
2478
2479template <class _Tp, class _Hash, class _Equal, class _Alloc>
2480typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator
2481__hash_table<_Tp, _Hash, _Equal, _Alloc>::erase(const_iterator __first,
2482 const_iterator __last)
2483{
Howard Hinnantb24c8022013-07-23 22:01:58 +00002484#if _LIBCPP_DEBUG_LEVEL >= 2
2485 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__first) == this,
2486 "unodered container::erase(iterator, iterator) called with an iterator not"
2487 " referring to this unodered container");
2488 _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__last) == this,
2489 "unodered container::erase(iterator, iterator) called with an iterator not"
2490 " referring to this unodered container");
2491#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002492 for (const_iterator __p = __first; __first != __last; __p = __first)
2493 {
2494 ++__first;
2495 erase(__p);
2496 }
Eric Fiselier40492ba2016-07-23 20:36:55 +00002497 __next_pointer __np = __last.__node_;
Howard Hinnantb24c8022013-07-23 22:01:58 +00002498#if _LIBCPP_DEBUG_LEVEL >= 2
2499 return iterator (__np, this);
2500#else
Howard Hinnant3e519522010-05-11 19:42:16 +00002501 return iterator (__np);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002502#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002503}
2504
2505template <class _Tp, class _Hash, class _Equal, class _Alloc>
2506template <class _Key>
2507typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2508__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_unique(const _Key& __k)
2509{
2510 iterator __i = find(__k);
2511 if (__i == end())
2512 return 0;
2513 erase(__i);
2514 return 1;
2515}
2516
2517template <class _Tp, class _Hash, class _Equal, class _Alloc>
2518template <class _Key>
2519typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2520__hash_table<_Tp, _Hash, _Equal, _Alloc>::__erase_multi(const _Key& __k)
2521{
2522 size_type __r = 0;
2523 iterator __i = find(__k);
2524 if (__i != end())
2525 {
2526 iterator __e = end();
2527 do
2528 {
2529 erase(__i++);
2530 ++__r;
2531 } while (__i != __e && key_eq()(*__i, __k));
2532 }
2533 return __r;
2534}
2535
2536template <class _Tp, class _Hash, class _Equal, class _Alloc>
2537typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_holder
Howard Hinnant37141072011-06-04 18:54:24 +00002538__hash_table<_Tp, _Hash, _Equal, _Alloc>::remove(const_iterator __p) _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +00002539{
2540 // current node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002541 __next_pointer __cn = __p.__node_;
Howard Hinnant3e519522010-05-11 19:42:16 +00002542 size_type __bc = bucket_count();
Eric Fiselier40492ba2016-07-23 20:36:55 +00002543 size_t __chash = __constrain_hash(__cn->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002544 // find previous node
Eric Fiselier40492ba2016-07-23 20:36:55 +00002545 __next_pointer __pn = __bucket_list_[__chash];
Howard Hinnant3e519522010-05-11 19:42:16 +00002546 for (; __pn->__next_ != __cn; __pn = __pn->__next_)
2547 ;
2548 // Fix up __bucket_list_
2549 // if __pn is not in same bucket (before begin is not in same bucket) &&
2550 // if __cn->__next_ is not in same bucket (nullptr is not in same bucket)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002551 if (__pn == __p1_.first().__ptr()
2552 || __constrain_hash(__pn->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002553 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002554 if (__cn->__next_ == nullptr
2555 || __constrain_hash(__cn->__next_->__hash(), __bc) != __chash)
Howard Hinnant3e519522010-05-11 19:42:16 +00002556 __bucket_list_[__chash] = nullptr;
2557 }
2558 // if __cn->__next_ is not in same bucket (nullptr is in same bucket)
2559 if (__cn->__next_ != nullptr)
2560 {
Eric Fiselier40492ba2016-07-23 20:36:55 +00002561 size_t __nhash = __constrain_hash(__cn->__next_->__hash(), __bc);
Howard Hinnant3e519522010-05-11 19:42:16 +00002562 if (__nhash != __chash)
2563 __bucket_list_[__nhash] = __pn;
2564 }
2565 // remove __cn
2566 __pn->__next_ = __cn->__next_;
2567 __cn->__next_ = nullptr;
2568 --size();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002569#if _LIBCPP_DEBUG_LEVEL >= 2
2570 __c_node* __c = __get_db()->__find_c_and_lock(this);
Eric Fiselier780b51d2016-12-28 05:53:01 +00002571 for (__i_node** __dp = __c->end_; __dp != __c->beg_; )
Howard Hinnantb24c8022013-07-23 22:01:58 +00002572 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002573 --__dp;
2574 iterator* __i = static_cast<iterator*>((*__dp)->__i_);
Howard Hinnantb24c8022013-07-23 22:01:58 +00002575 if (__i->__node_ == __cn)
2576 {
Eric Fiselier780b51d2016-12-28 05:53:01 +00002577 (*__dp)->__c_ = nullptr;
2578 if (--__c->end_ != __dp)
2579 memmove(__dp, __dp+1, (__c->end_ - __dp)*sizeof(__i_node*));
Howard Hinnantb24c8022013-07-23 22:01:58 +00002580 }
2581 }
2582 __get_db()->unlock();
2583#endif
Eric Fiselier40492ba2016-07-23 20:36:55 +00002584 return __node_holder(__cn->__upcast(), _Dp(__node_alloc(), true));
Howard Hinnant3e519522010-05-11 19:42:16 +00002585}
2586
2587template <class _Tp, class _Hash, class _Equal, class _Alloc>
2588template <class _Key>
Evgeniy Stepanov906c8722015-11-07 01:22:13 +00002589inline
Howard Hinnant3e519522010-05-11 19:42:16 +00002590typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2591__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_unique(const _Key& __k) const
2592{
2593 return static_cast<size_type>(find(__k) != end());
2594}
2595
2596template <class _Tp, class _Hash, class _Equal, class _Alloc>
2597template <class _Key>
2598typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2599__hash_table<_Tp, _Hash, _Equal, _Alloc>::__count_multi(const _Key& __k) const
2600{
2601 size_type __r = 0;
2602 const_iterator __i = find(__k);
2603 if (__i != end())
2604 {
2605 const_iterator __e = end();
2606 do
2607 {
2608 ++__i;
2609 ++__r;
2610 } while (__i != __e && key_eq()(*__i, __k));
2611 }
2612 return __r;
2613}
2614
2615template <class _Tp, class _Hash, class _Equal, class _Alloc>
2616template <class _Key>
2617pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2618 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2619__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2620 const _Key& __k)
2621{
2622 iterator __i = find(__k);
2623 iterator __j = __i;
2624 if (__i != end())
2625 ++__j;
2626 return pair<iterator, iterator>(__i, __j);
2627}
2628
2629template <class _Tp, class _Hash, class _Equal, class _Alloc>
2630template <class _Key>
2631pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2632 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2633__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_unique(
2634 const _Key& __k) const
2635{
2636 const_iterator __i = find(__k);
2637 const_iterator __j = __i;
2638 if (__i != end())
2639 ++__j;
2640 return pair<const_iterator, const_iterator>(__i, __j);
2641}
2642
2643template <class _Tp, class _Hash, class _Equal, class _Alloc>
2644template <class _Key>
2645pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator,
2646 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator>
2647__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2648 const _Key& __k)
2649{
2650 iterator __i = find(__k);
2651 iterator __j = __i;
2652 if (__i != end())
2653 {
2654 iterator __e = end();
2655 do
2656 {
2657 ++__j;
2658 } while (__j != __e && key_eq()(*__j, __k));
2659 }
2660 return pair<iterator, iterator>(__i, __j);
2661}
2662
2663template <class _Tp, class _Hash, class _Equal, class _Alloc>
2664template <class _Key>
2665pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator,
2666 typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::const_iterator>
2667__hash_table<_Tp, _Hash, _Equal, _Alloc>::__equal_range_multi(
2668 const _Key& __k) const
2669{
2670 const_iterator __i = find(__k);
2671 const_iterator __j = __i;
2672 if (__i != end())
2673 {
2674 const_iterator __e = end();
2675 do
2676 {
2677 ++__j;
2678 } while (__j != __e && key_eq()(*__j, __k));
2679 }
2680 return pair<const_iterator, const_iterator>(__i, __j);
2681}
2682
2683template <class _Tp, class _Hash, class _Equal, class _Alloc>
2684void
2685__hash_table<_Tp, _Hash, _Equal, _Alloc>::swap(__hash_table& __u)
Eric Fiselier87a82492015-07-18 20:40:46 +00002686#if _LIBCPP_STD_VER <= 11
Eric Fiselier780b51d2016-12-28 05:53:01 +00002687 _NOEXCEPT_DEBUG_(
Marshall Clowe3fbe142015-07-13 20:04:56 +00002688 __is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value
Marshall Clowe3fbe142015-07-13 20:04:56 +00002689 && (!allocator_traits<__pointer_allocator>::propagate_on_container_swap::value
2690 || __is_nothrow_swappable<__pointer_allocator>::value)
2691 && (!__node_traits::propagate_on_container_swap::value
2692 || __is_nothrow_swappable<__node_allocator>::value)
Marshall Clowe3fbe142015-07-13 20:04:56 +00002693 )
Eric Fiselier87a82492015-07-18 20:40:46 +00002694#else
Eric Fiselier780b51d2016-12-28 05:53:01 +00002695 _NOEXCEPT_DEBUG_(__is_nothrow_swappable<hasher>::value && __is_nothrow_swappable<key_equal>::value)
Eric Fiselier87a82492015-07-18 20:40:46 +00002696#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002697{
Eric Fiselier780b51d2016-12-28 05:53:01 +00002698 _LIBCPP_ASSERT(__node_traits::propagate_on_container_swap::value ||
2699 this->__node_alloc() == __u.__node_alloc(),
2700 "list::swap: Either propagate_on_container_swap must be true"
2701 " or the allocators must compare equal");
Howard Hinnant3e519522010-05-11 19:42:16 +00002702 {
2703 __node_pointer_pointer __npp = __bucket_list_.release();
2704 __bucket_list_.reset(__u.__bucket_list_.release());
2705 __u.__bucket_list_.reset(__npp);
2706 }
Howard Hinnantce48a112011-06-30 21:18:19 +00002707 _VSTD::swap(__bucket_list_.get_deleter().size(), __u.__bucket_list_.get_deleter().size());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002708 __swap_allocator(__bucket_list_.get_deleter().__alloc(),
Howard Hinnant3e519522010-05-11 19:42:16 +00002709 __u.__bucket_list_.get_deleter().__alloc());
Marshall Clowe3fbe142015-07-13 20:04:56 +00002710 __swap_allocator(__node_alloc(), __u.__node_alloc());
Howard Hinnantce48a112011-06-30 21:18:19 +00002711 _VSTD::swap(__p1_.first().__next_, __u.__p1_.first().__next_);
Howard Hinnant3e519522010-05-11 19:42:16 +00002712 __p2_.swap(__u.__p2_);
2713 __p3_.swap(__u.__p3_);
2714 if (size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002715 __bucket_list_[__constrain_hash(__p1_.first().__next_->__hash(), bucket_count())] =
2716 __p1_.first().__ptr();
Howard Hinnant3e519522010-05-11 19:42:16 +00002717 if (__u.size() > 0)
Eric Fiselier40492ba2016-07-23 20:36:55 +00002718 __u.__bucket_list_[__constrain_hash(__u.__p1_.first().__next_->__hash(), __u.bucket_count())] =
2719 __u.__p1_.first().__ptr();
Howard Hinnantb24c8022013-07-23 22:01:58 +00002720#if _LIBCPP_DEBUG_LEVEL >= 2
2721 __get_db()->swap(this, &__u);
2722#endif
Howard Hinnant3e519522010-05-11 19:42:16 +00002723}
2724
2725template <class _Tp, class _Hash, class _Equal, class _Alloc>
2726typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::size_type
2727__hash_table<_Tp, _Hash, _Equal, _Alloc>::bucket_size(size_type __n) const
2728{
Howard Hinnante5c13de2013-07-29 19:05:47 +00002729 _LIBCPP_ASSERT(__n < bucket_count(),
2730 "unordered container::bucket_size(n) called with n >= bucket_count()");
Eric Fiselier40492ba2016-07-23 20:36:55 +00002731 __next_pointer __np = __bucket_list_[__n];
Howard Hinnant3e519522010-05-11 19:42:16 +00002732 size_type __bc = bucket_count();
2733 size_type __r = 0;
2734 if (__np != nullptr)
2735 {
2736 for (__np = __np->__next_; __np != nullptr &&
Eric Fiselier40492ba2016-07-23 20:36:55 +00002737 __constrain_hash(__np->__hash(), __bc) == __n;
Howard Hinnant3e519522010-05-11 19:42:16 +00002738 __np = __np->__next_, ++__r)
2739 ;
2740 }
2741 return __r;
2742}
2743
Howard Hinnant37141072011-06-04 18:54:24 +00002744template <class _Tp, class _Hash, class _Equal, class _Alloc>
2745inline _LIBCPP_INLINE_VISIBILITY
2746void
2747swap(__hash_table<_Tp, _Hash, _Equal, _Alloc>& __x,
2748 __hash_table<_Tp, _Hash, _Equal, _Alloc>& __y)
2749 _NOEXCEPT_(_NOEXCEPT_(__x.swap(__y)))
2750{
2751 __x.swap(__y);
2752}
2753
Howard Hinnantb24c8022013-07-23 22:01:58 +00002754#if _LIBCPP_DEBUG_LEVEL >= 2
2755
2756template <class _Tp, class _Hash, class _Equal, class _Alloc>
2757bool
2758__hash_table<_Tp, _Hash, _Equal, _Alloc>::__dereferenceable(const const_iterator* __i) const
2759{
2760 return __i->__node_ != nullptr;
2761}
2762
2763template <class _Tp, class _Hash, class _Equal, class _Alloc>
2764bool
2765__hash_table<_Tp, _Hash, _Equal, _Alloc>::__decrementable(const const_iterator*) const
2766{
2767 return false;
2768}
2769
2770template <class _Tp, class _Hash, class _Equal, class _Alloc>
2771bool
2772__hash_table<_Tp, _Hash, _Equal, _Alloc>::__addable(const const_iterator*, ptrdiff_t) const
2773{
2774 return false;
2775}
2776
2777template <class _Tp, class _Hash, class _Equal, class _Alloc>
2778bool
2779__hash_table<_Tp, _Hash, _Equal, _Alloc>::__subscriptable(const const_iterator*, ptrdiff_t) const
2780{
2781 return false;
2782}
2783
2784#endif // _LIBCPP_DEBUG_LEVEL >= 2
Eric Fiseliera016efb2017-05-31 22:07:49 +00002785
Howard Hinnant3e519522010-05-11 19:42:16 +00002786_LIBCPP_END_NAMESPACE_STD
2787
Eric Fiseliera016efb2017-05-31 22:07:49 +00002788_LIBCPP_POP_MACROS
2789
Howard Hinnant3e519522010-05-11 19:42:16 +00002790#endif // _LIBCPP__HASH_TABLE